blob: 93db181a98d5045c601309dac8f88baa7060ec18 [file] [log] [blame]
Philip P. Moltmann4d3acf42017-03-20 11:05:52 -07001// 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#include "core/fxcodec/lgif/fx_gif.h"
8
9#include "core/fxcodec/lbmp/fx_bmp.h"
10#include "third_party/base/stl_util.h"
11
12void CGifLZWDecoder::Input(uint8_t* src_buf, uint32_t src_size) {
13 next_in = src_buf;
14 avail_in = src_size;
15}
16
17uint32_t CGifLZWDecoder::GetAvailInput() {
18 return avail_in;
19}
20
21CGifLZWDecoder::CGifLZWDecoder(FX_CHAR* error_ptr)
22 : code_size(0),
23 code_size_cur(0),
24 code_clear(0),
25 code_end(0),
26 code_next(0),
27 code_first(0),
28 stack_size(0),
29 code_old(0),
30 next_in(nullptr),
31 avail_in(0),
32 bits_left(0),
33 code_store(0),
34 err_msg_ptr(error_ptr) {}
35
36CGifLZWDecoder::~CGifLZWDecoder() {}
37
38void CGifLZWDecoder::InitTable(uint8_t code_len) {
39 code_size = code_len;
40 ASSERT(code_size < 32);
41 code_clear = 1 << code_size;
42 code_end = code_clear + 1;
43 bits_left = 0;
44 code_store = 0;
45 next_in = nullptr;
46 avail_in = 0;
47 stack_size = 0;
48 code_first = 0;
49 ClearTable();
50}
51void CGifLZWDecoder::ClearTable() {
52 code_size_cur = code_size + 1;
53 code_next = code_end + 1;
54 code_old = (uint16_t)-1;
55 FXSYS_memset(code_table, 0, sizeof(tag_Table) * GIF_MAX_LZW_CODE);
56 FXSYS_memset(stack, 0, GIF_MAX_LZW_CODE);
57 for (uint16_t i = 0; i < code_clear; i++) {
58 code_table[i].suffix = (uint8_t)i;
59 }
60}
61void CGifLZWDecoder::DecodeString(uint16_t code) {
62 stack_size = 0;
63 while (true) {
64 ASSERT(code <= code_next);
65 if (code < code_clear || code > code_next) {
66 break;
67 }
68 stack[GIF_MAX_LZW_CODE - 1 - stack_size++] = code_table[code].suffix;
69 code = code_table[code].prefix;
70 }
71 stack[GIF_MAX_LZW_CODE - 1 - stack_size++] = (uint8_t)code;
72 code_first = (uint8_t)code;
73}
74void CGifLZWDecoder::AddCode(uint16_t prefix_code, uint8_t append_char) {
75 if (code_next == GIF_MAX_LZW_CODE) {
76 return;
77 }
78 code_table[code_next].prefix = prefix_code;
79 code_table[code_next].suffix = append_char;
80 if (++code_next < GIF_MAX_LZW_CODE) {
81 if (code_next >> code_size_cur) {
82 code_size_cur++;
83 }
84 }
85}
86int32_t CGifLZWDecoder::Decode(uint8_t* des_buf, uint32_t& des_size) {
87 if (des_size == 0) {
88 return 3;
89 }
90 uint32_t i = 0;
91 if (stack_size != 0) {
92 if (des_size < stack_size) {
93 FXSYS_memcpy(des_buf, &stack[GIF_MAX_LZW_CODE - stack_size], des_size);
94 stack_size -= (uint16_t)des_size;
95 return 3;
96 }
97 FXSYS_memcpy(des_buf, &stack[GIF_MAX_LZW_CODE - stack_size], stack_size);
98 des_buf += stack_size;
99 i += stack_size;
100 stack_size = 0;
101 }
102 uint16_t code = 0;
103 while (i <= des_size && (avail_in > 0 || bits_left >= code_size_cur)) {
104 if (code_size_cur > 12) {
105 if (err_msg_ptr) {
106 FXSYS_strncpy(err_msg_ptr, "Code Length Out Of Range",
107 GIF_MAX_ERROR_SIZE - 1);
108 }
109 return 0;
110 }
111 if (avail_in > 0) {
112 if (bits_left > 31) {
113 if (err_msg_ptr)
114 FXSYS_strncpy(err_msg_ptr, "Decode Error", GIF_MAX_ERROR_SIZE - 1);
115 return 0;
116 }
117 code_store |= (*next_in++) << bits_left;
118 avail_in--;
119 bits_left += 8;
120 }
121 while (bits_left >= code_size_cur) {
122 code = (uint16_t)code_store & ((1 << code_size_cur) - 1);
123 code_store >>= code_size_cur;
124 bits_left -= code_size_cur;
125 if (code == code_clear) {
126 ClearTable();
127 continue;
128 } else if (code >= code_end) {
129 des_size = i;
130 return 1;
131 } else {
132 if (code_old != (uint16_t)-1) {
133 if (code_next < GIF_MAX_LZW_CODE) {
134 if (code == code_next) {
135 AddCode(code_old, code_first);
136 DecodeString(code);
137 } else if (code > code_next) {
138 if (err_msg_ptr) {
139 FXSYS_strncpy(err_msg_ptr, "Decode Error, Out Of Range",
140 GIF_MAX_ERROR_SIZE - 1);
141 }
142 return 0;
143 } else {
144 DecodeString(code);
145 uint8_t append_char = stack[GIF_MAX_LZW_CODE - stack_size];
146 AddCode(code_old, append_char);
147 }
148 }
149 } else {
150 DecodeString(code);
151 }
152 code_old = code;
153 if (i + stack_size > des_size) {
154 FXSYS_memcpy(des_buf, &stack[GIF_MAX_LZW_CODE - stack_size],
155 des_size - i);
156 stack_size -= (uint16_t)(des_size - i);
157 return 3;
158 }
159 FXSYS_memcpy(des_buf, &stack[GIF_MAX_LZW_CODE - stack_size],
160 stack_size);
161 des_buf += stack_size;
162 i += stack_size;
163 stack_size = 0;
164 }
165 }
166 }
167 if (avail_in == 0) {
168 des_size = i;
169 return 2;
170 }
171 return 0;
172}
173static bool gif_grow_buf(uint8_t*& dst_buf, uint32_t& dst_len, uint32_t size) {
174 if (dst_len < size) {
175 uint32_t len_org = dst_len;
176 while (dst_buf && dst_len < size) {
177 dst_len <<= 1;
178 // TODO(thestig): Probably should be a try-realloc here.
179 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_len);
180 }
181 if (!dst_buf) {
182 dst_len = size;
183 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_len);
184 }
185 FXSYS_memset(dst_buf + len_org, 0, dst_len - len_org);
186 return !!dst_buf;
187 }
188 return true;
189}
190static inline void gif_cut_index(uint8_t& val,
191 uint32_t index,
192 uint8_t index_bit,
193 uint8_t index_bit_use,
194 uint8_t bit_use) {
195 uint32_t cut = ((1 << (index_bit - index_bit_use)) - 1) << index_bit_use;
196 val |= ((index & cut) >> index_bit_use) << bit_use;
197}
198static inline uint8_t gif_cut_buf(const uint8_t* buf,
199 uint32_t& offset,
200 uint8_t bit_cut,
201 uint8_t& bit_offset,
202 uint32_t& bit_num) {
203 if (bit_cut != 8) {
204 uint16_t index = 0;
205 index |= ((1 << bit_cut) - 1) << (7 - bit_offset);
206 uint8_t ret = ((index & buf[offset]) >> (7 - bit_offset));
207 bit_offset += bit_cut;
208 if (bit_offset >= 8) {
209 if (bit_offset > 8) {
210 ret |= ((index & (buf[offset + 1] << 8)) >> 8);
211 }
212 bit_offset -= 8;
213 offset++;
214 }
215 bit_num += bit_cut;
216 return ret;
217 }
218 bit_num += bit_cut;
219 return buf[offset++];
220}
221CGifLZWEncoder::CGifLZWEncoder() {
222 FXSYS_memset(this, 0, sizeof(CGifLZWEncoder));
223}
224CGifLZWEncoder::~CGifLZWEncoder() {}
225void CGifLZWEncoder::ClearTable() {
226 index_bit_cur = code_size + 1;
227 index_num = code_end + 1;
228 table_cur = code_end + 1;
229 for (uint16_t i = 0; i < GIF_MAX_LZW_CODE; i++) {
230 code_table[i].prefix = 0;
231 code_table[i].suffix = 0;
232 }
233}
234void CGifLZWEncoder::Start(uint8_t code_len,
235 const uint8_t* src_buf,
236 uint8_t*& dst_buf,
237 uint32_t& offset) {
238 code_size = code_len + 1;
239 ASSERT(code_size < 32);
240 src_bit_cut = code_size;
241 if (code_len == 0) {
242 src_bit_cut = 1;
243 code_size = 2;
244 }
245 code_clear = 1 << code_size;
246 code_end = code_clear + 1;
247 dst_buf[offset++] = code_size;
248 bit_offset = 0;
249 ClearTable();
250 src_offset = 0;
251 src_bit_offset = 0;
252 src_bit_num = 0;
253 code_table[index_num].prefix = gif_cut_buf(src_buf, src_offset, src_bit_cut,
254 src_bit_offset, src_bit_num);
255 code_table[index_num].suffix = gif_cut_buf(src_buf, src_offset, src_bit_cut,
256 src_bit_offset, src_bit_num);
257}
258void CGifLZWEncoder::WriteBlock(uint8_t*& dst_buf,
259 uint32_t& dst_len,
260 uint32_t& offset) {
261 if (!gif_grow_buf(dst_buf, dst_len, offset + GIF_DATA_BLOCK + 1)) {
262 longjmp(jmp, 1);
263 }
264 dst_buf[offset++] = index_buf_len;
265 FXSYS_memcpy(&dst_buf[offset], index_buf, index_buf_len);
266 offset += index_buf_len;
267 FXSYS_memset(index_buf, 0, GIF_DATA_BLOCK);
268 index_buf_len = 0;
269}
270void CGifLZWEncoder::EncodeString(uint32_t index,
271 uint8_t*& dst_buf,
272 uint32_t& dst_len,
273 uint32_t& offset) {
274 uint8_t index_bit_use;
275 index_bit_use = 0;
276 if (index_buf_len == GIF_DATA_BLOCK) {
277 WriteBlock(dst_buf, dst_len, offset);
278 }
279 gif_cut_index(index_buf[index_buf_len], index, index_bit_cur, index_bit_use,
280 bit_offset);
281 if (index_bit_cur <= (8 - bit_offset)) {
282 bit_offset += index_bit_cur;
283 } else if (index_bit_cur <= (16 - bit_offset)) {
284 index_bit_use += (8 - bit_offset);
285 bit_offset = 0;
286 index_buf_len++;
287 if (index_buf_len == GIF_DATA_BLOCK) {
288 WriteBlock(dst_buf, dst_len, offset);
289 }
290 gif_cut_index(index_buf[index_buf_len], index, index_bit_cur, index_bit_use,
291 bit_offset);
292 bit_offset = index_bit_cur - index_bit_use;
293 } else {
294 index_bit_use += (8 - bit_offset);
295 bit_offset = 0;
296 index_buf_len++;
297 if (index_buf_len == GIF_DATA_BLOCK) {
298 WriteBlock(dst_buf, dst_len, offset);
299 }
300 gif_cut_index(index_buf[index_buf_len], index, index_bit_cur, index_bit_use,
301 bit_offset);
302 index_bit_use += 8;
303 bit_offset = 0;
304 index_buf_len++;
305 if (index_buf_len == GIF_DATA_BLOCK) {
306 WriteBlock(dst_buf, dst_len, offset);
307 }
308 gif_cut_index(index_buf[index_buf_len], index, index_bit_cur, index_bit_use,
309 bit_offset);
310 bit_offset = index_bit_cur - index_bit_use;
311 }
312 if (bit_offset == 8) {
313 bit_offset = 0;
314 index_buf_len++;
315 if (index_buf_len == GIF_DATA_BLOCK) {
316 WriteBlock(dst_buf, dst_len, offset);
317 }
318 }
319 if (index == code_end) {
320 index_buf_len++;
321 WriteBlock(dst_buf, dst_len, offset);
322 }
323 if (index_num++ >> index_bit_cur) {
324 index_bit_cur++;
325 }
326}
327bool CGifLZWEncoder::Encode(const uint8_t* src_buf,
328 uint32_t src_len,
329 uint8_t*& dst_buf,
330 uint32_t& dst_len,
331 uint32_t& offset) {
332 uint8_t suffix;
333 if (setjmp(jmp)) {
334 return false;
335 }
336 while (src_bit_num < src_len) {
337 if (!LookUpInTable(src_buf, src_offset, src_bit_offset)) {
338 EncodeString(code_table[index_num].prefix, dst_buf, dst_len, offset);
339 if (index_num == GIF_MAX_LZW_CODE) {
340 suffix = code_table[index_num - 1].suffix;
341 EncodeString(code_clear, dst_buf, dst_len, offset);
342 ClearTable();
343 code_table[index_num].prefix = suffix;
344 code_table[index_num].suffix = gif_cut_buf(
345 src_buf, src_offset, src_bit_cut, src_bit_offset, src_bit_num);
346 } else {
347 code_table[index_num].prefix = code_table[index_num - 1].suffix;
348 code_table[index_num].suffix = gif_cut_buf(
349 src_buf, src_offset, src_bit_cut, src_bit_offset, src_bit_num);
350 }
351 }
352 }
353 src_offset = 0;
354 src_bit_offset = 0;
355 src_bit_num = 0;
356 return true;
357}
358bool CGifLZWEncoder::LookUpInTable(const uint8_t* buf,
359 uint32_t& offset,
360 uint8_t& out_bit_offset) {
361 for (uint16_t i = table_cur; i < index_num; i++) {
362 if (code_table[i].prefix == code_table[index_num].prefix &&
363 code_table[i].suffix == code_table[index_num].suffix) {
364 code_table[index_num].prefix = i;
365 code_table[index_num].suffix =
366 gif_cut_buf(buf, offset, src_bit_cut, out_bit_offset, src_bit_num);
367 table_cur = i;
368 return true;
369 }
370 }
371 table_cur = code_end + 1;
372 return false;
373}
374void CGifLZWEncoder::Finish(uint8_t*& dst_buf,
375 uint32_t& dst_len,
376 uint32_t& offset) {
377 EncodeString(code_table[index_num].prefix, dst_buf, dst_len, offset);
378 EncodeString(code_end, dst_buf, dst_len, offset);
379 bit_offset = 0;
380 ClearTable();
381}
382gif_decompress_struct_p gif_create_decompress() {
383 gif_decompress_struct_p gif_ptr = FX_Alloc(gif_decompress_struct, 1);
384 FXSYS_memset(gif_ptr, 0, sizeof(gif_decompress_struct));
385 gif_ptr->decode_status = GIF_D_STATUS_SIG;
386 gif_ptr->img_ptr_arr_ptr = new std::vector<GifImage*>;
387 gif_ptr->cmt_data_ptr = new CFX_ByteString;
388 gif_ptr->pt_ptr_arr_ptr = new std::vector<GifPlainText*>;
389 return gif_ptr;
390}
391void gif_destroy_decompress(gif_decompress_struct_pp gif_ptr_ptr) {
392 if (!gif_ptr_ptr || !*gif_ptr_ptr)
393 return;
394
395 gif_decompress_struct_p gif_ptr = *gif_ptr_ptr;
396 *gif_ptr_ptr = nullptr;
397 FX_Free(gif_ptr->global_pal_ptr);
398 delete gif_ptr->img_decoder_ptr;
399 if (gif_ptr->img_ptr_arr_ptr) {
400 size_t size_img_arr = gif_ptr->img_ptr_arr_ptr->size();
401 for (size_t i = 0; i < size_img_arr; i++) {
402 GifImage* p = (*gif_ptr->img_ptr_arr_ptr)[i];
403 FX_Free(p->image_info_ptr);
404 FX_Free(p->image_gce_ptr);
405 FX_Free(p->image_row_buf);
406 if (p->local_pal_ptr && p->local_pal_ptr != gif_ptr->global_pal_ptr) {
407 FX_Free(p->local_pal_ptr);
408 }
409 FX_Free(p);
410 }
411 gif_ptr->img_ptr_arr_ptr->clear();
412 delete gif_ptr->img_ptr_arr_ptr;
413 }
414 delete gif_ptr->cmt_data_ptr;
415 FX_Free(gif_ptr->gce_ptr);
416 if (gif_ptr->pt_ptr_arr_ptr) {
417 size_t size_pt_arr = gif_ptr->pt_ptr_arr_ptr->size();
418 for (size_t i = 0; i < size_pt_arr; i++) {
419 GifPlainText* p = (*gif_ptr->pt_ptr_arr_ptr)[i];
420 FX_Free(p->gce_ptr);
421 FX_Free(p->pte_ptr);
422 delete p->string_ptr;
423 FX_Free(p);
424 }
425 gif_ptr->pt_ptr_arr_ptr->clear();
426 delete gif_ptr->pt_ptr_arr_ptr;
427 }
428 FX_Free(gif_ptr);
429}
430gif_compress_struct_p gif_create_compress() {
431 gif_compress_struct_p gif_ptr = FX_Alloc(gif_compress_struct, 1);
432 FXSYS_memset(gif_ptr, 0, sizeof(gif_compress_struct));
433 gif_ptr->img_encoder_ptr = new CGifLZWEncoder;
434 gif_ptr->header_ptr = FX_Alloc(GifHeader, 1);
435 if (!gif_ptr->header_ptr) {
436 delete (gif_ptr->img_encoder_ptr);
437 FX_Free(gif_ptr);
438 return nullptr;
439 }
440 FXSYS_memcpy(gif_ptr->header_ptr->signature, GIF_SIGNATURE, 3);
441 FXSYS_memcpy(gif_ptr->header_ptr->version, "89a", 3);
442 gif_ptr->lsd_ptr = FX_Alloc(GifLSD, 1);
443 if (!gif_ptr->lsd_ptr) {
444 FX_Free(gif_ptr->header_ptr);
445 delete (gif_ptr->img_encoder_ptr);
446 FX_Free(gif_ptr);
447 return nullptr;
448 }
449 FXSYS_memset(gif_ptr->lsd_ptr, 0, sizeof(GifLSD));
450 gif_ptr->image_info_ptr = FX_Alloc(GifImageInfo, 1);
451 if (!gif_ptr->image_info_ptr) {
452 FX_Free(gif_ptr->lsd_ptr);
453 FX_Free(gif_ptr->header_ptr);
454 delete (gif_ptr->img_encoder_ptr);
455 FX_Free(gif_ptr);
456 return nullptr;
457 }
458 FXSYS_memset(gif_ptr->image_info_ptr, 0, sizeof(GifImageInfo));
459 gif_ptr->gce_ptr = FX_Alloc(GifGCE, 1);
460 if (!gif_ptr->gce_ptr) {
461 FX_Free(gif_ptr->image_info_ptr);
462 FX_Free(gif_ptr->lsd_ptr);
463 FX_Free(gif_ptr->header_ptr);
464 delete (gif_ptr->img_encoder_ptr);
465 FX_Free(gif_ptr);
466 return nullptr;
467 }
468 gif_ptr->pte_ptr = FX_Alloc(GifPTE, 1);
469 if (!gif_ptr->pte_ptr) {
470 FX_Free(gif_ptr->gce_ptr);
471 FX_Free(gif_ptr->image_info_ptr);
472 FX_Free(gif_ptr->lsd_ptr);
473 FX_Free(gif_ptr->header_ptr);
474 delete (gif_ptr->img_encoder_ptr);
475 FX_Free(gif_ptr);
476 return nullptr;
477 }
478 FXSYS_memset(gif_ptr->pte_ptr, 0, sizeof(GifPTE));
479 gif_ptr->pte_ptr->block_size = 12;
480 return gif_ptr;
481}
482void gif_destroy_compress(gif_compress_struct_pp gif_ptr_ptr) {
483 if (!gif_ptr_ptr || !*gif_ptr_ptr)
484 return;
485
486 gif_compress_struct_p gif_ptr = *gif_ptr_ptr;
487 *gif_ptr_ptr = nullptr;
488 FX_Free(gif_ptr->header_ptr);
489 FX_Free(gif_ptr->lsd_ptr);
490 FX_Free(gif_ptr->global_pal);
491 FX_Free(gif_ptr->image_info_ptr);
492 FX_Free(gif_ptr->local_pal);
493 delete gif_ptr->img_encoder_ptr;
494 FX_Free(gif_ptr->gce_ptr);
495 FX_Free(gif_ptr->cmt_data_ptr);
496 FX_Free(gif_ptr->pte_ptr);
497 FX_Free(gif_ptr);
498}
499void gif_error(gif_decompress_struct_p gif_ptr, const FX_CHAR* err_msg) {
500 if (gif_ptr && gif_ptr->gif_error_fn) {
501 gif_ptr->gif_error_fn(gif_ptr, err_msg);
502 }
503}
504void gif_warn(gif_decompress_struct_p gif_ptr, const FX_CHAR* err_msg) {}
505int32_t gif_read_header(gif_decompress_struct_p gif_ptr) {
506 if (!gif_ptr)
507 return 0;
508
509 uint32_t skip_size_org = gif_ptr->skip_size;
510 ASSERT(sizeof(GifHeader) == 6);
511 GifHeader* gif_header_ptr = nullptr;
512 if (!gif_read_data(gif_ptr, (uint8_t**)&gif_header_ptr, 6))
513 return 2;
514
515 if (FXSYS_strncmp(gif_header_ptr->signature, GIF_SIGNATURE, 3) != 0 ||
516 gif_header_ptr->version[0] != '8' || gif_header_ptr->version[2] != 'a') {
517 gif_error(gif_ptr, "Not A Gif Image");
518 return 0;
519 }
520 ASSERT(sizeof(GifLSD) == 7);
521 GifLSD* gif_lsd_ptr = nullptr;
522 if (!gif_read_data(gif_ptr, (uint8_t**)&gif_lsd_ptr, 7)) {
523 gif_ptr->skip_size = skip_size_org;
524 return 2;
525 }
526 if (((GifGF*)&gif_lsd_ptr->global_flag)->global_pal) {
527 gif_ptr->global_pal_num = 2
528 << ((GifGF*)&gif_lsd_ptr->global_flag)->pal_bits;
529 ASSERT(sizeof(GifPalette) == 3);
530 int32_t global_pal_size = gif_ptr->global_pal_num * 3;
531 uint8_t* global_pal_ptr = nullptr;
532 if (!gif_read_data(gif_ptr, &global_pal_ptr, global_pal_size)) {
533 gif_ptr->skip_size = skip_size_org;
534 return 2;
535 }
536 gif_ptr->global_sort_flag = ((GifGF*)&gif_lsd_ptr->global_flag)->sort_flag;
537 gif_ptr->global_color_resolution =
538 ((GifGF*)&gif_lsd_ptr->global_flag)->color_resolution;
539 FX_Free(gif_ptr->global_pal_ptr);
540 gif_ptr->global_pal_ptr = (GifPalette*)FX_Alloc(uint8_t, global_pal_size);
541 FXSYS_memcpy(gif_ptr->global_pal_ptr, global_pal_ptr, global_pal_size);
542 }
543 gif_ptr->width = (int)GetWord_LSBFirst((uint8_t*)&gif_lsd_ptr->width);
544 gif_ptr->height = (int)GetWord_LSBFirst((uint8_t*)&gif_lsd_ptr->height);
545 gif_ptr->bc_index = gif_lsd_ptr->bc_index;
546 gif_ptr->pixel_aspect = gif_lsd_ptr->pixel_aspect;
547 return 1;
548}
549int32_t gif_get_frame(gif_decompress_struct_p gif_ptr) {
550 if (!gif_ptr)
551 return 0;
552
553 int32_t ret = 1;
554 while (true) {
555 switch (gif_ptr->decode_status) {
556 case GIF_D_STATUS_TAIL:
557 return 1;
558 case GIF_D_STATUS_SIG: {
559 uint8_t* sig_ptr = nullptr;
560 if (!gif_read_data(gif_ptr, &sig_ptr, 1))
561 return 2;
562
563 switch (*sig_ptr) {
564 case GIF_SIG_EXTENSION:
565 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_EXT);
566 continue;
567 case GIF_SIG_IMAGE:
568 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_IMG_INFO);
569 continue;
570 case GIF_SIG_TRAILER:
571 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_TAIL);
572 return 1;
573 default:
574 if (gif_ptr->avail_in) {
575 gif_warn(gif_ptr, "The Gif File has non_standard Tag!");
576 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_SIG);
577 continue;
578 }
579 gif_warn(gif_ptr, "The Gif File Doesn't have Trailer Tag!");
580 return 1;
581 }
582 }
583 case GIF_D_STATUS_EXT: {
584 uint8_t* ext_ptr = nullptr;
585 if (!gif_read_data(gif_ptr, &ext_ptr, 1))
586 return 2;
587
588 switch (*ext_ptr) {
589 case GIF_BLOCK_CE:
590 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_EXT_CE);
591 continue;
592 case GIF_BLOCK_GCE:
593 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_EXT_GCE);
594 continue;
595 case GIF_BLOCK_PTE:
596 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_EXT_PTE);
597 continue;
598 default: {
599 int32_t status = GIF_D_STATUS_EXT_UNE;
600 if (*ext_ptr == GIF_BLOCK_PTE) {
601 status = GIF_D_STATUS_EXT_PTE;
602 }
603 gif_save_decoding_status(gif_ptr, status);
604 continue;
605 }
606 }
607 }
608 case GIF_D_STATUS_IMG_INFO: {
609 ret = gif_decode_image_info(gif_ptr);
610 if (ret != 1) {
611 return ret;
612 }
613 continue;
614 }
615 case GIF_D_STATUS_IMG_DATA: {
616 uint8_t* data_size_ptr = nullptr;
617 uint8_t* data_ptr = nullptr;
618 uint32_t skip_size_org = gif_ptr->skip_size;
619 if (!gif_read_data(gif_ptr, &data_size_ptr, 1))
620 return 2;
621
622 while (*data_size_ptr != GIF_BLOCK_TERMINAL) {
623 if (!gif_read_data(gif_ptr, &data_ptr, *data_size_ptr)) {
624 gif_ptr->skip_size = skip_size_org;
625 return 2;
626 }
627 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_IMG_DATA);
628 skip_size_org = gif_ptr->skip_size;
629 if (!gif_read_data(gif_ptr, &data_size_ptr, 1))
630 return 2;
631 }
632 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_SIG);
633 continue;
634 }
635 default: {
636 ret = gif_decode_extension(gif_ptr);
637 if (ret != 1) {
638 return ret;
639 }
640 continue;
641 }
642 }
643 }
644 return 1;
645}
646void gif_takeover_gce_ptr(gif_decompress_struct_p gif_ptr,
647 GifGCE** gce_ptr_ptr) {
648 *gce_ptr_ptr = nullptr;
649 if (gif_ptr->gce_ptr && gce_ptr_ptr) {
650 *gce_ptr_ptr = gif_ptr->gce_ptr;
651 gif_ptr->gce_ptr = nullptr;
652 }
653}
654int32_t gif_decode_extension(gif_decompress_struct_p gif_ptr) {
655 uint8_t* data_size_ptr = nullptr;
656 uint8_t* data_ptr = nullptr;
657 uint32_t skip_size_org = gif_ptr->skip_size;
658 switch (gif_ptr->decode_status) {
659 case GIF_D_STATUS_EXT_CE: {
660 if (!gif_read_data(gif_ptr, &data_size_ptr, 1)) {
661 gif_ptr->skip_size = skip_size_org;
662 return 2;
663 }
664 gif_ptr->cmt_data_ptr->clear();
665 while (*data_size_ptr != GIF_BLOCK_TERMINAL) {
666 uint8_t data_size = *data_size_ptr;
667 if (!gif_read_data(gif_ptr, &data_ptr, *data_size_ptr) ||
668 !gif_read_data(gif_ptr, &data_size_ptr, 1)) {
669 gif_ptr->skip_size = skip_size_org;
670 return 2;
671 }
672 *(gif_ptr->cmt_data_ptr) +=
673 CFX_ByteString((const FX_CHAR*)data_ptr, data_size);
674 }
675 } break;
676 case GIF_D_STATUS_EXT_PTE: {
677 ASSERT(sizeof(GifPTE) == 13);
678 GifPTE* gif_pte_ptr = nullptr;
679 if (!gif_read_data(gif_ptr, (uint8_t**)&gif_pte_ptr, 13)) {
680 return 2;
681 }
682 GifPlainText* gif_pt_ptr = FX_Alloc(GifPlainText, 1);
683 FXSYS_memset(gif_pt_ptr, 0, sizeof(GifPlainText));
684 gif_takeover_gce_ptr(gif_ptr, &gif_pt_ptr->gce_ptr);
685 gif_pt_ptr->pte_ptr = FX_Alloc(GifPTE, 1);
686 gif_pt_ptr->string_ptr = new CFX_ByteString;
687 gif_pt_ptr->pte_ptr->block_size = gif_pte_ptr->block_size;
688 gif_pt_ptr->pte_ptr->grid_left =
689 GetWord_LSBFirst((uint8_t*)&gif_pte_ptr->grid_left);
690 gif_pt_ptr->pte_ptr->grid_top =
691 GetWord_LSBFirst((uint8_t*)&gif_pte_ptr->grid_top);
692 gif_pt_ptr->pte_ptr->grid_width =
693 GetWord_LSBFirst((uint8_t*)&gif_pte_ptr->grid_width);
694 gif_pt_ptr->pte_ptr->grid_height =
695 GetWord_LSBFirst((uint8_t*)&gif_pte_ptr->grid_height);
696 gif_pt_ptr->pte_ptr->char_width = gif_pte_ptr->char_width;
697 gif_pt_ptr->pte_ptr->char_height = gif_pte_ptr->char_height;
698 gif_pt_ptr->pte_ptr->fc_index = gif_pte_ptr->fc_index;
699 gif_pt_ptr->pte_ptr->bc_index = gif_pte_ptr->bc_index;
700 if (!gif_read_data(gif_ptr, &data_size_ptr, 1)) {
701 gif_ptr->skip_size = skip_size_org;
702 if (gif_pt_ptr) {
703 FX_Free(gif_pt_ptr->gce_ptr);
704 FX_Free(gif_pt_ptr->pte_ptr);
705 delete gif_pt_ptr->string_ptr;
706 FX_Free(gif_pt_ptr);
707 }
708 return 2;
709 }
710 while (*data_size_ptr != GIF_BLOCK_TERMINAL) {
711 uint8_t data_size = *data_size_ptr;
712 if (!gif_read_data(gif_ptr, &data_ptr, *data_size_ptr) ||
713 !gif_read_data(gif_ptr, &data_size_ptr, 1)) {
714 gif_ptr->skip_size = skip_size_org;
715 if (gif_pt_ptr) {
716 FX_Free(gif_pt_ptr->gce_ptr);
717 FX_Free(gif_pt_ptr->pte_ptr);
718 delete gif_pt_ptr->string_ptr;
719 FX_Free(gif_pt_ptr);
720 }
721 return 2;
722 }
723 *(gif_pt_ptr->string_ptr) +=
724 CFX_ByteString((const FX_CHAR*)data_ptr, data_size);
725 }
726 gif_ptr->pt_ptr_arr_ptr->push_back(gif_pt_ptr);
727 } break;
728 case GIF_D_STATUS_EXT_GCE: {
729 ASSERT(sizeof(GifGCE) == 5);
730 GifGCE* gif_gce_ptr = nullptr;
731 if (!gif_read_data(gif_ptr, (uint8_t**)&gif_gce_ptr, 6))
732 return 2;
733
734 if (!gif_ptr->gce_ptr)
735 gif_ptr->gce_ptr = FX_Alloc(GifGCE, 1);
736 gif_ptr->gce_ptr->block_size = gif_gce_ptr->block_size;
737 gif_ptr->gce_ptr->gce_flag = gif_gce_ptr->gce_flag;
738 gif_ptr->gce_ptr->delay_time =
739 GetWord_LSBFirst((uint8_t*)&gif_gce_ptr->delay_time);
740 gif_ptr->gce_ptr->trans_index = gif_gce_ptr->trans_index;
741 } break;
742 default: {
743 if (gif_ptr->decode_status == GIF_D_STATUS_EXT_PTE) {
744 FX_Free(gif_ptr->gce_ptr);
745 gif_ptr->gce_ptr = nullptr;
746 }
747 if (!gif_read_data(gif_ptr, &data_size_ptr, 1))
748 return 2;
749
750 while (*data_size_ptr != GIF_BLOCK_TERMINAL) {
751 if (!gif_read_data(gif_ptr, &data_ptr, *data_size_ptr) ||
752 !gif_read_data(gif_ptr, &data_size_ptr, 1)) {
753 gif_ptr->skip_size = skip_size_org;
754 return 2;
755 }
756 }
757 }
758 }
759 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_SIG);
760 return 1;
761}
762int32_t gif_decode_image_info(gif_decompress_struct_p gif_ptr) {
763 if (gif_ptr->width == 0 || gif_ptr->height == 0) {
764 gif_error(gif_ptr, "No Image Header Info");
765 return 0;
766 }
767 uint32_t skip_size_org = gif_ptr->skip_size;
768 ASSERT(sizeof(GifImageInfo) == 9);
769 GifImageInfo* gif_img_info_ptr = nullptr;
770 if (!gif_read_data(gif_ptr, (uint8_t**)&gif_img_info_ptr, 9))
771 return 2;
772
773 GifImage* gif_image_ptr = FX_Alloc(GifImage, 1);
774 FXSYS_memset(gif_image_ptr, 0, sizeof(GifImage));
775 gif_image_ptr->image_info_ptr = FX_Alloc(GifImageInfo, 1);
776 gif_image_ptr->image_info_ptr->left =
777 GetWord_LSBFirst((uint8_t*)&gif_img_info_ptr->left);
778 gif_image_ptr->image_info_ptr->top =
779 GetWord_LSBFirst((uint8_t*)&gif_img_info_ptr->top);
780 gif_image_ptr->image_info_ptr->width =
781 GetWord_LSBFirst((uint8_t*)&gif_img_info_ptr->width);
782 gif_image_ptr->image_info_ptr->height =
783 GetWord_LSBFirst((uint8_t*)&gif_img_info_ptr->height);
784 gif_image_ptr->image_info_ptr->local_flag = gif_img_info_ptr->local_flag;
785 if (gif_image_ptr->image_info_ptr->left +
786 gif_image_ptr->image_info_ptr->width >
787 gif_ptr->width ||
788 gif_image_ptr->image_info_ptr->top +
789 gif_image_ptr->image_info_ptr->height >
790 gif_ptr->height) {
791 FX_Free(gif_image_ptr->image_info_ptr);
792 FX_Free(gif_image_ptr->image_row_buf);
793 FX_Free(gif_image_ptr);
794 gif_error(gif_ptr, "Image Data Out Of LSD, The File May Be Corrupt");
795 return 0;
796 }
797 GifLF* gif_img_info_lf_ptr = (GifLF*)&gif_img_info_ptr->local_flag;
798 if (gif_img_info_lf_ptr->local_pal) {
799 ASSERT(sizeof(GifPalette) == 3);
800 int32_t loc_pal_size = (2 << gif_img_info_lf_ptr->pal_bits) * 3;
801 uint8_t* loc_pal_ptr = nullptr;
802 if (!gif_read_data(gif_ptr, &loc_pal_ptr, loc_pal_size)) {
803 gif_ptr->skip_size = skip_size_org;
804 FX_Free(gif_image_ptr->image_info_ptr);
805 FX_Free(gif_image_ptr->image_row_buf);
806 FX_Free(gif_image_ptr);
807 return 2;
808 }
809 gif_image_ptr->local_pal_ptr =
810 (GifPalette*)gif_ptr->gif_ask_buf_for_pal_fn(gif_ptr, loc_pal_size);
811 if (gif_image_ptr->local_pal_ptr) {
812 FXSYS_memcpy((uint8_t*)gif_image_ptr->local_pal_ptr, loc_pal_ptr,
813 loc_pal_size);
814 }
815 }
816 uint8_t* code_size_ptr = nullptr;
817 if (!gif_read_data(gif_ptr, &code_size_ptr, 1)) {
818 gif_ptr->skip_size = skip_size_org;
819 FX_Free(gif_image_ptr->image_info_ptr);
820 FX_Free(gif_image_ptr->local_pal_ptr);
821 FX_Free(gif_image_ptr->image_row_buf);
822 FX_Free(gif_image_ptr);
823 return 2;
824 }
825 gif_image_ptr->image_code_size = *code_size_ptr;
826 gif_ptr->gif_record_current_position_fn(gif_ptr,
827 &gif_image_ptr->image_data_pos);
828 gif_image_ptr->image_data_pos += gif_ptr->skip_size;
829 gif_takeover_gce_ptr(gif_ptr, &gif_image_ptr->image_gce_ptr);
830 gif_ptr->img_ptr_arr_ptr->push_back(gif_image_ptr);
831 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_IMG_DATA);
832 return 1;
833}
834int32_t gif_load_frame(gif_decompress_struct_p gif_ptr, int32_t frame_num) {
835 if (!gif_ptr || frame_num < 0 ||
836 frame_num >= pdfium::CollectionSize<int>(*gif_ptr->img_ptr_arr_ptr)) {
837 return 0;
838 }
839 uint8_t* data_size_ptr = nullptr;
840 uint8_t* data_ptr = nullptr;
841 uint32_t skip_size_org = gif_ptr->skip_size;
842 GifImage* gif_image_ptr = (*gif_ptr->img_ptr_arr_ptr)[frame_num];
843 uint32_t gif_img_row_bytes = gif_image_ptr->image_info_ptr->width;
844 if (gif_img_row_bytes == 0) {
845 gif_error(gif_ptr, "Error Invalid Number of Row Bytes");
846 return 0;
847 }
848 if (gif_ptr->decode_status == GIF_D_STATUS_TAIL) {
849 if (gif_image_ptr->image_row_buf) {
850 FX_Free(gif_image_ptr->image_row_buf);
851 gif_image_ptr->image_row_buf = nullptr;
852 }
853 gif_image_ptr->image_row_buf = FX_Alloc(uint8_t, gif_img_row_bytes);
854 GifGCE* gif_img_gce_ptr = gif_image_ptr->image_gce_ptr;
855 int32_t loc_pal_num =
856 ((GifLF*)&gif_image_ptr->image_info_ptr->local_flag)->local_pal
857 ? (2 << ((GifLF*)&gif_image_ptr->image_info_ptr->local_flag)
858 ->pal_bits)
859 : 0;
860 gif_ptr->avail_in = 0;
861 if (!gif_img_gce_ptr) {
862 bool bRes = gif_ptr->gif_get_record_position_fn(
863 gif_ptr, gif_image_ptr->image_data_pos,
864 gif_image_ptr->image_info_ptr->left,
865 gif_image_ptr->image_info_ptr->top,
866 gif_image_ptr->image_info_ptr->width,
867 gif_image_ptr->image_info_ptr->height, loc_pal_num,
868 gif_image_ptr->local_pal_ptr, 0, 0, -1, 0,
869 (bool)((GifLF*)&gif_image_ptr->image_info_ptr->local_flag)
870 ->interlace);
871 if (!bRes) {
872 FX_Free(gif_image_ptr->image_row_buf);
873 gif_image_ptr->image_row_buf = nullptr;
874 gif_error(gif_ptr, "Error Read Record Position Data");
875 return 0;
876 }
877 } else {
878 bool bRes = gif_ptr->gif_get_record_position_fn(
879 gif_ptr, gif_image_ptr->image_data_pos,
880 gif_image_ptr->image_info_ptr->left,
881 gif_image_ptr->image_info_ptr->top,
882 gif_image_ptr->image_info_ptr->width,
883 gif_image_ptr->image_info_ptr->height, loc_pal_num,
884 gif_image_ptr->local_pal_ptr,
885 (int32_t)gif_image_ptr->image_gce_ptr->delay_time,
886 (bool)((GifCEF*)&gif_image_ptr->image_gce_ptr->gce_flag)->user_input,
887 ((GifCEF*)&gif_image_ptr->image_gce_ptr->gce_flag)->transparency
888 ? (int32_t)gif_image_ptr->image_gce_ptr->trans_index
889 : -1,
890 (int32_t)((GifCEF*)&gif_image_ptr->image_gce_ptr->gce_flag)
891 ->disposal_method,
892 (bool)((GifLF*)&gif_image_ptr->image_info_ptr->local_flag)
893 ->interlace);
894 if (!bRes) {
895 FX_Free(gif_image_ptr->image_row_buf);
896 gif_image_ptr->image_row_buf = nullptr;
897 gif_error(gif_ptr, "Error Read Record Position Data");
898 return 0;
899 }
900 }
901 if (gif_image_ptr->image_code_size >= 32) {
902 FX_Free(gif_image_ptr->image_row_buf);
903 gif_image_ptr->image_row_buf = nullptr;
904 gif_error(gif_ptr, "Error Invalid Code Size");
905 return 0;
906 }
907 if (!gif_ptr->img_decoder_ptr)
908 gif_ptr->img_decoder_ptr = new CGifLZWDecoder(gif_ptr->err_ptr);
909 gif_ptr->img_decoder_ptr->InitTable(gif_image_ptr->image_code_size);
910 gif_ptr->img_row_offset = 0;
911 gif_ptr->img_row_avail_size = 0;
912 gif_ptr->img_pass_num = 0;
913 gif_image_ptr->image_row_num = 0;
914 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_IMG_DATA);
915 }
916 CGifLZWDecoder* img_decoder_ptr = gif_ptr->img_decoder_ptr;
917 if (gif_ptr->decode_status == GIF_D_STATUS_IMG_DATA) {
918 if (!gif_read_data(gif_ptr, &data_size_ptr, 1))
919 return 2;
920
921 if (*data_size_ptr != GIF_BLOCK_TERMINAL) {
922 if (!gif_read_data(gif_ptr, &data_ptr, *data_size_ptr)) {
923 gif_ptr->skip_size = skip_size_org;
924 return 2;
925 }
926 img_decoder_ptr->Input(data_ptr, *data_size_ptr);
927 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_IMG_DATA);
928 gif_ptr->img_row_offset += gif_ptr->img_row_avail_size;
929 gif_ptr->img_row_avail_size = gif_img_row_bytes - gif_ptr->img_row_offset;
930 int32_t ret = img_decoder_ptr->Decode(
931 gif_image_ptr->image_row_buf + gif_ptr->img_row_offset,
932 gif_ptr->img_row_avail_size);
933 if (ret == 0) {
934 gif_decoding_failure_at_tail_cleanup(gif_ptr, gif_image_ptr);
935 return 0;
936 }
937 while (ret != 0) {
938 if (ret == 1) {
939 gif_ptr->gif_get_row_fn(gif_ptr, gif_image_ptr->image_row_num,
940 gif_image_ptr->image_row_buf);
941 FX_Free(gif_image_ptr->image_row_buf);
942 gif_image_ptr->image_row_buf = nullptr;
943 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_TAIL);
944 return 1;
945 }
946 if (ret == 2) {
947 ASSERT(img_decoder_ptr->GetAvailInput() == 0);
948 skip_size_org = gif_ptr->skip_size;
949 if (!gif_read_data(gif_ptr, &data_size_ptr, 1))
950 return 2;
951
952 if (*data_size_ptr != GIF_BLOCK_TERMINAL) {
953 if (!gif_read_data(gif_ptr, &data_ptr, *data_size_ptr)) {
954 gif_ptr->skip_size = skip_size_org;
955 return 2;
956 }
957 img_decoder_ptr->Input(data_ptr, *data_size_ptr);
958 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_IMG_DATA);
959 gif_ptr->img_row_offset += gif_ptr->img_row_avail_size;
960 gif_ptr->img_row_avail_size =
961 gif_img_row_bytes - gif_ptr->img_row_offset;
962 ret = img_decoder_ptr->Decode(
963 gif_image_ptr->image_row_buf + gif_ptr->img_row_offset,
964 gif_ptr->img_row_avail_size);
965 }
966 }
967 if (ret == 3) {
968 if (((GifLF*)&gif_image_ptr->image_info_ptr->local_flag)->interlace) {
969 gif_ptr->gif_get_row_fn(gif_ptr, gif_image_ptr->image_row_num,
970 gif_image_ptr->image_row_buf);
971 gif_image_ptr->image_row_num +=
972 s_gif_interlace_step[gif_ptr->img_pass_num];
973 if (gif_image_ptr->image_row_num >=
974 (int32_t)gif_image_ptr->image_info_ptr->height) {
975 gif_ptr->img_pass_num++;
976 if (gif_ptr->img_pass_num == FX_ArraySize(s_gif_interlace_step)) {
977 gif_decoding_failure_at_tail_cleanup(gif_ptr, gif_image_ptr);
978 return 0;
979 }
980 gif_image_ptr->image_row_num =
981 s_gif_interlace_step[gif_ptr->img_pass_num] / 2;
982 }
983 } else {
984 gif_ptr->gif_get_row_fn(gif_ptr, gif_image_ptr->image_row_num++,
985 gif_image_ptr->image_row_buf);
986 }
987 gif_ptr->img_row_offset = 0;
988 gif_ptr->img_row_avail_size = gif_img_row_bytes;
989 ret = img_decoder_ptr->Decode(
990 gif_image_ptr->image_row_buf + gif_ptr->img_row_offset,
991 gif_ptr->img_row_avail_size);
992 }
993 if (ret == 0) {
994 gif_decoding_failure_at_tail_cleanup(gif_ptr, gif_image_ptr);
995 return 0;
996 }
997 }
998 }
999 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_TAIL);
1000 }
1001 gif_error(gif_ptr, "Decode Image Data Error");
1002 return 0;
1003}
1004void gif_decoding_failure_at_tail_cleanup(gif_decompress_struct_p gif_ptr,
1005 GifImage* gif_image_ptr) {
1006 FX_Free(gif_image_ptr->image_row_buf);
1007 gif_image_ptr->image_row_buf = nullptr;
1008 gif_save_decoding_status(gif_ptr, GIF_D_STATUS_TAIL);
1009 gif_error(gif_ptr, "Decode Image Data Error");
1010}
1011void gif_save_decoding_status(gif_decompress_struct_p gif_ptr, int32_t status) {
1012 gif_ptr->decode_status = status;
1013 gif_ptr->next_in += gif_ptr->skip_size;
1014 gif_ptr->avail_in -= gif_ptr->skip_size;
1015 gif_ptr->skip_size = 0;
1016}
1017uint8_t* gif_read_data(gif_decompress_struct_p gif_ptr,
1018 uint8_t** des_buf_pp,
1019 uint32_t data_size) {
1020 if (!gif_ptr || gif_ptr->avail_in < gif_ptr->skip_size + data_size)
1021 return nullptr;
1022
1023 *des_buf_pp = gif_ptr->next_in + gif_ptr->skip_size;
1024 gif_ptr->skip_size += data_size;
1025 return *des_buf_pp;
1026}
1027void gif_input_buffer(gif_decompress_struct_p gif_ptr,
1028 uint8_t* src_buf,
1029 uint32_t src_size) {
1030 gif_ptr->next_in = src_buf;
1031 gif_ptr->avail_in = src_size;
1032 gif_ptr->skip_size = 0;
1033}
1034uint32_t gif_get_avail_input(gif_decompress_struct_p gif_ptr,
1035 uint8_t** avail_buf_ptr) {
1036 if (avail_buf_ptr) {
1037 *avail_buf_ptr = nullptr;
1038 if (gif_ptr->avail_in > 0) {
1039 *avail_buf_ptr = gif_ptr->next_in;
1040 }
1041 }
1042 return gif_ptr->avail_in;
1043}
1044int32_t gif_get_frame_num(gif_decompress_struct_p gif_ptr) {
1045 return pdfium::CollectionSize<int32_t>(*gif_ptr->img_ptr_arr_ptr);
1046}
1047static bool gif_write_header(gif_compress_struct_p gif_ptr,
1048 uint8_t*& dst_buf,
1049 uint32_t& dst_len) {
1050 if (gif_ptr->cur_offset) {
1051 return true;
1052 }
1053 dst_len = sizeof(GifHeader) + sizeof(GifLSD) + sizeof(GifGF);
1054 dst_buf = FX_TryAlloc(uint8_t, dst_len);
1055 if (!dst_buf)
1056 return false;
1057
1058 FXSYS_memset(dst_buf, 0, dst_len);
1059 FXSYS_memcpy(dst_buf, gif_ptr->header_ptr, sizeof(GifHeader));
1060 gif_ptr->cur_offset += sizeof(GifHeader);
1061 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset, gif_ptr->lsd_ptr->width);
1062 gif_ptr->cur_offset += 2;
1063 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset, gif_ptr->lsd_ptr->height);
1064 gif_ptr->cur_offset += 2;
1065 dst_buf[gif_ptr->cur_offset++] = gif_ptr->lsd_ptr->global_flag;
1066 dst_buf[gif_ptr->cur_offset++] = gif_ptr->lsd_ptr->bc_index;
1067 dst_buf[gif_ptr->cur_offset++] = gif_ptr->lsd_ptr->pixel_aspect;
1068 if (gif_ptr->global_pal) {
1069 uint16_t size = sizeof(GifPalette) * gif_ptr->gpal_num;
1070 if (!gif_grow_buf(dst_buf, dst_len, gif_ptr->cur_offset + size)) {
1071 return false;
1072 }
1073 FXSYS_memcpy(&dst_buf[gif_ptr->cur_offset], gif_ptr->global_pal, size);
1074 gif_ptr->cur_offset += size;
1075 }
1076 return true;
1077}
1078void interlace_buf(const uint8_t* buf, uint32_t pitch, uint32_t height) {
1079 std::vector<uint8_t*> pass[4];
1080 uint32_t row = 0;
1081 uint8_t* temp;
1082 while (row < height) {
1083 size_t j;
1084 if (row % 8 == 0) {
1085 j = 0;
1086 } else if (row % 4 == 0) {
1087 j = 1;
1088 } else if (row % 2 == 0) {
1089 j = 2;
1090 } else {
1091 j = 3;
1092 }
1093 temp = FX_Alloc(uint8_t, pitch);
1094 FXSYS_memcpy(temp, &buf[pitch * row], pitch);
1095 pass[j].push_back(temp);
1096 row++;
1097 }
1098 for (size_t i = 0, row = 0; i < 4; i++) {
1099 for (size_t j = 0; j < pass[i].size(); j++, row++) {
1100 FXSYS_memcpy((uint8_t*)&buf[pitch * row], pass[i][j], pitch);
1101 FX_Free(pass[i][j]);
1102 }
1103 }
1104}
1105static void gif_write_block_data(const uint8_t* src_buf,
1106 uint32_t src_len,
1107 uint8_t*& dst_buf,
1108 uint32_t& dst_len,
1109 uint32_t& dst_offset) {
1110 uint32_t src_offset = 0;
1111 while (src_len > GIF_DATA_BLOCK) {
1112 dst_buf[dst_offset++] = GIF_DATA_BLOCK;
1113 FXSYS_memcpy(&dst_buf[dst_offset], &src_buf[src_offset], GIF_DATA_BLOCK);
1114 dst_offset += GIF_DATA_BLOCK;
1115 src_offset += GIF_DATA_BLOCK;
1116 src_len -= GIF_DATA_BLOCK;
1117 }
1118 dst_buf[dst_offset++] = (uint8_t)src_len;
1119 FXSYS_memcpy(&dst_buf[dst_offset], &src_buf[src_offset], src_len);
1120 dst_offset += src_len;
1121}
1122static bool gif_write_data(gif_compress_struct_p gif_ptr,
1123 uint8_t*& dst_buf,
1124 uint32_t& dst_len) {
1125 if (!gif_grow_buf(dst_buf, dst_len, gif_ptr->cur_offset + GIF_DATA_BLOCK)) {
1126 return false;
1127 }
1128 if (FXSYS_memcmp(gif_ptr->header_ptr->version, "89a", 3) == 0) {
1129 dst_buf[gif_ptr->cur_offset++] = GIF_SIG_EXTENSION;
1130 dst_buf[gif_ptr->cur_offset++] = GIF_BLOCK_GCE;
1131 gif_ptr->gce_ptr->block_size = 4;
1132 dst_buf[gif_ptr->cur_offset++] = gif_ptr->gce_ptr->block_size;
1133 gif_ptr->gce_ptr->gce_flag = 0;
1134 dst_buf[gif_ptr->cur_offset++] = gif_ptr->gce_ptr->gce_flag;
1135 gif_ptr->gce_ptr->delay_time = 10;
1136 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset,
1137 gif_ptr->gce_ptr->delay_time);
1138 gif_ptr->cur_offset += 2;
1139 gif_ptr->gce_ptr->trans_index = 0;
1140 dst_buf[gif_ptr->cur_offset++] = gif_ptr->gce_ptr->trans_index;
1141 dst_buf[gif_ptr->cur_offset++] = 0;
1142 }
1143 dst_buf[gif_ptr->cur_offset++] = GIF_SIG_IMAGE;
1144 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset,
1145 gif_ptr->image_info_ptr->left);
1146 gif_ptr->cur_offset += 2;
1147 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset, gif_ptr->image_info_ptr->top);
1148 gif_ptr->cur_offset += 2;
1149 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset,
1150 gif_ptr->image_info_ptr->width);
1151 gif_ptr->cur_offset += 2;
1152 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset,
1153 gif_ptr->image_info_ptr->height);
1154 gif_ptr->cur_offset += 2;
1155 GifLF& lf = (GifLF&)gif_ptr->image_info_ptr->local_flag;
1156 dst_buf[gif_ptr->cur_offset++] = gif_ptr->image_info_ptr->local_flag;
1157 if (gif_ptr->local_pal) {
1158 uint32_t pal_size = sizeof(GifPalette) * gif_ptr->lpal_num;
1159 if (!gif_grow_buf(dst_buf, dst_len, pal_size + gif_ptr->cur_offset)) {
1160 return false;
1161 }
1162 FXSYS_memcpy(&dst_buf[gif_ptr->cur_offset], gif_ptr->local_pal, pal_size);
1163 gif_ptr->cur_offset += pal_size;
1164 }
1165 if (lf.interlace) {
1166 interlace_buf(gif_ptr->src_buf, gif_ptr->src_pitch,
1167 gif_ptr->image_info_ptr->height);
1168 }
1169 uint8_t code_bit = lf.pal_bits;
1170 if (lf.local_pal == 0) {
1171 GifGF& gf = (GifGF&)gif_ptr->lsd_ptr->global_flag;
1172 code_bit = gf.pal_bits;
1173 }
1174 if (code_bit >= 31)
1175 return false;
1176 gif_ptr->img_encoder_ptr->Start(code_bit, gif_ptr->src_buf, dst_buf,
1177 gif_ptr->cur_offset);
1178 uint32_t i;
1179 for (i = 0; i < gif_ptr->src_row; i++) {
1180 if (!gif_ptr->img_encoder_ptr->Encode(
1181 &gif_ptr->src_buf[i * gif_ptr->src_pitch],
1182 gif_ptr->src_width * (code_bit + 1), dst_buf, dst_len,
1183 gif_ptr->cur_offset)) {
1184 return false;
1185 }
1186 }
1187 gif_ptr->img_encoder_ptr->Finish(dst_buf, dst_len, gif_ptr->cur_offset);
1188 dst_buf[gif_ptr->cur_offset++] = 0;
1189 if (FXSYS_memcmp(gif_ptr->header_ptr->version, "89a", 3) == 0 &&
1190 gif_ptr->cmt_data_ptr) {
1191 dst_buf[gif_ptr->cur_offset++] = GIF_SIG_EXTENSION;
1192 dst_buf[gif_ptr->cur_offset++] = GIF_BLOCK_CE;
1193 gif_write_block_data(gif_ptr->cmt_data_ptr, gif_ptr->cmt_data_len, dst_buf,
1194 dst_len, gif_ptr->cur_offset);
1195 dst_buf[gif_ptr->cur_offset++] = 0;
1196 }
1197 if (FXSYS_memcmp(gif_ptr->header_ptr->version, "89a", 3) == 0 &&
1198 gif_ptr->pte_data_ptr) {
1199 dst_buf[gif_ptr->cur_offset++] = GIF_SIG_EXTENSION;
1200 dst_buf[gif_ptr->cur_offset++] = GIF_BLOCK_PTE;
1201 dst_buf[gif_ptr->cur_offset++] = gif_ptr->pte_ptr->block_size;
1202 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset,
1203 gif_ptr->pte_ptr->grid_left);
1204 gif_ptr->cur_offset += 2;
1205 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset, gif_ptr->pte_ptr->grid_top);
1206 gif_ptr->cur_offset += 2;
1207 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset,
1208 gif_ptr->pte_ptr->grid_width);
1209 gif_ptr->cur_offset += 2;
1210 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset,
1211 gif_ptr->pte_ptr->grid_height);
1212 gif_ptr->cur_offset += 2;
1213 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset,
1214 gif_ptr->pte_ptr->char_width);
1215 gif_ptr->cur_offset += 2;
1216 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset,
1217 gif_ptr->pte_ptr->char_height);
1218 gif_ptr->cur_offset += 2;
1219 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset, gif_ptr->pte_ptr->fc_index);
1220 gif_ptr->cur_offset += 2;
1221 SetWord_LSBFirst(dst_buf + gif_ptr->cur_offset, gif_ptr->pte_ptr->bc_index);
1222 gif_ptr->cur_offset += 2;
1223 gif_write_block_data(gif_ptr->pte_data_ptr, gif_ptr->pte_data_len, dst_buf,
1224 dst_len, gif_ptr->cur_offset);
1225 gif_ptr->cur_offset += gif_ptr->pte_data_len;
1226 dst_buf[gif_ptr->cur_offset++] = 0;
1227 }
1228 dst_buf[gif_ptr->cur_offset++] = GIF_SIG_TRAILER;
1229 return true;
1230}
1231bool gif_encode(gif_compress_struct_p gif_ptr,
1232 uint8_t*& dst_buf,
1233 uint32_t& dst_len) {
1234 if (!gif_write_header(gif_ptr, dst_buf, dst_len)) {
1235 return false;
1236 }
1237 uint32_t cur_offset = gif_ptr->cur_offset;
1238 bool res = true;
1239 if (gif_ptr->frames) {
1240 gif_ptr->cur_offset--;
1241 }
1242 if (!gif_write_data(gif_ptr, dst_buf, dst_len)) {
1243 gif_ptr->cur_offset = cur_offset;
1244 res = false;
1245 }
1246 dst_len = gif_ptr->cur_offset;
1247 dst_buf[dst_len - 1] = GIF_SIG_TRAILER;
1248 if (res) {
1249 gif_ptr->frames++;
1250 }
1251 return res;
1252}