Home | History | Annotate | Download | only in image-decoders
      1 /*
      2  * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
      3  *
      4  *  This library is free software; you can redistribute it and/or
      5  *  modify it under the terms of the GNU Library General Public
      6  *  License as published by the Free Software Foundation; either
      7  *  version 2 of the License, or (at your option) any later version.
      8  *
      9  *  This library is distributed in the hope that it will be useful,
     10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  *  Library General Public License for more details.
     13  *
     14  *  You should have received a copy of the GNU Library General Public License
     15  *  along with this library; see the file COPYING.LIB.  If not, write to
     16  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  *  Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #include "config.h"
     22 #include "platform/image-decoders/ImageDecoder.h"
     23 
     24 #include "platform/graphics/DeferredImageDecoder.h"
     25 #include "platform/image-decoders/bmp/BMPImageDecoder.h"
     26 #include "platform/image-decoders/gif/GIFImageDecoder.h"
     27 #include "platform/image-decoders/ico/ICOImageDecoder.h"
     28 #include "platform/image-decoders/jpeg/JPEGImageDecoder.h"
     29 #include "platform/image-decoders/png/PNGImageDecoder.h"
     30 #include "platform/image-decoders/webp/WEBPImageDecoder.h"
     31 #include "wtf/PassOwnPtr.h"
     32 
     33 namespace blink {
     34 
     35 static unsigned copyFromSharedBuffer(char* buffer, unsigned bufferLength, const SharedBuffer& sharedBuffer, unsigned offset)
     36 {
     37     unsigned bytesExtracted = 0;
     38     const char* moreData;
     39     while (unsigned moreDataLength = sharedBuffer.getSomeData(moreData, offset)) {
     40         unsigned bytesToCopy = std::min(bufferLength - bytesExtracted, moreDataLength);
     41         memcpy(buffer + bytesExtracted, moreData, bytesToCopy);
     42         bytesExtracted += bytesToCopy;
     43         if (bytesExtracted == bufferLength)
     44             break;
     45         offset += bytesToCopy;
     46     }
     47     return bytesExtracted;
     48 }
     49 
     50 inline bool matchesGIFSignature(char* contents)
     51 {
     52     return !memcmp(contents, "GIF87a", 6) || !memcmp(contents, "GIF89a", 6);
     53 }
     54 
     55 inline bool matchesPNGSignature(char* contents)
     56 {
     57     return !memcmp(contents, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8);
     58 }
     59 
     60 inline bool matchesJPEGSignature(char* contents)
     61 {
     62     return !memcmp(contents, "\xFF\xD8\xFF", 3);
     63 }
     64 
     65 inline bool matchesWebPSignature(char* contents)
     66 {
     67     return !memcmp(contents, "RIFF", 4) && !memcmp(contents + 8, "WEBPVP", 6);
     68 }
     69 
     70 inline bool matchesBMPSignature(char* contents)
     71 {
     72     return !memcmp(contents, "BM", 2);
     73 }
     74 
     75 inline bool matchesICOSignature(char* contents)
     76 {
     77     return !memcmp(contents, "\x00\x00\x01\x00", 4);
     78 }
     79 
     80 inline bool matchesCURSignature(char* contents)
     81 {
     82     return !memcmp(contents, "\x00\x00\x02\x00", 4);
     83 }
     84 
     85 PassOwnPtr<ImageDecoder> ImageDecoder::create(const SharedBuffer& data, ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
     86 {
     87     static const unsigned longestSignatureLength = sizeof("RIFF????WEBPVP") - 1;
     88     ASSERT(longestSignatureLength == 14);
     89 
     90     size_t maxDecodedBytes = blink::Platform::current()->maxDecodedImageBytes();
     91 
     92     char contents[longestSignatureLength];
     93     if (copyFromSharedBuffer(contents, longestSignatureLength, data, 0) < longestSignatureLength)
     94         return nullptr;
     95 
     96     if (matchesJPEGSignature(contents))
     97         return adoptPtr(new JPEGImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
     98 
     99     if (matchesPNGSignature(contents))
    100         return adoptPtr(new PNGImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
    101 
    102     if (matchesGIFSignature(contents))
    103         return adoptPtr(new GIFImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
    104 
    105     if (matchesICOSignature(contents) || matchesCURSignature(contents))
    106         return adoptPtr(new ICOImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
    107 
    108     if (matchesWebPSignature(contents))
    109         return adoptPtr(new WEBPImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
    110 
    111     if (matchesBMPSignature(contents))
    112         return adoptPtr(new BMPImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
    113 
    114     return nullptr;
    115 }
    116 
    117 bool ImageDecoder::frameHasAlphaAtIndex(size_t index) const
    118 {
    119     return !frameIsCompleteAtIndex(index) || m_frameBufferCache[index].hasAlpha();
    120 }
    121 
    122 bool ImageDecoder::frameIsCompleteAtIndex(size_t index) const
    123 {
    124     return (index < m_frameBufferCache.size()) &&
    125         (m_frameBufferCache[index].status() == ImageFrame::FrameComplete);
    126 }
    127 
    128 unsigned ImageDecoder::frameBytesAtIndex(size_t index) const
    129 {
    130     if (m_frameBufferCache.size() <= index || m_frameBufferCache[index].status() == ImageFrame::FrameEmpty)
    131         return 0;
    132     // FIXME: Use the dimension of the requested frame.
    133     return m_size.area() * sizeof(ImageFrame::PixelData);
    134 }
    135 
    136 bool ImageDecoder::deferredImageDecodingEnabled()
    137 {
    138     return DeferredImageDecoder::enabled();
    139 }
    140 
    141 size_t ImageDecoder::clearCacheExceptFrame(size_t clearExceptFrame)
    142 {
    143     // Don't clear if there are no frames or only one frame.
    144     if (m_frameBufferCache.size() <= 1)
    145         return 0;
    146 
    147     size_t frameBytesCleared = 0;
    148     for (size_t i = 0; i < m_frameBufferCache.size(); ++i) {
    149         if (i != clearExceptFrame) {
    150             frameBytesCleared += frameBytesAtIndex(i);
    151             clearFrameBuffer(i);
    152         }
    153     }
    154     return frameBytesCleared;
    155 }
    156 
    157 void ImageDecoder::clearFrameBuffer(size_t frameIndex)
    158 {
    159     m_frameBufferCache[frameIndex].clearPixelData();
    160 }
    161 
    162 size_t ImageDecoder::findRequiredPreviousFrame(size_t frameIndex, bool frameRectIsOpaque)
    163 {
    164     ASSERT(frameIndex <= m_frameBufferCache.size());
    165     if (!frameIndex) {
    166         // The first frame doesn't rely on any previous data.
    167         return kNotFound;
    168     }
    169 
    170     const ImageFrame* currBuffer = &m_frameBufferCache[frameIndex];
    171     if ((frameRectIsOpaque || currBuffer->alphaBlendSource() == ImageFrame::BlendAtopBgcolor)
    172         && currBuffer->originalFrameRect().contains(IntRect(IntPoint(), size())))
    173         return kNotFound;
    174 
    175     // The starting state for this frame depends on the previous frame's
    176     // disposal method.
    177     size_t prevFrame = frameIndex - 1;
    178     const ImageFrame* prevBuffer = &m_frameBufferCache[prevFrame];
    179     ASSERT(prevBuffer->requiredPreviousFrameIndexValid());
    180 
    181     switch (prevBuffer->disposalMethod()) {
    182     case ImageFrame::DisposeNotSpecified:
    183     case ImageFrame::DisposeKeep:
    184         // prevFrame will be used as the starting state for this frame.
    185         // FIXME: Be even smarter by checking the frame sizes and/or alpha-containing regions.
    186         return prevFrame;
    187     case ImageFrame::DisposeOverwritePrevious:
    188         // Frames that use the DisposeOverwritePrevious method are effectively
    189         // no-ops in terms of changing the starting state of a frame compared to
    190         // the starting state of the previous frame, so skip over them and
    191         // return the required previous frame of it.
    192         return prevBuffer->requiredPreviousFrameIndex();
    193     case ImageFrame::DisposeOverwriteBgcolor:
    194         // If the previous frame fills the whole image, then the current frame
    195         // can be decoded alone. Likewise, if the previous frame could be
    196         // decoded without reference to any prior frame, the starting state for
    197         // this frame is a blank frame, so it can again be decoded alone.
    198         // Otherwise, the previous frame contributes to this frame.
    199         return (prevBuffer->originalFrameRect().contains(IntRect(IntPoint(), size()))
    200             || (prevBuffer->requiredPreviousFrameIndex() == kNotFound)) ? kNotFound : prevFrame;
    201     default:
    202         ASSERT_NOT_REACHED();
    203         return kNotFound;
    204     }
    205 }
    206 
    207 ImagePlanes::ImagePlanes()
    208 {
    209     for (int i = 0; i < 3; ++i) {
    210         m_planes[i] = 0;
    211         m_rowBytes[i] = 0;
    212     }
    213 }
    214 
    215 ImagePlanes::ImagePlanes(void* planes[3], size_t rowBytes[3])
    216 {
    217     for (int i = 0; i < 3; ++i) {
    218         m_planes[i] = planes[i];
    219         m_rowBytes[i] = rowBytes[i];
    220     }
    221 }
    222 
    223 void* ImagePlanes::plane(int i)
    224 {
    225     ASSERT((i >= 0) && i < 3);
    226     return m_planes[i];
    227 }
    228 
    229 size_t ImagePlanes::rowBytes(int i) const
    230 {
    231     ASSERT((i >= 0) && i < 3);
    232     return m_rowBytes[i];
    233 }
    234 
    235 } // namespace blink
    236