Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: core/fxcodec/jbig2/JBig2_Context.cpp

Issue 2149903002: Use smart pointers for various Jbig2 decoding contexts (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: address comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 27 matching lines...) Expand all
38 38
39 // Implement a very small least recently used (LRU) cache. It is very 39 // Implement a very small least recently used (LRU) cache. It is very
40 // common for a JBIG2 dictionary to span multiple pages in a PDF file, 40 // common for a JBIG2 dictionary to span multiple pages in a PDF file,
41 // and we do not want to decode the same dictionary over and over 41 // and we do not want to decode the same dictionary over and over
42 // again. We key off of the memory location of the dictionary. The 42 // again. We key off of the memory location of the dictionary. The
43 // list keeps track of the freshness of entries, with freshest ones 43 // list keeps track of the freshness of entries, with freshest ones
44 // at the front. Even a tiny cache size like 2 makes a dramatic 44 // at the front. Even a tiny cache size like 2 makes a dramatic
45 // difference for typical JBIG2 documents. 45 // difference for typical JBIG2 documents.
46 static const int kSymbolDictCacheMaxSize = 2; 46 static const int kSymbolDictCacheMaxSize = 2;
47 47
48 CJBig2_Context* CJBig2_Context::CreateContext(
49 CPDF_StreamAcc* pGlobalStream,
50 CPDF_StreamAcc* pSrcStream,
51 std::list<CJBig2_CachePair>* pSymbolDictCache,
52 IFX_Pause* pPause) {
53 return new CJBig2_Context(pGlobalStream, pSrcStream, pSymbolDictCache, pPause,
54 false);
55 }
56
57 void CJBig2_Context::DestroyContext(CJBig2_Context* pContext) {
58 delete pContext;
59 }
60
61 CJBig2_Context::CJBig2_Context(CPDF_StreamAcc* pGlobalStream, 48 CJBig2_Context::CJBig2_Context(CPDF_StreamAcc* pGlobalStream,
62 CPDF_StreamAcc* pSrcStream, 49 CPDF_StreamAcc* pSrcStream,
63 std::list<CJBig2_CachePair>* pSymbolDictCache, 50 std::list<CJBig2_CachePair>* pSymbolDictCache,
64 IFX_Pause* pPause, 51 IFX_Pause* pPause,
65 bool bIsGlobal) 52 bool bIsGlobal)
66 : m_nSegmentDecoded(0), 53 : m_nSegmentDecoded(0),
67 m_bInPage(false), 54 m_bInPage(false),
68 m_bBufSpecified(false), 55 m_bBufSpecified(false),
69 m_PauseStep(10), 56 m_PauseStep(10),
70 m_pPause(pPause), 57 m_pPause(pPause),
71 m_ProcessingStatus(FXCODEC_STATUS_FRAME_READY), 58 m_ProcessingStatus(FXCODEC_STATUS_FRAME_READY),
72 m_gbContext(nullptr),
73 m_dwOffset(0), 59 m_dwOffset(0),
74 m_pSymbolDictCache(pSymbolDictCache), 60 m_pSymbolDictCache(pSymbolDictCache),
75 m_bIsGlobal(bIsGlobal) { 61 m_bIsGlobal(bIsGlobal) {
76 if (pGlobalStream && (pGlobalStream->GetSize() > 0)) { 62 if (pGlobalStream && (pGlobalStream->GetSize() > 0)) {
77 m_pGlobalContext = new CJBig2_Context(nullptr, pGlobalStream, 63 m_pGlobalContext.reset(new CJBig2_Context(nullptr, pGlobalStream,
78 pSymbolDictCache, pPause, true); 64 pSymbolDictCache, pPause, true));
79 } else {
80 m_pGlobalContext = nullptr;
81 } 65 }
82
83 m_pStream.reset(new CJBig2_BitStream(pSrcStream)); 66 m_pStream.reset(new CJBig2_BitStream(pSrcStream));
84 } 67 }
85 68
86 CJBig2_Context::~CJBig2_Context() { 69 CJBig2_Context::~CJBig2_Context() {}
87 FX_Free(m_gbContext);
88 m_gbContext = nullptr;
89 delete m_pGlobalContext;
90 m_pGlobalContext = nullptr;
91 }
92 70
93 int32_t CJBig2_Context::decode_SquentialOrgnazation(IFX_Pause* pPause) { 71 int32_t CJBig2_Context::decode_SquentialOrgnazation(IFX_Pause* pPause) {
94 int32_t nRet; 72 int32_t nRet;
95 if (m_pStream->getByteLeft() <= 0) 73 if (m_pStream->getByteLeft() <= 0)
96 return JBIG2_END_OF_FILE; 74 return JBIG2_END_OF_FILE;
97 75
98 while (m_pStream->getByteLeft() >= JBIG2_MIN_SEGMENT_SIZE) { 76 while (m_pStream->getByteLeft() >= JBIG2_MIN_SEGMENT_SIZE) {
99 if (!m_pSegment) { 77 if (!m_pSegment) {
100 m_pSegment.reset(new CJBig2_Segment); 78 m_pSegment.reset(new CJBig2_Segment);
101 nRet = parseSegmentHeader(m_pSegment.get()); 79 nRet = parseSegmentHeader(m_pSegment.get());
(...skipping 997 matching lines...) Expand 10 before | Expand all | Expand 10 after
1099 return JBIG2_ERROR_TOO_SHORT; 1077 return JBIG2_ERROR_TOO_SHORT;
1100 } 1078 }
1101 } 1079 }
1102 } 1080 }
1103 } 1081 }
1104 pGRD->USESKIP = 0; 1082 pGRD->USESKIP = 0;
1105 m_pGRD = std::move(pGRD); 1083 m_pGRD = std::move(pGRD);
1106 } 1084 }
1107 pSegment->m_nResultType = JBIG2_IMAGE_POINTER; 1085 pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
1108 if (m_pGRD->MMR == 0) { 1086 if (m_pGRD->MMR == 0) {
1109 if (!m_gbContext) { 1087 if (!m_gbContext.size()) {
Lei Zhang 2016/07/18 23:55:10 if (m_gbContext.empty())
Wei Li 2016/07/19 21:21:29 Done.
1110 const size_t size = GetHuffContextSize(m_pGRD->GBTEMPLATE); 1088 const size_t size = GetHuffContextSize(m_pGRD->GBTEMPLATE);
1111 m_gbContext = FX_Alloc(JBig2ArithCtx, size); 1089 m_gbContext.resize(size);
1112 JBIG2_memset(m_gbContext, 0, sizeof(JBig2ArithCtx) * size);
1113 } 1090 }
1114 if (!m_pArithDecoder) { 1091 if (!m_pArithDecoder) {
1115 m_pArithDecoder.reset(new CJBig2_ArithDecoder(m_pStream.get())); 1092 m_pArithDecoder.reset(new CJBig2_ArithDecoder(m_pStream.get()));
1116 m_ProcessingStatus = m_pGRD->Start_decode_Arith( 1093 m_ProcessingStatus = m_pGRD->Start_decode_Arith(&pSegment->m_Result.im,
1117 &pSegment->m_Result.im, m_pArithDecoder.get(), m_gbContext, pPause); 1094 m_pArithDecoder.get(),
1095 &m_gbContext[0], pPause);
1118 } else { 1096 } else {
1119 m_ProcessingStatus = m_pGRD->Continue_decode(pPause); 1097 m_ProcessingStatus = m_pGRD->Continue_decode(pPause);
1120 } 1098 }
1121 if (m_ProcessingStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) { 1099 if (m_ProcessingStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
1122 if (pSegment->m_cFlags.s.type != 36) { 1100 if (pSegment->m_cFlags.s.type != 36) {
1123 if (!m_bBufSpecified) { 1101 if (!m_bBufSpecified) {
1124 JBig2PageInfo* pPageInfo = m_PageInfoList.back(); 1102 JBig2PageInfo* pPageInfo = m_PageInfoList.back();
1125 if ((pPageInfo->m_bIsStriped == 1) && 1103 if ((pPageInfo->m_bIsStriped == 1) &&
1126 (m_ri.y + m_ri.height > m_pPage->m_nHeight)) { 1104 (m_ri.y + m_ri.height > m_pPage->m_nHeight)) {
1127 m_pPage->expand(m_ri.y + m_ri.height, 1105 m_pPage->expand(m_ri.y + m_ri.height,
1128 (pPageInfo->m_cFlags & 4) ? 1 : 0); 1106 (pPageInfo->m_cFlags & 4) ? 1 : 0);
1129 } 1107 }
1130 } 1108 }
1131 FX_RECT Rect = m_pGRD->GetReplaceRect(); 1109 FX_RECT Rect = m_pGRD->GetReplaceRect();
1132 m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, 1110 m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top,
1133 pSegment->m_Result.im, 1111 pSegment->m_Result.im,
1134 (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect); 1112 (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect);
1135 } 1113 }
1136 return JBIG2_SUCCESS; 1114 return JBIG2_SUCCESS;
1137 } else { 1115 } else {
1138 m_pArithDecoder.reset(); 1116 m_pArithDecoder.reset();
1139 FX_Free(m_gbContext); 1117 m_gbContext.clear();
1140 m_gbContext = nullptr;
1141 if (!pSegment->m_Result.im) { 1118 if (!pSegment->m_Result.im) {
1142 m_ProcessingStatus = FXCODEC_STATUS_ERROR; 1119 m_ProcessingStatus = FXCODEC_STATUS_ERROR;
1143 m_pGRD.reset(); 1120 m_pGRD.reset();
1144 return JBIG2_ERROR_FATAL; 1121 return JBIG2_ERROR_FATAL;
1145 } 1122 }
1146 m_pStream->alignByte(); 1123 m_pStream->alignByte();
1147 m_pStream->offset(2); 1124 m_pStream->offset(2);
1148 } 1125 }
1149 } else { 1126 } else {
1150 m_pGRD->Start_decode_MMR(&pSegment->m_Result.im, m_pStream.get(), pPause); 1127 m_pGRD->Start_decode_MMR(&pSegment->m_Result.im, m_pStream.get(), pPause);
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
1402 SBSYMCODES[CURTEMP].code = CURCODE; 1379 SBSYMCODES[CURTEMP].code = CURCODE;
1403 CURCODE = CURCODE + 1; 1380 CURCODE = CURCODE + 1;
1404 } 1381 }
1405 CURTEMP = CURTEMP + 1; 1382 CURTEMP = CURTEMP + 1;
1406 } 1383 }
1407 CURLEN = CURLEN + 1; 1384 CURLEN = CURLEN + 1;
1408 } 1385 }
1409 FX_Free(LENCOUNT); 1386 FX_Free(LENCOUNT);
1410 FX_Free(FIRSTCODE); 1387 FX_Free(FIRSTCODE);
1411 } 1388 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698