OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fxcodec/jbig2/JBig2_Context.h" | 7 #include "core/fxcodec/jbig2/JBig2_Context.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <list> | 10 #include <list> |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 CPDF_StreamAcc* pSrcStream, | 62 CPDF_StreamAcc* pSrcStream, |
63 std::list<CJBig2_CachePair>* pSymbolDictCache, | 63 std::list<CJBig2_CachePair>* pSymbolDictCache, |
64 IFX_Pause* pPause, | 64 IFX_Pause* pPause, |
65 bool bIsGlobal) | 65 bool bIsGlobal) |
66 : m_nSegmentDecoded(0), | 66 : m_nSegmentDecoded(0), |
67 m_bInPage(false), | 67 m_bInPage(false), |
68 m_bBufSpecified(false), | 68 m_bBufSpecified(false), |
69 m_PauseStep(10), | 69 m_PauseStep(10), |
70 m_pPause(pPause), | 70 m_pPause(pPause), |
71 m_ProcessingStatus(FXCODEC_STATUS_FRAME_READY), | 71 m_ProcessingStatus(FXCODEC_STATUS_FRAME_READY), |
72 m_gbContext(NULL), | 72 m_gbContext(nullptr), |
73 m_dwOffset(0), | 73 m_dwOffset(0), |
74 m_pSymbolDictCache(pSymbolDictCache), | 74 m_pSymbolDictCache(pSymbolDictCache), |
75 m_bIsGlobal(bIsGlobal) { | 75 m_bIsGlobal(bIsGlobal) { |
76 if (pGlobalStream && (pGlobalStream->GetSize() > 0)) { | 76 if (pGlobalStream && (pGlobalStream->GetSize() > 0)) { |
77 m_pGlobalContext = new CJBig2_Context(nullptr, pGlobalStream, | 77 m_pGlobalContext = new CJBig2_Context(nullptr, pGlobalStream, |
78 pSymbolDictCache, pPause, true); | 78 pSymbolDictCache, pPause, true); |
79 } else { | 79 } else { |
80 m_pGlobalContext = nullptr; | 80 m_pGlobalContext = nullptr; |
81 } | 81 } |
82 | 82 |
83 m_pStream.reset(new CJBig2_BitStream(pSrcStream)); | 83 m_pStream.reset(new CJBig2_BitStream(pSrcStream)); |
84 } | 84 } |
85 | 85 |
86 CJBig2_Context::~CJBig2_Context() { | 86 CJBig2_Context::~CJBig2_Context() { |
87 FX_Free(m_gbContext); | 87 FX_Free(m_gbContext); |
88 m_gbContext = NULL; | 88 m_gbContext = nullptr; |
89 delete m_pGlobalContext; | 89 delete m_pGlobalContext; |
90 m_pGlobalContext = NULL; | 90 m_pGlobalContext = nullptr; |
91 } | 91 } |
92 | 92 |
93 int32_t CJBig2_Context::decode_SquentialOrgnazation(IFX_Pause* pPause) { | 93 int32_t CJBig2_Context::decode_SquentialOrgnazation(IFX_Pause* pPause) { |
94 int32_t nRet; | 94 int32_t nRet; |
95 if (m_pStream->getByteLeft() <= 0) | 95 if (m_pStream->getByteLeft() <= 0) |
96 return JBIG2_END_OF_FILE; | 96 return JBIG2_END_OF_FILE; |
97 | 97 |
98 while (m_pStream->getByteLeft() >= JBIG2_MIN_SEGMENT_SIZE) { | 98 while (m_pStream->getByteLeft() >= JBIG2_MIN_SEGMENT_SIZE) { |
99 if (!m_pSegment) { | 99 if (!m_pSegment) { |
100 m_pSegment.reset(new CJBig2_Segment); | 100 m_pSegment.reset(new CJBig2_Segment); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 int32_t count = 0; | 252 int32_t count = 0; |
253 for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { | 253 for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { |
254 CJBig2_Segment* pSeg = | 254 CJBig2_Segment* pSeg = |
255 findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); | 255 findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); |
256 if (pSeg && pSeg->m_cFlags.s.type == cType) { | 256 if (pSeg && pSeg->m_cFlags.s.type == cType) { |
257 if (count == nIndex) | 257 if (count == nIndex) |
258 return pSeg; | 258 return pSeg; |
259 ++count; | 259 ++count; |
260 } | 260 } |
261 } | 261 } |
262 return NULL; | 262 return nullptr; |
263 } | 263 } |
264 int32_t CJBig2_Context::parseSegmentHeader(CJBig2_Segment* pSegment) { | 264 int32_t CJBig2_Context::parseSegmentHeader(CJBig2_Segment* pSegment) { |
265 if (m_pStream->readInteger(&pSegment->m_dwNumber) != 0 || | 265 if (m_pStream->readInteger(&pSegment->m_dwNumber) != 0 || |
266 m_pStream->read1Byte(&pSegment->m_cFlags.c) != 0) { | 266 m_pStream->read1Byte(&pSegment->m_cFlags.c) != 0) { |
267 return JBIG2_ERROR_TOO_SHORT; | 267 return JBIG2_ERROR_TOO_SHORT; |
268 } | 268 } |
269 | 269 |
270 uint32_t dwTemp; | 270 uint32_t dwTemp; |
271 uint8_t cTemp = m_pStream->getCurByte(); | 271 uint8_t cTemp = m_pStream->getCurByte(); |
272 if ((cTemp >> 5) == 7) { | 272 if ((cTemp >> 5) == 7) { |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
730 findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); | 730 findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); |
731 if (pSeg->m_cFlags.s.type == 0) { | 731 if (pSeg->m_cFlags.s.type == 0) { |
732 const CJBig2_SymbolDict& dict = *pSeg->m_Result.sd; | 732 const CJBig2_SymbolDict& dict = *pSeg->m_Result.sd; |
733 for (size_t j = 0; j < dict.NumImages(); ++j) | 733 for (size_t j = 0; j < dict.NumImages(); ++j) |
734 SBSYMS.get()[dwTemp + j] = dict.GetImage(j); | 734 SBSYMS.get()[dwTemp + j] = dict.GetImage(j); |
735 dwTemp += dict.NumImages(); | 735 dwTemp += dict.NumImages(); |
736 } | 736 } |
737 } | 737 } |
738 pTRD->SBSYMS = SBSYMS.get(); | 738 pTRD->SBSYMS = SBSYMS.get(); |
739 } else { | 739 } else { |
740 pTRD->SBSYMS = NULL; | 740 pTRD->SBSYMS = nullptr; |
741 } | 741 } |
742 | 742 |
743 std::unique_ptr<JBig2HuffmanCode, FxFreeDeleter> SBSYMCODES; | 743 std::unique_ptr<JBig2HuffmanCode, FxFreeDeleter> SBSYMCODES; |
744 if (pTRD->SBHUFF == 1) { | 744 if (pTRD->SBHUFF == 1) { |
745 SBSYMCODES.reset( | 745 SBSYMCODES.reset( |
746 decodeSymbolIDHuffmanTable(m_pStream.get(), pTRD->SBNUMSYMS)); | 746 decodeSymbolIDHuffmanTable(m_pStream.get(), pTRD->SBNUMSYMS)); |
747 if (!SBSYMCODES) | 747 if (!SBSYMCODES) |
748 return JBIG2_ERROR_FATAL; | 748 return JBIG2_ERROR_FATAL; |
749 | 749 |
750 m_pStream->alignByte(); | 750 m_pStream->alignByte(); |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
939 if (!m_bBufSpecified) { | 939 if (!m_bBufSpecified) { |
940 JBig2PageInfo* pPageInfo = m_PageInfoList.back(); | 940 JBig2PageInfo* pPageInfo = m_PageInfoList.back(); |
941 if ((pPageInfo->m_bIsStriped == 1) && | 941 if ((pPageInfo->m_bIsStriped == 1) && |
942 (ri.y + ri.height > m_pPage->m_nHeight)) { | 942 (ri.y + ri.height > m_pPage->m_nHeight)) { |
943 m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0); | 943 m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0); |
944 } | 944 } |
945 } | 945 } |
946 m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, | 946 m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, |
947 (JBig2ComposeOp)(ri.flags & 0x03)); | 947 (JBig2ComposeOp)(ri.flags & 0x03)); |
948 delete pSegment->m_Result.im; | 948 delete pSegment->m_Result.im; |
949 pSegment->m_Result.im = NULL; | 949 pSegment->m_Result.im = nullptr; |
950 } | 950 } |
951 return JBIG2_SUCCESS; | 951 return JBIG2_SUCCESS; |
952 } | 952 } |
953 | 953 |
954 int32_t CJBig2_Context::parsePatternDict(CJBig2_Segment* pSegment, | 954 int32_t CJBig2_Context::parsePatternDict(CJBig2_Segment* pSegment, |
955 IFX_Pause* pPause) { | 955 IFX_Pause* pPause) { |
956 uint8_t cFlags; | 956 uint8_t cFlags; |
957 std::unique_ptr<CJBig2_PDDProc> pPDD(new CJBig2_PDDProc); | 957 std::unique_ptr<CJBig2_PDDProc> pPDD(new CJBig2_PDDProc); |
958 if (m_pStream->read1Byte(&cFlags) != 0 || | 958 if (m_pStream->read1Byte(&cFlags) != 0 || |
959 m_pStream->read1Byte(&pPDD->HDPW) != 0 || | 959 m_pStream->read1Byte(&pPDD->HDPW) != 0 || |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1057 if (!m_bBufSpecified) { | 1057 if (!m_bBufSpecified) { |
1058 JBig2PageInfo* pPageInfo = m_PageInfoList.back(); | 1058 JBig2PageInfo* pPageInfo = m_PageInfoList.back(); |
1059 if (pPageInfo->m_bIsStriped == 1 && | 1059 if (pPageInfo->m_bIsStriped == 1 && |
1060 ri.y + ri.height > m_pPage->m_nHeight) { | 1060 ri.y + ri.height > m_pPage->m_nHeight) { |
1061 m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0); | 1061 m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0); |
1062 } | 1062 } |
1063 } | 1063 } |
1064 m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, | 1064 m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, |
1065 (JBig2ComposeOp)(ri.flags & 0x03)); | 1065 (JBig2ComposeOp)(ri.flags & 0x03)); |
1066 delete pSegment->m_Result.im; | 1066 delete pSegment->m_Result.im; |
1067 pSegment->m_Result.im = NULL; | 1067 pSegment->m_Result.im = nullptr; |
1068 } | 1068 } |
1069 return JBIG2_SUCCESS; | 1069 return JBIG2_SUCCESS; |
1070 } | 1070 } |
1071 | 1071 |
1072 int32_t CJBig2_Context::parseGenericRegion(CJBig2_Segment* pSegment, | 1072 int32_t CJBig2_Context::parseGenericRegion(CJBig2_Segment* pSegment, |
1073 IFX_Pause* pPause) { | 1073 IFX_Pause* pPause) { |
1074 if (!m_pGRD) { | 1074 if (!m_pGRD) { |
1075 std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc); | 1075 std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc); |
1076 uint8_t cFlags; | 1076 uint8_t cFlags; |
1077 if (parseRegionInfo(&m_ri) != JBIG2_SUCCESS || | 1077 if (parseRegionInfo(&m_ri) != JBIG2_SUCCESS || |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1130 } | 1130 } |
1131 FX_RECT Rect = m_pGRD->GetReplaceRect(); | 1131 FX_RECT Rect = m_pGRD->GetReplaceRect(); |
1132 m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, | 1132 m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, |
1133 pSegment->m_Result.im, | 1133 pSegment->m_Result.im, |
1134 (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect); | 1134 (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect); |
1135 } | 1135 } |
1136 return JBIG2_SUCCESS; | 1136 return JBIG2_SUCCESS; |
1137 } else { | 1137 } else { |
1138 m_pArithDecoder.reset(); | 1138 m_pArithDecoder.reset(); |
1139 FX_Free(m_gbContext); | 1139 FX_Free(m_gbContext); |
1140 m_gbContext = NULL; | 1140 m_gbContext = nullptr; |
1141 if (!pSegment->m_Result.im) { | 1141 if (!pSegment->m_Result.im) { |
1142 m_ProcessingStatus = FXCODEC_STATUS_ERROR; | 1142 m_ProcessingStatus = FXCODEC_STATUS_ERROR; |
1143 m_pGRD.reset(); | 1143 m_pGRD.reset(); |
1144 return JBIG2_ERROR_FATAL; | 1144 return JBIG2_ERROR_FATAL; |
1145 } | 1145 } |
1146 m_pStream->alignByte(); | 1146 m_pStream->alignByte(); |
1147 m_pStream->offset(2); | 1147 m_pStream->offset(2); |
1148 } | 1148 } |
1149 } else { | 1149 } else { |
1150 m_pGRD->Start_decode_MMR(&pSegment->m_Result.im, m_pStream.get(), pPause); | 1150 m_pGRD->Start_decode_MMR(&pSegment->m_Result.im, m_pStream.get(), pPause); |
(...skipping 10 matching lines...) Expand all Loading... |
1161 (m_ri.y + m_ri.height > m_pPage->m_nHeight)) { | 1161 (m_ri.y + m_ri.height > m_pPage->m_nHeight)) { |
1162 m_pPage->expand(m_ri.y + m_ri.height, | 1162 m_pPage->expand(m_ri.y + m_ri.height, |
1163 (pPageInfo->m_cFlags & 4) ? 1 : 0); | 1163 (pPageInfo->m_cFlags & 4) ? 1 : 0); |
1164 } | 1164 } |
1165 } | 1165 } |
1166 FX_RECT Rect = m_pGRD->GetReplaceRect(); | 1166 FX_RECT Rect = m_pGRD->GetReplaceRect(); |
1167 m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, | 1167 m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, |
1168 pSegment->m_Result.im, | 1168 pSegment->m_Result.im, |
1169 (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect); | 1169 (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect); |
1170 delete pSegment->m_Result.im; | 1170 delete pSegment->m_Result.im; |
1171 pSegment->m_Result.im = NULL; | 1171 pSegment->m_Result.im = nullptr; |
1172 } | 1172 } |
1173 m_pGRD.reset(); | 1173 m_pGRD.reset(); |
1174 return JBIG2_SUCCESS; | 1174 return JBIG2_SUCCESS; |
1175 } | 1175 } |
1176 | 1176 |
1177 int32_t CJBig2_Context::parseGenericRefinementRegion(CJBig2_Segment* pSegment) { | 1177 int32_t CJBig2_Context::parseGenericRefinementRegion(CJBig2_Segment* pSegment) { |
1178 JBig2RegionInfo ri; | 1178 JBig2RegionInfo ri; |
1179 uint8_t cFlags; | 1179 uint8_t cFlags; |
1180 if (parseRegionInfo(&ri) != JBIG2_SUCCESS || | 1180 if (parseRegionInfo(&ri) != JBIG2_SUCCESS || |
1181 m_pStream->read1Byte(&cFlags) != 0) { | 1181 m_pStream->read1Byte(&cFlags) != 0) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1231 if (!m_bBufSpecified) { | 1231 if (!m_bBufSpecified) { |
1232 JBig2PageInfo* pPageInfo = m_PageInfoList.back(); | 1232 JBig2PageInfo* pPageInfo = m_PageInfoList.back(); |
1233 if ((pPageInfo->m_bIsStriped == 1) && | 1233 if ((pPageInfo->m_bIsStriped == 1) && |
1234 (ri.y + ri.height > m_pPage->m_nHeight)) { | 1234 (ri.y + ri.height > m_pPage->m_nHeight)) { |
1235 m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0); | 1235 m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0); |
1236 } | 1236 } |
1237 } | 1237 } |
1238 m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, | 1238 m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, |
1239 (JBig2ComposeOp)(ri.flags & 0x03)); | 1239 (JBig2ComposeOp)(ri.flags & 0x03)); |
1240 delete pSegment->m_Result.im; | 1240 delete pSegment->m_Result.im; |
1241 pSegment->m_Result.im = NULL; | 1241 pSegment->m_Result.im = nullptr; |
1242 } | 1242 } |
1243 return JBIG2_SUCCESS; | 1243 return JBIG2_SUCCESS; |
1244 } | 1244 } |
1245 | 1245 |
1246 int32_t CJBig2_Context::parseTable(CJBig2_Segment* pSegment) { | 1246 int32_t CJBig2_Context::parseTable(CJBig2_Segment* pSegment) { |
1247 pSegment->m_nResultType = JBIG2_HUFFMAN_TABLE_POINTER; | 1247 pSegment->m_nResultType = JBIG2_HUFFMAN_TABLE_POINTER; |
1248 pSegment->m_Result.ht = nullptr; | 1248 pSegment->m_Result.ht = nullptr; |
1249 std::unique_ptr<CJBig2_HuffmanTable> pHuff( | 1249 std::unique_ptr<CJBig2_HuffmanTable> pHuff( |
1250 new CJBig2_HuffmanTable(m_pStream.get())); | 1250 new CJBig2_HuffmanTable(m_pStream.get())); |
1251 if (!pHuff->IsOK()) | 1251 if (!pHuff->IsOK()) |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1402 SBSYMCODES[CURTEMP].code = CURCODE; | 1402 SBSYMCODES[CURTEMP].code = CURCODE; |
1403 CURCODE = CURCODE + 1; | 1403 CURCODE = CURCODE + 1; |
1404 } | 1404 } |
1405 CURTEMP = CURTEMP + 1; | 1405 CURTEMP = CURTEMP + 1; |
1406 } | 1406 } |
1407 CURLEN = CURLEN + 1; | 1407 CURLEN = CURLEN + 1; |
1408 } | 1408 } |
1409 FX_Free(LENCOUNT); | 1409 FX_Free(LENCOUNT); |
1410 FX_Free(FIRSTCODE); | 1410 FX_Free(FIRSTCODE); |
1411 } | 1411 } |
OLD | NEW |