Home | History | Annotate | Download | only in codec
      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/fxcodec/codec/ccodec_jbig2module.h"
      8 
      9 #include <list>
     10 #include <memory>
     11 
     12 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
     13 #include "core/fxcodec/JBig2_DocumentContext.h"
     14 #include "core/fxcodec/jbig2/JBig2_Context.h"
     15 #include "core/fxcodec/jbig2/JBig2_Image.h"
     16 #include "core/fxcrt/fx_memory.h"
     17 #include "third_party/base/ptr_util.h"
     18 
     19 JBig2_DocumentContext::JBig2_DocumentContext() {}
     20 
     21 JBig2_DocumentContext::~JBig2_DocumentContext() {}
     22 
     23 JBig2_DocumentContext* GetJBig2DocumentContext(
     24     std::unique_ptr<JBig2_DocumentContext>* pContextHolder) {
     25   if (!pContextHolder->get())
     26     *pContextHolder = pdfium::MakeUnique<JBig2_DocumentContext>();
     27   return pContextHolder->get();
     28 }
     29 
     30 CCodec_Jbig2Context::CCodec_Jbig2Context()
     31     : m_width(0),
     32       m_height(0),
     33       m_pGlobalStream(nullptr),
     34       m_pSrcStream(nullptr),
     35       m_dest_buf(0),
     36       m_dest_pitch(0) {}
     37 
     38 CCodec_Jbig2Context::~CCodec_Jbig2Context() {}
     39 
     40 CCodec_Jbig2Module::~CCodec_Jbig2Module() {}
     41 
     42 FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(
     43     CCodec_Jbig2Context* pJbig2Context,
     44     std::unique_ptr<JBig2_DocumentContext>* pContextHolder,
     45     uint32_t width,
     46     uint32_t height,
     47     const RetainPtr<CPDF_StreamAcc>& src_stream,
     48     const RetainPtr<CPDF_StreamAcc>& global_stream,
     49     uint8_t* dest_buf,
     50     uint32_t dest_pitch,
     51     IFX_PauseIndicator* pPause) {
     52   if (!pJbig2Context)
     53     return FXCODEC_STATUS_ERR_PARAMS;
     54 
     55   JBig2_DocumentContext* pJBig2DocumentContext =
     56       GetJBig2DocumentContext(pContextHolder);
     57   pJbig2Context->m_width = width;
     58   pJbig2Context->m_height = height;
     59   pJbig2Context->m_pSrcStream = src_stream;
     60   pJbig2Context->m_pGlobalStream = global_stream;
     61   pJbig2Context->m_dest_buf = dest_buf;
     62   pJbig2Context->m_dest_pitch = dest_pitch;
     63   memset(dest_buf, 0, height * dest_pitch);
     64   pJbig2Context->m_pContext = pdfium::MakeUnique<CJBig2_Context>(
     65       global_stream, src_stream, pJBig2DocumentContext->GetSymbolDictCache(),
     66       false);
     67   int ret = pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height,
     68                                                     dest_pitch, pPause);
     69   return Decode(pJbig2Context, ret);
     70 }
     71 
     72 FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(
     73     CCodec_Jbig2Context* pJbig2Context,
     74     IFX_PauseIndicator* pPause) {
     75   int ret = pJbig2Context->m_pContext->Continue(pPause);
     76   return Decode(pJbig2Context, ret);
     77 }
     78 
     79 FXCODEC_STATUS CCodec_Jbig2Module::Decode(CCodec_Jbig2Context* pJbig2Context,
     80                                           int result) {
     81   FXCODEC_STATUS status = pJbig2Context->m_pContext->GetProcessingStatus();
     82   if (status != FXCODEC_STATUS_DECODE_FINISH)
     83     return status;
     84 
     85   pJbig2Context->m_pContext.reset();
     86   if (result != JBIG2_SUCCESS)
     87     return FXCODEC_STATUS_ERROR;
     88 
     89   int dword_size = pJbig2Context->m_height * pJbig2Context->m_dest_pitch / 4;
     90   uint32_t* dword_buf = reinterpret_cast<uint32_t*>(pJbig2Context->m_dest_buf);
     91   for (int i = 0; i < dword_size; i++)
     92     dword_buf[i] = ~dword_buf[i];
     93   return FXCODEC_STATUS_DECODE_FINISH;
     94 }
     95