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/lbmp/fx_bmp.h" | |
11 struct FXBMP_Context { | |
12 bmp_decompress_struct_p bmp_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* bmp_alloc_func(unsigned int size) { | |
21 return FX_Alloc(char, size); | |
22 } | |
23 static void bmp_free_func(void* p) { | |
24 FX_Free(p); | |
25 } | |
26 }; | |
27 static void bmp_error_data(bmp_decompress_struct_p bmp_ptr, | |
28 const FX_CHAR* err_msg) { | |
29 FXSYS_strncpy((char*)bmp_ptr->err_ptr, err_msg, BMP_MAX_ERROR_SIZE - 1); | |
30 longjmp(bmp_ptr->jmpbuf, 1); | |
31 } | |
32 static void bmp_read_scanline(bmp_decompress_struct_p bmp_ptr, | |
33 int32_t row_num, | |
34 uint8_t* row_buf) { | |
35 FXBMP_Context* p = (FXBMP_Context*)bmp_ptr->context_ptr; | |
36 CCodec_BmpModule* pModule = (CCodec_BmpModule*)p->parent_ptr; | |
37 pModule->ReadScanlineCallback(p->child_ptr, row_num, row_buf); | |
38 } | |
39 static FX_BOOL bmp_get_data_position(bmp_decompress_struct_p bmp_ptr, | |
40 FX_DWORD rcd_pos) { | |
41 FXBMP_Context* p = (FXBMP_Context*)bmp_ptr->context_ptr; | |
42 CCodec_BmpModule* pModule = (CCodec_BmpModule*)p->parent_ptr; | |
43 return pModule->InputImagePositionBufCallback(p->child_ptr, rcd_pos); | |
44 } | |
45 void* CCodec_BmpModule::Start(void* pModule) { | |
46 FXBMP_Context* p = (FXBMP_Context*)FX_Alloc(uint8_t, sizeof(FXBMP_Context)); | |
47 if (p == NULL) { | |
48 return NULL; | |
49 } | |
50 FXSYS_memset(p, 0, sizeof(FXBMP_Context)); | |
51 if (p == NULL) { | |
52 return NULL; | |
53 } | |
54 p->m_AllocFunc = bmp_alloc_func; | |
55 p->m_FreeFunc = bmp_free_func; | |
56 p->bmp_ptr = NULL; | |
57 p->parent_ptr = (void*)this; | |
58 p->child_ptr = pModule; | |
59 p->bmp_ptr = bmp_create_decompress(); | |
60 if (p->bmp_ptr == NULL) { | |
61 FX_Free(p); | |
62 return NULL; | |
63 } | |
64 p->bmp_ptr->context_ptr = (void*)p; | |
65 p->bmp_ptr->err_ptr = m_szLastError; | |
66 p->bmp_ptr->bmp_error_fn = bmp_error_data; | |
67 p->bmp_ptr->bmp_get_row_fn = bmp_read_scanline; | |
68 p->bmp_ptr->bmp_get_data_position_fn = bmp_get_data_position; | |
69 return p; | |
70 } | |
71 void CCodec_BmpModule::Finish(void* pContext) { | |
72 FXBMP_Context* p = (FXBMP_Context*)pContext; | |
73 if (p) { | |
74 bmp_destroy_decompress(&p->bmp_ptr); | |
75 p->m_FreeFunc(p); | |
76 } | |
77 } | |
78 int32_t CCodec_BmpModule::ReadHeader(void* pContext, | |
79 int32_t* width, | |
80 int32_t* height, | |
81 FX_BOOL* tb_flag, | |
82 int32_t* components, | |
83 int32_t* pal_num, | |
84 FX_DWORD** pal_pp, | |
85 CFX_DIBAttribute* pAttribute) { | |
86 FXBMP_Context* p = (FXBMP_Context*)pContext; | |
87 if (setjmp(p->bmp_ptr->jmpbuf)) { | |
88 return 0; | |
89 } | |
90 int32_t ret = bmp_read_header(p->bmp_ptr); | |
91 if (ret != 1) { | |
92 return ret; | |
93 } | |
94 *width = p->bmp_ptr->width; | |
95 *height = p->bmp_ptr->height; | |
96 *tb_flag = p->bmp_ptr->imgTB_flag; | |
97 *components = p->bmp_ptr->components; | |
98 *pal_num = p->bmp_ptr->pal_num; | |
99 *pal_pp = p->bmp_ptr->pal_ptr; | |
100 if (pAttribute) { | |
101 pAttribute->m_wDPIUnit = FXCODEC_RESUNIT_METER; | |
102 pAttribute->m_nXDPI = p->bmp_ptr->dpi_x; | |
103 pAttribute->m_nYDPI = p->bmp_ptr->dpi_y; | |
104 pAttribute->m_nBmpCompressType = p->bmp_ptr->compress_flag; | |
105 } | |
106 return 1; | |
107 } | |
108 int32_t CCodec_BmpModule::LoadImage(void* pContext) { | |
109 FXBMP_Context* p = (FXBMP_Context*)pContext; | |
110 if (setjmp(p->bmp_ptr->jmpbuf)) { | |
111 return 0; | |
112 } | |
113 return bmp_decode_image(p->bmp_ptr); | |
114 } | |
115 FX_DWORD CCodec_BmpModule::GetAvailInput(void* pContext, | |
116 uint8_t** avial_buf_ptr) { | |
117 FXBMP_Context* p = (FXBMP_Context*)pContext; | |
118 return bmp_get_avail_input(p->bmp_ptr, avial_buf_ptr); | |
119 } | |
120 void CCodec_BmpModule::Input(void* pContext, | |
121 const uint8_t* src_buf, | |
122 FX_DWORD src_size) { | |
123 FXBMP_Context* p = (FXBMP_Context*)pContext; | |
124 bmp_input_buffer(p->bmp_ptr, (uint8_t*)src_buf, src_size); | |
125 } | |
OLD | NEW |