SkCodec::Result SkGifCodec::ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex) { // Use this as a container to hold information about any gif extension // blocks. This generally stores transparency and animation instructions. SavedImage saveExt; SkAutoTCallVProc<SavedImage, FreeExtension> autoFreeExt(&saveExt); saveExt.ExtensionBlocks = nullptr; saveExt.ExtensionBlockCount = 0; GifByteType* extData; int32_t extFunction; // 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. GifRecordType recordType; do { // Get the current record type if (GIF_ERROR == DGifGetRecordType(gif, &recordType)) { return gif_error("DGifGetRecordType failed.\n", kInvalidInput); } switch (recordType) { case IMAGE_DESC_RECORD_TYPE: { *transIndex = find_trans_index(saveExt); // 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 (GIF_ERROR == DGifGetExtension(gif, &extFunction, &extData)) { return gif_error("Could not get extension.\n", kIncompleteInput); } // Create an extension block with our data while (nullptr != extData) { // Add a single block #if GIFLIB_MAJOR < 5 if (AddExtensionBlock(&saveExt, extData[0], &extData[1]) == GIF_ERROR) { #else if (GIF_ERROR == GifAddExtensionBlock(&saveExt.ExtensionBlockCount, &saveExt.ExtensionBlocks, extFunction, extData[0], &extData[1])) { #endif return gif_error("Could not add extension block.\n", kIncompleteInput); } // Move to the next block if (GIF_ERROR == DGifGetExtensionNext(gif, &extData)) { return gif_error("Could not get next extension.\n", kIncompleteInput); } } break; // Signals the end of the gif file case TERMINATE_RECORD_TYPE: break; default: // DGifGetRecordType returns an error if the record type does // not match one of the above cases. This should not be // reached. SkASSERT(false); break; } } while (TERMINATE_RECORD_TYPE != recordType); return gif_error("Could not find any images to decode in gif file.\n", kInvalidInput); } bool SkGifCodec::GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRect) { // Get the encoded dimension values SavedImage* image = &gif->SavedImages[gif->ImageCount - 1]; const GifImageDesc& desc = image->ImageDesc; int frameLeft = desc.Left; int frameTop = desc.Top; int frameWidth = desc.Width; int frameHeight = desc.Height; int width = gif->SWidth; int height = gif->SHeight; // Ensure that the decode dimensions are large enough to contain the frame width = SkTMax(width, frameWidth + frameLeft); height = SkTMax(height, frameHeight + frameTop); // All of these dimensions should be positive, as they are encoded as unsigned 16-bit integers. // It is unclear why giflib casts them to ints. We will go ahead and check that they are // in fact positive. if (frameLeft < 0 || frameTop < 0 || frameWidth < 0 || frameHeight < 0 || width <= 0 || height <= 0) { return false; } frameRect->setXYWH(frameLeft, frameTop, frameWidth, frameHeight); size->set(width, height); return true; }
SkCodec::Result SkGifCodec::ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex) { // Use this as a container to hold information about any gif extension // blocks. This generally stores transparency and animation instructions. SavedImage saveExt; SkAutoTCallVProc<SavedImage, FreeExtension> autoFreeExt(&saveExt); saveExt.ExtensionBlocks = nullptr; saveExt.ExtensionBlockCount = 0; GifByteType* extData; int32_t extFunction; // 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. GifRecordType recordType; do { // Get the current record type if (GIF_ERROR == DGifGetRecordType(gif, &recordType)) { return gif_error("DGifGetRecordType failed.\n", kInvalidInput); } switch (recordType) { case IMAGE_DESC_RECORD_TYPE: { *transIndex = find_trans_index(saveExt); // 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 (GIF_ERROR == DGifGetExtension(gif, &extFunction, &extData)) { return gif_error("Could not get extension.\n", kIncompleteInput); } // Create an extension block with our data while (nullptr != extData) { // Add a single block if (GIF_ERROR == GifAddExtensionBlock(&saveExt.ExtensionBlockCount, &saveExt.ExtensionBlocks, extFunction, extData[0], &extData[1])) { return gif_error("Could not add extension block.\n", kIncompleteInput); } // Move to the next block if (GIF_ERROR == DGifGetExtensionNext(gif, &extData)) { return gif_error("Could not get next extension.\n", kIncompleteInput); } } break; // Signals the end of the gif file case TERMINATE_RECORD_TYPE: break; default: // DGifGetRecordType returns an error if the record type does // not match one of the above cases. This should not be // reached. SkASSERT(false); break; } } while (TERMINATE_RECORD_TYPE != recordType); return gif_error("Could not find any images to decode in gif file.\n", kInvalidInput); }