1 /*
2 * Copyright (C) 2006 Apple Computer, Inc.
3 *
4 * Portions are Copyright (C) 2001-6 mozilla.org
5 *
6 * Other contributors:
7 * Stuart Parmenter <stuart@mozilla.com>
8 *
9 * Copyright (C) 2007-2009 Torch Mobile, Inc.
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * Alternatively, the contents of this file may be used under the terms
26 * of either the Mozilla Public License Version 1.1, found at
27 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
28 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
29 * (the "GPL"), in which case the provisions of the MPL or the GPL are
30 * applicable instead of those above. If you wish to allow use of your
31 * version of this file only under the terms of one of those two
32 * licenses (the MPL or the GPL) and not to allow others to use your
33 * version of this file under the LGPL, indicate your decision by
34 * deletingthe provisions above and replace them with the notice and
35 * other provisions required by the MPL or the GPL, as the case may be.
36 * If you do not delete the provisions above, a recipient may use your
37 * version of this file under any of the LGPL, the MPL or the GPL.
38 */
39
40 #include "config.h"
41 #include "platform/image-decoders/jpeg/JPEGImageDecoder.h"
42
43 #include "platform/PlatformInstrumentation.h"
44 #include "wtf/PassOwnPtr.h"
45 #include "wtf/dtoa/utils.h"
46
47 extern "C" {
48 #include <stdio.h> // jpeglib.h needs stdio FILE.
49 #include "jpeglib.h"
50 #if USE(ICCJPEG)
51 #include "iccjpeg.h"
52 #endif
53 #if USE(QCMSLIB)
54 #include "qcms.h"
55 #endif
56 #include <setjmp.h>
57 }
58
59 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN)
60 #error Blink assumes a little-endian target.
61 #endif
62
63 #if defined(JCS_ALPHA_EXTENSIONS)
64 #define TURBO_JPEG_RGB_SWIZZLE
65 #if SK_B32_SHIFT // Output little-endian RGBA pixels (Android).
rgbOutputColorSpace()66 inline J_COLOR_SPACE rgbOutputColorSpace() { return JCS_EXT_RGBA; }
67 #else // Output little-endian BGRA pixels.
rgbOutputColorSpace()68 inline J_COLOR_SPACE rgbOutputColorSpace() { return JCS_EXT_BGRA; }
69 #endif
turboSwizzled(J_COLOR_SPACE colorSpace)70 inline bool turboSwizzled(J_COLOR_SPACE colorSpace) { return colorSpace == JCS_EXT_RGBA || colorSpace == JCS_EXT_BGRA; }
colorSpaceHasAlpha(J_COLOR_SPACE colorSpace)71 inline bool colorSpaceHasAlpha(J_COLOR_SPACE colorSpace) { return turboSwizzled(colorSpace); }
72 #else
rgbOutputColorSpace()73 inline J_COLOR_SPACE rgbOutputColorSpace() { return JCS_RGB; }
colorSpaceHasAlpha(J_COLOR_SPACE)74 inline bool colorSpaceHasAlpha(J_COLOR_SPACE) { return false; }
75 #endif
76
77 #if USE(LOW_QUALITY_IMAGE_NO_JPEG_DITHERING)
dctMethod()78 inline J_DCT_METHOD dctMethod() { return JDCT_IFAST; }
ditherMode()79 inline J_DITHER_MODE ditherMode() { return JDITHER_NONE; }
80 #else
dctMethod()81 inline J_DCT_METHOD dctMethod() { return JDCT_ISLOW; }
ditherMode()82 inline J_DITHER_MODE ditherMode() { return JDITHER_FS; }
83 #endif
84
85 #if USE(LOW_QUALITY_IMAGE_NO_JPEG_FANCY_UPSAMPLING)
doFancyUpsampling()86 inline bool doFancyUpsampling() { return false; }
87 #else
doFancyUpsampling()88 inline bool doFancyUpsampling() { return true; }
89 #endif
90
91 namespace {
92
93 const int exifMarker = JPEG_APP0 + 1;
94
95 // JPEG only supports a denominator of 8.
96 const unsigned scaleDenominator = 8;
97
98 } // namespace
99
100 namespace WebCore {
101
102 struct decoder_error_mgr {
103 struct jpeg_error_mgr pub; // "public" fields for IJG library
104 jmp_buf setjmp_buffer; // For handling catastropic errors
105 };
106
107 enum jstate {
108 JPEG_HEADER, // Reading JFIF headers
109 JPEG_START_DECOMPRESS,
110 JPEG_DECOMPRESS_PROGRESSIVE, // Output progressive pixels
111 JPEG_DECOMPRESS_SEQUENTIAL, // Output sequential pixels
112 JPEG_DONE,
113 JPEG_ERROR
114 };
115
116 void init_source(j_decompress_ptr jd);
117 boolean fill_input_buffer(j_decompress_ptr jd);
118 void skip_input_data(j_decompress_ptr jd, long num_bytes);
119 void term_source(j_decompress_ptr jd);
120 void error_exit(j_common_ptr cinfo);
121
122 // Implementation of a JPEG src object that understands our state machine
123 struct decoder_source_mgr {
124 // public fields; must be first in this struct!
125 struct jpeg_source_mgr pub;
126
127 JPEGImageReader* decoder;
128 };
129
readUint16(JOCTET * data,bool isBigEndian)130 static unsigned readUint16(JOCTET* data, bool isBigEndian)
131 {
132 if (isBigEndian)
133 return (GETJOCTET(data[0]) << 8) | GETJOCTET(data[1]);
134 return (GETJOCTET(data[1]) << 8) | GETJOCTET(data[0]);
135 }
136
readUint32(JOCTET * data,bool isBigEndian)137 static unsigned readUint32(JOCTET* data, bool isBigEndian)
138 {
139 if (isBigEndian)
140 return (GETJOCTET(data[0]) << 24) | (GETJOCTET(data[1]) << 16) | (GETJOCTET(data[2]) << 8) | GETJOCTET(data[3]);
141 return (GETJOCTET(data[3]) << 24) | (GETJOCTET(data[2]) << 16) | (GETJOCTET(data[1]) << 8) | GETJOCTET(data[0]);
142 }
143
checkExifHeader(jpeg_saved_marker_ptr marker,bool & isBigEndian,unsigned & ifdOffset)144 static bool checkExifHeader(jpeg_saved_marker_ptr marker, bool& isBigEndian, unsigned& ifdOffset)
145 {
146 // For exif data, the APP1 block is followed by 'E', 'x', 'i', 'f', '\0',
147 // then a fill byte, and then a tiff file that contains the metadata.
148 // A tiff file starts with 'I', 'I' (intel / little endian byte order) or
149 // 'M', 'M' (motorola / big endian byte order), followed by (uint16_t)42,
150 // followed by an uint32_t with the offset to the tag block, relative to the
151 // tiff file start.
152 const unsigned exifHeaderSize = 14;
153 if (!(marker->marker == exifMarker
154 && marker->data_length >= exifHeaderSize
155 && marker->data[0] == 'E'
156 && marker->data[1] == 'x'
157 && marker->data[2] == 'i'
158 && marker->data[3] == 'f'
159 && marker->data[4] == '\0'
160 // data[5] is a fill byte
161 && ((marker->data[6] == 'I' && marker->data[7] == 'I')
162 || (marker->data[6] == 'M' && marker->data[7] == 'M'))))
163 return false;
164
165 isBigEndian = marker->data[6] == 'M';
166 if (readUint16(marker->data + 8, isBigEndian) != 42)
167 return false;
168
169 ifdOffset = readUint32(marker->data + 10, isBigEndian);
170 return true;
171 }
172
readImageOrientation(jpeg_decompress_struct * info)173 static ImageOrientation readImageOrientation(jpeg_decompress_struct* info)
174 {
175 // The JPEG decoder looks at EXIF metadata.
176 // FIXME: Possibly implement XMP and IPTC support.
177 const unsigned orientationTag = 0x112;
178 const unsigned shortType = 3;
179 for (jpeg_saved_marker_ptr marker = info->marker_list; marker; marker = marker->next) {
180 bool isBigEndian;
181 unsigned ifdOffset;
182 if (!checkExifHeader(marker, isBigEndian, ifdOffset))
183 continue;
184 const unsigned offsetToTiffData = 6; // Account for 'Exif\0<fill byte>' header.
185 if (marker->data_length < offsetToTiffData || ifdOffset >= marker->data_length - offsetToTiffData)
186 continue;
187 ifdOffset += offsetToTiffData;
188
189 // The jpeg exif container format contains a tiff block for metadata.
190 // A tiff image file directory (ifd) consists of a uint16_t describing
191 // the number of ifd entries, followed by that many entries.
192 // When touching this code, it's useful to look at the tiff spec:
193 // http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
194 JOCTET* ifd = marker->data + ifdOffset;
195 JOCTET* end = marker->data + marker->data_length;
196 if (end - ifd < 2)
197 continue;
198 unsigned tagCount = readUint16(ifd, isBigEndian);
199 ifd += 2; // Skip over the uint16 that was just read.
200
201 // Every ifd entry is 2 bytes of tag, 2 bytes of contents datatype,
202 // 4 bytes of number-of-elements, and 4 bytes of either offset to the
203 // tag data, or if the data is small enough, the inlined data itself.
204 const int ifdEntrySize = 12;
205 for (unsigned i = 0; i < tagCount && end - ifd >= ifdEntrySize; ++i, ifd += ifdEntrySize) {
206 unsigned tag = readUint16(ifd, isBigEndian);
207 unsigned type = readUint16(ifd + 2, isBigEndian);
208 unsigned count = readUint32(ifd + 4, isBigEndian);
209 if (tag == orientationTag && type == shortType && count == 1)
210 return ImageOrientation::fromEXIFValue(readUint16(ifd + 8, isBigEndian));
211 }
212 }
213
214 return ImageOrientation();
215 }
216
217 #if USE(QCMSLIB)
readColorProfile(jpeg_decompress_struct * info,ColorProfile & colorProfile)218 static void readColorProfile(jpeg_decompress_struct* info, ColorProfile& colorProfile)
219 {
220 #if USE(ICCJPEG)
221 JOCTET* profile;
222 unsigned profileLength;
223
224 if (!read_icc_profile(info, &profile, &profileLength))
225 return;
226
227 // Only accept RGB color profiles from input class devices.
228 bool ignoreProfile = false;
229 char* profileData = reinterpret_cast<char*>(profile);
230 if (profileLength < ImageDecoder::iccColorProfileHeaderLength)
231 ignoreProfile = true;
232 else if (!ImageDecoder::rgbColorProfile(profileData, profileLength))
233 ignoreProfile = true;
234 else if (!ImageDecoder::inputDeviceColorProfile(profileData, profileLength))
235 ignoreProfile = true;
236
237 ASSERT(colorProfile.isEmpty());
238 if (!ignoreProfile)
239 colorProfile.append(profileData, profileLength);
240 free(profile);
241 #endif
242 }
243 #endif
244
245 class JPEGImageReader {
246 WTF_MAKE_FAST_ALLOCATED;
247 public:
JPEGImageReader(JPEGImageDecoder * decoder)248 JPEGImageReader(JPEGImageDecoder* decoder)
249 : m_decoder(decoder)
250 , m_bufferLength(0)
251 , m_bytesToSkip(0)
252 , m_state(JPEG_HEADER)
253 , m_samples(0)
254 #if USE(QCMSLIB)
255 , m_transform(0)
256 #endif
257 {
258 memset(&m_info, 0, sizeof(jpeg_decompress_struct));
259
260 // We set up the normal JPEG error routines, then override error_exit.
261 m_info.err = jpeg_std_error(&m_err.pub);
262 m_err.pub.error_exit = error_exit;
263
264 // Allocate and initialize JPEG decompression object.
265 jpeg_create_decompress(&m_info);
266
267 decoder_source_mgr* src = 0;
268 if (!m_info.src) {
269 src = (decoder_source_mgr*)fastZeroedMalloc(sizeof(decoder_source_mgr));
270 if (!src) {
271 m_state = JPEG_ERROR;
272 return;
273 }
274 }
275
276 m_info.src = (jpeg_source_mgr*)src;
277
278 // Set up callback functions.
279 src->pub.init_source = init_source;
280 src->pub.fill_input_buffer = fill_input_buffer;
281 src->pub.skip_input_data = skip_input_data;
282 src->pub.resync_to_restart = jpeg_resync_to_restart;
283 src->pub.term_source = term_source;
284 src->decoder = this;
285
286 #if USE(ICCJPEG)
287 // Retain ICC color profile markers for color management.
288 setup_read_icc_profile(&m_info);
289 #endif
290
291 // Keep APP1 blocks, for obtaining exif data.
292 jpeg_save_markers(&m_info, exifMarker, 0xFFFF);
293 }
294
~JPEGImageReader()295 ~JPEGImageReader()
296 {
297 close();
298 }
299
close()300 void close()
301 {
302 decoder_source_mgr* src = (decoder_source_mgr*)m_info.src;
303 if (src)
304 fastFree(src);
305 m_info.src = 0;
306
307 #if USE(QCMSLIB)
308 if (m_transform)
309 qcms_transform_release(m_transform);
310 m_transform = 0;
311 #endif
312 jpeg_destroy_decompress(&m_info);
313 }
314
skipBytes(long numBytes)315 void skipBytes(long numBytes)
316 {
317 decoder_source_mgr* src = (decoder_source_mgr*)m_info.src;
318 long bytesToSkip = std::min(numBytes, (long)src->pub.bytes_in_buffer);
319 src->pub.bytes_in_buffer -= (size_t)bytesToSkip;
320 src->pub.next_input_byte += bytesToSkip;
321
322 m_bytesToSkip = std::max(numBytes - bytesToSkip, static_cast<long>(0));
323 }
324
decode(const SharedBuffer & data,bool onlySize)325 bool decode(const SharedBuffer& data, bool onlySize)
326 {
327 unsigned newByteCount = data.size() - m_bufferLength;
328 unsigned readOffset = m_bufferLength - m_info.src->bytes_in_buffer;
329
330 m_info.src->bytes_in_buffer += newByteCount;
331 m_info.src->next_input_byte = (JOCTET*)(data.data()) + readOffset;
332
333 // If we still have bytes to skip, try to skip those now.
334 if (m_bytesToSkip)
335 skipBytes(m_bytesToSkip);
336
337 m_bufferLength = data.size();
338
339 // We need to do the setjmp here. Otherwise bad things will happen
340 if (setjmp(m_err.setjmp_buffer))
341 return m_decoder->setFailed();
342
343 switch (m_state) {
344 case JPEG_HEADER:
345 // Read file parameters with jpeg_read_header().
346 if (jpeg_read_header(&m_info, true) == JPEG_SUSPENDED)
347 return false; // I/O suspension.
348
349 switch (m_info.jpeg_color_space) {
350 case JCS_GRAYSCALE:
351 case JCS_RGB:
352 case JCS_YCbCr:
353 // libjpeg can convert GRAYSCALE and YCbCr image pixels to RGB.
354 m_info.out_color_space = rgbOutputColorSpace();
355 #if defined(TURBO_JPEG_RGB_SWIZZLE)
356 if (m_info.saw_JFIF_marker)
357 break;
358 // FIXME: Swizzle decoding does not support Adobe transform=0
359 // images (yet), so revert to using JSC_RGB in that case.
360 if (m_info.saw_Adobe_marker && !m_info.Adobe_transform)
361 m_info.out_color_space = JCS_RGB;
362 #endif
363 break;
364 case JCS_CMYK:
365 case JCS_YCCK:
366 // libjpeg can convert YCCK to CMYK, but neither to RGB, so we
367 // manually convert CMKY to RGB.
368 m_info.out_color_space = JCS_CMYK;
369 break;
370 default:
371 return m_decoder->setFailed();
372 }
373
374 m_state = JPEG_START_DECOMPRESS;
375
376 // We can fill in the size now that the header is available.
377 if (!m_decoder->setSize(m_info.image_width, m_info.image_height))
378 return false;
379
380 // Calculate and set decoded size.
381 m_info.scale_num = m_decoder->desiredScaleNumerator();
382 m_info.scale_denom = scaleDenominator;
383 jpeg_calc_output_dimensions(&m_info);
384 m_decoder->setDecodedSize(m_info.output_width, m_info.output_height);
385
386 m_decoder->setOrientation(readImageOrientation(info()));
387
388 #if USE(QCMSLIB)
389 // Allow color management of the decoded RGBA pixels if possible.
390 if (!m_decoder->ignoresGammaAndColorProfile()) {
391 ColorProfile colorProfile;
392 readColorProfile(info(), colorProfile);
393 createColorTransform(colorProfile, colorSpaceHasAlpha(m_info.out_color_space));
394 #if defined(TURBO_JPEG_RGB_SWIZZLE)
395 // Input RGBA data to qcms. Note: restored to BGRA on output.
396 if (m_transform && m_info.out_color_space == JCS_EXT_BGRA)
397 m_info.out_color_space = JCS_EXT_RGBA;
398 #endif
399 m_decoder->setHasColorProfile(!!m_transform);
400 }
401 #endif
402 // Don't allocate a giant and superfluous memory buffer when the
403 // image is a sequential JPEG.
404 m_info.buffered_image = jpeg_has_multiple_scans(&m_info);
405
406 if (onlySize) {
407 // We can stop here. Reduce our buffer length and available data.
408 m_bufferLength -= m_info.src->bytes_in_buffer;
409 m_info.src->bytes_in_buffer = 0;
410 return true;
411 }
412 // FALL THROUGH
413
414 case JPEG_START_DECOMPRESS:
415 // Set parameters for decompression.
416 // FIXME -- Should reset dct_method and dither mode for final pass
417 // of progressive JPEG.
418 m_info.dct_method = dctMethod();
419 m_info.dither_mode = ditherMode();
420 m_info.do_fancy_upsampling = doFancyUpsampling();
421 m_info.enable_2pass_quant = false;
422 m_info.do_block_smoothing = true;
423
424 // Make a one-row-high sample array that will go away when done with
425 // image. Always make it big enough to hold an RGB row. Since this
426 // uses the IJG memory manager, it must be allocated before the call
427 // to jpeg_start_compress().
428 // FIXME: note that some output color spaces do not need the samples
429 // buffer. Remove this allocation for those color spaces.
430 m_samples = (*m_info.mem->alloc_sarray)(reinterpret_cast<j_common_ptr>(&m_info), JPOOL_IMAGE, m_info.output_width * 4, 1);
431
432 // Start decompressor.
433 if (!jpeg_start_decompress(&m_info))
434 return false; // I/O suspension.
435
436 // If this is a progressive JPEG ...
437 m_state = (m_info.buffered_image) ? JPEG_DECOMPRESS_PROGRESSIVE : JPEG_DECOMPRESS_SEQUENTIAL;
438 // FALL THROUGH
439
440 case JPEG_DECOMPRESS_SEQUENTIAL:
441 if (m_state == JPEG_DECOMPRESS_SEQUENTIAL) {
442
443 if (!m_decoder->outputScanlines())
444 return false; // I/O suspension.
445
446 // If we've completed image output...
447 ASSERT(m_info.output_scanline == m_info.output_height);
448 m_state = JPEG_DONE;
449 }
450 // FALL THROUGH
451
452 case JPEG_DECOMPRESS_PROGRESSIVE:
453 if (m_state == JPEG_DECOMPRESS_PROGRESSIVE) {
454 int status;
455 do {
456 status = jpeg_consume_input(&m_info);
457 } while ((status != JPEG_SUSPENDED) && (status != JPEG_REACHED_EOI));
458
459 for (;;) {
460 if (!m_info.output_scanline) {
461 int scan = m_info.input_scan_number;
462
463 // If we haven't displayed anything yet
464 // (output_scan_number == 0) and we have enough data for
465 // a complete scan, force output of the last full scan.
466 if (!m_info.output_scan_number && (scan > 1) && (status != JPEG_REACHED_EOI))
467 --scan;
468
469 if (!jpeg_start_output(&m_info, scan))
470 return false; // I/O suspension.
471 }
472
473 if (m_info.output_scanline == 0xffffff)
474 m_info.output_scanline = 0;
475
476 // If outputScanlines() fails, it deletes |this|. Therefore,
477 // copy the decoder pointer and use it to check for failure
478 // to avoid member access in the failure case.
479 JPEGImageDecoder* decoder = m_decoder;
480 if (!decoder->outputScanlines()) {
481 if (decoder->failed()) // Careful; |this| is deleted.
482 return false;
483 if (!m_info.output_scanline)
484 // Didn't manage to read any lines - flag so we
485 // don't call jpeg_start_output() multiple times for
486 // the same scan.
487 m_info.output_scanline = 0xffffff;
488 return false; // I/O suspension.
489 }
490
491 if (m_info.output_scanline == m_info.output_height) {
492 if (!jpeg_finish_output(&m_info))
493 return false; // I/O suspension.
494
495 if (jpeg_input_complete(&m_info) && (m_info.input_scan_number == m_info.output_scan_number))
496 break;
497
498 m_info.output_scanline = 0;
499 }
500 }
501
502 m_state = JPEG_DONE;
503 }
504 // FALL THROUGH
505
506 case JPEG_DONE:
507 // Finish decompression.
508 return jpeg_finish_decompress(&m_info);
509
510 case JPEG_ERROR:
511 // We can get here if the constructor failed.
512 return m_decoder->setFailed();
513 }
514
515 return true;
516 }
517
info()518 jpeg_decompress_struct* info() { return &m_info; }
samples() const519 JSAMPARRAY samples() const { return m_samples; }
decoder()520 JPEGImageDecoder* decoder() { return m_decoder; }
521 #if USE(QCMSLIB)
colorTransform() const522 qcms_transform* colorTransform() const { return m_transform; }
523
createColorTransform(const ColorProfile & colorProfile,bool hasAlpha)524 void createColorTransform(const ColorProfile& colorProfile, bool hasAlpha)
525 {
526 if (m_transform)
527 qcms_transform_release(m_transform);
528 m_transform = 0;
529
530 if (colorProfile.isEmpty())
531 return;
532 qcms_profile* deviceProfile = ImageDecoder::qcmsOutputDeviceProfile();
533 if (!deviceProfile)
534 return;
535 qcms_profile* inputProfile = qcms_profile_from_memory(colorProfile.data(), colorProfile.size());
536 if (!inputProfile)
537 return;
538 // We currently only support color profiles for RGB profiled images.
539 ASSERT(icSigRgbData == qcms_profile_get_color_space(inputProfile));
540 qcms_data_type dataFormat = hasAlpha ? QCMS_DATA_RGBA_8 : QCMS_DATA_RGB_8;
541 // FIXME: Don't force perceptual intent if the image profile contains an intent.
542 m_transform = qcms_transform_create(inputProfile, dataFormat, deviceProfile, dataFormat, QCMS_INTENT_PERCEPTUAL);
543 qcms_profile_release(inputProfile);
544 }
545 #endif
546
547 private:
548 JPEGImageDecoder* m_decoder;
549 unsigned m_bufferLength;
550 int m_bytesToSkip;
551
552 jpeg_decompress_struct m_info;
553 decoder_error_mgr m_err;
554 jstate m_state;
555
556 JSAMPARRAY m_samples;
557
558 #if USE(QCMSLIB)
559 qcms_transform* m_transform;
560 #endif
561 };
562
563 // Override the standard error method in the IJG JPEG decoder code.
error_exit(j_common_ptr cinfo)564 void error_exit(j_common_ptr cinfo)
565 {
566 // Return control to the setjmp point.
567 decoder_error_mgr *err = reinterpret_cast_ptr<decoder_error_mgr *>(cinfo->err);
568 longjmp(err->setjmp_buffer, -1);
569 }
570
init_source(j_decompress_ptr)571 void init_source(j_decompress_ptr)
572 {
573 }
574
skip_input_data(j_decompress_ptr jd,long num_bytes)575 void skip_input_data(j_decompress_ptr jd, long num_bytes)
576 {
577 decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
578 src->decoder->skipBytes(num_bytes);
579 }
580
fill_input_buffer(j_decompress_ptr)581 boolean fill_input_buffer(j_decompress_ptr)
582 {
583 // Our decode step always sets things up properly, so if this method is ever
584 // called, then we have hit the end of the buffer. A return value of false
585 // indicates that we have no data to supply yet.
586 return false;
587 }
588
term_source(j_decompress_ptr jd)589 void term_source(j_decompress_ptr jd)
590 {
591 decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
592 src->decoder->decoder()->jpegComplete();
593 }
594
JPEGImageDecoder(ImageSource::AlphaOption alphaOption,ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption,size_t maxDecodedBytes)595 JPEGImageDecoder::JPEGImageDecoder(ImageSource::AlphaOption alphaOption,
596 ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption,
597 size_t maxDecodedBytes)
598 : ImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes)
599 , m_hasColorProfile(false)
600 {
601 }
602
~JPEGImageDecoder()603 JPEGImageDecoder::~JPEGImageDecoder()
604 {
605 }
606
isSizeAvailable()607 bool JPEGImageDecoder::isSizeAvailable()
608 {
609 if (!ImageDecoder::isSizeAvailable())
610 decode(true);
611
612 return ImageDecoder::isSizeAvailable();
613 }
614
setSize(unsigned width,unsigned height)615 bool JPEGImageDecoder::setSize(unsigned width, unsigned height)
616 {
617 if (!ImageDecoder::setSize(width, height))
618 return false;
619
620 if (!desiredScaleNumerator())
621 return setFailed();
622
623 setDecodedSize(width, height);
624 return true;
625 }
626
setDecodedSize(unsigned width,unsigned height)627 void JPEGImageDecoder::setDecodedSize(unsigned width, unsigned height)
628 {
629 m_decodedSize = IntSize(width, height);
630 }
631
desiredScaleNumerator() const632 unsigned JPEGImageDecoder::desiredScaleNumerator() const
633 {
634 size_t originalBytes = size().width() * size().height() * 4;
635 if (originalBytes <= m_maxDecodedBytes) {
636 return scaleDenominator;
637 }
638
639 // Downsample according to the maximum decoded size.
640 unsigned scaleNumerator = static_cast<unsigned>(floor(sqrt(
641 // MSVC needs explicit parameter type for sqrt().
642 static_cast<float>(m_maxDecodedBytes * scaleDenominator * scaleDenominator / originalBytes))));
643
644 return scaleNumerator;
645 }
646
frameBufferAtIndex(size_t index)647 ImageFrame* JPEGImageDecoder::frameBufferAtIndex(size_t index)
648 {
649 if (index)
650 return 0;
651
652 if (m_frameBufferCache.isEmpty()) {
653 m_frameBufferCache.resize(1);
654 m_frameBufferCache[0].setPremultiplyAlpha(m_premultiplyAlpha);
655 }
656
657 ImageFrame& frame = m_frameBufferCache[0];
658 if (frame.status() != ImageFrame::FrameComplete) {
659 PlatformInstrumentation::willDecodeImage("JPEG");
660 decode(false);
661 PlatformInstrumentation::didDecodeImage();
662 }
663
664 frame.notifyBitmapIfPixelsChanged();
665 return &frame;
666 }
667
setFailed()668 bool JPEGImageDecoder::setFailed()
669 {
670 m_reader.clear();
671 return ImageDecoder::setFailed();
672 }
673
setPixel(ImageFrame & buffer,ImageFrame::PixelData * pixel,JSAMPARRAY samples,int column)674 template <J_COLOR_SPACE colorSpace> void setPixel(ImageFrame& buffer, ImageFrame::PixelData* pixel, JSAMPARRAY samples, int column)
675 {
676 JSAMPLE* jsample = *samples + column * (colorSpace == JCS_RGB ? 3 : 4);
677
678 switch (colorSpace) {
679 case JCS_RGB:
680 buffer.setRGBARaw(pixel, jsample[0], jsample[1], jsample[2], 255);
681 break;
682 case JCS_CMYK:
683 // Source is 'Inverted CMYK', output is RGB.
684 // See: http://www.easyrgb.com/math.php?MATH=M12#text12
685 // Or: http://www.ilkeratalay.com/colorspacesfaq.php#rgb
686 // From CMYK to CMY:
687 // X = X * (1 - K ) + K [for X = C, M, or Y]
688 // Thus, from Inverted CMYK to CMY is:
689 // X = (1-iX) * (1 - (1-iK)) + (1-iK) => 1 - iX*iK
690 // From CMY (0..1) to RGB (0..1):
691 // R = 1 - C => 1 - (1 - iC*iK) => iC*iK [G and B similar]
692 unsigned k = jsample[3];
693 buffer.setRGBARaw(pixel, jsample[0] * k / 255, jsample[1] * k / 255, jsample[2] * k / 255, 255);
694 break;
695 }
696 }
697
outputRows(JPEGImageReader * reader,ImageFrame & buffer)698 template <J_COLOR_SPACE colorSpace> bool outputRows(JPEGImageReader* reader, ImageFrame& buffer)
699 {
700 JSAMPARRAY samples = reader->samples();
701 jpeg_decompress_struct* info = reader->info();
702 int width = info->output_width;
703
704 while (info->output_scanline < info->output_height) {
705 // jpeg_read_scanlines will increase the scanline counter, so we
706 // save the scanline before calling it.
707 int y = info->output_scanline;
708 // Request one scanline: returns 0 or 1 scanlines.
709 if (jpeg_read_scanlines(info, samples, 1) != 1)
710 return false;
711 #if USE(QCMSLIB)
712 if (reader->colorTransform() && colorSpace == JCS_RGB)
713 qcms_transform_data(reader->colorTransform(), *samples, *samples, width);
714 #endif
715 ImageFrame::PixelData* pixel = buffer.getAddr(0, y);
716 for (int x = 0; x < width; ++pixel, ++x)
717 setPixel<colorSpace>(buffer, pixel, samples, x);
718 }
719
720 buffer.setPixelsChanged(true);
721 return true;
722 }
723
outputScanlines()724 bool JPEGImageDecoder::outputScanlines()
725 {
726 if (m_frameBufferCache.isEmpty())
727 return false;
728
729 jpeg_decompress_struct* info = m_reader->info();
730
731 // Initialize the framebuffer if needed.
732 ImageFrame& buffer = m_frameBufferCache[0];
733 if (buffer.status() == ImageFrame::FrameEmpty) {
734 ASSERT(info->output_width == static_cast<JDIMENSION>(m_decodedSize.width()));
735 ASSERT(info->output_height == static_cast<JDIMENSION>(m_decodedSize.height()));
736
737 if (!buffer.setSize(info->output_width, info->output_height))
738 return setFailed();
739 buffer.setStatus(ImageFrame::FramePartial);
740 // The buffer is transparent outside the decoded area while the image is
741 // loading. The completed image will be marked fully opaque in jpegComplete().
742 buffer.setHasAlpha(true);
743
744 // For JPEGs, the frame always fills the entire image.
745 buffer.setOriginalFrameRect(IntRect(IntPoint(), size()));
746 }
747
748 #if defined(TURBO_JPEG_RGB_SWIZZLE)
749 if (turboSwizzled(info->out_color_space)) {
750 while (info->output_scanline < info->output_height) {
751 unsigned char* row = reinterpret_cast<unsigned char*>(buffer.getAddr(0, info->output_scanline));
752 if (jpeg_read_scanlines(info, &row, 1) != 1)
753 return false;
754 #if USE(QCMSLIB)
755 if (qcms_transform* transform = m_reader->colorTransform())
756 qcms_transform_data_type(transform, row, row, info->output_width, rgbOutputColorSpace() == JCS_EXT_BGRA ? QCMS_OUTPUT_BGRX : QCMS_OUTPUT_RGBX);
757 #endif
758 }
759 buffer.setPixelsChanged(true);
760 return true;
761 }
762 #endif
763
764 switch (info->out_color_space) {
765 case JCS_RGB:
766 return outputRows<JCS_RGB>(m_reader.get(), buffer);
767 case JCS_CMYK:
768 return outputRows<JCS_CMYK>(m_reader.get(), buffer);
769 default:
770 ASSERT_NOT_REACHED();
771 }
772
773 return setFailed();
774 }
775
jpegComplete()776 void JPEGImageDecoder::jpegComplete()
777 {
778 if (m_frameBufferCache.isEmpty())
779 return;
780
781 // Hand back an appropriately sized buffer, even if the image ended up being
782 // empty.
783 ImageFrame& buffer = m_frameBufferCache[0];
784 buffer.setHasAlpha(false);
785 buffer.setStatus(ImageFrame::FrameComplete);
786 }
787
decode(bool onlySize)788 void JPEGImageDecoder::decode(bool onlySize)
789 {
790 if (failed())
791 return;
792
793 if (!m_reader) {
794 m_reader = adoptPtr(new JPEGImageReader(this));
795 }
796
797 // If we couldn't decode the image but we've received all the data, decoding
798 // has failed.
799 if (!m_reader->decode(*m_data, onlySize) && isAllDataReceived())
800 setFailed();
801 // If we're done decoding the image, we don't need the JPEGImageReader
802 // anymore. (If we failed, |m_reader| has already been cleared.)
803 else if (!m_frameBufferCache.isEmpty() && (m_frameBufferCache[0].status() == ImageFrame::FrameComplete))
804 m_reader.clear();
805 }
806
807 }
808