Cleanup casting of FX_Alloc() return values.
[pdfium.git] / core / src / fxcodec / codec / fx_codec_jbig.cpp
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 "../../../include/fxcodec/fx_codec.h"
8 #include "codec_int.h"
9
10 CCodec_Jbig2Context::CCodec_Jbig2Context() {
11   FXSYS_memset(this, 0, sizeof(CCodec_Jbig2Context));
12 }
13 CCodec_Jbig2Module::~CCodec_Jbig2Module() {}
14 void* CCodec_Jbig2Module::CreateJbig2Context() {
15   return new CCodec_Jbig2Context();
16 }
17 void CCodec_Jbig2Module::DestroyJbig2Context(void* pJbig2Content) {
18   if (pJbig2Content) {
19     CJBig2_Context::DestroyContext(
20         ((CCodec_Jbig2Context*)pJbig2Content)->m_pContext);
21     delete (CCodec_Jbig2Context*)pJbig2Content;
22   }
23   pJbig2Content = NULL;
24 }
25 FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(void* pJbig2Context,
26                                                FX_DWORD width,
27                                                FX_DWORD height,
28                                                const uint8_t* src_buf,
29                                                FX_DWORD src_size,
30                                                const uint8_t* global_data,
31                                                FX_DWORD global_size,
32                                                uint8_t* dest_buf,
33                                                FX_DWORD dest_pitch,
34                                                IFX_Pause* pPause) {
35   if (!pJbig2Context) {
36     return FXCODEC_STATUS_ERR_PARAMS;
37   }
38   CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
39   m_pJbig2Context->m_width = width;
40   m_pJbig2Context->m_height = height;
41   m_pJbig2Context->m_src_buf = (unsigned char*)src_buf;
42   m_pJbig2Context->m_src_size = src_size;
43   m_pJbig2Context->m_global_data = global_data;
44   m_pJbig2Context->m_global_size = global_size;
45   m_pJbig2Context->m_dest_buf = dest_buf;
46   m_pJbig2Context->m_dest_pitch = dest_pitch;
47   m_pJbig2Context->m_pPause = pPause;
48   m_pJbig2Context->m_bFileReader = FALSE;
49   FXSYS_memset(dest_buf, 0, height * dest_pitch);
50   m_pJbig2Context->m_pContext = CJBig2_Context::CreateContext(
51       global_data, global_size, src_buf, src_size, JBIG2_EMBED_STREAM,
52       &m_SymbolDictCache, pPause);
53   if (!m_pJbig2Context->m_pContext) {
54     return FXCODEC_STATUS_ERROR;
55   }
56   int ret = m_pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height,
57                                                       dest_pitch, pPause);
58   if (m_pJbig2Context->m_pContext->GetProcessiveStatus() ==
59       FXCODEC_STATUS_DECODE_FINISH) {
60     CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
61     m_pJbig2Context->m_pContext = NULL;
62     if (ret != JBIG2_SUCCESS) {
63       return FXCODEC_STATUS_ERROR;
64     }
65     int dword_size = height * dest_pitch / 4;
66     FX_DWORD* dword_buf = (FX_DWORD*)dest_buf;
67     for (int i = 0; i < dword_size; i++) {
68       dword_buf[i] = ~dword_buf[i];
69     }
70     return FXCODEC_STATUS_DECODE_FINISH;
71   }
72   return m_pJbig2Context->m_pContext->GetProcessiveStatus();
73 }
74 FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(void* pJbig2Context,
75                                                   IFX_Pause* pPause) {
76   CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
77   int ret = m_pJbig2Context->m_pContext->Continue(pPause);
78   if (m_pJbig2Context->m_pContext->GetProcessiveStatus() !=
79       FXCODEC_STATUS_DECODE_FINISH) {
80     return m_pJbig2Context->m_pContext->GetProcessiveStatus();
81   }
82   if (m_pJbig2Context->m_bFileReader) {
83     CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
84     m_pJbig2Context->m_pContext = NULL;
85     if (ret != JBIG2_SUCCESS) {
86       FX_Free(m_pJbig2Context->m_src_buf);
87       m_pJbig2Context->m_src_buf = NULL;
88       return FXCODEC_STATUS_ERROR;
89     }
90     delete m_pJbig2Context->m_dest_image;
91     FX_Free(m_pJbig2Context->m_src_buf);
92     return FXCODEC_STATUS_DECODE_FINISH;
93   }
94   CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
95   m_pJbig2Context->m_pContext = NULL;
96   if (ret != JBIG2_SUCCESS) {
97     return FXCODEC_STATUS_ERROR;
98   }
99   int dword_size =
100       m_pJbig2Context->m_height * m_pJbig2Context->m_dest_pitch / 4;
101   FX_DWORD* dword_buf = (FX_DWORD*)m_pJbig2Context->m_dest_buf;
102   for (int i = 0; i < dword_size; i++) {
103     dword_buf[i] = ~dword_buf[i];
104   }
105   return FXCODEC_STATUS_DECODE_FINISH;
106 }