1 // Copyright 2014 PDFium Authors. All rights reserved.
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_GIF_H_
8 #define CORE_FXCODEC_GIF_CFX_GIF_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fxcrt/fx_memory_wrappers.h"
14 
15 class CFX_GifContext;
16 
17 extern const char kGifSignature87[];
18 extern const char kGifSignature89[];
19 
20 #define GIF_SIG_EXTENSION 0x21
21 #define GIF_SIG_IMAGE 0x2C
22 #define GIF_SIG_TRAILER 0x3B
23 #define GIF_BLOCK_GCE 0xF9
24 #define GIF_BLOCK_PTE 0x01
25 #define GIF_BLOCK_CE 0xFE
26 #define GIF_BLOCK_TERMINAL 0x00
27 #define GIF_MAX_LZW_EXP 12
28 #define GIF_MAX_LZW_CODE 4096
29 #define GIF_D_STATUS_SIG 0x01
30 #define GIF_D_STATUS_TAIL 0x02
31 #define GIF_D_STATUS_EXT 0x03
32 #define GIF_D_STATUS_EXT_CE 0x05
33 #define GIF_D_STATUS_EXT_GCE 0x06
34 #define GIF_D_STATUS_EXT_PTE 0x07
35 #define GIF_D_STATUS_EXT_UNE 0x08
36 #define GIF_D_STATUS_IMG_INFO 0x09
37 #define GIF_D_STATUS_IMG_DATA 0x0A
38 
39 #pragma pack(1)
40 struct CFX_GifGlobalFlags {
41   uint8_t pal_bits : 3;
42   uint8_t sort_flag : 1;
43   uint8_t color_resolution : 3;
44   uint8_t global_pal : 1;
45 };
46 
47 struct CFX_GifLocalFlags {
48   uint8_t pal_bits : 3;
49   uint8_t reserved : 2;
50   uint8_t sort_flag : 1;
51   uint8_t interlace : 1;
52   uint8_t local_pal : 1;
53 };
54 
55 struct CFX_GifHeader {
56   char signature[6];
57 };
58 
59 struct CFX_GifLocalScreenDescriptor {
60   uint16_t width;
61   uint16_t height;
62   CFX_GifGlobalFlags global_flags;
63   uint8_t bc_index;
64   uint8_t pixel_aspect;
65 };
66 
67 struct CFX_CFX_GifImageInfo {
68   uint16_t left;
69   uint16_t top;
70   uint16_t width;
71   uint16_t height;
72   CFX_GifLocalFlags local_flags;
73 };
74 
75 struct CFX_GifControlExtensionFlags {
76   uint8_t transparency : 1;
77   uint8_t user_input : 1;
78   uint8_t disposal_method : 3;
79   uint8_t reserved : 3;
80 };
81 
82 struct CFX_GifGraphicControlExtension {
83   uint8_t block_size;
84   CFX_GifControlExtensionFlags gce_flags;
85   uint16_t delay_time;
86   uint8_t trans_index;
87 };
88 
89 struct CFX_GifPlainTextExtension {
90   uint8_t block_size;
91   uint16_t grid_left;
92   uint16_t grid_top;
93   uint16_t grid_width;
94   uint16_t grid_height;
95   uint8_t char_width;
96   uint8_t char_height;
97   uint8_t fc_index;
98   uint8_t bc_index;
99 };
100 
101 struct GifApplicationExtension {
102   uint8_t block_size;
103   uint8_t app_identify[8];
104   uint8_t app_authentication[3];
105 };
106 
107 struct CFX_GifPalette {
108   uint8_t r;
109   uint8_t g;
110   uint8_t b;
111 };
112 #pragma pack()
113 
114 enum class CFX_GifDecodeStatus {
115   Error,
116   Success,
117   Unfinished,
118   InsufficientDestSize,  // Only used internally by CGifLZWDecoder::Decode()
119 };
120 
121 struct CFX_GifImage {
122   CFX_GifImage();
123   ~CFX_GifImage();
124 
125   std::unique_ptr<CFX_GifGraphicControlExtension> image_GCE;
126   std::vector<CFX_GifPalette> local_palettes;
127   std::vector<uint8_t, FxAllocAllocator<uint8_t>> row_buffer;
128   CFX_CFX_GifImageInfo image_info;
129   uint8_t local_pallette_exp;
130   uint8_t code_exp;
131   uint32_t data_pos;
132   int32_t row_num;
133 };
134 
135 #endif  // CORE_FXCODEC_GIF_CFX_GIF_H_
136