📄 gifimagereader.cpp
字号:
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Chris Saari <saari@netscape.com> * Apple Computer * Google, Inc. * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** *//*The Graphics Interchange Format(c) is the copyright property of CompuServeIncorporated. Only CompuServe Incorporated is authorized to define, redefine,enhance, alter, modify or change in any way the definition of the format.CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-freelicense for the use of the Graphics Interchange Format(sm) in computersoftware; computer software utilizing GIF(sm) must acknowledge ownership of theGraphics Interchange Format and its Service Mark by CompuServe Incorporated, inUser and Technical Documentation. Computer software utilizing GIF, which isdistributed or may be distributed without User or Technical Documentation mustdisplay to the screen or printer a message acknowledging ownership of theGraphics Interchange Format and the Service Mark by CompuServe Incorporated; inthis case, the acknowledgement may be displayed in an opening screen or leadingbanner, or a closing screen or trailing banner. A message such as the followingmay be used: "The Graphics Interchange Format(c) is the Copyright property of CompuServe Incorporated. GIF(sm) is a Service Mark property of CompuServe Incorporated."For further information, please contact : CompuServe Incorporated Graphics Technology Department 5000 Arlington Center Boulevard Columbus, Ohio 43220 U. S. A.CompuServe Incorporated maintains a mailing list with all those individuals andorganizations who wish to receive copies of this document when it is correctedor revised. This service is offered free of charge; please provide us with yourmailing address.*/#include "config.h"#include "GIFImageReader.h"#include <string.h>#include "GIFImageDecoder.h"using WebCore::GIFImageDecoder;// Define the Mozilla macro setup so that we can leave the macros alone.#define PR_BEGIN_MACRO do {#define PR_END_MACRO } while (0)/* * GETN(n, s) requests at least 'n' bytes available from 'q', at start of state 's' * * Note, the hold will never need to be bigger than 256 bytes to gather up in the hold, * as each GIF block (except colormaps) can never be bigger than 256 bytes. * Colormaps are directly copied in the resp. global_colormap or dynamically allocated local_colormap. * So a fixed buffer in GIFImageReader is good enough. * This buffer is only needed to copy left-over data from one GifWrite call to the next */#define GETN(n,s) \ PR_BEGIN_MACRO \ bytes_to_consume = (n); \ state = (s); \ PR_END_MACRO/* Get a 16-bit value stored in little-endian format */#define GETINT16(p) ((p)[1]<<8|(p)[0])//******************************************************************************// Send the data to the display front-end.void GIFImageReader::output_row(){ GIFFrameReader* gs = frame_reader; int drow_start, drow_end; drow_start = drow_end = gs->irow; /* * Haeberli-inspired hack for interlaced GIFs: Replicate lines while * displaying to diminish the "venetian-blind" effect as the image is * loaded. Adjust pixel vertical positions to avoid the appearance of the * image crawling up the screen as successive passes are drawn. */ if (gs->progressive_display && gs->interlaced && gs->ipass < 4) { unsigned row_dup = 0, row_shift = 0; switch (gs->ipass) { case 1: row_dup = 7; row_shift = 3; break; case 2: row_dup = 3; row_shift = 1; break; case 3: row_dup = 1; row_shift = 0; break; default: break; } drow_start -= row_shift; drow_end = drow_start + row_dup; /* Extend if bottom edge isn't covered because of the shift upward. */ if (((gs->height - 1) - drow_end) <= row_shift) drow_end = gs->height - 1; /* Clamp first and last rows to upper and lower edge of image. */ if (drow_start < 0) drow_start = 0; if ((unsigned)drow_end >= gs->height) drow_end = gs->height - 1; } /* Protect against too much image data */ if ((unsigned)drow_start >= gs->height) return; // CALLBACK: Let the client know we have decoded a row. if (clientptr && frame_reader) clientptr->haveDecodedRow(images_count - 1, frame_reader->rowbuf, frame_reader->rowend, drow_start, drow_end - drow_start + 1, gs->progressive_display && gs->interlaced && gs->ipass > 1); gs->rowp = gs->rowbuf; if (!gs->interlaced) gs->irow++; else { do { switch (gs->ipass) { case 1: gs->irow += 8; if (gs->irow >= gs->height) { gs->ipass++; gs->irow = 4; } break; case 2: gs->irow += 8; if (gs->irow >= gs->height) { gs->ipass++; gs->irow = 2; } break; case 3: gs->irow += 4; if (gs->irow >= gs->height) { gs->ipass++; gs->irow = 1; } break; case 4: gs->irow += 2; if (gs->irow >= gs->height){ gs->ipass++; gs->irow = 0; } break; default: break; } } while (gs->irow > (gs->height - 1)); }}//******************************************************************************/* Perform Lempel-Ziv-Welch decoding */int GIFImageReader::do_lzw(const unsigned char *q){ GIFFrameReader* gs = frame_reader; if (!gs) return 0; int code; int incode; const unsigned char *ch; /* Copy all the decoder state variables into locals so the compiler * won't worry about them being aliased. The locals will be homed * back into the GIF decoder structure when we exit. */ int avail = gs->avail; int bits = gs->bits; int cnt = count; int codesize = gs->codesize; int codemask = gs->codemask; int oldcode = gs->oldcode; int clear_code = gs->clear_code; unsigned char firstchar = gs->firstchar; int datum = gs->datum; if (!gs->prefix) { gs->prefix = new unsigned short[MAX_BITS]; memset(gs->prefix, 0, MAX_BITS * sizeof(short)); } unsigned short *prefix = gs->prefix; unsigned char *stackp = gs->stackp; unsigned char *suffix = gs->suffix; unsigned char *stack = gs->stack; unsigned char *rowp = gs->rowp; unsigned char *rowend = gs->rowend; unsigned rows_remaining = gs->rows_remaining; if (rowp == rowend) return 0;#define OUTPUT_ROW \ PR_BEGIN_MACRO \ output_row(); \ rows_remaining--; \ rowp = frame_reader->rowp; \ if (!rows_remaining) \ goto END; \ PR_END_MACRO for (ch = q; cnt-- > 0; ch++) { /* Feed the next byte into the decoder's 32-bit input buffer. */ datum += ((int) *ch) << bits; bits += 8; /* Check for underflow of decoder's 32-bit input buffer. */ while (bits >= codesize) { /* Get the leading variable-length symbol from the data stream */ code = datum & codemask; datum >>= codesize; bits -= codesize; /* Reset the dictionary to its original state, if requested */ if (code == clear_code) { codesize = gs->datasize + 1; codemask = (1 << codesize) - 1; avail = clear_code + 2; oldcode = -1; continue; } /* Check for explicit end-of-stream code */ if (code == (clear_code + 1)) { /* end-of-stream should only appear after all image data */ if (rows_remaining != 0) return -1; return 0; } if (oldcode == -1) { *rowp++ = suffix[code]; if (rowp == rowend) OUTPUT_ROW; firstchar = oldcode = code; continue; } incode = code; if (code >= avail) { *stackp++ = firstchar; code = oldcode; if (stackp == stack + MAX_BITS) return -1; } while (code >= clear_code) { if (code >= MAX_BITS || code == prefix[code]) return -1; // Even though suffix[] only holds characters through suffix[avail - 1], // allowing code >= avail here lets us be more tolerant of malformed // data. As long as code < MAX_BITS, the only risk is a garbled image, // which is no worse than refusing to display it. *stackp++ = suffix[code]; code = prefix[code]; if (stackp == stack + MAX_BITS) return -1; } *stackp++ = firstchar = suffix[code]; /* Define a new codeword in the dictionary. */ if (avail < 4096) { prefix[avail] = oldcode; suffix[avail] = firstchar; avail++; /* If we've used up all the codewords of a given length * increase the length of codewords by one bit, but don't * exceed the specified maximum codeword size of 12 bits. */ if (((avail & codemask) == 0) && (avail < 4096)) { codesize++; codemask += avail; } } oldcode = incode; /* Copy the decoded data out to the scanline buffer. */ do { *rowp++ = *--stackp; if (rowp == rowend) { OUTPUT_ROW; } } while (stackp > stack); } } END: /* Home the local copies of the GIF decoder state variables */ gs->avail = avail; gs->bits = bits; gs->codesize = codesize; gs->codemask = codemask; count = cnt; gs->oldcode = oldcode; gs->firstchar = firstchar; gs->datum = datum; gs->stackp = stackp; gs->rowp = rowp; gs->rows_remaining = rows_remaining; return 0;}/******************************************************************************//* * process data arriving from the stream for the gif decoder */bool GIFImageReader::read(const unsigned char *buf, unsigned len, GIFImageDecoder::GIFQuery query, unsigned haltAtFrame){ if (!len) { // No new data has come in since the last call, just ignore this call. return true; } const unsigned char *q = buf; // Add what we have so far to the block // If previous call to me left something in the hold first complete current block // Or if we are filling the colormaps, first complete the colormap unsigned char* p = 0; if (state == gif_global_colormap) p = global_colormap; else if (state == gif_image_colormap) p = frame_reader ? frame_reader->local_colormap : 0; else if (bytes_in_hold) p = hold; else p = 0; if (p || (state == gif_global_colormap) || (state == gif_image_colormap)) { // Add what we have sofar to the block unsigned l = len < bytes_to_consume ? len : bytes_to_consume; if (p) memcpy(p + bytes_in_hold, buf, l); if (l < bytes_to_consume) { // Not enough in 'buf' to complete current block, get more bytes_in_hold += l; bytes_to_consume -= l; if (clientptr) clientptr->decodingHalted(0); return true; } // Reset hold buffer count bytes_in_hold = 0; // Point 'q' to complete block in hold (or in colormap) q = p; } // Invariant: // 'q' is start of current to be processed block (hold, colormap or buf) // 'bytes_to_consume' is number of bytes to consume from 'buf' // 'buf' points to the bytes to be consumed from the input buffer // 'len' is number of bytes left in input buffer from position 'buf'. // At entrance of the for loop will 'buf' will be moved 'bytes_to_consume' // to point to next buffer, 'len' is adjusted accordingly. // So that next round in for loop, q gets pointed to the next buffer. for (;len >= bytes_to_consume; q=buf) { // Eat the current block from the buffer, q keeps pointed at current block buf += bytes_to_consume; len -= bytes_to_consume; switch (state) { case gif_lzw: if (do_lzw(q) < 0) { state = gif_error; break; } GETN(1, gif_sub_block); break; case gif_lzw_start: { /* Initialize LZW parser/decoder */ int datasize = *q; // Since we use a codesize of 1 more than the datasize, we need to ensure // that our datasize is strictly less than the MAX_LZW_BITS value (12). // This sets the largest possible codemask correctly at 4095. if (datasize >= MAX_LZW_BITS) { state = gif_error; break; } int clear_code = 1 << datasize; if (clear_code >= MAX_BITS) { state = gif_error; break; } if (frame_reader) { frame_reader->datasize = datasize; frame_reader->clear_code = clear_code; frame_reader->avail = frame_reader->clear_code + 2; frame_reader->oldcode = -1; frame_reader->codesize = frame_reader->datasize + 1; frame_reader->codemask = (1 << frame_reader->codesize) - 1; frame_reader->datum = frame_reader->bits = 0; /* init the tables */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -