Home | History | Annotate | Download | only in jpeg
      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 (at) 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).
     66 inline J_COLOR_SPACE rgbOutputColorSpace() { return JCS_EXT_RGBA; }
     67 #else // Output little-endian BGRA pixels.
     68 inline J_COLOR_SPACE rgbOutputColorSpace() { return JCS_EXT_BGRA; }
     69 #endif
     70 inline bool turboSwizzled(J_COLOR_SPACE colorSpace) { return colorSpace == JCS_EXT_RGBA || colorSpace == JCS_EXT_BGRA; }
     71 inline bool colorSpaceHasAlpha(J_COLOR_SPACE colorSpace) { return turboSwizzled(colorSpace); }
     72 #else
     73 inline J_COLOR_SPACE rgbOutputColorSpace() { return JCS_RGB; }
     74 inline bool colorSpaceHasAlpha(J_COLOR_SPACE) { return false; }
     75 #endif
     76 
     77 #if USE(LOW_QUALITY_IMAGE_NO_JPEG_DITHERING)
     78 inline J_DCT_METHOD dctMethod() { return JDCT_IFAST; }
     79 inline J_DITHER_MODE ditherMode() { return JDITHER_NONE; }
     80 #else
     81 inline J_DCT_METHOD dctMethod() { return JDCT_ISLOW; }
     82 inline J_DITHER_MODE ditherMode() { return JDITHER_FS; }
     83 #endif
     84 
     85 #if USE(LOW_QUALITY_IMAGE_NO_JPEG_FANCY_UPSAMPLING)
     86 inline bool doFancyUpsampling() { return false; }
     87 #else
     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 blink {
    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 enum yuv_subsampling {
    117     YUV_UNKNOWN,
    118     YUV_410,
    119     YUV_411,
    120     YUV_420,
    121     YUV_422,
    122     YUV_440,
    123     YUV_444
    124 };
    125 
    126 void init_source(j_decompress_ptr jd);
    127 boolean fill_input_buffer(j_decompress_ptr jd);
    128 void skip_input_data(j_decompress_ptr jd, long num_bytes);
    129 void term_source(j_decompress_ptr jd);
    130 void error_exit(j_common_ptr cinfo);
    131 
    132 // Implementation of a JPEG src object that understands our state machine
    133 struct decoder_source_mgr {
    134     // public fields; must be first in this struct!
    135     struct jpeg_source_mgr pub;
    136 
    137     JPEGImageReader* decoder;
    138 };
    139 
    140 static unsigned readUint16(JOCTET* data, bool isBigEndian)
    141 {
    142     if (isBigEndian)
    143         return (GETJOCTET(data[0]) << 8) | GETJOCTET(data[1]);
    144     return (GETJOCTET(data[1]) << 8) | GETJOCTET(data[0]);
    145 }
    146 
    147 static unsigned readUint32(JOCTET* data, bool isBigEndian)
    148 {
    149     if (isBigEndian)
    150         return (GETJOCTET(data[0]) << 24) | (GETJOCTET(data[1]) << 16) | (GETJOCTET(data[2]) << 8) | GETJOCTET(data[3]);
    151     return (GETJOCTET(data[3]) << 24) | (GETJOCTET(data[2]) << 16) | (GETJOCTET(data[1]) << 8) | GETJOCTET(data[0]);
    152 }
    153 
    154 static bool checkExifHeader(jpeg_saved_marker_ptr marker, bool& isBigEndian, unsigned& ifdOffset)
    155 {
    156     // For exif data, the APP1 block is followed by 'E', 'x', 'i', 'f', '\0',
    157     // then a fill byte, and then a tiff file that contains the metadata.
    158     // A tiff file starts with 'I', 'I' (intel / little endian byte order) or
    159     // 'M', 'M' (motorola / big endian byte order), followed by (uint16_t)42,
    160     // followed by an uint32_t with the offset to the tag block, relative to the
    161     // tiff file start.
    162     const unsigned exifHeaderSize = 14;
    163     if (!(marker->marker == exifMarker
    164         && marker->data_length >= exifHeaderSize
    165         && marker->data[0] == 'E'
    166         && marker->data[1] == 'x'
    167         && marker->data[2] == 'i'
    168         && marker->data[3] == 'f'
    169         && marker->data[4] == '\0'
    170         // data[5] is a fill byte
    171         && ((marker->data[6] == 'I' && marker->data[7] == 'I')
    172             || (marker->data[6] == 'M' && marker->data[7] == 'M'))))
    173         return false;
    174 
    175     isBigEndian = marker->data[6] == 'M';
    176     if (readUint16(marker->data + 8, isBigEndian) != 42)
    177         return false;
    178 
    179     ifdOffset = readUint32(marker->data + 10, isBigEndian);
    180     return true;
    181 }
    182 
    183 static ImageOrientation readImageOrientation(jpeg_decompress_struct* info)
    184 {
    185     // The JPEG decoder looks at EXIF metadata.
    186     // FIXME: Possibly implement XMP and IPTC support.
    187     const unsigned orientationTag = 0x112;
    188     const unsigned shortType = 3;
    189     for (jpeg_saved_marker_ptr marker = info->marker_list; marker; marker = marker->next) {
    190         bool isBigEndian;
    191         unsigned ifdOffset;
    192         if (!checkExifHeader(marker, isBigEndian, ifdOffset))
    193             continue;
    194         const unsigned offsetToTiffData = 6; // Account for 'Exif\0<fill byte>' header.
    195         if (marker->data_length < offsetToTiffData || ifdOffset >= marker->data_length - offsetToTiffData)
    196             continue;
    197         ifdOffset += offsetToTiffData;
    198 
    199         // The jpeg exif container format contains a tiff block for metadata.
    200         // A tiff image file directory (ifd) consists of a uint16_t describing
    201         // the number of ifd entries, followed by that many entries.
    202         // When touching this code, it's useful to look at the tiff spec:
    203         // http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
    204         JOCTET* ifd = marker->data + ifdOffset;
    205         JOCTET* end = marker->data + marker->data_length;
    206         if (end - ifd < 2)
    207             continue;
    208         unsigned tagCount = readUint16(ifd, isBigEndian);
    209         ifd += 2; // Skip over the uint16 that was just read.
    210 
    211         // Every ifd entry is 2 bytes of tag, 2 bytes of contents datatype,
    212         // 4 bytes of number-of-elements, and 4 bytes of either offset to the
    213         // tag data, or if the data is small enough, the inlined data itself.
    214         const int ifdEntrySize = 12;
    215         for (unsigned i = 0; i < tagCount && end - ifd >= ifdEntrySize; ++i, ifd += ifdEntrySize) {
    216             unsigned tag = readUint16(ifd, isBigEndian);
    217             unsigned type = readUint16(ifd + 2, isBigEndian);
    218             unsigned count = readUint32(ifd + 4, isBigEndian);
    219             if (tag == orientationTag && type == shortType && count == 1)
    220                 return ImageOrientation::fromEXIFValue(readUint16(ifd + 8, isBigEndian));
    221         }
    222     }
    223 
    224     return ImageOrientation();
    225 }
    226 
    227 #if USE(QCMSLIB)
    228 static void readColorProfile(jpeg_decompress_struct* info, ColorProfile& colorProfile)
    229 {
    230 #if USE(ICCJPEG)
    231     JOCTET* profile;
    232     unsigned profileLength;
    233 
    234     if (!read_icc_profile(info, &profile, &profileLength))
    235         return;
    236 
    237     // Only accept RGB color profiles from input class devices.
    238     bool ignoreProfile = false;
    239     char* profileData = reinterpret_cast<char*>(profile);
    240     if (profileLength < ImageDecoder::iccColorProfileHeaderLength)
    241         ignoreProfile = true;
    242     else if (!ImageDecoder::rgbColorProfile(profileData, profileLength))
    243         ignoreProfile = true;
    244     else if (!ImageDecoder::inputDeviceColorProfile(profileData, profileLength))
    245         ignoreProfile = true;
    246 
    247     ASSERT(colorProfile.isEmpty());
    248     if (!ignoreProfile)
    249         colorProfile.append(profileData, profileLength);
    250     free(profile);
    251 #endif
    252 }
    253 #endif
    254 
    255 static IntSize computeYUVSize(const jpeg_decompress_struct* info, int component, ImageDecoder::SizeType sizeType)
    256 {
    257     if (sizeType == ImageDecoder::SizeForMemoryAllocation) {
    258         return IntSize(info->cur_comp_info[component]->width_in_blocks * DCTSIZE, info->cur_comp_info[component]->height_in_blocks * DCTSIZE);
    259     }
    260     return IntSize(info->cur_comp_info[component]->downsampled_width, info->cur_comp_info[component]->downsampled_height);
    261 }
    262 
    263 static yuv_subsampling yuvSubsampling(const jpeg_decompress_struct& info)
    264 {
    265     if ((DCTSIZE == 8)
    266         && (info.num_components == 3)
    267         && (info.comps_in_scan >= info.num_components)
    268         && (info.scale_denom <= 8)
    269         && (info.cur_comp_info[0])
    270         && (info.cur_comp_info[1])
    271         && (info.cur_comp_info[2])
    272         && (info.cur_comp_info[1]->h_samp_factor == 1)
    273         && (info.cur_comp_info[1]->v_samp_factor == 1)
    274         && (info.cur_comp_info[2]->h_samp_factor == 1)
    275         && (info.cur_comp_info[2]->v_samp_factor == 1)) {
    276         int h = info.cur_comp_info[0]->h_samp_factor;
    277         int v = info.cur_comp_info[0]->v_samp_factor;
    278         // 4:4:4 : (h == 1) && (v == 1)
    279         // 4:4:0 : (h == 1) && (v == 2)
    280         // 4:2:2 : (h == 2) && (v == 1)
    281         // 4:2:0 : (h == 2) && (v == 2)
    282         // 4:1:1 : (h == 4) && (v == 1)
    283         // 4:1:0 : (h == 4) && (v == 2)
    284         if (v == 1) {
    285             switch (h) {
    286             case 1:
    287                 return YUV_444;
    288             case 2:
    289                 return YUV_422;
    290             case 4:
    291                 return YUV_411;
    292             default:
    293                 break;
    294             }
    295         } else if (v == 2) {
    296             switch (h) {
    297             case 1:
    298                 return YUV_440;
    299             case 2:
    300                 return YUV_420;
    301             case 4:
    302                 return YUV_410;
    303             default:
    304                 break;
    305             }
    306         }
    307     }
    308 
    309     return YUV_UNKNOWN;
    310 }
    311 
    312 class JPEGImageReader {
    313     WTF_MAKE_FAST_ALLOCATED;
    314 public:
    315     JPEGImageReader(JPEGImageDecoder* decoder)
    316         : m_decoder(decoder)
    317         , m_bufferLength(0)
    318         , m_bytesToSkip(0)
    319         , m_state(JPEG_HEADER)
    320         , m_samples(0)
    321 #if USE(QCMSLIB)
    322         , m_transform(0)
    323 #endif
    324     {
    325         memset(&m_info, 0, sizeof(jpeg_decompress_struct));
    326 
    327         // We set up the normal JPEG error routines, then override error_exit.
    328         m_info.err = jpeg_std_error(&m_err.pub);
    329         m_err.pub.error_exit = error_exit;
    330 
    331         // Allocate and initialize JPEG decompression object.
    332         jpeg_create_decompress(&m_info);
    333 
    334         decoder_source_mgr* src = 0;
    335         if (!m_info.src) {
    336             src = (decoder_source_mgr*)fastZeroedMalloc(sizeof(decoder_source_mgr));
    337             if (!src) {
    338                 m_state = JPEG_ERROR;
    339                 return;
    340             }
    341         }
    342 
    343         m_info.src = (jpeg_source_mgr*)src;
    344 
    345         // Set up callback functions.
    346         src->pub.init_source = init_source;
    347         src->pub.fill_input_buffer = fill_input_buffer;
    348         src->pub.skip_input_data = skip_input_data;
    349         src->pub.resync_to_restart = jpeg_resync_to_restart;
    350         src->pub.term_source = term_source;
    351         src->decoder = this;
    352 
    353 #if USE(ICCJPEG)
    354         // Retain ICC color profile markers for color management.
    355         setup_read_icc_profile(&m_info);
    356 #endif
    357 
    358         // Keep APP1 blocks, for obtaining exif data.
    359         jpeg_save_markers(&m_info, exifMarker, 0xFFFF);
    360     }
    361 
    362     ~JPEGImageReader()
    363     {
    364         close();
    365     }
    366 
    367     void close()
    368     {
    369         decoder_source_mgr* src = (decoder_source_mgr*)m_info.src;
    370         if (src)
    371             fastFree(src);
    372         m_info.src = 0;
    373 
    374 #if USE(QCMSLIB)
    375         clearColorTransform();
    376 #endif
    377         jpeg_destroy_decompress(&m_info);
    378     }
    379 
    380     void skipBytes(long numBytes)
    381     {
    382         decoder_source_mgr* src = (decoder_source_mgr*)m_info.src;
    383         long bytesToSkip = std::min(numBytes, (long)src->pub.bytes_in_buffer);
    384         src->pub.bytes_in_buffer -= (size_t)bytesToSkip;
    385         src->pub.next_input_byte += bytesToSkip;
    386 
    387         m_bytesToSkip = std::max(numBytes - bytesToSkip, static_cast<long>(0));
    388     }
    389 
    390     bool decode(const SharedBuffer& data, bool onlySize)
    391     {
    392         unsigned newByteCount = data.size() - m_bufferLength;
    393         unsigned readOffset = m_bufferLength - m_info.src->bytes_in_buffer;
    394 
    395         m_info.src->bytes_in_buffer += newByteCount;
    396         m_info.src->next_input_byte = (JOCTET*)(data.data()) + readOffset;
    397 
    398         // If we still have bytes to skip, try to skip those now.
    399         if (m_bytesToSkip)
    400             skipBytes(m_bytesToSkip);
    401 
    402         m_bufferLength = data.size();
    403 
    404         // We need to do the setjmp here. Otherwise bad things will happen
    405         if (setjmp(m_err.setjmp_buffer))
    406             return m_decoder->setFailed();
    407 
    408         J_COLOR_SPACE overrideColorSpace = JCS_UNKNOWN;
    409         switch (m_state) {
    410         case JPEG_HEADER:
    411             // Read file parameters with jpeg_read_header().
    412             if (jpeg_read_header(&m_info, true) == JPEG_SUSPENDED)
    413                 return false; // I/O suspension.
    414 
    415             switch (m_info.jpeg_color_space) {
    416             case JCS_YCbCr:
    417                 // libjpeg can convert YCbCr image pixels to RGB.
    418                 m_info.out_color_space = rgbOutputColorSpace();
    419                 if (m_decoder->hasImagePlanes() && (yuvSubsampling(m_info) != YUV_UNKNOWN))
    420                     overrideColorSpace = JCS_YCbCr;
    421                 break;
    422             case JCS_GRAYSCALE:
    423             case JCS_RGB:
    424                 // libjpeg can convert GRAYSCALE image pixels to RGB.
    425                 m_info.out_color_space = rgbOutputColorSpace();
    426 #if defined(TURBO_JPEG_RGB_SWIZZLE)
    427                 if (m_info.saw_JFIF_marker)
    428                     break;
    429                 // FIXME: Swizzle decoding does not support Adobe transform=0
    430                 // images (yet), so revert to using JSC_RGB in that case.
    431                 if (m_info.saw_Adobe_marker && !m_info.Adobe_transform)
    432                     m_info.out_color_space = JCS_RGB;
    433 #endif
    434                 break;
    435             case JCS_CMYK:
    436             case JCS_YCCK:
    437                 // libjpeg can convert YCCK to CMYK, but neither to RGB, so we
    438                 // manually convert CMKY to RGB.
    439                 m_info.out_color_space = JCS_CMYK;
    440                 break;
    441             default:
    442                 return m_decoder->setFailed();
    443             }
    444 
    445             m_state = JPEG_START_DECOMPRESS;
    446 
    447             // We can fill in the size now that the header is available.
    448             if (!m_decoder->setSize(m_info.image_width, m_info.image_height))
    449                 return false;
    450 
    451             // Calculate and set decoded size.
    452             m_info.scale_num = m_decoder->desiredScaleNumerator();
    453             m_info.scale_denom = scaleDenominator;
    454             jpeg_calc_output_dimensions(&m_info);
    455             m_decoder->setDecodedSize(m_info.output_width, m_info.output_height);
    456 
    457             m_decoder->setOrientation(readImageOrientation(info()));
    458 
    459 #if USE(QCMSLIB)
    460             // Allow color management of the decoded RGBA pixels if possible.
    461             if (!m_decoder->ignoresGammaAndColorProfile()) {
    462                 ColorProfile colorProfile;
    463                 readColorProfile(info(), colorProfile);
    464                 createColorTransform(colorProfile, colorSpaceHasAlpha(m_info.out_color_space));
    465                 if (m_transform) {
    466                     overrideColorSpace = JCS_UNKNOWN;
    467 #if defined(TURBO_JPEG_RGB_SWIZZLE)
    468                     // Input RGBA data to qcms. Note: restored to BGRA on output.
    469                     if (m_info.out_color_space == JCS_EXT_BGRA)
    470                         m_info.out_color_space = JCS_EXT_RGBA;
    471 #endif
    472                 }
    473                 m_decoder->setHasColorProfile(!!m_transform);
    474             }
    475 #endif
    476             if (overrideColorSpace == JCS_YCbCr) {
    477                 m_info.out_color_space = JCS_YCbCr;
    478                 m_info.raw_data_out = TRUE;
    479                 m_uvSize = computeYUVSize(&m_info, 1, ImageDecoder::SizeForMemoryAllocation); // U size and V size have to be the same if we got here
    480             }
    481 
    482             // Don't allocate a giant and superfluous memory buffer when the
    483             // image is a sequential JPEG.
    484             m_info.buffered_image = jpeg_has_multiple_scans(&m_info);
    485 
    486             if (onlySize) {
    487                 // We can stop here. Reduce our buffer length and available data.
    488                 m_bufferLength -= m_info.src->bytes_in_buffer;
    489                 m_info.src->bytes_in_buffer = 0;
    490                 return true;
    491             }
    492         // FALL THROUGH
    493 
    494         case JPEG_START_DECOMPRESS:
    495             // Set parameters for decompression.
    496             // FIXME -- Should reset dct_method and dither mode for final pass
    497             // of progressive JPEG.
    498             m_info.dct_method = dctMethod();
    499             m_info.dither_mode = ditherMode();
    500             m_info.do_fancy_upsampling = doFancyUpsampling();
    501             m_info.enable_2pass_quant = false;
    502             m_info.do_block_smoothing = true;
    503 
    504             // Make a one-row-high sample array that will go away when done with
    505             // image. Always make it big enough to hold an RGB row. Since this
    506             // uses the IJG memory manager, it must be allocated before the call
    507             // to jpeg_start_compress().
    508             // FIXME: note that some output color spaces do not need the samples
    509             // buffer. Remove this allocation for those color spaces.
    510             m_samples = (*m_info.mem->alloc_sarray)(reinterpret_cast<j_common_ptr>(&m_info), JPOOL_IMAGE, m_info.output_width * 4, m_info.out_color_space == JCS_YCbCr ? 2 : 1);
    511 
    512             // Start decompressor.
    513             if (!jpeg_start_decompress(&m_info))
    514                 return false; // I/O suspension.
    515 
    516             // If this is a progressive JPEG ...
    517             m_state = (m_info.buffered_image) ? JPEG_DECOMPRESS_PROGRESSIVE : JPEG_DECOMPRESS_SEQUENTIAL;
    518         // FALL THROUGH
    519 
    520         case JPEG_DECOMPRESS_SEQUENTIAL:
    521             if (m_state == JPEG_DECOMPRESS_SEQUENTIAL) {
    522 
    523                 if (!m_decoder->outputScanlines())
    524                     return false; // I/O suspension.
    525 
    526                 // If we've completed image output...
    527                 ASSERT(m_info.output_scanline == m_info.output_height);
    528                 m_state = JPEG_DONE;
    529             }
    530         // FALL THROUGH
    531 
    532         case JPEG_DECOMPRESS_PROGRESSIVE:
    533             if (m_state == JPEG_DECOMPRESS_PROGRESSIVE) {
    534                 int status;
    535                 do {
    536                     status = jpeg_consume_input(&m_info);
    537                 } while ((status != JPEG_SUSPENDED) && (status != JPEG_REACHED_EOI));
    538 
    539                 for (;;) {
    540                     if (!m_info.output_scanline) {
    541                         int scan = m_info.input_scan_number;
    542 
    543                         // If we haven't displayed anything yet
    544                         // (output_scan_number == 0) and we have enough data for
    545                         // a complete scan, force output of the last full scan.
    546                         if (!m_info.output_scan_number && (scan > 1) && (status != JPEG_REACHED_EOI))
    547                             --scan;
    548 
    549                         if (!jpeg_start_output(&m_info, scan))
    550                             return false; // I/O suspension.
    551                     }
    552 
    553                     if (m_info.output_scanline == 0xffffff)
    554                         m_info.output_scanline = 0;
    555 
    556                     // If outputScanlines() fails, it deletes |this|. Therefore,
    557                     // copy the decoder pointer and use it to check for failure
    558                     // to avoid member access in the failure case.
    559                     JPEGImageDecoder* decoder = m_decoder;
    560                     if (!decoder->outputScanlines()) {
    561                         if (decoder->failed()) // Careful; |this| is deleted.
    562                             return false;
    563                         if (!m_info.output_scanline)
    564                             // Didn't manage to read any lines - flag so we
    565                             // don't call jpeg_start_output() multiple times for
    566                             // the same scan.
    567                             m_info.output_scanline = 0xffffff;
    568                         return false; // I/O suspension.
    569                     }
    570 
    571                     if (m_info.output_scanline == m_info.output_height) {
    572                         if (!jpeg_finish_output(&m_info))
    573                             return false; // I/O suspension.
    574 
    575                         if (jpeg_input_complete(&m_info) && (m_info.input_scan_number == m_info.output_scan_number))
    576                             break;
    577 
    578                         m_info.output_scanline = 0;
    579                     }
    580                 }
    581 
    582                 m_state = JPEG_DONE;
    583             }
    584         // FALL THROUGH
    585 
    586         case JPEG_DONE:
    587             // Finish decompression.
    588             return jpeg_finish_decompress(&m_info);
    589 
    590         case JPEG_ERROR:
    591             // We can get here if the constructor failed.
    592             return m_decoder->setFailed();
    593         }
    594 
    595         return true;
    596     }
    597 
    598     jpeg_decompress_struct* info() { return &m_info; }
    599     JSAMPARRAY samples() const { return m_samples; }
    600     JPEGImageDecoder* decoder() { return m_decoder; }
    601     IntSize uvSize() const { return m_uvSize; }
    602 #if USE(QCMSLIB)
    603     qcms_transform* colorTransform() const { return m_transform; }
    604 
    605     void clearColorTransform()
    606     {
    607         if (m_transform)
    608             qcms_transform_release(m_transform);
    609         m_transform = 0;
    610     }
    611 
    612     void createColorTransform(const ColorProfile& colorProfile, bool hasAlpha)
    613     {
    614         clearColorTransform();
    615 
    616         if (colorProfile.isEmpty())
    617             return;
    618         qcms_profile* deviceProfile = ImageDecoder::qcmsOutputDeviceProfile();
    619         if (!deviceProfile)
    620             return;
    621         qcms_profile* inputProfile = qcms_profile_from_memory(colorProfile.data(), colorProfile.size());
    622         if (!inputProfile)
    623             return;
    624         // We currently only support color profiles for RGB profiled images.
    625         ASSERT(icSigRgbData == qcms_profile_get_color_space(inputProfile));
    626         qcms_data_type dataFormat = hasAlpha ? QCMS_DATA_RGBA_8 : QCMS_DATA_RGB_8;
    627         // FIXME: Don't force perceptual intent if the image profile contains an intent.
    628         m_transform = qcms_transform_create(inputProfile, dataFormat, deviceProfile, dataFormat, QCMS_INTENT_PERCEPTUAL);
    629         qcms_profile_release(inputProfile);
    630     }
    631 #endif
    632 
    633 private:
    634     JPEGImageDecoder* m_decoder;
    635     unsigned m_bufferLength;
    636     int m_bytesToSkip;
    637 
    638     jpeg_decompress_struct m_info;
    639     decoder_error_mgr m_err;
    640     jstate m_state;
    641 
    642     JSAMPARRAY m_samples;
    643 
    644     IntSize m_uvSize;
    645 
    646 #if USE(QCMSLIB)
    647     qcms_transform* m_transform;
    648 #endif
    649 };
    650 
    651 // Override the standard error method in the IJG JPEG decoder code.
    652 void error_exit(j_common_ptr cinfo)
    653 {
    654     // Return control to the setjmp point.
    655     decoder_error_mgr *err = reinterpret_cast_ptr<decoder_error_mgr *>(cinfo->err);
    656     longjmp(err->setjmp_buffer, -1);
    657 }
    658 
    659 void init_source(j_decompress_ptr)
    660 {
    661 }
    662 
    663 void skip_input_data(j_decompress_ptr jd, long num_bytes)
    664 {
    665     decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
    666     src->decoder->skipBytes(num_bytes);
    667 }
    668 
    669 boolean fill_input_buffer(j_decompress_ptr)
    670 {
    671     // Our decode step always sets things up properly, so if this method is ever
    672     // called, then we have hit the end of the buffer.  A return value of false
    673     // indicates that we have no data to supply yet.
    674     return false;
    675 }
    676 
    677 void term_source(j_decompress_ptr jd)
    678 {
    679     decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
    680     src->decoder->decoder()->jpegComplete();
    681 }
    682 
    683 JPEGImageDecoder::JPEGImageDecoder(ImageSource::AlphaOption alphaOption,
    684     ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption,
    685     size_t maxDecodedBytes)
    686     : ImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes)
    687     , m_hasColorProfile(false)
    688 {
    689 }
    690 
    691 JPEGImageDecoder::~JPEGImageDecoder()
    692 {
    693 }
    694 
    695 bool JPEGImageDecoder::isSizeAvailable()
    696 {
    697     if (!ImageDecoder::isSizeAvailable())
    698          decode(true);
    699 
    700     return ImageDecoder::isSizeAvailable();
    701 }
    702 
    703 bool JPEGImageDecoder::setSize(unsigned width, unsigned height)
    704 {
    705     if (!ImageDecoder::setSize(width, height))
    706         return false;
    707 
    708     if (!desiredScaleNumerator())
    709         return setFailed();
    710 
    711     setDecodedSize(width, height);
    712     return true;
    713 }
    714 
    715 void JPEGImageDecoder::setDecodedSize(unsigned width, unsigned height)
    716 {
    717     m_decodedSize = IntSize(width, height);
    718 }
    719 
    720 IntSize JPEGImageDecoder::decodedYUVSize(int component, ImageDecoder::SizeType sizeType) const
    721 {
    722     ASSERT((component >= 0) && (component <= 2) && m_reader);
    723     const jpeg_decompress_struct* info = m_reader->info();
    724 
    725     ASSERT(info->out_color_space == JCS_YCbCr);
    726     return computeYUVSize(info, component, sizeType);
    727 }
    728 
    729 unsigned JPEGImageDecoder::desiredScaleNumerator() const
    730 {
    731     size_t originalBytes = size().width() * size().height() * 4;
    732     if (originalBytes <= m_maxDecodedBytes) {
    733         return scaleDenominator;
    734     }
    735 
    736     // Downsample according to the maximum decoded size.
    737     unsigned scaleNumerator = static_cast<unsigned>(floor(sqrt(
    738         // MSVC needs explicit parameter type for sqrt().
    739         static_cast<float>(m_maxDecodedBytes * scaleDenominator * scaleDenominator / originalBytes))));
    740 
    741     return scaleNumerator;
    742 }
    743 
    744 bool JPEGImageDecoder::canDecodeToYUV() const
    745 {
    746     ASSERT(ImageDecoder::isSizeAvailable() && m_reader);
    747 
    748     return m_reader->info()->out_color_space == JCS_YCbCr;
    749 }
    750 
    751 bool JPEGImageDecoder::decodeToYUV()
    752 {
    753     if (!hasImagePlanes())
    754         return false;
    755     PlatformInstrumentation::willDecodeImage("JPEG");
    756     decode(false);
    757     PlatformInstrumentation::didDecodeImage();
    758     return !failed();
    759 }
    760 
    761 ImageFrame* JPEGImageDecoder::frameBufferAtIndex(size_t index)
    762 {
    763     if (index)
    764         return 0;
    765 
    766     if (m_frameBufferCache.isEmpty()) {
    767         m_frameBufferCache.resize(1);
    768         m_frameBufferCache[0].setPremultiplyAlpha(m_premultiplyAlpha);
    769     }
    770 
    771     ImageFrame& frame = m_frameBufferCache[0];
    772     if (frame.status() != ImageFrame::FrameComplete) {
    773         PlatformInstrumentation::willDecodeImage("JPEG");
    774         decode(false);
    775         PlatformInstrumentation::didDecodeImage();
    776     }
    777 
    778     frame.notifyBitmapIfPixelsChanged();
    779     return &frame;
    780 }
    781 
    782 bool JPEGImageDecoder::setFailed()
    783 {
    784     m_reader.clear();
    785     return ImageDecoder::setFailed();
    786 }
    787 
    788 void JPEGImageDecoder::setImagePlanes(PassOwnPtr<ImagePlanes> imagePlanes)
    789 {
    790     m_imagePlanes = imagePlanes;
    791 }
    792 
    793 template <J_COLOR_SPACE colorSpace> void setPixel(ImageFrame& buffer, ImageFrame::PixelData* pixel, JSAMPARRAY samples, int column)
    794 {
    795     ASSERT_NOT_REACHED();
    796 }
    797 
    798 template <> void setPixel<JCS_RGB>(ImageFrame& buffer, ImageFrame::PixelData* pixel, JSAMPARRAY samples, int column)
    799 {
    800     JSAMPLE* jsample = *samples + column * 3;
    801     buffer.setRGBARaw(pixel, jsample[0], jsample[1], jsample[2], 255);
    802 }
    803 
    804 template <> void setPixel<JCS_CMYK>(ImageFrame& buffer, ImageFrame::PixelData* pixel, JSAMPARRAY samples, int column)
    805 {
    806     JSAMPLE* jsample = *samples + column * 4;
    807 
    808     // Source is 'Inverted CMYK', output is RGB.
    809     // See: http://www.easyrgb.com/math.php?MATH=M12#text12
    810     // Or: http://www.ilkeratalay.com/colorspacesfaq.php#rgb
    811     // From CMYK to CMY:
    812     // X =   X    * (1 -   K   ) +   K  [for X = C, M, or Y]
    813     // Thus, from Inverted CMYK to CMY is:
    814     // X = (1-iX) * (1 - (1-iK)) + (1-iK) => 1 - iX*iK
    815     // From CMY (0..1) to RGB (0..1):
    816     // R = 1 - C => 1 - (1 - iC*iK) => iC*iK  [G and B similar]
    817     unsigned k = jsample[3];
    818     buffer.setRGBARaw(pixel, jsample[0] * k / 255, jsample[1] * k / 255, jsample[2] * k / 255, 255);
    819 }
    820 
    821 template <J_COLOR_SPACE colorSpace> bool outputRows(JPEGImageReader* reader, ImageFrame& buffer)
    822 {
    823     JSAMPARRAY samples = reader->samples();
    824     jpeg_decompress_struct* info = reader->info();
    825     int width = info->output_width;
    826 
    827     while (info->output_scanline < info->output_height) {
    828         // jpeg_read_scanlines will increase the scanline counter, so we
    829         // save the scanline before calling it.
    830         int y = info->output_scanline;
    831         // Request one scanline: returns 0 or 1 scanlines.
    832         if (jpeg_read_scanlines(info, samples, 1) != 1)
    833             return false;
    834 #if USE(QCMSLIB)
    835         if (reader->colorTransform() && colorSpace == JCS_RGB)
    836             qcms_transform_data(reader->colorTransform(), *samples, *samples, width);
    837 #endif
    838         ImageFrame::PixelData* pixel = buffer.getAddr(0, y);
    839         for (int x = 0; x < width; ++pixel, ++x)
    840             setPixel<colorSpace>(buffer, pixel, samples, x);
    841     }
    842 
    843     buffer.setPixelsChanged(true);
    844     return true;
    845 }
    846 
    847 static bool outputRawData(JPEGImageReader* reader, ImagePlanes* imagePlanes)
    848 {
    849     JSAMPARRAY samples = reader->samples();
    850     jpeg_decompress_struct* info = reader->info();
    851     JSAMPARRAY bufferraw[3];
    852     JSAMPROW bufferraw2[32];
    853     bufferraw[0] = &bufferraw2[0]; // Y channel rows (8 or 16)
    854     bufferraw[1] = &bufferraw2[16]; // U channel rows (8)
    855     bufferraw[2] = &bufferraw2[24]; // V channel rows (8)
    856     int yWidth = info->output_width;
    857     int yHeight = info->output_height;
    858     int yMaxH = yHeight - 1;
    859     int v = info->cur_comp_info[0]->v_samp_factor;
    860     IntSize uvSize = reader->uvSize();
    861     int uvMaxH = uvSize.height() - 1;
    862     JSAMPROW outputY = static_cast<JSAMPROW>(imagePlanes->plane(0));
    863     JSAMPROW outputU = static_cast<JSAMPROW>(imagePlanes->plane(1));
    864     JSAMPROW outputV = static_cast<JSAMPROW>(imagePlanes->plane(2));
    865     size_t rowBytesY = imagePlanes->rowBytes(0);
    866     size_t rowBytesU = imagePlanes->rowBytes(1);
    867     size_t rowBytesV = imagePlanes->rowBytes(2);
    868 
    869     int yScanlinesToRead = DCTSIZE * v;
    870     JSAMPROW yLastRow = *samples;
    871     JSAMPROW uLastRow = yLastRow + 2 * yWidth;
    872     JSAMPROW vLastRow = uLastRow + 2 * yWidth;
    873     JSAMPROW dummyRow = vLastRow + 2 * yWidth;
    874 
    875     while (info->output_scanline < info->output_height) {
    876         // Request 8 or 16 scanlines: returns 0 or more scanlines.
    877         bool hasYLastRow(false), hasUVLastRow(false);
    878         // Assign 8 or 16 rows of memory to read the Y channel.
    879         for (int i = 0; i < yScanlinesToRead; ++i) {
    880             int scanline = (info->output_scanline + i);
    881             if (scanline < yMaxH) {
    882                 bufferraw2[i] = &outputY[scanline * rowBytesY];
    883             } else if (scanline == yMaxH) {
    884                 bufferraw2[i] = yLastRow;
    885                 hasYLastRow = true;
    886             } else {
    887                 bufferraw2[i] = dummyRow;
    888             }
    889         }
    890         int scaledScanline = info->output_scanline / v;
    891         // Assign 8 rows of memory to read the U and V channels.
    892         for (int i = 0; i < 8; ++i) {
    893             int scanline = (scaledScanline + i);
    894             if (scanline < uvMaxH) {
    895                 bufferraw2[16 + i] = &outputU[scanline * rowBytesU];
    896                 bufferraw2[24 + i] = &outputV[scanline * rowBytesV];
    897             } else if (scanline == uvMaxH) {
    898                 bufferraw2[16 + i] = uLastRow;
    899                 bufferraw2[24 + i] = vLastRow;
    900                 hasUVLastRow = true;
    901             } else {
    902                 bufferraw2[16 + i] = dummyRow;
    903                 bufferraw2[24 + i] = dummyRow;
    904             }
    905         }
    906         JDIMENSION scanlinesRead = jpeg_read_raw_data(info, bufferraw, yScanlinesToRead);
    907 
    908         if (scanlinesRead == 0)
    909             return false;
    910 
    911         if (hasYLastRow) {
    912             memcpy(&outputY[yMaxH * rowBytesY], yLastRow, yWidth);
    913         }
    914         if (hasUVLastRow) {
    915             memcpy(&outputU[uvMaxH * rowBytesU], uLastRow, uvSize.width());
    916             memcpy(&outputV[uvMaxH * rowBytesV], vLastRow, uvSize.width());
    917         }
    918     }
    919 
    920     info->output_scanline = std::min(info->output_scanline, info->output_height);
    921 
    922     return true;
    923 }
    924 
    925 bool JPEGImageDecoder::outputScanlines()
    926 {
    927     if (hasImagePlanes()) {
    928         return outputRawData(m_reader.get(), m_imagePlanes.get());
    929     }
    930 
    931     if (m_frameBufferCache.isEmpty())
    932         return false;
    933 
    934     jpeg_decompress_struct* info = m_reader->info();
    935 
    936     // Initialize the framebuffer if needed.
    937     ImageFrame& buffer = m_frameBufferCache[0];
    938     if (buffer.status() == ImageFrame::FrameEmpty) {
    939         ASSERT(info->output_width == static_cast<JDIMENSION>(m_decodedSize.width()));
    940         ASSERT(info->output_height == static_cast<JDIMENSION>(m_decodedSize.height()));
    941 
    942         if (!buffer.setSize(info->output_width, info->output_height))
    943             return setFailed();
    944         buffer.setStatus(ImageFrame::FramePartial);
    945         // The buffer is transparent outside the decoded area while the image is
    946         // loading. The completed image will be marked fully opaque in jpegComplete().
    947         buffer.setHasAlpha(true);
    948 
    949         // For JPEGs, the frame always fills the entire image.
    950         buffer.setOriginalFrameRect(IntRect(IntPoint(), size()));
    951     }
    952 
    953 #if defined(TURBO_JPEG_RGB_SWIZZLE)
    954     if (turboSwizzled(info->out_color_space)) {
    955         while (info->output_scanline < info->output_height) {
    956             unsigned char* row = reinterpret_cast<unsigned char*>(buffer.getAddr(0, info->output_scanline));
    957             if (jpeg_read_scanlines(info, &row, 1) != 1)
    958                 return false;
    959 #if USE(QCMSLIB)
    960             if (qcms_transform* transform = m_reader->colorTransform())
    961                 qcms_transform_data_type(transform, row, row, info->output_width, rgbOutputColorSpace() == JCS_EXT_BGRA ? QCMS_OUTPUT_BGRX : QCMS_OUTPUT_RGBX);
    962 #endif
    963         }
    964         buffer.setPixelsChanged(true);
    965         return true;
    966     }
    967 #endif
    968 
    969     switch (info->out_color_space) {
    970     case JCS_RGB:
    971         return outputRows<JCS_RGB>(m_reader.get(), buffer);
    972     case JCS_CMYK:
    973         return outputRows<JCS_CMYK>(m_reader.get(), buffer);
    974     default:
    975         ASSERT_NOT_REACHED();
    976     }
    977 
    978     return setFailed();
    979 }
    980 
    981 void JPEGImageDecoder::jpegComplete()
    982 {
    983     if (m_frameBufferCache.isEmpty())
    984         return;
    985 
    986     // Hand back an appropriately sized buffer, even if the image ended up being
    987     // empty.
    988     ImageFrame& buffer = m_frameBufferCache[0];
    989     buffer.setHasAlpha(false);
    990     buffer.setStatus(ImageFrame::FrameComplete);
    991 }
    992 
    993 void JPEGImageDecoder::decode(bool onlySize)
    994 {
    995     if (failed())
    996         return;
    997 
    998     if (!m_reader) {
    999         m_reader = adoptPtr(new JPEGImageReader(this));
   1000     }
   1001 
   1002     // If we couldn't decode the image but we've received all the data, decoding
   1003     // has failed.
   1004     if (!m_reader->decode(*m_data, onlySize) && isAllDataReceived())
   1005         setFailed();
   1006     // If we're done decoding the image, we don't need the JPEGImageReader
   1007     // anymore.  (If we failed, |m_reader| has already been cleared.)
   1008     else if ((!m_frameBufferCache.isEmpty() && (m_frameBufferCache[0].status() == ImageFrame::FrameComplete)) || (hasImagePlanes() && !onlySize))
   1009         m_reader.clear();
   1010 }
   1011 
   1012 }
   1013