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 "JPEGImageDecoder.h"
     42 #include <stdio.h>  // Needed by jpeglib.h for FILE.
     43 
     44 #if OS(WINCE) || PLATFORM(BREWMP_SIMULATOR)
     45 // Remove warning: 'FAR' macro redefinition
     46 #undef FAR
     47 
     48 // jmorecfg.h in libjpeg checks for XMD_H with the comment: "X11/xmd.h correctly defines INT32"
     49 // fix INT32 redefinition error by pretending we are X11/xmd.h
     50 #define XMD_H
     51 #endif
     52 
     53 extern "C" {
     54 
     55 #include "jpeglib.h"
     56 
     57 #if USE(ICCJPEG)
     58 #include "iccjpeg.h"
     59 #endif
     60 
     61 }
     62 
     63 #include <setjmp.h>
     64 
     65 namespace WebCore {
     66 
     67 struct decoder_error_mgr {
     68     struct jpeg_error_mgr pub; // "public" fields for IJG library
     69     jmp_buf setjmp_buffer;     // For handling catastropic errors
     70 };
     71 
     72 enum jstate {
     73     JPEG_HEADER,                 // Reading JFIF headers
     74     JPEG_START_DECOMPRESS,
     75     JPEG_DECOMPRESS_PROGRESSIVE, // Output progressive pixels
     76     JPEG_DECOMPRESS_SEQUENTIAL,  // Output sequential pixels
     77     JPEG_DONE,
     78     JPEG_ERROR
     79 };
     80 
     81 void init_source(j_decompress_ptr jd);
     82 boolean fill_input_buffer(j_decompress_ptr jd);
     83 void skip_input_data(j_decompress_ptr jd, long num_bytes);
     84 void term_source(j_decompress_ptr jd);
     85 void error_exit(j_common_ptr cinfo);
     86 
     87 // Implementation of a JPEG src object that understands our state machine
     88 struct decoder_source_mgr {
     89     // public fields; must be first in this struct!
     90     struct jpeg_source_mgr pub;
     91 
     92     JPEGImageReader* decoder;
     93 };
     94 
     95 static ColorProfile readColorProfile(jpeg_decompress_struct* info)
     96 {
     97 #if USE(ICCJPEG)
     98     JOCTET* profile;
     99     unsigned int profileLength;
    100 
    101     if (!read_icc_profile(info, &profile, &profileLength))
    102         return ColorProfile();
    103 
    104     ColorProfile colorProfile;
    105     colorProfile.append(reinterpret_cast<char*>(profile), profileLength);
    106     free(profile);
    107     return colorProfile;
    108 #else
    109     return ColorProfile();
    110 #endif
    111 }
    112 
    113 class JPEGImageReader
    114 {
    115 public:
    116     JPEGImageReader(JPEGImageDecoder* decoder)
    117         : m_decoder(decoder)
    118         , m_bufferLength(0)
    119         , m_bytesToSkip(0)
    120         , m_state(JPEG_HEADER)
    121         , m_samples(0)
    122     {
    123         memset(&m_info, 0, sizeof(jpeg_decompress_struct));
    124 
    125         // We set up the normal JPEG error routines, then override error_exit.
    126         m_info.err = jpeg_std_error(&m_err.pub);
    127         m_err.pub.error_exit = error_exit;
    128 
    129         // Allocate and initialize JPEG decompression object.
    130         jpeg_create_decompress(&m_info);
    131 
    132         decoder_source_mgr* src = 0;
    133         if (!m_info.src) {
    134             src = (decoder_source_mgr*)fastCalloc(sizeof(decoder_source_mgr), 1);
    135             if (!src) {
    136                 m_state = JPEG_ERROR;
    137                 return;
    138             }
    139         }
    140 
    141         m_info.src = (jpeg_source_mgr*)src;
    142 
    143         // Set up callback functions.
    144         src->pub.init_source = init_source;
    145         src->pub.fill_input_buffer = fill_input_buffer;
    146         src->pub.skip_input_data = skip_input_data;
    147         src->pub.resync_to_restart = jpeg_resync_to_restart;
    148         src->pub.term_source = term_source;
    149         src->decoder = this;
    150 
    151         // Enable these markers for the ICC color profile.
    152         // Apparently there are 16 of these markers.  I don't see anywhere in the header with this constant.
    153         for (unsigned i = 0; i < 0xF; ++i)
    154             jpeg_save_markers(&m_info, JPEG_APP0 + i, 0xFFFF);
    155     }
    156 
    157     ~JPEGImageReader()
    158     {
    159         close();
    160     }
    161 
    162     void close()
    163     {
    164         decoder_source_mgr* src = (decoder_source_mgr*)m_info.src;
    165         if (src)
    166             fastFree(src);
    167         m_info.src = 0;
    168 
    169         jpeg_destroy_decompress(&m_info);
    170     }
    171 
    172     void skipBytes(long numBytes)
    173     {
    174         decoder_source_mgr* src = (decoder_source_mgr*)m_info.src;
    175         long bytesToSkip = std::min(numBytes, (long)src->pub.bytes_in_buffer);
    176         src->pub.bytes_in_buffer -= (size_t)bytesToSkip;
    177         src->pub.next_input_byte += bytesToSkip;
    178 
    179         m_bytesToSkip = std::max(numBytes - bytesToSkip, static_cast<long>(0));
    180     }
    181 
    182     bool decode(const SharedBuffer& data, bool onlySize)
    183     {
    184         m_decodingSizeOnly = onlySize;
    185 
    186         unsigned newByteCount = data.size() - m_bufferLength;
    187         unsigned readOffset = m_bufferLength - m_info.src->bytes_in_buffer;
    188 
    189         m_info.src->bytes_in_buffer += newByteCount;
    190         m_info.src->next_input_byte = (JOCTET*)(data.data()) + readOffset;
    191 
    192         // If we still have bytes to skip, try to skip those now.
    193         if (m_bytesToSkip)
    194             skipBytes(m_bytesToSkip);
    195 
    196         m_bufferLength = data.size();
    197 
    198         // We need to do the setjmp here. Otherwise bad things will happen
    199         if (setjmp(m_err.setjmp_buffer))
    200             return m_decoder->setFailed();
    201 
    202         switch (m_state) {
    203         case JPEG_HEADER:
    204             // Read file parameters with jpeg_read_header().
    205             if (jpeg_read_header(&m_info, true) == JPEG_SUSPENDED)
    206                 return false; // I/O suspension.
    207 
    208             // Let libjpeg take care of gray->RGB and YCbCr->RGB conversions.
    209             switch (m_info.jpeg_color_space) {
    210             case JCS_GRAYSCALE:
    211             case JCS_YCbCr:
    212                 // Grayscale images get "upsampled" by libjpeg.  If we use
    213                 // their color profile, CoreGraphics will "upsample" them
    214                 // again, resulting in horizontal distortions.
    215                 m_decoder->setIgnoreGammaAndColorProfile(true);
    216                 // Note fall-through!
    217             case JCS_RGB:
    218                 m_info.out_color_space = JCS_RGB;
    219                 break;
    220             case JCS_CMYK:
    221             case JCS_YCCK:
    222                 // jpeglib cannot convert these to rgb, but it can convert ycck
    223                 // to cmyk.
    224                 m_info.out_color_space = JCS_CMYK;
    225 
    226                 // Same as with grayscale images, we convert CMYK images to RGBA
    227                 // ones. When we keep the color profiles of these CMYK images,
    228                 // CoreGraphics will convert their colors again. So, we discard
    229                 // their color profiles to prevent color corruption.
    230                 m_decoder->setIgnoreGammaAndColorProfile(true);
    231                 break;
    232             default:
    233                 return m_decoder->setFailed();
    234             }
    235 
    236             // Don't allocate a giant and superfluous memory buffer when the
    237             // image is a sequential JPEG.
    238             m_info.buffered_image = jpeg_has_multiple_scans(&m_info);
    239 
    240             // Used to set up image size so arrays can be allocated.
    241             jpeg_calc_output_dimensions(&m_info);
    242 
    243             // Make a one-row-high sample array that will go away when done with
    244             // image. Always make it big enough to hold an RGB row.  Since this
    245             // uses the IJG memory manager, it must be allocated before the call
    246             // to jpeg_start_compress().
    247             m_samples = (*m_info.mem->alloc_sarray)((j_common_ptr) &m_info, JPOOL_IMAGE, m_info.output_width * 4, 1);
    248 
    249             m_state = JPEG_START_DECOMPRESS;
    250 
    251             // We can fill in the size now that the header is available.
    252             if (!m_decoder->setSize(m_info.image_width, m_info.image_height))
    253                 return false;
    254 
    255             if (!m_decoder->ignoresGammaAndColorProfile())
    256                 m_decoder->setColorProfile(readColorProfile(info()));
    257 
    258             if (m_decodingSizeOnly) {
    259                 // We can stop here.  Reduce our buffer length and available
    260                 // data.
    261                 m_bufferLength -= m_info.src->bytes_in_buffer;
    262                 m_info.src->bytes_in_buffer = 0;
    263                 return true;
    264             }
    265         // FALL THROUGH
    266 
    267         case JPEG_START_DECOMPRESS:
    268             // Set parameters for decompression.
    269             // FIXME -- Should reset dct_method and dither mode for final pass
    270             // of progressive JPEG.
    271             m_info.dct_method =  JDCT_ISLOW;
    272             m_info.dither_mode = JDITHER_FS;
    273             m_info.do_fancy_upsampling = true;
    274             m_info.enable_2pass_quant = false;
    275             m_info.do_block_smoothing = true;
    276 
    277             // Start decompressor.
    278             if (!jpeg_start_decompress(&m_info))
    279                 return false; // I/O suspension.
    280 
    281             // If this is a progressive JPEG ...
    282             m_state = (m_info.buffered_image) ? JPEG_DECOMPRESS_PROGRESSIVE : JPEG_DECOMPRESS_SEQUENTIAL;
    283         // FALL THROUGH
    284 
    285         case JPEG_DECOMPRESS_SEQUENTIAL:
    286             if (m_state == JPEG_DECOMPRESS_SEQUENTIAL) {
    287 
    288                 if (!m_decoder->outputScanlines())
    289                     return false; // I/O suspension.
    290 
    291                 // If we've completed image output...
    292                 ASSERT(m_info.output_scanline == m_info.output_height);
    293                 m_state = JPEG_DONE;
    294             }
    295         // FALL THROUGH
    296 
    297         case JPEG_DECOMPRESS_PROGRESSIVE:
    298             if (m_state == JPEG_DECOMPRESS_PROGRESSIVE) {
    299                 int status;
    300                 do {
    301                     status = jpeg_consume_input(&m_info);
    302                 } while ((status != JPEG_SUSPENDED) && (status != JPEG_REACHED_EOI));
    303 
    304                 for (;;) {
    305                     if (!m_info.output_scanline) {
    306                         int scan = m_info.input_scan_number;
    307 
    308                         // If we haven't displayed anything yet
    309                         // (output_scan_number == 0) and we have enough data for
    310                         // a complete scan, force output of the last full scan.
    311                         if (!m_info.output_scan_number && (scan > 1) && (status != JPEG_REACHED_EOI))
    312                             --scan;
    313 
    314                         if (!jpeg_start_output(&m_info, scan))
    315                             return false; // I/O suspension.
    316                     }
    317 
    318                     if (m_info.output_scanline == 0xffffff)
    319                         m_info.output_scanline = 0;
    320 
    321                     if (!m_decoder->outputScanlines()) {
    322                         if (!m_info.output_scanline)
    323                             // Didn't manage to read any lines - flag so we
    324                             // don't call jpeg_start_output() multiple times for
    325                             // the same scan.
    326                             m_info.output_scanline = 0xffffff;
    327                         return false; // I/O suspension.
    328                     }
    329 
    330                     if (m_info.output_scanline == m_info.output_height) {
    331                         if (!jpeg_finish_output(&m_info))
    332                             return false; // I/O suspension.
    333 
    334                         if (jpeg_input_complete(&m_info) && (m_info.input_scan_number == m_info.output_scan_number))
    335                             break;
    336 
    337                         m_info.output_scanline = 0;
    338                     }
    339                 }
    340 
    341                 m_state = JPEG_DONE;
    342             }
    343         // FALL THROUGH
    344 
    345         case JPEG_DONE:
    346             // Finish decompression.
    347             return jpeg_finish_decompress(&m_info);
    348 
    349         case JPEG_ERROR:
    350             // We can get here if the constructor failed.
    351             return m_decoder->setFailed();
    352         }
    353 
    354         return true;
    355     }
    356 
    357     jpeg_decompress_struct* info() { return &m_info; }
    358     JSAMPARRAY samples() const { return m_samples; }
    359     JPEGImageDecoder* decoder() { return m_decoder; }
    360 
    361 private:
    362     JPEGImageDecoder* m_decoder;
    363     unsigned m_bufferLength;
    364     int m_bytesToSkip;
    365     bool m_decodingSizeOnly;
    366     bool m_initialized;
    367 
    368     jpeg_decompress_struct m_info;
    369     decoder_error_mgr m_err;
    370     jstate m_state;
    371 
    372     JSAMPARRAY m_samples;
    373 };
    374 
    375 // Override the standard error method in the IJG JPEG decoder code.
    376 void error_exit(j_common_ptr cinfo)
    377 {
    378     // Return control to the setjmp point.
    379     decoder_error_mgr *err = (decoder_error_mgr *) cinfo->err;
    380     longjmp(err->setjmp_buffer, -1);
    381 }
    382 
    383 void init_source(j_decompress_ptr jd)
    384 {
    385 }
    386 
    387 void skip_input_data(j_decompress_ptr jd, long num_bytes)
    388 {
    389     decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
    390     src->decoder->skipBytes(num_bytes);
    391 }
    392 
    393 boolean fill_input_buffer(j_decompress_ptr jd)
    394 {
    395     // Our decode step always sets things up properly, so if this method is ever
    396     // called, then we have hit the end of the buffer.  A return value of false
    397     // indicates that we have no data to supply yet.
    398     return false;
    399 }
    400 
    401 void term_source(j_decompress_ptr jd)
    402 {
    403     decoder_source_mgr *src = (decoder_source_mgr *)jd->src;
    404     src->decoder->decoder()->jpegComplete();
    405 }
    406 
    407 JPEGImageDecoder::JPEGImageDecoder(ImageSource::AlphaOption alphaOption,
    408                                    ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
    409     : ImageDecoder(alphaOption, gammaAndColorProfileOption)
    410 {
    411 }
    412 
    413 JPEGImageDecoder::~JPEGImageDecoder()
    414 {
    415 }
    416 
    417 bool JPEGImageDecoder::isSizeAvailable()
    418 {
    419     if (!ImageDecoder::isSizeAvailable())
    420          decode(true);
    421 
    422     return ImageDecoder::isSizeAvailable();
    423 }
    424 
    425 bool JPEGImageDecoder::setSize(unsigned width, unsigned height)
    426 {
    427     if (!ImageDecoder::setSize(width, height))
    428         return false;
    429 
    430     prepareScaleDataIfNecessary();
    431     return true;
    432 }
    433 
    434 ImageFrame* JPEGImageDecoder::frameBufferAtIndex(size_t index)
    435 {
    436     if (index)
    437         return 0;
    438 
    439     if (m_frameBufferCache.isEmpty()) {
    440         m_frameBufferCache.resize(1);
    441         m_frameBufferCache[0].setPremultiplyAlpha(m_premultiplyAlpha);
    442     }
    443 
    444     ImageFrame& frame = m_frameBufferCache[0];
    445     if (frame.status() != ImageFrame::FrameComplete)
    446         decode(false);
    447     return &frame;
    448 }
    449 
    450 bool JPEGImageDecoder::setFailed()
    451 {
    452     m_reader.clear();
    453     return ImageDecoder::setFailed();
    454 }
    455 
    456 bool JPEGImageDecoder::outputScanlines()
    457 {
    458     if (m_frameBufferCache.isEmpty())
    459         return false;
    460 
    461     // Initialize the framebuffer if needed.
    462     ImageFrame& buffer = m_frameBufferCache[0];
    463     if (buffer.status() == ImageFrame::FrameEmpty) {
    464         if (!buffer.setSize(scaledSize().width(), scaledSize().height()))
    465             return setFailed();
    466         buffer.setStatus(ImageFrame::FramePartial);
    467         buffer.setHasAlpha(false);
    468         buffer.setColorProfile(m_colorProfile);
    469 
    470         // For JPEGs, the frame always fills the entire image.
    471         buffer.setOriginalFrameRect(IntRect(IntPoint(), size()));
    472     }
    473 
    474     jpeg_decompress_struct* info = m_reader->info();
    475     JSAMPARRAY samples = m_reader->samples();
    476 
    477     while (info->output_scanline < info->output_height) {
    478         // jpeg_read_scanlines will increase the scanline counter, so we
    479         // save the scanline before calling it.
    480         int sourceY = info->output_scanline;
    481         /* Request one scanline.  Returns 0 or 1 scanlines. */
    482         if (jpeg_read_scanlines(info, samples, 1) != 1)
    483             return false;
    484 
    485         int destY = scaledY(sourceY);
    486         if (destY < 0)
    487             continue;
    488         int width = m_scaled ? m_scaledColumns.size() : info->output_width;
    489         for (int x = 0; x < width; ++x) {
    490             JSAMPLE* jsample = *samples + (m_scaled ? m_scaledColumns[x] : x) * ((info->out_color_space == JCS_RGB) ? 3 : 4);
    491             if (info->out_color_space == JCS_RGB)
    492                 buffer.setRGBA(x, destY, jsample[0], jsample[1], jsample[2], 0xFF);
    493             else if (info->out_color_space == JCS_CMYK) {
    494                 // Source is 'Inverted CMYK', output is RGB.
    495                 // See: http://www.easyrgb.com/math.php?MATH=M12#text12
    496                 // Or:  http://www.ilkeratalay.com/colorspacesfaq.php#rgb
    497                 // From CMYK to CMY:
    498                 // X =   X    * (1 -   K   ) +   K  [for X = C, M, or Y]
    499                 // Thus, from Inverted CMYK to CMY is:
    500                 // X = (1-iX) * (1 - (1-iK)) + (1-iK) => 1 - iX*iK
    501                 // From CMY (0..1) to RGB (0..1):
    502                 // R = 1 - C => 1 - (1 - iC*iK) => iC*iK  [G and B similar]
    503                 unsigned k = jsample[3];
    504                 buffer.setRGBA(x, destY, jsample[0] * k / 255, jsample[1] * k / 255, jsample[2] * k / 255, 0xFF);
    505             } else {
    506                 ASSERT_NOT_REACHED();
    507                 return setFailed();
    508             }
    509         }
    510     }
    511 
    512     return true;
    513 }
    514 
    515 void JPEGImageDecoder::jpegComplete()
    516 {
    517     if (m_frameBufferCache.isEmpty())
    518         return;
    519 
    520     // Hand back an appropriately sized buffer, even if the image ended up being
    521     // empty.
    522     m_frameBufferCache[0].setStatus(ImageFrame::FrameComplete);
    523 }
    524 
    525 void JPEGImageDecoder::decode(bool onlySize)
    526 {
    527     if (failed())
    528         return;
    529 
    530     if (!m_reader)
    531         m_reader.set(new JPEGImageReader(this));
    532 
    533     // If we couldn't decode the image but we've received all the data, decoding
    534     // has failed.
    535     if (!m_reader->decode(*m_data, onlySize) && isAllDataReceived())
    536         setFailed();
    537     // If we're done decoding the image, we don't need the JPEGImageReader
    538     // anymore.  (If we failed, |m_reader| has already been cleared.)
    539     else if (!m_frameBufferCache.isEmpty() && (m_frameBufferCache[0].status() == ImageFrame::FrameComplete))
    540         m_reader.clear();
    541 }
    542 
    543 }
    544