-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpeg_trans.cpp
207 lines (174 loc) · 6.49 KB
/
jpeg_trans.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Author: mlckq <[email protected]>
* File: jpeg_trans.cpp
* Create Date: 2014-09-05 12:35
*/
#include "jpeg_trans.h"
#include <cstring>
#define BLOCK_SIZE 16384
#define QUALITY 80
using std::vector;
struct DestWithBuff {
jpeg_destination_mgr dest;
BuffType *buff;
};
BuffType compress_to_data(const GrayImage& img_data) {
return compress_to_data(&img_data.data[0], img_data.width, img_data.height);
}
BuffType compress_to_data(JOCTET const* img_data, int width, int height) {
jpeg_compress_struct cinfo;
jpeg_error_mgr jerr;
BuffType buff;
JSAMPROW row_pointer[1];
int row_stride;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 1;
cinfo.in_color_space = JCS_GRAYSCALE;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 80, true);
DestWithBuff *dest = (DestWithBuff*)
(* cinfo.mem->alloc_small)((j_common_ptr)&cinfo, JPOOL_PERMANENT, sizeof(DestWithBuff));
dest->buff = &buff;
cinfo.dest = (jpeg_destination_mgr*) dest;
cinfo.dest->init_destination = [](j_compress_ptr c) {
BuffType &buff = * ((DestWithBuff*) c->dest)->buff;
buff.resize(BLOCK_SIZE);
c->dest->next_output_byte = buff.data();
c->dest->free_in_buffer = buff.size();
};
cinfo.dest->empty_output_buffer = [](j_compress_ptr c) -> boolean {
BuffType &buff = * ((DestWithBuff*) c->dest)->buff;
size_t old_size = buff.size();
buff.resize(old_size + BLOCK_SIZE);
c->dest->next_output_byte = buff.data() + old_size;
c->dest->free_in_buffer = buff.size() - old_size;
return true;
};
cinfo.dest->term_destination = [](j_compress_ptr c) {
BuffType &buff = * ((DestWithBuff*) c->dest)->buff;
buff.resize(buff.size() - c->dest->free_in_buffer);
};
jpeg_start_compress(&cinfo, true);
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = const_cast<JOCTET*>(&img_data[cinfo.next_scanline * width]);
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return buff;
}
GrayImage decompress_from_data(const BuffType& jpg_data) {
return decompress_from_data(&jpg_data[0], jpg_data.size());
}
BuffType compress_yuv420(JOCTET const* yuv_data, int width, int height) {
jpeg_compress_struct cinfo;
jpeg_error_mgr jerr;
BuffType buff;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
jpeg_set_defaults(&cinfo);
jpeg_set_colorspace(&cinfo, JCS_YCbCr);
#if JPEG_LIB_VERSION >= 70
cinfo.do_fancy_downsampling = FALSE;
#endif
cinfo.raw_data_in = TRUE;
cinfo.comp_info[0].h_samp_factor = 2;
cinfo.comp_info[0].v_samp_factor = 2;
cinfo.comp_info[1].h_samp_factor = 1;
cinfo.comp_info[1].v_samp_factor = 1;
cinfo.comp_info[2].h_samp_factor = 1;
cinfo.comp_info[2].v_samp_factor = 1;
jpeg_set_quality(&cinfo, 80, true);
cinfo.dct_method = JDCT_FASTEST;
DestWithBuff *dest = (DestWithBuff*)
(* cinfo.mem->alloc_small)((j_common_ptr)&cinfo, JPOOL_PERMANENT, sizeof(DestWithBuff));
dest->buff = &buff;
cinfo.dest = (jpeg_destination_mgr*) dest;
cinfo.dest->init_destination = [](j_compress_ptr c) {
BuffType &buff = * ((DestWithBuff*) c->dest)->buff;
buff.resize(BLOCK_SIZE);
c->dest->next_output_byte = buff.data();
c->dest->free_in_buffer = buff.size();
};
cinfo.dest->empty_output_buffer = [](j_compress_ptr c) -> boolean {
BuffType &buff = * ((DestWithBuff*) c->dest)->buff;
size_t old_size = buff.size();
buff.resize(old_size + BLOCK_SIZE);
c->dest->next_output_byte = buff.data() + old_size;
c->dest->free_in_buffer = buff.size() - old_size;
return true;
};
cinfo.dest->term_destination = [](j_compress_ptr c) {
BuffType &buff = * ((DestWithBuff*) c->dest)->buff;
buff.resize(buff.size() - c->dest->free_in_buffer);
};
jpeg_start_compress(&cinfo, true);
JSAMPROW y[16];
JSAMPROW cb[8];
JSAMPROW cr[8];
JSAMPARRAY data[3];
data[0] = y;
data[1] = cr;
data[2] = cb;
int ysz = width * height;
JOCTET* data_src = const_cast<JOCTET*>(yuv_data);
for (int j = 0; j < height; j += 16) {
for (int i = 0; i < 16; ++ i) {
y[i] = data_src + width * (i + j);
if (i % 2 == 0) {
cb[i >> 1] = data_src + ysz + (width >> 1) * ((i + j) >> 1);
cr[i >> 1] = data_src + ysz + (ysz >> 2) + (width >> 1) * ((i + j) >> 1);
}
}
jpeg_write_raw_data(&cinfo, data, 16);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return buff;
}
GrayImage decompress_from_data(JOCTET const* jpg_data, size_t size) {
GrayImage img;
jpeg_decompress_struct cinfo;
jpeg_error_mgr jerr;
JSAMPARRAY buffer;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
cinfo.src = (jpeg_source_mgr *)
(* cinfo.mem->alloc_small) ((j_common_ptr)&cinfo, JPOOL_PERMANENT, sizeof(jpeg_source_mgr));
cinfo.src->init_source = [](j_decompress_ptr c) { };
cinfo.src->fill_input_buffer = [](j_decompress_ptr c) -> boolean {
return true;
};
cinfo.src->skip_input_data = [](j_decompress_ptr c, long num_bytes) {
if (num_bytes > 0) {
c->src->next_input_byte += num_bytes;
c->src->bytes_in_buffer -= num_bytes;
}
};
cinfo.src->term_source = [](j_decompress_ptr c) {};
cinfo.src->resync_to_restart = jpeg_resync_to_restart;
cinfo.src->bytes_in_buffer = size;
cinfo.src->next_input_byte = const_cast<JOCTET*>(jpg_data);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
img.width = cinfo.output_width;
img.height = cinfo.output_height;
img.channel = cinfo.output_components;
img.data.resize(img.width * img.height * img.channel);
int stride = img.width * img.channel;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, stride, 1);
for (int i = 0; cinfo.output_scanline < cinfo.output_height; ++i) {
jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(img.data.data() + i * stride, buffer[0], stride);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return img;
}