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/. */
|
2007-06-25 20:41:21 +04:00
|
|
|
/*
|
|
|
|
The Graphics Interchange Format(c) is the copyright property of CompuServe
|
|
|
|
Incorporated. 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-free
|
|
|
|
license for the use of the Graphics Interchange Format(sm) in computer
|
|
|
|
software; computer software utilizing GIF(sm) must acknowledge ownership of the
|
|
|
|
Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in
|
|
|
|
User and Technical Documentation. Computer software utilizing GIF, which is
|
|
|
|
distributed or may be distributed without User or Technical Documentation must
|
|
|
|
display to the screen or printer a message acknowledging ownership of the
|
|
|
|
Graphics Interchange Format and the Service Mark by CompuServe Incorporated; in
|
|
|
|
this case, the acknowledgement may be displayed in an opening screen or leading
|
|
|
|
banner, or a closing screen or trailing banner. A message such as the following
|
|
|
|
may 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 and
|
|
|
|
organizations who wish to receive copies of this document when it is corrected
|
|
|
|
or revised. This service is offered free of charge; please provide us with your
|
|
|
|
mailing address.
|
|
|
|
*/
|
|
|
|
|
2016-06-30 20:27:03 +03:00
|
|
|
#include "nsGIFDecoder2.h"
|
|
|
|
|
2007-06-25 20:41:21 +04:00
|
|
|
#include <stddef.h>
|
2003-06-29 03:29:11 +04:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
#include "imgFrame.h"
|
2016-06-08 22:17:17 +03:00
|
|
|
#include "mozilla/EndianUtils.h"
|
2010-08-14 08:09:49 +04:00
|
|
|
#include "RasterImage.h"
|
2016-03-10 02:39:02 +03:00
|
|
|
#include "SurfacePipeFactory.h"
|
2001-02-21 01:45:10 +03:00
|
|
|
|
2007-08-26 09:08:53 +04:00
|
|
|
#include "gfxColor.h"
|
2013-09-07 07:08:36 +04:00
|
|
|
#include "gfxPlatform.h"
|
2009-04-07 20:02:11 +04:00
|
|
|
#include "qcms.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2015-02-03 18:05:49 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2015-08-12 20:41:02 +03:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
using std::max;
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
namespace mozilla {
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2010-08-23 06:30:46 +04:00
|
|
|
|
2001-02-21 01:45:10 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// GIF Decoder Implementation
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
static const size_t GIF_HEADER_LEN = 6;
|
|
|
|
static const size_t GIF_SCREEN_DESCRIPTOR_LEN = 7;
|
|
|
|
static const size_t BLOCK_HEADER_LEN = 1;
|
|
|
|
static const size_t SUB_BLOCK_HEADER_LEN = 1;
|
|
|
|
static const size_t EXTENSION_HEADER_LEN = 2;
|
|
|
|
static const size_t GRAPHIC_CONTROL_EXTENSION_LEN = 4;
|
|
|
|
static const size_t APPLICATION_EXTENSION_LEN = 11;
|
|
|
|
static const size_t IMAGE_DESCRIPTOR_LEN = 9;
|
|
|
|
|
|
|
|
// Masks for reading color table information from packed fields in the screen
|
|
|
|
// descriptor and image descriptor blocks.
|
|
|
|
static const uint8_t PACKED_FIELDS_COLOR_TABLE_BIT = 0x80;
|
|
|
|
static const uint8_t PACKED_FIELDS_INTERLACED_BIT = 0x40;
|
|
|
|
static const uint8_t PACKED_FIELDS_TABLE_DEPTH_MASK = 0x07;
|
|
|
|
|
2015-01-16 02:11:35 +03:00
|
|
|
nsGIFDecoder2::nsGIFDecoder2(RasterImage* aImage)
|
2013-01-19 01:47:18 +04:00
|
|
|
: Decoder(aImage),
|
2016-07-18 08:51:19 +03:00
|
|
|
mLexer(Transition::To(State::GIF_HEADER, GIF_HEADER_LEN),
|
|
|
|
Transition::TerminateSuccess()),
|
2008-02-19 10:36:50 +03:00
|
|
|
mOldColor(0),
|
2014-06-18 18:54:59 +04:00
|
|
|
mCurrentFrameIndex(-1),
|
2016-06-08 22:17:17 +03:00
|
|
|
mColorTablePos(0),
|
2019-03-15 20:29:02 +03:00
|
|
|
mColormap(nullptr),
|
|
|
|
mColormapSize(0),
|
2018-06-14 08:21:37 +03:00
|
|
|
mColorMask('\0'),
|
2011-10-17 18:59:28 +04:00
|
|
|
mGIFOpen(false),
|
2019-09-26 02:31:40 +03:00
|
|
|
mSawTransparency(false),
|
|
|
|
mSwizzleFn(nullptr) {
|
2022-03-18 18:22:46 +03:00
|
|
|
// Clear out the structure, excluding the arrays. Ensure that the global
|
|
|
|
// colormap is initialized as opaque.
|
2007-06-25 20:41:21 +04:00
|
|
|
memset(&mGIFStruct, 0, sizeof(mGIFStruct));
|
2022-03-18 18:22:46 +03:00
|
|
|
memset(mGIFStruct.global_colormap, 0xFF, sizeof(mGIFStruct.global_colormap));
|
2019-09-26 02:31:40 +03:00
|
|
|
|
|
|
|
// Each color table will need to be unpacked.
|
2019-11-12 21:22:33 +03:00
|
|
|
mSwizzleFn = SwizzleRow(SurfaceFormat::R8G8B8, SurfaceFormat::OS_RGBA);
|
2019-09-26 02:31:40 +03:00
|
|
|
MOZ_ASSERT(mSwizzleFn);
|
2001-02-21 01:45:10 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 07:51:06 +03:00
|
|
|
nsGIFDecoder2::~nsGIFDecoder2() { free(mGIFStruct.local_colormap); }
|
2001-02-21 01:45:10 +03:00
|
|
|
|
2024-06-25 01:44:30 +03:00
|
|
|
nsresult nsGIFDecoder2::FinishWithErrorInternal() {
|
|
|
|
if (mGIFOpen) {
|
2024-06-25 01:44:30 +03:00
|
|
|
if (WantsFrameCount()) {
|
|
|
|
PostFrameCount(mGIFStruct.images_decoded);
|
|
|
|
}
|
2024-06-25 01:44:30 +03:00
|
|
|
PostLoopCount(mGIFStruct.loop_count);
|
|
|
|
mGIFOpen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Decoder::FinishWithErrorInternal();
|
|
|
|
}
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
nsresult nsGIFDecoder2::FinishInternal() {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!HasError(), "Shouldn't call FinishInternal after error!");
|
2010-09-12 19:22:31 +04:00
|
|
|
|
2024-06-25 01:44:30 +03:00
|
|
|
if (!mGIFOpen) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PostLoopCount(mGIFStruct.loop_count);
|
|
|
|
|
2010-09-12 19:22:30 +04:00
|
|
|
// If the GIF got cut off, handle it anyway
|
2024-06-25 01:44:30 +03:00
|
|
|
if (mCurrentFrameIndex == mGIFStruct.images_decoded) {
|
|
|
|
EndImageFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WantsFrameCount()) {
|
|
|
|
PostFrameCount(mGIFStruct.images_decoded);
|
|
|
|
}
|
|
|
|
|
2024-06-25 01:44:30 +03:00
|
|
|
if (!IsMetadataDecode()) {
|
|
|
|
PostDecodeDone();
|
2009-09-13 02:44:18 +04:00
|
|
|
}
|
2016-07-11 10:38:54 +03:00
|
|
|
|
2024-06-25 01:44:30 +03:00
|
|
|
mGIFOpen = false;
|
2016-07-11 10:38:54 +03:00
|
|
|
return NS_OK;
|
2001-02-21 01:45:10 +03:00
|
|
|
}
|
|
|
|
|
2001-05-31 18:25:33 +04:00
|
|
|
void nsGIFDecoder2::FlushImageData() {
|
2016-03-10 02:39:02 +03:00
|
|
|
Maybe<SurfaceInvalidRect> invalidRect = mPipe.TakeInvalidRect();
|
|
|
|
if (!invalidRect) {
|
2015-09-18 01:03:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
PostInvalidation(invalidRect->mInputSpaceRect,
|
|
|
|
Some(invalidRect->mOutputSpaceRect));
|
2001-05-31 18:25:33 +04:00
|
|
|
}
|
|
|
|
|
2001-02-21 01:45:10 +03:00
|
|
|
//******************************************************************************
|
2007-03-21 02:56:50 +03:00
|
|
|
// GIF decoder callback methods. Part of public API for GIF2
|
2001-02-21 01:45:10 +03:00
|
|
|
//******************************************************************************
|
|
|
|
|
|
|
|
//******************************************************************************
|
2014-11-14 20:59:00 +03:00
|
|
|
void nsGIFDecoder2::BeginGIF() {
|
|
|
|
if (mGIFOpen) {
|
2007-06-25 20:41:21 +04:00
|
|
|
return;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-04-26 11:11:52 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mGIFOpen = true;
|
2001-02-21 01:45:10 +03:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
PostSize(mGIFStruct.screen_width, mGIFStruct.screen_height);
|
2001-02-21 01:45:10 +03:00
|
|
|
}
|
|
|
|
|
2021-10-06 17:41:17 +03:00
|
|
|
bool nsGIFDecoder2::CheckForTransparency(const OrientedIntRect& aFrameRect) {
|
2015-08-12 20:41:02 +03:00
|
|
|
// Check if the image has a transparent color in its palette.
|
|
|
|
if (mGIFStruct.is_transparent) {
|
|
|
|
PostHasTransparency();
|
2015-09-26 11:36:23 +03:00
|
|
|
return true;
|
2015-08-12 20:41:02 +03:00
|
|
|
}
|
|
|
|
|
2022-12-15 03:41:56 +03:00
|
|
|
// This is a bit of a hack. Some sites will use a 1x1 gif that includes no
|
|
|
|
// header information indicating it is transparent, no palette, and no image
|
|
|
|
// data at all (so no pixels get written) to represent a transparent pixel
|
|
|
|
// using the absolute least number of bytes. Generally things are setup to
|
|
|
|
// detect transparency without decoding the image data. So to detect this kind
|
|
|
|
// of transparency without decoing the image data we would have to assume
|
|
|
|
// every gif is transparent, which we would like to avoid. Changing things so
|
|
|
|
// that we can detect transparency at any point of decoding is a bigger change
|
|
|
|
// and not worth it for one questionable 1x1 gif. Using this "trick" for
|
|
|
|
// anything but 1x1 transparent spacer gifs doesn't make sense, so it's
|
|
|
|
// reasonable to target 1x1 gifs just for this.
|
|
|
|
if (mGIFStruct.screen_width == 1 && mGIFStruct.screen_height == 1) {
|
|
|
|
PostHasTransparency();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:41:02 +03:00
|
|
|
if (mGIFStruct.images_decoded > 0) {
|
2015-09-26 11:36:23 +03:00
|
|
|
return false; // We only care about first frame padding below.
|
2015-08-12 20:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we need padding on the first frame, that means we don't draw into part
|
|
|
|
// of the image at all. Report that as transparency.
|
2021-10-06 17:41:17 +03:00
|
|
|
OrientedIntRect imageRect(0, 0, mGIFStruct.screen_width,
|
|
|
|
mGIFStruct.screen_height);
|
2015-08-12 20:41:02 +03:00
|
|
|
if (!imageRect.IsEqualEdges(aFrameRect)) {
|
|
|
|
PostHasTransparency();
|
2015-09-26 11:36:23 +03:00
|
|
|
mSawTransparency = true; // Make sure we don't optimize it away.
|
|
|
|
return true;
|
2015-08-12 20:41:02 +03:00
|
|
|
}
|
2015-09-26 11:36:23 +03:00
|
|
|
|
|
|
|
return false;
|
2015-08-12 20:41:02 +03:00
|
|
|
}
|
|
|
|
|
2001-02-21 01:45:10 +03:00
|
|
|
//******************************************************************************
|
2021-10-06 17:41:17 +03:00
|
|
|
nsresult nsGIFDecoder2::BeginImageFrame(const OrientedIntRect& aFrameRect,
|
2016-03-10 02:39:02 +03:00
|
|
|
uint16_t aDepth, bool aIsInterlaced) {
|
2014-11-17 22:16:45 +03:00
|
|
|
MOZ_ASSERT(HasSize());
|
|
|
|
|
2024-06-25 01:44:30 +03:00
|
|
|
// If we are just counting frames for a metadata decode, there is no actual
|
|
|
|
// decoding done. We are just iterating over the blocks to find when a frame
|
|
|
|
// begins and ends.
|
|
|
|
if (WantsFrameCount()) {
|
|
|
|
mCurrentFrameIndex = mGIFStruct.images_decoded;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
bool hasTransparency = CheckForTransparency(aFrameRect);
|
2015-07-11 05:26:15 +03:00
|
|
|
|
2015-09-18 01:03:34 +03:00
|
|
|
// Make sure there's no animation if we're downscaling.
|
2016-08-05 14:21:40 +03:00
|
|
|
MOZ_ASSERT_IF(Size() != OutputSize(), !GetImageMetadata().HasAnimation());
|
2015-09-18 01:03:34 +03:00
|
|
|
|
2019-03-15 20:29:02 +03:00
|
|
|
Maybe<AnimationParams> animParams;
|
|
|
|
if (!IsFirstFrameDecode()) {
|
2021-10-06 17:41:17 +03:00
|
|
|
animParams.emplace(aFrameRect.ToUnknownRect(),
|
2019-03-15 20:29:02 +03:00
|
|
|
FrameTimeout::FromRawMilliseconds(mGIFStruct.delay_time),
|
|
|
|
uint32_t(mGIFStruct.images_decoded), BlendMethod::OVER,
|
|
|
|
DisposalMethod(mGIFStruct.disposal_method));
|
|
|
|
}
|
2018-05-29 15:36:12 +03:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
SurfacePipeFlags pipeFlags =
|
|
|
|
aIsInterlaced ? SurfacePipeFlags::DEINTERLACE : SurfacePipeFlags();
|
2016-03-10 02:39:02 +03:00
|
|
|
|
2018-09-17 22:06:28 +03:00
|
|
|
gfx::SurfaceFormat format;
|
2016-03-10 02:39:02 +03:00
|
|
|
if (mGIFStruct.images_decoded == 0) {
|
|
|
|
// The first frame may be displayed progressively.
|
|
|
|
pipeFlags |= SurfacePipeFlags::PROGRESSIVE_DISPLAY;
|
|
|
|
|
2020-01-06 22:03:41 +03:00
|
|
|
// Only allow opaque surfaces if we are decoding a single image without
|
|
|
|
// transparency. For an animation, there isn't much benefit to RGBX given
|
|
|
|
// the current frame is constantly changing, and there are many risks
|
|
|
|
// since BlendAnimationFilter is able to clear rows of data.
|
|
|
|
format = hasTransparency || animParams ? SurfaceFormat::OS_RGBA
|
|
|
|
: SurfaceFormat::OS_RGBX;
|
2018-09-17 22:06:28 +03:00
|
|
|
} else {
|
2019-11-12 21:22:33 +03:00
|
|
|
format = SurfaceFormat::OS_RGBA;
|
2018-09-17 22:06:28 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 20:29:02 +03:00
|
|
|
Maybe<SurfacePipe> pipe = SurfacePipeFactory::CreateSurfacePipe(
|
2019-10-02 16:37:29 +03:00
|
|
|
this, Size(), OutputSize(), aFrameRect, format, format, animParams,
|
|
|
|
mTransform, pipeFlags);
|
2014-06-18 18:54:59 +04:00
|
|
|
mCurrentFrameIndex = mGIFStruct.images_decoded;
|
2015-07-11 05:26:15 +03:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
if (!pipe) {
|
|
|
|
mPipe = SurfacePipe();
|
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-18 01:03:34 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mPipe = std::move(*pipe);
|
2016-03-10 02:39:02 +03:00
|
|
|
return NS_OK;
|
2007-08-26 09:08:53 +04:00
|
|
|
}
|
2001-04-25 23:52:49 +04:00
|
|
|
|
2001-02-21 01:45:10 +03:00
|
|
|
//******************************************************************************
|
2014-11-14 20:59:00 +03:00
|
|
|
void nsGIFDecoder2::EndImageFrame() {
|
2024-06-25 01:44:30 +03:00
|
|
|
if (WantsFrameCount()) {
|
|
|
|
mGIFStruct.pixels_remaining = 0;
|
|
|
|
mGIFStruct.images_decoded++;
|
2024-07-24 06:16:16 +03:00
|
|
|
mGIFStruct.delay_time = 0;
|
|
|
|
mColormap = nullptr;
|
|
|
|
mColormapSize = 0;
|
2024-06-25 01:44:30 +03:00
|
|
|
mCurrentFrameIndex = -1;
|
|
|
|
|
|
|
|
// Keep updating the count every time we find a frame.
|
|
|
|
PostFrameCount(mGIFStruct.images_decoded);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
Opacity opacity = Opacity::SOME_TRANSPARENCY;
|
2012-12-20 00:11:42 +04:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
if (mGIFStruct.images_decoded == 0) {
|
2016-06-08 22:17:17 +03:00
|
|
|
// We need to send invalidations for the first frame.
|
2010-09-12 19:22:30 +04:00
|
|
|
FlushImageData();
|
2007-11-08 00:33:57 +03:00
|
|
|
|
2015-01-08 11:04:31 +03:00
|
|
|
// The first frame was preallocated with alpha; if it wasn't transparent, we
|
|
|
|
// should fix that. We can also mark it opaque unconditionally if we didn't
|
|
|
|
// actually see any transparent pixels - this test is only valid for the
|
|
|
|
// first frame.
|
2015-09-26 11:36:23 +03:00
|
|
|
if (!mGIFStruct.is_transparent && !mSawTransparency) {
|
2016-03-10 02:39:02 +03:00
|
|
|
opacity = Opacity::FULLY_OPAQUE;
|
2008-01-08 12:51:13 +03:00
|
|
|
}
|
|
|
|
}
|
2006-03-27 23:04:51 +04:00
|
|
|
|
2009-09-02 19:50:14 +04:00
|
|
|
// Unconditionally increment images_decoded, because we unconditionally
|
|
|
|
// append frames in BeginImageFrame(). This ensures that images_decoded
|
2010-08-23 06:30:45 +04:00
|
|
|
// always refers to the frame in mImage we're currently decoding,
|
2009-09-02 19:50:14 +04:00
|
|
|
// even if some of them weren't decoded properly and thus are blank.
|
|
|
|
mGIFStruct.images_decoded++;
|
|
|
|
|
2019-12-10 08:30:54 +03:00
|
|
|
// Reset graphic control extension parameters that we shouldn't reuse
|
|
|
|
// between frames.
|
|
|
|
mGIFStruct.delay_time = 0;
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
// Tell the superclass we finished a frame
|
2018-05-29 15:36:12 +03:00
|
|
|
PostFrameStop(opacity);
|
2001-02-21 01:45:10 +03:00
|
|
|
|
2007-08-26 09:08:53 +04:00
|
|
|
// Reset the transparent pixel
|
|
|
|
if (mOldColor) {
|
|
|
|
mColormap[mGIFStruct.tpixel] = mOldColor;
|
|
|
|
mOldColor = 0;
|
2007-06-25 20:41:21 +04:00
|
|
|
}
|
2009-07-22 08:20:01 +04:00
|
|
|
|
2019-03-15 20:29:02 +03:00
|
|
|
mColormap = nullptr;
|
|
|
|
mColormapSize = 0;
|
2014-06-18 18:54:59 +04:00
|
|
|
mCurrentFrameIndex = -1;
|
2007-06-25 20:41:21 +04:00
|
|
|
}
|
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
template <typename PixelSize>
|
|
|
|
PixelSize nsGIFDecoder2::ColormapIndexToPixel(uint8_t aIndex) {
|
|
|
|
MOZ_ASSERT(sizeof(PixelSize) == sizeof(uint32_t));
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
// Retrieve the next color, clamping to the size of the colormap.
|
|
|
|
uint32_t color = mColormap[aIndex & mColorMask];
|
2014-11-14 20:59:00 +03:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
// Check for transparency.
|
|
|
|
if (mGIFStruct.is_transparent) {
|
|
|
|
mSawTransparency = mSawTransparency || color == 0;
|
|
|
|
}
|
2007-11-08 00:33:57 +03:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
return color;
|
|
|
|
}
|
2007-11-08 00:33:57 +03:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
template <>
|
|
|
|
uint8_t nsGIFDecoder2::ColormapIndexToPixel<uint8_t>(uint8_t aIndex) {
|
|
|
|
return aIndex & mColorMask;
|
|
|
|
}
|
2014-11-14 20:59:00 +03:00
|
|
|
|
2016-03-10 02:39:02 +03:00
|
|
|
template <typename PixelSize>
|
2023-03-27 10:20:25 +03:00
|
|
|
std::tuple<int32_t, Maybe<WriteState>> nsGIFDecoder2::YieldPixels(
|
2018-05-25 13:52:03 +03:00
|
|
|
const uint8_t* aData, size_t aLength, size_t* aBytesReadOut,
|
|
|
|
PixelSize* aPixelBlock, int32_t aBlockSize) {
|
2016-06-08 22:17:17 +03:00
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
MOZ_ASSERT(aBytesReadOut);
|
2016-03-10 02:39:02 +03:00
|
|
|
MOZ_ASSERT(mGIFStruct.stackp >= mGIFStruct.stack);
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// Advance to the next byte we should read.
|
|
|
|
const uint8_t* data = aData + *aBytesReadOut;
|
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
int32_t written = 0;
|
|
|
|
while (aBlockSize > written) {
|
|
|
|
// If we don't have any decoded data to yield, try to read some input and
|
|
|
|
// produce some.
|
|
|
|
if (mGIFStruct.stackp == mGIFStruct.stack) {
|
|
|
|
while (mGIFStruct.bits < mGIFStruct.codesize &&
|
|
|
|
*aBytesReadOut < aLength) {
|
|
|
|
// Feed the next byte into the decoder's 32-bit input buffer.
|
|
|
|
mGIFStruct.datum += int32_t(*data) << mGIFStruct.bits;
|
|
|
|
mGIFStruct.bits += 8;
|
|
|
|
data += 1;
|
|
|
|
*aBytesReadOut += 1;
|
|
|
|
}
|
2007-11-27 12:35:18 +03:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
if (mGIFStruct.bits < mGIFStruct.codesize) {
|
2023-03-27 10:20:25 +03:00
|
|
|
return std::make_tuple(written, Some(WriteState::NEED_MORE_DATA));
|
2018-05-25 13:52:03 +03:00
|
|
|
}
|
2015-09-18 01:03:34 +03:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
// Get the leading variable-length symbol from the data stream.
|
|
|
|
int code = mGIFStruct.datum & mGIFStruct.codemask;
|
|
|
|
mGIFStruct.datum >>= mGIFStruct.codesize;
|
|
|
|
mGIFStruct.bits -= mGIFStruct.codesize;
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
const int clearCode = ClearCode();
|
2007-08-26 09:08:53 +04:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
// Reset the dictionary to its original state, if requested
|
|
|
|
if (code == clearCode) {
|
|
|
|
mGIFStruct.codesize = mGIFStruct.datasize + 1;
|
|
|
|
mGIFStruct.codemask = (1 << mGIFStruct.codesize) - 1;
|
|
|
|
mGIFStruct.avail = clearCode + 2;
|
|
|
|
mGIFStruct.oldcode = -1;
|
2023-03-27 10:20:25 +03:00
|
|
|
return std::make_tuple(written, Some(WriteState::NEED_MORE_DATA));
|
2018-05-25 13:52:03 +03:00
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
// Check for explicit end-of-stream code. It should only appear after all
|
|
|
|
// image data, but if that was the case we wouldn't be in this function,
|
|
|
|
// so this is always an error condition.
|
|
|
|
if (code == (clearCode + 1)) {
|
2023-03-27 10:20:25 +03:00
|
|
|
return std::make_tuple(written, Some(WriteState::FAILURE));
|
2007-06-25 20:41:21 +04:00
|
|
|
}
|
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
if (mGIFStruct.oldcode == -1) {
|
|
|
|
if (code >= MAX_BITS) {
|
|
|
|
// The code's too big; something's wrong.
|
2023-03-27 10:20:25 +03:00
|
|
|
return std::make_tuple(written, Some(WriteState::FAILURE));
|
2018-05-25 13:52:03 +03:00
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
mGIFStruct.firstchar = mGIFStruct.oldcode = code;
|
2015-10-25 23:14:14 +03:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
// Yield a pixel at the appropriate index in the colormap.
|
|
|
|
mGIFStruct.pixels_remaining--;
|
|
|
|
aPixelBlock[written++] =
|
|
|
|
ColormapIndexToPixel<PixelSize>(mGIFStruct.suffix[code]);
|
|
|
|
continue;
|
2007-06-25 20:41:21 +04:00
|
|
|
}
|
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
int incode = code;
|
|
|
|
if (code >= mGIFStruct.avail) {
|
|
|
|
*mGIFStruct.stackp++ = mGIFStruct.firstchar;
|
|
|
|
code = mGIFStruct.oldcode;
|
|
|
|
|
|
|
|
if (mGIFStruct.stackp >= mGIFStruct.stack + MAX_BITS) {
|
|
|
|
// Stack overflow; something's wrong.
|
2023-03-27 10:20:25 +03:00
|
|
|
return std::make_tuple(written, Some(WriteState::FAILURE));
|
2018-05-25 13:52:03 +03:00
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
}
|
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
while (code >= clearCode) {
|
|
|
|
if ((code >= MAX_BITS) || (code == mGIFStruct.prefix[code])) {
|
2023-03-27 10:20:25 +03:00
|
|
|
return std::make_tuple(written, Some(WriteState::FAILURE));
|
2018-05-25 13:52:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*mGIFStruct.stackp++ = mGIFStruct.suffix[code];
|
|
|
|
code = mGIFStruct.prefix[code];
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
if (mGIFStruct.stackp >= mGIFStruct.stack + MAX_BITS) {
|
|
|
|
// Stack overflow; something's wrong.
|
2023-03-27 10:20:25 +03:00
|
|
|
return std::make_tuple(written, Some(WriteState::FAILURE));
|
2018-05-25 13:52:03 +03:00
|
|
|
}
|
2016-03-10 02:39:02 +03:00
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
*mGIFStruct.stackp++ = mGIFStruct.firstchar = mGIFStruct.suffix[code];
|
|
|
|
|
|
|
|
// Define a new codeword in the dictionary.
|
|
|
|
if (mGIFStruct.avail < 4096) {
|
|
|
|
mGIFStruct.prefix[mGIFStruct.avail] = mGIFStruct.oldcode;
|
|
|
|
mGIFStruct.suffix[mGIFStruct.avail] = mGIFStruct.firstchar;
|
|
|
|
mGIFStruct.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 (((mGIFStruct.avail & mGIFStruct.codemask) == 0) &&
|
|
|
|
(mGIFStruct.avail < 4096)) {
|
|
|
|
mGIFStruct.codesize++;
|
|
|
|
mGIFStruct.codemask += mGIFStruct.avail;
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
}
|
2018-05-25 13:52:03 +03:00
|
|
|
|
|
|
|
mGIFStruct.oldcode = incode;
|
2016-03-10 02:39:02 +03:00
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
if (MOZ_UNLIKELY(mGIFStruct.stackp <= mGIFStruct.stack)) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("No decoded data but we didn't return early?");
|
2023-03-27 10:20:25 +03:00
|
|
|
return std::make_tuple(written, Some(WriteState::FAILURE));
|
2018-05-25 13:52:03 +03:00
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2018-05-25 13:52:03 +03:00
|
|
|
// Yield a pixel at the appropriate index in the colormap.
|
|
|
|
mGIFStruct.pixels_remaining--;
|
|
|
|
aPixelBlock[written++] =
|
|
|
|
ColormapIndexToPixel<PixelSize>(*--mGIFStruct.stackp);
|
2016-03-10 02:39:02 +03:00
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2023-03-27 10:20:25 +03:00
|
|
|
return std::make_tuple(written, Maybe<WriteState>());
|
2016-03-10 02:39:02 +03:00
|
|
|
}
|
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
/// Expand the colormap from RGB to Packed ARGB as needed by Cairo.
|
|
|
|
/// And apply any LCMS transformation.
|
2019-04-04 23:30:10 +03:00
|
|
|
void nsGIFDecoder2::ConvertColormap(uint32_t* aColormap, uint32_t aColors) {
|
2024-06-25 01:44:30 +03:00
|
|
|
// If we are just counting frames for a metadata decode, there is no need to
|
|
|
|
// prep the colormap.
|
|
|
|
if (!aColors || WantsFrameCount()) {
|
2019-09-26 02:31:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-26 09:08:53 +04:00
|
|
|
// Apply CMS transformation if enabled and available
|
2021-02-13 06:59:23 +03:00
|
|
|
if (mCMSMode == CMSMode::All) {
|
2020-04-10 19:26:33 +03:00
|
|
|
qcms_transform* transform = GetCMSsRGBTransform(SurfaceFormat::R8G8B8);
|
2014-11-14 20:59:00 +03:00
|
|
|
if (transform) {
|
2009-04-07 20:02:11 +04:00
|
|
|
qcms_transform_data(transform, aColormap, aColormap, aColors);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2007-08-26 09:08:53 +04:00
|
|
|
}
|
2016-07-01 04:59:29 +03:00
|
|
|
|
2019-09-26 02:31:40 +03:00
|
|
|
// Expand color table from RGB to BGRA.
|
|
|
|
MOZ_ASSERT(mSwizzleFn);
|
|
|
|
uint8_t* data = reinterpret_cast<uint8_t*>(aColormap);
|
|
|
|
mSwizzleFn(data, data, aColors);
|
2007-08-26 09:08:53 +04:00
|
|
|
}
|
|
|
|
|
2016-07-15 08:39:39 +03:00
|
|
|
LexerResult nsGIFDecoder2::DoDecode(SourceBufferIterator& aIterator,
|
|
|
|
IResumable* aOnResume) {
|
2016-07-11 10:05:51 +03:00
|
|
|
MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
|
2016-06-08 22:17:17 +03:00
|
|
|
|
2016-07-15 08:39:39 +03:00
|
|
|
return mLexer.Lex(
|
|
|
|
aIterator, aOnResume,
|
2016-07-11 10:07:07 +03:00
|
|
|
[=](State aState, const char* aData, size_t aLength) {
|
|
|
|
switch (aState) {
|
|
|
|
case State::GIF_HEADER:
|
|
|
|
return ReadGIFHeader(aData);
|
|
|
|
case State::SCREEN_DESCRIPTOR:
|
|
|
|
return ReadScreenDescriptor(aData);
|
|
|
|
case State::GLOBAL_COLOR_TABLE:
|
|
|
|
return ReadGlobalColorTable(aData, aLength);
|
|
|
|
case State::FINISHED_GLOBAL_COLOR_TABLE:
|
|
|
|
return FinishedGlobalColorTable();
|
|
|
|
case State::BLOCK_HEADER:
|
|
|
|
return ReadBlockHeader(aData);
|
|
|
|
case State::EXTENSION_HEADER:
|
|
|
|
return ReadExtensionHeader(aData);
|
|
|
|
case State::GRAPHIC_CONTROL_EXTENSION:
|
|
|
|
return ReadGraphicControlExtension(aData);
|
|
|
|
case State::APPLICATION_IDENTIFIER:
|
|
|
|
return ReadApplicationIdentifier(aData);
|
|
|
|
case State::NETSCAPE_EXTENSION_SUB_BLOCK:
|
|
|
|
return ReadNetscapeExtensionSubBlock(aData);
|
|
|
|
case State::NETSCAPE_EXTENSION_DATA:
|
|
|
|
return ReadNetscapeExtensionData(aData);
|
|
|
|
case State::IMAGE_DESCRIPTOR:
|
|
|
|
return ReadImageDescriptor(aData);
|
|
|
|
case State::LOCAL_COLOR_TABLE:
|
|
|
|
return ReadLocalColorTable(aData, aLength);
|
|
|
|
case State::FINISHED_LOCAL_COLOR_TABLE:
|
|
|
|
return FinishedLocalColorTable();
|
|
|
|
case State::IMAGE_DATA_BLOCK:
|
|
|
|
return ReadImageDataBlock(aData);
|
|
|
|
case State::IMAGE_DATA_SUB_BLOCK:
|
|
|
|
return ReadImageDataSubBlock(aData);
|
|
|
|
case State::LZW_DATA:
|
|
|
|
return ReadLZWData(aData, aLength);
|
|
|
|
case State::SKIP_LZW_DATA:
|
|
|
|
return Transition::ContinueUnbuffered(State::SKIP_LZW_DATA);
|
|
|
|
case State::FINISHED_LZW_DATA:
|
|
|
|
return Transition::To(State::IMAGE_DATA_SUB_BLOCK,
|
|
|
|
SUB_BLOCK_HEADER_LEN);
|
2024-07-17 14:39:13 +03:00
|
|
|
case State::FINISH_END_IMAGE_FRAME:
|
|
|
|
return Transition::To(State::BLOCK_HEADER, BLOCK_HEADER_LEN);
|
2016-07-11 10:07:07 +03:00
|
|
|
case State::SKIP_SUB_BLOCKS:
|
|
|
|
return SkipSubBlocks(aData);
|
|
|
|
case State::SKIP_DATA_THEN_SKIP_SUB_BLOCKS:
|
|
|
|
return Transition::ContinueUnbuffered(
|
|
|
|
State::SKIP_DATA_THEN_SKIP_SUB_BLOCKS);
|
|
|
|
case State::FINISHED_SKIPPING_DATA:
|
|
|
|
return Transition::To(State::SKIP_SUB_BLOCKS, SUB_BLOCK_HEADER_LEN);
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown State");
|
|
|
|
}
|
|
|
|
});
|
2016-06-08 22:17:17 +03:00
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadGIFHeader(
|
|
|
|
const char* aData) {
|
|
|
|
// We retrieve the version here but because many GIF encoders set header
|
|
|
|
// fields incorrectly, we barely use it; features which should only appear in
|
|
|
|
// GIF89a are always accepted.
|
|
|
|
if (strncmp(aData, "GIF87a", GIF_HEADER_LEN) == 0) {
|
|
|
|
mGIFStruct.version = 87;
|
|
|
|
} else if (strncmp(aData, "GIF89a", GIF_HEADER_LEN) == 0) {
|
|
|
|
mGIFStruct.version = 89;
|
|
|
|
} else {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
2007-08-26 09:08:53 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
return Transition::To(State::SCREEN_DESCRIPTOR, GIF_SCREEN_DESCRIPTOR_LEN);
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadScreenDescriptor(
|
|
|
|
const char* aData) {
|
|
|
|
mGIFStruct.screen_width = LittleEndian::readUint16(aData + 0);
|
|
|
|
mGIFStruct.screen_height = LittleEndian::readUint16(aData + 2);
|
|
|
|
|
|
|
|
const uint8_t packedFields = aData[4];
|
|
|
|
|
|
|
|
// XXX: Should we be capturing these values even if there is no global color
|
|
|
|
// table?
|
|
|
|
mGIFStruct.global_colormap_depth =
|
|
|
|
(packedFields & PACKED_FIELDS_TABLE_DEPTH_MASK) + 1;
|
|
|
|
mGIFStruct.global_colormap_count = 1 << mGIFStruct.global_colormap_depth;
|
|
|
|
|
|
|
|
// We ignore several fields in the header. We don't care about the 'sort
|
|
|
|
// flag', which indicates if the global color table's entries are sorted in
|
|
|
|
// order of importance - if we need to render this image for a device with a
|
|
|
|
// narrower color gamut than GIF supports we'll handle that at a different
|
|
|
|
// layer. We have no use for the pixel aspect ratio as well. Finally, we
|
|
|
|
// intentionally ignore the background color index, as implementing that
|
|
|
|
// feature would not be web compatible - when a GIF image frame doesn't cover
|
|
|
|
// the entire area of the image, the area that's not covered should always be
|
|
|
|
// transparent.
|
|
|
|
|
|
|
|
if (packedFields & PACKED_FIELDS_COLOR_TABLE_BIT) {
|
|
|
|
MOZ_ASSERT(mColorTablePos == 0);
|
|
|
|
|
|
|
|
// We read the global color table in unbuffered mode since it can be quite
|
|
|
|
// large and it'd be preferable to avoid unnecessary copies.
|
|
|
|
const size_t globalColorTableSize = 3 * mGIFStruct.global_colormap_count;
|
|
|
|
return Transition::ToUnbuffered(State::FINISHED_GLOBAL_COLOR_TABLE,
|
|
|
|
State::GLOBAL_COLOR_TABLE,
|
|
|
|
globalColorTableSize);
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
return Transition::To(State::BLOCK_HEADER, BLOCK_HEADER_LEN);
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadGlobalColorTable(
|
|
|
|
const char* aData, size_t aLength) {
|
|
|
|
uint8_t* dest =
|
|
|
|
reinterpret_cast<uint8_t*>(mGIFStruct.global_colormap) + mColorTablePos;
|
|
|
|
memcpy(dest, aData, aLength);
|
|
|
|
mColorTablePos += aLength;
|
|
|
|
return Transition::ContinueUnbuffered(State::GLOBAL_COLOR_TABLE);
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State>
|
|
|
|
nsGIFDecoder2::FinishedGlobalColorTable() {
|
|
|
|
ConvertColormap(mGIFStruct.global_colormap, mGIFStruct.global_colormap_count);
|
|
|
|
mColorTablePos = 0;
|
|
|
|
return Transition::To(State::BLOCK_HEADER, BLOCK_HEADER_LEN);
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadBlockHeader(
|
|
|
|
const char* aData) {
|
|
|
|
// Determine what type of block we're dealing with.
|
|
|
|
switch (aData[0]) {
|
|
|
|
case GIF_EXTENSION_INTRODUCER:
|
|
|
|
return Transition::To(State::EXTENSION_HEADER, EXTENSION_HEADER_LEN);
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
case GIF_IMAGE_SEPARATOR:
|
|
|
|
return Transition::To(State::IMAGE_DESCRIPTOR, IMAGE_DESCRIPTOR_LEN);
|
2014-11-25 10:42:43 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
case GIF_TRAILER:
|
|
|
|
FinishInternal();
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
|
|
|
|
default:
|
|
|
|
// If we get anything other than GIF_IMAGE_SEPARATOR,
|
|
|
|
// GIF_EXTENSION_INTRODUCER, or GIF_TRAILER, there is extraneous data
|
|
|
|
// between blocks. The GIF87a spec tells us to keep reading until we find
|
|
|
|
// an image separator, but GIF89a says such a file is corrupt. We follow
|
|
|
|
// GIF89a and bail out.
|
|
|
|
|
|
|
|
if (mGIFStruct.images_decoded > 0) {
|
|
|
|
// The file is corrupt, but we successfully decoded some frames, so we
|
|
|
|
// may as well consider the decode successful and display them.
|
|
|
|
FinishInternal();
|
|
|
|
return Transition::TerminateSuccess();
|
2014-11-25 10:42:43 +03:00
|
|
|
}
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// No images decoded; there is nothing to display.
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 10:37:13 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadExtensionHeader(
|
|
|
|
const char* aData) {
|
|
|
|
const uint8_t label = aData[0];
|
|
|
|
const uint8_t extensionHeaderLength = aData[1];
|
2015-08-14 10:37:13 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// If the extension header is zero length, just treat it as a block terminator
|
|
|
|
// and move on to the next block immediately.
|
|
|
|
if (extensionHeaderLength == 0) {
|
|
|
|
return Transition::To(State::BLOCK_HEADER, BLOCK_HEADER_LEN);
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
switch (label) {
|
|
|
|
case GIF_GRAPHIC_CONTROL_LABEL:
|
|
|
|
// The GIF spec mandates that the Control Extension header block length is
|
|
|
|
// 4 bytes, and the parser for this block reads 4 bytes, so we must
|
|
|
|
// enforce that the buffer contains at least this many bytes. If the GIF
|
|
|
|
// specifies a different length, we allow that, so long as it's larger;
|
|
|
|
// the additional data will simply be ignored.
|
|
|
|
return Transition::To(
|
|
|
|
State::GRAPHIC_CONTROL_EXTENSION,
|
|
|
|
max<uint8_t>(extensionHeaderLength, GRAPHIC_CONTROL_EXTENSION_LEN));
|
|
|
|
|
|
|
|
case GIF_APPLICATION_EXTENSION_LABEL:
|
|
|
|
// Again, the spec specifies that an application extension header is 11
|
|
|
|
// bytes, but for compatibility with GIFs in the wild, we allow deviation
|
|
|
|
// from the spec. This is important for real-world compatibility, as GIFs
|
|
|
|
// in the wild exist with application extension headers that are both
|
|
|
|
// shorter and longer than 11 bytes. However, we only try to actually
|
|
|
|
// interpret the application extension if the length is correct;
|
|
|
|
// otherwise, we just skip the block unconditionally.
|
|
|
|
return extensionHeaderLength == APPLICATION_EXTENSION_LEN
|
|
|
|
? Transition::To(State::APPLICATION_IDENTIFIER,
|
|
|
|
extensionHeaderLength)
|
|
|
|
: Transition::ToUnbuffered(
|
|
|
|
State::FINISHED_SKIPPING_DATA,
|
|
|
|
State::SKIP_DATA_THEN_SKIP_SUB_BLOCKS,
|
|
|
|
extensionHeaderLength);
|
2015-09-18 01:03:34 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
default:
|
|
|
|
// Skip over any other type of extension block, including comment and
|
|
|
|
// plain text blocks.
|
|
|
|
return Transition::ToUnbuffered(State::FINISHED_SKIPPING_DATA,
|
|
|
|
State::SKIP_DATA_THEN_SKIP_SUB_BLOCKS,
|
|
|
|
extensionHeaderLength);
|
|
|
|
}
|
|
|
|
}
|
2015-09-18 01:03:34 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State>
|
|
|
|
nsGIFDecoder2::ReadGraphicControlExtension(const char* aData) {
|
|
|
|
mGIFStruct.is_transparent = aData[0] & 0x1;
|
|
|
|
mGIFStruct.tpixel = uint8_t(aData[3]);
|
|
|
|
mGIFStruct.disposal_method = (aData[0] >> 2) & 0x7;
|
|
|
|
|
|
|
|
if (mGIFStruct.disposal_method == 4) {
|
|
|
|
// Some encoders (and apparently some specs) represent
|
|
|
|
// DisposalMethod::RESTORE_PREVIOUS as 4, but 3 is used in the canonical
|
|
|
|
// spec and is more popular, so we normalize to 3.
|
|
|
|
mGIFStruct.disposal_method = 3;
|
|
|
|
} else if (mGIFStruct.disposal_method > 4) {
|
|
|
|
// This GIF is using a disposal method which is undefined in the spec.
|
|
|
|
// Treat it as DisposalMethod::NOT_SPECIFIED.
|
|
|
|
mGIFStruct.disposal_method = 0;
|
|
|
|
}
|
2015-08-01 04:10:23 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
DisposalMethod method = DisposalMethod(mGIFStruct.disposal_method);
|
|
|
|
if (method == DisposalMethod::CLEAR_ALL || method == DisposalMethod::CLEAR) {
|
|
|
|
// We may have to display the background under this image during animation
|
|
|
|
// playback, so we regard it as transparent.
|
|
|
|
PostHasTransparency();
|
|
|
|
}
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
mGIFStruct.delay_time = LittleEndian::readUint16(aData + 1) * 10;
|
2019-12-10 08:30:54 +03:00
|
|
|
if (!HasAnimation() && mGIFStruct.delay_time > 0) {
|
2016-07-20 02:22:34 +03:00
|
|
|
PostIsAnimated(FrameTimeout::FromRawMilliseconds(mGIFStruct.delay_time));
|
2016-06-08 22:17:17 +03:00
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
return Transition::To(State::SKIP_SUB_BLOCKS, SUB_BLOCK_HEADER_LEN);
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadApplicationIdentifier(
|
|
|
|
const char* aData) {
|
|
|
|
if ((strncmp(aData, "NETSCAPE2.0", 11) == 0) ||
|
|
|
|
(strncmp(aData, "ANIMEXTS1.0", 11) == 0)) {
|
|
|
|
// This is a Netscape application extension block.
|
|
|
|
return Transition::To(State::NETSCAPE_EXTENSION_SUB_BLOCK,
|
|
|
|
SUB_BLOCK_HEADER_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is an application extension we don't care about. Just skip it.
|
|
|
|
return Transition::To(State::SKIP_SUB_BLOCKS, SUB_BLOCK_HEADER_LEN);
|
|
|
|
}
|
2016-03-10 02:39:02 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State>
|
|
|
|
nsGIFDecoder2::ReadNetscapeExtensionSubBlock(const char* aData) {
|
|
|
|
const uint8_t blockLength = aData[0];
|
|
|
|
if (blockLength == 0) {
|
|
|
|
// We hit the block terminator.
|
|
|
|
return Transition::To(State::BLOCK_HEADER, BLOCK_HEADER_LEN);
|
|
|
|
}
|
2015-07-11 05:26:15 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// We consume a minimum of 3 bytes in accordance with the specs for the
|
|
|
|
// Netscape application extension block, such as they are.
|
|
|
|
const size_t extensionLength = max<uint8_t>(blockLength, 3);
|
|
|
|
return Transition::To(State::NETSCAPE_EXTENSION_DATA, extensionLength);
|
|
|
|
}
|
2016-03-10 02:39:02 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadNetscapeExtensionData(
|
|
|
|
const char* aData) {
|
|
|
|
// Documentation for NETSCAPE2.0 / ANIMEXTS1.0 extensions can be found at:
|
|
|
|
// https://wiki.whatwg.org/wiki/GIF
|
|
|
|
static const uint8_t NETSCAPE_LOOPING_EXTENSION_SUB_BLOCK_ID = 1;
|
|
|
|
static const uint8_t NETSCAPE_BUFFERING_EXTENSION_SUB_BLOCK_ID = 2;
|
|
|
|
|
|
|
|
const uint8_t subBlockID = aData[0] & 7;
|
|
|
|
switch (subBlockID) {
|
|
|
|
case NETSCAPE_LOOPING_EXTENSION_SUB_BLOCK_ID:
|
|
|
|
// This is looping extension.
|
|
|
|
mGIFStruct.loop_count = LittleEndian::readUint16(aData + 1);
|
2017-05-12 23:29:12 +03:00
|
|
|
// Zero loop count is infinite animation loop request.
|
|
|
|
if (mGIFStruct.loop_count == 0) {
|
|
|
|
mGIFStruct.loop_count = -1;
|
|
|
|
}
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
return Transition::To(State::NETSCAPE_EXTENSION_SUB_BLOCK,
|
|
|
|
SUB_BLOCK_HEADER_LEN);
|
|
|
|
|
|
|
|
case NETSCAPE_BUFFERING_EXTENSION_SUB_BLOCK_ID:
|
|
|
|
// We allow, but ignore, this extension.
|
|
|
|
return Transition::To(State::NETSCAPE_EXTENSION_SUB_BLOCK,
|
|
|
|
SUB_BLOCK_HEADER_LEN);
|
2013-01-28 21:27:35 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
default:
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
}
|
2013-01-28 21:27:35 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadImageDescriptor(
|
|
|
|
const char* aData) {
|
2016-07-30 02:44:16 +03:00
|
|
|
// On the first frame, we don't need to yield, and none of the other checks
|
|
|
|
// below apply, so we can just jump right into FinishImageDescriptor().
|
|
|
|
if (mGIFStruct.images_decoded == 0) {
|
|
|
|
return FinishImageDescriptor(aData);
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-07-30 02:44:16 +03:00
|
|
|
if (!HasAnimation()) {
|
|
|
|
// We should've already called PostIsAnimated(); this must be a corrupt
|
|
|
|
// animated image with a first frame timeout of zero. Signal that we're
|
|
|
|
// animated now, before the first-frame decode early exit below, so that
|
|
|
|
// RasterImage can detect that this happened.
|
|
|
|
PostIsAnimated(FrameTimeout::FromRawMilliseconds(0));
|
|
|
|
}
|
2007-06-25 20:41:21 +04:00
|
|
|
|
2016-07-30 02:44:16 +03:00
|
|
|
if (IsFirstFrameDecode()) {
|
|
|
|
// We're about to get a second frame, but we only want the first. Stop
|
|
|
|
// decoding now.
|
|
|
|
FinishInternal();
|
|
|
|
return Transition::TerminateSuccess();
|
2016-06-08 22:17:17 +03:00
|
|
|
}
|
2007-10-24 10:01:16 +04:00
|
|
|
|
2016-08-05 14:21:40 +03:00
|
|
|
MOZ_ASSERT(Size() == OutputSize(), "Downscaling an animated image?");
|
2024-07-17 14:39:13 +03:00
|
|
|
return FinishImageDescriptor(aData);
|
2016-07-30 02:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::FinishImageDescriptor(
|
|
|
|
const char* aData) {
|
2021-10-06 17:41:17 +03:00
|
|
|
OrientedIntRect frameRect;
|
2016-06-08 22:17:17 +03:00
|
|
|
|
|
|
|
// Get image offsets with respect to the screen origin.
|
2017-12-21 00:46:28 +03:00
|
|
|
frameRect.SetRect(
|
|
|
|
LittleEndian::readUint16(aData + 0), LittleEndian::readUint16(aData + 2),
|
|
|
|
LittleEndian::readUint16(aData + 4), LittleEndian::readUint16(aData + 6));
|
2016-06-08 22:17:17 +03:00
|
|
|
|
|
|
|
if (!mGIFStruct.images_decoded) {
|
|
|
|
// Work around GIF files where
|
|
|
|
// * at least one of the logical screen dimensions is smaller than the
|
|
|
|
// same dimension in the first image, or
|
|
|
|
// * GIF87a files where the first image's dimensions do not match the
|
|
|
|
// logical screen dimensions.
|
2017-08-14 15:29:56 +03:00
|
|
|
if (mGIFStruct.screen_height < frameRect.Height() ||
|
|
|
|
mGIFStruct.screen_width < frameRect.Width() ||
|
2016-06-08 22:17:17 +03:00
|
|
|
mGIFStruct.version == 87) {
|
2017-08-14 15:29:56 +03:00
|
|
|
mGIFStruct.screen_height = frameRect.Height();
|
|
|
|
mGIFStruct.screen_width = frameRect.Width();
|
2016-06-08 22:17:17 +03:00
|
|
|
frameRect.MoveTo(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the image container with the right size.
|
|
|
|
BeginGIF();
|
|
|
|
if (HasError()) {
|
|
|
|
// Setting the size led to an error.
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
2024-06-25 01:44:30 +03:00
|
|
|
// If we're doing a metadata decode without the frame count, we're done.
|
|
|
|
if (IsMetadataDecode() && !WantsFrameCount()) {
|
2016-06-08 22:17:17 +03:00
|
|
|
CheckForTransparency(frameRect);
|
|
|
|
FinishInternal();
|
|
|
|
return Transition::TerminateSuccess();
|
2007-06-25 20:41:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// Work around broken GIF files that have zero frame width or height; in this
|
|
|
|
// case, we'll treat the frame as having the same size as the overall image.
|
2017-08-14 15:29:56 +03:00
|
|
|
if (frameRect.Height() == 0 || frameRect.Width() == 0) {
|
|
|
|
frameRect.SetHeight(mGIFStruct.screen_height);
|
|
|
|
frameRect.SetWidth(mGIFStruct.screen_width);
|
2016-06-08 22:17:17 +03:00
|
|
|
|
|
|
|
// If that still resulted in zero frame width or height, give up.
|
2017-08-14 15:29:56 +03:00
|
|
|
if (frameRect.Height() == 0 || frameRect.Width() == 0) {
|
2016-06-08 22:17:17 +03:00
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
2007-10-24 10:01:16 +04:00
|
|
|
}
|
2013-01-28 21:27:35 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// Determine |depth| (log base 2 of the number of colors in the palette).
|
|
|
|
bool haveLocalColorTable = false;
|
|
|
|
uint16_t depth = 0;
|
|
|
|
uint8_t packedFields = aData[8];
|
|
|
|
|
|
|
|
if (packedFields & PACKED_FIELDS_COLOR_TABLE_BIT) {
|
|
|
|
// Get the palette depth from the local color table.
|
|
|
|
depth = (packedFields & PACKED_FIELDS_TABLE_DEPTH_MASK) + 1;
|
|
|
|
haveLocalColorTable = true;
|
|
|
|
} else {
|
|
|
|
// Get the palette depth from the global color table.
|
|
|
|
depth = mGIFStruct.global_colormap_depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the transparent color index is greater than the number of colors in the
|
|
|
|
// color table, we may need a higher color depth than |depth| would specify.
|
|
|
|
// Our internal representation of the image will instead use |realDepth|,
|
2019-09-12 12:00:52 +03:00
|
|
|
// which is the smallest color depth that can accommodate the existing palette
|
2016-06-08 22:17:17 +03:00
|
|
|
// *and* the transparent color index.
|
|
|
|
uint16_t realDepth = depth;
|
|
|
|
while (mGIFStruct.tpixel >= (1 << realDepth) && realDepth < 8) {
|
|
|
|
realDepth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a mask used to ensure that color values fit within the colormap.
|
|
|
|
mColorMask = 0xFF >> (8 - realDepth);
|
|
|
|
|
|
|
|
// Determine if this frame is interlaced or not.
|
|
|
|
const bool isInterlaced = packedFields & PACKED_FIELDS_INTERLACED_BIT;
|
|
|
|
|
|
|
|
// Create the SurfacePipe we'll use to write output for this frame.
|
|
|
|
if (NS_FAILED(BeginImageFrame(frameRect, realDepth, isInterlaced))) {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear state from last image.
|
2018-02-08 02:00:06 +03:00
|
|
|
mGIFStruct.pixels_remaining =
|
|
|
|
int64_t(frameRect.Width()) * int64_t(frameRect.Height());
|
2016-06-08 22:17:17 +03:00
|
|
|
|
|
|
|
if (haveLocalColorTable) {
|
|
|
|
// We have a local color table, so prepare to read it into the palette of
|
|
|
|
// the current frame.
|
|
|
|
mGIFStruct.local_colormap_size = 1 << depth;
|
|
|
|
|
2018-09-17 22:06:28 +03:00
|
|
|
if (!mColormap) {
|
|
|
|
// Ensure our current colormap buffer is large enough to hold the new one.
|
2016-06-08 22:17:17 +03:00
|
|
|
mColormapSize = sizeof(uint32_t) << realDepth;
|
2018-09-17 22:06:28 +03:00
|
|
|
if (mGIFStruct.local_colormap_buffer_size < mColormapSize) {
|
|
|
|
if (mGIFStruct.local_colormap) {
|
|
|
|
free(mGIFStruct.local_colormap);
|
|
|
|
}
|
|
|
|
mGIFStruct.local_colormap_buffer_size = mColormapSize;
|
2016-06-08 22:17:17 +03:00
|
|
|
mGIFStruct.local_colormap =
|
|
|
|
static_cast<uint32_t*>(moz_xmalloc(mColormapSize));
|
2022-03-18 18:22:46 +03:00
|
|
|
// Ensure the local colormap is initialized as opaque.
|
|
|
|
memset(mGIFStruct.local_colormap, 0xFF, mColormapSize);
|
2018-09-17 22:06:28 +03:00
|
|
|
} else {
|
|
|
|
mColormapSize = mGIFStruct.local_colormap_buffer_size;
|
2013-01-28 21:27:35 +04:00
|
|
|
}
|
2018-09-17 22:06:28 +03:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
mColormap = mGIFStruct.local_colormap;
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:06:28 +03:00
|
|
|
MOZ_ASSERT(mColormap);
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
const size_t size = 3 << depth;
|
|
|
|
if (mColormapSize > size) {
|
|
|
|
// Clear the part of the colormap which will be unused with this palette.
|
2016-07-01 04:59:29 +03:00
|
|
|
// If a GIF references an invalid palette entry, ensure the entry is
|
|
|
|
// opaque white. This is needed for Skia as if it isn't, RGBX surfaces
|
|
|
|
// will cause blending issues with Skia.
|
|
|
|
memset(reinterpret_cast<uint8_t*>(mColormap) + size, 0xFF,
|
2016-06-08 22:17:17 +03:00
|
|
|
mColormapSize - size);
|
2013-01-28 21:27:35 +04:00
|
|
|
}
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
MOZ_ASSERT(mColorTablePos == 0);
|
|
|
|
|
|
|
|
// We read the local color table in unbuffered mode since it can be quite
|
|
|
|
// large and it'd be preferable to avoid unnecessary copies.
|
|
|
|
return Transition::ToUnbuffered(State::FINISHED_LOCAL_COLOR_TABLE,
|
|
|
|
State::LOCAL_COLOR_TABLE, size);
|
2007-06-25 20:41:21 +04:00
|
|
|
}
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// There's no local color table; copy the global color table into the palette
|
|
|
|
// of the current frame.
|
2018-09-17 22:06:28 +03:00
|
|
|
if (mColormap) {
|
2016-06-08 22:17:17 +03:00
|
|
|
memcpy(mColormap, mGIFStruct.global_colormap, mColormapSize);
|
|
|
|
} else {
|
|
|
|
mColormap = mGIFStruct.global_colormap;
|
2010-09-12 19:22:30 +04:00
|
|
|
}
|
2016-06-08 22:17:17 +03:00
|
|
|
|
|
|
|
return Transition::To(State::IMAGE_DATA_BLOCK, BLOCK_HEADER_LEN);
|
2001-02-21 01:45:10 +03:00
|
|
|
}
|
2010-08-23 06:30:46 +04:00
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadLocalColorTable(
|
|
|
|
const char* aData, size_t aLength) {
|
2024-06-25 01:44:30 +03:00
|
|
|
// If we are just counting frames for a metadata decode, there is no need to
|
|
|
|
// prep the colormap.
|
|
|
|
if (!WantsFrameCount()) {
|
|
|
|
uint8_t* dest = reinterpret_cast<uint8_t*>(mColormap) + mColorTablePos;
|
|
|
|
memcpy(dest, aData, aLength);
|
|
|
|
mColorTablePos += aLength;
|
|
|
|
}
|
2016-06-08 22:17:17 +03:00
|
|
|
return Transition::ContinueUnbuffered(State::LOCAL_COLOR_TABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::FinishedLocalColorTable() {
|
|
|
|
ConvertColormap(mColormap, mGIFStruct.local_colormap_size);
|
|
|
|
mColorTablePos = 0;
|
|
|
|
return Transition::To(State::IMAGE_DATA_BLOCK, BLOCK_HEADER_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadImageDataBlock(
|
|
|
|
const char* aData) {
|
|
|
|
// Make sure the transparent pixel is transparent in the colormap.
|
|
|
|
if (mGIFStruct.is_transparent) {
|
|
|
|
// Save the old value so we can restore it later.
|
|
|
|
if (mColormap == mGIFStruct.global_colormap) {
|
|
|
|
mOldColor = mColormap[mGIFStruct.tpixel];
|
|
|
|
}
|
|
|
|
mColormap[mGIFStruct.tpixel] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the LZW decoder.
|
|
|
|
mGIFStruct.datasize = uint8_t(aData[0]);
|
2017-11-23 09:40:17 +03:00
|
|
|
if (mGIFStruct.datasize > MAX_LZW_BITS) {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
2016-06-08 22:17:17 +03:00
|
|
|
const int clearCode = ClearCode();
|
2017-11-23 09:40:17 +03:00
|
|
|
if (clearCode >= MAX_BITS) {
|
2016-06-08 22:17:17 +03:00
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
mGIFStruct.avail = clearCode + 2;
|
|
|
|
mGIFStruct.oldcode = -1;
|
|
|
|
mGIFStruct.codesize = mGIFStruct.datasize + 1;
|
|
|
|
mGIFStruct.codemask = (1 << mGIFStruct.codesize) - 1;
|
|
|
|
mGIFStruct.datum = mGIFStruct.bits = 0;
|
|
|
|
|
|
|
|
// Initialize the tables.
|
|
|
|
for (int i = 0; i < clearCode; i++) {
|
|
|
|
mGIFStruct.suffix[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
mGIFStruct.stackp = mGIFStruct.stack;
|
|
|
|
|
|
|
|
// Begin reading image data sub-blocks.
|
|
|
|
return Transition::To(State::IMAGE_DATA_SUB_BLOCK, SUB_BLOCK_HEADER_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadImageDataSubBlock(
|
|
|
|
const char* aData) {
|
|
|
|
const uint8_t subBlockLength = aData[0];
|
|
|
|
if (subBlockLength == 0) {
|
2024-07-17 14:39:13 +03:00
|
|
|
// We hit the block terminator. Yield if we are decoding multiple frames.
|
2016-06-08 22:17:17 +03:00
|
|
|
EndImageFrame();
|
2024-07-17 14:39:13 +03:00
|
|
|
if (IsFirstFrameDecode()) {
|
|
|
|
return Transition::To(State::BLOCK_HEADER, BLOCK_HEADER_LEN);
|
|
|
|
}
|
|
|
|
return Transition::ToAfterYield(State::FINISH_END_IMAGE_FRAME);
|
2016-06-08 22:17:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mGIFStruct.pixels_remaining == 0) {
|
2016-06-21 06:42:31 +03:00
|
|
|
// We've already written to the entire image; we should've hit the block
|
|
|
|
// terminator at this point. This image is corrupt, but we'll tolerate it.
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
if (subBlockLength == GIF_TRAILER) {
|
|
|
|
// This GIF is missing the block terminator for the final block; we'll put
|
|
|
|
// up with it.
|
|
|
|
FinishInternal();
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
}
|
|
|
|
|
2016-06-21 06:42:31 +03:00
|
|
|
// We're not at the end of the image, so just skip the extra data.
|
|
|
|
return Transition::ToUnbuffered(State::FINISHED_LZW_DATA,
|
|
|
|
State::SKIP_LZW_DATA, subBlockLength);
|
2013-01-28 21:27:35 +04:00
|
|
|
}
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// Handle the standard case: there's data in the sub-block and pixels left to
|
|
|
|
// fill in the image. We read the sub-block unbuffered so we can get pixels on
|
|
|
|
// the screen as soon as possible.
|
|
|
|
return Transition::ToUnbuffered(State::FINISHED_LZW_DATA, State::LZW_DATA,
|
|
|
|
subBlockLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::ReadLZWData(
|
|
|
|
const char* aData, size_t aLength) {
|
2024-06-25 01:44:30 +03:00
|
|
|
// If we are just counting frames for a metadata decode, there is no need to
|
|
|
|
// do the actual decode.
|
|
|
|
if (WantsFrameCount()) {
|
|
|
|
return Transition::ContinueUnbuffered(State::LZW_DATA);
|
|
|
|
}
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
const uint8_t* data = reinterpret_cast<const uint8_t*>(aData);
|
|
|
|
size_t length = aLength;
|
|
|
|
|
2016-09-19 16:16:28 +03:00
|
|
|
while (mGIFStruct.pixels_remaining > 0 &&
|
|
|
|
(length > 0 || mGIFStruct.bits >= mGIFStruct.codesize)) {
|
2016-06-08 22:17:17 +03:00
|
|
|
size_t bytesRead = 0;
|
|
|
|
|
2019-03-15 20:29:02 +03:00
|
|
|
auto result = mPipe.WritePixelBlocks<uint32_t>(
|
|
|
|
[&](uint32_t* aPixelBlock, int32_t aBlockSize) {
|
|
|
|
return YieldPixels<uint32_t>(data, length, &bytesRead, aPixelBlock,
|
|
|
|
aBlockSize);
|
|
|
|
});
|
2016-06-08 22:17:17 +03:00
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(bytesRead > length)) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Overread?");
|
|
|
|
bytesRead = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance our position in the input based upon what YieldPixel() consumed.
|
|
|
|
data += bytesRead;
|
|
|
|
length -= bytesRead;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case WriteState::NEED_MORE_DATA:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case WriteState::FINISHED:
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(mGIFStruct.pixels_remaining <= 0,
|
|
|
|
"too many pixels");
|
2016-06-08 22:17:17 +03:00
|
|
|
mGIFStruct.pixels_remaining = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WriteState::FAILURE:
|
2024-03-01 14:42:43 +03:00
|
|
|
if (mGIFStruct.images_decoded > 0) {
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
}
|
2016-06-08 22:17:17 +03:00
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're done, but keep going until we consume all the data in the sub-block.
|
|
|
|
return Transition::ContinueUnbuffered(State::LZW_DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsGIFDecoder2::State> nsGIFDecoder2::SkipSubBlocks(
|
|
|
|
const char* aData) {
|
|
|
|
// In the SKIP_SUB_BLOCKS state we skip over data sub-blocks that we're not
|
|
|
|
// interested in. Blocks consist of a block header (which can be up to 255
|
|
|
|
// bytes in length) and a series of data sub-blocks. Each data sub-block
|
|
|
|
// consists of a single byte length value, followed by the data itself. A data
|
|
|
|
// sub-block with a length of zero terminates the overall block.
|
|
|
|
// SKIP_SUB_BLOCKS reads a sub-block length value. If it's zero, we've arrived
|
|
|
|
// at the next block. Otherwise, we enter the SKIP_DATA_THEN_SKIP_SUB_BLOCKS
|
|
|
|
// state to skip over the sub-block data and return to SKIP_SUB_BLOCKS at the
|
|
|
|
// start of the next sub-block.
|
|
|
|
|
|
|
|
const uint8_t nextSubBlockLength = aData[0];
|
|
|
|
if (nextSubBlockLength == 0) {
|
|
|
|
// We hit the block terminator, so the sequence of data sub-blocks is over;
|
|
|
|
// begin processing another block.
|
|
|
|
return Transition::To(State::BLOCK_HEADER, BLOCK_HEADER_LEN);
|
2013-01-28 21:27:35 +04:00
|
|
|
}
|
|
|
|
|
2016-06-08 22:17:17 +03:00
|
|
|
// Skip to the next sub-block length value.
|
|
|
|
return Transition::ToUnbuffered(State::FINISHED_SKIPPING_DATA,
|
|
|
|
State::SKIP_DATA_THEN_SKIP_SUB_BLOCKS,
|
|
|
|
nextSubBlockLength);
|
2013-01-28 21:27:35 +04:00
|
|
|
}
|
|
|
|
|
2016-08-03 02:45:22 +03:00
|
|
|
Maybe<Telemetry::HistogramID> nsGIFDecoder2::SpeedHistogram() const {
|
|
|
|
return Some(Telemetry::IMAGE_DECODE_SPEED_GIF);
|
2011-09-23 00:25:56 +04:00
|
|
|
}
|
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-23 06:30:46 +04:00
|
|
|
} // namespace mozilla
|