1 // Copyright 2017 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FXCODEC_GIF_CFX_GIFCONTEXT_H_ 8 #define CORE_FXCODEC_GIF_CFX_GIFCONTEXT_H_ 9 10 #include <memory> 11 #include <vector> 12 13 #include "core/fxcodec/gif/cfx_gif.h" 14 #include "core/fxcodec/gif/gif_decoder.h" 15 #include "core/fxcodec/gif/lzw_decompressor.h" 16 #include "core/fxcrt/retain_ptr.h" 17 #include "core/fxcrt/unowned_ptr.h" 18 #include "third_party/base/containers/span.h" 19 20 class CFX_CodecMemory; 21 22 namespace fxcodec { 23 24 class CFX_GifContext : public ProgressiveDecoderIface::Context { 25 public: 26 explicit CFX_GifContext(GifDecoder::Delegate* delegate); 27 ~CFX_GifContext() override; 28 29 void ReadScanline(int32_t row_num, pdfium::span<uint8_t> row_buf); 30 bool GetRecordPosition(uint32_t cur_pos, 31 int32_t left, 32 int32_t top, 33 int32_t width, 34 int32_t height, 35 int32_t pal_num, 36 CFX_GifPalette* pal, 37 int32_t trans_index, 38 bool interlace); 39 GifDecoder::Status ReadHeader(); 40 GifDecoder::Status GetFrame(); 41 GifDecoder::Status LoadFrame(size_t frame_num); 42 void SetInputBuffer(RetainPtr<CFX_CodecMemory> codec_memory); 43 uint32_t GetAvailInput() const; GetFrameNum()44 size_t GetFrameNum() const { return images_.size(); } 45 46 UnownedPtr<GifDecoder::Delegate> const delegate_; 47 std::vector<CFX_GifPalette> global_palette_; 48 uint8_t global_palette_exp_ = 0; 49 uint32_t img_row_offset_ = 0; 50 uint32_t img_row_avail_size_ = 0; 51 int32_t decode_status_ = GIF_D_STATUS_SIG; 52 std::unique_ptr<CFX_GifGraphicControlExtension> graphic_control_extension_; 53 std::vector<std::unique_ptr<CFX_GifImage>> images_; 54 std::unique_ptr<LZWDecompressor> lzw_decompressor_; 55 int width_ = 0; 56 int height_ = 0; 57 uint8_t bc_index_ = 0; 58 uint8_t global_sort_flag_ = 0; 59 uint8_t global_color_resolution_ = 0; 60 uint8_t img_pass_num_ = 0; 61 62 protected: 63 bool ReadAllOrNone(uint8_t* dest, uint32_t size); 64 GifDecoder::Status ReadGifSignature(); 65 GifDecoder::Status ReadLogicalScreenDescriptor(); 66 67 RetainPtr<CFX_CodecMemory> input_buffer_; 68 69 private: 70 void SaveDecodingStatus(int32_t status); 71 GifDecoder::Status DecodeExtension(); 72 GifDecoder::Status DecodeImageInfo(); 73 void DecodingFailureAtTailCleanup(CFX_GifImage* gif_image); 74 bool ScanForTerminalMarker(); 75 uint8_t GetPaletteExp(CFX_GifImage* gif_image) const; 76 }; 77 78 } // namespace fxcodec 79 80 #endif // CORE_FXCODEC_GIF_CFX_GIFCONTEXT_H_ 81