summaryrefslogtreecommitdiff
path: root/core/fxcodec/codec/fx_codec_jbig.cpp
blob: 4876cef53d5125748d1deffe2e8d48a9b4a73a03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "core/fxcodec/codec/ccodec_jbig2module.h"

#include <list>

#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h"
#include "core/fxcodec/jbig2/JBig2_Context.h"
#include "core/fxcodec/jbig2/JBig2_Image.h"
#include "core/fxcrt/include/fx_memory.h"

// Holds per-document JBig2 related data.
class JBig2DocumentContext : public CFX_Deletable {
 public:
  std::list<CJBig2_CachePair>* GetSymbolDictCache() {
    return &m_SymbolDictCache;
  }

  ~JBig2DocumentContext() override {
    for (auto it : m_SymbolDictCache) {
      delete it.second;
    }
  }

 private:
  std::list<CJBig2_CachePair> m_SymbolDictCache;
};

JBig2DocumentContext* GetJBig2DocumentContext(
    std::unique_ptr<CFX_Deletable>* pContextHolder) {
  if (!pContextHolder->get())
    pContextHolder->reset(new JBig2DocumentContext());
  return static_cast<JBig2DocumentContext*>(pContextHolder->get());
}

CCodec_Jbig2Context::CCodec_Jbig2Context() {
  FXSYS_memset(this, 0, sizeof(CCodec_Jbig2Context));
}

CCodec_Jbig2Module::~CCodec_Jbig2Module() {}

CCodec_Jbig2Context* CCodec_Jbig2Module::CreateJbig2Context() {
  return new CCodec_Jbig2Context();
}

void CCodec_Jbig2Module::DestroyJbig2Context(
    CCodec_Jbig2Context* pJbig2Context) {
  if (pJbig2Context) {
    CJBig2_Context::DestroyContext(pJbig2Context->m_pContext);
    delete pJbig2Context;
  }
  pJbig2Context = nullptr;
}

FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(
    CCodec_Jbig2Context* pJbig2Context,
    std::unique_ptr<CFX_Deletable>* pContextHolder,
    uint32_t width,
    uint32_t height,
    CPDF_StreamAcc* src_stream,
    CPDF_StreamAcc* global_stream,
    uint8_t* dest_buf,
    uint32_t dest_pitch,
    IFX_Pause* pPause) {
  if (!pJbig2Context)
    return FXCODEC_STATUS_ERR_PARAMS;

  JBig2DocumentContext* pJBig2DocumentContext =
      GetJBig2DocumentContext(pContextHolder);
  pJbig2Context->m_width = width;
  pJbig2Context->m_height = height;
  pJbig2Context->m_pSrcStream = src_stream;
  pJbig2Context->m_pGlobalStream = global_stream;
  pJbig2Context->m_dest_buf = dest_buf;
  pJbig2Context->m_dest_pitch = dest_pitch;
  pJbig2Context->m_pPause = pPause;
  FXSYS_memset(dest_buf, 0, height * dest_pitch);
  pJbig2Context->m_pContext = CJBig2_Context::CreateContext(
      global_stream, src_stream, pJBig2DocumentContext->GetSymbolDictCache(),
      pPause);
  if (!pJbig2Context->m_pContext)
    return FXCODEC_STATUS_ERROR;

  int ret = pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height,
                                                    dest_pitch, pPause);
  if (pJbig2Context->m_pContext->GetProcessingStatus() ==
      FXCODEC_STATUS_DECODE_FINISH) {
    CJBig2_Context::DestroyContext(pJbig2Context->m_pContext);
    pJbig2Context->m_pContext = nullptr;
    if (ret != JBIG2_SUCCESS)
      return FXCODEC_STATUS_ERROR;

    int dword_size = height * dest_pitch / 4;
    uint32_t* dword_buf = (uint32_t*)dest_buf;
    for (int i = 0; i < dword_size; i++)
      dword_buf[i] = ~dword_buf[i];
    return FXCODEC_STATUS_DECODE_FINISH;
  }
  return pJbig2Context->m_pContext->GetProcessingStatus();
}

FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(
    CCodec_Jbig2Context* pJbig2Context,
    IFX_Pause* pPause) {
  int ret = pJbig2Context->m_pContext->Continue(pPause);
  if (pJbig2Context->m_pContext->GetProcessingStatus() !=
      FXCODEC_STATUS_DECODE_FINISH) {
    return pJbig2Context->m_pContext->GetProcessingStatus();
  }
  CJBig2_Context::DestroyContext(pJbig2Context->m_pContext);
  pJbig2Context->m_pContext = nullptr;
  if (ret != JBIG2_SUCCESS)
    return FXCODEC_STATUS_ERROR;

  int dword_size = pJbig2Context->m_height * pJbig2Context->m_dest_pitch / 4;
  uint32_t* dword_buf = (uint32_t*)pJbig2Context->m_dest_buf;
  for (int i = 0; i < dword_size; i++)
    dword_buf[i] = ~dword_buf[i];
  return FXCODEC_STATUS_DECODE_FINISH;
}