blob: c2a8f45dbd3d2b24b7c843dc59785d3b23cf4f2a [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
Dichen Zhangd18bc302022-12-16 20:55:24 +000029const uint32_t kAPP0Marker = JPEG_APP0; // JFIF
30const uint32_t kAPP1Marker = JPEG_APP0 + 1; // EXIF, XMP
31const uint32_t kAPP2Marker = JPEG_APP0 + 2; // ICC
32
33const std::string kXmpNameSpace = "http://ns.adobe.com/xap/1.0/";
34const std::string kExifIdCode = "Exif";
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000035
Dichen Zhang0d12ba82022-10-19 20:44:51 +000036struct jpegr_source_mgr : jpeg_source_mgr {
37 jpegr_source_mgr(const uint8_t* ptr, int len);
38 ~jpegr_source_mgr();
39
40 const uint8_t* mBufferPtr;
41 size_t mBufferLength;
42};
43
44struct jpegrerror_mgr {
45 struct jpeg_error_mgr pub;
46 jmp_buf setjmp_buffer;
47};
48
49static void jpegr_init_source(j_decompress_ptr cinfo) {
50 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
51 src->next_input_byte = static_cast<const JOCTET*>(src->mBufferPtr);
52 src->bytes_in_buffer = src->mBufferLength;
53}
54
55static boolean jpegr_fill_input_buffer(j_decompress_ptr /* cinfo */) {
56 ALOGE("%s : should not get here", __func__);
57 return FALSE;
58}
59
60static void jpegr_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
61 jpegr_source_mgr* src = static_cast<jpegr_source_mgr*>(cinfo->src);
62
63 if (num_bytes > static_cast<long>(src->bytes_in_buffer)) {
64 ALOGE("jpegr_skip_input_data - num_bytes > (long)src->bytes_in_buffer");
65 } else {
66 src->next_input_byte += num_bytes;
67 src->bytes_in_buffer -= num_bytes;
68 }
69}
70
71static void jpegr_term_source(j_decompress_ptr /*cinfo*/) {}
72
73jpegr_source_mgr::jpegr_source_mgr(const uint8_t* ptr, int len) :
74 mBufferPtr(ptr), mBufferLength(len) {
75 init_source = jpegr_init_source;
76 fill_input_buffer = jpegr_fill_input_buffer;
77 skip_input_data = jpegr_skip_input_data;
78 resync_to_restart = jpeg_resync_to_restart;
79 term_source = jpegr_term_source;
80}
81
82jpegr_source_mgr::~jpegr_source_mgr() {}
83
84static void jpegrerror_exit(j_common_ptr cinfo) {
85 jpegrerror_mgr* err = reinterpret_cast<jpegrerror_mgr*>(cinfo->err);
86 longjmp(err->setjmp_buffer, 1);
87}
88
89JpegDecoder::JpegDecoder() {
Dichen Zhangd18bc302022-12-16 20:55:24 +000090 mExifPos = 0;
Dichen Zhang0d12ba82022-10-19 20:44:51 +000091}
92
93JpegDecoder::~JpegDecoder() {
94}
95
96bool JpegDecoder::decompressImage(const void* image, int length) {
97 if (image == nullptr || length <= 0) {
98 ALOGE("Image size can not be handled: %d", length);
99 return false;
100 }
101
102 mResultBuffer.clear();
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000103 mXMPBuffer.clear();
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000104 if (!decode(image, length)) {
105 return false;
106 }
107
108 return true;
109}
110
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400111void* JpegDecoder::getDecompressedImagePtr() {
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000112 return mResultBuffer.data();
113}
114
115size_t JpegDecoder::getDecompressedImageSize() {
116 return mResultBuffer.size();
117}
118
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000119void* JpegDecoder::getXMPPtr() {
120 return mXMPBuffer.data();
121}
122
123size_t JpegDecoder::getXMPSize() {
124 return mXMPBuffer.size();
125}
126
Dichen Zhangd18bc302022-12-16 20:55:24 +0000127void* JpegDecoder::getEXIFPtr() {
128 return mEXIFBuffer.data();
129}
130
131size_t JpegDecoder::getEXIFSize() {
132 return mEXIFBuffer.size();
133}
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000134
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400135size_t JpegDecoder::getDecompressedImageWidth() {
136 return mWidth;
137}
138
139size_t JpegDecoder::getDecompressedImageHeight() {
140 return mHeight;
141}
142
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000143bool JpegDecoder::decode(const void* image, int length) {
144 jpeg_decompress_struct cinfo;
145 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
146 jpegrerror_mgr myerr;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000147
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000148 cinfo.err = jpeg_std_error(&myerr.pub);
149 myerr.pub.error_exit = jpegrerror_exit;
150
151 if (setjmp(myerr.setjmp_buffer)) {
152 jpeg_destroy_decompress(&cinfo);
153 return false;
154 }
155 jpeg_create_decompress(&cinfo);
156
Dichen Zhangd18bc302022-12-16 20:55:24 +0000157 jpeg_save_markers(&cinfo, kAPP0Marker, 0xFFFF);
158 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
159 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000160
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000161 cinfo.src = &mgr;
162 jpeg_read_header(&cinfo, TRUE);
163
Dichen Zhangd18bc302022-12-16 20:55:24 +0000164 // Save XMP data and EXIF data.
165 // Here we only handle the first XMP / EXIF package.
166 // The parameter pos is used for capturing start offset of EXIF, which is hacky, but working...
167 // We assume that all packages are starting with two bytes marker (eg FF E1 for EXIF package),
168 // two bytes of package length which is stored in marker->original_length, and the real data
169 // which is stored in marker->data. The pos is adding up all previous package lengths (
170 // 4 bytes marker and length, marker->original_length) before EXIF appears. Note that here we
171 // we are using marker->original_length instead of marker->data_length because in case the real
172 // package length is larger than the limitation, jpeg-turbo will only copy the data within the
173 // limitation (represented by data_length) and this may vary from original_length / real offset.
174 // A better solution is making jpeg_marker_struct holding the offset, but currently it doesn't.
175 bool exifAppears = false;
176 bool xmpAppears = false;
177 size_t pos = 2; // position after SOI
178 for (jpeg_marker_struct* marker = cinfo.marker_list;
179 marker && !(exifAppears && xmpAppears);
180 marker = marker->next) {
181
182 pos += 4;
183 pos += marker->original_length;
184
185 if (marker->marker != kAPP1Marker) {
186 continue;
187 }
188
189 const unsigned int len = marker->data_length;
190 if (!xmpAppears &&
191 len > kXmpNameSpace.size() &&
192 !strncmp(reinterpret_cast<const char*>(marker->data),
193 kXmpNameSpace.c_str(),
194 kXmpNameSpace.size())) {
195 mXMPBuffer.resize(len+1, 0);
196 memcpy(static_cast<void*>(mXMPBuffer.data()), marker->data, len);
197 xmpAppears = true;
198 } else if (!exifAppears &&
199 len > kExifIdCode.size() &&
200 !strncmp(reinterpret_cast<const char*>(marker->data),
201 kExifIdCode.c_str(),
202 kExifIdCode.size())) {
203 mEXIFBuffer.resize(len, 0);
204 memcpy(static_cast<void*>(mEXIFBuffer.data()), marker->data, len);
205 exifAppears = true;
206 mExifPos = pos - marker->original_length;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000207 }
208 }
209
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400210 mWidth = cinfo.image_width;
211 mHeight = cinfo.image_height;
212
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000213 if (cinfo.jpeg_color_space == JCS_YCbCr) {
214 mResultBuffer.resize(cinfo.image_width * cinfo.image_height * 3 / 2, 0);
215 } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
216 mResultBuffer.resize(cinfo.image_width * cinfo.image_height, 0);
217 }
218
219 cinfo.raw_data_out = TRUE;
220 cinfo.dct_method = JDCT_IFAST;
221 cinfo.out_color_space = cinfo.jpeg_color_space;
222
223 jpeg_start_decompress(&cinfo);
224
225 if (!decompress(&cinfo, static_cast<const uint8_t*>(mResultBuffer.data()),
226 cinfo.jpeg_color_space == JCS_GRAYSCALE)) {
227 return false;
228 }
229
230 jpeg_finish_decompress(&cinfo);
231 jpeg_destroy_decompress(&cinfo);
232
233 return true;
234}
235
Dichen Zhangd18bc302022-12-16 20:55:24 +0000236// TODO (Fyodor/Dichen): merge this method with getCompressedImageParameters() since they have
237// similar functionality. Yet Dichen is not familiar with who's calling
238// getCompressedImageParameters(), looks like it's used by some pending CLs.
239bool JpegDecoder::extractEXIF(const void* image, int length) {
240 jpeg_decompress_struct cinfo;
241 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
242 jpegrerror_mgr myerr;
243
244 cinfo.err = jpeg_std_error(&myerr.pub);
245 myerr.pub.error_exit = jpegrerror_exit;
246
247 if (setjmp(myerr.setjmp_buffer)) {
248 jpeg_destroy_decompress(&cinfo);
249 return false;
250 }
251 jpeg_create_decompress(&cinfo);
252
253 jpeg_save_markers(&cinfo, kAPP0Marker, 0xFFFF);
254 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
255 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
256
257 cinfo.src = &mgr;
258 jpeg_read_header(&cinfo, TRUE);
259
260 bool exifAppears = false;
261 size_t pos = 2; // position after SOI
262 for (jpeg_marker_struct* marker = cinfo.marker_list;
263 marker && !exifAppears;
264 marker = marker->next) {
265
266 pos += 4;
267 pos += marker->original_length;
268
269 if (marker->marker != kAPP1Marker) {
270 continue;
271 }
272
273 const unsigned int len = marker->data_length;
274 if (!exifAppears &&
275 len > kExifIdCode.size() &&
276 !strncmp(reinterpret_cast<const char*>(marker->data),
277 kExifIdCode.c_str(),
278 kExifIdCode.size())) {
279 mEXIFBuffer.resize(len, 0);
280 memcpy(static_cast<void*>(mEXIFBuffer.data()), marker->data, len);
281 exifAppears = true;
282 mExifPos = pos - marker->original_length;
283 }
284 }
285
286 jpeg_destroy_decompress(&cinfo);
287 return true;
288}
289
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000290bool JpegDecoder::decompress(jpeg_decompress_struct* cinfo, const uint8_t* dest,
291 bool isSingleChannel) {
292 if (isSingleChannel) {
293 return decompressSingleChannel(cinfo, dest);
294 }
295 return decompressYUV(cinfo, dest);
296}
297
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000298bool JpegDecoder::getCompressedImageParameters(const void* image, int length,
299 size_t *pWidth, size_t *pHeight,
300 std::vector<uint8_t> *&iccData , std::vector<uint8_t> *&exifData) {
301 jpeg_decompress_struct cinfo;
302 jpegr_source_mgr mgr(static_cast<const uint8_t*>(image), length);
303 jpegrerror_mgr myerr;
304 cinfo.err = jpeg_std_error(&myerr.pub);
305 myerr.pub.error_exit = jpegrerror_exit;
306
307 if (setjmp(myerr.setjmp_buffer)) {
308 jpeg_destroy_decompress(&cinfo);
309 return false;
310 }
311 jpeg_create_decompress(&cinfo);
312
Dichen Zhangd18bc302022-12-16 20:55:24 +0000313 jpeg_save_markers(&cinfo, kAPP1Marker, 0xFFFF);
314 jpeg_save_markers(&cinfo, kAPP2Marker, 0xFFFF);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000315
316 cinfo.src = &mgr;
317 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
318 jpeg_destroy_decompress(&cinfo);
319 return false;
320 }
321
322 *pWidth = cinfo.image_width;
323 *pHeight = cinfo.image_height;
324
325 //TODO: Parse iccProfile and exifData
326 (void)iccData;
327 (void)exifData;
328
329
330 jpeg_destroy_decompress(&cinfo);
331 return true;
332}
333
334
Dichen Zhang0d12ba82022-10-19 20:44:51 +0000335bool JpegDecoder::decompressYUV(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
336
337 JSAMPROW y[kCompressBatchSize];
338 JSAMPROW cb[kCompressBatchSize / 2];
339 JSAMPROW cr[kCompressBatchSize / 2];
340 JSAMPARRAY planes[3] {y, cb, cr};
341
342 size_t y_plane_size = cinfo->image_width * cinfo->image_height;
343 size_t uv_plane_size = y_plane_size / 4;
344 uint8_t* y_plane = const_cast<uint8_t*>(dest);
345 uint8_t* u_plane = const_cast<uint8_t*>(dest + y_plane_size);
346 uint8_t* v_plane = const_cast<uint8_t*>(dest + y_plane_size + uv_plane_size);
347 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
348 memset(empty.get(), 0, cinfo->image_width);
349
350 while (cinfo->output_scanline < cinfo->image_height) {
351 for (int i = 0; i < kCompressBatchSize; ++i) {
352 size_t scanline = cinfo->output_scanline + i;
353 if (scanline < cinfo->image_height) {
354 y[i] = y_plane + scanline * cinfo->image_width;
355 } else {
356 y[i] = empty.get();
357 }
358 }
359 // cb, cr only have half scanlines
360 for (int i = 0; i < kCompressBatchSize / 2; ++i) {
361 size_t scanline = cinfo->output_scanline / 2 + i;
362 if (scanline < cinfo->image_height / 2) {
363 int offset = scanline * (cinfo->image_width / 2);
364 cb[i] = u_plane + offset;
365 cr[i] = v_plane + offset;
366 } else {
367 cb[i] = cr[i] = empty.get();
368 }
369 }
370
371 int processed = jpeg_read_raw_data(cinfo, planes, kCompressBatchSize);
372 if (processed != kCompressBatchSize) {
373 ALOGE("Number of processed lines does not equal input lines.");
374 return false;
375 }
376 }
377 return true;
378}
379
380bool JpegDecoder::decompressSingleChannel(jpeg_decompress_struct* cinfo, const uint8_t* dest) {
381 JSAMPROW y[kCompressBatchSize];
382 JSAMPARRAY planes[1] {y};
383
384 uint8_t* y_plane = const_cast<uint8_t*>(dest);
385 std::unique_ptr<uint8_t[]> empty(new uint8_t[cinfo->image_width]);
386 memset(empty.get(), 0, cinfo->image_width);
387
388 while (cinfo->output_scanline < cinfo->image_height) {
389 for (int i = 0; i < kCompressBatchSize; ++i) {
390 size_t scanline = cinfo->output_scanline + i;
391 if (scanline < cinfo->image_height) {
392 y[i] = y_plane + scanline * cinfo->image_width;
393 } else {
394 y[i] = empty.get();
395 }
396 }
397
398 int processed = jpeg_read_raw_data(cinfo, planes, kCompressBatchSize);
399 if (processed != kCompressBatchSize / 2) {
400 ALOGE("Number of processed lines does not equal input lines.");
401 return false;
402 }
403 }
404 return true;
405}
406
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400407} // namespace android