OLD | NEW |
| (Empty) |
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_SRC_FXCODEC_LGIF_FX_GIF_H_ | |
8 #define CORE_SRC_FXCODEC_LGIF_FX_GIF_H_ | |
9 | |
10 #include <setjmp.h> | |
11 | |
12 #include "core/include/fxcrt/fx_basic.h" | |
13 | |
14 #define GIF_SIGNATURE "GIF" | |
15 #define GIF_SIG_EXTENSION 0x21 | |
16 #define GIF_SIG_IMAGE 0x2C | |
17 #define GIF_SIG_TRAILER 0x3B | |
18 #define GIF_BLOCK_GCE 0xF9 | |
19 #define GIF_BLOCK_PTE 0x01 | |
20 #define GIF_BLOCK_CE 0xFE | |
21 #define GIF_BLOCK_AE 0xFF | |
22 #define GIF_BLOCK_TERMINAL 0x00 | |
23 #define GIF_MAX_LZW_CODE 4096 | |
24 #define GIF_DATA_BLOCK 255 | |
25 #define GIF_MAX_ERROR_SIZE 256 | |
26 #define GIF_D_STATUS_SIG 0x01 | |
27 #define GIF_D_STATUS_TAIL 0x02 | |
28 #define GIF_D_STATUS_EXT 0x03 | |
29 #define GIF_D_STATUS_EXT_AE 0x04 | |
30 #define GIF_D_STATUS_EXT_CE 0x05 | |
31 #define GIF_D_STATUS_EXT_GCE 0x06 | |
32 #define GIF_D_STATUS_EXT_PTE 0x07 | |
33 #define GIF_D_STATUS_EXT_UNE 0x08 | |
34 #define GIF_D_STATUS_IMG_INFO 0x09 | |
35 #define GIF_D_STATUS_IMG_DATA 0x0A | |
36 #pragma pack(1) | |
37 typedef struct tagGifGF { | |
38 uint8_t pal_bits : 3; | |
39 uint8_t sort_flag : 1; | |
40 uint8_t color_resolution : 3; | |
41 uint8_t global_pal : 1; | |
42 } GifGF; | |
43 typedef struct tagGifLF { | |
44 uint8_t pal_bits : 3; | |
45 uint8_t reserved : 2; | |
46 uint8_t sort_flag : 1; | |
47 uint8_t interlace : 1; | |
48 uint8_t local_pal : 1; | |
49 } GifLF; | |
50 typedef struct tagGifHeader { | |
51 char signature[3]; | |
52 char version[3]; | |
53 } GifHeader; | |
54 typedef struct tagGifLSD { | |
55 FX_WORD width; | |
56 FX_WORD height; | |
57 uint8_t global_flag; | |
58 uint8_t bc_index; | |
59 uint8_t pixel_aspect; | |
60 } GifLSD; | |
61 typedef struct tagGifImageInfo { | |
62 FX_WORD left; | |
63 FX_WORD top; | |
64 FX_WORD width; | |
65 FX_WORD height; | |
66 | |
67 uint8_t local_flag; | |
68 } GifImageInfo; | |
69 typedef struct tagGifCEF { | |
70 uint8_t transparency : 1; | |
71 uint8_t user_input : 1; | |
72 uint8_t disposal_method : 3; | |
73 uint8_t reserved : 3; | |
74 } GifCEF; | |
75 typedef struct tagGifGCE { | |
76 uint8_t block_size; | |
77 uint8_t gce_flag; | |
78 FX_WORD delay_time; | |
79 uint8_t trans_index; | |
80 } GifGCE; | |
81 typedef struct tagGifPTE { | |
82 uint8_t block_size; | |
83 FX_WORD grid_left; | |
84 FX_WORD grid_top; | |
85 FX_WORD grid_width; | |
86 FX_WORD grid_height; | |
87 | |
88 uint8_t char_width; | |
89 uint8_t char_height; | |
90 | |
91 uint8_t fc_index; | |
92 uint8_t bc_index; | |
93 } GifPTE; | |
94 typedef struct tagGifAE { | |
95 uint8_t block_size; | |
96 uint8_t app_identify[8]; | |
97 uint8_t app_authentication[3]; | |
98 } GifAE; | |
99 typedef struct tagGifPalette { uint8_t r, g, b; } GifPalette; | |
100 #pragma pack() | |
101 typedef struct tagGifImage { | |
102 GifGCE* image_gce_ptr; | |
103 GifPalette* local_pal_ptr; | |
104 GifImageInfo* image_info_ptr; | |
105 uint8_t image_code_size; | |
106 FX_DWORD image_data_pos; | |
107 uint8_t* image_row_buf; | |
108 int32_t image_row_num; | |
109 } GifImage; | |
110 typedef struct tagGifPlainText { | |
111 GifGCE* gce_ptr; | |
112 GifPTE* pte_ptr; | |
113 CFX_ByteString* string_ptr; | |
114 } GifPlainText; | |
115 class CGifLZWDecoder { | |
116 public: | |
117 struct tag_Table { | |
118 FX_WORD prefix; | |
119 uint8_t suffix; | |
120 }; | |
121 CGifLZWDecoder(FX_CHAR* error_ptr = NULL) { err_msg_ptr = error_ptr; } | |
122 void InitTable(uint8_t code_len); | |
123 | |
124 int32_t Decode(uint8_t* des_buf, FX_DWORD& des_size); | |
125 | |
126 void Input(uint8_t* src_buf, FX_DWORD src_size); | |
127 FX_DWORD GetAvailInput(); | |
128 | |
129 private: | |
130 void ClearTable(); | |
131 void AddCode(FX_WORD prefix_code, uint8_t append_char); | |
132 void DecodeString(FX_WORD code); | |
133 uint8_t code_size; | |
134 uint8_t code_size_cur; | |
135 FX_WORD code_clear; | |
136 FX_WORD code_end; | |
137 FX_WORD code_next; | |
138 uint8_t code_first; | |
139 uint8_t stack[GIF_MAX_LZW_CODE]; | |
140 FX_WORD stack_size; | |
141 tag_Table code_table[GIF_MAX_LZW_CODE]; | |
142 FX_WORD code_old; | |
143 | |
144 uint8_t* next_in; | |
145 FX_DWORD avail_in; | |
146 | |
147 uint8_t bits_left; | |
148 FX_DWORD code_store; | |
149 | |
150 FX_CHAR* err_msg_ptr; | |
151 }; | |
152 class CGifLZWEncoder { | |
153 public: | |
154 struct tag_Table { | |
155 FX_WORD prefix; | |
156 uint8_t suffix; | |
157 }; | |
158 CGifLZWEncoder(); | |
159 ~CGifLZWEncoder(); | |
160 void Start(uint8_t code_len, | |
161 const uint8_t* src_buf, | |
162 uint8_t*& dst_buf, | |
163 FX_DWORD& offset); | |
164 FX_BOOL Encode(const uint8_t* src_buf, | |
165 FX_DWORD src_len, | |
166 uint8_t*& dst_buf, | |
167 FX_DWORD& dst_len, | |
168 FX_DWORD& offset); | |
169 void Finish(uint8_t*& dst_buf, FX_DWORD& dst_len, FX_DWORD& offset); | |
170 | |
171 private: | |
172 void ClearTable(); | |
173 FX_BOOL LookUpInTable(const uint8_t* buf, | |
174 FX_DWORD& offset, | |
175 uint8_t& bit_offset); | |
176 void EncodeString(FX_DWORD index, | |
177 uint8_t*& dst_buf, | |
178 FX_DWORD& dst_len, | |
179 FX_DWORD& offset); | |
180 void WriteBlock(uint8_t*& dst_buf, FX_DWORD& dst_len, FX_DWORD& offset); | |
181 jmp_buf jmp; | |
182 FX_DWORD src_offset; | |
183 uint8_t src_bit_offset; | |
184 uint8_t src_bit_cut; | |
185 FX_DWORD src_bit_num; | |
186 uint8_t code_size; | |
187 FX_WORD code_clear; | |
188 FX_WORD code_end; | |
189 FX_WORD index_num; | |
190 uint8_t bit_offset; | |
191 uint8_t index_bit_cur; | |
192 uint8_t index_buf[GIF_DATA_BLOCK]; | |
193 uint8_t index_buf_len; | |
194 tag_Table code_table[GIF_MAX_LZW_CODE]; | |
195 FX_WORD table_cur; | |
196 }; | |
197 typedef struct tag_gif_decompress_struct gif_decompress_struct; | |
198 typedef gif_decompress_struct* gif_decompress_struct_p; | |
199 typedef gif_decompress_struct_p* gif_decompress_struct_pp; | |
200 static const int32_t s_gif_interlace_step[4] = {8, 8, 4, 2}; | |
201 struct tag_gif_decompress_struct { | |
202 jmp_buf jmpbuf; | |
203 FX_CHAR* err_ptr; | |
204 void (*gif_error_fn)(gif_decompress_struct_p gif_ptr, const FX_CHAR* err_msg); | |
205 void* context_ptr; | |
206 int width; | |
207 int height; | |
208 GifPalette* global_pal_ptr; | |
209 int32_t global_pal_num; | |
210 uint8_t global_sort_flag; | |
211 uint8_t global_color_resolution; | |
212 | |
213 uint8_t bc_index; | |
214 uint8_t pixel_aspect; | |
215 CGifLZWDecoder* img_decoder_ptr; | |
216 FX_DWORD img_row_offset; | |
217 FX_DWORD img_row_avail_size; | |
218 uint8_t img_pass_num; | |
219 CFX_ArrayTemplate<GifImage*>* img_ptr_arr_ptr; | |
220 uint8_t* (*gif_ask_buf_for_pal_fn)(gif_decompress_struct_p gif_ptr, | |
221 int32_t pal_size); | |
222 uint8_t* next_in; | |
223 FX_DWORD avail_in; | |
224 int32_t decode_status; | |
225 FX_DWORD skip_size; | |
226 void (*gif_record_current_position_fn)(gif_decompress_struct_p gif_ptr, | |
227 FX_DWORD* cur_pos_ptr); | |
228 void (*gif_get_row_fn)(gif_decompress_struct_p gif_ptr, | |
229 int32_t row_num, | |
230 uint8_t* row_buf); | |
231 FX_BOOL (*gif_get_record_position_fn)(gif_decompress_struct_p gif_ptr, | |
232 FX_DWORD cur_pos, | |
233 int32_t left, int32_t top, int32_t width, int32_t height, | |
234 int32_t pal_num, void* pal_ptr, | |
235 int32_t delay_time, FX_BOOL user_input, | |
236 int32_t trans_index, int32_t disposal_method, FX_BOOL interlace); | |
237 CFX_ByteString* cmt_data_ptr; | |
238 GifGCE* gce_ptr; | |
239 CFX_ArrayTemplate<GifPlainText*>* pt_ptr_arr_ptr; | |
240 }; | |
241 typedef struct tag_gif_compress_struct gif_compress_struct; | |
242 typedef gif_compress_struct* gif_compress_struct_p; | |
243 typedef gif_compress_struct_p* gif_compress_struct_pp; | |
244 struct tag_gif_compress_struct { | |
245 const uint8_t* src_buf; | |
246 FX_DWORD src_pitch; | |
247 FX_DWORD src_width; | |
248 FX_DWORD src_row; | |
249 FX_DWORD cur_offset; | |
250 FX_DWORD frames; | |
251 GifHeader* header_ptr; | |
252 GifLSD* lsd_ptr; | |
253 GifPalette* global_pal; | |
254 FX_WORD gpal_num; | |
255 GifPalette* local_pal; | |
256 FX_WORD lpal_num; | |
257 GifImageInfo* image_info_ptr; | |
258 CGifLZWEncoder* img_encoder_ptr; | |
259 | |
260 uint8_t* cmt_data_ptr; | |
261 FX_DWORD cmt_data_len; | |
262 GifGCE* gce_ptr; | |
263 GifPTE* pte_ptr; | |
264 const uint8_t* pte_data_ptr; | |
265 FX_DWORD pte_data_len; | |
266 }; | |
267 | |
268 void gif_error(gif_decompress_struct_p gif_ptr, const FX_CHAR* err_msg); | |
269 void gif_warn(gif_decompress_struct_p gif_ptr, const FX_CHAR* err_msg); | |
270 gif_decompress_struct_p gif_create_decompress(); | |
271 void gif_destroy_decompress(gif_decompress_struct_pp gif_ptr_ptr); | |
272 gif_compress_struct_p gif_create_compress(); | |
273 void gif_destroy_compress(gif_compress_struct_pp gif_ptr_ptr); | |
274 int32_t gif_read_header(gif_decompress_struct_p gif_ptr); | |
275 int32_t gif_get_frame(gif_decompress_struct_p gif_ptr); | |
276 int32_t gif_get_frame_num(gif_decompress_struct_p gif_ptr); | |
277 int32_t gif_decode_extension(gif_decompress_struct_p gif_ptr); | |
278 int32_t gif_decode_image_info(gif_decompress_struct_p gif_ptr); | |
279 void gif_takeover_gce_ptr(gif_decompress_struct_p gif_ptr, | |
280 GifGCE** gce_ptr_ptr); | |
281 int32_t gif_load_frame(gif_decompress_struct_p gif_ptr, int32_t frame_num); | |
282 uint8_t* gif_read_data(gif_decompress_struct_p gif_ptr, | |
283 uint8_t** des_buf_pp, | |
284 FX_DWORD data_size); | |
285 void gif_save_decoding_status(gif_decompress_struct_p gif_ptr, int32_t status); | |
286 void gif_input_buffer(gif_decompress_struct_p gif_ptr, | |
287 uint8_t* src_buf, | |
288 FX_DWORD src_size); | |
289 FX_DWORD gif_get_avail_input(gif_decompress_struct_p gif_ptr, | |
290 uint8_t** avial_buf_ptr); | |
291 void interlace_buf(const uint8_t* buf, FX_DWORD width, FX_DWORD height); | |
292 FX_BOOL gif_encode(gif_compress_struct_p gif_ptr, | |
293 uint8_t*& dst_buf, | |
294 FX_DWORD& dst_len); | |
295 | |
296 #endif // CORE_SRC_FXCODEC_LGIF_FX_GIF_H_ | |
OLD | NEW |