Chromium Code Reviews| Index: src/codec/SkCodec_libgif.cpp |
| diff --git a/src/codec/SkCodec_libgif.cpp b/src/codec/SkCodec_libgif.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eadfbe87a1c384b22624ea2365dca48c3c6d5ab5 |
| --- /dev/null |
| +++ b/src/codec/SkCodec_libgif.cpp |
| @@ -0,0 +1,495 @@ |
| +/* |
| + * Copyright 2015 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkCodec_libgif.h" |
| +#include "SkCodecPriv.h" |
| +#include "SkColorPriv.h" |
| +#include "SkColorTable.h" |
| +#include "SkGifInterlaceIter.h" |
| +#include "SkStream.h" |
| +#include "SkSwizzler.h" |
| +#include "SkUtils.h" |
| + |
| +/* |
| + * Checks the start of the stream to see if the image is a gif |
| + */ |
| +bool SkGifCodec::IsGif(SkStream* stream) { |
| + char buf[GIF_STAMP_LEN]; |
| + if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) { |
| + if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 || |
| + memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || |
| + memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) { |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +/* |
| + * Error reporting function |
| + * TODO: Add option to turn off printing of error |
|
scroggo
2015/03/26 20:03:54
This is done by using SkCodecPrintf
msarett
2015/03/26 22:26:37
Done.
|
| + */ |
| + static void gif_message(const char* msg) { |
| + SkCodecPrintf("Gif Warning: %s\n", msg); |
|
scroggo
2015/03/26 20:03:53
We might want two different functions: one for err
msarett
2015/03/26 22:26:37
Agreed. This is better.
|
| + } |
| + |
| + /* |
| + * This function cleans up the gif object after the decode completes |
| + * It is used in a SkAutoTCallIProc template |
| + */ |
| +int32_t close_gif(GifFileType* gif) { |
| +#if GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0) |
| + return DGifCloseFile(gif); |
| +#else |
| + return DGifCloseFile(gif, NULL); |
| +#endif |
| +} |
| + |
| +/* |
| + * This function free extension data that has been saved to assist the image |
| + * decoder |
| + */ |
| +void free_extension(SavedImage* image) { |
| + if (NULL != image->ExtensionBlocks) { |
| +#if GIFLIB_MAJOR < 5 |
| + FreeExtension(image); |
| +#else |
| + GifFreeExtensions(&image->ExtensionBlockCount, &image->ExtensionBlocks); |
| +#endif |
| + } |
| +} |
| + |
| +/* |
| + * Read function that will be passed to gif_lib |
| + */ |
| +static int32_t read_bytes_callback(GifFileType* fileType, GifByteType* out, |
|
scroggo
2015/03/26 20:03:53
What does gif_lib expect? size_t seems most approp
msarett
2015/03/26 22:26:37
Agreed, but it expects an int return type.
|
| + int32_t size) { |
| + SkStream* stream = (SkStream*) fileType->UserData; |
| + return (int32_t) stream->read(out, size); |
| +} |
| + |
| +/* |
| + * Check if a there is an index of the color table for a transparent pixel |
| + */ |
| +static int32_t find_trans_index(const SavedImage& image, uint32_t colorCount) { |
| + // If there is a transparent index specified, it will be contained in an |
| + // extension block. |
| + for (int32_t i = 0; i < image.ExtensionBlockCount; i++) { |
| + // Get an extension block |
| + const ExtensionBlock& extBlock = image.ExtensionBlocks[i]; |
| + |
| + // Most of the extension blocks contained in gif images are useless. |
|
scroggo
2015/03/26 20:03:53
Really? Any idea why they are there? Or are they j
msarett
2015/03/26 22:26:37
They are really in fact useless. But I don't thin
|
| + // Specifically, we need to check for a graphics control extension, |
| + // which may contain transparency information. Also, note that a valid |
| + // graphics control extension is always four bytes. The fourth byte |
| + // is the transparent index (if it exists), so we need at least four |
| + // bytes. |
| + if (GRAPHICS_EXT_FUNC_CODE == extBlock.Function && |
| + extBlock.ByteCount >= 4) { |
| + |
| + // Check the "transparent color flag" which indicates whether a |
|
scroggo
2015/03/26 20:03:53
Is there a constant/macro for "transparent color f
msarett
2015/03/26 22:26:37
The library actually doesn't have one. It makes a
|
| + // transparent index exists. |
| + if (1 == (extBlock.Bytes[0] & 1)) { |
| + |
| + // Use unsigned int32_t to prevent sign extending |
| + uint32_t transIndex = extBlock.Bytes[3]; |
| + if (transIndex < colorCount) { |
| + return transIndex; |
| + } |
| + } |
| + } |
| + } |
| + |
| + // Flag indicating that a valid index was not found |
| + return -1; |
| +} |
| + |
| +/* |
| + * Assumes IsGif was called and returned true |
| + * Creates a gif decoder |
| + * Reads enough of the stream to determine the image format |
| + */ |
| +SkCodec* SkGifCodec::NewFromStream(SkStream* stream) { |
| + // Read gif header, logical screen descriptor, and global color table |
| +#if GIFLIB_MAJOR < 5 |
| + SkAutoTCallIProc<GifFileType, close_gif> autoCloseGif( |
|
scroggo
2015/03/26 20:03:53
Just like when we call an SkAutoTDeleteArray "buff
msarett
2015/03/26 22:26:37
Done.
|
| + DGifOpen(stream, read_bytes_callback)); |
| +#else |
| + SkAutoTCallIProc<GifFileType, close_gif> autoCloseGif( |
| + DGifOpen(stream, read_bytes_callback, NULL)); |
|
scroggo
2015/03/26 20:03:53
Can we factor out the two different versions like
msarett
2015/03/26 22:26:37
Done.
|
| +#endif |
| + if (NULL == (GifFileType*) autoCloseGif) { |
|
scroggo
2015/03/26 20:03:53
Do you need to explicitly cast here for it to work
msarett
2015/03/26 22:26:37
Apparently not :)
|
| + gif_message("DGifOpen failed.\n"); |
| + return NULL; |
| + } |
| + |
| + // Get fields from header |
| + const int32_t width = autoCloseGif->SWidth; |
| + const int32_t height = autoCloseGif->SHeight; |
| + if (width <= 0 || height <= 0) { |
| + gif_message("Invalid dimensions.\n"); |
| + return NULL; |
| + } |
| + |
| + // Return the codec |
| + // kIndex is the most natural color type for gifs, so we set this as |
| + // the default. |
| + // Many gifs specify a color table index for transparent pixels. Every |
| + // other pixel is guaranteed to be opaque. Despite this, because of the |
| + // possiblity of transparent pixels, we cannot assume that the image is |
| + // opaque. However, we can at least mark it as kPremul, since pixels will |
| + // either have a 0xFF alpha component or be completely zeroed. |
| + const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, |
| + kIndex_8_SkColorType, kPremul_SkAlphaType); |
| + return SkNEW_ARGS(SkGifCodec, (imageInfo, stream, autoCloseGif.detach())); |
| +} |
| + |
| +SkGifCodec::SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, |
| + GifFileType* gif) |
| + : INHERITED(srcInfo, stream) |
| + , fGif(gif) |
| +{} |
| + |
| +/* |
| + * Checks if the conversion between the input image and the requested output |
| + * image has been implemented |
| + */ |
| +static bool conversion_possible(const SkImageInfo& dst, |
| + const SkImageInfo& src) { |
| + // Ensure that the profile type is unchanged |
| + if (dst.profileType() != src.profileType()) { |
| + return false; |
| + } |
| + |
| + // Check for supported color and alpha types |
| + switch (dst.colorType()) { |
| + case kN32_SkColorType: |
| + return src.alphaType() == dst.alphaType() || |
| + (kPremul_SkAlphaType == dst.alphaType() && |
| + kUnpremul_SkAlphaType == src.alphaType()); |
| + default: |
| + return false; |
| + } |
| +} |
| + |
| +/* |
| + * Initiates the gif decode |
| + */ |
| +SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo, |
| + void* dst, size_t dstRowBytes, |
| + const Options& opts, SkPMColor*, int*) { |
| + // Use auto call procedures to ensure memory is cleaned up |
| + SkAutoTCallIProc<GifFileType, close_gif> autoClose(fGif); |
| + SavedImage saveExt; |
| + SkAutoTCallVProc<SavedImage, free_extension> autoFreeExt(&saveExt); |
| + |
| + // Check for valid input parameters |
| + if (!this->rewindIfNeeded()) { |
| + return kCouldNotRewind; |
| + } |
| + if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| + gif_message("Scaling not supported.\n"); |
| + return kInvalidScale; |
| + } |
| + if (!conversion_possible(dstInfo, this->getInfo())) { |
| + gif_message("Cannot convert input type to output type.\n"); |
| + return kInvalidConversion; |
| + } |
| + |
| + // Use this as a container to hold information about any gif extension |
| + // blocks. This generally stores transparency and animation instructions. |
| + saveExt.ExtensionBlocks = NULL; |
| + saveExt.ExtensionBlockCount = 0; |
| + GifByteType* extData; |
| +#if GIFLIB_MAJOR >= 5 |
| + int32_t extFunction; |
| +#endif |
| + |
| + // We will loop over components of gif images until we find an image. Once |
| + // we find an image, we will decode and return it. While many gif files |
| + // contain more than one image, we will simply decode the first image. |
| + const int32_t width = dstInfo.width(); |
| + const int32_t height = dstInfo.height(); |
| + GifRecordType recordType = UNDEFINED_RECORD_TYPE; |
| + while (TERMINATE_RECORD_TYPE != recordType) { |
| + // Get the current record type |
| + if (GIF_ERROR == DGifGetRecordType(fGif, &recordType)) { |
| + gif_message("DGifGetRecordType failed.\n"); |
| + return kInvalidInput; |
| + } |
| + |
| + switch (recordType) { |
| + case IMAGE_DESC_RECORD_TYPE: { |
| + // Read the image descriptor |
| + if (GIF_ERROR == DGifGetImageDesc(fGif)) { |
| + gif_message("DGifGetImageDesc failed.\n"); |
| + return kInvalidInput; |
| + } |
| + |
| + // If reading the image descriptor is successful, the image |
| + // count will be incremented |
| + SkASSERT(fGif->ImageCount >= 1); |
| + SavedImage* image = &fGif->SavedImages[fGif->ImageCount - 1]; |
| + |
| + // Process the descriptor |
| + const GifImageDesc& desc = image->ImageDesc; |
| + int32_t imageLeft = desc.Left; |
| + int32_t imageTop = desc.Top; |
| + int32_t innerWidth = desc.Width; |
| + int32_t innerHeight = desc.Height; |
| + // Fail on non-positive dimensions |
| + if (innerWidth <= 0 || innerHeight <= 0) { |
| + gif_message("Invalid dimensions for inner image.\n"); |
| + return kInvalidInput; |
| + } |
| + // Treat the following cases as warnings and try to fix |
| + if (innerWidth > width) { |
| + gif_message("Inner image too wide, shrinking.\n"); |
| + innerWidth = width; |
| + imageLeft = 0; |
| + } else if (imageLeft + innerWidth > width) { |
| + gif_message("Shifting inner image to left to fit.\n"); |
| + imageLeft = width - innerWidth; |
| + } else if (imageLeft < 0) { |
| + gif_message("Shifting image to right to fit\n"); |
| + imageLeft = 0; |
| + } |
| + if (innerHeight > height) { |
| + gif_message("Inner image too tall, shrinking.\n"); |
| + innerHeight = height; |
| + imageTop = 0; |
| + } else if (imageTop + innerHeight > height) { |
| + gif_message("Shifting inner image up to fit.\n"); |
| + imageTop = height - innerHeight; |
| + } else if (imageTop < 0) { |
| + gif_message("Shifting image down to fit\n"); |
| + imageTop = 0; |
| + } |
| + |
| + // Set up the color table |
| + uint32_t colorCount = 0; |
| + // Allocate maximum storage to deal with invalid indices safely |
| + const uint32_t maxColors = 256; |
| + SkAutoTDeleteArray<SkPMColor> colorTable( |
|
scroggo
2015/03/26 20:03:53
This is better as
SkPMColor colorPtr[maxColors];
msarett
2015/03/26 22:26:37
Done.
|
| + SkNEW_ARRAY(SkPMColor, maxColors)); |
| + ColorMapObject* colorMap = fGif->Image.ColorMap; |
| + // If there is no local color table, use the global color table |
| + if (NULL == colorMap) { |
| + colorMap = fGif->SColorMap; |
| + } |
| + if (NULL != colorMap) { |
| + colorCount = colorMap->ColorCount; |
| + SkASSERT(colorCount == |
| + (unsigned) (1 << (colorMap->BitsPerPixel))); |
| + SkASSERT(colorCount <= 256); |
| + for (uint32_t i = 0; i < colorCount; i++) { |
| + colorTable.get()[i] = SkPackARGB32(0xFF, |
| + colorMap->Colors[i].Red, |
| + colorMap->Colors[i].Green, |
| + colorMap->Colors[i].Blue); |
| + } |
| + } |
| + |
| + // This is used to fill unspecified pixels in the image data. |
| + uint32_t fillIndex = fGif->SBackGroundColor; |
| + bool fillBackground = true; |
| + ZeroInitialized zeroInit = opts.fZeroInitialized; |
| + |
| + // Gifs have the option to specify the color at a single |
| + // index of the color table as transparent. |
| + { |
| + int32_t transIndex = find_trans_index(saveExt, colorCount); |
| + |
| + // If the background is already zeroed and we have a valid |
| + // transparent index, we do not need to fill the background. |
| + if (transIndex >= 0) { |
| + colorTable.get()[transIndex] = SK_ColorTRANSPARENT; |
| + // If there is a transparent index, we also use this as |
| + // the fill index. |
| + fillIndex = transIndex; |
| + fillBackground = (kYes_ZeroInitialized != zeroInit); |
|
scroggo
2015/03/26 20:03:53
Is it possible that the fillIndex, either as speci
msarett
2015/03/26 22:26:37
Hmm yeah it will add complexity when we care about
scroggo
2015/03/27 13:09:36
Really? There aren't any images with invalid entri
msarett
2015/03/27 14:23:19
Yeah this is an actual guarantee. The gif color t
scroggo
2015/03/27 14:36:11
Oh yeah, of course!
|
| + } else if (fillIndex >= colorCount) { |
| + // If the fill index is invalid, we default to 0. This |
| + // behavior is unspecified but matches SkImageDecoder. |
| + fillIndex = 0; |
| + } |
| + } |
| + |
| + // Fill in the color table for indices greater than color count. |
| + // This allows for predictable, safe behavior. |
| + for (uint32_t i = colorCount; i < maxColors; i++) { |
| + colorTable.get()[i] = colorTable.get()[fillIndex]; |
| + } |
| + |
| + // Check if image is only a subset of the image frame |
| + SkAutoTDelete<SkSwizzler> swizzler(NULL); |
| + if (innerWidth < width || innerHeight < height) { |
| + |
| + // Modify the destination info |
| + const SkImageInfo subsetDstInfo = |
| + dstInfo.makeWH(innerWidth, innerHeight); |
| + |
| + // Fill the destination with the fill color |
| + // FIXME: This may not be the behavior that we want for |
| + // animated gifs where we draw on top of the |
| + // previous frame. |
| + SkColorType dstColorType = dstInfo.colorType(); |
| + if (fillBackground) { |
| + switch (dstColorType) { |
| + case kN32_SkColorType: |
| + sk_memset32((SkPMColor*) dst, |
| + colorTable.get()[fillIndex], |
| + dstRowBytes * height / sizeof(SkPMColor)); |
| + break; |
| + default: |
| + SkASSERT(false); |
| + break; |
| + } |
| + } |
| + |
| + // Modify the dst pointer |
| + const int32_t dstBytesPerPixel = |
| + SkColorTypeBytesPerPixel(dstColorType); |
| + void* subsetDst = SkTAddOffset<void*>(dst, |
| + dstRowBytes * imageTop + |
| + dstBytesPerPixel * imageLeft); |
| + |
| + // Create the subset swizzler |
| + swizzler.reset(SkSwizzler::CreateSwizzler( |
| + SkSwizzler::kIndex, colorTable.get(), |
| + subsetDstInfo, subsetDst, dstRowBytes, zeroInit)); |
| + } else { |
| + // Create the fully dimensional swizzler |
| + swizzler.reset(SkSwizzler::CreateSwizzler( |
| + SkSwizzler::kIndex, colorTable.get(), dstInfo, dst, |
| + dstRowBytes, zeroInit)); |
| + } |
| + |
| + // Stores output from dgiflib and input to the swizzler |
| + SkAutoTDeleteArray<uint8_t> |
| + buffer(SkNEW_ARRAY(uint8_t, innerWidth)); |
| + |
| + // Check the interlace flag and iterate over rows of the input |
| + if (fGif->Image.Interlace) { |
| + // In interlace mode, the rows of input are rearranged in |
| + // the output image. We use an iterator to take care of |
| + // the rearranging. |
| + SkGifInterlaceIter iter(innerHeight); |
| + for (int32_t y = 0; y < innerHeight; y++) { |
| + if (GIF_ERROR == DGifGetLine(fGif, buffer.get(), |
| + innerWidth)) { |
| + gif_message(SkStringPrintf( |
| + "Could not decode line %d of %d.\n", |
| + y, height - 1).c_str()); |
| + // Recover from error by filling remainder of image |
| + if (fillBackground) { |
| + memset(buffer.get(), fillIndex, innerWidth); |
| + for (; y < innerHeight; y++) { |
| + swizzler->next(buffer.get(), iter.nextY()); |
| + } |
| + } |
| + return kIncompleteInput; |
| + } |
| + swizzler->next(buffer.get(), iter.nextY()); |
| + } |
| + } else { |
| + // Standard mode |
| + for (int32_t y = 0; y < innerHeight; y++) { |
| + if (GIF_ERROR == DGifGetLine(fGif, buffer.get(), |
| + innerWidth)) { |
| + gif_message(SkStringPrintf( |
| + "Could not decode line %d of %d.\n", |
| + y, height - 1).c_str()); |
| + if (fillBackground) { |
| + void* dstPtr = SkTAddOffset<void*>( |
| + dst, y * dstRowBytes); |
| + memset(dstPtr, fillIndex, |
|
scroggo
2015/03/26 20:03:54
For N32, we need to memset32 with the colorPtr[fil
msarett
2015/03/26 22:26:37
Wow yeah, I messed that up.
This makes me wonder
scroggo
2015/03/27 13:09:36
Possibly. It would make this code much simpler, fo
|
| + (height - y) * dstRowBytes); |
| + } |
| + return kIncompleteInput; |
| + } |
| + swizzler->next(buffer.get()); |
| + } |
| + } |
| + |
| + // FIXME: Gif files may have multiple images stored in a single |
| + // file. This is most commonly used to enable |
| + // animations. Since we are leaving animated gifs as a |
| + // TODO, we will return kSuccess after decoding the |
| + // first image in the file. This is the same behavior |
| + // as SkImageDecoder_libgif. |
| + // |
| + // Most times this works pretty well, but sometimes it |
| + // doesn't. For example, I have an animated test image |
| + // where the first image in the file is 1x1, but the |
| + // subsequent images are meaningful. This currently |
| + // displays the 1x1 image, which is not ideal. Right |
| + // now I am leaving this as an issue that will be |
| + // addressed when we implement animated gifs. |
| + // |
| + // It is also possible (not explicitly disallowed in the |
| + // specification) that gif files provide multiple |
| + // images in a single file that are all meant to be |
| + // displayed in the same frame together. I will |
| + // currently leave this unimplemented until I find a |
| + // test case that expects this behavior. |
| + return kSuccess; |
| + } |
| + |
| + // Extensions are used to specify special properties of the image |
| + // such as transparency or animation. |
| + case EXTENSION_RECORD_TYPE: |
| + // Read extension data |
| +#if GIFLIB_MAJOR < 5 |
| + if (GIF_ERROR == |
| + DGifGetExtension(fGif, &saveExt.Function, &extData)) { |
| +#else |
| + if (GIF_ERROR == |
| + DGifGetExtension(fGif, &extFunction, &extData)) { |
| +#endif |
| + gif_message("Could not get extension.\n"); |
| + return kInvalidInput; |
| + } |
| + |
| + // Create an extension block with our data |
| + while (NULL != extData) { |
| + // Add a single block |
| +#if GIFLIB_MAJOR < 5 |
| + if (GIF_ERROR == AddExtensionBlock(&saveExt, extData[0], |
| + &extData[1])) { |
| +#else |
| + if (GIF_ERROR == |
| + GifAddExtensionBlock(&saveExt.ExtensionBlockCount, |
| + &saveExt.ExtensionBlocks, extFunction, extData[0], |
| + &extData[1])) { |
| +#endif |
| + gif_message("Could not add extension block.\n"); |
| + return kInvalidInput; |
| + } |
| + // Move to the next block |
| + if (GIF_ERROR == DGifGetExtensionNext(fGif, &extData)) { |
| + gif_message("Could not get next extension.\n"); |
| + return kInvalidInput; |
| + } |
| +#if GIFLIB_MAJOR < 5 |
| + saveExt.Function = 0; |
| +#endif |
| + } |
| + break; |
| + |
| + // Signals the end of the gif file |
| + case TERMINATE_RECORD_TYPE: |
| + break; |
| + |
| + default: |
| + break; |
| + } |
| + } |
| + |
| + gif_message("Could not find any images to decode in gif file.\n"); |
| + return kInvalidInput; |
| +} |