Convert CPDF_ImageMap to std::map.
authorLei Zhang <thestig@chromium.org>
Wed, 10 Jun 2015 22:25:22 +0000 (15:25 -0700)
committerLei Zhang <thestig@chromium.org>
Wed, 10 Jun 2015 22:25:22 +0000 (15:25 -0700)
Also simplify CPDF_DocPageData::ReleaseIccProfile() since its first
argument is always null.

R=tsepez@chromium.org

Review URL: https://codereview.chromium.org/1149273004.

core/include/fpdfapi/fpdf_resource.h
core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp
core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp
core/src/fpdfapi/fpdf_page/pageint.h

index cd190b9..501e3be 100644 (file)
@@ -43,14 +43,12 @@ public:
     ObjClass   m_Obj;
     FX_DWORD   m_nCount;
 };
-typedef CPDF_CountedObject<CPDF_Image*>         CPDF_CountedImage;
 typedef CPDF_CountedObject<CPDF_IccProfile*>    CPDF_CountedICCProfile;
 typedef CPDF_CountedObject<CPDF_StreamAcc*>     CPDF_CountedStreamAcc;
 using CPDF_CountedColorSpace = CPDF_CountedObject<CPDF_ColorSpace*>;
 using CPDF_CountedPattern = CPDF_CountedObject<CPDF_Pattern*>;
 
 
-typedef CFX_MapPtrTemplate<FX_DWORD, CPDF_CountedImage*>            CPDF_ImageMap;
 typedef CFX_MapPtrTemplate<CPDF_Stream*, CPDF_CountedICCProfile*>   CPDF_IccProfileMap;
 typedef CFX_MapPtrTemplate<CPDF_Stream*, CPDF_CountedStreamAcc*>    CPDF_FontFileMap;
 
index 1b989be..dc3d454 100644 (file)
@@ -583,7 +583,7 @@ CPDF_ICCBasedCS::~CPDF_ICCBasedCS()
         m_pAlterCS->ReleaseCS();
     }
     if (m_pProfile && m_pDocument) {
-        m_pDocument->GetPageData()->ReleaseIccProfile(NULL, m_pProfile);
+        m_pDocument->GetPageData()->ReleaseIccProfile(m_pProfile);
     }
 }
 FX_BOOL CPDF_ICCBasedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray)
index b277b3c..6c3d8c6 100644 (file)
 #include "../fpdf_font/font_int.h"
 #include "pageint.h"
 
+namespace {
+
+template <class KeyType, class ValueType>
+KeyType PDF_DocPageData_FindValue(
+    const CFX_MapPtrTemplate<KeyType, CPDF_CountedObject<ValueType>*>& map,
+    ValueType findValue,
+    CPDF_CountedObject<ValueType>*& findData)
+{
+    FX_POSITION pos = map.GetStartPosition();
+    while (pos) {
+        KeyType findKey;
+        map.GetNextAssoc(pos, findKey, findData);
+        if (findData->m_Obj == findValue) {
+            return findKey;
+        }
+    }
+    findData = nullptr;
+    return (KeyType)nullptr;
+}
+
+template <class KeyType, class ValueType>
+void PDF_DocPageData_Release(
+    CFX_MapPtrTemplate<KeyType, CPDF_CountedObject<ValueType>*>& map,
+    KeyType findKey,
+    ValueType findValue,
+    FX_BOOL bForce)
+{
+    if (!findKey && !findValue)
+        return;
+
+    CPDF_CountedObject<ValueType>* findData = nullptr;
+    if (!findKey) {
+        findKey = PDF_DocPageData_FindValue<KeyType, ValueType>(map, findValue, findData);
+    } else if (!map.Lookup(findKey, findData)) {
+        return;
+    }
+    if (!findData)
+        return;
+
+    if ((-- findData->m_nCount) == 0 || bForce) {
+        delete findData->m_Obj;
+        delete findData;
+        map.RemoveKey(findKey);
+    }
+}
+
+template <class KeyType, class ValueType>
+void PDF_DocPageData_Release_Key(
+    KeyType findKey,
+    FX_BOOL bForce,
+    std::map<KeyType, CPDF_CountedObject<ValueType>*>* map)
+{
+    if (!findKey)
+        return;
+
+    CPDF_CountedObject<ValueType>* findData = nullptr;
+    auto it = map->find(findKey);
+    if (it != map->end())
+        findData = it->second;
+    if (!findData)
+        return;
+
+    if ((--findData->m_nCount) == 0 || bForce) {
+        delete findData->m_Obj;
+        delete findData;
+        map->erase(it);
+    }
+}
+
+}  // namespace
+
 class CPDF_PageModule : public CPDF_PageModuleDef
 {
 public:
@@ -126,12 +197,10 @@ void CPDF_Document::RemoveColorSpaceFromPageData(CPDF_Object* pCSObj)
 }
 CPDF_DocPageData::CPDF_DocPageData(CPDF_Document *pPDFDoc)
     : m_pPDFDoc(pPDFDoc),
-      m_ImageMap(),
       m_IccProfileMap(),
       m_FontFileMap(),
       m_bForceClear(FALSE)
 {
-    m_ImageMap.InitHashTable(64);
     m_IccProfileMap.InitHashTable(16);
     m_FontFileMap.InitHashTable(32);
 }
@@ -230,18 +299,17 @@ void CPDF_DocPageData::Clear(FX_BOOL bForceRelease)
             m_FontFileMap.RemoveKey(ftKey);
         }
     }
-    pos = m_ImageMap.GetStartPosition();
-    while (pos) {
-        FX_DWORD objNum;
-        CPDF_CountedObject<CPDF_Image*>* imageData;
-        m_ImageMap.GetNextAssoc(pos, objNum, imageData);
-        if (!imageData->m_Obj) {
+
+    for (auto it = m_ImageMap.begin(); it != m_ImageMap.end();) {
+        auto curr_it = it++;
+        CPDF_CountedImage* imageData = curr_it->second;
+        if (!imageData->m_Obj)
             continue;
-        }
+
         if (bForceRelease || imageData->m_nCount < 2) {
             delete imageData->m_Obj;
             delete imageData;
-            m_ImageMap.RemoveKey(objNum);
+            m_ImageMap.erase(curr_it);
         }
     }
 }
@@ -517,30 +585,34 @@ void CPDF_DocPageData::ReleasePattern(CPDF_Object* pPatternObj)
 
 CPDF_Image* CPDF_DocPageData::GetImage(CPDF_Object* pImageStream)
 {
-    if (!pImageStream) {
-        return NULL;
-    }
-    FX_DWORD dwImageObjNum = pImageStream->GetObjNum();
-    CPDF_CountedObject<CPDF_Image*>* imageData;
-    if (m_ImageMap.Lookup(dwImageObjNum, imageData)) {
-        imageData->m_nCount ++;
+    if (!pImageStream)
+        return nullptr;
+
+    const FX_DWORD dwImageObjNum = pImageStream->GetObjNum();
+    auto it = m_ImageMap.find(dwImageObjNum);
+    if (it != m_ImageMap.end()) {
+        CPDF_CountedImage* imageData = it->second;
+        imageData->m_nCount++;
         return imageData->m_Obj;
     }
-    imageData = new CPDF_CountedObject<CPDF_Image*>;
+    CPDF_CountedImage* imageData = new CPDF_CountedImage;
     CPDF_Image* pImage = new CPDF_Image(m_pPDFDoc);
     pImage->LoadImageF((CPDF_Stream*)pImageStream, FALSE);
     imageData->m_nCount = 2;
     imageData->m_Obj = pImage;
-    m_ImageMap.SetAt(dwImageObjNum, imageData);
+    m_ImageMap[dwImageObjNum] = imageData;
     return pImage;
 }
+
 void CPDF_DocPageData::ReleaseImage(CPDF_Object* pImageStream)
 {
-    if (!pImageStream) {
+    if (!pImageStream)
         return;
-    }
-    PDF_DocPageData_Release<FX_DWORD, CPDF_Image*>(m_ImageMap, pImageStream->GetObjNum(), NULL);
+
+    PDF_DocPageData_Release_Key<FX_DWORD, CPDF_Image*>(
+        pImageStream->GetObjNum(), FALSE, &m_ImageMap);
 }
+
 CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(CPDF_Stream* pIccProfileStream)
 {
     if (!pIccProfileStream) {
@@ -569,26 +641,14 @@ CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(CPDF_Stream* pIccProfileStream)
     m_HashProfileMap.SetAt(CFX_ByteStringC(digest, 20), pIccProfileStream);
     return pProfile;
 }
-void CPDF_DocPageData::ReleaseIccProfile(CPDF_Stream* pIccProfileStream, CPDF_IccProfile* pIccProfile)
+
+void CPDF_DocPageData::ReleaseIccProfile(CPDF_IccProfile* pIccProfile)
 {
-    if (!pIccProfileStream && !pIccProfile) {
-        return;
-    }
-    CPDF_CountedObject<CPDF_IccProfile*>* ipData = NULL;
-    if (m_IccProfileMap.Lookup(pIccProfileStream, ipData) && ipData->m_nCount < 2) {
-        FX_POSITION pos = m_HashProfileMap.GetStartPosition();
-        while (pos) {
-            CFX_ByteString key;
-            CPDF_Stream* pFindStream = NULL;
-            m_HashProfileMap.GetNextAssoc(pos, key, (void*&)pFindStream);
-            if (pIccProfileStream == pFindStream) {
-                m_HashProfileMap.RemoveKey(key);
-                break;
-            }
-        }
-    }
-    PDF_DocPageData_Release<CPDF_Stream*, CPDF_IccProfile*>(m_IccProfileMap, pIccProfileStream, pIccProfile);
+    ASSERT(pIccProfile);
+    PDF_DocPageData_Release<CPDF_Stream*, CPDF_IccProfile*>(
+        m_IccProfileMap, nullptr, pIccProfile, FALSE);
 }
+
 CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc(CPDF_Stream* pFontStream)
 {
     if (!pFontStream) {
index 2855969..3e6abc2 100644 (file)
@@ -12,7 +12,6 @@
 #include "../../../include/fpdfapi/fpdf_pageobj.h"
 
 #define PARSE_STEP_LIMIT               100
-#define STREAM_PARSE_BUFSIZE   20480
 
 class CPDF_StreamParser
 {
@@ -328,40 +327,6 @@ public:
     FX_FLOAT                           m_TextLeading, m_TextRise, m_TextHorzScale;
 };
 
-template <class KeyType, class ValueType>
-KeyType PDF_DocPageData_FindValue(const CFX_MapPtrTemplate<KeyType, CPDF_CountedObject<ValueType>*> &map, ValueType findValue, CPDF_CountedObject<ValueType>*& findData)
-{
-    FX_POSITION pos = map.GetStartPosition();
-    while (pos) {
-        KeyType findKey;
-        map.GetNextAssoc(pos, findKey, findData);
-        if (findData->m_Obj == findValue) {
-            return findKey;
-        }
-    }
-    findData = NULL;
-    return (KeyType)(uintptr_t)NULL;
-}
-template <class KeyType, class ValueType>
-FX_BOOL PDF_DocPageData_Release(CFX_MapPtrTemplate<KeyType, CPDF_CountedObject<ValueType>*> &map, KeyType findKey, ValueType findValue, FX_BOOL bForce = FALSE)
-{
-    if (!findKey && !findValue) {
-        return FALSE;
-    }
-    CPDF_CountedObject<ValueType>* findData = NULL;
-    if (!findKey) {
-        findKey = PDF_DocPageData_FindValue<KeyType, ValueType>(map, findValue, findData);
-    } else if (!map.Lookup(findKey, findData)) {
-        return FALSE;
-    }
-    if (findData && ((-- findData->m_nCount) == 0 || bForce)) {
-        delete findData->m_Obj;
-        delete findData;
-        map.RemoveKey(findKey);
-        return TRUE;
-    }
-    return FALSE;
-}
 class CPDF_DocPageData
 {
   public:
@@ -380,7 +345,7 @@ class CPDF_DocPageData
     CPDF_Image*                 GetImage(CPDF_Object* pImageStream);
     void                        ReleaseImage(CPDF_Object* pImageStream);
     CPDF_IccProfile*            GetIccProfile(CPDF_Stream* pIccProfileStream);
-    void                        ReleaseIccProfile(CPDF_Stream* pIccProfileStream, CPDF_IccProfile* pIccProfile);
+    void                        ReleaseIccProfile(CPDF_IccProfile* pIccProfile);
     CPDF_StreamAcc*             GetFontFileStreamAcc(CPDF_Stream* pFontStream);
     void                        ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream, FX_BOOL bForce = FALSE);
     FX_BOOL                     IsForceClear() const {return m_bForceClear;}
@@ -388,7 +353,6 @@ class CPDF_DocPageData
     CPDF_CountedPattern*        FindPatternPtr(CPDF_Object* pPatternObj) const;
 
     CPDF_Document*              m_pPDFDoc;
-    CPDF_ImageMap               m_ImageMap;
     CPDF_IccProfileMap          m_IccProfileMap;
     CFX_MapByteStringToPtr      m_HashProfileMap;
     CPDF_FontFileMap            m_FontFileMap;
@@ -396,13 +360,16 @@ class CPDF_DocPageData
 
   private:
     using CPDF_CountedFont = CPDF_CountedObject<CPDF_Font*>;
+    using CPDF_CountedImage = CPDF_CountedObject<CPDF_Image*>;
 
     using CPDF_ColorSpaceMap = std::map<CPDF_Object*, CPDF_CountedColorSpace*>;
     using CPDF_FontMap = std::map<CPDF_Dictionary*, CPDF_CountedFont*>;
+    using CPDF_ImageMap = std::map<FX_DWORD, CPDF_CountedImage*>;
     using CPDF_PatternMap = std::map<CPDF_Object*, CPDF_CountedPattern*>;
 
     CPDF_ColorSpaceMap          m_ColorSpaceMap;
     CPDF_FontMap                m_FontMap;
+    CPDF_ImageMap               m_ImageMap;
     CPDF_PatternMap             m_PatternMap;
 };