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