| 1 | /* |
| 2 | * Copyright (C) 2006 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "ScalableImageDecoder.h" |
| 29 | #if ENABLE(APNG) |
| 30 | #include <png.h> |
| 31 | #endif |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | class PNGImageReader; |
| 36 | |
| 37 | // This class decodes the PNG image format. |
| 38 | class PNGImageDecoder final : public ScalableImageDecoder { |
| 39 | public: |
| 40 | static Ref<ScalableImageDecoder> create(AlphaOption alphaOption, GammaAndColorProfileOption gammaAndColorProfileOption) |
| 41 | { |
| 42 | return adoptRef(*new PNGImageDecoder(alphaOption, gammaAndColorProfileOption)); |
| 43 | } |
| 44 | |
| 45 | virtual ~PNGImageDecoder(); |
| 46 | |
| 47 | // ScalableImageDecoder |
| 48 | String filenameExtension() const override { return "png"_s ; } |
| 49 | #if ENABLE(APNG) |
| 50 | size_t frameCount() const override { return m_frameCount; } |
| 51 | RepetitionCount repetitionCount() const override; |
| 52 | #endif |
| 53 | bool setSize(const IntSize&) override; |
| 54 | ScalableImageDecoderFrame* frameBufferAtIndex(size_t index) override; |
| 55 | // CAUTION: setFailed() deletes |m_reader|. Be careful to avoid |
| 56 | // accessing deleted memory, especially when calling this from inside |
| 57 | // PNGImageReader! |
| 58 | bool setFailed() override; |
| 59 | |
| 60 | // Callbacks from libpng |
| 61 | void (); |
| 62 | void rowAvailable(unsigned char* rowBuffer, unsigned rowIndex, int interlacePass); |
| 63 | void pngComplete(); |
| 64 | #if ENABLE(APNG) |
| 65 | void readChunks(png_unknown_chunkp); |
| 66 | void (); |
| 67 | |
| 68 | void init(); |
| 69 | void clearFrameBufferCache(size_t clearBeforeFrame) override; |
| 70 | #endif |
| 71 | |
| 72 | bool isComplete() const |
| 73 | { |
| 74 | if (m_frameBufferCache.isEmpty()) |
| 75 | return false; |
| 76 | |
| 77 | for (auto& imageFrame : m_frameBufferCache) { |
| 78 | if (!imageFrame.isComplete()) |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool isCompleteAtIndex(size_t index) |
| 86 | { |
| 87 | return (index < m_frameBufferCache.size() && m_frameBufferCache[index].isComplete()); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | PNGImageDecoder(AlphaOption, GammaAndColorProfileOption); |
| 92 | void tryDecodeSize(bool allDataReceived) override { decode(true, 0, allDataReceived); } |
| 93 | |
| 94 | // Decodes the image. If |onlySize| is true, stops decoding after |
| 95 | // calculating the image size. If decoding fails but there is no more |
| 96 | // data coming, sets the "decode failure" flag. |
| 97 | void decode(bool onlySize, unsigned haltAtFrame, bool allDataReceived); |
| 98 | #if ENABLE(APNG) |
| 99 | void initFrameBuffer(size_t frameIndex); |
| 100 | void frameComplete(); |
| 101 | int processingStart(png_unknown_chunkp); |
| 102 | int processingFinish(); |
| 103 | void fallbackNotAnimated(); |
| 104 | #endif |
| 105 | |
| 106 | std::unique_ptr<PNGImageReader> m_reader; |
| 107 | bool m_doNothingOnFailure; |
| 108 | unsigned m_currentFrame; |
| 109 | #if ENABLE(APNG) |
| 110 | png_structp m_png; |
| 111 | png_infop m_info; |
| 112 | bool m_isAnimated; |
| 113 | bool m_frameInfo; |
| 114 | bool m_frameIsHidden; |
| 115 | bool m_hasInfo; |
| 116 | int m_gamma; |
| 117 | size_t m_frameCount; |
| 118 | unsigned m_playCount; |
| 119 | unsigned m_totalFrames; |
| 120 | unsigned m_sizePLTE; |
| 121 | unsigned m_sizetRNS; |
| 122 | unsigned m_sequenceNumber; |
| 123 | unsigned m_width; |
| 124 | unsigned m_height; |
| 125 | unsigned m_xOffset; |
| 126 | unsigned m_yOffset; |
| 127 | unsigned m_delayNumerator; |
| 128 | unsigned m_delayDenominator; |
| 129 | unsigned m_dispose; |
| 130 | unsigned m_blend; |
| 131 | png_byte m_dataIHDR[12 + 13]; |
| 132 | png_byte m_dataPLTE[12 + 256 * 3]; |
| 133 | png_byte m_datatRNS[12 + 256]; |
| 134 | #endif |
| 135 | }; |
| 136 | |
| 137 | } // namespace WebCore |
| 138 | |