| 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 #include "core/include/fxcodec/fx_codec.h" | |
| 8 #include "core/include/fxge/fx_dib.h" | |
| 9 #include "core/src/fxcodec/codec/codec_int.h" | |
| 10 #include "core/src/fxcodec/lgif/fx_gif.h" | |
| 11 struct FXGIF_Context { | |
| 12 gif_decompress_struct_p gif_ptr; | |
| 13 void* parent_ptr; | |
| 14 void* child_ptr; | |
| 15 | |
| 16 void* (*m_AllocFunc)(unsigned int); | |
| 17 void (*m_FreeFunc)(void*); | |
| 18 }; | |
| 19 extern "C" { | |
| 20 static void* gif_alloc_func(unsigned int size) { | |
| 21 return FX_Alloc(char, size); | |
| 22 } | |
| 23 static void gif_free_func(void* p) { | |
| 24 FX_Free(p); | |
| 25 } | |
| 26 }; | |
| 27 static void gif_error_data(gif_decompress_struct_p gif_ptr, | |
| 28 const FX_CHAR* err_msg) { | |
| 29 FXSYS_strncpy((char*)gif_ptr->err_ptr, err_msg, GIF_MAX_ERROR_SIZE - 1); | |
| 30 longjmp(gif_ptr->jmpbuf, 1); | |
| 31 } | |
| 32 static uint8_t* gif_ask_buf_for_pal(gif_decompress_struct_p gif_ptr, | |
| 33 int32_t pal_size) { | |
| 34 FXGIF_Context* p = (FXGIF_Context*)gif_ptr->context_ptr; | |
| 35 CCodec_GifModule* pModule = (CCodec_GifModule*)p->parent_ptr; | |
| 36 return pModule->AskLocalPaletteBufCallback( | |
| 37 p->child_ptr, gif_get_frame_num(gif_ptr), pal_size); | |
| 38 } | |
| 39 static void gif_record_current_position(gif_decompress_struct_p gif_ptr, | |
| 40 FX_DWORD* cur_pos_ptr) { | |
| 41 FXGIF_Context* p = (FXGIF_Context*)gif_ptr->context_ptr; | |
| 42 CCodec_GifModule* pModule = (CCodec_GifModule*)p->parent_ptr; | |
| 43 pModule->RecordCurrentPositionCallback(p->child_ptr, *cur_pos_ptr); | |
| 44 } | |
| 45 static void gif_read_scanline(gif_decompress_struct_p gif_ptr, | |
| 46 int32_t row_num, | |
| 47 uint8_t* row_buf) { | |
| 48 FXGIF_Context* p = (FXGIF_Context*)gif_ptr->context_ptr; | |
| 49 CCodec_GifModule* pModule = (CCodec_GifModule*)p->parent_ptr; | |
| 50 pModule->ReadScanlineCallback(p->child_ptr, row_num, row_buf); | |
| 51 } | |
| 52 static FX_BOOL gif_get_record_position(gif_decompress_struct_p gif_ptr, | |
| 53 FX_DWORD cur_pos, | |
| 54 int32_t left, | |
| 55 int32_t top, | |
| 56 int32_t width, | |
| 57 int32_t height, | |
| 58 int32_t pal_num, | |
| 59 void* pal_ptr, | |
| 60 int32_t delay_time, | |
| 61 FX_BOOL user_input, | |
| 62 int32_t trans_index, | |
| 63 int32_t disposal_method, | |
| 64 FX_BOOL interlace) { | |
| 65 FXGIF_Context* p = (FXGIF_Context*)gif_ptr->context_ptr; | |
| 66 CCodec_GifModule* pModule = (CCodec_GifModule*)p->parent_ptr; | |
| 67 return pModule->InputRecordPositionBufCallback( | |
| 68 p->child_ptr, cur_pos, FX_RECT(left, top, left + width, top + height), | |
| 69 pal_num, pal_ptr, delay_time, user_input, trans_index, disposal_method, | |
| 70 interlace); | |
| 71 } | |
| 72 void* CCodec_GifModule::Start(void* pModule) { | |
| 73 FXGIF_Context* p = (FXGIF_Context*)FX_Alloc(uint8_t, sizeof(FXGIF_Context)); | |
| 74 if (p == NULL) { | |
| 75 return NULL; | |
| 76 } | |
| 77 FXSYS_memset(p, 0, sizeof(FXGIF_Context)); | |
| 78 p->m_AllocFunc = gif_alloc_func; | |
| 79 p->m_FreeFunc = gif_free_func; | |
| 80 p->gif_ptr = NULL; | |
| 81 p->parent_ptr = (void*)this; | |
| 82 p->child_ptr = pModule; | |
| 83 p->gif_ptr = gif_create_decompress(); | |
| 84 if (p->gif_ptr == NULL) { | |
| 85 FX_Free(p); | |
| 86 return NULL; | |
| 87 } | |
| 88 p->gif_ptr->context_ptr = (void*)p; | |
| 89 p->gif_ptr->err_ptr = m_szLastError; | |
| 90 p->gif_ptr->gif_error_fn = gif_error_data; | |
| 91 p->gif_ptr->gif_ask_buf_for_pal_fn = gif_ask_buf_for_pal; | |
| 92 p->gif_ptr->gif_record_current_position_fn = gif_record_current_position; | |
| 93 p->gif_ptr->gif_get_row_fn = gif_read_scanline; | |
| 94 p->gif_ptr->gif_get_record_position_fn = gif_get_record_position; | |
| 95 return p; | |
| 96 } | |
| 97 void CCodec_GifModule::Finish(void* pContext) { | |
| 98 FXGIF_Context* p = (FXGIF_Context*)pContext; | |
| 99 if (p) { | |
| 100 gif_destroy_decompress(&p->gif_ptr); | |
| 101 p->m_FreeFunc(p); | |
| 102 } | |
| 103 } | |
| 104 int32_t CCodec_GifModule::ReadHeader(void* pContext, | |
| 105 int* width, | |
| 106 int* height, | |
| 107 int* pal_num, | |
| 108 void** pal_pp, | |
| 109 int* bg_index, | |
| 110 CFX_DIBAttribute* pAttribute) { | |
| 111 FXGIF_Context* p = (FXGIF_Context*)pContext; | |
| 112 if (setjmp(p->gif_ptr->jmpbuf)) { | |
| 113 return 0; | |
| 114 } | |
| 115 int32_t ret = gif_read_header(p->gif_ptr); | |
| 116 if (ret != 1) { | |
| 117 return ret; | |
| 118 } | |
| 119 if (pAttribute) { | |
| 120 } | |
| 121 *width = p->gif_ptr->width; | |
| 122 *height = p->gif_ptr->height; | |
| 123 *pal_num = p->gif_ptr->global_pal_num; | |
| 124 *pal_pp = p->gif_ptr->global_pal_ptr; | |
| 125 *bg_index = p->gif_ptr->bc_index; | |
| 126 return 1; | |
| 127 } | |
| 128 int32_t CCodec_GifModule::LoadFrameInfo(void* pContext, int* frame_num) { | |
| 129 FXGIF_Context* p = (FXGIF_Context*)pContext; | |
| 130 if (setjmp(p->gif_ptr->jmpbuf)) { | |
| 131 return 0; | |
| 132 } | |
| 133 int32_t ret = gif_get_frame(p->gif_ptr); | |
| 134 if (ret != 1) { | |
| 135 return ret; | |
| 136 } | |
| 137 *frame_num = gif_get_frame_num(p->gif_ptr); | |
| 138 return 1; | |
| 139 } | |
| 140 int32_t CCodec_GifModule::LoadFrame(void* pContext, | |
| 141 int frame_num, | |
| 142 CFX_DIBAttribute* pAttribute) { | |
| 143 FXGIF_Context* p = (FXGIF_Context*)pContext; | |
| 144 if (setjmp(p->gif_ptr->jmpbuf)) { | |
| 145 return 0; | |
| 146 } | |
| 147 int32_t ret = gif_load_frame(p->gif_ptr, frame_num); | |
| 148 if (ret == 1) { | |
| 149 if (pAttribute) { | |
| 150 pAttribute->m_nGifLeft = | |
| 151 p->gif_ptr->img_ptr_arr_ptr->GetAt(frame_num)->image_info_ptr->left; | |
| 152 pAttribute->m_nGifTop = | |
| 153 p->gif_ptr->img_ptr_arr_ptr->GetAt(frame_num)->image_info_ptr->top; | |
| 154 pAttribute->m_fAspectRatio = p->gif_ptr->pixel_aspect; | |
| 155 if (p->gif_ptr->cmt_data_ptr) { | |
| 156 const uint8_t* buf = | |
| 157 (const uint8_t*)p->gif_ptr->cmt_data_ptr->GetBuffer(0); | |
| 158 FX_DWORD len = p->gif_ptr->cmt_data_ptr->GetLength(); | |
| 159 if (len > 21) { | |
| 160 uint8_t size = *buf++; | |
| 161 if (size) { | |
| 162 pAttribute->m_strAuthor = CFX_ByteString(buf, size); | |
| 163 } else { | |
| 164 pAttribute->m_strAuthor.Empty(); | |
| 165 } | |
| 166 buf += size; | |
| 167 size = *buf++; | |
| 168 if (size == 20) { | |
| 169 FXSYS_memcpy(pAttribute->m_strTime, buf, size); | |
| 170 } | |
| 171 } | |
| 172 } | |
| 173 } | |
| 174 } | |
| 175 return ret; | |
| 176 } | |
| 177 FX_DWORD CCodec_GifModule::GetAvailInput(void* pContext, | |
| 178 uint8_t** avial_buf_ptr) { | |
| 179 FXGIF_Context* p = (FXGIF_Context*)pContext; | |
| 180 return gif_get_avail_input(p->gif_ptr, avial_buf_ptr); | |
| 181 } | |
| 182 void CCodec_GifModule::Input(void* pContext, | |
| 183 const uint8_t* src_buf, | |
| 184 FX_DWORD src_size) { | |
| 185 FXGIF_Context* p = (FXGIF_Context*)pContext; | |
| 186 gif_input_buffer(p->gif_ptr, (uint8_t*)src_buf, src_size); | |
| 187 } | |
| OLD | NEW |