| OLD | NEW |
| (Empty) |
| 1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | |
| 2 /* ***** BEGIN LICENSE BLOCK ***** | |
| 3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | |
| 4 * | |
| 5 * The contents of this file are subject to the Mozilla Public License Version | |
| 6 * 1.1 (the "License"); you may not use this file except in compliance with | |
| 7 * the License. You may obtain a copy of the License at | |
| 8 * http://www.mozilla.org/MPL/ | |
| 9 * | |
| 10 * Software distributed under the License is distributed on an "AS IS" basis, | |
| 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
| 12 * for the specific language governing rights and limitations under the | |
| 13 * License. | |
| 14 * | |
| 15 * The Original Code is Mozilla Communicator client code. | |
| 16 * | |
| 17 * The Initial Developer of the Original Code is | |
| 18 * Netscape Communications Corporation. | |
| 19 * Portions created by the Initial Developer are Copyright (C) 1998 | |
| 20 * the Initial Developer. All Rights Reserved. | |
| 21 * | |
| 22 * Contributor(s): | |
| 23 * | |
| 24 * Alternatively, the contents of this file may be used under the terms of | |
| 25 * either the GNU General Public License Version 2 or later (the "GPL"), or | |
| 26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | |
| 27 * in which case the provisions of the GPL or the LGPL are applicable instead | |
| 28 * of those above. If you wish to allow use of your version of this file only | |
| 29 * under the terms of either the GPL or the LGPL, and not to allow others to | |
| 30 * use your version of this file under the terms of the MPL, indicate your | |
| 31 * decision by deleting the provisions above and replace them with the notice | |
| 32 * and other provisions required by the GPL or the LGPL. If you do not delete | |
| 33 * the provisions above, a recipient may use your version of this file under | |
| 34 * the terms of any one of the MPL, the GPL or the LGPL. | |
| 35 * | |
| 36 * ***** END LICENSE BLOCK ***** */ | |
| 37 | |
| 38 #ifndef GIFImageReader_h | |
| 39 #define GIFImageReader_h | |
| 40 | |
| 41 // Define ourselves as the clientPtr. Mozilla just hacked their C++ callback cl
ass into this old C decoder, | |
| 42 // so we will too. | |
| 43 #include "platform/SharedBuffer.h" | |
| 44 #include "core/platform/image-decoders/gif/GIFImageDecoder.h" | |
| 45 #include "wtf/OwnPtr.h" | |
| 46 #include "wtf/PassOwnPtr.h" | |
| 47 #include "wtf/Vector.h" | |
| 48 | |
| 49 #define MAX_DICTIONARY_ENTRY_BITS 12 | |
| 50 #define MAX_DICTIONARY_ENTRIES 4096 // 2^MAX_DICTIONARY_ENTRY_BITS | |
| 51 #define MAX_COLORS 256 | |
| 52 #define BYTES_PER_COLORMAP_ENTRY 3 | |
| 53 | |
| 54 const int cLoopCountNotSeen = -2; | |
| 55 | |
| 56 // List of possible parsing states. | |
| 57 enum GIFState { | |
| 58 GIFType, | |
| 59 GIFGlobalHeader, | |
| 60 GIFGlobalColormap, | |
| 61 GIFImageStart, | |
| 62 GIFImageHeader, | |
| 63 GIFImageColormap, | |
| 64 GIFImageBody, | |
| 65 GIFLZWStart, | |
| 66 GIFLZW, | |
| 67 GIFSubBlock, | |
| 68 GIFExtension, | |
| 69 GIFControlExtension, | |
| 70 GIFConsumeBlock, | |
| 71 GIFSkipBlock, | |
| 72 GIFDone, | |
| 73 GIFCommentExtension, | |
| 74 GIFApplicationExtension, | |
| 75 GIFNetscapeExtensionBlock, | |
| 76 GIFConsumeNetscapeExtension, | |
| 77 GIFConsumeComment | |
| 78 }; | |
| 79 | |
| 80 struct GIFFrameContext; | |
| 81 | |
| 82 // LZW decoder state machine. | |
| 83 class GIFLZWContext { | |
| 84 WTF_MAKE_FAST_ALLOCATED; | |
| 85 public: | |
| 86 GIFLZWContext(WebCore::GIFImageDecoder* client, const GIFFrameContext* frame
Context) | |
| 87 : codesize(0) | |
| 88 , codemask(0) | |
| 89 , clearCode(0) | |
| 90 , avail(0) | |
| 91 , oldcode(0) | |
| 92 , firstchar(0) | |
| 93 , bits(0) | |
| 94 , datum(0) | |
| 95 , ipass(0) | |
| 96 , irow(0) | |
| 97 , rowsRemaining(0) | |
| 98 , rowIter(0) | |
| 99 , m_client(client) | |
| 100 , m_frameContext(frameContext) | |
| 101 { } | |
| 102 | |
| 103 bool prepareToDecode(); | |
| 104 bool outputRow(GIFRow::const_iterator rowBegin); | |
| 105 bool doLZW(const unsigned char* block, size_t bytesInBlock); | |
| 106 bool hasRemainingRows() { return rowsRemaining; } | |
| 107 | |
| 108 private: | |
| 109 // LZW decoding states and output states. | |
| 110 int codesize; | |
| 111 int codemask; | |
| 112 int clearCode; // Codeword used to trigger dictionary reset. | |
| 113 int avail; // Index of next available slot in dictionary. | |
| 114 int oldcode; | |
| 115 unsigned char firstchar; | |
| 116 int bits; // Number of unread bits in "datum". | |
| 117 int datum; // 32-bit input buffer. | |
| 118 int ipass; // Interlace pass; Ranges 1-4 if interlaced. | |
| 119 size_t irow; // Current output row, starting at zero. | |
| 120 size_t rowsRemaining; // Rows remaining to be output. | |
| 121 | |
| 122 unsigned short prefix[MAX_DICTIONARY_ENTRIES]; | |
| 123 unsigned char suffix[MAX_DICTIONARY_ENTRIES]; | |
| 124 unsigned short suffixLength[MAX_DICTIONARY_ENTRIES]; | |
| 125 GIFRow rowBuffer; // Single scanline temporary buffer. | |
| 126 GIFRow::iterator rowIter; | |
| 127 | |
| 128 // Initialized during construction and read-only. | |
| 129 WebCore::GIFImageDecoder* m_client; | |
| 130 const GIFFrameContext* m_frameContext; | |
| 131 }; | |
| 132 | |
| 133 // Data structure for one LZW block. | |
| 134 struct GIFLZWBlock { | |
| 135 WTF_MAKE_FAST_ALLOCATED; | |
| 136 public: | |
| 137 GIFLZWBlock(size_t position, size_t size) | |
| 138 : blockPosition(position) | |
| 139 , blockSize(size) | |
| 140 { | |
| 141 } | |
| 142 | |
| 143 size_t blockPosition; | |
| 144 size_t blockSize; | |
| 145 }; | |
| 146 | |
| 147 class GIFColorMap { | |
| 148 WTF_MAKE_FAST_ALLOCATED; | |
| 149 public: | |
| 150 typedef Vector<WebCore::ImageFrame::PixelData> Table; | |
| 151 | |
| 152 GIFColorMap() | |
| 153 : m_isDefined(false) | |
| 154 , m_position(0) | |
| 155 , m_colors(0) | |
| 156 { | |
| 157 } | |
| 158 | |
| 159 // Set position and number of colors for the RGB table in the data stream. | |
| 160 void setTablePositionAndSize(size_t position, size_t colors) | |
| 161 { | |
| 162 m_position = position; | |
| 163 m_colors = colors; | |
| 164 } | |
| 165 void setDefined() { m_isDefined = true; } | |
| 166 bool isDefined() const { return m_isDefined; } | |
| 167 | |
| 168 // Build RGBA table using the data stream. | |
| 169 void buildTable(const unsigned char* data, size_t length); | |
| 170 const Table& table() const { return m_table; } | |
| 171 | |
| 172 private: | |
| 173 bool m_isDefined; | |
| 174 size_t m_position; | |
| 175 size_t m_colors; | |
| 176 Table m_table; | |
| 177 }; | |
| 178 | |
| 179 // Frame output state machine. | |
| 180 struct GIFFrameContext { | |
| 181 WTF_MAKE_FAST_ALLOCATED; | |
| 182 public: | |
| 183 GIFFrameContext(int id) | |
| 184 : m_frameId(id) | |
| 185 , m_xOffset(0) | |
| 186 , m_yOffset(0) | |
| 187 , m_width(0) | |
| 188 , m_height(0) | |
| 189 , m_transparentPixel(kNotFound) | |
| 190 , m_disposalMethod(WebCore::ImageFrame::DisposeNotSpecified) | |
| 191 , m_dataSize(0) | |
| 192 , m_progressiveDisplay(false) | |
| 193 , m_interlaced(false) | |
| 194 , m_delayTime(0) | |
| 195 , m_currentLzwBlock(0) | |
| 196 , m_isComplete(false) | |
| 197 , m_isHeaderDefined(false) | |
| 198 , m_isDataSizeDefined(false) | |
| 199 { | |
| 200 } | |
| 201 | |
| 202 ~GIFFrameContext() | |
| 203 { | |
| 204 } | |
| 205 | |
| 206 void addLzwBlock(size_t position, size_t size) | |
| 207 { | |
| 208 m_lzwBlocks.append(GIFLZWBlock(position, size)); | |
| 209 } | |
| 210 | |
| 211 bool decode(const unsigned char* data, size_t length, WebCore::GIFImageDecod
er* client, bool* frameDecoded); | |
| 212 | |
| 213 int frameId() const { return m_frameId; } | |
| 214 void setRect(unsigned x, unsigned y, unsigned width, unsigned height) | |
| 215 { | |
| 216 m_xOffset = x; | |
| 217 m_yOffset = y; | |
| 218 m_width = width; | |
| 219 m_height = height; | |
| 220 } | |
| 221 WebCore::IntRect frameRect() const { return WebCore::IntRect(m_xOffset, m_yO
ffset, m_width, m_height); } | |
| 222 unsigned xOffset() const { return m_xOffset; } | |
| 223 unsigned yOffset() const { return m_yOffset; } | |
| 224 unsigned width() const { return m_width; } | |
| 225 unsigned height() const { return m_height; } | |
| 226 size_t transparentPixel() const { return m_transparentPixel; } | |
| 227 void setTransparentPixel(size_t pixel) { m_transparentPixel = pixel; } | |
| 228 WebCore::ImageFrame::DisposalMethod disposalMethod() const { return m_dispos
alMethod; } | |
| 229 void setDisposalMethod(WebCore::ImageFrame::DisposalMethod disposalMethod) {
m_disposalMethod = disposalMethod; } | |
| 230 unsigned delayTime() const { return m_delayTime; } | |
| 231 void setDelayTime(unsigned delay) { m_delayTime = delay; } | |
| 232 bool isComplete() const { return m_isComplete; } | |
| 233 void setComplete() { m_isComplete = true; } | |
| 234 bool isHeaderDefined() const { return m_isHeaderDefined; } | |
| 235 void setHeaderDefined() { m_isHeaderDefined = true; } | |
| 236 bool isDataSizeDefined() const { return m_isDataSizeDefined; } | |
| 237 int dataSize() const { return m_dataSize; } | |
| 238 void setDataSize(int size) | |
| 239 { | |
| 240 m_dataSize = size; | |
| 241 m_isDataSizeDefined = true; | |
| 242 } | |
| 243 bool progressiveDisplay() const { return m_progressiveDisplay; } | |
| 244 void setProgressiveDisplay(bool progressiveDisplay) { m_progressiveDisplay =
progressiveDisplay; } | |
| 245 bool interlaced() const { return m_interlaced; } | |
| 246 void setInterlaced(bool interlaced) { m_interlaced = interlaced; } | |
| 247 | |
| 248 void clearDecodeState() { m_lzwContext.clear(); } | |
| 249 const GIFColorMap& localColorMap() const { return m_localColorMap; } | |
| 250 GIFColorMap& localColorMap() { return m_localColorMap; } | |
| 251 | |
| 252 private: | |
| 253 int m_frameId; | |
| 254 unsigned m_xOffset; | |
| 255 unsigned m_yOffset; // With respect to "screen" origin. | |
| 256 unsigned m_width; | |
| 257 unsigned m_height; | |
| 258 size_t m_transparentPixel; // Index of transparent pixel. Value is kNotFound
if there is no transparent pixel. | |
| 259 WebCore::ImageFrame::DisposalMethod m_disposalMethod; // Restore to backgrou
nd, leave in place, etc. | |
| 260 int m_dataSize; | |
| 261 | |
| 262 bool m_progressiveDisplay; // If true, do Haeberli interlace hack. | |
| 263 bool m_interlaced; // True, if scanlines arrive interlaced order. | |
| 264 | |
| 265 unsigned m_delayTime; // Display time, in milliseconds, for this image in a
multi-image GIF. | |
| 266 | |
| 267 OwnPtr<GIFLZWContext> m_lzwContext; | |
| 268 Vector<GIFLZWBlock> m_lzwBlocks; // LZW blocks for this frame. | |
| 269 GIFColorMap m_localColorMap; | |
| 270 | |
| 271 size_t m_currentLzwBlock; | |
| 272 bool m_isComplete; | |
| 273 bool m_isHeaderDefined; | |
| 274 bool m_isDataSizeDefined; | |
| 275 }; | |
| 276 | |
| 277 class GIFImageReader { | |
| 278 WTF_MAKE_FAST_ALLOCATED; | |
| 279 public: | |
| 280 GIFImageReader(WebCore::GIFImageDecoder* client = 0) | |
| 281 : m_client(client) | |
| 282 , m_state(GIFType) | |
| 283 , m_bytesToConsume(6) // Number of bytes for GIF type, either "GIF87a" o
r "GIF89a". | |
| 284 , m_bytesRead(0) | |
| 285 , m_version(0) | |
| 286 , m_screenWidth(0) | |
| 287 , m_screenHeight(0) | |
| 288 , m_loopCount(cLoopCountNotSeen) | |
| 289 , m_parseCompleted(false) | |
| 290 { | |
| 291 } | |
| 292 | |
| 293 ~GIFImageReader() | |
| 294 { | |
| 295 } | |
| 296 | |
| 297 void setData(PassRefPtr<WebCore::SharedBuffer> data) { m_data = data; } | |
| 298 bool parse(WebCore::GIFImageDecoder::GIFParseQuery); | |
| 299 bool decode(size_t frameIndex); | |
| 300 | |
| 301 size_t imagesCount() const | |
| 302 { | |
| 303 if (m_frames.isEmpty()) | |
| 304 return 0; | |
| 305 | |
| 306 // This avoids counting an empty frame when the file is truncated right
after | |
| 307 // GIFControlExtension but before GIFImageHeader. | |
| 308 // FIXME: This extra complexity is not necessary and we should just repo
rt m_frames.size(). | |
| 309 return m_frames.last()->isHeaderDefined() ? m_frames.size() : m_frames.s
ize() - 1; | |
| 310 } | |
| 311 int loopCount() const { return m_loopCount; } | |
| 312 | |
| 313 const GIFColorMap& globalColorMap() const | |
| 314 { | |
| 315 return m_globalColorMap; | |
| 316 } | |
| 317 | |
| 318 const GIFFrameContext* frameContext(size_t index) const | |
| 319 { | |
| 320 return index < m_frames.size() ? m_frames[index].get() : 0; | |
| 321 } | |
| 322 | |
| 323 bool parseCompleted() const { return m_parseCompleted; } | |
| 324 | |
| 325 void clearDecodeState(size_t index) { m_frames[index]->clearDecodeState(); } | |
| 326 | |
| 327 private: | |
| 328 bool parseData(size_t dataPosition, size_t len, WebCore::GIFImageDecoder::GI
FParseQuery); | |
| 329 void setRemainingBytes(size_t); | |
| 330 | |
| 331 const unsigned char* data(size_t dataPosition) const | |
| 332 { | |
| 333 return reinterpret_cast<const unsigned char*>(m_data->data()) + dataPosi
tion; | |
| 334 } | |
| 335 | |
| 336 void addFrameIfNecessary(); | |
| 337 bool currentFrameIsFirstFrame() const | |
| 338 { | |
| 339 return m_frames.isEmpty() || (m_frames.size() == 1u && !m_frames[0]->isC
omplete()); | |
| 340 } | |
| 341 | |
| 342 WebCore::GIFImageDecoder* m_client; | |
| 343 | |
| 344 // Parsing state machine. | |
| 345 GIFState m_state; // Current decoder master state. | |
| 346 size_t m_bytesToConsume; // Number of bytes to consume for next stage of par
sing. | |
| 347 size_t m_bytesRead; // Number of bytes processed. | |
| 348 | |
| 349 // Global (multi-image) state. | |
| 350 int m_version; // Either 89 for GIF89 or 87 for GIF87. | |
| 351 unsigned m_screenWidth; // Logical screen width & height. | |
| 352 unsigned m_screenHeight; | |
| 353 GIFColorMap m_globalColorMap; | |
| 354 int m_loopCount; // Netscape specific extension block to control the number
of animation loops a GIF renders. | |
| 355 | |
| 356 Vector<OwnPtr<GIFFrameContext> > m_frames; | |
| 357 | |
| 358 RefPtr<WebCore::SharedBuffer> m_data; | |
| 359 bool m_parseCompleted; | |
| 360 }; | |
| 361 | |
| 362 #endif | |
| OLD | NEW |