Home | History | Annotate | Download | only in qt
      1 /*
      2  * Copyright (C) 2006 Friedemann Kleint <fkleint (at) trolltech.com>
      3  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
      4  *
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 #include "ImageDecoderQt.h"
     31 
     32 #include <QtCore/QByteArray>
     33 #include <QtCore/QBuffer>
     34 
     35 #include <QtGui/QImageReader>
     36 #include <qdebug.h>
     37 
     38 namespace WebCore {
     39 
     40 ImageDecoder* ImageDecoder::create(const SharedBuffer& data, ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
     41 {
     42     // We need at least 4 bytes to figure out what kind of image we're dealing with.
     43     if (data.size() < 4)
     44         return 0;
     45 
     46     return new ImageDecoderQt(alphaOption, gammaAndColorProfileOption);
     47 }
     48 
     49 ImageDecoderQt::ImageDecoderQt(ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
     50     : ImageDecoder(alphaOption, gammaAndColorProfileOption)
     51     , m_repetitionCount(cAnimationNone)
     52 {
     53 }
     54 
     55 ImageDecoderQt::~ImageDecoderQt()
     56 {
     57 }
     58 
     59 void ImageDecoderQt::setData(SharedBuffer* data, bool allDataReceived)
     60 {
     61     if (failed())
     62         return;
     63 
     64     // No progressive loading possible
     65     if (!allDataReceived)
     66         return;
     67 
     68     // Cache our own new data.
     69     ImageDecoder::setData(data, allDataReceived);
     70 
     71     // We expect to be only called once with allDataReceived
     72     ASSERT(!m_buffer);
     73     ASSERT(!m_reader);
     74 
     75     // Attempt to load the data
     76     QByteArray imageData = QByteArray::fromRawData(m_data->data(), m_data->size());
     77     m_buffer.set(new QBuffer);
     78     m_buffer->setData(imageData);
     79     m_buffer->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
     80     m_reader.set(new QImageReader(m_buffer.get(), m_format));
     81 
     82     // This will force the JPEG decoder to use JDCT_IFAST
     83     m_reader->setQuality(49);
     84 
     85     // QImageReader only allows retrieving the format before reading the image
     86     m_format = m_reader->format();
     87 }
     88 
     89 bool ImageDecoderQt::isSizeAvailable()
     90 {
     91     if (!ImageDecoder::isSizeAvailable() && m_reader)
     92         internalDecodeSize();
     93 
     94     return ImageDecoder::isSizeAvailable();
     95 }
     96 
     97 size_t ImageDecoderQt::frameCount()
     98 {
     99     if (m_frameBufferCache.isEmpty() && m_reader) {
    100         if (m_reader->supportsAnimation()) {
    101             int imageCount = m_reader->imageCount();
    102 
    103             // Fixup for Qt decoders... imageCount() is wrong
    104             // and jumpToNextImage does not work either... so
    105             // we will have to parse everything...
    106             if (!imageCount)
    107                 forceLoadEverything();
    108             else {
    109                 m_frameBufferCache.resize(imageCount);
    110                 for (size_t i = 0; i < m_frameBufferCache.size(); ++i)
    111                     m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha);
    112             }
    113         } else {
    114             m_frameBufferCache.resize(1);
    115             m_frameBufferCache[0].setPremultiplyAlpha(m_premultiplyAlpha);
    116         }
    117     }
    118 
    119     return m_frameBufferCache.size();
    120 }
    121 
    122 int ImageDecoderQt::repetitionCount() const
    123 {
    124     if (m_reader && m_reader->supportsAnimation())
    125         m_repetitionCount = m_reader->loopCount();
    126     return m_repetitionCount;
    127 }
    128 
    129 String ImageDecoderQt::filenameExtension() const
    130 {
    131     return String(m_format.constData(), m_format.length());
    132 };
    133 
    134 ImageFrame* ImageDecoderQt::frameBufferAtIndex(size_t index)
    135 {
    136     // In case the ImageDecoderQt got recreated we don't know
    137     // yet how many images we are going to have and need to
    138     // find that out now.
    139     size_t count = m_frameBufferCache.size();
    140     if (!failed() && !count) {
    141         internalDecodeSize();
    142         count = frameCount();
    143     }
    144 
    145     if (index >= count)
    146         return 0;
    147 
    148     ImageFrame& frame = m_frameBufferCache[index];
    149     if (frame.status() != ImageFrame::FrameComplete && m_reader)
    150         internalReadImage(index);
    151     return &frame;
    152 }
    153 
    154 void ImageDecoderQt::clearFrameBufferCache(size_t /*index*/)
    155 {
    156 }
    157 
    158 void ImageDecoderQt::internalDecodeSize()
    159 {
    160     ASSERT(m_reader);
    161 
    162     // If we have a QSize() something failed
    163     QSize size = m_reader->size();
    164     if (size.isEmpty()) {
    165         setFailed();
    166         return clearPointers();
    167     }
    168 
    169     setSize(size.width(), size.height());
    170 }
    171 
    172 void ImageDecoderQt::internalReadImage(size_t frameIndex)
    173 {
    174     ASSERT(m_reader);
    175 
    176     if (m_reader->supportsAnimation())
    177         m_reader->jumpToImage(frameIndex);
    178     else if (frameIndex) {
    179         setFailed();
    180         return clearPointers();
    181     }
    182 
    183     if (!internalHandleCurrentImage(frameIndex))
    184       setFailed();
    185 
    186     // Attempt to return some memory
    187     for (int i = 0; i < m_frameBufferCache.size(); ++i) {
    188         if (m_frameBufferCache[i].status() != ImageFrame::FrameComplete)
    189             return;
    190     }
    191 
    192     clearPointers();
    193 }
    194 
    195 bool ImageDecoderQt::internalHandleCurrentImage(size_t frameIndex)
    196 {
    197     QPixmap pixmap = QPixmap::fromImageReader(m_reader.get());
    198 
    199     if (pixmap.isNull()) {
    200         frameCount();
    201         repetitionCount();
    202         clearPointers();
    203         return false;
    204     }
    205 
    206     // now into the ImageFrame - even if the image is not
    207     ImageFrame* const buffer = &m_frameBufferCache[frameIndex];
    208     buffer->setOriginalFrameRect(m_reader->currentImageRect());
    209     buffer->setStatus(ImageFrame::FrameComplete);
    210     buffer->setDuration(m_reader->nextImageDelay());
    211     buffer->setPixmap(pixmap);
    212     return true;
    213 }
    214 
    215 // The QImageIOHandler is not able to tell us how many frames
    216 // we have and we need to parse every image. We do this by
    217 // increasing the m_frameBufferCache by one and try to parse
    218 // the image. We stop when QImage::read fails and then need
    219 // to resize the m_frameBufferCache to the final size and update
    220 // the failed bit. If we failed to decode the first image
    221 // then we truly failed to decode, otherwise we're OK.
    222 
    223 // TODO: Do not increment the m_frameBufferCache.size() by one but more than one
    224 void ImageDecoderQt::forceLoadEverything()
    225 {
    226     int imageCount = 0;
    227 
    228     do {
    229         m_frameBufferCache.resize(++imageCount);
    230     } while (internalHandleCurrentImage(imageCount - 1));
    231 
    232     // If we failed decoding the first image we actually
    233     // have no images and need to set the failed bit.
    234     // Otherwise, we want to forget about
    235     // the last attempt to decode a image.
    236     m_frameBufferCache.resize(imageCount - 1);
    237     for (size_t i = 0; i < m_frameBufferCache.size(); ++i)
    238         m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha);
    239     if (imageCount == 1)
    240       setFailed();
    241 }
    242 
    243 void ImageDecoderQt::clearPointers()
    244 {
    245     m_reader.clear();
    246     m_buffer.clear();
    247 }
    248 }
    249 
    250 // vim: ts=4 sw=4 et
    251