summaryrefslogtreecommitdiff
path: root/core/src/fxcodec/jbig2
diff options
context:
space:
mode:
authorJohn Abd-El-Malek <jabdelmalek@google.com>2014-05-17 22:33:34 -0700
committerJohn Abd-El-Malek <jabdelmalek@google.com>2014-05-17 22:33:34 -0700
commit5110c4743751145c4ae1934cd1d83bc6c55bb43f (patch)
treeb141608096b73163182764c25b895d3df4b2c182 /core/src/fxcodec/jbig2
parent76b563d2feed92ed328afb1f15e3466a9536b11b (diff)
downloadpdfium-5110c4743751145c4ae1934cd1d83bc6c55bb43f.tar.xz
Initial commit.
Diffstat (limited to 'core/src/fxcodec/jbig2')
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithDecoder.h126
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp105
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h39
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithQe.h64
-rw-r--r--core/src/fxcodec/jbig2/JBig2_BitStream.h316
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Context.cpp1812
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Context.h135
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Define.h34
-rw-r--r--core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp4290
-rw-r--r--core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h278
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp55
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h24
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp193
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable.h42
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h251
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Image.cpp1619
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Image.h68
-rw-r--r--core/src/fxcodec/jbig2/JBig2_List.h67
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Module.h32
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Object.cpp72
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Object.h43
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Page.h19
-rw-r--r--core/src/fxcodec/jbig2/JBig2_PatternDict.cpp24
-rw-r--r--core/src/fxcodec/jbig2/JBig2_PatternDict.h22
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Segment.cpp53
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Segment.h68
-rw-r--r--core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp34
-rw-r--r--core/src/fxcodec/jbig2/JBig2_SymbolDict.h26
28 files changed, 9911 insertions, 0 deletions
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h b/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h
new file mode 100644
index 0000000000..1664257411
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h
@@ -0,0 +1,126 @@
+// 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
+
+#ifndef _JBIG2_ARITHMETIC_DECODER_H_
+#define _JBIG2_ARITHMETIC_DECODER_H_
+#include "JBig2_Define.h"
+#include "JBig2_BitStream.h"
+#include "JBig2_ArithQe.h"
+typedef struct {
+ unsigned int MPS;
+ unsigned int I;
+} JBig2ArithCtx;
+class CJBig2_ArithDecoder : public CJBig2_Object
+{
+public:
+
+ CJBig2_ArithDecoder(CJBig2_BitStream *pStream);
+
+ ~CJBig2_ArithDecoder();
+
+ int DECODE(JBig2ArithCtx *pCX);
+private:
+
+ void INITDEC();
+
+ void BYTEIN();
+ unsigned char B;
+ unsigned int C;
+ unsigned int A;
+ unsigned int CT;
+ CJBig2_BitStream *m_pStream;
+};
+inline CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream *pStream)
+{
+ m_pStream = pStream;
+ INITDEC();
+}
+inline CJBig2_ArithDecoder::~CJBig2_ArithDecoder()
+{
+}
+inline void CJBig2_ArithDecoder::INITDEC()
+{
+ B = m_pStream->getCurByte_arith();
+ C = (B ^ 0xff) << 16;;
+ BYTEIN();
+ C = C << 7;
+ CT = CT - 7;
+ A = 0x8000;
+}
+inline void CJBig2_ArithDecoder::BYTEIN()
+{
+ unsigned char B1;
+ if(B == 0xff) {
+ B1 = m_pStream->getNextByte_arith();
+ if(B1 > 0x8f) {
+ CT = 8;
+ } else {
+ m_pStream->incByteIdx();
+ B = B1;
+ C = C + 0xfe00 - (B << 9);
+ CT = 7;
+ }
+ } else {
+ m_pStream->incByteIdx();
+ B = m_pStream->getCurByte_arith();
+ C = C + 0xff00 - (B << 8);
+ CT = 8;
+ }
+}
+inline int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx *pCX)
+{
+ int D;
+ const JBig2ArithQe * qe = &QeTable[pCX->I];
+ A = A - qe->Qe;
+ if((C >> 16) < A) {
+ if(A & 0x8000) {
+ D = pCX->MPS;
+ } else {
+ if(A < qe->Qe) {
+ D = 1 - pCX->MPS;
+ if(qe->nSwitch == 1) {
+ pCX->MPS = 1 - pCX->MPS;
+ }
+ pCX->I = qe->NLPS;
+ } else {
+ D = pCX->MPS;
+ pCX->I = qe->NMPS;
+ }
+ do {
+ if (CT == 0) {
+ BYTEIN();
+ }
+ A <<= 1;
+ C <<= 1;
+ CT--;
+ } while ((A & 0x8000) == 0);
+ }
+ } else {
+ C -= A << 16;
+ if(A < qe->Qe) {
+ A = qe->Qe;
+ D = pCX->MPS;
+ pCX->I = qe->NMPS;
+ } else {
+ A = qe->Qe;
+ D = 1 - pCX->MPS;
+ if(qe->nSwitch == 1) {
+ pCX->MPS = 1 - pCX->MPS;
+ }
+ pCX->I = qe->NLPS;
+ }
+ do {
+ if (CT == 0) {
+ BYTEIN();
+ }
+ A <<= 1;
+ C <<= 1;
+ CT--;
+ } while ((A & 0x8000) == 0);
+ }
+ return D;
+}
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
new file mode 100644
index 0000000000..bb4e0bba9f
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
@@ -0,0 +1,105 @@
+// 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 "JBig2_ArithIntDecoder.h"
+CJBig2_ArithIntDecoder::CJBig2_ArithIntDecoder()
+{
+ IAx = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), 512);
+ JBIG2_memset(IAx, 0, sizeof(JBig2ArithCtx) * 512);
+}
+CJBig2_ArithIntDecoder::~CJBig2_ArithIntDecoder()
+{
+ m_pModule->JBig2_Free(IAx);
+}
+int CJBig2_ArithIntDecoder::decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult)
+{
+ int PREV, V;
+ int S, D;
+ int nNeedBits, nTemp, i;
+ PREV = 1;
+ S = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | S;
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ nNeedBits = 32;
+ V = 4436;
+ } else {
+ nNeedBits = 12;
+ V = 340;
+ }
+ } else {
+ nNeedBits = 8;
+ V = 84;
+ }
+ } else {
+ nNeedBits = 6;
+ V = 20;
+ }
+ } else {
+ nNeedBits = 4;
+ V = 4;
+ }
+ } else {
+ nNeedBits = 2;
+ V = 0;
+ }
+ nTemp = 0;
+ for(i = 0; i < nNeedBits; i++) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ if(PREV < 256) {
+ PREV = (PREV << 1) | D;
+ } else {
+ PREV = (((PREV << 1) | D) & 511) | 256;
+ }
+ nTemp = (nTemp << 1) | D;
+ }
+ V += nTemp;
+ if(S == 1 && V > 0) {
+ V = -V;
+ }
+ *nResult = V;
+ if(S == 1 && V == 0) {
+ return JBIG2_OOB;
+ }
+ return 0;
+}
+CJBig2_ArithIaidDecoder::CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA)
+{
+ SBSYMCODELEN = SBSYMCODELENA;
+ IAID = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), (1 << SBSYMCODELEN));
+ JBIG2_memset(IAID, 0, sizeof(JBig2ArithCtx) * (int)(1 << SBSYMCODELEN));
+}
+CJBig2_ArithIaidDecoder::~CJBig2_ArithIaidDecoder()
+{
+ m_pModule->JBig2_Free(IAID);
+}
+int CJBig2_ArithIaidDecoder::decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult)
+{
+ int PREV;
+ int D;
+ int i;
+ PREV = 1;
+ for(i = 0; i < SBSYMCODELEN; i++) {
+ D = pArithDecoder->DECODE(IAID + PREV);
+ PREV = (PREV << 1) | D;
+ }
+ PREV = PREV - (1 << SBSYMCODELEN);
+ *nResult = PREV;
+ return 0;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h
new file mode 100644
index 0000000000..ad4656f54d
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h
@@ -0,0 +1,39 @@
+// 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
+
+#ifndef _JBIG2_ARITH_INT_DECODER_H_
+#define _JBIG2_ARITH_INT_DECODER_H_
+#include "JBig2_Module.h"
+#include "JBig2_ArithDecoder.h"
+class CJBig2_ArithIntDecoder : public CJBig2_Object
+{
+public:
+
+ CJBig2_ArithIntDecoder();
+
+ ~CJBig2_ArithIntDecoder();
+
+ int decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult);
+private:
+
+ JBig2ArithCtx *IAx;
+};
+class CJBig2_ArithIaidDecoder : public CJBig2_Object
+{
+public:
+
+ CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA);
+
+ ~CJBig2_ArithIaidDecoder();
+
+ int decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult);
+private:
+
+ JBig2ArithCtx *IAID;
+
+ unsigned char SBSYMCODELEN;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithQe.h b/core/src/fxcodec/jbig2/JBig2_ArithQe.h
new file mode 100644
index 0000000000..17bc18aa03
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_ArithQe.h
@@ -0,0 +1,64 @@
+// 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
+
+#ifndef _JBIG2_ARITH_QE_H_
+#define _JBIG2_ARITH_QE_H_
+typedef struct {
+ unsigned int Qe;
+ unsigned int NMPS;
+ unsigned int NLPS;
+ unsigned int nSwitch;
+} JBig2ArithQe;
+const JBig2ArithQe QeTable[] = {
+ { 0x5601, 1, 1, 1 },
+ { 0x3401, 2, 6, 0 },
+ { 0x1801, 3, 9, 0 },
+ { 0x0AC1, 4, 12, 0 },
+ { 0x0521, 5, 29, 0 },
+ { 0x0221, 38, 33, 0 },
+ { 0x5601, 7, 6, 1 },
+ { 0x5401, 8, 14, 0 },
+ { 0x4801, 9, 14, 0 },
+ { 0x3801, 10, 14, 0 },
+ { 0x3001, 11, 17, 0 },
+ { 0x2401, 12, 18, 0 },
+ { 0x1C01, 13, 20, 0 },
+ { 0x1601, 29, 21, 0 },
+ { 0x5601, 15, 14, 1 },
+ { 0x5401, 16, 14, 0 },
+ { 0x5101, 17, 15, 0 },
+ { 0x4801, 18, 16, 0 },
+ { 0x3801, 19, 17, 0 },
+ { 0x3401, 20, 18, 0 },
+ { 0x3001, 21, 19, 0 },
+ { 0x2801, 22, 19, 0 },
+ { 0x2401, 23, 20, 0 },
+ { 0x2201, 24, 21, 0 },
+ { 0x1C01, 25, 22, 0 },
+ { 0x1801, 26, 23, 0 },
+ { 0x1601, 27, 24, 0 },
+ { 0x1401, 28, 25, 0 },
+ { 0x1201, 29, 26, 0 },
+ { 0x1101, 30, 27, 0 },
+ { 0x0AC1, 31, 28, 0 },
+ { 0x09C1, 32, 29, 0 },
+ { 0x08A1, 33, 30, 0 },
+ { 0x0521, 34, 31, 0 },
+ { 0x0441, 35, 32, 0 },
+ { 0x02A1, 36, 33, 0 },
+ { 0x0221, 37, 34, 0 },
+ { 0x0141, 38, 35, 0 },
+ { 0x0111, 39, 36, 0 },
+ { 0x0085, 40, 37, 0 },
+ { 0x0049, 41, 38, 0 },
+ { 0x0025, 42, 39, 0 },
+ { 0x0015, 43, 40, 0 },
+ { 0x0009, 44, 41, 0 },
+ { 0x0005, 45, 42, 0 },
+ { 0x0001, 45, 43, 0 },
+ { 0x5601, 46, 46, 0 }
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_BitStream.h b/core/src/fxcodec/jbig2/JBig2_BitStream.h
new file mode 100644
index 0000000000..8ed473a150
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_BitStream.h
@@ -0,0 +1,316 @@
+// 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
+
+#ifndef _JBIG2_BIT_STREAM_H_
+#define _JBIG2_BIT_STREAM_H_
+#include "JBig2_Define.h"
+class CJBig2_BitStream : public CJBig2_Object
+{
+public:
+
+ CJBig2_BitStream(FX_BYTE *pBuffer, FX_DWORD dwLength);
+
+ CJBig2_BitStream(CJBig2_BitStream &bs);
+
+ ~CJBig2_BitStream();
+
+ FX_INT32 readNBits(FX_DWORD nBits, FX_DWORD *dwResult);
+
+ FX_INT32 readNBits(FX_DWORD nBits, FX_INT32 *nResult);
+
+ FX_INT32 read1Bit(FX_DWORD *dwResult);
+
+ FX_INT32 read1Bit(FX_BOOL *bResult);
+
+ FX_INT32 read1Byte(FX_BYTE *cResult);
+
+ FX_INT32 readInteger(FX_DWORD *dwResult);
+
+ FX_INT32 readShortInteger(FX_WORD *wResult);
+
+ void alignByte();
+
+ void align4Byte();
+
+ FX_BYTE getAt(FX_DWORD dwOffset);
+
+ FX_BYTE getCurByte();
+
+ FX_BYTE getNextByte();
+
+ FX_INT32 incByteIdx();
+
+ FX_BYTE getCurByte_arith();
+
+ FX_BYTE getNextByte_arith();
+
+ FX_DWORD getOffset();
+
+ void setOffset(FX_DWORD dwOffset);
+
+ FX_DWORD getBitPos();
+
+ void setBitPos(FX_DWORD dwBitPos);
+
+ FX_BYTE *getBuf();
+
+ FX_DWORD getLength()
+ {
+ return m_dwLength;
+ }
+
+ FX_BYTE *getPointer();
+
+ void offset(FX_DWORD dwOffset);
+
+ FX_DWORD getByteLeft();
+private:
+
+ FX_BYTE *m_pBuf;
+
+ FX_DWORD m_dwLength;
+
+ FX_DWORD m_dwByteIdx;
+
+ FX_DWORD m_dwBitIdx;
+};
+inline CJBig2_BitStream::CJBig2_BitStream(FX_BYTE *pBuffer, FX_DWORD dwLength)
+{
+ m_pBuf = pBuffer;
+ m_dwLength = dwLength;
+ m_dwByteIdx = 0;
+ m_dwBitIdx = 0;
+ if (m_dwLength > 256 * 1024 * 1024) {
+ m_dwLength = 0;
+ m_pBuf = NULL;
+ }
+}
+inline CJBig2_BitStream::CJBig2_BitStream(CJBig2_BitStream &bs)
+{
+ m_pBuf = bs.m_pBuf;
+ m_dwLength = bs.m_dwLength;
+ m_dwByteIdx = bs.m_dwByteIdx;
+ m_dwBitIdx = bs.m_dwBitIdx;
+}
+inline CJBig2_BitStream::~CJBig2_BitStream()
+{
+}
+inline FX_INT32 CJBig2_BitStream::readNBits(FX_DWORD dwBits, FX_DWORD *dwResult)
+{
+ FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx;
+ if(dwTemp <= (m_dwLength << 3)) {
+ *dwResult = 0;
+ if(dwTemp + dwBits <= (m_dwLength << 3)) {
+ dwTemp = dwBits;
+ } else {
+ dwTemp = (m_dwLength << 3) - dwTemp;
+ }
+ while(dwTemp > 0) {
+ *dwResult = (*dwResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
+ if(m_dwBitIdx == 7) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ } else {
+ m_dwBitIdx ++;
+ }
+ dwTemp --;
+ }
+ return 0;
+ } else {
+ return -1;
+ }
+}
+inline FX_INT32 CJBig2_BitStream::readNBits(FX_DWORD dwBits, FX_INT32 *nResult)
+{
+ FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx;
+ if(dwTemp <= (m_dwLength << 3)) {
+ *nResult = 0;
+ if(dwTemp + dwBits <= (m_dwLength << 3)) {
+ dwTemp = dwBits;
+ } else {
+ dwTemp = (m_dwLength << 3) - dwTemp;
+ }
+ while(dwTemp > 0) {
+ *nResult = (*nResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
+ if(m_dwBitIdx == 7) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ } else {
+ m_dwBitIdx ++;
+ }
+ dwTemp --;
+ }
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+inline FX_INT32 CJBig2_BitStream::read1Bit(FX_DWORD *dwResult)
+{
+ if(m_dwByteIdx < m_dwLength) {
+ *dwResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
+ if(m_dwBitIdx == 7) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ } else {
+ m_dwBitIdx ++;
+ }
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+inline FX_INT32 CJBig2_BitStream::read1Bit(FX_BOOL *bResult)
+{
+ if(m_dwByteIdx < m_dwLength) {
+ *bResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
+ if(m_dwBitIdx == 7) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ } else {
+ m_dwBitIdx ++;
+ }
+ return 0;
+ } else {
+ return -1;
+ }
+}
+inline FX_INT32 CJBig2_BitStream::read1Byte(FX_BYTE *cResult)
+{
+ if(m_dwByteIdx < m_dwLength) {
+ *cResult = m_pBuf[m_dwByteIdx];
+ m_dwByteIdx ++;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+inline FX_INT32 CJBig2_BitStream::readInteger(FX_DWORD *dwResult)
+{
+ if(m_dwByteIdx + 3 < m_dwLength) {
+ *dwResult = (m_pBuf[m_dwByteIdx] << 24) | (m_pBuf[m_dwByteIdx + 1] << 16)
+ | (m_pBuf[m_dwByteIdx + 2] << 8) | m_pBuf[m_dwByteIdx + 3];
+ m_dwByteIdx += 4;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+inline FX_INT32 CJBig2_BitStream::readShortInteger(FX_WORD *dwResult)
+{
+ if(m_dwByteIdx + 1 < m_dwLength) {
+ *dwResult = (m_pBuf[m_dwByteIdx] << 8) | m_pBuf[m_dwByteIdx + 1];
+ m_dwByteIdx += 2;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+inline void CJBig2_BitStream::alignByte()
+{
+ if(m_dwBitIdx != 0) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ }
+}
+inline void CJBig2_BitStream::align4Byte()
+{
+ if(m_dwBitIdx != 0) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ }
+ m_dwByteIdx = (m_dwByteIdx + 3) & -4;
+}
+inline FX_BYTE CJBig2_BitStream::getAt(FX_DWORD dwOffset)
+{
+ if(dwOffset < m_dwLength) {
+ return m_pBuf[dwOffset];
+ } else {
+ return 0;
+ }
+}
+inline FX_BYTE CJBig2_BitStream::getCurByte()
+{
+ if(m_dwByteIdx < m_dwLength) {
+ return m_pBuf[m_dwByteIdx];
+ } else {
+ return 0;
+ }
+}
+inline FX_BYTE CJBig2_BitStream::getNextByte()
+{
+ if(m_dwByteIdx + 1 < m_dwLength) {
+ return m_pBuf[m_dwByteIdx + 1];
+ } else {
+ return 0;
+ }
+}
+inline FX_INT32 CJBig2_BitStream::incByteIdx()
+{
+ if(m_dwByteIdx < m_dwLength) {
+ m_dwByteIdx ++;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+inline FX_BYTE CJBig2_BitStream::getCurByte_arith()
+{
+ if(m_dwByteIdx < m_dwLength) {
+ return m_pBuf[m_dwByteIdx];
+ } else {
+ return 0xff;
+ }
+}
+inline FX_BYTE CJBig2_BitStream::getNextByte_arith()
+{
+ if(m_dwByteIdx + 1 < m_dwLength) {
+ return m_pBuf[m_dwByteIdx + 1];
+ } else {
+ return 0xff;
+ }
+}
+inline FX_DWORD CJBig2_BitStream::getOffset()
+{
+ return m_dwByteIdx;
+}
+inline void CJBig2_BitStream::setOffset(FX_DWORD dwOffset)
+{
+ if (dwOffset > m_dwLength) {
+ dwOffset = m_dwLength;
+ }
+ m_dwByteIdx = dwOffset;
+}
+inline FX_DWORD CJBig2_BitStream::getBitPos()
+{
+ return (m_dwByteIdx << 3) + m_dwBitIdx;
+}
+inline void CJBig2_BitStream::setBitPos(FX_DWORD dwBitPos)
+{
+ m_dwByteIdx = dwBitPos >> 3;
+ m_dwBitIdx = dwBitPos & 7;
+}
+inline FX_BYTE *CJBig2_BitStream::getBuf()
+{
+ return m_pBuf;
+}
+inline FX_BYTE *CJBig2_BitStream::getPointer()
+{
+ return m_pBuf + m_dwByteIdx;
+}
+inline void CJBig2_BitStream::offset(FX_DWORD dwOffset)
+{
+ m_dwByteIdx += dwOffset;
+}
+inline FX_DWORD CJBig2_BitStream::getByteLeft()
+{
+ return m_dwLength - m_dwByteIdx;
+}
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Context.cpp b/core/src/fxcodec/jbig2/JBig2_Context.cpp
new file mode 100644
index 0000000000..856c0c3bda
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Context.cpp
@@ -0,0 +1,1812 @@
+// 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 "JBig2_Context.h"
+void OutputBitmap(CJBig2_Image* pImage)
+{
+ if(!pImage) {
+ return;
+ }
+}
+CJBig2_Context *CJBig2_Context::CreateContext(CJBig2_Module *pModule, FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
+ FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause)
+{
+ return new(pModule) CJBig2_Context(pGlobalData, dwGlobalLength, pData, dwLength, nStreamType, pPause);
+}
+void CJBig2_Context::DestroyContext(CJBig2_Context *pContext)
+{
+ if(pContext) {
+ delete pContext;
+ }
+}
+CJBig2_Context::CJBig2_Context(FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
+ FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause)
+{
+ if(pGlobalData && (dwGlobalLength > 0)) {
+ JBIG2_ALLOC(m_pGlobalContext, CJBig2_Context(NULL, 0, pGlobalData, dwGlobalLength,
+ JBIG2_EMBED_STREAM, pPause));
+ } else {
+ m_pGlobalContext = NULL;
+ }
+ JBIG2_ALLOC(m_pStream, CJBig2_BitStream(pData, dwLength));
+ m_nStreamType = nStreamType;
+ m_nState = JBIG2_OUT_OF_PAGE;
+ JBIG2_ALLOC(m_pSegmentList, CJBig2_List<CJBig2_Segment>);
+ JBIG2_ALLOC(m_pPageInfoList, CJBig2_List<JBig2PageInfo>(1));
+ m_pPage = NULL;
+ m_bBufSpecified = FALSE;
+ m_pPause = pPause;
+ m_nSegmentDecoded = 0;
+ m_PauseStep = 10;
+ m_pArithDecoder = NULL;
+ m_pGRD = NULL;
+ m_gbContext = NULL;
+ m_pSegment = NULL;
+ m_dwOffset = 0;
+ m_ProcessiveStatus = FXCODEC_STATUS_FRAME_READY;
+}
+CJBig2_Context::~CJBig2_Context()
+{
+ if(m_pArithDecoder) {
+ delete m_pArithDecoder;
+ }
+ m_pArithDecoder = NULL;
+ if(m_pGRD) {
+ delete m_pGRD;
+ }
+ m_pGRD = NULL;
+ if(m_gbContext) {
+ delete m_gbContext;
+ }
+ m_gbContext = NULL;
+ if(m_pGlobalContext) {
+ delete m_pGlobalContext;
+ }
+ m_pGlobalContext = NULL;
+ if(m_pPageInfoList) {
+ delete m_pPageInfoList;
+ }
+ m_pPageInfoList = NULL;
+ if(m_bBufSpecified && m_pPage) {
+ delete m_pPage;
+ }
+ m_pPage = NULL;
+ if(m_pStream) {
+ delete m_pStream;
+ }
+ m_pStream = NULL;
+ if(m_pSegmentList) {
+ delete m_pSegmentList;
+ }
+ m_pSegmentList = NULL;
+}
+FX_INT32 CJBig2_Context::decodeFile(IFX_Pause* pPause)
+{
+ FX_BYTE cFlags;
+ FX_DWORD dwTemp;
+ const FX_BYTE fileID[] = {0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A};
+ FX_INT32 nRet;
+ if(m_pStream->getByteLeft() < 8) {
+ m_pModule->JBig2_Error("file header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if(JBIG2_memcmp(m_pStream->getPointer(), fileID, 8) != 0) {
+ m_pModule->JBig2_Error("not jbig2 file");
+ nRet = JBIG2_ERROR_FILE_FORMAT;
+ goto failed;
+ }
+ m_pStream->offset(8);
+ if(m_pStream->read1Byte(&cFlags) != 0) {
+ m_pModule->JBig2_Error("file header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if(!(cFlags & 0x02)) {
+ if(m_pStream->readInteger(&dwTemp) != 0) {
+ m_pModule->JBig2_Error("file header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if(dwTemp > 0) {
+ delete m_pPageInfoList;
+ JBIG2_ALLOC(m_pPageInfoList, CJBig2_List<JBig2PageInfo>(dwTemp));
+ }
+ }
+ if(cFlags & 0x01) {
+ m_nStreamType = JBIG2_SQUENTIAL_STREAM;
+ return decode_SquentialOrgnazation(pPause);
+ } else {
+ m_nStreamType = JBIG2_RANDOM_STREAM;
+ return decode_RandomOrgnazation_FirstPage(pPause);
+ }
+failed:
+ return nRet;
+}
+FX_INT32 CJBig2_Context::decode_SquentialOrgnazation(IFX_Pause* pPause)
+{
+ FX_INT32 nRet;
+ if(m_pStream->getByteLeft() > 0) {
+ while(m_pStream->getByteLeft() >= JBIG2_MIN_SEGMENT_SIZE) {
+ if(m_pSegment == NULL) {
+ JBIG2_ALLOC(m_pSegment, CJBig2_Segment());
+ nRet = parseSegmentHeader(m_pSegment);
+ if(nRet != JBIG2_SUCCESS) {
+ delete m_pSegment;
+ m_pSegment = NULL;
+ return nRet;
+ }
+ m_dwOffset = m_pStream->getOffset();
+ }
+ nRet = parseSegmentData(m_pSegment, pPause);
+ if(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ m_PauseStep = 2;
+ return JBIG2_SUCCESS;
+ }
+ if((nRet == JBIG2_END_OF_PAGE) || (nRet == JBIG2_END_OF_FILE)) {
+ delete m_pSegment;
+ m_pSegment = NULL;
+ break;
+ } else if(nRet != JBIG2_SUCCESS) {
+ delete m_pSegment;
+ m_pSegment = NULL;
+ return nRet;
+ }
+ m_pSegmentList->addItem(m_pSegment);
+ if(m_pSegment->m_dwData_length != 0xffffffff) {
+ m_dwOffset = m_dwOffset + m_pSegment->m_dwData_length;
+ m_pStream->setOffset(m_dwOffset);
+ } else {
+ m_pStream->offset(4);
+ }
+ OutputBitmap(m_pPage);
+ m_pSegment = NULL;
+ if(m_pStream->getByteLeft() > 0 && m_pPage && pPause && pPause->NeedToPauseNow()) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ m_PauseStep = 2;
+ return JBIG2_SUCCESS;
+ }
+ }
+ } else {
+ return JBIG2_END_OF_FILE;
+ }
+ return JBIG2_SUCCESS;
+}
+FX_INT32 CJBig2_Context::decode_EmbedOrgnazation(IFX_Pause* pPause)
+{
+ return decode_SquentialOrgnazation(pPause);
+}
+FX_INT32 CJBig2_Context::decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause)
+{
+ CJBig2_Segment *pSegment;
+ FX_INT32 nRet;
+ while(m_pStream->getByteLeft() > JBIG2_MIN_SEGMENT_SIZE) {
+ JBIG2_ALLOC(pSegment, CJBig2_Segment());
+ nRet = parseSegmentHeader(pSegment);
+ if(nRet != JBIG2_SUCCESS) {
+ delete pSegment;
+ return nRet;
+ } else if(pSegment->m_cFlags.s.type == 51) {
+ delete pSegment;
+ break;
+ }
+ m_pSegmentList->addItem(pSegment);
+ if(pPause && m_pPause && pPause->NeedToPauseNow()) {
+ m_PauseStep = 3;
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return JBIG2_SUCCESS;
+ }
+ }
+ m_nSegmentDecoded = 0;
+ return decode_RandomOrgnazation(pPause);
+}
+FX_INT32 CJBig2_Context::decode_RandomOrgnazation(IFX_Pause* pPause)
+{
+ FX_INT32 nRet;
+ for(; m_nSegmentDecoded < m_pSegmentList->getLength(); m_nSegmentDecoded++) {
+ nRet = parseSegmentData(m_pSegmentList->getAt(m_nSegmentDecoded), pPause);
+ if((nRet == JBIG2_END_OF_PAGE) || (nRet == JBIG2_END_OF_FILE)) {
+ break;
+ } else if(nRet != JBIG2_SUCCESS) {
+ return nRet;
+ }
+ if(m_pPage && pPause && pPause->NeedToPauseNow()) {
+ m_PauseStep = 4;
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return JBIG2_SUCCESS;
+ }
+ }
+ return JBIG2_SUCCESS;
+}
+FX_INT32 CJBig2_Context::getFirstPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause)
+{
+ FX_INT32 nRet = 0;
+ if(m_pGlobalContext) {
+ nRet = m_pGlobalContext->decode_EmbedOrgnazation(pPause);
+ if(nRet != JBIG2_SUCCESS) {
+ m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
+ return nRet;
+ }
+ }
+ m_bFirstPage = TRUE;
+ m_PauseStep = 0;
+ if(m_pPage) {
+ delete m_pPage;
+ }
+ JBIG2_ALLOC(m_pPage, CJBig2_Image(width, height, stride, pBuf));
+ m_bBufSpecified = TRUE;
+ if(m_pPage && pPause && pPause->NeedToPauseNow()) {
+ m_PauseStep = 1;
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return nRet;
+ }
+ int ret = Continue(pPause);
+ return ret;
+}
+FX_INT32 CJBig2_Context::Continue(IFX_Pause* pPause)
+{
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_READY;
+ FX_INT32 nRet;
+ if(m_PauseStep <= 1) {
+ switch(m_nStreamType) {
+ case JBIG2_FILE_STREAM:
+ nRet = decodeFile(pPause);
+ break;
+ case JBIG2_SQUENTIAL_STREAM:
+ nRet = decode_SquentialOrgnazation(pPause);
+ break;
+ case JBIG2_RANDOM_STREAM:
+ if(m_bFirstPage) {
+ nRet = decode_RandomOrgnazation_FirstPage(pPause);
+ } else {
+ nRet = decode_RandomOrgnazation(pPause);
+ }
+ break;
+ case JBIG2_EMBED_STREAM:
+ nRet = decode_EmbedOrgnazation(pPause);
+ break;
+ default:
+ m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
+ return JBIG2_ERROR_STREAM_TYPE;
+ }
+ } else if(m_PauseStep == 2) {
+ nRet = decode_SquentialOrgnazation(pPause);
+ } else if(m_PauseStep == 3) {
+ nRet = decode_RandomOrgnazation_FirstPage(pPause);
+ } else if(m_PauseStep == 4) {
+ nRet = decode_RandomOrgnazation(pPause);
+ } else if(m_PauseStep == 5) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return JBIG2_SUCCESS;
+ }
+ if(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ return nRet;
+ }
+ m_PauseStep = 5;
+ if(!m_bBufSpecified && nRet == JBIG2_SUCCESS) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return JBIG2_SUCCESS;
+ }
+ if(nRet == JBIG2_SUCCESS) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ } else {
+ m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
+ }
+ return nRet;
+}
+FX_INT32 CJBig2_Context::getNextPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause)
+{
+ FX_INT32 nRet = JBIG2_ERROR_STREAM_TYPE;
+ m_bFirstPage = FALSE;
+ m_PauseStep = 0;
+ if(m_pPage) {
+ delete m_pPage;
+ }
+ JBIG2_ALLOC(m_pPage, CJBig2_Image(width, height, stride, pBuf));
+ m_bBufSpecified = TRUE;
+ if(m_pPage && pPause && pPause->NeedToPauseNow()) {
+ m_PauseStep = 1;
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return nRet;
+ }
+ return Continue(pPause);
+ switch(m_nStreamType) {
+ case JBIG2_FILE_STREAM:
+ nRet = decodeFile(pPause);
+ break;
+ case JBIG2_SQUENTIAL_STREAM:
+ nRet = decode_SquentialOrgnazation(pPause);
+ break;
+ case JBIG2_RANDOM_STREAM:
+ nRet = decode_RandomOrgnazation(pPause);
+ break;
+ case JBIG2_EMBED_STREAM:
+ nRet = decode_EmbedOrgnazation(pPause);
+ break;
+ default:
+ return JBIG2_ERROR_STREAM_TYPE;
+ }
+ return nRet;
+}
+FX_INT32 CJBig2_Context::getFirstPage(CJBig2_Image **image, IFX_Pause* pPause)
+{
+ FX_INT32 nRet;
+ m_bFirstPage = TRUE;
+ m_PauseStep = 0;
+ if(m_pGlobalContext) {
+ nRet = m_pGlobalContext->decode_EmbedOrgnazation(pPause);
+ if(nRet != JBIG2_SUCCESS) {
+ return nRet;
+ }
+ }
+ m_bBufSpecified = FALSE;
+ return Continue(pPause);
+}
+FX_INT32 CJBig2_Context::getNextPage(CJBig2_Image **image, IFX_Pause* pPause)
+{
+ FX_INT32 nRet;
+ m_bBufSpecified = FALSE;
+ m_bFirstPage = FALSE;
+ m_PauseStep = 0;
+ switch(m_nStreamType) {
+ case JBIG2_FILE_STREAM:
+ nRet = decodeFile(pPause);
+ break;
+ case JBIG2_SQUENTIAL_STREAM:
+ nRet = decode_SquentialOrgnazation(pPause);
+ break;
+ case JBIG2_RANDOM_STREAM:
+ nRet = decode_RandomOrgnazation(pPause);
+ break;
+ case JBIG2_EMBED_STREAM:
+ nRet = decode_EmbedOrgnazation(pPause);
+ break;
+ default:
+ return JBIG2_ERROR_STREAM_TYPE;
+ }
+ if(nRet == JBIG2_SUCCESS) {
+ *image = m_pPage;
+ m_pPage = NULL;
+ return JBIG2_SUCCESS;
+ }
+ return nRet;
+}
+CJBig2_Segment *CJBig2_Context::findSegmentByNumber(FX_DWORD dwNumber)
+{
+ CJBig2_Segment *pSeg;
+ FX_INT32 i;
+ if(m_pGlobalContext) {
+ pSeg = m_pGlobalContext->findSegmentByNumber(dwNumber);
+ if(pSeg) {
+ return pSeg;
+ }
+ }
+ for(i = 0; i < m_pSegmentList->getLength(); i++) {
+ pSeg = m_pSegmentList->getAt(i);
+ if(pSeg->m_dwNumber == dwNumber) {
+ return pSeg;
+ }
+ }
+ return NULL;
+}
+CJBig2_Segment *CJBig2_Context::findReferredSegmentByTypeAndIndex(CJBig2_Segment *pSegment,
+ FX_BYTE cType, FX_INT32 nIndex)
+{
+ CJBig2_Segment *pSeg;
+ FX_INT32 i, count;
+ count = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg && pSeg->m_cFlags.s.type == cType) {
+ if(count == nIndex) {
+ return pSeg;
+ } else {
+ count ++;
+ }
+ }
+ }
+ return NULL;
+}
+FX_INT32 CJBig2_Context::parseSegmentHeader(CJBig2_Segment *pSegment)
+{
+ FX_BYTE cSSize, cPSize;
+ FX_BYTE cTemp;
+ FX_WORD wTemp;
+ FX_DWORD dwTemp;
+ if((m_pStream->readInteger(&pSegment->m_dwNumber) != 0)
+ || (m_pStream->read1Byte(&pSegment->m_cFlags.c) != 0)) {
+ goto failed;
+ }
+ cTemp = m_pStream->getCurByte();
+ if((cTemp >> 5) == 7) {
+ if(m_pStream->readInteger((FX_DWORD*)&pSegment->m_nReferred_to_segment_count) != 0) {
+ goto failed;
+ }
+ pSegment->m_nReferred_to_segment_count &= 0x1fffffff;
+ if (pSegment->m_nReferred_to_segment_count > JBIG2_MAX_REFERRED_SEGMENT_COUNT) {
+ m_pModule->JBig2_Error("Too many referred segments.");
+ return JBIG2_ERROR_LIMIT;
+ }
+ dwTemp = 5 + 4 + (pSegment->m_nReferred_to_segment_count + 1) / 8;
+ } else {
+ if(m_pStream->read1Byte(&cTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_nReferred_to_segment_count = cTemp >> 5;
+ dwTemp = 5 + 1;
+ }
+ cSSize = pSegment->m_dwNumber > 65536 ? 4 : pSegment->m_dwNumber > 256 ? 2 : 1;
+ cPSize = pSegment->m_cFlags.s.page_association_size ? 4 : 1;
+ if(pSegment->m_nReferred_to_segment_count) {
+ pSegment->m_pReferred_to_segment_numbers = (FX_DWORD*)m_pModule->JBig2_Malloc2(
+ sizeof(FX_DWORD), pSegment->m_nReferred_to_segment_count);
+ for(FX_INT32 i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ switch(cSSize) {
+ case 1:
+ if(m_pStream->read1Byte(&cTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_pReferred_to_segment_numbers[i] = cTemp;
+ break;
+ case 2:
+ if(m_pStream->readShortInteger(&wTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_pReferred_to_segment_numbers[i] = wTemp;
+ break;
+ case 4:
+ if(m_pStream->readInteger(&dwTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_pReferred_to_segment_numbers[i] = dwTemp;
+ break;
+ }
+ if (pSegment->m_pReferred_to_segment_numbers[i] >= pSegment->m_dwNumber) {
+ m_pModule->JBig2_Error("The referred segment number is greater than this segment number.");
+ goto failed;
+ }
+ }
+ }
+ if(cPSize == 1) {
+ if(m_pStream->read1Byte(&cTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_dwPage_association = cTemp;
+ } else {
+ if(m_pStream->readInteger(&pSegment->m_dwPage_association) != 0) {
+ goto failed;
+ }
+ }
+ if(m_pStream->readInteger(&pSegment->m_dwData_length) != 0) {
+ goto failed;
+ }
+ pSegment->m_pData = m_pStream->getPointer();
+ pSegment->m_State = JBIG2_SEGMENT_DATA_UNPARSED;
+ return JBIG2_SUCCESS;
+failed:
+ m_pModule->JBig2_Error("header too short.");
+ return JBIG2_ERROR_TOO_SHORT;
+}
+FX_INT32 CJBig2_Context::parseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_INT32 ret = ProcessiveParseSegmentData(pSegment, pPause);
+ while(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE && m_pStream->getByteLeft() > 0) {
+ ret = ProcessiveParseSegmentData(pSegment, pPause);
+ }
+ return ret;
+}
+FX_INT32 CJBig2_Context::ProcessiveParseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ switch(pSegment->m_cFlags.s.type) {
+ case 0:
+ return parseSymbolDict(pSegment, pPause);
+ case 4:
+ case 6:
+ case 7:
+ if(m_nState == JBIG2_OUT_OF_PAGE) {
+ goto failed2;
+ } else {
+ return parseTextRegion(pSegment);
+ }
+ case 16:
+ return parsePatternDict(pSegment, pPause);
+ case 20:
+ case 22:
+ case 23:
+ if(m_nState == JBIG2_OUT_OF_PAGE) {
+ goto failed2;
+ } else {
+ return parseHalftoneRegion(pSegment, pPause);
+ }
+ case 36:
+ case 38:
+ case 39:
+ if(m_nState == JBIG2_OUT_OF_PAGE) {
+ goto failed2;
+ } else {
+ return parseGenericRegion(pSegment, pPause);
+ }
+ case 40:
+ case 42:
+ case 43:
+ if(m_nState == JBIG2_OUT_OF_PAGE) {
+ goto failed2;
+ } else {
+ return parseGenericRefinementRegion(pSegment);
+ }
+ case 48: {
+ FX_WORD wTemp;
+ JBig2PageInfo *pPageInfo;
+ JBIG2_ALLOC(pPageInfo, JBig2PageInfo);
+ if((m_pStream->readInteger(&pPageInfo->m_dwWidth) != 0)
+ || (m_pStream->readInteger(&pPageInfo->m_dwHeight) != 0)
+ || (m_pStream->readInteger(&pPageInfo->m_dwResolutionX) != 0)
+ || (m_pStream->readInteger(&pPageInfo->m_dwResolutionY) != 0)
+ || (m_pStream->read1Byte(&pPageInfo->m_cFlags) != 0)
+ || (m_pStream->readShortInteger(&wTemp) != 0)) {
+ delete pPageInfo;
+ goto failed1;
+ }
+ pPageInfo->m_bIsStriped = ((wTemp >> 15) & 1) ? 1 : 0;
+ pPageInfo->m_wMaxStripeSize = wTemp & 0x7fff;
+ if((pPageInfo->m_dwHeight == 0xffffffff) && (pPageInfo->m_bIsStriped != 1)) {
+ m_pModule->JBig2_Warn("page height = 0xffffffff buf stripe field is 0");
+ pPageInfo->m_bIsStriped = 1;
+ }
+ if(!m_bBufSpecified) {
+ if(m_pPage) {
+ delete m_pPage;
+ }
+ if(pPageInfo->m_dwHeight == 0xffffffff) {
+ JBIG2_ALLOC(m_pPage, CJBig2_Image(pPageInfo->m_dwWidth, pPageInfo->m_wMaxStripeSize));
+ } else {
+ JBIG2_ALLOC(m_pPage, CJBig2_Image(pPageInfo->m_dwWidth, pPageInfo->m_dwHeight));
+ }
+ }
+ m_pPage->fill((pPageInfo->m_cFlags & 4) ? 1 : 0);
+ m_pPageInfoList->addItem(pPageInfo);
+ m_nState = JBIG2_IN_PAGE;
+ }
+ break;
+ case 49:
+ m_nState = JBIG2_OUT_OF_PAGE;
+ return JBIG2_END_OF_PAGE;
+ break;
+ case 50:
+ m_pStream->offset(pSegment->m_dwData_length);
+ break;
+ case 51:
+ return JBIG2_END_OF_FILE;
+ case 52:
+ m_pStream->offset(pSegment->m_dwData_length);
+ break;
+ case 53:
+ return parseTable(pSegment);
+ case 62:
+ m_pStream->offset(pSegment->m_dwData_length);
+ break;
+ default:
+ break;
+ }
+ return JBIG2_SUCCESS;
+failed1:
+ m_pModule->JBig2_Error("segment data too short.");
+ return JBIG2_ERROR_TOO_SHORT;
+failed2:
+ m_pModule->JBig2_Error("segment syntax error.");
+ return JBIG2_ERROR_FETAL;
+}
+FX_INT32 CJBig2_Context::parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_DWORD dwTemp;
+ FX_WORD wFlags;
+ FX_BYTE cSDHUFFDH, cSDHUFFDW, cSDHUFFBMSIZE, cSDHUFFAGGINST;
+ CJBig2_HuffmanTable *Table_B1 = NULL, *Table_B2 = NULL, *Table_B3 = NULL, *Table_B4 = NULL, *Table_B5 = NULL;
+ FX_INT32 i, nIndex, nRet;
+ CJBig2_Segment *pSeg = NULL, *pLRSeg = NULL;
+ FX_BOOL bUsed;
+ CJBig2_Image ** SDINSYMS = NULL;
+ CJBig2_SDDProc *pSymbolDictDecoder;
+ JBig2ArithCtx *gbContext = NULL, *grContext = NULL;
+ CJBig2_ArithDecoder *pArithDecoder;
+ JBIG2_ALLOC(pSymbolDictDecoder, CJBig2_SDDProc());
+ if(m_pStream->readShortInteger(&wFlags) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFF = wFlags & 0x0001;
+ pSymbolDictDecoder->SDREFAGG = (wFlags >> 1) & 0x0001;
+ pSymbolDictDecoder->SDTEMPLATE = (wFlags >> 10) & 0x0003;
+ pSymbolDictDecoder->SDRTEMPLATE = (wFlags >> 12) & 0x0003;
+ cSDHUFFDH = (wFlags >> 2) & 0x0003;
+ cSDHUFFDW = (wFlags >> 4) & 0x0003;
+ cSDHUFFBMSIZE = (wFlags >> 6) & 0x0001;
+ cSDHUFFAGGINST = (wFlags >> 7) & 0x0001;
+ if(pSymbolDictDecoder->SDHUFF == 0) {
+ if(pSymbolDictDecoder->SDTEMPLATE == 0) {
+ dwTemp = 8;
+ } else {
+ dwTemp = 2;
+ }
+ for(i = 0; i < (FX_INT32)dwTemp; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&pSymbolDictDecoder->SDAT[i]) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ if((pSymbolDictDecoder->SDREFAGG == 1) && (pSymbolDictDecoder->SDRTEMPLATE == 0)) {
+ for(i = 0; i < 4; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&pSymbolDictDecoder->SDRAT[i]) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ if((m_pStream->readInteger(&pSymbolDictDecoder->SDNUMEXSYMS) != 0)
+ || (m_pStream->readInteger(&pSymbolDictDecoder->SDNUMNEWSYMS) != 0)) {
+ m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if (pSymbolDictDecoder->SDNUMEXSYMS > JBIG2_MAX_EXPORT_SYSMBOLS
+ || pSymbolDictDecoder->SDNUMNEWSYMS > JBIG2_MAX_NEW_SYSMBOLS) {
+ m_pModule->JBig2_Error("symbol dictionary segment : too many export/new symbols.");
+ nRet = JBIG2_ERROR_LIMIT;
+ goto failed;
+ }
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ if(!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) {
+ m_pModule->JBig2_Error("symbol dictionary segment : can't find refered to segments");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ }
+ pSymbolDictDecoder->SDNUMINSYMS = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg->m_cFlags.s.type == 0) {
+ pSymbolDictDecoder->SDNUMINSYMS += pSeg->m_Result.sd->SDNUMEXSYMS;
+ pLRSeg = pSeg;
+ }
+ }
+ if(pSymbolDictDecoder->SDNUMINSYMS == 0) {
+ SDINSYMS = NULL;
+ } else {
+ SDINSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(
+ sizeof(CJBig2_Image*), pSymbolDictDecoder->SDNUMINSYMS);
+ dwTemp = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg->m_cFlags.s.type == 0) {
+ JBIG2_memcpy(SDINSYMS + dwTemp, pSeg->m_Result.sd->SDEXSYMS,
+ pSeg->m_Result.sd->SDNUMEXSYMS * sizeof(CJBig2_Image*));
+ dwTemp += pSeg->m_Result.sd->SDNUMEXSYMS;
+ }
+ }
+ }
+ pSymbolDictDecoder->SDINSYMS = SDINSYMS;
+ if(pSymbolDictDecoder->SDHUFF == 1) {
+ if((cSDHUFFDH == 2) || (cSDHUFFDW == 2)) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFDH=2 or SDHUFFDW=2 is not permitted.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ nIndex = 0;
+ if(cSDHUFFDH == 0) {
+ JBIG2_ALLOC(Table_B4, CJBig2_HuffmanTable(HuffmanTable_B4,
+ sizeof(HuffmanTable_B4) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B4));
+ pSymbolDictDecoder->SDHUFFDH = Table_B4;
+ } else if(cSDHUFFDH == 1) {
+ JBIG2_ALLOC(Table_B5, CJBig2_HuffmanTable(HuffmanTable_B5,
+ sizeof(HuffmanTable_B5) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B5));
+ pSymbolDictDecoder->SDHUFFDH = Table_B5;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFDH can't find user supplied table.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFFDH = pSeg->m_Result.ht;
+ }
+ if(cSDHUFFDW == 0) {
+ JBIG2_ALLOC(Table_B2, CJBig2_HuffmanTable(HuffmanTable_B2,
+ sizeof(HuffmanTable_B2) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B2));
+ pSymbolDictDecoder->SDHUFFDW = Table_B2;
+ } else if(cSDHUFFDW == 1) {
+ JBIG2_ALLOC(Table_B3, CJBig2_HuffmanTable(HuffmanTable_B3,
+ sizeof(HuffmanTable_B3) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B3));
+ pSymbolDictDecoder->SDHUFFDW = Table_B3;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFDW can't find user supplied table.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFFDW = pSeg->m_Result.ht;
+ }
+ if(cSDHUFFBMSIZE == 0) {
+ JBIG2_ALLOC(Table_B1, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ pSymbolDictDecoder->SDHUFFBMSIZE = Table_B1;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFBMSIZE can't find user supplied table.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFFBMSIZE = pSeg->m_Result.ht;
+ }
+ if(pSymbolDictDecoder->SDREFAGG == 1) {
+ if(cSDHUFFAGGINST == 0) {
+ if(!Table_B1) {
+ JBIG2_ALLOC(Table_B1, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ }
+ pSymbolDictDecoder->SDHUFFAGGINST = Table_B1;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFAGGINST can't find user supplied table.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFFAGGINST = pSeg->m_Result.ht;
+ }
+ }
+ }
+ if((wFlags & 0x0100) && pLRSeg && pLRSeg->m_Result.sd->m_bContextRetained) {
+ if (pSymbolDictDecoder->SDHUFF == 0) {
+ dwTemp = pSymbolDictDecoder->SDTEMPLATE == 0 ? 65536 : pSymbolDictDecoder->SDTEMPLATE == 1 ?
+ 8192 : 1024;
+ gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memcpy(gbContext, pLRSeg->m_Result.sd->m_gbContext, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ if (pSymbolDictDecoder->SDREFAGG == 1) {
+ dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13;
+ grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memcpy(grContext, pLRSeg->m_Result.sd->m_grContext, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ } else {
+ if (pSymbolDictDecoder->SDHUFF == 0) {
+ dwTemp = pSymbolDictDecoder->SDTEMPLATE == 0 ? 65536 : pSymbolDictDecoder->SDTEMPLATE == 1 ?
+ 8192 : 1024;
+ gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ if (pSymbolDictDecoder->SDREFAGG == 1) {
+ dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13;
+ grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ }
+ pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER;
+ if(pSymbolDictDecoder->SDHUFF == 0) {
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith(pArithDecoder, gbContext, grContext);
+ delete pArithDecoder;
+ if(pSegment->m_Result.sd == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ } else {
+ pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman(m_pStream, gbContext, grContext, pPause);
+ if(pSegment->m_Result.sd == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ if(wFlags & 0x0200) {
+ pSegment->m_Result.sd->m_bContextRetained = TRUE;
+ if(pSymbolDictDecoder->SDHUFF == 0) {
+ pSegment->m_Result.sd->m_gbContext = gbContext;
+ }
+ if(pSymbolDictDecoder->SDREFAGG == 1) {
+ pSegment->m_Result.sd->m_grContext = grContext;
+ }
+ bUsed = TRUE;
+ } else {
+ bUsed = FALSE;
+ }
+ delete pSymbolDictDecoder;
+ if(SDINSYMS) {
+ m_pModule->JBig2_Free(SDINSYMS);
+ }
+ if(Table_B1) {
+ delete Table_B1;
+ }
+ if(Table_B2) {
+ delete Table_B2;
+ }
+ if(Table_B3) {
+ delete Table_B3;
+ }
+ if(Table_B4) {
+ delete Table_B4;
+ }
+ if(Table_B5) {
+ delete Table_B5;
+ }
+ if(bUsed == FALSE) {
+ if(gbContext) {
+ m_pModule->JBig2_Free(gbContext);
+ }
+ if(grContext) {
+ m_pModule->JBig2_Free(grContext);
+ }
+ }
+ return JBIG2_SUCCESS;
+failed:
+ delete pSymbolDictDecoder;
+ if(SDINSYMS) {
+ m_pModule->JBig2_Free(SDINSYMS);
+ }
+ if(Table_B1) {
+ delete Table_B1;
+ }
+ if(Table_B2) {
+ delete Table_B2;
+ }
+ if(Table_B3) {
+ delete Table_B3;
+ }
+ if(Table_B4) {
+ delete Table_B4;
+ }
+ if(Table_B5) {
+ delete Table_B5;
+ }
+ if(gbContext) {
+ m_pModule->JBig2_Free(gbContext);
+ }
+ if(grContext) {
+ m_pModule->JBig2_Free(grContext);
+ }
+ return nRet;
+}
+
+FX_BOOL CJBig2_Context::parseTextRegion(CJBig2_Segment *pSegment)
+{
+ FX_DWORD dwTemp;
+ FX_WORD wFlags;
+ FX_INT32 i, nIndex, nRet;
+ JBig2RegionInfo ri;
+ CJBig2_Segment *pSeg;
+ CJBig2_Image **SBSYMS = NULL;
+ JBig2HuffmanCode *SBSYMCODES = NULL;
+ FX_BYTE cSBHUFFFS, cSBHUFFDS, cSBHUFFDT, cSBHUFFRDW, cSBHUFFRDH, cSBHUFFRDX, cSBHUFFRDY, cSBHUFFRSIZE;
+ CJBig2_HuffmanTable *Table_B1 = NULL,
+ *Table_B6 = NULL,
+ *Table_B7 = NULL,
+ *Table_B8 = NULL,
+ *Table_B9 = NULL,
+ *Table_B10 = NULL,
+ *Table_B11 = NULL,
+ *Table_B12 = NULL,
+ *Table_B13 = NULL,
+ *Table_B14 = NULL,
+ *Table_B15 = NULL;
+ JBig2ArithCtx *grContext = NULL;
+ CJBig2_ArithDecoder *pArithDecoder;
+ CJBig2_TRDProc *pTRD;
+ JBIG2_ALLOC(pTRD, CJBig2_TRDProc());
+ if((parseRegionInfo(&ri) != JBIG2_SUCCESS)
+ || (m_pStream->readShortInteger(&wFlags) != 0)) {
+ m_pModule->JBig2_Error("text region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ pTRD->SBW = ri.width;
+ pTRD->SBH = ri.height;
+ pTRD->SBHUFF = wFlags & 0x0001;
+ pTRD->SBREFINE = (wFlags >> 1) & 0x0001;
+ dwTemp = (wFlags >> 2) & 0x0003;
+ pTRD->SBSTRIPS = 1 << dwTemp;
+ pTRD->REFCORNER = (JBig2Corner)((wFlags >> 4) & 0x0003);
+ pTRD->TRANSPOSED = (wFlags >> 6) & 0x0001;
+ pTRD->SBCOMBOP = (JBig2ComposeOp)((wFlags >> 7) & 0x0003);
+ pTRD->SBDEFPIXEL = (wFlags >> 9) & 0x0001;
+ pTRD->SBDSOFFSET = (wFlags >> 10) & 0x001f;
+ if(pTRD->SBDSOFFSET >= 0x0010) {
+ pTRD->SBDSOFFSET = pTRD->SBDSOFFSET - 0x0020;
+ }
+ pTRD->SBRTEMPLATE = (wFlags >> 15) & 0x0001;
+ if(pTRD->SBHUFF == 1) {
+ if(m_pStream->readShortInteger(&wFlags) != 0) {
+ m_pModule->JBig2_Error("text region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ cSBHUFFFS = wFlags & 0x0003;
+ cSBHUFFDS = (wFlags >> 2) & 0x0003;
+ cSBHUFFDT = (wFlags >> 4) & 0x0003;
+ cSBHUFFRDW = (wFlags >> 6) & 0x0003;
+ cSBHUFFRDH = (wFlags >> 8) & 0x0003;
+ cSBHUFFRDX = (wFlags >> 10) & 0x0003;
+ cSBHUFFRDY = (wFlags >> 12) & 0x0003;
+ cSBHUFFRSIZE = (wFlags >> 14) & 0x0001;
+ }
+ if((pTRD->SBREFINE == 1) && (pTRD->SBRTEMPLATE == 0)) {
+ for(i = 0; i < 4; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&pTRD->SBRAT[i]) != 0) {
+ m_pModule->JBig2_Error("text region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ if(m_pStream->readInteger(&pTRD->SBNUMINSTANCES) != 0) {
+ m_pModule->JBig2_Error("text region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ if(!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) {
+ m_pModule->JBig2_Error("text region segment : can't find refered to segments");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ }
+ pTRD->SBNUMSYMS = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg->m_cFlags.s.type == 0) {
+ pTRD->SBNUMSYMS += pSeg->m_Result.sd->SDNUMEXSYMS;
+ }
+ }
+ if (pTRD->SBNUMSYMS > 0) {
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(
+ sizeof(CJBig2_Image*), pTRD->SBNUMSYMS);
+ dwTemp = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg->m_cFlags.s.type == 0) {
+ JBIG2_memcpy(SBSYMS + dwTemp, pSeg->m_Result.sd->SDEXSYMS,
+ pSeg->m_Result.sd->SDNUMEXSYMS * sizeof(CJBig2_Image*));
+ dwTemp += pSeg->m_Result.sd->SDNUMEXSYMS;
+ }
+ }
+ pTRD->SBSYMS = SBSYMS;
+ } else {
+ pTRD->SBSYMS = NULL;
+ }
+ if(pTRD->SBHUFF == 1) {
+ SBSYMCODES = decodeSymbolIDHuffmanTable(m_pStream, pTRD->SBNUMSYMS);
+ if(SBSYMCODES == NULL) {
+ m_pModule->JBig2_Error("text region segment: symbol ID huffman table decode failure!");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ pTRD->SBSYMCODES = SBSYMCODES;
+ } else {
+ dwTemp = 0;
+ while((FX_DWORD)(1 << dwTemp) < pTRD->SBNUMSYMS) {
+ dwTemp ++;
+ }
+ pTRD->SBSYMCODELEN = (FX_BYTE)dwTemp;
+ }
+ if(pTRD->SBHUFF == 1) {
+ if((cSBHUFFFS == 2) || (cSBHUFFRDW == 2) || (cSBHUFFRDH == 2)
+ || (cSBHUFFRDX == 2) || (cSBHUFFRDY == 2)) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFFS=2 or SBHUFFRDW=2 or "
+ "SBHUFFRDH=2 or SBHUFFRDX=2 or SBHUFFRDY=2 is not permitted");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ nIndex = 0;
+ if(cSBHUFFFS == 0) {
+ JBIG2_ALLOC(Table_B6, CJBig2_HuffmanTable(HuffmanTable_B6,
+ sizeof(HuffmanTable_B6) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B6));
+ pTRD->SBHUFFFS = Table_B6;
+ } else if(cSBHUFFFS == 1) {
+ JBIG2_ALLOC(Table_B7, CJBig2_HuffmanTable(HuffmanTable_B7,
+ sizeof(HuffmanTable_B7) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B7));
+ pTRD->SBHUFFFS = Table_B7;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFFS can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFFS = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFDS == 0) {
+ JBIG2_ALLOC(Table_B8, CJBig2_HuffmanTable(HuffmanTable_B8,
+ sizeof(HuffmanTable_B8) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B8));
+ pTRD->SBHUFFDS = Table_B8;
+ } else if(cSBHUFFDS == 1) {
+ JBIG2_ALLOC(Table_B9, CJBig2_HuffmanTable(HuffmanTable_B9,
+ sizeof(HuffmanTable_B9) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B9));
+ pTRD->SBHUFFDS = Table_B9;
+ } else if(cSBHUFFDS == 2) {
+ JBIG2_ALLOC(Table_B10, CJBig2_HuffmanTable(HuffmanTable_B10,
+ sizeof(HuffmanTable_B10) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B10));
+ pTRD->SBHUFFDS = Table_B10;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFDS can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFDS = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFDT == 0) {
+ JBIG2_ALLOC(Table_B11, CJBig2_HuffmanTable(HuffmanTable_B11,
+ sizeof(HuffmanTable_B11) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B11));
+ pTRD->SBHUFFDT = Table_B11;
+ } else if(cSBHUFFDT == 1) {
+ JBIG2_ALLOC(Table_B12, CJBig2_HuffmanTable(HuffmanTable_B12,
+ sizeof(HuffmanTable_B12) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B12));
+ pTRD->SBHUFFDT = Table_B12;
+ } else if(cSBHUFFDT == 2) {
+ JBIG2_ALLOC(Table_B13, CJBig2_HuffmanTable(HuffmanTable_B13,
+ sizeof(HuffmanTable_B13) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B13));
+ pTRD->SBHUFFDT = Table_B13;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFDT can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFDT = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRDW == 0) {
+ JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
+ sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
+ pTRD->SBHUFFRDW = Table_B14;
+ } else if(cSBHUFFRDW == 1) {
+ JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ pTRD->SBHUFFRDW = Table_B15;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRDW can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRDW = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRDH == 0) {
+ if(!Table_B14) {
+ JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
+ sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
+ }
+ pTRD->SBHUFFRDH = Table_B14;
+ } else if(cSBHUFFRDH == 1) {
+ if(!Table_B15) {
+ JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ }
+ pTRD->SBHUFFRDH = Table_B15;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRDH can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRDH = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRDX == 0) {
+ if(!Table_B14) {
+ JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
+ sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
+ }
+ pTRD->SBHUFFRDX = Table_B14;
+ } else if(cSBHUFFRDX == 1) {
+ if(!Table_B15) {
+ JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ }
+ pTRD->SBHUFFRDX = Table_B15;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRDX can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRDX = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRDY == 0) {
+ if(!Table_B14) {
+ JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
+ sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
+ }
+ pTRD->SBHUFFRDY = Table_B14;
+ } else if(cSBHUFFRDY == 1) {
+ if(!Table_B15) {
+ JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ }
+ pTRD->SBHUFFRDY = Table_B15;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRDY can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRDY = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRSIZE == 0) {
+ JBIG2_ALLOC(Table_B1, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ pTRD->SBHUFFRSIZE = Table_B1;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRSIZE can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRSIZE = pSeg->m_Result.ht;
+ }
+ }
+ if(pTRD->SBREFINE == 1) {
+ dwTemp = pTRD->SBRTEMPLATE ? 1 << 10 : 1 << 13;
+ grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ if(pTRD->SBHUFF == 0) {
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ pSegment->m_Result.im = pTRD->decode_Arith(pArithDecoder, grContext);
+ delete pArithDecoder;
+ if(pSegment->m_Result.im == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ } else {
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ pSegment->m_Result.im = pTRD->decode_Huffman(m_pStream, grContext);
+ if(pSegment->m_Result.im == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ if(pSegment->m_cFlags.s.type != 4) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (ri.y + ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, (JBig2ComposeOp)(ri.flags & 0x03));
+ delete pSegment->m_Result.im;
+ pSegment->m_Result.im = NULL;
+ }
+ delete pTRD;
+ if(SBSYMS) {
+ m_pModule->JBig2_Free(SBSYMS);
+ }
+ if(SBSYMCODES) {
+ m_pModule->JBig2_Free(SBSYMCODES);
+ }
+ if(grContext) {
+ m_pModule->JBig2_Free(grContext);
+ }
+ if(Table_B1) {
+ delete Table_B1;
+ }
+ if(Table_B6) {
+ delete Table_B6;
+ }
+ if(Table_B7) {
+ delete Table_B7;
+ }
+ if(Table_B8) {
+ delete Table_B8;
+ }
+ if(Table_B9) {
+ delete Table_B9;
+ }
+ if(Table_B10) {
+ delete Table_B10;
+ }
+ if(Table_B11) {
+ delete Table_B11;
+ }
+ if(Table_B12) {
+ delete Table_B12;
+ }
+ if(Table_B13) {
+ delete Table_B13;
+ }
+ if(Table_B14) {
+ delete Table_B14;
+ }
+ if(Table_B15) {
+ delete Table_B15;
+ }
+ return JBIG2_SUCCESS;
+failed:
+ delete pTRD;
+ if(SBSYMS) {
+ m_pModule->JBig2_Free(SBSYMS);
+ }
+ if(SBSYMCODES) {
+ m_pModule->JBig2_Free(SBSYMCODES);
+ }
+ if(grContext) {
+ m_pModule->JBig2_Free(grContext);
+ }
+ if(Table_B1) {
+ delete Table_B1;
+ }
+ if(Table_B6) {
+ delete Table_B6;
+ }
+ if(Table_B7) {
+ delete Table_B7;
+ }
+ if(Table_B8) {
+ delete Table_B8;
+ }
+ if(Table_B9) {
+ delete Table_B9;
+ }
+ if(Table_B10) {
+ delete Table_B10;
+ }
+ if(Table_B11) {
+ delete Table_B11;
+ }
+ if(Table_B12) {
+ delete Table_B12;
+ }
+ if(Table_B13) {
+ delete Table_B13;
+ }
+ if(Table_B14) {
+ delete Table_B14;
+ }
+ if(Table_B15) {
+ delete Table_B15;
+ }
+ return nRet;
+}
+
+FX_BOOL CJBig2_Context::parsePatternDict(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_DWORD dwTemp;
+ FX_BYTE cFlags;
+ JBig2ArithCtx *gbContext;
+ CJBig2_ArithDecoder *pArithDecoder;
+ CJBig2_PDDProc *pPDD;
+ FX_INT32 nRet;
+ JBIG2_ALLOC(pPDD, CJBig2_PDDProc());
+ if((m_pStream->read1Byte(&cFlags) != 0)
+ || (m_pStream->read1Byte(&pPDD->HDPW) != 0)
+ || (m_pStream->read1Byte(&pPDD->HDPH) != 0)
+ || (m_pStream->readInteger(&pPDD->GRAYMAX) != 0)) {
+ m_pModule->JBig2_Error("pattern dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if (pPDD->GRAYMAX > JBIG2_MAX_PATTERN_INDEX) {
+ m_pModule->JBig2_Error("pattern dictionary segment : too max gray max.");
+ nRet = JBIG2_ERROR_LIMIT;
+ goto failed;
+ }
+ pPDD->HDMMR = cFlags & 0x01;
+ pPDD->HDTEMPLATE = (cFlags >> 1) & 0x03;
+ pSegment->m_nResultType = JBIG2_PATTERN_DICT_POINTER;
+ if(pPDD->HDMMR == 0) {
+ dwTemp = pPDD->HDTEMPLATE == 0 ? 65536 : pPDD->HDTEMPLATE == 1 ? 8192 : 1024;
+ gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_Result.pd = pPDD->decode_Arith(pArithDecoder, gbContext, pPause);
+ delete pArithDecoder;
+ if(pSegment->m_Result.pd == NULL) {
+ m_pModule->JBig2_Free(gbContext);
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(gbContext);
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ } else {
+ pSegment->m_Result.pd = pPDD->decode_MMR(m_pStream, pPause);
+ if(pSegment->m_Result.pd == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ delete pPDD;
+ return JBIG2_SUCCESS;
+failed:
+ delete pPDD;
+ return nRet;
+}
+FX_BOOL CJBig2_Context::parseHalftoneRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_DWORD dwTemp;
+ FX_BYTE cFlags;
+ JBig2RegionInfo ri;
+ CJBig2_Segment *pSeg;
+ CJBig2_PatternDict *pPatternDict;
+ JBig2ArithCtx *gbContext;
+ CJBig2_ArithDecoder *pArithDecoder;
+ CJBig2_HTRDProc *pHRD;
+ FX_INT32 nRet;
+ JBIG2_ALLOC(pHRD, CJBig2_HTRDProc());
+ if((parseRegionInfo(&ri) != JBIG2_SUCCESS)
+ || (m_pStream->read1Byte(&cFlags) != 0)
+ || (m_pStream->readInteger(&pHRD->HGW) != 0)
+ || (m_pStream->readInteger(&pHRD->HGH) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pHRD->HGX) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pHRD->HGY) != 0)
+ || (m_pStream->readShortInteger(&pHRD->HRX) != 0)
+ || (m_pStream->readShortInteger(&pHRD->HRY) != 0)) {
+ m_pModule->JBig2_Error("halftone region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ pHRD->HBW = ri.width;
+ pHRD->HBH = ri.height;
+ pHRD->HMMR = cFlags & 0x01;
+ pHRD->HTEMPLATE = (cFlags >> 1) & 0x03;
+ pHRD->HENABLESKIP = (cFlags >> 3) & 0x01;
+ pHRD->HCOMBOP = (JBig2ComposeOp)((cFlags >> 4) & 0x07);
+ pHRD->HDEFPIXEL = (cFlags >> 7) & 0x01;
+ if(pSegment->m_nReferred_to_segment_count != 1) {
+ m_pModule->JBig2_Error("halftone region segment : refered to segment count not equals 1");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[0]);
+ if( (pSeg == NULL) || (pSeg->m_cFlags.s.type != 16)) {
+ m_pModule->JBig2_Error("halftone region segment : refered to segment is not pattern dict");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pPatternDict = pSeg->m_Result.pd;
+ if((pPatternDict == NULL) || (pPatternDict->NUMPATS == 0)) {
+ m_pModule->JBig2_Error("halftone region segment : has no patterns input");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pHRD->HNUMPATS = pPatternDict->NUMPATS;
+ pHRD->HPATS = pPatternDict->HDPATS;
+ pHRD->HPW = pPatternDict->HDPATS[0]->m_nWidth;
+ pHRD->HPH = pPatternDict->HDPATS[0]->m_nHeight;
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ if(pHRD->HMMR == 0) {
+ dwTemp = pHRD->HTEMPLATE == 0 ? 65536 : pHRD->HTEMPLATE == 1 ? 8192 : 1024;
+ gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_Result.im = pHRD->decode_Arith(pArithDecoder, gbContext, pPause);
+ delete pArithDecoder;
+ if(pSegment->m_Result.im == NULL) {
+ m_pModule->JBig2_Free(gbContext);
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(gbContext);
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ } else {
+ pSegment->m_Result.im = pHRD->decode_MMR(m_pStream, pPause);
+ if(pSegment->m_Result.im == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ if(pSegment->m_cFlags.s.type != 20) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (ri.y + ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, (JBig2ComposeOp)(ri.flags & 0x03));
+ delete pSegment->m_Result.im;
+ pSegment->m_Result.im = NULL;
+ }
+ delete pHRD;
+ return JBIG2_SUCCESS;
+failed:
+ delete pHRD;
+ return nRet;
+}
+
+FX_BOOL CJBig2_Context::parseGenericRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_DWORD dwTemp;
+ FX_BYTE cFlags;
+ FX_INT32 i, nRet;
+ if(m_pGRD == NULL) {
+ JBIG2_ALLOC(m_pGRD, CJBig2_GRDProc());
+ if((parseRegionInfo(&m_ri) != JBIG2_SUCCESS)
+ || (m_pStream->read1Byte(&cFlags) != 0)) {
+ m_pModule->JBig2_Error("generic region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if (m_ri.height < 0 || m_ri.width < 0) {
+ m_pModule->JBig2_Error("generic region segment : wrong data.");
+ nRet = JBIG2_FAILED;
+ goto failed;
+ }
+ m_pGRD->GBW = m_ri.width;
+ m_pGRD->GBH = m_ri.height;
+ m_pGRD->MMR = cFlags & 0x01;
+ m_pGRD->GBTEMPLATE = (cFlags >> 1) & 0x03;
+ m_pGRD->TPGDON = (cFlags >> 3) & 0x01;
+ if(m_pGRD->MMR == 0) {
+ if(m_pGRD->GBTEMPLATE == 0) {
+ for(i = 0; i < 8; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&m_pGRD->GBAT[i]) != 0) {
+ m_pModule->JBig2_Error("generic region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ } else {
+ for(i = 0; i < 2; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&m_pGRD->GBAT[i]) != 0) {
+ m_pModule->JBig2_Error("generic region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ }
+ m_pGRD->USESKIP = 0;
+ }
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ if(m_pGRD->MMR == 0) {
+ dwTemp = m_pGRD->GBTEMPLATE == 0 ? 65536 : m_pGRD->GBTEMPLATE == 1 ? 8192 : 1024;
+ if(m_gbContext == NULL) {
+ m_gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc(sizeof(JBig2ArithCtx) * dwTemp);
+ JBIG2_memset(m_gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ if(m_pArithDecoder == NULL) {
+ JBIG2_ALLOC(m_pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ m_ProcessiveStatus = m_pGRD->Start_decode_Arith(&pSegment->m_Result.im, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProcessiveStatus = m_pGRD->Continue_decode(pPause);
+ }
+ OutputBitmap(pSegment->m_Result.im);
+ if(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ if(pSegment->m_cFlags.s.type != 36) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (m_ri.y + m_ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(m_ri.y + m_ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ FX_RECT Rect = m_pGRD->GetReplaceRect();
+ m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, pSegment->m_Result.im, (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect);
+ }
+ return JBIG2_SUCCESS;
+ } else {
+ delete m_pArithDecoder;
+ m_pArithDecoder = NULL;
+ if(pSegment->m_Result.im == NULL) {
+ m_pModule->JBig2_Free(m_gbContext);
+ nRet = JBIG2_ERROR_FETAL;
+ m_gbContext = NULL;
+ m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(m_gbContext);
+ m_gbContext = NULL;
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ }
+ } else {
+ FXCODEC_STATUS status = m_pGRD->Start_decode_MMR(&pSegment->m_Result.im, m_pStream, pPause);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ m_pGRD->Continue_decode(pPause);
+ }
+ if(pSegment->m_Result.im == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ if(pSegment->m_cFlags.s.type != 36) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (m_ri.y + m_ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(m_ri.y + m_ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ FX_RECT Rect = m_pGRD->GetReplaceRect();
+ m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, pSegment->m_Result.im, (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect);
+ delete pSegment->m_Result.im;
+ pSegment->m_Result.im = NULL;
+ }
+ delete m_pGRD;
+ m_pGRD = NULL;
+ return JBIG2_SUCCESS;
+failed:
+ delete m_pGRD;
+ m_pGRD = NULL;
+ return nRet;
+}
+
+FX_BOOL CJBig2_Context::parseGenericRefinementRegion(CJBig2_Segment *pSegment)
+{
+ FX_DWORD dwTemp;
+ JBig2RegionInfo ri;
+ CJBig2_Segment *pSeg;
+ FX_INT32 i, nRet;
+ FX_BYTE cFlags;
+ JBig2ArithCtx *grContext;
+ CJBig2_GRRDProc *pGRRD;
+ CJBig2_ArithDecoder *pArithDecoder;
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ if((parseRegionInfo(&ri) != JBIG2_SUCCESS)
+ || (m_pStream->read1Byte(&cFlags) != 0)) {
+ m_pModule->JBig2_Error("generic refinement region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ pGRRD->GRW = ri.width;
+ pGRRD->GRH = ri.height;
+ pGRRD->GRTEMPLATE = cFlags & 0x01;
+ pGRRD->TPGRON = (cFlags >> 1) & 0x01;
+ if(pGRRD->GRTEMPLATE == 0) {
+ for(i = 0; i < 4; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&pGRRD->GRAT[i]) != 0) {
+ m_pModule->JBig2_Error("generic refinement region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ pSeg = NULL;
+ if(pSegment->m_nReferred_to_segment_count > 0) {
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = this->findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[0]);
+ if(pSeg == NULL) {
+ m_pModule->JBig2_Error("generic refinement region segment : can't find refered to segments");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ if((pSeg->m_cFlags.s.type == 4) || (pSeg->m_cFlags.s.type == 20)
+ || (pSeg->m_cFlags.s.type == 36) || (pSeg->m_cFlags.s.type == 40)) {
+ break;
+ }
+ }
+ if(i >= pSegment->m_nReferred_to_segment_count) {
+ m_pModule->JBig2_Error("generic refinement region segment : can't find refered to intermediate region");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pGRRD->GRREFERENCE = pSeg->m_Result.im;
+ } else {
+ pGRRD->GRREFERENCE = m_pPage;
+ }
+ pGRRD->GRREFERENCEDX = 0;
+ pGRRD->GRREFERENCEDY = 0;
+ dwTemp = pGRRD->GRTEMPLATE ? 1 << 10 : 1 << 13;
+ grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ pSegment->m_Result.im = pGRRD->decode(pArithDecoder, grContext);
+ delete pArithDecoder;
+ if(pSegment->m_Result.im == NULL) {
+ m_pModule->JBig2_Free(grContext);
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(grContext);
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ if(pSegment->m_cFlags.s.type != 40) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (ri.y + ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, (JBig2ComposeOp)(ri.flags & 0x03));
+ delete pSegment->m_Result.im;
+ pSegment->m_Result.im = NULL;
+ }
+ delete pGRRD;
+ return JBIG2_SUCCESS;
+failed:
+ delete pGRRD;
+ return nRet;
+}
+FX_BOOL CJBig2_Context::parseTable(CJBig2_Segment *pSegment)
+{
+ pSegment->m_nResultType = JBIG2_HUFFMAN_TABLE_POINTER;
+ JBIG2_ALLOC(pSegment->m_Result.ht, CJBig2_HuffmanTable(m_pStream));
+ if(!pSegment->m_Result.ht->isOK()) {
+ delete pSegment->m_Result.ht;
+ pSegment->m_Result.ht = NULL;
+ return JBIG2_ERROR_FETAL;
+ }
+ m_pStream->alignByte();
+ return JBIG2_SUCCESS;
+}
+FX_INT32 CJBig2_Context::parseRegionInfo(JBig2RegionInfo *pRI)
+{
+ if((m_pStream->readInteger((FX_DWORD*)&pRI->width) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pRI->height) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pRI->x) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pRI->y) != 0)
+ || (m_pStream->read1Byte(&pRI->flags) != 0)) {
+ return JBIG2_ERROR_TOO_SHORT;
+ }
+ return JBIG2_SUCCESS;
+}
+JBig2HuffmanCode *CJBig2_Context::decodeSymbolIDHuffmanTable(CJBig2_BitStream *pStream,
+ FX_DWORD SBNUMSYMS)
+{
+ JBig2HuffmanCode *SBSYMCODES;
+ FX_INT32 runcodes[35], runcodes_len[35], runcode;
+ FX_INT32 i, j, nTemp, nVal, nBits;
+ FX_INT32 run;
+ SBSYMCODES = (JBig2HuffmanCode*)m_pModule->JBig2_Malloc2(sizeof(JBig2HuffmanCode), SBNUMSYMS);
+ for (i = 0; i < 35; i ++) {
+ if(pStream->readNBits(4, &runcodes_len[i]) != 0) {
+ goto failed;
+ }
+ }
+ huffman_assign_code(runcodes, runcodes_len, 35);
+ i = 0;
+ while(i < (int)SBNUMSYMS) {
+ nVal = 0;
+ nBits = 0;
+ for(;;) {
+ if(pStream->read1Bit(&nTemp) != 0) {
+ goto failed;
+ }
+ nVal = (nVal << 1) | nTemp;
+ nBits ++;
+ for(j = 0; j < 35; j++) {
+ if((nBits == runcodes_len[j]) && (nVal == runcodes[j])) {
+ break;
+ }
+ }
+ if(j < 35) {
+ break;
+ }
+ }
+ runcode = j;
+ if(runcode < 32) {
+ SBSYMCODES[i].codelen = runcode;
+ run = 0;
+ } else if(runcode == 32) {
+ if(pStream->readNBits(2, &nTemp) != 0) {
+ goto failed;
+ }
+ run = nTemp + 3;
+ } else if(runcode == 33) {
+ if(pStream->readNBits(3, &nTemp) != 0) {
+ goto failed;
+ }
+ run = nTemp + 3;
+ } else if(runcode == 34) {
+ if(pStream->readNBits(7, &nTemp) != 0) {
+ goto failed;
+ }
+ run = nTemp + 11;
+ }
+ if(run > 0) {
+ if (i + run > (int)SBNUMSYMS) {
+ goto failed;
+ }
+ for(j = 0; j < run; j++) {
+ if(runcode == 32 && i > 0) {
+ SBSYMCODES[i + j].codelen = SBSYMCODES[i - 1].codelen;
+ } else {
+ SBSYMCODES[i + j].codelen = 0;
+ }
+ }
+ i += run;
+ } else {
+ i ++;
+ }
+ }
+ huffman_assign_code(SBSYMCODES, SBNUMSYMS);
+ return SBSYMCODES;
+failed:
+ m_pModule->JBig2_Free(SBSYMCODES);
+ return NULL;
+}
+void CJBig2_Context::huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP)
+{
+ int CURLEN, LENMAX, CURCODE, CURTEMP, i;
+ int *LENCOUNT;
+ int *FIRSTCODE;
+ LENMAX = 0;
+ for(i = 0; i < NTEMP; i++) {
+ if(PREFLEN[i] > LENMAX) {
+ LENMAX = PREFLEN[i];
+ }
+ }
+ LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
+ FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ for(i = 0; i < NTEMP; i++) {
+ LENCOUNT[PREFLEN[i]] ++;
+ }
+ CURLEN = 1;
+ FIRSTCODE[0] = 0;
+ LENCOUNT[0] = 0;
+ while(CURLEN <= LENMAX) {
+ FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
+ CURCODE = FIRSTCODE[CURLEN];
+ CURTEMP = 0;
+ while(CURTEMP < NTEMP) {
+ if(PREFLEN[CURTEMP] == CURLEN) {
+ CODES[CURTEMP] = CURCODE;
+ CURCODE = CURCODE + 1;
+ }
+ CURTEMP = CURTEMP + 1;
+ }
+ CURLEN = CURLEN + 1;
+ }
+ m_pModule->JBig2_Free(LENCOUNT);
+ m_pModule->JBig2_Free(FIRSTCODE);
+}
+void CJBig2_Context::huffman_assign_code(JBig2HuffmanCode *SBSYMCODES, int NTEMP)
+{
+ int CURLEN, LENMAX, CURCODE, CURTEMP, i;
+ int *LENCOUNT;
+ int *FIRSTCODE;
+ LENMAX = 0;
+ for(i = 0; i < NTEMP; i++) {
+ if(SBSYMCODES[i].codelen > LENMAX) {
+ LENMAX = SBSYMCODES[i].codelen;
+ }
+ }
+ LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
+ FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ for(i = 0; i < NTEMP; i++) {
+ LENCOUNT[SBSYMCODES[i].codelen] ++;
+ }
+ CURLEN = 1;
+ FIRSTCODE[0] = 0;
+ LENCOUNT[0] = 0;
+ while(CURLEN <= LENMAX) {
+ FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
+ CURCODE = FIRSTCODE[CURLEN];
+ CURTEMP = 0;
+ while(CURTEMP < NTEMP) {
+ if(SBSYMCODES[CURTEMP].codelen == CURLEN) {
+ SBSYMCODES[CURTEMP].code = CURCODE;
+ CURCODE = CURCODE + 1;
+ }
+ CURTEMP = CURTEMP + 1;
+ }
+ CURLEN = CURLEN + 1;
+ }
+ m_pModule->JBig2_Free(LENCOUNT);
+ m_pModule->JBig2_Free(FIRSTCODE);
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Context.h b/core/src/fxcodec/jbig2/JBig2_Context.h
new file mode 100644
index 0000000000..369dba3512
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Context.h
@@ -0,0 +1,135 @@
+// 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
+
+#ifndef _JBIG2_CONTEXT_H_
+#define _JBIG2_CONTEXT_H_
+#include "JBig2_Module.h"
+#include "JBig2_List.h"
+#include "JBig2_Segment.h"
+#include "JBig2_Page.h"
+#include "JBig2_GeneralDecoder.h"
+#include "../../../include/fxcodec/fx_codec_def.h"
+#include "../../../include/fxcrt/fx_basic.h"
+typedef enum {
+ JBIG2_OUT_OF_PAGE = 0,
+ JBIG2_IN_PAGE,
+} JBig2State;
+#define JBIG2_SUCCESS 0
+#define JBIG2_FAILED -1
+#define JBIG2_ERROR_TOO_SHORT -2
+#define JBIG2_ERROR_FETAL -3
+#define JBIG2_END_OF_PAGE 2
+#define JBIG2_END_OF_FILE 3
+#define JBIG2_ERROR_FILE_FORMAT -4
+#define JBIG2_ERROR_STREAM_TYPE -5
+#define JBIG2_ERROR_LIMIT -6
+#define JBIG2_FILE_STREAM 0
+#define JBIG2_SQUENTIAL_STREAM 1
+#define JBIG2_RANDOM_STREAM 2
+#define JBIG2_EMBED_STREAM 3
+#define JBIG2_MIN_SEGMENT_SIZE 11
+class CJBig2_Context : public CJBig2_Object
+{
+public:
+
+ static CJBig2_Context *CreateContext(CJBig2_Module *pModule, FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
+ FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause = NULL);
+
+ static void DestroyContext(CJBig2_Context *pContext);
+
+ FX_INT32 getFirstPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause);
+
+ FX_INT32 getNextPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause);
+
+ FX_INT32 getFirstPage(CJBig2_Image **image, IFX_Pause* pPause);
+
+ FX_INT32 getNextPage(CJBig2_Image **image, IFX_Pause* pPause);
+ FX_INT32 Continue(IFX_Pause* pPause);
+ FXCODEC_STATUS GetProcessiveStatus()
+ {
+ return m_ProcessiveStatus;
+ };
+private:
+
+ CJBig2_Context(FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
+ FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause);
+
+ ~CJBig2_Context();
+
+ FX_INT32 decodeFile(IFX_Pause* pPause);
+
+ FX_INT32 decode_SquentialOrgnazation(IFX_Pause* pPause);
+
+ FX_INT32 decode_EmbedOrgnazation(IFX_Pause* pPause);
+
+ FX_INT32 decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
+
+ FX_INT32 decode_RandomOrgnazation(IFX_Pause* pPause);
+
+ CJBig2_Segment *findSegmentByNumber(FX_DWORD dwNumber);
+
+ CJBig2_Segment *findReferredSegmentByTypeAndIndex(CJBig2_Segment *pSegment, FX_BYTE cType, FX_INT32 nIndex);
+
+ FX_INT32 parseSegmentHeader(CJBig2_Segment *pSegment);
+
+ FX_INT32 parseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+ FX_INT32 ProcessiveParseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseTextRegion(CJBig2_Segment *pSegment);
+
+ FX_INT32 parsePatternDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseHalftoneRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseGenericRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseGenericRefinementRegion(CJBig2_Segment *pSegment);
+
+ FX_INT32 parseTable(CJBig2_Segment *pSegment);
+
+ FX_INT32 parseRegionInfo(JBig2RegionInfo *pRI);
+
+
+
+ JBig2HuffmanCode *decodeSymbolIDHuffmanTable(CJBig2_BitStream *pStream, FX_DWORD SBNUMSYMS);
+
+ void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);
+
+ void huffman_assign_code(JBig2HuffmanCode *SBSYMCODES, int NTEMP);
+
+private:
+
+ CJBig2_Context *m_pGlobalContext;
+
+ FX_INT32 m_nStreamType;
+
+ CJBig2_BitStream *m_pStream;
+
+ FX_INT32 m_nState;
+
+ CJBig2_List<CJBig2_Segment> *m_pSegmentList;
+
+ CJBig2_List<JBig2PageInfo> *m_pPageInfoList;
+
+ CJBig2_Image *m_pPage;
+
+ FX_BOOL m_bBufSpecified;
+
+ FX_INT32 m_nSegmentDecoded;
+ IFX_Pause* m_pPause;
+ FX_INT32 m_PauseStep;
+ FXCODEC_STATUS m_ProcessiveStatus;
+ FX_BOOL m_bFirstPage;
+ CJBig2_ArithDecoder *m_pArithDecoder;
+ CJBig2_GRDProc *m_pGRD;
+ JBig2ArithCtx *m_gbContext;
+ CJBig2_Segment *m_pSegment;
+ FX_DWORD m_dwOffset;
+ JBig2RegionInfo m_ri;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Define.h b/core/src/fxcodec/jbig2/JBig2_Define.h
new file mode 100644
index 0000000000..1019b6af5d
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Define.h
@@ -0,0 +1,34 @@
+// 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
+
+#ifndef _JBIG2_DEFINE_H_
+#define _JBIG2_DEFINE_H_
+#include "../../../include/fxcrt/fx_system.h"
+#define JBIG2_memset FXSYS_memset8
+#define JBIG2_memcmp FXSYS_memcmp32
+#define JBIG2_memcpy FXSYS_memcpy32
+#include "JBig2_Object.h"
+#define JBIG2_OOB 1
+typedef struct {
+ FX_INT32 width,
+ height;
+ FX_INT32 x,
+ y;
+ FX_BYTE flags;
+} JBig2RegionInfo;
+typedef struct {
+ FX_INT32 codelen;
+ FX_INT32 code;
+} JBig2HuffmanCode;
+extern "C" {
+ void _FaxG4Decode(void *pModule, FX_LPCBYTE src_buf, FX_DWORD src_size, int* pbitpos, FX_LPBYTE dest_buf, int width, int height, int pitch = 0);
+};
+#define JBIG2_MAX_REFERRED_SEGMENT_COUNT 64
+#define JBIG2_MAX_EXPORT_SYSMBOLS 65535
+#define JBIG2_MAX_NEW_SYSMBOLS 65535
+#define JBIG2_MAX_PATTERN_INDEX 65535
+#define JBIG2_MAX_IMAGE_SIZE 65535
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp
new file mode 100644
index 0000000000..9aa714a228
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp
@@ -0,0 +1,4290 @@
+// 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 "JBig2_GeneralDecoder.h"
+#include "JBig2_ArithDecoder.h"
+#include "JBig2_ArithIntDecoder.h"
+#include "JBig2_HuffmanDecoder.h"
+#include "JBig2_HuffmanTable.h"
+#include "JBig2_PatternDict.h"
+CJBig2_Image *CJBig2_GRDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ if (GBW == 0 || GBH == 0) {
+ CJBig2_Image* pImage;
+ JBIG2_ALLOC(pImage, CJBig2_Image(GBW, GBH));
+ return pImage;
+ }
+ if(GBTEMPLATE == 0) {
+ if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)
+ && (GBAT[2] == (signed char) - 3) && (GBAT[3] == (signed char) - 1)
+ && (GBAT[4] == 2) && (GBAT[5] == (signed char) - 2)
+ && (GBAT[6] == (signed char) - 2) && (GBAT[7] == (signed char) - 2)) {
+ return decode_Arith_Template0_opt3(pArithDecoder, gbContext);
+ } else {
+ return decode_Arith_Template0_unopt(pArithDecoder, gbContext);
+ }
+ } else if(GBTEMPLATE == 1) {
+ if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)) {
+ return decode_Arith_Template1_opt3(pArithDecoder, gbContext);
+ } else {
+ return decode_Arith_Template1_unopt(pArithDecoder, gbContext);
+ }
+ } else if(GBTEMPLATE == 2) {
+ if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
+ return decode_Arith_Template2_opt3(pArithDecoder, gbContext);
+ } else {
+ return decode_Arith_Template2_unopt(pArithDecoder, gbContext);
+ }
+ } else {
+ if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
+ return decode_Arith_Template3_opt3(pArithDecoder, gbContext);
+ } else {
+ return decode_Arith_Template3_unopt(pArithDecoder, gbContext);
+ }
+ }
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(2, h - 2);
+ line1 |= GBREG->getPixel(1, h - 2) << 1;
+ line1 |= GBREG->getPixel(0, h - 2) << 2;
+ line2 = GBREG->getPixel(3, h - 1);
+ line2 |= GBREG->getPixel(2, h - 1) << 1;
+ line2 |= GBREG->getPixel(1, h - 1) << 2;
+ line2 |= GBREG->getPixel(0, h - 1) << 3;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 11;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x1f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 4, h - 1)) & 0x7f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, cVal;
+ FX_INTPTR nStride, nStride2;
+ FX_INT32 nBits, k;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = (h > 1) ? pLine[-nStride2] << 6 : 0;
+ line2 = (h > 0) ? pLine[-nStride] : 0;
+ CONTEXT = (line1 & 0xf800) | (line2 & 0x07f0);
+ for(FX_DWORD w = 0; w < GBW; w += 8) {
+ if(w + 8 < GBW) {
+ nBits = 8;
+ if(h > 1) {
+ line1 = (line1 << 8) | (pLine[-nStride2 + (w >> 3) + 1] << 6);
+ }
+ if(h > 0) {
+ line2 = (line2 << 8) | (pLine[-nStride + (w >> 3) + 1]);
+ }
+ } else {
+ nBits = GBW - w;
+ if(h > 1) {
+ line1 <<= 8;
+ }
+ if(h > 0) {
+ line2 <<= 8;
+ }
+ }
+ cVal = 0;
+ for(k = 0; k < nBits; k++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0800)
+ | ((line2 >> (7 - k)) & 0x0010);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ FX_DWORD height = GBH & 0x7fffffff;
+ for(FX_DWORD h = 0; h < height; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ if(h > 1) {
+ pLine1 = pLine - nStride2;
+ pLine2 = pLine - nStride;
+ line1 = (*pLine1++) << 6;
+ line2 = *pLine2++;
+ CONTEXT = ((line1 & 0xf800) | (line2 & 0x07f0));
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 6);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> k) & 0x0800)
+ | ((line2 >> k) & 0x0010));
+ }
+ pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0800)
+ | ((line2 >> (7 - k)) & 0x0010));
+ }
+ pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = pLine - nStride;
+ line2 = (h & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 & 0x07f0);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(h & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line2 >> k) & 0x0010));
+ }
+ pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | (((line2 >> (7 - k))) & 0x0010));
+ }
+ pLine[nLineBytes] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= line2 << 5;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
+ CONTEXT |= line1 << 12;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(2, h - 2);
+ line1 |= GBREG->getPixel(1, h - 2) << 1;
+ line1 |= GBREG->getPixel(0, h - 2) << 2;
+ line2 = GBREG->getPixel(3, h - 1);
+ line2 |= GBREG->getPixel(2, h - 1) << 1;
+ line2 |= GBREG->getPixel(1, h - 1) << 2;
+ line2 |= GBREG->getPixel(0, h - 1) << 3;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 9;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 4, h - 1)) & 0x3f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, cVal;
+ FX_INTPTR nStride, nStride2;
+ FX_INT32 nBits, k;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = (h > 1) ? pLine[-nStride2] << 4 : 0;
+ line2 = (h > 0) ? pLine[-nStride] : 0;
+ CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
+ for(FX_DWORD w = 0; w < GBW; w += 8) {
+ if(w + 8 < GBW) {
+ nBits = 8;
+ if(h > 1) {
+ line1 = (line1 << 8) | (pLine[-nStride2 + (w >> 3) + 1] << 4);
+ }
+ if(h > 0) {
+ line2 = (line2 << 8) | (pLine[-nStride + (w >> 3) + 1]);
+ }
+ } else {
+ nBits = GBW - w;
+ if(h > 1) {
+ line1 <<= 8;
+ }
+ if(h > 0) {
+ line2 <<= 8;
+ }
+ }
+ cVal = 0;
+ for(k = 0; k < nBits; k++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0200)
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ if(h > 1) {
+ pLine1 = pLine - nStride2;
+ pLine2 = pLine - nStride;
+ line1 = (*pLine1++) << 4;
+ line2 = *pLine2++;
+ CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 4);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> k) & 0x0200)
+ | ((line2 >> (k + 1)) & 0x0008);
+ }
+ pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0200)
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = pLine - nStride;
+ line2 = (h & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 >> 1) & 0x01f8;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(h & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line2 >> (k + 1)) & 0x0008);
+ }
+ pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ pLine[nLineBytes] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(2, h - 2);
+ line1 |= GBREG->getPixel(1, h - 2) << 1;
+ line1 |= GBREG->getPixel(0, h - 2) << 2;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 9;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= line2 << 2;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, cVal;
+ FX_INTPTR nStride, nStride2;
+ FX_INT32 nBits, k;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = (h > 1) ? pLine[-nStride2] << 1 : 0;
+ line2 = (h > 0) ? pLine[-nStride] : 0;
+ CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
+ for(FX_DWORD w = 0; w < GBW; w += 8) {
+ if(w + 8 < GBW) {
+ nBits = 8;
+ if(h > 1) {
+ line1 = (line1 << 8) | (pLine[-nStride2 + (w >> 3) + 1] << 1);
+ }
+ if(h > 0) {
+ line2 = (line2 << 8) | (pLine[-nStride + (w >> 3) + 1]);
+ }
+ } else {
+ nBits = GBW - w;
+ if(h > 1) {
+ line1 <<= 8;
+ }
+ if(h > 0) {
+ line2 <<= 8;
+ }
+ }
+ cVal = 0;
+ for(k = 0; k < nBits; k++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0080)
+ | ((line2 >> (10 - k)) & 0x0004);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ if(h > 1) {
+ pLine1 = pLine - nStride2;
+ pLine2 = pLine - nStride;
+ line1 = (*pLine1++) << 1;
+ line2 = *pLine2++;
+ CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 1);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> k) & 0x0080)
+ | ((line2 >> (k + 3)) & 0x0004);
+ }
+ pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0080)
+ | ((line2 >> (10 - k)) & 0x0004);
+ }
+ pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = pLine - nStride;
+ line2 = (h & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 >> 3) & 0x007c;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(h & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line2 >> (k + 3)) & 0x0004);
+ }
+ pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | (((line2 >> (10 - k))) & 0x0004);
+ }
+ pLine[nLineBytes] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(1, h - 1);
+ line2 |= GBREG->getPixel(0, h - 1) << 1;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x0f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(2, h - 1);
+ line1 |= GBREG->getPixel(1, h - 1) << 1;
+ line1 |= GBREG->getPixel(0, h - 1) << 2;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= line1 << 4;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x3f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1;
+ FX_BYTE *pLine, cVal;
+ FX_INTPTR nStride, nStride2;
+ FX_INT32 nBits, k;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = (h > 0) ? pLine[-nStride] : 0;
+ CONTEXT = (line1 >> 1) & 0x03f0;
+ for(FX_DWORD w = 0; w < GBW; w += 8) {
+ if(w + 8 < GBW) {
+ nBits = 8;
+ if(h > 0) {
+ line1 = (line1 << 8) | (pLine[-nStride + (w >> 3) + 1]);
+ }
+ } else {
+ nBits = GBW - w;
+ if(h > 0) {
+ line1 <<= 8;
+ }
+ }
+ cVal = 0;
+ for(k = 0; k < nBits; k++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (8 - k)) & 0x0010);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1;
+ FX_BYTE *pLine, *pLine1, cVal;
+ FX_INT32 nStride, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ if(h > 0) {
+ pLine1 = pLine - nStride;
+ line1 = *pLine1++;
+ CONTEXT = (line1 >> 1) & 0x03f0;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | (*pLine1++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (k + 1)) & 0x0010);
+ }
+ pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (8 - k)) & 0x0010);
+ }
+ pLine[nLineBytes] = cVal;
+ } else {
+ CONTEXT = 0;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
+ }
+ pLine[cc] = cVal;
+ }
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
+ }
+ pLine[nLineBytes] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(1, h - 1);
+ line1 |= GBREG->getPixel(0, h - 1) << 1;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= line1 << 5;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x1f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_V2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT = 0x9b25;
+ break;
+ case 1:
+ CONTEXT = 0x0795;
+ break;
+ case 2:
+ CONTEXT = 0x00e5;
+ break;
+ case 3:
+ CONTEXT = 0x0195;
+ break;
+ }
+ SLTP = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ switch(GBTEMPLATE) {
+ case 0: {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= line2 << 5;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
+ CONTEXT |= line1 << 12;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ break;
+ case 1: {
+ line1 = GBREG->getPixel(2, h - 2);
+ line1 |= GBREG->getPixel(1, h - 2) << 1;
+ line1 |= GBREG->getPixel(0, h - 2) << 2;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 9;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ break;
+ case 2: {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(1, h - 1);
+ line2 |= GBREG->getPixel(0, h - 1) << 1;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x0f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ break;
+ case 3: {
+ line1 = GBREG->getPixel(1, h - 1);
+ line1 |= GBREG->getPixel(0, h - 1) << 1;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= line1 << 5;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x1f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ break;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT = 0;
+ CJBig2_Image *GBREG;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT = 0x9b25;
+ break;
+ case 1:
+ CONTEXT = 0x0795;
+ break;
+ case 2:
+ CONTEXT = 0x00e5;
+ break;
+ case 3:
+ CONTEXT = 0x0195;
+ break;
+ }
+ SLTP = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ GBREG->setPixel(w, h, GBREG->getPixel(w, h - 1));
+ }
+ } else {
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ GBREG->setPixel(w, h, 0);
+ } else {
+ CONTEXT = 0;
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT |= GBREG->getPixel(w - 1, h);
+ CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, h) << 2;
+ CONTEXT |= GBREG->getPixel(w - 4, h) << 3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= GBREG->getPixel(w + 2, h - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w, h - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 9;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 2) << 12;
+ CONTEXT |= GBREG->getPixel(w, h - 2) << 13;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 2) << 14;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
+ break;
+ case 1:
+ CONTEXT |= GBREG->getPixel(w - 1, h);
+ CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, h) << 2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
+ CONTEXT |= GBREG->getPixel(w + 2, h - 1) << 4;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w, h - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w + 2, h - 2) << 9;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 2) << 10;
+ CONTEXT |= GBREG->getPixel(w, h - 2) << 11;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 2) << 12;
+ break;
+ case 2:
+ CONTEXT |= GBREG->getPixel(w - 1, h);
+ CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 3;
+ CONTEXT |= GBREG->getPixel(w, h - 1) << 4;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 2) << 7;
+ CONTEXT |= GBREG->getPixel(w, h - 2) << 8;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 2) << 9;
+ break;
+ case 3:
+ CONTEXT |= GBREG->getPixel(w - 1, h);
+ CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, h) << 2;
+ CONTEXT |= GBREG->getPixel(w - 4, h) << 3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w, h - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w - 3, h - 1) << 9;
+ break;
+ }
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ GBREG->setPixel(w, h, bVal);
+ }
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_MMR(CJBig2_BitStream *pStream)
+{
+ int bitpos, i;
+ CJBig2_Image *pImage;
+ JBIG2_ALLOC(pImage, CJBig2_Image(GBW, GBH));
+ if (pImage->m_pData == NULL) {
+ delete pImage;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ bitpos = (int)pStream->getBitPos();
+ _FaxG4Decode(m_pModule, pStream->getBuf(), pStream->getLength(), &bitpos, pImage->m_pData, GBW, GBH, pImage->m_nStride);
+ pStream->setBitPos(bitpos);
+ for(i = 0; (FX_DWORD)i < pImage->m_nStride * GBH; i++) {
+ pImage->m_pData[i] = ~pImage->m_pData[i];
+ }
+ return pImage;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ if (GRW == 0 || GRH == 0) {
+ CJBig2_Image* pImage;
+ JBIG2_ALLOC(pImage, CJBig2_Image(GRW, GRH));
+ return pImage;
+ }
+ if(GRTEMPLATE == 0) {
+ if((GRAT[0] == (signed char) - 1) && (GRAT[1] == (signed char) - 1)
+ && (GRAT[2] == (signed char) - 1) && (GRAT[3] == (signed char) - 1)
+ && (GRREFERENCEDX == 0) && (GRW == (FX_DWORD)GRREFERENCE->m_nWidth)) {
+ return decode_Template0_opt(pArithDecoder, grContext);
+ } else {
+ return decode_Template0_unopt(pArithDecoder, grContext);
+ }
+ } else {
+ if((GRREFERENCEDX == 0) && (GRW == (FX_DWORD)GRREFERENCE->m_nWidth)) {
+ return decode_Template1_opt(pArithDecoder, grContext);
+ } else {
+ return decode_Template1_unopt(pArithDecoder, grContext);
+ }
+ }
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ FX_DWORD line1, line2, line3, line4, line5;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ GRREG->fill(0);
+ for(FX_DWORD h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ SLTP = pArithDecoder->DECODE(&grContext[0x0010]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 0) {
+ line1 = GRREG->getPixel(1, h - 1);
+ line1 |= GRREG->getPixel(0, h - 1) << 1;
+ line2 = 0;
+ line3 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
+ line3 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1) << 1;
+ line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
+ line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ CONTEXT = line5;
+ CONTEXT |= line4 << 3;
+ CONTEXT |= line3 << 6;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
+ CONTEXT |= line2 << 9;
+ CONTEXT |= line1 << 10;
+ CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ GRREG->setPixel(w, h, bVal);
+ line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x03;
+ line2 = ((line2 << 1) | bVal) & 0x01;
+ line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY - 1)) & 0x03;
+ line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
+ line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x07;
+ }
+ } else {
+ line1 = GRREG->getPixel(1, h - 1);
+ line1 |= GRREG->getPixel(0, h - 1) << 1;
+ line2 = 0;
+ line3 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
+ line3 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1) << 1;
+ line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
+ line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ bVal = GRREFERENCE->getPixel(w, h);
+ if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h + 1)))) {
+ CONTEXT = line5;
+ CONTEXT |= line4 << 3;
+ CONTEXT |= line3 << 6;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
+ CONTEXT |= line2 << 9;
+ CONTEXT |= line1 << 10;
+ CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ }
+ GRREG->setPixel(w, h, bVal);
+ line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x03;
+ line2 = ((line2 << 1) | bVal) & 0x01;
+ line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY - 1)) & 0x03;
+ line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
+ line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x07;
+ }
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ FX_DWORD line1, line1_r, line2_r, line3_r;
+ FX_BYTE *pLine, *pLineR, cVal;
+ FX_INTPTR nStride, nStrideR, nOffset;
+ FX_INT32 k, nBits;
+ FX_INT32 GRWR, GRHR;
+ FX_INT32 GRW, GRH;
+ GRW = (FX_INT32)CJBig2_GRRDProc::GRW;
+ GRH = (FX_INT32)CJBig2_GRRDProc::GRH;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ if (GRREG->m_pData == NULL) {
+ delete GRREG;
+ m_pModule->JBig2_Error("Generic refinement region decoding procedure: Create Image Failed with width = %d, height = %d\n", GRW, GRH);
+ return NULL;
+ }
+ pLine = GRREG->m_pData;
+ pLineR = GRREFERENCE->m_pData;
+ nStride = GRREG->m_nStride;
+ nStrideR = GRREFERENCE->m_nStride;
+ GRWR = (FX_INT32)GRREFERENCE->m_nWidth;
+ GRHR = (FX_INT32)GRREFERENCE->m_nHeight;
+ if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
+ GRREFERENCEDY = 0;
+ }
+ nOffset = -GRREFERENCEDY * nStrideR;
+ for (FX_INT32 h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ SLTP = pArithDecoder->DECODE(&grContext[0x0010]);
+ LTP = LTP ^ SLTP;
+ }
+ line1 = (h > 0) ? pLine[-nStride] << 4 : 0;
+ FX_INT32 reference_h = h - GRREFERENCEDY;
+ FX_BOOL line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
+ FX_BOOL line2_r_ok = (reference_h > -1 && reference_h < GRHR);
+ FX_BOOL line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
+ line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
+ line2_r = line2_r_ok ? pLineR[nOffset] : 0;
+ line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
+ if(LTP == 0) {
+ CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0)
+ | ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
+ for (FX_INT32 w = 0; w < GRW; w += 8) {
+ nBits = GRW - w > 8 ? 8 : GRW - w;
+ if (h > 0)
+ line1 = (line1 << 8) |
+ (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
+ if (h > GRHR + GRREFERENCEDY + 1) {
+ line1_r = 0;
+ line2_r = 0;
+ line3_r = 0;
+ } else {
+ if(line1_r_ok)
+ line1_r = (line1_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
+ if(line2_r_ok)
+ line2_r = (line2_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
+ if(line3_r_ok)
+ line3_r = (line3_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
+ else {
+ line3_r = 0;
+ }
+ }
+ cVal = 0;
+ for (k = 0; k < nBits; k++) {
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
+ ((line1 >> (7 - k)) & 0x0400) |
+ ((line1_r >> (7 - k)) & 0x0040) |
+ ((line2_r >> (10 - k)) & 0x0008) |
+ ((line3_r >> (13 - k)) & 0x0001);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ } else {
+ CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0)
+ | ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
+ for (FX_INT32 w = 0; w < GRW; w += 8) {
+ nBits = GRW - w > 8 ? 8 : GRW - w;
+ if (h > 0)
+ line1 = (line1 << 8) |
+ (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
+ if(line1_r_ok)
+ line1_r = (line1_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
+ if(line2_r_ok)
+ line2_r = (line2_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
+ if(line3_r_ok)
+ line3_r = (line3_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
+ else {
+ line3_r = 0;
+ }
+ cVal = 0;
+ for (k = 0; k < nBits; k++) {
+ bVal = GRREFERENCE->getPixel(w + k, h);
+ if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w + k - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + k - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + k, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h + 1)))) {
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
+ ((line1 >> (7 - k)) & 0x0400) |
+ ((line1_r >> (7 - k)) & 0x0040) |
+ ((line2_r >> (10 - k)) & 0x0008) |
+ ((line3_r >> (13 - k)) & 0x0001);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ if (h < GRHR + GRREFERENCEDY) {
+ pLineR += nStrideR;
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ FX_DWORD line1, line2, line3, line4, line5;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ GRREG->fill(0);
+ for(FX_DWORD h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ SLTP = pArithDecoder->DECODE(&grContext[0x0008]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 0) {
+ line1 = GRREG->getPixel(1, h - 1);
+ line1 |= GRREG->getPixel(0, h - 1) << 1;
+ line1 |= GRREG->getPixel(-1, h - 1) << 2;
+ line2 = 0;
+ line3 = GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
+ line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
+ line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ CONTEXT = line5;
+ CONTEXT |= line4 << 2;
+ CONTEXT |= line3 << 5;
+ CONTEXT |= line2 << 6;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ GRREG->setPixel(w, h, bVal);
+ line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x07;
+ line2 = ((line2 << 1) | bVal) & 0x01;
+ line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1)) & 0x01;
+ line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
+ line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x03;
+ }
+ } else {
+ line1 = GRREG->getPixel(1, h - 1);
+ line1 |= GRREG->getPixel(0, h - 1) << 1;
+ line1 |= GRREG->getPixel(-1, h - 1) << 2;
+ line2 = 0;
+ line3 = GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
+ line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
+ line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ bVal = GRREFERENCE->getPixel(w, h);
+ if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h + 1)))) {
+ CONTEXT = line5;
+ CONTEXT |= line4 << 2;
+ CONTEXT |= line3 << 5;
+ CONTEXT |= line2 << 6;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ }
+ GRREG->setPixel(w, h, bVal);
+ line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x07;
+ line2 = ((line2 << 1) | bVal) & 0x01;
+ line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1)) & 0x01;
+ line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
+ line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x03;
+ }
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ FX_DWORD line1, line1_r, line2_r, line3_r;
+ FX_BYTE *pLine, *pLineR, cVal;
+ FX_INTPTR nStride, nStrideR, nOffset;
+ FX_INT32 k, nBits;
+ FX_INT32 GRWR, GRHR;
+ FX_INT32 GRW, GRH;
+ GRW = (FX_INT32)CJBig2_GRRDProc::GRW;
+ GRH = (FX_INT32)CJBig2_GRRDProc::GRH;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ if (GRREG->m_pData == NULL) {
+ delete GRREG;
+ m_pModule->JBig2_Error("Generic refinement region decoding procedure: Create Image Failed with width = %d, height = %d\n", GRW, GRH);
+ return NULL;
+ }
+ pLine = GRREG->m_pData;
+ pLineR = GRREFERENCE->m_pData;
+ nStride = GRREG->m_nStride;
+ nStrideR = GRREFERENCE->m_nStride;
+ GRWR = (FX_INT32)GRREFERENCE->m_nWidth;
+ GRHR = (FX_INT32)GRREFERENCE->m_nHeight;
+ if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
+ GRREFERENCEDY = 0;
+ }
+ nOffset = -GRREFERENCEDY * nStrideR;
+ for (FX_INT32 h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ SLTP = pArithDecoder->DECODE(&grContext[0x0008]);
+ LTP = LTP ^ SLTP;
+ }
+ line1 = (h > 0) ? pLine[-nStride] << 1 : 0;
+ FX_INT32 reference_h = h - GRREFERENCEDY;
+ FX_BOOL line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
+ FX_BOOL line2_r_ok = (reference_h > -1 && reference_h < GRHR);
+ FX_BOOL line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
+ line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
+ line2_r = line2_r_ok ? pLineR[nOffset] : 0;
+ line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
+ if(LTP == 0) {
+ CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020)
+ | ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
+ for (FX_INT32 w = 0; w < GRW; w += 8) {
+ nBits = GRW - w > 8 ? 8 : GRW - w;
+ if (h > 0)
+ line1 = (line1 << 8) |
+ (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
+ if(line1_r_ok)
+ line1_r = (line1_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
+ if(line2_r_ok)
+ line2_r = (line2_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
+ if(line3_r_ok)
+ line3_r = (line3_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
+ else {
+ line3_r = 0;
+ }
+ cVal = 0;
+ for (k = 0; k < nBits; k++) {
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
+ ((line1 >> (7 - k)) & 0x0080) |
+ ((line1_r >> (9 - k)) & 0x0020) |
+ ((line2_r >> (11 - k)) & 0x0004) |
+ ((line3_r >> (13 - k)) & 0x0001);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ } else {
+ CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020)
+ | ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
+ for (FX_INT32 w = 0; w < GRW; w += 8) {
+ nBits = GRW - w > 8 ? 8 : GRW - w;
+ if (h > 0)
+ line1 = (line1 << 8) |
+ (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
+ if(line1_r_ok)
+ line1_r = (line1_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
+ if(line2_r_ok)
+ line2_r = (line2_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
+ if(line3_r_ok)
+ line3_r = (line3_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
+ else {
+ line3_r = 0;
+ }
+ cVal = 0;
+ for (k = 0; k < nBits; k++) {
+ bVal = GRREFERENCE->getPixel(w + k, h);
+ if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w + k - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + k - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + k, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h + 1)))) {
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
+ ((line1 >> (7 - k)) & 0x0080) |
+ ((line1_r >> (9 - k)) & 0x0020) |
+ ((line2_r >> (11 - k)) & 0x0004) |
+ ((line3_r >> (13 - k)) & 0x0001);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ if (h < GRHR + GRREFERENCEDY) {
+ pLineR += nStrideR;
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_BOOL TPGRPIX, TPGRVAL;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ GRREG->fill(0);
+ for(FX_DWORD h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ switch(GRTEMPLATE) {
+ case 0:
+ CONTEXT = 0x0010;
+ break;
+ case 1:
+ CONTEXT = 0x0008;
+ break;
+ }
+ SLTP = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 0) {
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ CONTEXT = 0;
+ switch(GRTEMPLATE) {
+ case 0:
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 3;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 4;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 5;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1) << 6;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 7;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
+ CONTEXT |= GRREG->getPixel(w - 1, h) << 9;
+ CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 10;
+ CONTEXT |= GRREG->getPixel(w, h - 1) << 11;
+ CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
+ break;
+ case 1:
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 2;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 3;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 4;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 5;
+ CONTEXT |= GRREG->getPixel(w - 1, h) << 6;
+ CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 7;
+ CONTEXT |= GRREG->getPixel(w, h - 1) << 8;
+ CONTEXT |= GRREG->getPixel(w - 1, h - 1) << 9;
+ break;
+ }
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ GRREG->setPixel(w, h, bVal);
+ }
+ } else {
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ bVal = GRREFERENCE->getPixel(w, h);
+ if(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h + 1))) {
+ TPGRPIX = 1;
+ TPGRVAL = bVal;
+ } else {
+ TPGRPIX = 0;
+ }
+ if(TPGRPIX) {
+ GRREG->setPixel(w, h, TPGRVAL);
+ } else {
+ CONTEXT = 0;
+ switch(GRTEMPLATE) {
+ case 0:
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 3;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 4;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 5;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1) << 6;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 7;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
+ CONTEXT |= GRREG->getPixel(w - 1, h) << 9;
+ CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 10;
+ CONTEXT |= GRREG->getPixel(w, h - 1) << 11;
+ CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
+ break;
+ case 1:
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 2;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 3;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 4;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 5;
+ CONTEXT |= GRREG->getPixel(w - 1, h) << 6;
+ CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 7;
+ CONTEXT |= GRREG->getPixel(w, h - 1) << 8;
+ CONTEXT |= GRREG->getPixel(w - 1, h - 1) << 9;
+ break;
+ }
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ GRREG->setPixel(w, h, bVal);
+ }
+ }
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_TRDProc::decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *grContext)
+{
+ FX_INT32 STRIPT, FIRSTS;
+ FX_DWORD NINSTANCES;
+ FX_INT32 DT, DFS, CURS;
+ FX_BYTE CURT;
+ FX_INT32 SI, TI;
+ FX_DWORD IDI;
+ CJBig2_Image *IBI;
+ FX_DWORD WI, HI;
+ FX_INT32 IDS;
+ FX_BOOL RI;
+ FX_INT32 RDWI, RDHI, RDXI, RDYI;
+ CJBig2_Image *IBOI;
+ FX_DWORD WOI, HOI;
+ CJBig2_Image *SBREG;
+ FX_BOOL bFirst;
+ FX_DWORD nTmp;
+ FX_INT32 nVal, nBits;
+ CJBig2_HuffmanDecoder *pHuffmanDecoder;
+ CJBig2_GRRDProc *pGRRD;
+ CJBig2_ArithDecoder *pArithDecoder;
+ JBIG2_ALLOC(pHuffmanDecoder, CJBig2_HuffmanDecoder(pStream));
+ JBIG2_ALLOC(SBREG, CJBig2_Image(SBW, SBH));
+ SBREG->fill(SBDEFPIXEL);
+ if(pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ STRIPT *= SBSTRIPS;
+ STRIPT = -STRIPT;
+ FIRSTS = 0;
+ NINSTANCES = 0;
+ while(NINSTANCES < SBNUMINSTANCES) {
+ if(pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ DT *= SBSTRIPS;
+ STRIPT = STRIPT + DT;
+ bFirst = TRUE;
+ for(;;) {
+ if(bFirst) {
+ if(pHuffmanDecoder->decodeAValue(SBHUFFFS, &DFS) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ FIRSTS = FIRSTS + DFS;
+ CURS = FIRSTS;
+ bFirst = FALSE;
+ } else {
+ nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS);
+ if(nVal == JBIG2_OOB) {
+ break;
+ } else if(nVal != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ } else {
+ CURS = CURS + IDS + SBDSOFFSET;
+ }
+ }
+ if(SBSTRIPS == 1) {
+ CURT = 0;
+ } else {
+ nTmp = 1;
+ while((FX_DWORD)(1 << nTmp) < SBSTRIPS) {
+ nTmp ++;
+ }
+ if(pStream->readNBits(nTmp, &nVal) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ CURT = nVal;
+ }
+ TI = STRIPT + CURT;
+ nVal = 0;
+ nBits = 0;
+ for(;;) {
+ if(pStream->read1Bit(&nTmp) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ nVal = (nVal << 1) | nTmp;
+ nBits ++;
+ for(IDI = 0; IDI < SBNUMSYMS; IDI++) {
+ if((nBits == SBSYMCODES[IDI].codelen) && (nVal == SBSYMCODES[IDI].code)) {
+ break;
+ }
+ }
+ if(IDI < SBNUMSYMS) {
+ break;
+ }
+ }
+ if(SBREFINE == 0) {
+ RI = 0;
+ } else {
+ if(pStream->read1Bit(&RI) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ }
+ if(RI == 0) {
+ IBI = SBSYMS[IDI];
+ } else {
+ if((pHuffmanDecoder->decodeAValue(SBHUFFRDW, &RDWI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRDH, &RDHI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRDY, &RDYI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ pStream->alignByte();
+ nTmp = pStream->getOffset();
+ IBOI = SBSYMS[IDI];
+ if (!IBOI) {
+ goto failed;
+ }
+ WOI = IBOI->m_nWidth;
+ HOI = IBOI->m_nHeight;
+ if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): Invalid RDWI or RDHI value.");
+ goto failed;
+ }
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ pGRRD->GRW = WOI + RDWI;
+ pGRRD->GRH = HOI + RDHI;
+ pGRRD->GRTEMPLATE = SBRTEMPLATE;
+ pGRRD->GRREFERENCE = IBOI;
+ pGRRD->GRREFERENCEDX = (RDWI >> 2) + RDXI;
+ pGRRD->GRREFERENCEDY = (RDHI >> 2) + RDYI;
+ pGRRD->TPGRON = 0;
+ pGRRD->GRAT[0] = SBRAT[0];
+ pGRRD->GRAT[1] = SBRAT[1];
+ pGRRD->GRAT[2] = SBRAT[2];
+ pGRRD->GRAT[3] = SBRAT[3];
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(pStream));
+ IBI = pGRRD->decode(pArithDecoder, grContext);
+ if(IBI == NULL) {
+ delete pGRRD;
+ delete pArithDecoder;
+ goto failed;
+ }
+ delete pArithDecoder;
+ pStream->alignByte();
+ pStream->offset(2);
+ if((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) {
+ delete IBI;
+ delete pGRRD;
+ m_pModule->JBig2_Error("text region decoding procedure (huffman):"
+ "bytes processed by generic refinement region decoding procedure doesn't equal SBHUFFRSIZE.");
+ goto failed;
+ }
+ delete pGRRD;
+ }
+ if (!IBI) {
+ continue;
+ }
+ WI = IBI->m_nWidth;
+ HI = IBI->m_nHeight;
+ if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
+ CURS = CURS + WI - 1;
+ } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
+ CURS = CURS + HI - 1;
+ }
+ SI = CURS;
+ if(TRANSPOSED == 0) {
+ switch(REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ SBREG->composeFrom(SI, TI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ SBREG->composeFrom(SI - WI + 1, TI - HI + 1, IBI, SBCOMBOP);
+ break;
+ }
+ } else {
+ switch(REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ SBREG->composeFrom(TI, SI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ SBREG->composeFrom(TI - WI + 1, SI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ SBREG->composeFrom(TI, SI - HI + 1, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ SBREG->composeFrom(TI - WI + 1, SI - HI + 1, IBI, SBCOMBOP);
+ break;
+ }
+ }
+ if(RI != 0) {
+ delete IBI;
+ }
+ if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
+ CURS = CURS + WI - 1;
+ } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
+ || (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
+ CURS = CURS + HI - 1;
+ }
+ NINSTANCES = NINSTANCES + 1;
+ }
+ }
+ delete pHuffmanDecoder;
+ return SBREG;
+failed:
+ delete pHuffmanDecoder;
+ delete SBREG;
+ return NULL;
+}
+CJBig2_Image *CJBig2_TRDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext,
+ JBig2IntDecoderState *pIDS)
+{
+ FX_INT32 STRIPT, FIRSTS;
+ FX_DWORD NINSTANCES;
+ FX_INT32 DT, DFS, CURS;
+ FX_INT32 CURT;
+ FX_INT32 SI, TI;
+ FX_DWORD IDI;
+ CJBig2_Image *IBI;
+ FX_DWORD WI, HI;
+ FX_INT32 IDS;
+ FX_BOOL RI;
+ FX_INT32 RDWI, RDHI, RDXI, RDYI;
+ CJBig2_Image *IBOI;
+ FX_DWORD WOI, HOI;
+ CJBig2_Image *SBREG;
+ FX_BOOL bFirst;
+ FX_INT32 nRet, nVal;
+ FX_INT32 bRetained;
+ CJBig2_ArithIntDecoder *IADT, *IAFS, *IADS, *IAIT, *IARI, *IARDW, *IARDH, *IARDX, *IARDY;
+ CJBig2_ArithIaidDecoder *IAID;
+ CJBig2_GRRDProc *pGRRD;
+ if(pIDS) {
+ IADT = pIDS->IADT;
+ IAFS = pIDS->IAFS;
+ IADS = pIDS->IADS;
+ IAIT = pIDS->IAIT;
+ IARI = pIDS->IARI;
+ IARDW = pIDS->IARDW;
+ IARDH = pIDS->IARDH;
+ IARDX = pIDS->IARDX;
+ IARDY = pIDS->IARDY;
+ IAID = pIDS->IAID;
+ bRetained = TRUE;
+ } else {
+ JBIG2_ALLOC(IADT, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAFS, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IADS, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAIT, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARI, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDW, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDH, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDX, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDY, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAID , CJBig2_ArithIaidDecoder(SBSYMCODELEN));
+ bRetained = FALSE;
+ }
+ JBIG2_ALLOC(SBREG, CJBig2_Image(SBW, SBH));
+ SBREG->fill(SBDEFPIXEL);
+ if(IADT->decode(pArithDecoder, &STRIPT) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ STRIPT *= SBSTRIPS;
+ STRIPT = -STRIPT;
+ FIRSTS = 0;
+ NINSTANCES = 0;
+ while(NINSTANCES < SBNUMINSTANCES) {
+ if(IADT->decode(pArithDecoder, &DT) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ DT *= SBSTRIPS;
+ STRIPT = STRIPT + DT;
+ bFirst = TRUE;
+ for(;;) {
+ if(bFirst) {
+ if(IAFS->decode(pArithDecoder, &DFS) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ FIRSTS = FIRSTS + DFS;
+ CURS = FIRSTS;
+ bFirst = FALSE;
+ } else {
+ nRet = IADS->decode(pArithDecoder, &IDS);
+ if(nRet == JBIG2_OOB) {
+ break;
+ } else if(nRet != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ } else {
+ CURS = CURS + IDS + SBDSOFFSET;
+ }
+ }
+ if (NINSTANCES >= SBNUMINSTANCES) {
+ break;
+ }
+ if(SBSTRIPS == 1) {
+ CURT = 0;
+ } else {
+ if(IAIT->decode(pArithDecoder, &nVal) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ CURT = nVal;
+ }
+ TI = STRIPT + CURT;
+ if(IAID->decode(pArithDecoder, &nVal) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ IDI = nVal;
+ if(IDI >= SBNUMSYMS) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): symbol id out of range.(%d/%d)",
+ IDI, SBNUMSYMS);
+ goto failed;
+ }
+ if(SBREFINE == 0) {
+ RI = 0;
+ } else {
+ if(IARI->decode(pArithDecoder, &RI) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ }
+ if (!SBSYMS[IDI]) {
+ goto failed;
+ }
+ if(RI == 0) {
+ IBI = SBSYMS[IDI];
+ } else {
+ if((IARDW->decode(pArithDecoder, &RDWI) == -1)
+ || (IARDH->decode(pArithDecoder, &RDHI) == -1)
+ || (IARDX->decode(pArithDecoder, &RDXI) == -1)
+ || (IARDY->decode(pArithDecoder, &RDYI) == -1)) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ IBOI = SBSYMS[IDI];
+ WOI = IBOI->m_nWidth;
+ HOI = IBOI->m_nHeight;
+ if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): Invalid RDWI or RDHI value.");
+ goto failed;
+ }
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ pGRRD->GRW = WOI + RDWI;
+ pGRRD->GRH = HOI + RDHI;
+ pGRRD->GRTEMPLATE = SBRTEMPLATE;
+ pGRRD->GRREFERENCE = IBOI;
+ pGRRD->GRREFERENCEDX = (RDWI >> 1) + RDXI;
+ pGRRD->GRREFERENCEDY = (RDHI >> 1) + RDYI;
+ pGRRD->TPGRON = 0;
+ pGRRD->GRAT[0] = SBRAT[0];
+ pGRRD->GRAT[1] = SBRAT[1];
+ pGRRD->GRAT[2] = SBRAT[2];
+ pGRRD->GRAT[3] = SBRAT[3];
+ IBI = pGRRD->decode(pArithDecoder, grContext);
+ if(IBI == NULL) {
+ delete pGRRD;
+ goto failed;
+ }
+ delete pGRRD;
+ }
+ WI = IBI->m_nWidth;
+ HI = IBI->m_nHeight;
+ if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
+ CURS = CURS + WI - 1;
+ } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
+ CURS = CURS + HI - 1;
+ }
+ SI = CURS;
+ if(TRANSPOSED == 0) {
+ switch(REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ SBREG->composeFrom(SI, TI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ SBREG->composeFrom(SI - WI + 1, TI - HI + 1, IBI, SBCOMBOP);
+ break;
+ }
+ } else {
+ switch(REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ SBREG->composeFrom(TI, SI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ SBREG->composeFrom(TI - WI + 1, SI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ SBREG->composeFrom(TI, SI - HI + 1, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ SBREG->composeFrom(TI - WI + 1, SI - HI + 1, IBI, SBCOMBOP);
+ break;
+ }
+ }
+ if(RI != 0) {
+ delete IBI;
+ }
+ if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
+ CURS = CURS + WI - 1;
+ } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
+ || (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
+ CURS = CURS + HI - 1;
+ }
+ NINSTANCES = NINSTANCES + 1;
+ }
+ }
+ if(bRetained == FALSE) {
+ delete IADT;
+ delete IAFS;
+ delete IADS;
+ delete IAIT;
+ delete IARI;
+ delete IARDW;
+ delete IARDH;
+ delete IARDX;
+ delete IARDY;
+ delete IAID;
+ }
+ return SBREG;
+failed:
+ if(bRetained == FALSE) {
+ delete IADT;
+ delete IAFS;
+ delete IADS;
+ delete IAIT;
+ delete IARI;
+ delete IARDW;
+ delete IARDH;
+ delete IARDX;
+ delete IARDY;
+ delete IAID;
+ }
+ delete SBREG;
+ return NULL;
+}
+CJBig2_SymbolDict *CJBig2_SDDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
+ JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext)
+{
+ CJBig2_Image **SDNEWSYMS;
+ FX_DWORD HCHEIGHT, NSYMSDECODED;
+ FX_INT32 HCDH;
+ FX_DWORD SYMWIDTH, TOTWIDTH, HCFIRSTSYM;
+ FX_INT32 DW;
+ CJBig2_Image *BS;
+ FX_DWORD I, J, REFAGGNINST;
+ FX_BOOL *EXFLAGS;
+ FX_DWORD EXINDEX;
+ FX_BOOL CUREXFLAG;
+ FX_DWORD EXRUNLENGTH;
+ FX_INT32 nVal;
+ FX_DWORD nTmp;
+ FX_BOOL SBHUFF;
+ FX_DWORD SBNUMSYMS;
+ FX_BYTE SBSYMCODELEN;
+ FX_DWORD IDI;
+ FX_INT32 RDXI, RDYI;
+ CJBig2_Image **SBSYMS;
+ CJBig2_HuffmanTable *SBHUFFFS, *SBHUFFDS, *SBHUFFDT, *SBHUFFRDW, *SBHUFFRDH, *SBHUFFRDX, *SBHUFFRDY,
+ *SBHUFFRSIZE;
+ CJBig2_GRRDProc *pGRRD;
+ CJBig2_GRDProc *pGRD;
+ CJBig2_ArithIntDecoder *IADH, *IADW, *IAAI, *IARDX, *IARDY, *IAEX,
+ *IADT, *IAFS, *IADS, *IAIT, *IARI, *IARDW, *IARDH;
+ CJBig2_ArithIaidDecoder *IAID;
+ CJBig2_SymbolDict *pDict;
+ JBIG2_ALLOC(IADH, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IADW, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAAI, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDX, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDY, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAEX, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IADT, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAFS, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IADS, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAIT, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARI, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDW, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDH, CJBig2_ArithIntDecoder());
+ nTmp = 0;
+ while((FX_DWORD)(1 << nTmp) < (SDNUMINSYMS + SDNUMNEWSYMS)) {
+ nTmp ++;
+ }
+ JBIG2_ALLOC(IAID, CJBig2_ArithIaidDecoder((FX_BYTE)nTmp));
+ SDNEWSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SDNUMNEWSYMS, sizeof(CJBig2_Image*));
+ FXSYS_memset32(SDNEWSYMS, 0 , SDNUMNEWSYMS * sizeof(CJBig2_Image*));
+ HCHEIGHT = 0;
+ NSYMSDECODED = 0;
+ while(NSYMSDECODED < SDNUMNEWSYMS) {
+ BS = NULL;
+ if(IADH->decode(pArithDecoder, &HCDH) == -1) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ HCHEIGHT = HCHEIGHT + HCDH;
+ if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): invalid HCHEIGHT value.");
+ goto failed;
+ }
+ SYMWIDTH = 0;
+ TOTWIDTH = 0;
+ HCFIRSTSYM = NSYMSDECODED;
+ for(;;) {
+ nVal = IADW->decode(pArithDecoder, &DW);
+ if(nVal == JBIG2_OOB) {
+ break;
+ } else if(nVal != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ } else {
+ if (NSYMSDECODED >= SDNUMNEWSYMS) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): NSYMSDECODED >= SDNUMNEWSYMS.");
+ goto failed;
+ }
+ SYMWIDTH = SYMWIDTH + DW;
+ if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): invalid SYMWIDTH value.");
+ goto failed;
+ } else if (HCHEIGHT == 0 || SYMWIDTH == 0) {
+ TOTWIDTH = TOTWIDTH + SYMWIDTH;
+ SDNEWSYMS[NSYMSDECODED] = NULL;
+ NSYMSDECODED = NSYMSDECODED + 1;
+ continue;
+ }
+ TOTWIDTH = TOTWIDTH + SYMWIDTH;
+ }
+ if(SDREFAGG == 0) {
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = 0;
+ pGRD->GBW = SYMWIDTH;
+ pGRD->GBH = HCHEIGHT;
+ pGRD->GBTEMPLATE = SDTEMPLATE;
+ pGRD->TPGDON = 0;
+ pGRD->USESKIP = 0;
+ pGRD->GBAT[0] = SDAT[0];
+ pGRD->GBAT[1] = SDAT[1];
+ pGRD->GBAT[2] = SDAT[2];
+ pGRD->GBAT[3] = SDAT[3];
+ pGRD->GBAT[4] = SDAT[4];
+ pGRD->GBAT[5] = SDAT[5];
+ pGRD->GBAT[6] = SDAT[6];
+ pGRD->GBAT[7] = SDAT[7];
+ BS = pGRD->decode_Arith(pArithDecoder, gbContext);
+ if(BS == NULL) {
+ delete pGRD;
+ goto failed;
+ }
+ delete pGRD;
+ } else {
+ if(IAAI->decode(pArithDecoder, (int*)&REFAGGNINST) == -1) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ if(REFAGGNINST > 1) {
+ CJBig2_TRDProc *pDecoder;
+ JBIG2_ALLOC(pDecoder, CJBig2_TRDProc());
+ pDecoder->SBHUFF = SDHUFF;
+ pDecoder->SBREFINE = 1;
+ pDecoder->SBW = SYMWIDTH;
+ pDecoder->SBH = HCHEIGHT;
+ pDecoder->SBNUMINSTANCES = REFAGGNINST;
+ pDecoder->SBSTRIPS = 1;
+ pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
+ SBNUMSYMS = pDecoder->SBNUMSYMS;
+ nTmp = 0;
+ while((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
+ nTmp ++;
+ }
+ SBSYMCODELEN = (FX_BYTE)nTmp;
+ pDecoder->SBSYMCODELEN = SBSYMCODELEN;
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
+ pDecoder->SBSYMS = SBSYMS;
+ pDecoder->SBDEFPIXEL = 0;
+ pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
+ pDecoder->TRANSPOSED = 0;
+ pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
+ pDecoder->SBDSOFFSET = 0;
+ JBIG2_ALLOC(SBHUFFFS, CJBig2_HuffmanTable(HuffmanTable_B6,
+ sizeof(HuffmanTable_B6) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B6));
+ JBIG2_ALLOC(SBHUFFDS, CJBig2_HuffmanTable(HuffmanTable_B8,
+ sizeof(HuffmanTable_B8) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B8));
+ JBIG2_ALLOC(SBHUFFDT, CJBig2_HuffmanTable(HuffmanTable_B11,
+ sizeof(HuffmanTable_B11) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B11));
+ JBIG2_ALLOC(SBHUFFRDW, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDH, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDX, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDY, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRSIZE, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ pDecoder->SBHUFFFS = SBHUFFFS;
+ pDecoder->SBHUFFDS = SBHUFFDS;
+ pDecoder->SBHUFFDT = SBHUFFDT;
+ pDecoder->SBHUFFRDW = SBHUFFRDW;
+ pDecoder->SBHUFFRDH = SBHUFFRDH;
+ pDecoder->SBHUFFRDX = SBHUFFRDX;
+ pDecoder->SBHUFFRDY = SBHUFFRDY;
+ pDecoder->SBHUFFRSIZE = SBHUFFRSIZE;
+ pDecoder->SBRTEMPLATE = SDRTEMPLATE;
+ pDecoder->SBRAT[0] = SDRAT[0];
+ pDecoder->SBRAT[1] = SDRAT[1];
+ pDecoder->SBRAT[2] = SDRAT[2];
+ pDecoder->SBRAT[3] = SDRAT[3];
+ JBig2IntDecoderState ids;
+ ids.IADT = IADT;
+ ids.IAFS = IAFS;
+ ids.IADS = IADS;
+ ids.IAIT = IAIT;
+ ids.IARI = IARI;
+ ids.IARDW = IARDW;
+ ids.IARDH = IARDH;
+ ids.IARDX = IARDX;
+ ids.IARDY = IARDY;
+ ids.IAID = IAID;
+ BS = pDecoder->decode_Arith(pArithDecoder, grContext, &ids);
+ if(BS == NULL) {
+ m_pModule->JBig2_Free(SBSYMS);
+ delete SBHUFFFS;
+ delete SBHUFFDS;
+ delete SBHUFFDT;
+ delete SBHUFFRDW;
+ delete SBHUFFRDH;
+ delete SBHUFFRDX;
+ delete SBHUFFRDY;
+ delete SBHUFFRSIZE;
+ delete pDecoder;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(SBSYMS);
+ delete SBHUFFFS;
+ delete SBHUFFDS;
+ delete SBHUFFDT;
+ delete SBHUFFRDW;
+ delete SBHUFFRDH;
+ delete SBHUFFRDX;
+ delete SBHUFFRDY;
+ delete SBHUFFRSIZE;
+ delete pDecoder;
+ } else if(REFAGGNINST == 1) {
+ SBHUFF = SDHUFF;
+ SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
+ if(IAID->decode(pArithDecoder, (int*)&IDI) == -1) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ if((IARDX->decode(pArithDecoder, &RDXI) == -1)
+ || (IARDY->decode(pArithDecoder, &RDYI) == -1)) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ if (IDI >= SBNUMSYMS) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith):"
+ " refinement references unknown symbol %d", IDI);
+ goto failed;
+ }
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
+ if (!SBSYMS[IDI]) {
+ m_pModule->JBig2_Free(SBSYMS);
+ goto failed;
+ }
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ pGRRD->GRW = SYMWIDTH;
+ pGRRD->GRH = HCHEIGHT;
+ pGRRD->GRTEMPLATE = SDRTEMPLATE;
+ pGRRD->GRREFERENCE = SBSYMS[IDI];
+ pGRRD->GRREFERENCEDX = RDXI;
+ pGRRD->GRREFERENCEDY = RDYI;
+ pGRRD->TPGRON = 0;
+ pGRRD->GRAT[0] = SDRAT[0];
+ pGRRD->GRAT[1] = SDRAT[1];
+ pGRRD->GRAT[2] = SDRAT[2];
+ pGRRD->GRAT[3] = SDRAT[3];
+ BS = pGRRD->decode(pArithDecoder, grContext);
+ if(BS == NULL) {
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ }
+ }
+ SDNEWSYMS[NSYMSDECODED] = BS;
+ BS = NULL;
+ NSYMSDECODED = NSYMSDECODED + 1;
+ }
+ }
+ EXINDEX = 0;
+ CUREXFLAG = 0;
+ EXFLAGS = (FX_BOOL*)m_pModule->JBig2_Malloc2(sizeof(FX_BOOL), (SDNUMINSYMS + SDNUMNEWSYMS));
+ while(EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
+ if(IAEX->decode(pArithDecoder, (int*)&EXRUNLENGTH) == -1) {
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS) {
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): Invalid EXRUNLENGTH value.");
+ goto failed;
+ }
+ if(EXRUNLENGTH != 0) {
+ for(I = EXINDEX; I < EXINDEX + EXRUNLENGTH; I++) {
+ EXFLAGS[I] = CUREXFLAG;
+ }
+ }
+ EXINDEX = EXINDEX + EXRUNLENGTH;
+ CUREXFLAG = !CUREXFLAG;
+ }
+ JBIG2_ALLOC(pDict, CJBig2_SymbolDict());
+ pDict->SDNUMEXSYMS = SDNUMEXSYMS;
+ pDict->SDEXSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), SDNUMEXSYMS);
+ I = J = 0;
+ for(I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) {
+ if(EXFLAGS[I] && J < SDNUMEXSYMS) {
+ if(I < SDNUMINSYMS) {
+ JBIG2_ALLOC(pDict->SDEXSYMS[J], CJBig2_Image(*SDINSYMS[I]));
+ } else {
+ pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS];
+ }
+ J = J + 1;
+ } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) {
+ delete SDNEWSYMS[I - SDNUMINSYMS];
+ }
+ }
+ if (J < SDNUMEXSYMS) {
+ pDict->SDNUMEXSYMS = J;
+ }
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Free(SDNEWSYMS);
+ delete IADH;
+ delete IADW;
+ delete IAAI;
+ delete IARDX;
+ delete IARDY;
+ delete IAEX;
+ delete IAID;
+ delete IADT;
+ delete IAFS;
+ delete IADS;
+ delete IAIT;
+ delete IARI;
+ delete IARDW;
+ delete IARDH;
+ return pDict;
+failed:
+ for(I = 0; I < NSYMSDECODED; I++) {
+ if (SDNEWSYMS[I]) {
+ delete SDNEWSYMS[I];
+ SDNEWSYMS[I] = NULL;
+ }
+ }
+ m_pModule->JBig2_Free(SDNEWSYMS);
+ delete IADH;
+ delete IADW;
+ delete IAAI;
+ delete IARDX;
+ delete IARDY;
+ delete IAEX;
+ delete IAID;
+ delete IADT;
+ delete IAFS;
+ delete IADS;
+ delete IAIT;
+ delete IARI;
+ delete IARDW;
+ delete IARDH;
+ return NULL;
+}
+CJBig2_SymbolDict *CJBig2_SDDProc::decode_Huffman(CJBig2_BitStream *pStream,
+ JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext, IFX_Pause* pPause)
+{
+ CJBig2_Image **SDNEWSYMS;
+ FX_DWORD *SDNEWSYMWIDTHS;
+ FX_DWORD HCHEIGHT, NSYMSDECODED;
+ FX_INT32 HCDH;
+ FX_DWORD SYMWIDTH, TOTWIDTH, HCFIRSTSYM;
+ FX_INT32 DW;
+ CJBig2_Image *BS, *BHC;
+ FX_DWORD I, J, REFAGGNINST;
+ FX_BOOL *EXFLAGS;
+ FX_DWORD EXINDEX;
+ FX_BOOL CUREXFLAG;
+ FX_DWORD EXRUNLENGTH;
+ FX_INT32 nVal, nBits;
+ FX_DWORD nTmp;
+ FX_BOOL SBHUFF;
+ FX_DWORD SBNUMSYMS;
+ FX_BYTE SBSYMCODELEN;
+ JBig2HuffmanCode *SBSYMCODES;
+ FX_DWORD IDI;
+ FX_INT32 RDXI, RDYI;
+ FX_DWORD BMSIZE;
+ FX_DWORD stride;
+ CJBig2_Image **SBSYMS;
+ CJBig2_HuffmanTable *SBHUFFFS, *SBHUFFDS, *SBHUFFDT, *SBHUFFRDW, *SBHUFFRDH, *SBHUFFRDX, *SBHUFFRDY,
+ *SBHUFFRSIZE, *pTable;
+ CJBig2_HuffmanDecoder *pHuffmanDecoder;
+ CJBig2_GRRDProc *pGRRD;
+ CJBig2_ArithDecoder *pArithDecoder;
+ CJBig2_GRDProc *pGRD;
+ CJBig2_SymbolDict *pDict;
+ JBIG2_ALLOC(pHuffmanDecoder, CJBig2_HuffmanDecoder(pStream));
+ SDNEWSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SDNUMNEWSYMS, sizeof(CJBig2_Image*));
+ FXSYS_memset32(SDNEWSYMS, 0 , SDNUMNEWSYMS * sizeof(CJBig2_Image*));
+ SDNEWSYMWIDTHS = NULL;
+ BHC = NULL;
+ if(SDREFAGG == 0) {
+ SDNEWSYMWIDTHS = (FX_DWORD *)m_pModule->JBig2_Malloc2(SDNUMNEWSYMS, sizeof(FX_DWORD));
+ FXSYS_memset32(SDNEWSYMWIDTHS, 0 , SDNUMNEWSYMS * sizeof(FX_DWORD));
+ }
+ HCHEIGHT = 0;
+ NSYMSDECODED = 0;
+ BS = NULL;
+ while(NSYMSDECODED < SDNUMNEWSYMS) {
+ if(pHuffmanDecoder->decodeAValue(SDHUFFDH, &HCDH) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ HCHEIGHT = HCHEIGHT + HCDH;
+ if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): invalid HCHEIGHT value.");
+ goto failed;
+ }
+ SYMWIDTH = 0;
+ TOTWIDTH = 0;
+ HCFIRSTSYM = NSYMSDECODED;
+ for(;;) {
+ nVal = pHuffmanDecoder->decodeAValue(SDHUFFDW, &DW);
+ if(nVal == JBIG2_OOB) {
+ break;
+ } else if(nVal != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ } else {
+ if (NSYMSDECODED >= SDNUMNEWSYMS) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): NSYMSDECODED >= SDNUMNEWSYMS.");
+ goto failed;
+ }
+ SYMWIDTH = SYMWIDTH + DW;
+ if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): invalid SYMWIDTH value.");
+ goto failed;
+ } else if (HCHEIGHT == 0 || SYMWIDTH == 0) {
+ TOTWIDTH = TOTWIDTH + SYMWIDTH;
+ SDNEWSYMS[NSYMSDECODED] = NULL;
+ NSYMSDECODED = NSYMSDECODED + 1;
+ continue;
+ }
+ TOTWIDTH = TOTWIDTH + SYMWIDTH;
+ }
+ if(SDREFAGG == 1) {
+ if(pHuffmanDecoder->decodeAValue(SDHUFFAGGINST, (int*)&REFAGGNINST) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ BS = NULL;
+ if(REFAGGNINST > 1) {
+ CJBig2_TRDProc *pDecoder;
+ JBIG2_ALLOC(pDecoder, CJBig2_TRDProc());
+ pDecoder->SBHUFF = SDHUFF;
+ pDecoder->SBREFINE = 1;
+ pDecoder->SBW = SYMWIDTH;
+ pDecoder->SBH = HCHEIGHT;
+ pDecoder->SBNUMINSTANCES = REFAGGNINST;
+ pDecoder->SBSTRIPS = 1;
+ pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
+ SBNUMSYMS = pDecoder->SBNUMSYMS;
+ SBSYMCODES = (JBig2HuffmanCode*)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(JBig2HuffmanCode));
+ nTmp = 1;
+ while((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
+ nTmp ++;
+ }
+ for(I = 0; I < SBNUMSYMS; I++) {
+ SBSYMCODES[I].codelen = nTmp;
+ SBSYMCODES[I].code = I;
+ }
+ pDecoder->SBSYMCODES = SBSYMCODES;
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
+ pDecoder->SBSYMS = SBSYMS;
+ pDecoder->SBDEFPIXEL = 0;
+ pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
+ pDecoder->TRANSPOSED = 0;
+ pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
+ pDecoder->SBDSOFFSET = 0;
+ JBIG2_ALLOC(SBHUFFFS, CJBig2_HuffmanTable(HuffmanTable_B6,
+ sizeof(HuffmanTable_B6) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B6));
+ JBIG2_ALLOC(SBHUFFDS, CJBig2_HuffmanTable(HuffmanTable_B8,
+ sizeof(HuffmanTable_B8) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B8));
+ JBIG2_ALLOC(SBHUFFDT, CJBig2_HuffmanTable(HuffmanTable_B11,
+ sizeof(HuffmanTable_B11) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B11));
+ JBIG2_ALLOC(SBHUFFRDW, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDH, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDX, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDY, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRSIZE, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ pDecoder->SBHUFFFS = SBHUFFFS;
+ pDecoder->SBHUFFDS = SBHUFFDS;
+ pDecoder->SBHUFFDT = SBHUFFDT;
+ pDecoder->SBHUFFRDW = SBHUFFRDW;
+ pDecoder->SBHUFFRDH = SBHUFFRDH;
+ pDecoder->SBHUFFRDX = SBHUFFRDX;
+ pDecoder->SBHUFFRDY = SBHUFFRDY;
+ pDecoder->SBHUFFRSIZE = SBHUFFRSIZE;
+ pDecoder->SBRTEMPLATE = SDRTEMPLATE;
+ pDecoder->SBRAT[0] = SDRAT[0];
+ pDecoder->SBRAT[1] = SDRAT[1];
+ pDecoder->SBRAT[2] = SDRAT[2];
+ pDecoder->SBRAT[3] = SDRAT[3];
+ BS = pDecoder->decode_Huffman(pStream, grContext);
+ if(BS == NULL) {
+ m_pModule->JBig2_Free(SBSYMCODES);
+ m_pModule->JBig2_Free(SBSYMS);
+ delete SBHUFFFS;
+ delete SBHUFFDS;
+ delete SBHUFFDT;
+ delete SBHUFFRDW;
+ delete SBHUFFRDH;
+ delete SBHUFFRDX;
+ delete SBHUFFRDY;
+ delete SBHUFFRSIZE;
+ delete pDecoder;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(SBSYMCODES);
+ m_pModule->JBig2_Free(SBSYMS);
+ delete SBHUFFFS;
+ delete SBHUFFDS;
+ delete SBHUFFDT;
+ delete SBHUFFRDW;
+ delete SBHUFFRDH;
+ delete SBHUFFRDX;
+ delete SBHUFFRDY;
+ delete SBHUFFRSIZE;
+ delete pDecoder;
+ } else if(REFAGGNINST == 1) {
+ SBHUFF = SDHUFF;
+ SBNUMSYMS = SDNUMINSYMS + SDNUMNEWSYMS;
+ nTmp = 1;
+ while((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
+ nTmp ++;
+ }
+ SBSYMCODELEN = (FX_BYTE)nTmp;
+ SBSYMCODES = (JBig2HuffmanCode*)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(JBig2HuffmanCode));
+ for(I = 0; I < SBNUMSYMS; I++) {
+ SBSYMCODES[I].codelen = SBSYMCODELEN;
+ SBSYMCODES[I].code = I;
+ }
+ nVal = 0;
+ nBits = 0;
+ for(;;) {
+ if(pStream->read1Bit(&nTmp) != 0) {
+ m_pModule->JBig2_Free(SBSYMCODES);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ nVal = (nVal << 1) | nTmp;
+ for(IDI = 0; IDI < SBNUMSYMS; IDI++) {
+ if((nVal == SBSYMCODES[IDI].code)
+ && (nBits == SBSYMCODES[IDI].codelen)) {
+ break;
+ }
+ }
+ if(IDI < SBNUMSYMS) {
+ break;
+ }
+ }
+ m_pModule->JBig2_Free(SBSYMCODES);
+ JBIG2_ALLOC(SBHUFFRDX, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRSIZE, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ if((pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDYI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) {
+ delete SBHUFFRDX;
+ delete SBHUFFRSIZE;
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ delete SBHUFFRDX;
+ delete SBHUFFRSIZE;
+ pStream->alignByte();
+ nTmp = pStream->getOffset();
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ pGRRD->GRW = SYMWIDTH;
+ pGRRD->GRH = HCHEIGHT;
+ pGRRD->GRTEMPLATE = SDRTEMPLATE;
+ pGRRD->GRREFERENCE = SBSYMS[IDI];
+ pGRRD->GRREFERENCEDX = RDXI;
+ pGRRD->GRREFERENCEDY = RDYI;
+ pGRRD->TPGRON = 0;
+ pGRRD->GRAT[0] = SDRAT[0];
+ pGRRD->GRAT[1] = SDRAT[1];
+ pGRRD->GRAT[2] = SDRAT[2];
+ pGRRD->GRAT[3] = SDRAT[3];
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(pStream));
+ BS = pGRRD->decode(pArithDecoder, grContext);
+ if(BS == NULL) {
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ delete pArithDecoder;
+ goto failed;
+ }
+ pStream->alignByte();
+ pStream->offset(2);
+ if((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) {
+ delete BS;
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ delete pArithDecoder;
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman):"
+ "bytes processed by generic refinement region decoding procedure doesn't equal SBHUFFRSIZE.");
+ goto failed;
+ }
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ delete pArithDecoder;
+ }
+ SDNEWSYMS[NSYMSDECODED] = BS;
+ }
+ if(SDREFAGG == 0) {
+ SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
+ }
+ NSYMSDECODED = NSYMSDECODED + 1;
+ }
+ if(SDREFAGG == 0) {
+ if(pHuffmanDecoder->decodeAValue(SDHUFFBMSIZE, (FX_INT32*)&BMSIZE) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ pStream->alignByte();
+ if(BMSIZE == 0) {
+ stride = (TOTWIDTH + 7) >> 3;
+ if(pStream->getByteLeft() >= stride * HCHEIGHT) {
+ JBIG2_ALLOC(BHC, CJBig2_Image(TOTWIDTH, HCHEIGHT));
+ for(I = 0; I < HCHEIGHT; I ++) {
+ JBIG2_memcpy(BHC->m_pData + I * BHC->m_nStride, pStream->getPointer(), stride);
+ pStream->offset(stride);
+ }
+ } else {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ } else {
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = 1;
+ pGRD->GBW = TOTWIDTH;
+ pGRD->GBH = HCHEIGHT;
+ FXCODEC_STATUS status = pGRD->Start_decode_MMR(&BHC, pStream);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ delete pGRD;
+ pStream->alignByte();
+ }
+ nTmp = 0;
+ if (!BHC) {
+ continue;
+ }
+ for(I = HCFIRSTSYM; I < NSYMSDECODED; I++) {
+ SDNEWSYMS[I] = BHC->subImage(nTmp, 0, SDNEWSYMWIDTHS[I], HCHEIGHT);
+ nTmp += SDNEWSYMWIDTHS[I];
+ }
+ delete BHC;
+ BHC = NULL;
+ }
+ }
+ EXINDEX = 0;
+ CUREXFLAG = 0;
+ JBIG2_ALLOC(pTable, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ EXFLAGS = (FX_BOOL*)m_pModule->JBig2_Malloc2(sizeof(FX_BOOL), (SDNUMINSYMS + SDNUMNEWSYMS));
+ while(EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
+ if(pHuffmanDecoder->decodeAValue(pTable, (int*)&EXRUNLENGTH) != 0) {
+ delete pTable;
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS) {
+ delete pTable;
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): Invalid EXRUNLENGTH value.");
+ goto failed;
+ }
+ if(EXRUNLENGTH != 0) {
+ for(I = EXINDEX; I < EXINDEX + EXRUNLENGTH; I++) {
+ EXFLAGS[I] = CUREXFLAG;
+ }
+ }
+ EXINDEX = EXINDEX + EXRUNLENGTH;
+ CUREXFLAG = !CUREXFLAG;
+ }
+ delete pTable;
+ JBIG2_ALLOC(pDict, CJBig2_SymbolDict());
+ pDict->SDNUMEXSYMS = SDNUMEXSYMS;
+ pDict->SDEXSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), SDNUMEXSYMS);
+ I = J = 0;
+ for(I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) {
+ if(EXFLAGS[I] && J < SDNUMEXSYMS) {
+ if(I < SDNUMINSYMS) {
+ JBIG2_ALLOC(pDict->SDEXSYMS[J], CJBig2_Image(*SDINSYMS[I]));
+ } else {
+ pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS];
+ }
+ J = J + 1;
+ } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) {
+ delete SDNEWSYMS[I - SDNUMINSYMS];
+ }
+ }
+ if (J < SDNUMEXSYMS) {
+ pDict->SDNUMEXSYMS = J;
+ }
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Free(SDNEWSYMS);
+ if(SDREFAGG == 0) {
+ m_pModule->JBig2_Free(SDNEWSYMWIDTHS);
+ }
+ delete pHuffmanDecoder;
+ return pDict;
+failed:
+ for(I = 0; I < NSYMSDECODED; I++) {
+ if (SDNEWSYMS[I]) {
+ delete SDNEWSYMS[I];
+ }
+ }
+ m_pModule->JBig2_Free(SDNEWSYMS);
+ if(SDREFAGG == 0) {
+ m_pModule->JBig2_Free(SDNEWSYMWIDTHS);
+ }
+ delete pHuffmanDecoder;
+ return NULL;
+}
+CJBig2_Image *CJBig2_HTRDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
+ JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_DWORD ng, mg;
+ FX_INT32 x, y;
+ CJBig2_Image *HSKIP;
+ FX_DWORD HBPP;
+ FX_DWORD *GI;
+ CJBig2_Image *HTREG;
+ CJBig2_GSIDProc *pGID;
+ JBIG2_ALLOC(HTREG, CJBig2_Image(HBW, HBH));
+ HTREG->fill(HDEFPIXEL);
+ HSKIP = NULL;
+ if(HENABLESKIP == 1) {
+ JBIG2_ALLOC(HSKIP, CJBig2_Image(HGW, HGH));
+ for(mg = 0; mg < HGH; mg++) {
+ for(ng = 0; ng < HGW; ng++) {
+ x = (HGX + mg * HRY + ng * HRX) >> 8;
+ y = (HGY + mg * HRX - ng * HRY) >> 8;
+ if((x + HPW <= 0) | (x >= (FX_INT32)HBW)
+ | (y + HPH <= 0) | (y >= (FX_INT32)HPH)) {
+ HSKIP->setPixel(ng, mg, 1);
+ } else {
+ HSKIP->setPixel(ng, mg, 0);
+ }
+ }
+ }
+ }
+ HBPP = 1;
+ while((FX_DWORD)(1 << HBPP) < HNUMPATS) {
+ HBPP ++;
+ }
+ JBIG2_ALLOC(pGID, CJBig2_GSIDProc());
+ pGID->GSMMR = HMMR;
+ pGID->GSW = HGW;
+ pGID->GSH = HGH;
+ pGID->GSBPP = (FX_BYTE)HBPP;
+ pGID->GSUSESKIP = HENABLESKIP;
+ pGID->GSKIP = HSKIP;
+ pGID->GSTEMPLATE = HTEMPLATE;
+ GI = pGID->decode_Arith(pArithDecoder, gbContext, pPause);
+ if(GI == NULL) {
+ goto failed;
+ }
+ for(mg = 0; mg < HGH; mg++) {
+ for(ng = 0; ng < HGW; ng++) {
+ x = (HGX + mg * HRY + ng * HRX) >> 8;
+ y = (HGY + mg * HRX - ng * HRY) >> 8;
+ FX_DWORD pat_index = GI[mg * HGW + ng];
+ if (pat_index >= HNUMPATS) {
+ pat_index = HNUMPATS - 1;
+ }
+ HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
+ }
+ }
+ m_pModule->JBig2_Free(GI);
+ if(HSKIP) {
+ delete HSKIP;
+ }
+ delete pGID;
+ return HTREG;
+failed:
+ if(HSKIP) {
+ delete HSKIP;
+ }
+ delete pGID;
+ delete HTREG;
+ return NULL;
+}
+CJBig2_Image *CJBig2_HTRDProc::decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause)
+{
+ FX_DWORD ng, mg;
+ FX_INT32 x, y;
+ FX_DWORD HBPP;
+ FX_DWORD *GI;
+ CJBig2_Image *HTREG;
+ CJBig2_GSIDProc *pGID;
+ JBIG2_ALLOC(HTREG, CJBig2_Image(HBW, HBH));
+ HTREG->fill(HDEFPIXEL);
+ HBPP = 1;
+ while((FX_DWORD)(1 << HBPP) < HNUMPATS) {
+ HBPP ++;
+ }
+ JBIG2_ALLOC(pGID, CJBig2_GSIDProc());
+ pGID->GSMMR = HMMR;
+ pGID->GSW = HGW;
+ pGID->GSH = HGH;
+ pGID->GSBPP = (FX_BYTE)HBPP;
+ pGID->GSUSESKIP = 0;
+ GI = pGID->decode_MMR(pStream, pPause);
+ if(GI == NULL) {
+ goto failed;
+ }
+ for(mg = 0; mg < HGH; mg++) {
+ for(ng = 0; ng < HGW; ng++) {
+ x = (HGX + mg * HRY + ng * HRX) >> 8;
+ y = (HGY + mg * HRX - ng * HRY) >> 8;
+ FX_DWORD pat_index = GI[mg * HGW + ng];
+ if (pat_index >= HNUMPATS) {
+ pat_index = HNUMPATS - 1;
+ }
+ HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
+ }
+ }
+ m_pModule->JBig2_Free(GI);
+ delete pGID;
+ return HTREG;
+failed:
+ delete pGID;
+ delete HTREG;
+ return NULL;
+}
+CJBig2_PatternDict *CJBig2_PDDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
+ JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_DWORD GRAY;
+ CJBig2_Image *BHDC = NULL;
+ CJBig2_PatternDict *pDict;
+ CJBig2_GRDProc *pGRD;
+ JBIG2_ALLOC(pDict, CJBig2_PatternDict());
+ pDict->NUMPATS = GRAYMAX + 1;
+ pDict->HDPATS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), pDict->NUMPATS);
+ JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*)*pDict->NUMPATS);
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = HDMMR;
+ pGRD->GBW = (GRAYMAX + 1) * HDPW;
+ pGRD->GBH = HDPH;
+ pGRD->GBTEMPLATE = HDTEMPLATE;
+ pGRD->TPGDON = 0;
+ pGRD->USESKIP = 0;
+ pGRD->GBAT[0] = -(FX_INT32)HDPW;
+ pGRD->GBAT[1] = 0;
+ if(pGRD->GBTEMPLATE == 0) {
+ pGRD->GBAT[2] = -3;
+ pGRD->GBAT[3] = -1;
+ pGRD->GBAT[4] = 2;
+ pGRD->GBAT[5] = -2;
+ pGRD->GBAT[6] = -2;
+ pGRD->GBAT[7] = -2;
+ }
+ FXCODEC_STATUS status = pGRD->Start_decode_Arith(&BHDC, pArithDecoder, gbContext);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(BHDC == NULL) {
+ delete pGRD;
+ goto failed;
+ }
+ delete pGRD;
+ GRAY = 0;
+ while(GRAY <= GRAYMAX) {
+ pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
+ GRAY = GRAY + 1;
+ }
+ delete BHDC;
+ return pDict;
+failed:
+ delete pDict;
+ return NULL;
+}
+
+CJBig2_PatternDict *CJBig2_PDDProc::decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause)
+{
+ FX_DWORD GRAY;
+ CJBig2_Image *BHDC = NULL;
+ CJBig2_PatternDict *pDict;
+ CJBig2_GRDProc *pGRD;
+ JBIG2_ALLOC(pDict, CJBig2_PatternDict());
+ pDict->NUMPATS = GRAYMAX + 1;
+ pDict->HDPATS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), pDict->NUMPATS);
+ JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*)*pDict->NUMPATS);
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = HDMMR;
+ pGRD->GBW = (GRAYMAX + 1) * HDPW;
+ pGRD->GBH = HDPH;
+ FXCODEC_STATUS status = pGRD->Start_decode_MMR(&BHDC, pStream);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(BHDC == NULL) {
+ delete pGRD;
+ goto failed;
+ }
+ delete pGRD;
+ GRAY = 0;
+ while(GRAY <= GRAYMAX) {
+ pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
+ GRAY = GRAY + 1;
+ }
+ delete BHDC;
+ return pDict;
+failed:
+ delete pDict;
+ return NULL;
+}
+FX_DWORD *CJBig2_GSIDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
+ JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ CJBig2_Image **GSPLANES;
+ FX_INT32 J, K;
+ FX_DWORD x, y;
+ FX_DWORD *GSVALS;
+ CJBig2_GRDProc *pGRD;
+ GSPLANES = (CJBig2_Image **)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), GSBPP);
+ if (!GSPLANES) {
+ return NULL;
+ }
+ GSVALS = (FX_DWORD*)m_pModule->JBig2_Malloc3(sizeof(FX_DWORD), GSW, GSH);
+ if (!GSVALS) {
+ m_pModule->JBig2_Free(GSPLANES);
+ return NULL;
+ }
+ JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*)*GSBPP);
+ JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD)*GSW * GSH);
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = GSMMR;
+ pGRD->GBW = GSW;
+ pGRD->GBH = GSH;
+ pGRD->GBTEMPLATE = GSTEMPLATE;
+ pGRD->TPGDON = 0;
+ pGRD->USESKIP = GSUSESKIP;
+ pGRD->SKIP = GSKIP;
+ if(GSTEMPLATE <= 1) {
+ pGRD->GBAT[0] = 3;
+ } else {
+ pGRD->GBAT[0] = 2;
+ }
+ pGRD->GBAT[1] = -1;
+ if(pGRD->GBTEMPLATE == 0) {
+ pGRD->GBAT[2] = -3;
+ pGRD->GBAT[3] = -1;
+ pGRD->GBAT[4] = 2;
+ pGRD->GBAT[5] = -2;
+ pGRD->GBAT[6] = -2;
+ pGRD->GBAT[7] = -2;
+ }
+ FXCODEC_STATUS status = pGRD->Start_decode_Arith(&GSPLANES[GSBPP - 1], pArithDecoder, gbContext);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(GSPLANES[GSBPP - 1] == NULL) {
+ goto failed;
+ }
+ J = GSBPP - 2;
+ while(J >= 0) {
+ FXCODEC_STATUS status = pGRD->Start_decode_Arith(&GSPLANES[J], pArithDecoder, gbContext);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(GSPLANES[J] == NULL) {
+ for(K = GSBPP - 1; K > J; K--) {
+ delete GSPLANES[K];
+ goto failed;
+ }
+ }
+ GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR);
+ J = J - 1;
+ }
+ for(y = 0; y < GSH; y++) {
+ for(x = 0; x < GSW; x++) {
+ for(J = 0; J < GSBPP; J++) {
+ GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J;
+ }
+ }
+ }
+ for(J = 0; J < GSBPP; J++) {
+ delete GSPLANES[J];
+ }
+ m_pModule->JBig2_Free(GSPLANES);
+ delete pGRD;
+ return GSVALS;
+failed:
+ m_pModule->JBig2_Free(GSPLANES);
+ delete pGRD;
+ m_pModule->JBig2_Free(GSVALS);
+ return NULL;
+}
+FX_DWORD *CJBig2_GSIDProc::decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause)
+{
+ CJBig2_Image **GSPLANES;
+ FX_INT32 J, K;
+ FX_DWORD x, y;
+ FX_DWORD *GSVALS;
+ CJBig2_GRDProc *pGRD;
+ GSPLANES = (CJBig2_Image **)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), GSBPP);
+ if (!GSPLANES) {
+ return NULL;
+ }
+ GSVALS = (FX_DWORD*)m_pModule->JBig2_Malloc3(sizeof(FX_DWORD), GSW, GSH);
+ if (!GSVALS) {
+ if (GSPLANES) {
+ m_pModule->JBig2_Free(GSPLANES);
+ }
+ return NULL;
+ }
+ JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*)*GSBPP);
+ JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD)*GSW * GSH);
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = GSMMR;
+ pGRD->GBW = GSW;
+ pGRD->GBH = GSH;
+ FXCODEC_STATUS status = pGRD->Start_decode_MMR(&GSPLANES[GSBPP - 1], pStream);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(GSPLANES[GSBPP - 1] == NULL) {
+ goto failed;
+ }
+ pStream->alignByte();
+ pStream->offset(3);
+ J = GSBPP - 2;
+ while(J >= 0) {
+ FXCODEC_STATUS status = pGRD->Start_decode_MMR(&GSPLANES[J], pStream);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(GSPLANES[J] == NULL) {
+ for(K = GSBPP - 1; K > J; K--) {
+ delete GSPLANES[K];
+ goto failed;
+ }
+ }
+ pStream->alignByte();
+ pStream->offset(3);
+ GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR);
+ J = J - 1;
+ }
+ for(y = 0; y < GSH; y++) {
+ for(x = 0; x < GSW; x++) {
+ for(J = 0; J < GSBPP; J++) {
+ GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J;
+ }
+ }
+ }
+ for(J = 0; J < GSBPP; J++) {
+ delete GSPLANES[J];
+ }
+ m_pModule->JBig2_Free(GSPLANES);
+ delete pGRD;
+ return GSVALS;
+failed:
+ m_pModule->JBig2_Free(GSPLANES);
+ delete pGRD;
+ m_pModule->JBig2_Free(GSVALS);
+ return NULL;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ if (GBW == 0 || GBH == 0) {
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
+ m_pPause = pPause;
+ if(*pImage == NULL) {
+ JBIG2_ALLOC((*pImage), CJBig2_Image(GBW, GBH));
+ }
+ if ((*pImage)->m_pData == NULL) {
+ delete *pImage;
+ *pImage = NULL;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return FXCODEC_STATUS_ERROR;
+ }
+ m_DecodeType = 1;
+ m_pImage = pImage;
+ (*m_pImage)->fill(0);
+ m_pArithDecoder = pArithDecoder;
+ m_gbContext = gbContext;
+ LTP = 0;
+ m_pLine = NULL;
+ m_loopIndex = 0;
+ return decode_Arith(pPause);
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith(IFX_Pause* pPause)
+{
+ int iline = m_loopIndex;
+ CJBig2_Image* pImage = *m_pImage;
+ if(GBTEMPLATE == 0) {
+ if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)
+ && (GBAT[2] == (signed char) - 3) && (GBAT[3] == (signed char) - 1)
+ && (GBAT[4] == 2) && (GBAT[5] == (signed char) - 2)
+ && (GBAT[6] == (signed char) - 2) && (GBAT[7] == (signed char) - 2)) {
+ m_ProssiveStatus = decode_Arith_Template0_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProssiveStatus = decode_Arith_Template0_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
+ }
+ } else if(GBTEMPLATE == 1) {
+ if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)) {
+ m_ProssiveStatus = decode_Arith_Template1_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProssiveStatus = decode_Arith_Template1_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
+ }
+ } else if(GBTEMPLATE == 2) {
+ if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
+ m_ProssiveStatus = decode_Arith_Template2_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProssiveStatus = decode_Arith_Template2_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
+ }
+ } else {
+ if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
+ m_ProssiveStatus = decode_Arith_Template3_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProssiveStatus = decode_Arith_Template3_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
+ }
+ }
+ m_ReplaceRect.left = 0;
+ m_ReplaceRect.right = pImage->m_nWidth;
+ m_ReplaceRect.top = iline;
+ m_ReplaceRect.bottom = m_loopIndex;
+ if(m_ProssiveStatus == FXCODEC_STATUS_DECODE_FINISH) {
+ m_loopIndex = 0;
+ }
+ return m_ProssiveStatus;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith_V2(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ if(GBW == 0 || GBH == 0) {
+ * pImage = NULL;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+ }
+ if(*pImage == NULL) {
+ JBIG2_ALLOC((*pImage), CJBig2_Image(GBW, GBH));
+ }
+ if ((*pImage)->m_pData == NULL) {
+ delete *pImage;
+ *pImage = NULL;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return FXCODEC_STATUS_ERROR;
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
+ m_DecodeType = 2;
+ m_pPause = pPause;
+ m_pImage = pImage;
+ (*m_pImage)->fill(0);
+ LTP = 0;
+ m_loopIndex = 0;
+ m_pArithDecoder = pArithDecoder;
+ m_gbContext = gbContext;
+ return decode_Arith_V2(pPause);
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_V2(IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG = *m_pImage;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT = 0x9b25;
+ break;
+ case 1:
+ CONTEXT = 0x0795;
+ break;
+ case 2:
+ CONTEXT = 0x00e5;
+ break;
+ case 3:
+ CONTEXT = 0x0195;
+ break;
+ }
+ SLTP = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ switch(GBTEMPLATE) {
+ case 0: {
+ line1 = GBREG->getPixel(1, m_loopIndex - 2);
+ line1 |= GBREG->getPixel(0, m_loopIndex - 2) << 1;
+ line2 = GBREG->getPixel(2, m_loopIndex - 1);
+ line2 |= GBREG->getPixel(1, m_loopIndex - 1) << 1;
+ line2 |= GBREG->getPixel(0, m_loopIndex - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= line2 << 5;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
+ CONTEXT |= line1 << 12;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ break;
+ case 1: {
+ line1 = GBREG->getPixel(2, m_loopIndex - 2);
+ line1 |= GBREG->getPixel(1, m_loopIndex - 2) << 1;
+ line1 |= GBREG->getPixel(0, m_loopIndex - 2) << 2;
+ line2 = GBREG->getPixel(2, m_loopIndex - 1);
+ line2 |= GBREG->getPixel(1, m_loopIndex - 1) << 1;
+ line2 |= GBREG->getPixel(0, m_loopIndex - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 9;
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, m_loopIndex - 2)) & 0x0f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ break;
+ case 2: {
+ line1 = GBREG->getPixel(1, m_loopIndex - 2);
+ line1 |= GBREG->getPixel(0, m_loopIndex - 2) << 1;
+ line2 = GBREG->getPixel(1, m_loopIndex - 1);
+ line2 |= GBREG->getPixel(0, m_loopIndex - 1) << 1;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 7;
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 1)) & 0x0f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ break;
+ case 3: {
+ line1 = GBREG->getPixel(1, m_loopIndex - 1);
+ line1 |= GBREG->getPixel(0, m_loopIndex - 1) << 1;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= line1 << 5;
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 1)) & 0x1f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ break;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex ++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith_V1(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ if(GBW == 0 || GBH == 0) {
+ * pImage = NULL;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+ }
+ if(*pImage == NULL) {
+ JBIG2_ALLOC((*pImage), CJBig2_Image(GBW, GBH));
+ }
+ if ((*pImage)->m_pData == NULL) {
+ delete *pImage;
+ *pImage = NULL;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return FXCODEC_STATUS_ERROR;
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
+ m_pPause = pPause;
+ m_pImage = pImage;
+ m_DecodeType = 3;
+ (*m_pImage)->fill(0);
+ LTP = 0;
+ m_loopIndex = 0;
+ m_pArithDecoder = pArithDecoder;
+ m_gbContext = gbContext;
+ return decode_Arith_V1(pPause);
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_V1(IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT = 0;
+ CJBig2_Image *GBREG = (*m_pImage);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT = 0x9b25;
+ break;
+ case 1:
+ CONTEXT = 0x0795;
+ break;
+ case 2:
+ CONTEXT = 0x00e5;
+ break;
+ case 3:
+ CONTEXT = 0x0195;
+ break;
+ }
+ SLTP = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ GBREG->setPixel(w, m_loopIndex, GBREG->getPixel(w, m_loopIndex - 1));
+ }
+ } else {
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ GBREG->setPixel(w, m_loopIndex, 0);
+ } else {
+ CONTEXT = 0;
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex) << 2;
+ CONTEXT |= GBREG->getPixel(w - 4, m_loopIndex) << 3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= GBREG->getPixel(w + 2, m_loopIndex - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 9;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 2) << 12;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 2) << 13;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 2) << 14;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
+ break;
+ case 1:
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex) << 2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 3;
+ CONTEXT |= GBREG->getPixel(w + 2, m_loopIndex - 1) << 4;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w + 2, m_loopIndex - 2) << 9;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 2) << 10;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 2) << 11;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 2) << 12;
+ break;
+ case 2:
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 3;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 4;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 2) << 7;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 2) << 8;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 2) << 9;
+ break;
+ case 3:
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex) << 2;
+ CONTEXT |= GBREG->getPixel(w - 4, m_loopIndex) << 3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex - 1) << 9;
+ break;
+ }
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex ++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Start_decode_MMR(CJBig2_Image** pImage, CJBig2_BitStream *pStream, IFX_Pause* pPause)
+{
+ int bitpos, i;
+ JBIG2_ALLOC((* pImage), CJBig2_Image(GBW, GBH));
+ if ((* pImage)->m_pData == NULL) {
+ delete (* pImage);
+ (* pImage) = NULL;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return m_ProssiveStatus;
+ }
+ bitpos = (int)pStream->getBitPos();
+ _FaxG4Decode(m_pModule, pStream->getBuf(), pStream->getLength(), &bitpos, (* pImage)->m_pData, GBW, GBH, (* pImage)->m_nStride);
+ pStream->setBitPos(bitpos);
+ for(i = 0; (FX_DWORD)i < (* pImage)->m_nStride * GBH; i++) {
+ (* pImage)->m_pData[i] = ~(* pImage)->m_pData[i];
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return m_ProssiveStatus;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_MMR()
+{
+ return m_ProssiveStatus;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Continue_decode(IFX_Pause* pPause)
+{
+ if(m_ProssiveStatus != FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ return m_ProssiveStatus;
+ }
+ switch (m_DecodeType) {
+ case 1:
+ return decode_Arith(pPause);
+ case 2:
+ return decode_Arith_V2(pPause);
+ case 3:
+ return decode_Arith_V1(pPause);
+ case 4:
+ return decode_MMR();
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return m_ProssiveStatus;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template0_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ if(m_pLine == NULL) {
+ m_pLine = pImage->m_pData;
+ }
+ nStride = pImage->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ FX_DWORD height = GBH & 0x7fffffff;
+ for(; m_loopIndex < height; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ if(m_loopIndex > 1) {
+ pLine1 = m_pLine - nStride2;
+ pLine2 = m_pLine - nStride;
+ line1 = (*pLine1++) << 6;
+ line2 = *pLine2++;
+ CONTEXT = ((line1 & 0xf800) | (line2 & 0x07f0));
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 6);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> k) & 0x0800)
+ | ((line2 >> k) & 0x0010));
+ }
+ m_pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0800)
+ | ((line2 >> (7 - k)) & 0x0010));
+ }
+ m_pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = m_pLine - nStride;
+ line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 & 0x07f0);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(m_loopIndex & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line2 >> k) & 0x0010));
+ }
+ m_pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line2 >> (7 - k)) & 0x0010));
+ }
+ m_pLine[nLineBytes] = cVal;
+ }
+ }
+ m_pLine += nStride;
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template0_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2, line3;
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ line1 = pImage->getPixel(1, m_loopIndex - 2);
+ line1 |= pImage->getPixel(0, m_loopIndex - 2) << 1;
+ line2 = pImage->getPixel(2, m_loopIndex - 1);
+ line2 |= pImage->getPixel(1, m_loopIndex - 1) << 1;
+ line2 |= pImage->getPixel(0, m_loopIndex - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= line2 << 5;
+ CONTEXT |= pImage->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
+ CONTEXT |= pImage->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
+ CONTEXT |= line1 << 12;
+ CONTEXT |= pImage->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ pImage->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
+ line2 = ((line2 << 1) | pImage->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template1_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ if (!m_pLine) {
+ m_pLine = pImage->m_pData;
+ }
+ nStride = pImage->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ if(m_loopIndex > 1) {
+ pLine1 = m_pLine - nStride2;
+ pLine2 = m_pLine - nStride;
+ line1 = (*pLine1++) << 4;
+ line2 = *pLine2++;
+ CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 4);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> k) & 0x0200)
+ | ((line2 >> (k + 1)) & 0x0008);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0200)
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ m_pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = m_pLine - nStride;
+ line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 >> 1) & 0x01f8;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(m_loopIndex & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line2 >> (k + 1)) & 0x0008);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ m_pLine[nLineBytes] = cVal;
+ }
+ }
+ m_pLine += nStride;
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template1_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2, line3;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(h, h - 1);
+ } else {
+ line1 = pImage->getPixel(2, h - 2);
+ line1 |= pImage->getPixel(1, h - 2) << 1;
+ line1 |= pImage->getPixel(0, h - 2) << 2;
+ line2 = pImage->getPixel(2, h - 1);
+ line2 |= pImage->getPixel(1, h - 1) << 1;
+ line2 |= pImage->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= pImage->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 9;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ pImage->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | pImage->getPixel(w + 3, h - 2)) & 0x0f;
+ line2 = ((line2 << 1) | pImage->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template2_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ if(!m_pLine) {
+ m_pLine = pImage->m_pData;
+ }
+ nStride = pImage->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ if(m_loopIndex > 1) {
+ pLine1 = m_pLine - nStride2;
+ pLine2 = m_pLine - nStride;
+ line1 = (*pLine1++) << 1;
+ line2 = *pLine2++;
+ CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 1);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> k) & 0x0080)
+ | ((line2 >> (k + 3)) & 0x0004);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0080)
+ | ((line2 >> (10 - k)) & 0x0004);
+ }
+ m_pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = m_pLine - nStride;
+ line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 >> 3) & 0x007c;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(m_loopIndex & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line2 >> (k + 3)) & 0x0004);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | (((line2 >> (10 - k))) & 0x0004);
+ }
+ m_pLine[nLineBytes] = cVal;
+ }
+ }
+ m_pLine += nStride;
+ if(pPause && m_loopIndex % 50 == 0 && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template2_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2, line3;
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ line1 = pImage->getPixel(1, m_loopIndex - 2);
+ line1 |= pImage->getPixel(0, m_loopIndex - 2) << 1;
+ line2 = pImage->getPixel(1, m_loopIndex - 1);
+ line2 |= pImage->getPixel(0, m_loopIndex - 1) << 1;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ pImage->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
+ line2 = ((line2 << 1) | pImage->getPixel(w + 2, m_loopIndex - 1)) & 0x0f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template3_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1;
+ FX_BYTE *pLine1, cVal;
+ FX_INT32 nStride, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ if (!m_pLine) {
+ m_pLine = pImage->m_pData;
+ }
+ nStride = pImage->m_nStride;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ if(m_loopIndex > 0) {
+ pLine1 = m_pLine - nStride;
+ line1 = *pLine1++;
+ CONTEXT = (line1 >> 1) & 0x03f0;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | (*pLine1++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (k + 1)) & 0x0010);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (8 - k)) & 0x0010);
+ }
+ m_pLine[nLineBytes] = cVal;
+ } else {
+ CONTEXT = 0;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
+ }
+ m_pLine[cc] = cVal;
+ }
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
+ }
+ m_pLine[nLineBytes] = cVal;
+ }
+ }
+ m_pLine += nStride;
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template3_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2;
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ line1 = pImage->getPixel(1, m_loopIndex - 1);
+ line1 |= pImage->getPixel(0, m_loopIndex - 1) << 1;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= line1 << 5;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ pImage->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 1)) & 0x1f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h
new file mode 100644
index 0000000000..a89d0d62ef
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h
@@ -0,0 +1,278 @@
+// 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
+
+#ifndef _JBIG2_GENERAL_DECODER_H_
+#define _JBIG2_GENERAL_DECODER_H_
+#include "../../../include/fxcodec/fx_codec_def.h"
+#include "../../../include/fxcrt/fx_basic.h"
+#include "JBig2_Define.h"
+#include "JBig2_SymbolDict.h"
+#include "JBig2_ArithDecoder.h"
+#include "JBig2_ArithIntDecoder.h"
+#include "../../../include/fxcrt/fx_coordinates.h"
+class CJBig2_HuffmanTable;
+class CJBig2_Image;
+class CJBig2_PatternDict;
+typedef enum {
+ JBIG2_CORNER_BOTTOMLEFT = 0,
+ JBIG2_CORNER_TOPLEFT = 1,
+ JBIG2_CORNER_BOTTOMRIGHT = 2,
+ JBIG2_CORNER_TOPRIGHT = 3
+} JBig2Corner;
+class CJBig2_GRDProc : public CJBig2_Object
+{
+public:
+ CJBig2_GRDProc()
+ {
+ m_loopIndex = 0;
+ m_pLine = NULL;
+ m_pPause = NULL;
+ m_DecodeType = 0;
+ LTP = 0;
+ m_ReplaceRect.left = 0;
+ m_ReplaceRect.bottom = 0;
+ m_ReplaceRect.top = 0;
+ m_ReplaceRect.right = 0;
+ }
+
+ CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_V2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_MMR(CJBig2_BitStream *pStream);
+ FXCODEC_STATUS Start_decode_Arith(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
+ FXCODEC_STATUS Start_decode_Arith_V2(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
+ FXCODEC_STATUS Start_decode_Arith_V1(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
+ FXCODEC_STATUS Start_decode_MMR(CJBig2_Image** pImage, CJBig2_BitStream *pStream, IFX_Pause* pPause = NULL);
+ FXCODEC_STATUS Continue_decode(IFX_Pause* pPause);
+ FX_RECT GetReplaceRect()
+ {
+ return m_ReplaceRect;
+ };
+private:
+ FXCODEC_STATUS decode_Arith(IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_V2(IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_V1(IFX_Pause* pPause);
+ FXCODEC_STATUS decode_MMR();
+ FXCODEC_STATUS decode_Arith_Template0_opt3(CJBig2_Image*pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template0_unopt(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template1_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template1_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template2_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template2_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template3_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template3_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FX_DWORD m_loopIndex;
+ FX_BYTE * m_pLine;
+ IFX_Pause* m_pPause;
+ FXCODEC_STATUS m_ProssiveStatus;
+ CJBig2_Image** m_pImage;
+ CJBig2_ArithDecoder *m_pArithDecoder;
+ JBig2ArithCtx *m_gbContext;
+ FX_WORD m_DecodeType;
+ FX_BOOL LTP;
+ FX_RECT m_ReplaceRect;
+private:
+
+ CJBig2_Image *decode_Arith_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template0_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template0_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template1_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template1_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template2_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template2_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template2_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template2_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template3_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template3_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template3_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template3_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+public:
+ FX_BOOL MMR;
+ FX_DWORD GBW;
+ FX_DWORD GBH;
+ FX_BYTE GBTEMPLATE;
+ FX_BOOL TPGDON;
+ FX_BOOL USESKIP;
+ CJBig2_Image * SKIP;
+ signed char GBAT[8];
+};
+class CJBig2_GRRDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_Image *decode(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+public:
+ FX_DWORD GRW;
+ FX_DWORD GRH;
+ FX_BOOL GRTEMPLATE;
+ CJBig2_Image *GRREFERENCE;
+ FX_INT32 GRREFERENCEDX;
+ FX_INT32 GRREFERENCEDY;
+ FX_BOOL TPGRON;
+ signed char GRAT[4];
+};
+typedef struct {
+ CJBig2_ArithIntDecoder *IADT,
+ *IAFS,
+ *IADS,
+ *IAIT,
+ *IARI,
+ *IARDW,
+ *IARDH,
+ *IARDX,
+ *IARDY;
+ CJBig2_ArithIaidDecoder *IAID;
+} JBig2IntDecoderState;
+class CJBig2_TRDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_Image *decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext,
+ JBig2IntDecoderState *pIDS = NULL);
+public:
+ FX_BOOL SBHUFF;
+ FX_BOOL SBREFINE;
+ FX_DWORD SBW;
+ FX_DWORD SBH;
+ FX_DWORD SBNUMINSTANCES;
+ FX_DWORD SBSTRIPS;
+ FX_DWORD SBNUMSYMS;
+
+ JBig2HuffmanCode *SBSYMCODES;
+ FX_BYTE SBSYMCODELEN;
+
+ CJBig2_Image **SBSYMS;
+ FX_BOOL SBDEFPIXEL;
+
+ JBig2ComposeOp SBCOMBOP;
+ FX_BOOL TRANSPOSED;
+
+ JBig2Corner REFCORNER;
+ signed char SBDSOFFSET;
+ CJBig2_HuffmanTable *SBHUFFFS,
+ *SBHUFFDS,
+ *SBHUFFDT,
+ *SBHUFFRDW,
+ *SBHUFFRDH,
+ *SBHUFFRDX,
+ *SBHUFFRDY,
+ *SBHUFFRSIZE;
+ FX_BOOL SBRTEMPLATE;
+ signed char SBRAT[4];
+};
+class CJBig2_SDDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_SymbolDict *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext);
+
+ CJBig2_SymbolDict *decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext, IFX_Pause* pPause);
+public:
+ FX_BOOL SDHUFF;
+ FX_BOOL SDREFAGG;
+ FX_DWORD SDNUMINSYMS;
+ CJBig2_Image ** SDINSYMS;
+ FX_DWORD SDNUMNEWSYMS;
+ FX_DWORD SDNUMEXSYMS;
+ CJBig2_HuffmanTable *SDHUFFDH,
+ *SDHUFFDW,
+ *SDHUFFBMSIZE,
+ *SDHUFFAGGINST;
+ FX_BYTE SDTEMPLATE;
+ signed char SDAT[8];
+ FX_BOOL SDRTEMPLATE;
+ signed char SDRAT[4];
+};
+class CJBig2_HTRDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+
+ CJBig2_Image *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
+public:
+ FX_DWORD HBW,
+ HBH;
+ FX_BOOL HMMR;
+ FX_BYTE HTEMPLATE;
+ FX_DWORD HNUMPATS;
+ CJBig2_Image **HPATS;
+ FX_BOOL HDEFPIXEL;
+ JBig2ComposeOp HCOMBOP;
+ FX_BOOL HENABLESKIP;
+ FX_DWORD HGW,
+ HGH;
+ FX_INT32 HGX,
+ HGY;
+ FX_WORD HRX,
+ HRY;
+ FX_BYTE HPW,
+ HPH;
+};
+class CJBig2_PDDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_PatternDict *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+
+ CJBig2_PatternDict *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
+public:
+ FX_BOOL HDMMR;
+ FX_BYTE HDPW,
+ HDPH;
+ FX_DWORD GRAYMAX;
+ FX_BYTE HDTEMPLATE;
+};
+class CJBig2_GSIDProc : public CJBig2_Object
+{
+public:
+
+ FX_DWORD *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+
+ FX_DWORD *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
+public:
+ FX_BOOL GSMMR;
+ FX_BOOL GSUSESKIP;
+ FX_BYTE GSBPP;
+ FX_DWORD GSW,
+ GSH;
+ FX_BYTE GSTEMPLATE;
+ CJBig2_Image *GSKIP;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
new file mode 100644
index 0000000000..8199fe7c21
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
@@ -0,0 +1,55 @@
+// 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 "JBig2_HuffmanDecoder.h"
+CJBig2_HuffmanDecoder::CJBig2_HuffmanDecoder(CJBig2_BitStream *pStream)
+{
+ m_pStream = pStream;
+}
+CJBig2_HuffmanDecoder::~CJBig2_HuffmanDecoder()
+{
+}
+int CJBig2_HuffmanDecoder::decodeAValue(CJBig2_HuffmanTable *pTable, int *nResult)
+{
+ int nVal, nTmp, i, nBits;
+ nVal = 0;
+ nBits = 0;
+ while(1) {
+ if(m_pStream->read1Bit(&nTmp) == -1) {
+ return -1;
+ }
+ nVal = (nVal << 1) | nTmp;
+ nBits ++;
+ for(i = 0; i < pTable->NTEMP; i++) {
+ if((pTable->PREFLEN[i] == nBits) && (pTable->CODES[i] == nVal)) {
+ if((pTable->HTOOB == 1) && (i == pTable->NTEMP - 1)) {
+ return JBIG2_OOB;
+ }
+ if(m_pStream->readNBits(pTable->RANGELEN[i], &nTmp) == -1) {
+ return -1;
+ }
+ if(pTable->HTOOB) {
+ if(i == pTable->NTEMP - 3) {
+ *nResult = pTable->RANGELOW[i] - nTmp;
+ return 0;
+ } else {
+ *nResult = pTable->RANGELOW[i] + nTmp;
+ return 0;
+ }
+ } else {
+ if(i == pTable->NTEMP - 2) {
+ *nResult = pTable->RANGELOW[i] - nTmp;
+ return 0;
+ } else {
+ *nResult = pTable->RANGELOW[i] + nTmp;
+ return 0;
+ }
+ }
+ }
+ }
+ }
+ return -2;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h
new file mode 100644
index 0000000000..38b2021d20
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h
@@ -0,0 +1,24 @@
+// 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
+
+#ifndef _JBIG2_HUFFMAN_DECODER_H_
+#define _JBIG2_HUFFMAN_DECODER_H_
+#include "JBig2_BitStream.h"
+#include "JBig2_HuffmanTable.h"
+class CJBig2_HuffmanDecoder : public CJBig2_Object
+{
+public:
+
+ CJBig2_HuffmanDecoder(CJBig2_BitStream *pStream);
+
+ ~CJBig2_HuffmanDecoder();
+
+ int decodeAValue(CJBig2_HuffmanTable *pTable, int *nResult);
+private:
+
+ CJBig2_BitStream *m_pStream;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
new file mode 100644
index 0000000000..af4a54988d
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
@@ -0,0 +1,193 @@
+// 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 "JBig2_HuffmanTable.h"
+#include "JBig2_BitStream.h"
+#include <string.h>
+
+CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine *pTable, int nLines,
+ FX_BOOL bHTOOB)
+{
+ init();
+ m_bOK = parseFromStandardTable(pTable, nLines, bHTOOB);
+}
+
+CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream *pStream)
+{
+ init();
+ m_bOK = parseFromCodedBuffer(pStream);
+}
+
+CJBig2_HuffmanTable::~CJBig2_HuffmanTable()
+{
+ if(CODES) {
+ m_pModule->JBig2_Free(CODES);
+ }
+ if(PREFLEN) {
+ m_pModule->JBig2_Free(PREFLEN);
+ }
+ if(RANGELEN) {
+ m_pModule->JBig2_Free(RANGELEN);
+ }
+ if(RANGELOW) {
+ m_pModule->JBig2_Free(RANGELOW);
+ }
+}
+void CJBig2_HuffmanTable::init()
+{
+ HTOOB = FALSE;
+ NTEMP = 0;
+ CODES = NULL;
+ PREFLEN = NULL;
+ RANGELEN = NULL;
+ RANGELOW = NULL;
+}
+int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine *pTable, int nLines, FX_BOOL bHTOOB)
+{
+ int CURLEN, LENMAX, CURCODE, CURTEMP, i;
+ int *LENCOUNT;
+ int *FIRSTCODE;
+ HTOOB = bHTOOB;
+ NTEMP = nLines;
+ CODES = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ PREFLEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ RANGELEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ RANGELOW = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ LENMAX = 0;
+ for(i = 0; i < NTEMP; i++) {
+ PREFLEN[i] = pTable[i].PREFLEN;
+ RANGELEN[i] = pTable[i].RANDELEN;
+ RANGELOW[i] = pTable[i].RANGELOW;
+ if(PREFLEN[i] > LENMAX) {
+ LENMAX = PREFLEN[i];
+ }
+ }
+ LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
+ FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ for(i = 0; i < NTEMP; i++) {
+ LENCOUNT[PREFLEN[i]] ++;
+ }
+ CURLEN = 1;
+ FIRSTCODE[0] = 0;
+ LENCOUNT[0] = 0;
+ while(CURLEN <= LENMAX) {
+ FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
+ CURCODE = FIRSTCODE[CURLEN];
+ CURTEMP = 0;
+ while(CURTEMP < NTEMP) {
+ if(PREFLEN[CURTEMP] == CURLEN) {
+ CODES[CURTEMP] = CURCODE;
+ CURCODE = CURCODE + 1;
+ }
+ CURTEMP = CURTEMP + 1;
+ }
+ CURLEN = CURLEN + 1;
+ }
+ m_pModule->JBig2_Free(LENCOUNT);
+ m_pModule->JBig2_Free(FIRSTCODE);
+ return 1;
+}
+
+#define HT_CHECK_MEMORY_ADJUST \
+ if(NTEMP >= nSize) \
+ { \
+ nSize += 16; \
+ PREFLEN = (int*)m_pModule->JBig2_Realloc(PREFLEN,sizeof(int)*nSize); \
+ RANGELEN = (int*)m_pModule->JBig2_Realloc(RANGELEN,sizeof(int)*nSize); \
+ RANGELOW = (int*)m_pModule->JBig2_Realloc(RANGELOW,sizeof(int)*nSize); \
+ }
+int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream *pStream)
+{
+ unsigned char HTPS, HTRS;
+ int HTLOW, HTHIGH;
+ int CURRANGELOW;
+ int nSize = 16;
+ int CURLEN, LENMAX, CURCODE, CURTEMP, i;
+ int *LENCOUNT;
+ int *FIRSTCODE;
+ unsigned char cTemp;
+ if(pStream->read1Byte(&cTemp) == -1) {
+ goto failed;
+ }
+ HTOOB = cTemp & 0x01;
+ HTPS = ((cTemp >> 1) & 0x07) + 1;
+ HTRS = ((cTemp >> 4) & 0x07) + 1;
+ if(pStream->readInteger((FX_DWORD*)&HTLOW) == -1 ||
+ pStream->readInteger((FX_DWORD*)&HTHIGH) == -1) {
+ goto failed;
+ }
+ PREFLEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize);
+ RANGELEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize);
+ RANGELOW = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize);
+ CURRANGELOW = HTLOW;
+ NTEMP = 0;
+ do {
+ HT_CHECK_MEMORY_ADJUST
+ if((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
+ || (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) {
+ goto failed;
+ }
+ RANGELOW[NTEMP] = CURRANGELOW;
+ CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]);
+ NTEMP = NTEMP + 1;
+ } while(CURRANGELOW < HTHIGH);
+ HT_CHECK_MEMORY_ADJUST
+ if(pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
+ goto failed;
+ }
+ RANGELEN[NTEMP] = 32;
+ RANGELOW[NTEMP] = HTLOW - 1;
+ NTEMP = NTEMP + 1;
+ HT_CHECK_MEMORY_ADJUST
+ if(pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
+ goto failed;
+ }
+ RANGELEN[NTEMP] = 32;
+ RANGELOW[NTEMP] = HTHIGH;
+ NTEMP = NTEMP + 1;
+ if(HTOOB) {
+ HT_CHECK_MEMORY_ADJUST
+ if(pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
+ goto failed;
+ }
+ NTEMP = NTEMP + 1;
+ }
+ CODES = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ LENMAX = 0;
+ for(i = 0; i < NTEMP; i++) {
+ if(PREFLEN[i] > LENMAX) {
+ LENMAX = PREFLEN[i];
+ }
+ }
+ LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
+ FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ for(i = 0; i < NTEMP; i++) {
+ LENCOUNT[PREFLEN[i]] ++;
+ }
+ CURLEN = 1;
+ FIRSTCODE[0] = 0;
+ LENCOUNT[0] = 0;
+ while(CURLEN <= LENMAX) {
+ FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
+ CURCODE = FIRSTCODE[CURLEN];
+ CURTEMP = 0;
+ while(CURTEMP < NTEMP) {
+ if(PREFLEN[CURTEMP] == CURLEN) {
+ CODES[CURTEMP] = CURCODE;
+ CURCODE = CURCODE + 1;
+ }
+ CURTEMP = CURTEMP + 1;
+ }
+ CURLEN = CURLEN + 1;
+ }
+ m_pModule->JBig2_Free(LENCOUNT);
+ m_pModule->JBig2_Free(FIRSTCODE);
+ return TRUE;
+failed:
+ return FALSE;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h
new file mode 100644
index 0000000000..df051c28d4
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h
@@ -0,0 +1,42 @@
+// 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
+
+#ifndef _JBIG2_HUFFMAN_TABLE_H_
+#define _JBIG2_HUFFMAN_TABLE_H_
+#include "JBig2_Module.h"
+#include "JBig2_HuffmanTable_Standard.h"
+#include "JBig2_BitStream.h"
+class CJBig2_HuffmanTable : public CJBig2_Object
+{
+public:
+
+ CJBig2_HuffmanTable(const JBig2TableLine *pTable, int nLines, FX_BOOL bHTOOB);
+
+ CJBig2_HuffmanTable(CJBig2_BitStream *pStream);
+
+ ~CJBig2_HuffmanTable();
+
+ void init();
+
+ int parseFromStandardTable(const JBig2TableLine *pTable, int nLines, FX_BOOL bHTOOB);
+
+ int parseFromCodedBuffer(CJBig2_BitStream *pStream);
+
+ FX_BOOL isOK()
+ {
+ return m_bOK;
+ }
+private:
+ FX_BOOL HTOOB;
+ int NTEMP;
+ int *CODES;
+ int *PREFLEN;
+ int *RANGELEN;
+ int *RANGELOW;
+ FX_BOOL m_bOK;
+ friend class CJBig2_HuffmanDecoder;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h b/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h
new file mode 100644
index 0000000000..f6114364f1
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h
@@ -0,0 +1,251 @@
+// 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
+
+#ifndef _JBIG2_HUFFMAN_TABLE_STANDARD_H_
+#define _JBIG2_HUFFMAN_TABLE_STANDARD_H_
+typedef struct {
+ int PREFLEN;
+ int RANDELEN;
+ int RANGELOW;
+} JBig2TableLine;
+const FX_BOOL HuffmanTable_HTOOB_B1 = FALSE;
+const JBig2TableLine HuffmanTable_B1[] = {
+ { 1, 4, 0 },
+ { 2, 8, 16 },
+ { 3, 16, 272 },
+ { 0, 32, -1 },
+ { 3, 32, 65808 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B2 = TRUE;
+const JBig2TableLine HuffmanTable_B2[] = {
+ { 1, 0, 0 },
+ { 2, 0, 1 },
+ { 3, 0, 2 },
+ { 4, 3, 3 },
+ { 5, 6, 11 },
+ { 0, 32, -1 },
+ { 6, 32, 75 },
+ { 6, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B3 = TRUE;
+const JBig2TableLine HuffmanTable_B3[] = {
+ { 8, 8, -256 },
+ { 1, 0, 0 },
+ { 2, 0, 1 },
+ { 3, 0, 2 },
+ { 4, 3, 3 },
+ { 5, 6, 11 },
+ { 8, 32, -257 },
+ { 7, 32, 75 },
+ { 6, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B4 = FALSE;
+const JBig2TableLine HuffmanTable_B4[] = {
+ { 1, 0, 1 },
+ { 2, 0, 2 },
+ { 3, 0, 3 },
+ { 4, 3, 4 },
+ { 5, 6, 12 },
+ { 0, 32, -1 },
+ { 5, 32, 76 },
+};
+const FX_BOOL HuffmanTable_HTOOB_B5 = FALSE;
+const JBig2TableLine HuffmanTable_B5[] = {
+ { 7, 8, -255 },
+ { 1, 0, 1 },
+ { 2, 0, 2 },
+ { 3, 0, 3 },
+ { 4, 3, 4 },
+ { 5, 6, 12 },
+ { 7, 32, -256 },
+ { 6, 32, 76 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B6 = FALSE;
+const JBig2TableLine HuffmanTable_B6[] = {
+ { 5, 10, -2048 },
+ { 4, 9, -1024 },
+ { 4, 8, -512 },
+ { 4, 7, -256 },
+ { 5, 6, -128 },
+ { 5, 5, -64 },
+ { 4, 5, -32 },
+ { 2, 7, 0 },
+ { 3, 7, 128 },
+ { 3, 8, 256 },
+ { 4, 9, 512 },
+ { 4, 10, 1024 },
+ { 6, 32, -2049 },
+ { 6, 32, 2048 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B7 = FALSE;
+const JBig2TableLine HuffmanTable_B7[] = {
+ { 4, 9, -1024 },
+ { 3, 8, -512 },
+ { 4, 7, -256 },
+ { 5, 6, -128 },
+ { 5, 5, -64 },
+ { 4, 5, -32 },
+ { 4, 5, 0 },
+ { 5, 5, 32 },
+ { 5, 6, 64 },
+ { 4, 7, 128 },
+ { 3, 8, 256 },
+ { 3, 9, 512 },
+ { 3, 10, 1024 },
+ { 5, 32, -1025 },
+ { 5, 32, 2048 },
+};
+const FX_BOOL HuffmanTable_HTOOB_B8 = TRUE;
+const JBig2TableLine HuffmanTable_B8[] = {
+ { 8, 3, -15 },
+ { 9, 1, -7 },
+ { 8, 1, -5 },
+ { 9, 0, -3 },
+ { 7, 0, -2 },
+ { 4, 0, -1 },
+ { 2, 1, 0 },
+ { 5, 0, 2 },
+ { 6, 0, 3 },
+ { 3, 4, 4 },
+ { 6, 1, 20 },
+ { 4, 4, 22 },
+ { 4, 5, 38 },
+ { 5, 6, 70 },
+ { 5, 7, 134 },
+ { 6, 7, 262 },
+ { 7, 8, 390 },
+ { 6, 10, 646 },
+ { 9, 32, -16 },
+ { 9, 32, 1670 },
+ { 2, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B9 = TRUE;
+const JBig2TableLine HuffmanTable_B9[] = {
+ { 8, 4, -31 },
+ { 9, 2, -15 },
+ { 8, 2, -11 },
+ { 9, 1, -7 },
+ { 7, 1, -5 },
+ { 4, 1, -3 },
+ { 3, 1, -1 },
+ { 3, 1, 1 },
+ { 5, 1, 3 },
+ { 6, 1, 5 },
+ { 3, 5, 7 },
+ { 6, 2, 39 },
+ { 4, 5, 43 },
+ { 4, 6, 75 },
+ { 5, 7, 139 },
+ { 5, 8, 267 },
+ { 6, 8, 523 },
+ { 7, 9, 779 },
+ { 6, 11, 1291 },
+ { 9, 32, -32 },
+ { 9, 32, 3339 },
+ { 2, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B10 = TRUE;
+const JBig2TableLine HuffmanTable_B10[] = {
+ { 7, 4, -21 },
+ { 8, 0, -5 },
+ { 7, 0, -4 },
+ { 5, 0, -3 },
+ { 2, 2, -2 },
+ { 5, 0, 2 },
+ { 6, 0, 3 },
+ { 7, 0, 4 },
+ { 8, 0, 5 },
+ { 2, 6, 6 },
+ { 5, 5, 70 },
+ { 6, 5, 102 },
+ { 6, 6, 134 },
+ { 6, 7, 198 },
+ { 6, 8, 326 },
+ { 6, 9, 582 },
+ { 6, 10, 1094 },
+ { 7, 11, 2118 },
+ { 8, 32, -22 },
+ { 8, 32, 4166 },
+ { 2, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B11 = FALSE;
+const JBig2TableLine HuffmanTable_B11[] = {
+ { 1, 0, 1 },
+ { 2, 1, 2 },
+ { 4, 0, 4 },
+ { 4, 1, 5 },
+ { 5, 1, 7 },
+ { 5, 2, 9 },
+ { 6, 2, 13 },
+ { 7, 2, 17 },
+ { 7, 3, 21 },
+ { 7, 4, 29 },
+ { 7, 5, 45 },
+ { 7, 6, 77 },
+ { 0, 32, 0 },
+ { 7, 32, 141 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B12 = FALSE;
+const JBig2TableLine HuffmanTable_B12[] = {
+ { 1, 0, 1 },
+ { 2, 0, 2 },
+ { 3, 1, 3 },
+ { 5, 0, 5 },
+ { 5, 1, 6 },
+ { 6, 1, 8 },
+ { 7, 0, 10 },
+ { 7, 1, 11 },
+ { 7, 2, 13 },
+ { 7, 3, 17 },
+ { 7, 4, 25 },
+ { 8, 5, 41 },
+ { 0, 32, 0 },
+ { 8, 32, 73 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B13 = FALSE;
+const JBig2TableLine HuffmanTable_B13[] = {
+ { 1, 0, 1 },
+ { 3, 0, 2 },
+ { 4, 0, 3 },
+ { 5, 0, 4 },
+ { 4, 1, 5 },
+ { 3, 3, 7 },
+ { 6, 1, 15 },
+ { 6, 2, 17 },
+ { 6, 3, 21 },
+ { 6, 4, 29 },
+ { 6, 5, 45 },
+ { 7, 6, 77 },
+ { 0, 32, 0 },
+ { 7, 32, 141 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B14 = FALSE;
+const JBig2TableLine HuffmanTable_B14[] = {
+ { 3, 0, -2 },
+ { 3, 0, -1 },
+ { 1, 0, 0 },
+ { 3, 0, 1 },
+ { 3, 0, 2 },
+ { 0, 32, -3 },
+ { 0, 32, 3 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B15 = FALSE;
+const JBig2TableLine HuffmanTable_B15[] = {
+ { 7, 4, -24 },
+ { 6, 2, -8 },
+ { 5, 1, -4 },
+ { 4, 0, -2 },
+ { 3, 0, -1 },
+ { 1, 0, 0 },
+ { 3, 0, 1 },
+ { 4, 0, 2 },
+ { 5, 1, 3 },
+ { 6, 2, 5 },
+ { 7, 4, 9 },
+ { 7, 32, -25 },
+ { 7, 32, 25 }
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Image.cpp b/core/src/fxcodec/jbig2/JBig2_Image.cpp
new file mode 100644
index 0000000000..1719028176
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Image.cpp
@@ -0,0 +1,1619 @@
+// 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 "JBig2_Image.h"
+#include "../../../include/fxcrt/fx_basic.h"
+#include "../../../include/fxcrt/fx_coordinates.h"
+#include <limits.h>
+CJBig2_Image::CJBig2_Image(FX_INT32 w, FX_INT32 h)
+{
+ m_nWidth = w;
+ m_nHeight = h;
+ if (m_nWidth <= 0 || m_nHeight <= 0 || m_nWidth > INT_MAX - 31) {
+ m_pData = NULL;
+ m_bNeedFree = FALSE;
+ return;
+ }
+ m_nStride = ((w + 31) >> 5) << 2;
+ if (m_nStride * m_nHeight > 0 && 104857600 / (int)m_nStride > m_nHeight) {
+ m_pData = (FX_BYTE *)m_pModule->JBig2_Malloc2(m_nStride, m_nHeight);
+ } else {
+ m_pData = NULL;
+ }
+ m_bNeedFree = TRUE;
+}
+CJBig2_Image::CJBig2_Image(FX_INT32 w, FX_INT32 h, FX_INT32 stride, FX_BYTE*pBuf)
+{
+ m_nWidth = w;
+ m_nHeight = h;
+ m_nStride = stride;
+ m_pData = pBuf;
+ m_bNeedFree = FALSE;
+}
+CJBig2_Image::CJBig2_Image(CJBig2_Image &im)
+{
+ m_pModule = im.m_pModule;
+ m_nWidth = im.m_nWidth;
+ m_nHeight = im.m_nHeight;
+ m_nStride = im.m_nStride;
+ if (im.m_pData) {
+ m_pData = (FX_BYTE*)m_pModule->JBig2_Malloc2(m_nStride, m_nHeight);
+ JBIG2_memcpy(m_pData, im.m_pData, m_nStride * m_nHeight);
+ } else {
+ m_pData = NULL;
+ }
+ m_bNeedFree = TRUE;
+}
+CJBig2_Image::~CJBig2_Image()
+{
+ if(m_bNeedFree && m_pData) {
+ m_pModule->JBig2_Free(m_pData);
+ }
+}
+FX_BOOL CJBig2_Image::getPixel(FX_INT32 x, FX_INT32 y)
+{
+ if (!m_pData) {
+ return 0;
+ }
+ FX_INT32 m, n;
+ if(x < 0 || x >= m_nWidth) {
+ return 0;
+ }
+ if(y < 0 || y >= m_nHeight) {
+ return 0;
+ }
+ m = y * m_nStride + (x >> 3);
+ n = x & 7;
+ return ((m_pData[m] >> (7 - n)) & 1);
+}
+
+FX_INT32 CJBig2_Image::setPixel(FX_INT32 x, FX_INT32 y, FX_BOOL v)
+{
+ if (!m_pData) {
+ return 0;
+ }
+ FX_INT32 m, n;
+ if(x < 0 || x >= m_nWidth) {
+ return 0;
+ }
+ if(y < 0 || y >= m_nHeight) {
+ return 0;
+ }
+ m = y * m_nStride + (x >> 3);
+ n = x & 7;
+ if(v) {
+ m_pData[m] |= 1 << (7 - n);
+ } else {
+ m_pData[m] &= ~(1 << (7 - n));
+ }
+ return 1;
+}
+void CJBig2_Image::copyLine(FX_INT32 hTo, FX_INT32 hFrom)
+{
+ if (!m_pData) {
+ return;
+ }
+ if(hFrom < 0 || hFrom >= m_nHeight) {
+ JBIG2_memset(m_pData + hTo * m_nStride, 0, m_nStride);
+ } else {
+ JBIG2_memcpy(m_pData + hTo * m_nStride, m_pData + hFrom * m_nStride, m_nStride);
+ }
+}
+void CJBig2_Image::fill(FX_BOOL v)
+{
+ if (!m_pData) {
+ return;
+ }
+ JBIG2_memset(m_pData, v ? 0xff : 0, m_nStride * m_nHeight);
+}
+FX_BOOL CJBig2_Image::composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
+{
+ if (!m_pData) {
+ return FALSE;
+ }
+ return composeTo_opt2(pDst, x, y, op);
+}
+FX_BOOL CJBig2_Image::composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect)
+{
+ if (!m_pData) {
+ return FALSE;
+ }
+ if (NULL == pSrcRect || *pSrcRect == FX_RECT(0, 0, m_nWidth, m_nHeight)) {
+ return composeTo_opt2(pDst, x, y, op);
+ }
+ return composeTo_opt2(pDst, x, y, op, pSrcRect);
+}
+FX_BOOL CJBig2_Image::composeTo_unopt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
+{
+ FX_INT32 w, h, dx, dy;
+ FX_INT32 i, j;
+ w = m_nWidth;
+ h = m_nHeight;
+ dx = dy = 0;
+ if(x < 0) {
+ dx += -x;
+ w -= -x;
+ x = 0;
+ }
+ if(y < 0) {
+ dy += -y;
+ h -= -y;
+ y = 0;
+ }
+ if(x + w > pDst->m_nWidth) {
+ w = pDst->m_nWidth - x;
+ }
+ if(y + h > pDst->m_nHeight) {
+ h = pDst->m_nHeight - y;
+ }
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j,
+ (getPixel(i + dx, j + dy) | pDst->getPixel(x + i, y + j)) & 1);
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j,
+ (getPixel(i + dx, j + dy) & pDst->getPixel(x + i, y + j)) & 1);
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j,
+ (getPixel(i + dx, j + dy) ^ pDst->getPixel(x + i, y + j)) & 1);
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j,
+ (~(getPixel(i + dx, j + dy) ^ pDst->getPixel(x + i, y + j))) & 1);
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j, getPixel(i + dx, j + dy));
+ }
+ }
+ break;
+ }
+ return TRUE;
+}
+
+FX_BOOL CJBig2_Image::composeTo_opt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
+{
+ FX_INT32 x0, x1, y0, y1, xx, yy;
+ FX_BYTE *pLineSrc, *pLineDst, *srcPtr, *destPtr;
+ FX_DWORD src0, src1, src, dest, s1, s2, m1, m2, m3;
+ FX_BOOL oneByte;
+ if (!m_pData) {
+ return FALSE;
+ }
+ if (y < 0) {
+ y0 = -y;
+ } else {
+ y0 = 0;
+ }
+ if (y + m_nHeight > pDst->m_nHeight) {
+ y1 = pDst->m_nHeight - y;
+ } else {
+ y1 = m_nHeight;
+ }
+ if (y0 >= y1) {
+ return FALSE;
+ }
+ if (x >= 0) {
+ x0 = x & ~7;
+ } else {
+ x0 = 0;
+ }
+ x1 = x + m_nWidth;
+ if (x1 > pDst->m_nWidth) {
+ x1 = pDst->m_nWidth;
+ }
+ if (x0 >= x1) {
+ return FALSE;
+ }
+ s1 = x & 7;
+ s2 = 8 - s1;
+ m1 = 0xff >> (x1 & 7);
+ m2 = 0xff << (((x1 & 7) == 0) ? 0 : 8 - (x1 & 7));
+ m3 = (0xff >> s1) & m2;
+ oneByte = x0 == ((x1 - 1) & ~7);
+ pLineDst = pDst->m_pData + y * pDst->m_nStride;
+ pLineSrc = m_pData + y0 * m_nStride;
+ if(oneByte) {
+ if(x >= 0) {
+ switch(op) {
+ case JBIG2_COMPOSE_OR: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest |= (*srcPtr >> s1) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest &= ((0xff00 | *srcPtr) >> s1) | m1;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest ^= (*srcPtr >> s1) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest ^= ((*srcPtr ^ 0xff) >> s1) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest = (dest & ~m3) | ((*srcPtr >> s1) & m3);
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ }
+ } else {
+ switch(op) {
+ case JBIG2_COMPOSE_OR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest |= *srcPtr & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest &= *srcPtr | m1;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest ^= *srcPtr & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest ^= (*srcPtr ^ 0xff) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest = (*srcPtr & m2) | (dest & m1);
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ }
+ }
+ } else {
+ if(x >= 0) {
+ switch(op) {
+ case JBIG2_COMPOSE_OR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest |= src1 >> s1;
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest |= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest |= src & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest &= (0xff00 | src1) >> s1;
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest &= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest &= src | m1;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest ^= src1 >> s1;
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest ^= (src1 ^ 0xff) >> s1;
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src ^ 0xff;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= (src ^ 0xff) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest = (dest & (0xff << s2)) | (src1 >> s1);
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest = src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest = (src & m2) | (dest & m1);
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ }
+ } else {
+ switch(op) {
+ case JBIG2_COMPOSE_OR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest |= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest |= src & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest &= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest &= src | m1;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src ^ 0xff;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= (src ^ 0xff) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest = src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest = (src & m2) | (dest & m1);
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ }
+ }
+ }
+ return TRUE;
+}
+FX_BOOL CJBig2_Image::composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op)
+{
+ if (!m_pData) {
+ return FALSE;
+ }
+ return pSrc->composeTo(this, x, y, op);
+}
+FX_BOOL CJBig2_Image::composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op, const FX_RECT* pSrcRect)
+{
+ if (!m_pData) {
+ return FALSE;
+ }
+ return pSrc->composeTo(this, x, y, op, pSrcRect);
+}
+CJBig2_Image *CJBig2_Image::subImage_unopt(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h)
+{
+ CJBig2_Image *pImage;
+ FX_INT32 i, j;
+ JBIG2_ALLOC(pImage, CJBig2_Image(w, h));
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pImage->setPixel(i, j, getPixel(x + i, y + j));
+ }
+ }
+ return pImage;
+}
+#define JBIG2_GETDWORD(buf) ((FX_DWORD)(((buf)[0] << 24) | ((buf)[1] << 16) | ((buf)[2] << 8) | (buf)[3]))
+CJBig2_Image *CJBig2_Image::subImage(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h)
+{
+ CJBig2_Image *pImage;
+ FX_INT32 m, n, j;
+ FX_BYTE *pLineSrc, *pLineDst;
+ FX_DWORD wTmp;
+ FX_BYTE *pSrc, *pSrcEnd, *pDst, *pDstEnd;
+ if (w == 0 || h == 0) {
+ return NULL;
+ }
+ JBIG2_ALLOC(pImage, CJBig2_Image(w, h));
+ if (!m_pData) {
+ pImage->fill(0);
+ return pImage;
+ }
+ if (!pImage->m_pData) {
+ return pImage;
+ }
+ pLineSrc = m_pData + m_nStride * y;
+ pLineDst = pImage->m_pData;
+ m = (x >> 5) << 2;
+ n = x & 31;
+ if(n == 0) {
+ for(j = 0; j < h; j++) {
+ pSrc = pLineSrc + m;
+ pSrcEnd = pLineSrc + m_nStride;
+ pDst = pLineDst;
+ pDstEnd = pLineDst + pImage->m_nStride;
+ for(; pDst < pDstEnd; pSrc += 4, pDst += 4) {
+ *((FX_DWORD *)pDst) = *((FX_DWORD *)pSrc);
+ }
+ pLineSrc += m_nStride;
+ pLineDst += pImage->m_nStride;
+ }
+ } else {
+ for(j = 0; j < h; j++) {
+ pSrc = pLineSrc + m;
+ pSrcEnd = pLineSrc + m_nStride;
+ pDst = pLineDst;
+ pDstEnd = pLineDst + pImage->m_nStride;
+ for(; pDst < pDstEnd; pSrc += 4, pDst += 4) {
+ if(pSrc + 4 < pSrcEnd) {
+ wTmp = (JBIG2_GETDWORD(pSrc) << n) | (JBIG2_GETDWORD(pSrc + 4) >> (32 - n));
+ } else {
+ wTmp = JBIG2_GETDWORD(pSrc) << n;
+ }
+ pDst[0] = (FX_BYTE)(wTmp >> 24);
+ pDst[1] = (FX_BYTE)(wTmp >> 16);
+ pDst[2] = (FX_BYTE)(wTmp >> 8);
+ pDst[3] = (FX_BYTE)wTmp;
+ }
+ pLineSrc += m_nStride;
+ pLineDst += pImage->m_nStride;
+ }
+ }
+ return pImage;
+}
+void CJBig2_Image::expand(FX_INT32 h, FX_BOOL v)
+{
+ if (!m_pData) {
+ return;
+ }
+ m_pData = (FX_BYTE*)m_pModule->JBig2_Realloc(m_pData, h * m_nStride);
+ if(h > m_nHeight) {
+ JBIG2_memset(m_pData + m_nHeight * m_nStride, v ? 0xff : 0, (h - m_nHeight)*m_nStride);
+ }
+ m_nHeight = h;
+}
+FX_BOOL CJBig2_Image::composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
+{
+ FX_INT32 xs0, ys0, xs1, ys1, xd0, yd0, xd1, yd1, xx, yy, w, h, middleDwords, lineLeft;
+ FX_DWORD s1, d1, d2, shift, shift1, shift2, tmp, tmp1, tmp2, maskL, maskR, maskM;
+ FX_BYTE *lineSrc, *lineDst, *sp, *dp;
+ if (!m_pData) {
+ return FALSE;
+ }
+ if (x < -1048576 || x > 1048576 || y < -1048576 || y > 1048576) {
+ return FALSE;
+ }
+ if(y < 0) {
+ ys0 = -y;
+ } else {
+ ys0 = 0;
+ }
+ if(y + m_nHeight > pDst->m_nHeight) {
+ ys1 = pDst->m_nHeight - y;
+ } else {
+ ys1 = m_nHeight;
+ }
+ if(x < 0) {
+ xs0 = -x;
+ } else {
+ xs0 = 0;
+ }
+ if(x + m_nWidth > pDst->m_nWidth) {
+ xs1 = pDst->m_nWidth - x;
+ } else {
+ xs1 = m_nWidth;
+ }
+ if((ys0 >= ys1) || (xs0 >= xs1)) {
+ return 0;
+ }
+ w = xs1 - xs0;
+ h = ys1 - ys0;
+ if(y < 0) {
+ yd0 = 0;
+ } else {
+ yd0 = y;
+ }
+ if(x < 0) {
+ xd0 = 0;
+ } else {
+ xd0 = x;
+ }
+ xd1 = xd0 + w;
+ yd1 = yd0 + h;
+ d1 = xd0 & 31;
+ d2 = xd1 & 31;
+ s1 = xs0 & 31;
+ maskL = 0xffffffff >> d1;
+ maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32);
+ maskM = maskL & maskR;
+ lineSrc = m_pData + ys0 * m_nStride + ((xs0 >> 5) << 2);
+ lineLeft = m_nStride - ((xs0 >> 5) << 2);
+ lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2);
+ if((xd0 & ~31) == ((xd1 - 1) & ~31)) {
+ if((xs0 & ~31) == ((xs1 - 1) & ~31)) {
+ if(s1 > d1) {
+ shift = s1 - d1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = JBIG2_GETDWORD(lineSrc) << shift;
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else {
+ shift = d1 - s1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = JBIG2_GETDWORD(lineSrc) >> shift;
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ } else {
+ shift1 = s1 - d1;
+ shift2 = 32 - shift1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = (JBIG2_GETDWORD(lineSrc) << shift1) | (JBIG2_GETDWORD(lineSrc + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ } else {
+ if(s1 > d1) {
+ shift1 = s1 - d1;
+ shift2 = 32 - shift1;
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (
+ ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else if(s1 == d1) {
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else {
+ shift1 = d1 - s1;
+ shift2 = 32 - shift1;
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp) >> shift1;
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift2) | ((JBIG2_GETDWORD(sp + 4)) >> shift1);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift2) | (
+ ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift1);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ }
+ return 1;
+}
+FX_BOOL CJBig2_Image::composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect)
+{
+ FX_INT32 xs0, ys0, xs1, ys1, xd0, yd0, xd1, yd1, xx, yy, w, h, middleDwords, lineLeft;
+ FX_DWORD s1, d1, d2, shift, shift1, shift2, tmp, tmp1, tmp2, maskL, maskR, maskM;
+ FX_BYTE *lineSrc, *lineDst, *sp, *dp;
+ FX_INT32 sw, sh;
+ if (!m_pData) {
+ return FALSE;
+ }
+ if (x < -1048576 || x > 1048576 || y < -1048576 || y > 1048576) {
+ return FALSE;
+ }
+ sw = pSrcRect->Width();
+ sh = pSrcRect->Height();
+ if(y < 0) {
+ ys0 = -y;
+ } else {
+ ys0 = 0;
+ }
+ if(y + sh > pDst->m_nHeight) {
+ ys1 = pDst->m_nHeight - y;
+ } else {
+ ys1 = sh;
+ }
+ if(x < 0) {
+ xs0 = -x;
+ } else {
+ xs0 = 0;
+ }
+ if(x + sw > pDst->m_nWidth) {
+ xs1 = pDst->m_nWidth - x;
+ } else {
+ xs1 = sw;
+ }
+ if((ys0 >= ys1) || (xs0 >= xs1)) {
+ return 0;
+ }
+ w = xs1 - xs0;
+ h = ys1 - ys0;
+ if(y < 0) {
+ yd0 = 0;
+ } else {
+ yd0 = y;
+ }
+ if(x < 0) {
+ xd0 = 0;
+ } else {
+ xd0 = x;
+ }
+ xd1 = xd0 + w;
+ yd1 = yd0 + h;
+ d1 = xd0 & 31;
+ d2 = xd1 & 31;
+ s1 = xs0 & 31;
+ maskL = 0xffffffff >> d1;
+ maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32);
+ maskM = maskL & maskR;
+ lineSrc = m_pData + (pSrcRect->top + ys0) * m_nStride + (((xs0 + pSrcRect->left) >> 5) << 2);
+ lineLeft = m_nStride - ((xs0 >> 5) << 2);
+ lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2);
+ if((xd0 & ~31) == ((xd1 - 1) & ~31)) {
+ if((xs0 & ~31) == ((xs1 - 1) & ~31)) {
+ if(s1 > d1) {
+ shift = s1 - d1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = JBIG2_GETDWORD(lineSrc) << shift;
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else {
+ shift = d1 - s1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = JBIG2_GETDWORD(lineSrc) >> shift;
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ } else {
+ shift1 = s1 - d1;
+ shift2 = 32 - shift1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = (JBIG2_GETDWORD(lineSrc) << shift1) | (JBIG2_GETDWORD(lineSrc + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ } else {
+ if(s1 > d1) {
+ shift1 = s1 - d1;
+ shift2 = 32 - shift1;
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (
+ ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else if(s1 == d1) {
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else {
+ shift1 = d1 - s1;
+ shift2 = 32 - shift1;
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp) >> shift1;
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift2) | ((JBIG2_GETDWORD(sp + 4)) >> shift1);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift2) | (
+ ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift1);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ }
+ return 1;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Image.h b/core/src/fxcodec/jbig2/JBig2_Image.h
new file mode 100644
index 0000000000..3c69f647e3
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Image.h
@@ -0,0 +1,68 @@
+// 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
+
+#ifndef _JBIG2_IMAGE_H_
+#define _JBIG2_IMAGE_H_
+#include "JBig2_Define.h"
+#include "JBig2_Module.h"
+typedef enum {
+ JBIG2_COMPOSE_OR = 0,
+ JBIG2_COMPOSE_AND = 1,
+ JBIG2_COMPOSE_XOR = 2,
+ JBIG2_COMPOSE_XNOR = 3,
+ JBIG2_COMPOSE_REPLACE = 4
+} JBig2ComposeOp;
+struct FX_RECT;
+class CJBig2_Image : public CJBig2_Object
+{
+public:
+
+ CJBig2_Image(FX_INT32 w, FX_INT32 h);
+
+ CJBig2_Image(FX_INT32 w, FX_INT32 h, FX_INT32 stride, FX_BYTE*pBuf);
+
+ CJBig2_Image(CJBig2_Image &im);
+
+ ~CJBig2_Image();
+
+ FX_BOOL getPixel(FX_INT32 x, FX_INT32 y);
+
+ FX_INT32 setPixel(FX_INT32 x, FX_INT32 y, FX_BOOL v);
+
+ void copyLine(FX_INT32 hTo, FX_INT32 hFrom);
+
+ void fill(FX_BOOL v);
+
+ FX_BOOL composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
+ FX_BOOL composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect);
+
+ FX_BOOL composeTo_unopt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
+
+ FX_BOOL composeTo_opt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
+
+ FX_BOOL composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
+ FX_BOOL composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect);
+
+ FX_BOOL composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op);
+ FX_BOOL composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op, const FX_RECT* pSrcRect);
+ CJBig2_Image *subImage_unopt(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h);
+
+ CJBig2_Image *subImage(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h);
+
+ void expand(FX_INT32 h, FX_BOOL v);
+public:
+
+ FX_INT32 m_nWidth;
+
+ FX_INT32 m_nHeight;
+
+ FX_INT32 m_nStride;
+
+ FX_BYTE *m_pData;
+
+ FX_BOOL m_bNeedFree;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_List.h b/core/src/fxcodec/jbig2/JBig2_List.h
new file mode 100644
index 0000000000..a737d9e112
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_List.h
@@ -0,0 +1,67 @@
+// 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
+
+#ifndef _JBIG2_LIST_H_
+#define _JBIG2_LIST_H_
+#include "JBig2_Define.h"
+#include "JBig2_Object.h"
+template <class TYPE>
+class CJBig2_List : public CJBig2_Object
+{
+public:
+
+ CJBig2_List(FX_INT32 nSize = 8)
+ {
+ m_nSize = nSize;
+ m_pArray = (TYPE**)m_pModule->JBig2_Malloc2(sizeof(TYPE*), nSize);
+ m_nLength = 0;
+ }
+
+ ~CJBig2_List()
+ {
+ clear();
+ m_pModule->JBig2_Free(m_pArray);
+ }
+
+ void clear()
+ {
+ FX_INT32 i;
+ for(i = 0; i < m_nLength; i++) {
+ delete m_pArray[i];
+ }
+ m_nLength = 0;
+ }
+
+ void addItem(TYPE *pItem)
+ {
+ if(m_nLength >= m_nSize) {
+ m_nSize += 8;
+ m_pArray = (TYPE**)m_pModule->JBig2_Realloc(m_pArray, sizeof(TYPE*)*m_nSize);
+ }
+ m_pArray[m_nLength++] = pItem;
+ }
+
+
+ FX_INT32 getLength()
+ {
+ return m_nLength;
+ }
+
+ TYPE *getAt(FX_INT32 nIndex)
+ {
+ return m_pArray[nIndex];
+ }
+
+ TYPE *getLast()
+ {
+ return m_pArray[m_nLength - 1];
+ }
+private:
+ FX_INT32 m_nSize;
+ TYPE **m_pArray;
+ FX_INT32 m_nLength;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Module.h b/core/src/fxcodec/jbig2/JBig2_Module.h
new file mode 100644
index 0000000000..ccde5ade02
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Module.h
@@ -0,0 +1,32 @@
+// 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
+
+#ifndef _JBIG2_MODULE_H_
+#define _JBIG2_MODULE_H_
+#include "JBig2_Define.h"
+class CJBig2_Module
+{
+public:
+
+ virtual void *JBig2_Malloc(FX_DWORD dwSize) = 0;
+
+ virtual void *JBig2_Malloc2(FX_DWORD num, FX_DWORD dwSize) = 0;
+
+ virtual void *JBig2_Malloc3(FX_DWORD num, FX_DWORD dwSize, FX_DWORD dwSize2) = 0;
+
+ virtual void *JBig2_Realloc(FX_LPVOID pMem, FX_DWORD dwSize) = 0;
+
+ virtual void JBig2_Free(FX_LPVOID pMem) = 0;
+
+ virtual void JBig2_Assert(FX_INT32 nExpression) {};
+
+ virtual void JBig2_Error(FX_LPCSTR format, ...) {};
+
+ virtual void JBig2_Warn(FX_LPCSTR format, ...) {};
+
+ virtual void JBig2_Log(FX_LPCSTR format, ...) {};
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Object.cpp b/core/src/fxcodec/jbig2/JBig2_Object.cpp
new file mode 100644
index 0000000000..3a94f0e65a
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Object.cpp
@@ -0,0 +1,72 @@
+// 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 "JBig2_Object.h"
+#include "JBig2_Module.h"
+void *CJBig2_Object::operator new(size_t size, CJBig2_Module *pModule, FX_LPCSTR filename, int line)
+{
+ CJBig2_Object *p;
+ p = (CJBig2_Object *)pModule->JBig2_Malloc((FX_DWORD)size);
+ p->m_pModule = pModule;
+ return p;
+}
+void CJBig2_Object::operator delete(void *p, CJBig2_Module *pModule, FX_LPCSTR filename, int line)
+{
+ pModule->JBig2_Free(p);
+}
+void *CJBig2_Object::operator new(size_t size, CJBig2_Module *pModule)
+{
+ CJBig2_Object *p;
+ p = (CJBig2_Object *)pModule->JBig2_Malloc((FX_DWORD)size);
+ p->m_pModule = pModule;
+ return p;
+}
+void CJBig2_Object::operator delete(void *p)
+{
+ ((CJBig2_Object *)p)->m_pModule->JBig2_Free(p);
+}
+void CJBig2_Object::operator delete(void *p, CJBig2_Module *pModule)
+{
+ pModule->JBig2_Free(p);
+}
+void *CJBig2_Object::operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size,
+ FX_LPCSTR filename, int line)
+{
+ void *p;
+ FX_BYTE *pCur, *pEnd;
+ p = (FX_BYTE *)pModule->JBig2_Malloc((FX_DWORD)size);
+ pCur = (FX_BYTE *)p;
+ pEnd = pCur + size;
+ for(; pCur < pEnd; pCur += unit_size) {
+ ((CJBig2_Object *)pCur)->m_pModule = pModule;
+ }
+ return p;
+}
+void CJBig2_Object::operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size,
+ FX_LPCSTR filename, int line)
+{
+ pModule->JBig2_Free(p);
+}
+void *CJBig2_Object::operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size)
+{
+ void *p;
+ FX_BYTE *pCur, *pEnd;
+ p = (FX_BYTE *)pModule->JBig2_Malloc((FX_DWORD)size);
+ pCur = (FX_BYTE *)p;
+ pEnd = pCur + size;
+ for(; pCur < pEnd; pCur += unit_size) {
+ ((CJBig2_Object *)pCur)->m_pModule = pModule;
+ }
+ return p;
+}
+void CJBig2_Object::operator delete[](void* p)
+{
+ ((CJBig2_Object *)p)->m_pModule->JBig2_Free(p);
+}
+void CJBig2_Object::operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size)
+{
+ pModule->JBig2_Free(p);
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Object.h b/core/src/fxcodec/jbig2/JBig2_Object.h
new file mode 100644
index 0000000000..7fef1a21a9
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Object.h
@@ -0,0 +1,43 @@
+// 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
+
+#ifndef _JBIG2_OBJECT_H_
+#define _JBIG2_OBJECT_H_
+#include "JBig2_Define.h"
+class CJBig2_Module;
+#define _JBIG2_NO_EXPECTION_
+class CJBig2_Object
+{
+public:
+
+ void *operator new(size_t size, CJBig2_Module *pModule, FX_LPCSTR filename, int line);
+
+ void operator delete(void *p, CJBig2_Module *pModule, FX_LPCSTR filename, int line);
+
+ void *operator new(size_t size, CJBig2_Module *pModule);
+
+ void operator delete(void *p);
+
+ void operator delete(void *p, CJBig2_Module *pModule);
+
+ void *operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size,
+ FX_LPCSTR filename, int line);
+
+ void operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size,
+ FX_LPCSTR filename, int line);
+
+ void *operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size);
+
+ void operator delete[](void* p);
+
+ void operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size);
+public:
+
+ CJBig2_Module *m_pModule;
+};
+#define JBIG2_NEW new(m_pModule)
+#define JBIG2_ALLOC(p, a) p = JBIG2_NEW a; p->m_pModule = m_pModule;
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Page.h b/core/src/fxcodec/jbig2/JBig2_Page.h
new file mode 100644
index 0000000000..490705448c
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Page.h
@@ -0,0 +1,19 @@
+// 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
+
+#ifndef _JBIG2_PAGE_H_
+#define _JBIG2_PAGE_H_
+#include "JBig2_Image.h"
+struct JBig2PageInfo : public CJBig2_Object {
+ FX_DWORD m_dwWidth,
+ m_dwHeight;
+ FX_DWORD m_dwResolutionX,
+ m_dwResolutionY;
+ FX_BYTE m_cFlags;
+ FX_BOOL m_bIsStriped;
+ FX_WORD m_wMaxStripeSize;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp b/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp
new file mode 100644
index 0000000000..58c52a5848
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp
@@ -0,0 +1,24 @@
+// 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 "JBig2_PatternDict.h"
+CJBig2_PatternDict::CJBig2_PatternDict()
+{
+ NUMPATS = 0;
+ HDPATS = NULL;
+}
+
+CJBig2_PatternDict::~CJBig2_PatternDict()
+{
+ if(HDPATS) {
+ for(FX_DWORD i = 0; i < NUMPATS; i++) {
+ if(HDPATS[i]) {
+ delete HDPATS[i];
+ }
+ }
+ m_pModule->JBig2_Free(HDPATS);
+ }
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_PatternDict.h b/core/src/fxcodec/jbig2/JBig2_PatternDict.h
new file mode 100644
index 0000000000..c8686507d9
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_PatternDict.h
@@ -0,0 +1,22 @@
+// 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
+
+#ifndef _JBIG2_PATTERN_DICT_H_
+#define _JBIG2_PATTERN_DICT_H_
+#include "JBig2_Define.h"
+#include "JBig2_Image.h"
+class CJBig2_PatternDict : public CJBig2_Object
+{
+public:
+
+ CJBig2_PatternDict();
+
+ ~CJBig2_PatternDict();
+public:
+ FX_DWORD NUMPATS;
+ CJBig2_Image **HDPATS;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Segment.cpp b/core/src/fxcodec/jbig2/JBig2_Segment.cpp
new file mode 100644
index 0000000000..788207dc6e
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Segment.cpp
@@ -0,0 +1,53 @@
+// 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 "JBig2_Segment.h"
+CJBig2_Segment::CJBig2_Segment()
+{
+ init();
+}
+CJBig2_Segment::~CJBig2_Segment()
+{
+ clean();
+}
+void CJBig2_Segment::init()
+{
+ m_dwNumber = 0;
+ m_cFlags.c = 0;
+ m_nReferred_to_segment_count = 0;
+ m_pReferred_to_segment_numbers = NULL;
+ m_dwPage_association = 0;
+ m_dwData_length = 0;
+ m_dwHeader_Length = 0;
+ m_pData = NULL;
+ m_State = JBIG2_SEGMENT_HEADER_UNPARSED;
+ m_nResultType = JBIG2_VOID_POINTER;
+ m_Result.vd = NULL;
+}
+void CJBig2_Segment::clean()
+{
+ if(m_pReferred_to_segment_numbers) {
+ m_pModule->JBig2_Free(m_pReferred_to_segment_numbers);
+ }
+ if(m_Result.vd) {
+ switch(m_nResultType) {
+ case JBIG2_IMAGE_POINTER:
+ delete m_Result.im;
+ break;
+ case JBIG2_SYMBOL_DICT_POINTER:
+ delete m_Result.sd;
+ break;
+ case JBIG2_PATTERN_DICT_POINTER:
+ delete m_Result.pd;
+ break;
+ case JBIG2_HUFFMAN_TABLE_POINTER:
+ delete m_Result.ht;
+ break;
+ default:
+ m_pModule->JBig2_Free(m_Result.vd);
+ }
+ }
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Segment.h b/core/src/fxcodec/jbig2/JBig2_Segment.h
new file mode 100644
index 0000000000..48c31b17bb
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_Segment.h
@@ -0,0 +1,68 @@
+// 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
+
+#ifndef _JBIG2_SEGMENT_H_
+#define _JBIG2_SEGMENT_H_
+#include "JBig2_Define.h"
+#include "JBig2_SymbolDict.h"
+#include "JBig2_PatternDict.h"
+#include "JBig2_Module.h"
+#include "JBig2_HuffmanTable.h"
+#define JBIG2_GET_INT32(buf) (((buf)[0]<<24) | ((buf)[1]<<16) | ((buf)[2]<<8) | (buf)[3])
+#define JBIG2_GET_INT16(buf) (((buf)[0]<<8) | (buf)[1])
+typedef enum {
+ JBIG2_SEGMENT_HEADER_UNPARSED,
+ JBIG2_SEGMENT_DATA_UNPARSED,
+ JBIG2_SEGMENT_PARSE_COMPLETE,
+ JBIG2_SEGMENT_PAUSED,
+ JBIG2_SEGMENT_ERROR
+} JBig2_SegmentState;
+typedef enum {
+ JBIG2_VOID_POINTER = 0,
+ JBIG2_IMAGE_POINTER,
+ JBIG2_SYMBOL_DICT_POINTER,
+ JBIG2_PATTERN_DICT_POINTER,
+ JBIG2_HUFFMAN_TABLE_POINTER
+} JBig2_ResultType;
+class CJBig2_Segment : public CJBig2_Object
+{
+public:
+
+ CJBig2_Segment();
+
+ ~CJBig2_Segment();
+
+ void init();
+
+ void clean();
+public:
+ FX_DWORD m_dwNumber;
+ union {
+ struct {
+ FX_BYTE type : 6;
+ FX_BYTE page_association_size : 1;
+ FX_BYTE deferred_non_retain : 1;
+ } s;
+ FX_BYTE c;
+ } m_cFlags;
+ FX_INT32 m_nReferred_to_segment_count;
+ FX_DWORD * m_pReferred_to_segment_numbers;
+ FX_DWORD m_dwPage_association;
+ FX_DWORD m_dwData_length;
+
+ FX_DWORD m_dwHeader_Length;
+ FX_BYTE *m_pData;
+ JBig2_SegmentState m_State;
+ JBig2_ResultType m_nResultType;
+ union {
+ CJBig2_SymbolDict *sd;
+ CJBig2_PatternDict *pd;
+ CJBig2_Image *im;
+ CJBig2_HuffmanTable *ht;
+ FX_LPVOID vd;
+ } m_Result;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp b/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp
new file mode 100644
index 0000000000..8516c045c6
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp
@@ -0,0 +1,34 @@
+// 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 "JBig2_SymbolDict.h"
+CJBig2_SymbolDict::CJBig2_SymbolDict()
+{
+ SDNUMEXSYMS = 0;
+ SDEXSYMS = NULL;
+ m_bContextRetained = FALSE;
+ m_gbContext = m_grContext = NULL;
+}
+
+CJBig2_SymbolDict::~CJBig2_SymbolDict()
+{
+ if(SDEXSYMS) {
+ for(FX_DWORD i = 0; i < SDNUMEXSYMS; i++) {
+ if(SDEXSYMS[i]) {
+ delete SDEXSYMS[i];
+ }
+ }
+ m_pModule->JBig2_Free(SDEXSYMS);
+ }
+ if(m_bContextRetained) {
+ if(m_gbContext) {
+ m_pModule->JBig2_Free(m_gbContext);
+ }
+ if(m_grContext) {
+ m_pModule->JBig2_Free(m_grContext);
+ }
+ }
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_SymbolDict.h b/core/src/fxcodec/jbig2/JBig2_SymbolDict.h
new file mode 100644
index 0000000000..cb97a167d1
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_SymbolDict.h
@@ -0,0 +1,26 @@
+// 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
+
+#ifndef _JBIG2_SYMBOL_DICT_H_
+#define _JBIG2_SYMBOL_DICT_H_
+#include "JBig2_Define.h"
+#include "JBig2_ArithDecoder.h"
+#include "JBig2_Image.h"
+class CJBig2_SymbolDict : public CJBig2_Object
+{
+public:
+
+ CJBig2_SymbolDict();
+
+ ~CJBig2_SymbolDict();
+public:
+ FX_DWORD SDNUMEXSYMS;
+ CJBig2_Image **SDEXSYMS;
+ FX_BOOL m_bContextRetained;
+ JBig2ArithCtx *m_gbContext,
+ *m_grContext;
+};
+#endif