msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCodec.h" |
| 9 | #include "SkImageInfo.h" |
| 10 | |
| 11 | #include "gif_lib.h" |
| 12 | |
| 13 | /* |
| 14 | * |
| 15 | * This class implements the decoding for gif images |
| 16 | * |
| 17 | */ |
| 18 | class SkGifCodec : public SkCodec { |
| 19 | public: |
| 20 | |
| 21 | /* |
| 22 | * Checks the start of the stream to see if the image is a gif |
| 23 | */ |
| 24 | static bool IsGif(SkStream*); |
| 25 | |
| 26 | /* |
| 27 | * Assumes IsGif was called and returned true |
| 28 | * Creates a gif decoder |
| 29 | * Reads enough of the stream to determine the image format |
| 30 | */ |
| 31 | static SkCodec* NewFromStream(SkStream*); |
| 32 | |
| 33 | |
| 34 | protected: |
| 35 | |
| 36 | /* |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame^] | 37 | * Read enough of the stream to initialize the SkGifCodec. |
| 38 | * Returns a bool representing success or failure. |
| 39 | * |
| 40 | * @param codecOut |
| 41 | * If it returned true, and codecOut was not NULL, |
| 42 | * codecOut will be set to a new SkGifCodec. |
| 43 | * |
| 44 | * @param gifOut |
| 45 | * If it returned true, and codecOut was NULL, |
| 46 | * gifOut must be non-NULL and gifOut will be set to a new |
| 47 | * GifFileType pointer. |
| 48 | * |
| 49 | * @param stream |
| 50 | * Deleted on failure. |
| 51 | * codecOut will take ownership of it in the case where we created a codec. |
| 52 | * Ownership is unchanged when we returned a gifOut. |
| 53 | * |
| 54 | */ |
| 55 | static bool ReadHeader(SkStream* stream, SkCodec** codecOut, GifFileType** gifOut); |
| 56 | |
| 57 | /* |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 58 | * Initiates the gif decode |
| 59 | */ |
| 60 | Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, |
| 61 | SkPMColor*, int32_t*) override; |
| 62 | |
| 63 | SkEncodedFormat onGetEncodedFormat() const override { |
| 64 | return kGIF_SkEncodedFormat; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | |
| 69 | /* |
| 70 | * This function cleans up the gif object after the decode completes |
| 71 | * It is used in a SkAutoTCallIProc template |
| 72 | */ |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame^] | 73 | static void CloseGif(GifFileType* gif); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Frees any extension data used in the decode |
| 77 | * Used in a SkAutoTCallVProc |
| 78 | */ |
| 79 | static void FreeExtension(SavedImage* image); |
| 80 | |
| 81 | /* |
| 82 | * Creates an instance of the decoder |
| 83 | * Called only by NewFromStream |
| 84 | * |
| 85 | * @param srcInfo contains the source width and height |
| 86 | * @param stream the stream of image data |
| 87 | * @param gif pointer to library type that manages gif decode |
| 88 | * takes ownership |
| 89 | */ |
| 90 | SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif); |
| 91 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame^] | 92 | SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 93 | |
| 94 | typedef SkCodec INHERITED; |
| 95 | }; |