/*
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WEBPImageDecoder.h"

#if USE(WEBP)

#if PLATFORM(CHROMIUM)
#include "TraceEvent.h"
#endif

#if WEBP_ICC_ANIMATION_SUPPORT
#include "RuntimeEnabledFeatures.h"
#include "webp/format_constants.h"
#endif

#if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN)
inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : MODE_RGBA; }
#elif USE(SKIA) && SK_B32_SHIFT
inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : MODE_RGBA; }
#else // LITTLE_ENDIAN, output BGRA pixels.
#if CAIRO_USE_ARGB
inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_bgrA : MODE_BGRA; }
#else
inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : MODE_RGBA; }
#endif
#endif

namespace WebCore {

WEBPImageDecoder::WEBPImageDecoder(ImageSource::AlphaOption alphaOption,
                                   ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
: ImageDecoder(alphaOption, gammaAndColorProfileOption)
, m_decoder(0)
, m_frameBackgroundHasAlpha(false)
#if WEBP_ICC_ANIMATION_SUPPORT
, m_demux(0)
, m_demuxState(WEBP_DEMUX_PARSING_HEADER)
, m_formatFlags(0)
, m_haveAlreadyParsedThisData(false)
, m_haveReadAnimationParameters(false)
, m_repetitionCount(cAnimationLoopOnce)
, m_decodedHeight(0)
#else
, m_hasAlpha(false)
#endif
{
}

WEBPImageDecoder::~WEBPImageDecoder()
{
    clear();
}
void WEBPImageDecoder::clear()
{
#if WEBP_ICC_ANIMATION_SUPPORT
    WebPDemuxDelete(m_demux);
    m_demux = 0;
#endif
    clearDecoder();
}

void WEBPImageDecoder::clearDecoder()
{
    WebPIDelete(m_decoder);
    m_decoder = 0;
#if WEBP_ICC_ANIMATION_SUPPORT
    m_decodedHeight = 0;
    m_frameBackgroundHasAlpha = false;
#endif
}

bool WEBPImageDecoder::isSizeAvailable()
{
	if (!ImageDecoder::isSizeAvailable()) {
#if WEBP_ICC_ANIMATION_SUPPORT
        updateDemuxer();
#else
        decode(reinterpret_cast<const uint8_t*>(m_data->data()), m_data->size(), true, 0);
#endif
    }
    return ImageDecoder::isSizeAvailable();
}

size_t WEBPImageDecoder::frameCount()
{
#if WEBP_ICC_ANIMATION_SUPPORT
    if (!updateDemuxer())
        return 0;
#else
    if (m_frameBufferCache.isEmpty()) {
        m_frameBufferCache.resize(1);
        m_frameBufferCache[0].setPremultiplyAlpha(true/*m_premultiplyAlpha*/);
    }
#endif
    return m_frameBufferCache.size();
}

ImageFrame* WEBPImageDecoder::frameBufferAtIndex(size_t index)
{
    if (index >= frameCount())
        return 0;
    ImageFrame& frame = m_frameBufferCache[index];
    if (frame.status() == ImageFrame::FrameComplete)
        return &frame;
#if WEBP_ICC_ANIMATION_SUPPORT
    /* if (RuntimeEnabledFeatures::animatedWebPEnabled()) */ {
        Vector<size_t> framesToDecode;
        size_t frameToDecode = index;
        do {
            framesToDecode.append(frameToDecode);
            frameToDecode = m_frameBufferCache[frameToDecode].requiredPreviousFrameIndex();
        } while (frameToDecode != notFound && m_frameBufferCache[frameToDecode].status() != ImageFrame::FrameComplete);
        ASSERT(m_demux);
        for (size_t i = framesToDecode.size(); i > 0; --i) {
            size_t frameIndex = framesToDecode[i - 1];
            WebPIterator webpFrame;
            if (!WebPDemuxGetFrame(m_demux, frameIndex + 1, &webpFrame))
                return 0;
            if ((m_formatFlags & ANIMATION_FLAG) && !initFrameBuffer(webpFrame, frameIndex)) {
                WebPDemuxReleaseIterator(&webpFrame);
                return 0;
            }
            //PlatformInstrumentation::willDecodeImage("WEBP");
            decode(webpFrame.fragment.bytes, webpFrame.fragment.size, false, frameIndex);
            //PlatformInstrumentation::didDecodeImage();
            WebPDemuxReleaseIterator(&webpFrame);
            // We need more data to continue decoding.
            if (m_frameBufferCache[frameIndex].status() != ImageFrame::FrameComplete)
                break;
        }
        // It is also a fatal error if all data is received and we have decoded all
        // frames available but the file is truncated.
        if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && m_demux && m_demuxState != WEBP_DEMUX_DONE)
            setFailed();
        return &frame;
    }
#endif
    ASSERT(!index);
    //PlatformInstrumentation::willDecodeImage("WEBP");
    decode(reinterpret_cast<const uint8_t*>(m_data->data()), m_data->size(), false, index);
    //PlatformInstrumentation::didDecodeImage();
    return &frame;
}

#if WEBP_ICC_ANIMATION_SUPPORT
void WEBPImageDecoder::setData(SharedBuffer* data, bool allDataReceived)
{
    if (failed())
        return;
    ImageDecoder::setData(data, allDataReceived);
    m_haveAlreadyParsedThisData = false;
}

int WEBPImageDecoder::repetitionCount() const
{
    return failed() ? cAnimationLoopOnce : m_repetitionCount;
}

#if 0
bool WEBPImageDecoder::frameIsCompleteAtIndex(size_t index) const
{
    //if (!RuntimeEnabledFeatures::animatedWebPEnabled())
    //    return ImageDecoder::frameIsCompleteAtIndex(index);
    if (!m_demux || m_demuxState <= WEBP_DEMUX_PARSING_HEADER)
        return false;
    if (!(m_formatFlags & ANIMATION_FLAG))
        return ImageDecoder::frameIsCompleteAtIndex(index);
    bool frameIsLoadedAtIndex = index < m_frameBufferCache.size();
    return frameIsLoadedAtIndex;
}

float WEBPImageDecoder::frameDurationAtIndex(size_t index) const
{
    return index < m_frameBufferCache.size() ? m_frameBufferCache[index].duration() : 0;
}
#endif

bool WEBPImageDecoder::updateDemuxer()
{
    if (m_haveAlreadyParsedThisData)
        return true;
    m_haveAlreadyParsedThisData = true;
    if (m_data->size() < RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE)
        return false; // Wait for headers so that WebPDemuxPartial doesn't return null.
    WebPDemuxDelete(m_demux);
    WebPData inputData = { reinterpret_cast<const uint8_t*>(m_data->data()), m_data->size() };
    m_demux = WebPDemuxPartial(&inputData, &m_demuxState);
    if (!m_demux)
        return setFailed();
    if (m_demuxState <= WEBP_DEMUX_PARSING_HEADER)
        return false; // Not enough data for parsing canvas width/height yet.
    bool hasAnimation = (m_formatFlags & ANIMATION_FLAG);
    if (!ImageDecoder::isSizeAvailable()) {
        m_formatFlags = WebPDemuxGetI(m_demux, WEBP_FF_FORMAT_FLAGS);
        hasAnimation = (m_formatFlags & ANIMATION_FLAG);
        //if (hasAnimation && !RuntimeEnabledFeatures::animatedWebPEnabled())
        //    return setFailed();
        if (!setSize(WebPDemuxGetI(m_demux, WEBP_FF_CANVAS_WIDTH), WebPDemuxGetI(m_demux, WEBP_FF_CANVAS_HEIGHT)))
            return setFailed();
    }
    ASSERT(ImageDecoder::isSizeAvailable());
    const size_t newFrameCount = WebPDemuxGetI(m_demux, WEBP_FF_FRAME_COUNT);
    if (hasAnimation && !m_haveReadAnimationParameters && newFrameCount) {
        // As we have parsed at least one frame (even if partially),
        // we must already have parsed the animation properties.
        // This is because ANIM chunk always precedes ANMF chunks.
        m_repetitionCount = WebPDemuxGetI(m_demux, WEBP_FF_LOOP_COUNT);
        ASSERT(m_repetitionCount == (m_repetitionCount & 0xffff)); // Loop count is always <= 16 bits.
        if (!m_repetitionCount)
            m_repetitionCount = cAnimationLoopInfinite;
        m_haveReadAnimationParameters = true;
    }
    const size_t oldFrameCount = m_frameBufferCache.size();
    if (newFrameCount > oldFrameCount) {
        m_frameBufferCache.resize(newFrameCount);
        for (size_t i = oldFrameCount; i < newFrameCount; ++i) {
            m_frameBufferCache[i].setPremultiplyAlpha(true /*m_premultiplyAlpha*/);
            if (!hasAnimation) {
                ASSERT(!i);
                m_frameBufferCache[i].setRequiredPreviousFrameIndex(notFound);
                continue;
            }
            WebPIterator animatedFrame;
            WebPDemuxGetFrame(m_demux, i + 1, &animatedFrame);
            ASSERT(animatedFrame.complete == 1);
            m_frameBufferCache[i].setDuration(animatedFrame.duration);
            m_frameBufferCache[i].setDisposalMethod(animatedFrame.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ? ImageFrame::DisposeOverwriteBgcolor : ImageFrame::DisposeKeep);
            WebPDemuxReleaseIterator(&animatedFrame);
            m_frameBufferCache[i].setRequiredPreviousFrameIndex(findRequiredPreviousFrame(i));
        }
    }
    return true;
}

bool WEBPImageDecoder::initFrameBuffer(const WebPIterator& frame, size_t frameIndex)
{
    ImageFrame& buffer = m_frameBufferCache[frameIndex];
    if (buffer.status() != ImageFrame::FrameEmpty) // Already initialized.
        return true;
    // Initialize the frame rect in our buffer.
    IntRect frameRect(frame.x_offset, frame.y_offset, frame.width, frame.height);
    // Make sure the frameRect doesn't extend outside the buffer.
    if (frameRect.maxX() > size().width())
        frameRect.setWidth(size().width() - frame.x_offset);
    if (frameRect.maxY() > size().height())
        frameRect.setHeight(size().height() - frame.y_offset);
    buffer.setOriginalFrameRect(frameRect);
    const size_t requiredPreviousFrameIndex = buffer.requiredPreviousFrameIndex();
    if (requiredPreviousFrameIndex == notFound) {
        // This frame doesn't rely on any previous data.
        if (!buffer.setSize(size().width(), size().height()))
            return setFailed();
        m_frameBackgroundHasAlpha = !frameRect.contains(IntRect(IntPoint(), size()));
    } else {
        const ImageFrame& prevBuffer = m_frameBufferCache[requiredPreviousFrameIndex];
        ASSERT(prevBuffer.status() == ImageFrame::FrameComplete);
        // Preserve the last frame as the starting state for this frame.
        if (!buffer.copyBitmapData(prevBuffer))
            return setFailed();
        if (prevBuffer.disposalMethod() == ImageFrame::DisposeOverwriteBgcolor) {
            // We want to clear the previous frame to transparent, without
            // affecting pixels in the image outside of the frame.
            const IntRect& prevRect = prevBuffer.originalFrameRect();
            ASSERT(!prevRect.contains(IntRect(IntPoint(), size())));
            buffer.zeroFillFrameRect(prevRect);
        }
        m_frameBackgroundHasAlpha = prevBuffer.hasAlpha() || (prevBuffer.disposalMethod() == ImageFrame::DisposeOverwriteBgcolor);
    }
    buffer.setStatus(ImageFrame::FramePartial);
    // The buffer is transparent outside the decoded area while the image is loading.
    // The correct value of 'hasAlpha' for the frame will be set when it is fully decoded.
    buffer.setHasAlpha(true);
    return true;
}

size_t WEBPImageDecoder::clearCacheExceptFrame(size_t clearExceptFrame)
{
    // If |clearExceptFrame| has status FrameComplete, we preserve that frame.
    // Otherwise, we preserve a previous frame with status FrameComplete whose data is required
    // to decode |clearExceptFrame|, either in initFrameBuffer() or ApplyPostProcessing().
    // All other frames can be cleared.
    while ((clearExceptFrame < m_frameBufferCache.size()) && (m_frameBufferCache[clearExceptFrame].status() != ImageFrame::FrameComplete))
        clearExceptFrame = m_frameBufferCache[clearExceptFrame].requiredPreviousFrameIndex();
    return ImageDecoder::clearCacheExceptFrame(clearExceptFrame);
}

void WEBPImageDecoder::clearFrameBuffer(size_t frameIndex)
{
    if (m_demux && m_demuxState >= WEBP_DEMUX_PARSED_HEADER && m_frameBufferCache[frameIndex].status() == ImageFrame::FramePartial) {
        // Clear the decoder state so that this partial frame can be decoded again when requested.
        clearDecoder();
    }
    ImageDecoder::clearFrameBuffer(frameIndex);
}
#endif // WEBP_ICC_ANIMATION_SUPPORT

#if WEBP_ICC_ANIMATION_SUPPORT
void WEBPImageDecoder::applyPostProcessing(size_t frameIndex)
{
    ImageFrame& buffer = m_frameBufferCache[frameIndex];
    int width;
    int decodedHeight;
    if (!WebPIDecGetRGB(m_decoder, &decodedHeight, &width, 0, 0))
        return; // See also https://bugs.webkit.org/show_bug.cgi?id=74062
    if (decodedHeight <= 0)
        return;
    const IntRect& frameRect = buffer.originalFrameRect();
    ASSERT_WITH_SECURITY_IMPLICATION(width == frameRect.width());
    ASSERT_WITH_SECURITY_IMPLICATION(decodedHeight <= frameRect.height());
    const int left = frameRect.x();
    const int top = frameRect.y();
    // During the decoding of current frame, we may have set some pixels to be transparent (i.e. alpha < 255).
    // However, the value of each of these pixels should have been determined by blending it against the value
    // of that pixel in the previous frame. So, we correct these pixels based on disposal method of the previous
    // frame and the previous frame buffer.
    // FIXME: This could be avoided if libwebp decoder had an API that used the previous required frame
    // to do the alpha-blending by itself.
    if ((m_formatFlags & ANIMATION_FLAG) && frameIndex) {
        ImageFrame& prevBuffer = m_frameBufferCache[frameIndex - 1];
        ImageFrame::FrameDisposalMethod prevMethod = prevBuffer.disposalMethod();
        if (prevMethod == ImageFrame::DisposeKeep) { // Restore transparent pixels to pixels in previous canvas.
            ASSERT(prevBuffer.status() == ImageFrame::FrameComplete);
            for (int y = m_decodedHeight; y < decodedHeight; ++y) {
                const int canvasY = top + y;
                for (int x = 0; x < width; ++x) {
                    const int canvasX = left + x;
                    ImageFrame::PixelData& pixel = *buffer.getAddr(canvasX, canvasY);
                    // FIXME: Use alpha-blending when alpha is between 0 and 255.
                    // Alpha-blending is being implemented in: https://bugs.webkit.org/show_bug.cgi?id=17022
                    if (!((pixel >> 24) & 0xff)) {
                        ImageFrame::PixelData prevPixel = *prevBuffer.getAddr(canvasX, canvasY);
                        pixel = prevPixel;
                    }
                }
            }
        } else if (prevMethod == ImageFrame::DisposeOverwriteBgcolor && buffer.requiredPreviousFrameIndex() != notFound) {
            // Note: if the requiredPreviousFrameIndex is |notFound|, there's nothing to do.
            ASSERT(prevBuffer.status() == ImageFrame::FrameComplete);
            const IntRect& prevRect = prevBuffer.originalFrameRect();
            // We need to restore transparent pixels to as they were just after initFrame() call. That is:
            //   * Transparent if it belongs to prevRect <-- This is a no-op.
            //   * Pixel in the previous canvas otherwise <-- Need to restore.
            for (int y = m_decodedHeight; y < decodedHeight; ++y) {
                const int canvasY = top + y;
                for (int x = 0; x < width; ++x) {
                    const int canvasX = left + x;
                    ImageFrame::PixelData& pixel = *buffer.getAddr(canvasX, canvasY);
                    // FIXME: Use alpha-blending when alpha is between 0 and 255.
                    if (!((pixel >> 24) & 0xff) && !prevRect.contains(IntPoint(canvasX, canvasY))) {
                        ImageFrame::PixelData prevPixel = *prevBuffer.getAddr(canvasX, canvasY);
                        pixel = prevPixel;
                    }
                }
            }
        }
    }
    m_decodedHeight = decodedHeight;
}
#endif // WEBP_ICC_ANIMATION_SUPPORT

bool WEBPImageDecoder::decode(const uint8_t* dataBytes, size_t dataSize, bool onlySize, size_t frameIndex)
{
    if (failed())
        return false;
    if (!ImageDecoder::isSizeAvailable()) {
        static const size_t imageHeaderSize = 30;
        if (dataSize < imageHeaderSize)
            return false;
        int width, height;
        WebPBitstreamFeatures features;
        if (WebPGetFeatures(dataBytes, dataSize, &features) != VP8_STATUS_OK)
            return setFailed();
        width = features.width;
        height = features.height;
#if WEBP_ICC_ANIMATION_SUPPORT
        m_formatFlags = features.has_alpha ? ALPHA_FLAG : 0;
#else
        m_hasAlpha = features.has_alpha;
#endif
        if (!setSize(width, height))
            return setFailed();
    }
    ASSERT(ImageDecoder::isSizeAvailable());
    if (onlySize)
        return true;
    ASSERT(m_frameBufferCache.size() > frameIndex);
    ImageFrame& buffer = m_frameBufferCache[frameIndex];
    ASSERT(buffer.status() != ImageFrame::FrameComplete);
    if (buffer.status() == ImageFrame::FrameEmpty) {
        if (!buffer.setSize(size().width(), size().height()))
            return setFailed();
        buffer.setStatus(ImageFrame::FramePartial);
        // The buffer is transparent outside the decoded area while the image is loading.
        // The correct value of 'hasAlpha' for the frame will be set when it is fully decoded.
        buffer.setHasAlpha(true);
        buffer.setOriginalFrameRect(IntRect(IntPoint(), size()));
    }
    const IntRect& frameRect = buffer.originalFrameRect();
    if (!m_decoder) {
#if WEBP_ICC_ANIMATION_SUPPORT
        WEBP_CSP_MODE mode = outputMode(m_formatFlags & ALPHA_FLAG);
#else
        WEBP_CSP_MODE mode = outputMode(m_hasAlpha);
#endif
        //if (!m_premultiplyAlpha)
        //    mode = outputMode(false);
        WebPInitDecBuffer(&m_decoderBuffer);
        m_decoderBuffer.colorspace = mode;
        m_decoderBuffer.u.RGBA.stride = buffer.rowStride();
        m_decoderBuffer.u.RGBA.size = m_decoderBuffer.u.RGBA.stride * frameRect.height();
        m_decoderBuffer.is_external_memory = 1;
        m_decoder = WebPINewDecoder(&m_decoderBuffer);
        if (!m_decoder)
            return setFailed();
    }
    m_decoderBuffer.u.RGBA.rgba = reinterpret_cast<uint8_t*>(buffer.getAddr(frameRect.x(), frameRect.y()));
    switch (WebPIUpdate(m_decoder, dataBytes, dataSize)) {
    case VP8_STATUS_OK:
        applyPostProcessing(frameIndex);
#if WEBP_ICC_ANIMATION_SUPPORT
        buffer.setHasAlpha((m_formatFlags & ALPHA_FLAG) || m_frameBackgroundHasAlpha);
#endif
        buffer.setStatus(ImageFrame::FrameComplete);
        clearDecoder();
        return true;
    case VP8_STATUS_SUSPENDED:
        applyPostProcessing(frameIndex);
        return false;
    default:
        clear();
        return setFailed();
    }
}

} // namespace WebCore

#endif
