blob: 0185e55e9edc800c6907df6de280c22867eb827a [file] [log] [blame]
Dichen Zhang0d12ba82022-10-19 20:44:51 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <jpegrecoverymap/jpegdecoder.h>
18
19#include <cutils/log.h>
20
21#include <errno.h>
22#include <setjmp.h>
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000023#include <string>
24
25using namespace std;
Dichen Zhang0d12ba82022-10-19 20:44:51 +000026
27namespace android::recoverymap {
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000028
29const uint32_t kExifMarker = JPEG_APP0 + 1;
30const uint32_t kICCMarker = JPEG_APP0 + 2;
31
Dichen Zhang0d12ba82022-10-19 20:44:51 +000032struct jpegr_source_mgr : jpeg_source_mgr {
33 jpegr_source_mgr(const uint8_t* ptr, int len);
34 ~jpegr_source_mgr();
35
36 const uint8_t* mBufferPtr;
37 size_t mBufferLength;
38};
39
40struct jpegrerror_mgr {
41 struct jpeg_error_mgr pub;
42 jmp_buf setjmp_buffer;
43};
44
45static void jpegr_init_source(j_decompress_ptr cinfo) {
46 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
47 src->next_input_byte = static_cast<const JOCTET*>(src->mBufferPtr);
48 src->bytes_in_buffer = src->mBufferLength;
49}
50
51static boolean jpegr_fill_input_buffer(j_decompress_ptr /* cinfo */) {
52 ALOGE("%s : should not get here", __func__);
53 return FALSE;
54}
55
56static void jpegr_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
57 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
58
59 if (num_bytes > static_cast<long>(src->bytes_in_buffer)) {
60 ALOGE("jpegr_skip_input_data - num_bytes > (long)src->bytes_in_buffer");
61 } else {
62 src->next_input_byte += num_bytes;
63 src->bytes_in_buffer -= num_bytes;
64 }
65}
66
67static void jpegr_term_source(j_decompress_ptr /*cinfo*/) {}
68
69jpegr_source_mgr::jpegr_source_mgr(const uint8_t* ptr, int len) :
70 mBufferPtr(ptr), mBufferLength(len) {
71 init_source = jpegr_init_source;
72 fill_input_buffer = jpegr_fill_input_buffer;
73 skip_input_data = jpegr_skip_input_data;
74 resync_to_restart = jpeg_resync_to_restart;
75 term_source = jpegr_term_source;
76}
77
78jpegr_source_mgr::~jpegr_source_mgr() {}
79
80static void jpegrerror_exit(j_common_ptr cinfo) {
81 jpegrerror_mgr* err = reinterpret_cast<jpegrerror_mgr*>(cinfo->err);
82 longjmp(err->setjmp_buffer, 1);
83}
84
85JpegDecoder::JpegDecoder() {
86}
87
88JpegDecoder::~JpegDecoder() {
89}
90
91bool JpegDecoder::decompressImage(const void* image, int length) {
92 if (image == nullptr || length <= 0) {
93 ALOGE("Image size can not be handled: %d", length);
94 return false;
95 }
96
97 mResultBuffer.clear();
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000098 mXMPBuffer.clear();
Dichen Zhang0d12ba82022-10-19 20:44:51 +000099 if (!decode(image, length)) {
100 return false;
101 }
102
103 return true;
104}
105
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400106void* JpegDecoder::getDecompressedImagePtr() {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000107 return mResultBuffer.data();
108}
109
110size_t JpegDecoder::getDecompressedImageSize() {
111 return mResultBuffer.size();
112}
113
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000114void* JpegDecoder::getXMPPtr() {
115 return mXMPBuffer.data();
116}
117
118size_t JpegDecoder::getXMPSize() {
119 return mXMPBuffer.size();
120}
121
122
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400123size_t JpegDecoder::getDecompressedImageWidth() {
124 return mWidth;
125}
126
127size_t JpegDecoder::getDecompressedImageHeight() {
128 return mHeight;
129}
130
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000131bool JpegDecoder::decode(const void* image, int length) {
132 jpeg_decompress_struct cinfo;
133 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
134 jpegrerror_mgr myerr;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000135 string nameSpace = "http://ns.adobe.com/xap/1.0/";
136
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000137 cinfo.err = jpeg_std_error(&myerr.pub);
138 myerr.pub.error_exit = jpegrerror_exit;
139
140 if (setjmp(myerr.setjmp_buffer)) {
141 jpeg_destroy_decompress(&cinfo);
142 return false;
143 }
144 jpeg_create_decompress(&cinfo);
145
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000146 jpeg_save_markers(&cinfo, kExifMarker, 0xFFFF);
147
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000148 cinfo.src = &mgr;
149 jpeg_read_header(&cinfo, TRUE);
150
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000151 // Save XMP Data
152 for (jpeg_marker_struct* marker = cinfo.marker_list; marker; marker = marker->next) {
153 if (marker->marker == kExifMarker) {
154 const unsigned int len = marker->data_length;
155 if (len > nameSpace.size() &&
156 !strncmp(reinterpret_cast<const char*>(marker->data),
157 nameSpace.c_str(), nameSpace.size())) {
158 mXMPBuffer.resize(len+1, 0);
159 memcpy(static_cast<void*>(mXMPBuffer.data()), marker->data, len);
160 break;
161 }
162 }
163 }
164
165
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400166 mWidth = cinfo.image_width;
167 mHeight = cinfo.image_height;
168
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000169 if (cinfo.jpeg_color_space == JCS_YCbCr) {
170 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 3 / 2, 0);
171 } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
172 mResultBuffer.resize(cinfo.image_width * cinfo.image_height, 0);
173 }
174
175 cinfo.raw_data_out = TRUE;
176 cinfo.dct_method = JDCT_IFAST;
177 cinfo.out_color_space = cinfo.jpeg_color_space;
178
179 jpeg_start_decompress(&cinfo);
180
181 if (!decompress(&cinfo, static_cast<const uint8_t*>(mResultBuffer.data()),
182 cinfo.jpeg_color_space == JCS_GRAYSCALE)) {
183 return false;
184 }
185
186 jpeg_finish_decompress(&cinfo);
187 jpeg_destroy_decompress(&cinfo);
188
189 return true;
190}
191
192bool JpegDecoder::decompress(jpeg_decompress_struct* cinfo, const uint8_t* dest,
193 bool isSingleChannel) {
194 if (isSingleChannel) {
195 return decompressSingleChannel(cinfo, dest);
196 }
197 return decompressYUV(cinfo, dest);
198}
199
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000200bool JpegDecoder::getCompressedImageParameters(const void* image, int length,
201 size_t *pWidth, size_t *pHeight,
202 std::vector<uint8_t> *&iccData , std::vector<uint8_t> *&exifData) {
203 jpeg_decompress_struct cinfo;
204 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
205 jpegrerror_mgr myerr;
206 cinfo.err = jpeg_std_error(&myerr.pub);
207 myerr.pub.error_exit = jpegrerror_exit;
208
209 if (setjmp(myerr.setjmp_buffer)) {
210 jpeg_destroy_decompress(&cinfo);
211 return false;
212 }
213 jpeg_create_decompress(&cinfo);
214
215 jpeg_save_markers(&cinfo, kExifMarker, 0xFFFF);
216 jpeg_save_markers(&cinfo, kICCMarker, 0xFFFF);
217
218 cinfo.src = &mgr;
219 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
220 jpeg_destroy_decompress(&cinfo);
221 return false;
222 }
223
224 *pWidth = cinfo.image_width;
225 *pHeight = cinfo.image_height;
226
227 //TODO: Parse iccProfile and exifData
228 (void)iccData;
229 (void)exifData;
230
231
232 jpeg_destroy_decompress(&cinfo);
233 return true;
234}
235
236
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000237bool JpegDecoder::decompressYUV(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
238
239 JSAMPROW y[kCompressBatchSize];
240 JSAMPROW cb[kCompressBatchSize / 2];
241 JSAMPROW cr[kCompressBatchSize / 2];
242 JSAMPARRAY planes[3] {y, cb, cr};
243
244 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
245 size_t uv_plane_size = y_plane_size / 4;
246 uint8_t* y_plane = const_cast<uint8_t*>(dest);
247 uint8_t* u_plane = const_cast<uint8_t*>(dest + y_plane_size);
248 uint8_t* v_plane = const_cast<uint8_t*>(dest + y_plane_size + uv_plane_size);
249 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
250 memset(empty.get(), 0, cinfo->image_width);
251
252 while (cinfo->output_scanline < cinfo->image_height) {
253 for (int i = 0; i < kCompressBatchSize; ++i) {
254 size_t scanline = cinfo->output_scanline + i;
255 if (scanline < cinfo->image_height) {
256 y[i] = y_plane + scanline * cinfo->image_width;
257 } else {
258 y[i] = empty.get();
259 }
260 }
261 // cb, cr only have half scanlines
262 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
263 size_t scanline = cinfo->output_scanline / 2 + i;
264 if (scanline < cinfo->image_height / 2) {
265 int offset = scanline * (cinfo->image_width / 2);
266 cb[i] = u_plane + offset;
267 cr[i] = v_plane + offset;
268 } else {
269 cb[i] = cr[i] = empty.get();
270 }
271 }
272
273 int processed = jpeg_read_raw_data(cinfo, planes, kCompressBatchSize);
274 if (processed != kCompressBatchSize) {
275 ALOGE("Number of processed lines does not equal input lines.");
276 return false;
277 }
278 }
279 return true;
280}
281
282bool JpegDecoder::decompressSingleChannel(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
283 JSAMPROW y[kCompressBatchSize];
284 JSAMPARRAY planes[1] {y};
285
286 uint8_t* y_plane = const_cast<uint8_t*>(dest);
287 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
288 memset(empty.get(), 0, cinfo->image_width);
289
290 while (cinfo->output_scanline < cinfo->image_height) {
291 for (int i = 0; i < kCompressBatchSize; ++i) {
292 size_t scanline = cinfo->output_scanline + i;
293 if (scanline < cinfo->image_height) {
294 y[i] = y_plane + scanline * cinfo->image_width;
295 } else {
296 y[i] = empty.get();
297 }
298 }
299
300 int processed = jpeg_read_raw_data(cinfo, planes, kCompressBatchSize);
301 if (processed != kCompressBatchSize / 2) {
302 ALOGE("Number of processed lines does not equal input lines.");
303 return false;
304 }
305 }
306 return true;
307}
308
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400309} // namespace android