2001-02-21 01:45:10 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-02-21 01:45:10 +03:00
|
|
|
|
2015-04-06 02:22:00 +03:00
|
|
|
#ifndef mozilla_image_decoders_nsGIFDecoder2_h
|
|
|
|
#define mozilla_image_decoders_nsGIFDecoder2_h
|
2001-02-21 01:45:10 +03:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
#include "Decoder.h"
|
2001-02-21 01:45:10 +03:00
|
|
|
#include "GIF2.h"
|
2016-06-08 22:17:17 +03:00
|
|
|
#include "StreamingLexer.h"
|
2016-03-10 02:39:02 +03:00
|
|
|
#include "SurfacePipe.h"
|
2001-02-21 01:45:10 +03:00
|
|
|
|
2010-08-14 08:09:49 +04:00
|
|
|
namespace mozilla {
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2010-08-14 08:09:49 +04:00
|
|
|
class RasterImage;
|
|
|
|
|
2001-02-21 01:45:10 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// nsGIFDecoder2 Definition
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
class nsGIFDecoder2 : public Decoder
|
2001-02-21 01:45:10 +03:00
|
|
|
{
|
|
|
|
public:
|
2005-03-10 16:31:26 +03:00
|
|
|
~nsGIFDecoder2();
|
2001-05-31 18:25:33 +04:00
|
|
|
|
2016-08-03 02:45:22 +03:00
|
|
|
protected:
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult DoDecode(SourceBufferIterator& aIterator,
|
|
|
|
IResumable* aOnResume) override;
|
2016-07-11 10:38:54 +03:00
|
|
|
nsresult FinishInternal() override;
|
2016-08-03 02:45:22 +03:00
|
|
|
|
2017-02-15 22:15:15 +03:00
|
|
|
Maybe<Telemetry::HistogramID> SpeedHistogram() const override;
|
2010-08-23 06:30:46 +04:00
|
|
|
|
2007-06-25 20:41:21 +04:00
|
|
|
private:
|
2015-07-23 08:39:56 +03:00
|
|
|
friend class DecoderFactory;
|
|
|
|
|
|
|
|
// Decoders should only be instantiated via DecoderFactory.
|
|
|
|
explicit nsGIFDecoder2(RasterImage* aImage);
|
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
/// Called when we begin decoding the image.
|
2007-06-25 20:41:21 +04:00
|
|
|
void BeginGIF();
|
2016-03-10 02:39:02 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when we begin decoding a frame.
|
|
|
|
*
|
|
|
|
* @param aFrameRect The region of the image that contains data. The region
|
|
|
|
* outside this rect is transparent.
|
|
|
|
* @param aDepth The palette depth of this frame.
|
|
|
|
* @param aIsInterlaced If true, this frame is an interlaced frame.
|
|
|
|
*/
|
|
|
|
nsresult BeginImageFrame(const gfx::IntRect& aFrameRect,
|
|
|
|
uint16_t aDepth,
|
|
|
|
bool aIsInterlaced);
|
|
|
|
|
|
|
|
/// Called when we finish decoding a frame.
|
2007-06-25 20:41:21 +04:00
|
|
|
void EndImageFrame();
|
2016-03-10 02:39:02 +03:00
|
|
|
|
|
|
|
/// Called when we finish decoding the entire image.
|
2010-09-12 19:22:30 +04:00
|
|
|
void FlushImageData();
|
2003-02-22 19:38:47 +03:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
/// Transforms a palette index into a pixel.
|
|
|
|
template <typename PixelSize> PixelSize
|
|
|
|
ColormapIndexToPixel(uint8_t aIndex);
|
|
|
|
|
|
|
|
/// A generator function that performs LZW decompression and yields pixels.
|
|
|
|
template <typename PixelSize> NextPixel<PixelSize>
|
2016-06-08 22:17:17 +03:00
|
|
|
YieldPixel(const uint8_t* aData, size_t aLength, size_t* aBytesReadOut);
|
|
|
|
|
|
|
|
/// Checks if we have transparency, either because the header indicates that
|
|
|
|
/// there's alpha, or because the frame rect doesn't cover the entire image.
|
|
|
|
bool CheckForTransparency(const gfx::IntRect& aFrameRect);
|
|
|
|
|
|
|
|
// @return the clear code used for LZW decompression.
|
|
|
|
int ClearCode() const { return 1 << mGIFStruct.datasize; }
|
|
|
|
|
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
FAILURE,
|
|
|
|
SUCCESS,
|
|
|
|
GIF_HEADER,
|
|
|
|
SCREEN_DESCRIPTOR,
|
|
|
|
GLOBAL_COLOR_TABLE,
|
|
|
|
FINISHED_GLOBAL_COLOR_TABLE,
|
|
|
|
BLOCK_HEADER,
|
|
|
|
EXTENSION_HEADER,
|
|
|
|
GRAPHIC_CONTROL_EXTENSION,
|
|
|
|
APPLICATION_IDENTIFIER,
|
|
|
|
NETSCAPE_EXTENSION_SUB_BLOCK,
|
|
|
|
NETSCAPE_EXTENSION_DATA,
|
|
|
|
IMAGE_DESCRIPTOR,
|
2016-07-30 02:44:16 +03:00
|
|
|
FINISH_IMAGE_DESCRIPTOR,
|
2016-06-08 22:17:17 +03:00
|
|
|
LOCAL_COLOR_TABLE,
|
|
|
|
FINISHED_LOCAL_COLOR_TABLE,
|
|
|
|
IMAGE_DATA_BLOCK,
|
|
|
|
IMAGE_DATA_SUB_BLOCK,
|
|
|
|
LZW_DATA,
|
2016-06-21 06:42:31 +03:00
|
|
|
SKIP_LZW_DATA,
|
2016-06-08 22:17:17 +03:00
|
|
|
FINISHED_LZW_DATA,
|
|
|
|
SKIP_SUB_BLOCKS,
|
|
|
|
SKIP_DATA_THEN_SKIP_SUB_BLOCKS,
|
|
|
|
FINISHED_SKIPPING_DATA
|
|
|
|
};
|
|
|
|
|
|
|
|
LexerTransition<State> ReadGIFHeader(const char* aData);
|
|
|
|
LexerTransition<State> ReadScreenDescriptor(const char* aData);
|
|
|
|
LexerTransition<State> ReadGlobalColorTable(const char* aData, size_t aLength);
|
|
|
|
LexerTransition<State> FinishedGlobalColorTable();
|
|
|
|
LexerTransition<State> ReadBlockHeader(const char* aData);
|
|
|
|
LexerTransition<State> ReadExtensionHeader(const char* aData);
|
|
|
|
LexerTransition<State> ReadGraphicControlExtension(const char* aData);
|
|
|
|
LexerTransition<State> ReadApplicationIdentifier(const char* aData);
|
|
|
|
LexerTransition<State> ReadNetscapeExtensionSubBlock(const char* aData);
|
|
|
|
LexerTransition<State> ReadNetscapeExtensionData(const char* aData);
|
|
|
|
LexerTransition<State> ReadImageDescriptor(const char* aData);
|
2016-07-30 02:44:16 +03:00
|
|
|
LexerTransition<State> FinishImageDescriptor(const char* aData);
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<State> ReadLocalColorTable(const char* aData, size_t aLength);
|
|
|
|
LexerTransition<State> FinishedLocalColorTable();
|
|
|
|
LexerTransition<State> ReadImageDataBlock(const char* aData);
|
|
|
|
LexerTransition<State> ReadImageDataSubBlock(const char* aData);
|
|
|
|
LexerTransition<State> ReadLZWData(const char* aData, size_t aLength);
|
|
|
|
LexerTransition<State> SkipSubBlocks(const char* aData);
|
|
|
|
|
|
|
|
// The StreamingLexer used to manage input. The initial size of the buffer is
|
|
|
|
// chosen as a little larger than the maximum size of any fixed-length data we
|
|
|
|
// have to read for a state. We read variable-length data in unbuffered mode
|
|
|
|
// so the buffer shouldn't have to be resized during decoding.
|
|
|
|
StreamingLexer<State, 16> mLexer;
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mOldColor; // The old value of the transparent pixel
|
2009-07-22 08:20:01 +04:00
|
|
|
|
|
|
|
// The frame number of the currently-decoding frame when we're in the middle
|
|
|
|
// of decoding it, and -1 otherwise.
|
2014-06-18 18:54:59 +04:00
|
|
|
int32_t mCurrentFrameIndex;
|
2009-07-22 08:20:01 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// When we're reading in the global or local color table, this records our
|
|
|
|
// current position - i.e., the offset into which the next byte should be
|
|
|
|
// written.
|
|
|
|
size_t mColorTablePos;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t mColorMask; // Apply this to the pixel to keep within colormap
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mGIFOpen;
|
|
|
|
bool mSawTransparency;
|
2001-02-21 01:45:10 +03:00
|
|
|
|
2007-06-25 20:41:21 +04:00
|
|
|
gif_struct mGIFStruct;
|
2016-03-10 02:39:02 +03:00
|
|
|
|
|
|
|
SurfacePipe mPipe; /// The SurfacePipe used to write to the output surface.
|
2007-06-25 20:41:21 +04:00
|
|
|
};
|
2002-09-18 03:05:27 +04:00
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-23 06:30:46 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-04-06 02:22:00 +03:00
|
|
|
#endif // mozilla_image_decoders_nsGIFDecoder2_h
|