diff options
author | Lei Zhang <thestig@chromium.org> | 2015-10-13 15:48:30 -0700 |
---|---|---|
committer | Lei Zhang <thestig@chromium.org> | 2015-10-13 15:48:30 -0700 |
commit | d80c3b49dea43a240407549b186e83b0f8ca998b (patch) | |
tree | 2bbe611823abeaf134af5ced7e29a3af51cfafd3 | |
parent | 1f4c92d3957a7e57d3efcdde1fdb0f325b37815d (diff) | |
download | pdfium-d80c3b49dea43a240407549b186e83b0f8ca998b.tar.xz |
Merge to M47: Put CJBig2_SymbolDict's images in a CJBig2_List container.
Also mark it private.
R=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1395613003 .
(cherry picked from commit 8793b4a071fad51a770b93838e0752505b020e43)
Review URL: https://codereview.chromium.org/1396013005 .
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_Context.cpp | 27 | ||||
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_Context.h | 1 | ||||
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_List.h | 25 | ||||
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_SddProc.cpp | 22 | ||||
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp | 41 | ||||
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_SymbolDict.h | 23 |
6 files changed, 71 insertions, 68 deletions
diff --git a/core/src/fxcodec/jbig2/JBig2_Context.cpp b/core/src/fxcodec/jbig2/JBig2_Context.cpp index f2c44b726a..4e21a8cca4 100644 --- a/core/src/fxcodec/jbig2/JBig2_Context.cpp +++ b/core/src/fxcodec/jbig2/JBig2_Context.cpp @@ -8,6 +8,7 @@ #include <list> +#include "JBig2_ArithDecoder.h" #include "JBig2_GrdProc.h" #include "JBig2_GrrdProc.h" #include "JBig2_HtrdProc.h" @@ -498,7 +499,7 @@ int32_t CJBig2_Context::parseSymbolDict(CJBig2_Segment* pSegment, 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; + pSymbolDictDecoder->SDNUMINSYMS += pSeg->m_Result.sd->NumImages(); pLRSeg = pSeg; } } @@ -510,9 +511,10 @@ int32_t CJBig2_Context::parseSymbolDict(CJBig2_Segment* pSegment, 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; + const CJBig2_SymbolDict& dict = *pSeg->m_Result.sd; + for (size_t j = 0; j < dict.NumImages(); ++j) + SDINSYMS[dwTemp + j] = dict.GetImage(j); + dwTemp += dict.NumImages(); } } } @@ -623,7 +625,8 @@ int32_t CJBig2_Context::parseSymbolDict(CJBig2_Segment* pSegment, for (std::list<CJBig2_CachePair>::iterator it = m_pSymbolDictCache->begin(); it != m_pSymbolDictCache->end(); ++it) { if (it->first == key) { - pSegment->m_Result.sd = it->second->DeepCopy(); + nonstd::unique_ptr<CJBig2_SymbolDict> copy(it->second->DeepCopy()); + pSegment->m_Result.sd = copy.release(); m_pSymbolDictCache->push_front(*it); m_pSymbolDictCache->erase(it); cache_hit = true; @@ -651,13 +654,14 @@ int32_t CJBig2_Context::parseSymbolDict(CJBig2_Segment* pSegment, } m_pStream->alignByte(); } - CJBig2_SymbolDict* value = pSegment->m_Result.sd->DeepCopy(); + nonstd::unique_ptr<CJBig2_SymbolDict> value = + pSegment->m_Result.sd->DeepCopy(); if (value && kSymbolDictCacheMaxSize > 0) { while (m_pSymbolDictCache->size() >= kSymbolDictCacheMaxSize) { delete m_pSymbolDictCache->back().second; m_pSymbolDictCache->pop_back(); } - m_pSymbolDictCache->push_front(CJBig2_CachePair(key, value)); + m_pSymbolDictCache->push_front(CJBig2_CachePair(key, value.release())); } } if (wFlags & 0x0200) { @@ -777,7 +781,7 @@ int32_t CJBig2_Context::parseTextRegion(CJBig2_Segment* pSegment) { 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; + pTRD->SBNUMSYMS += pSeg->m_Result.sd->NumImages(); } } if (pTRD->SBNUMSYMS > 0) { @@ -786,9 +790,10 @@ int32_t CJBig2_Context::parseTextRegion(CJBig2_Segment* pSegment) { 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; + const CJBig2_SymbolDict& dict = *pSeg->m_Result.sd; + for (size_t j = 0; j < dict.NumImages(); ++j) + SBSYMS[dwTemp + j] = dict.GetImage(j); + dwTemp += dict.NumImages(); } } pTRD->SBSYMS = SBSYMS; diff --git a/core/src/fxcodec/jbig2/JBig2_Context.h b/core/src/fxcodec/jbig2/JBig2_Context.h index 8681609ca0..f3191611b6 100644 --- a/core/src/fxcodec/jbig2/JBig2_Context.h +++ b/core/src/fxcodec/jbig2/JBig2_Context.h @@ -16,6 +16,7 @@ #include "JBig2_Page.h" #include "JBig2_Segment.h" +class CJBig2_ArithDecoder; class CJBig2_GRDProc; class IFX_Pause; diff --git a/core/src/fxcodec/jbig2/JBig2_List.h b/core/src/fxcodec/jbig2/JBig2_List.h index e033eb23ea..ffdd22c3ca 100644 --- a/core/src/fxcodec/jbig2/JBig2_List.h +++ b/core/src/fxcodec/jbig2/JBig2_List.h @@ -4,11 +4,13 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#ifndef _JBIG2_LIST_H_ -#define _JBIG2_LIST_H_ +#ifndef CORE_SRC_FXCODEC_JBIG2_JBIG2_LIST_H_ +#define CORE_SRC_FXCODEC_JBIG2_JBIG2_LIST_H_ #include <vector> +// A poor man's ScopedVector for pointers of TYPE. +// Owns all the pointers contained within and deletes them on destruction. template <class TYPE> class CJBig2_List { public: @@ -18,23 +20,28 @@ class CJBig2_List { clear(); } + TYPE* get(size_t index) const { return m_vector[index]; } + TYPE* back() const { return m_vector.back(); } + size_t size() const { return m_vector.size(); } + + // Deletes all the pointers contained within. void clear() { for (size_t i = 0; i < m_vector.size(); ++i) delete m_vector[i]; m_vector.clear(); } + // Takes ownership of |pItem|. void push_back(TYPE* pItem) { m_vector.push_back(pItem); } - size_t size() const { return m_vector.size(); } - void resize(size_t count) { m_vector.resize(count); } - - TYPE* get(size_t index) { return m_vector[index]; } - - TYPE* back() { return m_vector.back(); } + void resize(size_t count) { + for (size_t i = count; i < size(); ++i) + delete m_vector[i]; + m_vector.resize(count); + } private: std::vector<TYPE*> m_vector; }; -#endif +#endif // CORE_SRC_FXCODEC_JBIG2_JBIG2_LIST_H_ diff --git a/core/src/fxcodec/jbig2/JBig2_SddProc.cpp b/core/src/fxcodec/jbig2/JBig2_SddProc.cpp index 16f4a9024e..1d0393a7e3 100644 --- a/core/src/fxcodec/jbig2/JBig2_SddProc.cpp +++ b/core/src/fxcodec/jbig2/JBig2_SddProc.cpp @@ -258,24 +258,19 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Arith( CUREXFLAG = !CUREXFLAG; } pDict.reset(new CJBig2_SymbolDict); - pDict->SDNUMEXSYMS = SDNUMEXSYMS; - pDict->SDEXSYMS = FX_Alloc(CJBig2_Image*, SDNUMEXSYMS); I = J = 0; for (I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) { if (EXFLAGS[I] && J < SDNUMEXSYMS) { if (I < SDNUMINSYMS) { - pDict->SDEXSYMS[J] = new CJBig2_Image(*SDINSYMS[I]); + pDict->AddImage(new CJBig2_Image(*SDINSYMS[I])); } else { - pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS]; + pDict->AddImage(SDNEWSYMS[I - SDNUMINSYMS]); } - J = J + 1; + ++J; } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) { delete SDNEWSYMS[I - SDNUMINSYMS]; } } - if (J < SDNUMEXSYMS) { - pDict->SDNUMEXSYMS = J; - } FX_Free(EXFLAGS); FX_Free(SDNEWSYMS); return pDict.release(); @@ -600,24 +595,19 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman(CJBig2_BitStream* pStream, EXINDEX = EXINDEX + EXRUNLENGTH; CUREXFLAG = !CUREXFLAG; } - pDict->SDNUMEXSYMS = SDNUMEXSYMS; - pDict->SDEXSYMS = FX_Alloc(CJBig2_Image*, SDNUMEXSYMS); I = J = 0; for (I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) { if (EXFLAGS[I] && J < SDNUMEXSYMS) { if (I < SDNUMINSYMS) { - pDict->SDEXSYMS[J] = new CJBig2_Image(*SDINSYMS[I]); + pDict->AddImage(new CJBig2_Image(*SDINSYMS[I])); } else { - pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS]; + pDict->AddImage(SDNEWSYMS[I - SDNUMINSYMS]); } - J = J + 1; + ++J; } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) { delete SDNEWSYMS[I - SDNUMINSYMS]; } } - if (J < SDNUMEXSYMS) { - pDict->SDNUMEXSYMS = J; - } FX_Free(EXFLAGS); FX_Free(SDNEWSYMS); if (SDREFAGG == 0) { diff --git a/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp b/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp index ef02df1072..a8f8a94529 100644 --- a/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp +++ b/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp @@ -7,41 +7,30 @@ #include "JBig2_SymbolDict.h" #include "../../../include/fxcrt/fx_memory.h" +#include "JBig2_Image.h" CJBig2_SymbolDict::CJBig2_SymbolDict() { - SDNUMEXSYMS = 0; - SDEXSYMS = NULL; m_bContextRetained = FALSE; m_gbContext = m_grContext = NULL; } -CJBig2_SymbolDict* CJBig2_SymbolDict::DeepCopy() { - CJBig2_SymbolDict* src = this; - if (src->m_bContextRetained || src->m_gbContext || src->m_grContext) { - return NULL; - } - CJBig2_SymbolDict* dst = new CJBig2_SymbolDict; - dst->SDNUMEXSYMS = src->SDNUMEXSYMS; - dst->SDEXSYMS = FX_Alloc(CJBig2_Image*, src->SDNUMEXSYMS); - for (FX_DWORD i = 0; i < src->SDNUMEXSYMS; i++) { - if (src->SDEXSYMS[i]) { - dst->SDEXSYMS[i] = new CJBig2_Image(*(src->SDEXSYMS[i])); - } else { - dst->SDEXSYMS[i] = NULL; - } - } - return dst; -} - CJBig2_SymbolDict::~CJBig2_SymbolDict() { - if (SDEXSYMS) { - for (FX_DWORD i = 0; i < SDNUMEXSYMS; i++) { - delete SDEXSYMS[i]; - } - FX_Free(SDEXSYMS); - } if (m_bContextRetained) { FX_Free(m_gbContext); FX_Free(m_grContext); } } + +nonstd::unique_ptr<CJBig2_SymbolDict> CJBig2_SymbolDict::DeepCopy() const { + nonstd::unique_ptr<CJBig2_SymbolDict> dst; + const CJBig2_SymbolDict* src = this; + if (src->m_bContextRetained || src->m_gbContext || src->m_grContext) + return dst; + + dst.reset(new CJBig2_SymbolDict); + for (size_t i = 0; i < src->m_SDEXSYMS.size(); ++i) { + CJBig2_Image* image = src->m_SDEXSYMS.get(i); + dst->m_SDEXSYMS.push_back(image ? new CJBig2_Image(*image) : nullptr); + } + return dst; +} diff --git a/core/src/fxcodec/jbig2/JBig2_SymbolDict.h b/core/src/fxcodec/jbig2/JBig2_SymbolDict.h index 19d0f606a5..bec19f3eb7 100644 --- a/core/src/fxcodec/jbig2/JBig2_SymbolDict.h +++ b/core/src/fxcodec/jbig2/JBig2_SymbolDict.h @@ -7,22 +7,33 @@ #ifndef _JBIG2_SYMBOL_DICT_H_ #define _JBIG2_SYMBOL_DICT_H_ -#include "JBig2_ArithDecoder.h" -#include "JBig2_Define.h" -#include "JBig2_Image.h" +#include "../../../../third_party/base/nonstd_unique_ptr.h" +#include "../../../include/fxcrt/fx_basic.h" +#include "JBig2_List.h" + +class CJBig2_Image; +struct JBig2ArithCtx; class CJBig2_SymbolDict { public: CJBig2_SymbolDict(); - CJBig2_SymbolDict* DeepCopy(); ~CJBig2_SymbolDict(); + nonstd::unique_ptr<CJBig2_SymbolDict> DeepCopy() const; + + // Takes ownership of |image|. + void AddImage(CJBig2_Image* image) { m_SDEXSYMS.push_back(image); } + + size_t NumImages() const { return m_SDEXSYMS.size(); } + CJBig2_Image* GetImage(size_t index) const { return m_SDEXSYMS.get(index); } + public: - FX_DWORD SDNUMEXSYMS; - CJBig2_Image** SDEXSYMS; FX_BOOL m_bContextRetained; JBig2ArithCtx* m_gbContext; JBig2ArithCtx* m_grContext; + + private: + CJBig2_List<CJBig2_Image> m_SDEXSYMS; }; #endif |