Use smart pointers for various Jbig2 decoding contexts

Use unique_ptr for class owned member variables, and remove unnecessary
or unused functions and member variable.

BUG=pdfium:518

Review-Url: https://codereview.chromium.org/2149903002
diff --git a/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp
index 9157434..3edfe58 100644
--- a/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp
+++ b/core/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp
@@ -113,7 +113,6 @@
       m_pCompData(nullptr),
       m_pLineBuf(nullptr),
       m_pMaskedLine(nullptr),
-      m_pJbig2Context(nullptr),
       m_pMask(nullptr),
       m_pMaskStream(nullptr),
       m_Status(0) {}
@@ -127,10 +126,6 @@
   if (pCS && m_pDocument) {
     m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray());
   }
-  if (m_pJbig2Context) {
-    CCodec_Jbig2Module* pJbig2Module = CPDF_ModuleMgr::Get()->GetJbig2Module();
-    pJbig2Module->DestroyJbig2Context(m_pJbig2Context);
-  }
 }
 
 CFX_DIBitmap* CPDF_DIBSource::GetBitmap() const {
@@ -345,7 +340,7 @@
     }
     CCodec_Jbig2Module* pJbig2Module = CPDF_ModuleMgr::Get()->GetJbig2Module();
     if (!m_pJbig2Context) {
-      m_pJbig2Context = pJbig2Module->CreateJbig2Context();
+      m_pJbig2Context.reset(new CCodec_Jbig2Context());
       if (m_pStreamAcc->GetImageParam()) {
         CPDF_Stream* pGlobals =
             m_pStreamAcc->GetImageParam()->GetStreamBy("JBIG2Globals");
@@ -355,14 +350,13 @@
         }
       }
       ret = pJbig2Module->StartDecode(
-          m_pJbig2Context, m_pDocument->CodecContext(), m_Width, m_Height,
+          m_pJbig2Context.get(), m_pDocument->CodecContext(), m_Width, m_Height,
           m_pStreamAcc.get(), m_pGlobalStream.get(),
           m_pCachedBitmap->GetBuffer(), m_pCachedBitmap->GetPitch(), pPause);
       if (ret < 0) {
         m_pCachedBitmap.reset();
         m_pGlobalStream.reset();
-        pJbig2Module->DestroyJbig2Context(m_pJbig2Context);
-        m_pJbig2Context = nullptr;
+        m_pJbig2Context.reset();
         return 0;
       }
       if (ret == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
@@ -381,12 +375,11 @@
       }
       return ret1;
     }
-    ret = pJbig2Module->ContinueDecode(m_pJbig2Context, pPause);
+    ret = pJbig2Module->ContinueDecode(m_pJbig2Context.get(), pPause);
     if (ret < 0) {
       m_pCachedBitmap.reset();
       m_pGlobalStream.reset();
-      pJbig2Module->DestroyJbig2Context(m_pJbig2Context);
-      m_pJbig2Context = nullptr;
+      m_pJbig2Context.reset();
       return 0;
     }
     if (ret == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
@@ -1060,8 +1053,8 @@
       }
     } else if (m_bColorKey) {
       uint32_t reset_argb, set_argb;
-      reset_argb = m_pPalette ? m_pPalette[0] : 0xFF000000;
-      set_argb = m_pPalette ? m_pPalette[1] : 0xFFFFFFFF;
+      reset_argb = m_pPalette ? m_pPalette.get()[0] : 0xFF000000;
+      set_argb = m_pPalette ? m_pPalette.get()[1] : 0xFFFFFFFF;
       if (m_pCompData[0].m_ColorKeyMin == 0) {
         reset_argb = 0;
       }
@@ -1106,9 +1099,9 @@
       for (int col = 0; col < m_Width; col++) {
         uint8_t index = *pSrcPixel++;
         if (m_pPalette) {
-          *pDestPixel++ = FXARGB_B(m_pPalette[index]);
-          *pDestPixel++ = FXARGB_G(m_pPalette[index]);
-          *pDestPixel++ = FXARGB_R(m_pPalette[index]);
+          *pDestPixel++ = FXARGB_B(m_pPalette.get()[index]);
+          *pDestPixel++ = FXARGB_G(m_pPalette.get()[index]);
+          *pDestPixel++ = FXARGB_R(m_pPalette.get()[index]);
         } else {
           *pDestPixel++ = index;
           *pDestPixel++ = index;
@@ -1240,8 +1233,8 @@
       reset_argb = (uint32_t)-1;
     }
   } else if (m_bColorKey) {
-    reset_argb = m_pPalette ? m_pPalette[0] : 0xFF000000;
-    set_argb = m_pPalette ? m_pPalette[1] : 0xFFFFFFFF;
+    reset_argb = m_pPalette ? m_pPalette.get()[0] : 0xFF000000;
+    set_argb = m_pPalette ? m_pPalette.get()[1] : 0xFFFFFFFF;
     if (m_pCompData[0].m_ColorKeyMin == 0) {
       reset_argb = 0;
     }
@@ -1267,8 +1260,8 @@
   } else {
     if (dest_Bpp == 1) {
     } else if (m_pPalette) {
-      reset_argb = m_pPalette[0];
-      set_argb = m_pPalette[1];
+      reset_argb = m_pPalette.get()[0];
+      set_argb = m_pPalette.get()[1];
     }
   }
   for (int i = 0; i < clip_width; i++) {
@@ -1334,9 +1327,9 @@
       uint8_t* pDestPixel = dest_scan + i * 4;
       uint8_t index = pSrcLine[src_x];
       if (m_pPalette) {
-        *pDestPixel++ = FXARGB_B(m_pPalette[index]);
-        *pDestPixel++ = FXARGB_G(m_pPalette[index]);
-        *pDestPixel++ = FXARGB_R(m_pPalette[index]);
+        *pDestPixel++ = FXARGB_B(m_pPalette.get()[index]);
+        *pDestPixel++ = FXARGB_G(m_pPalette.get()[index]);
+        *pDestPixel++ = FXARGB_R(m_pPalette.get()[index]);
       } else {
         *pDestPixel++ = index;
         *pDestPixel++ = index;
@@ -1360,7 +1353,7 @@
       dest_scan[i] = index;
     } else {
       int dest_pos = i * dest_Bpp;
-      FX_ARGB argb = m_pPalette[index];
+      FX_ARGB argb = m_pPalette.get()[index];
       dest_scan[dest_pos] = FXARGB_B(argb);
       dest_scan[dest_pos + 1] = FXARGB_G(argb);
       dest_scan[dest_pos + 2] = FXARGB_R(argb);
diff --git a/core/fpdfapi/fpdf_render/render_int.h b/core/fpdfapi/fpdf_render/render_int.h
index 5477389..edbbad5 100644
--- a/core/fpdfapi/fpdf_render/render_int.h
+++ b/core/fpdfapi/fpdf_render/render_int.h
@@ -602,9 +602,9 @@
   uint8_t* m_pMaskedLine;
   std::unique_ptr<CFX_DIBitmap> m_pCachedBitmap;
   std::unique_ptr<CCodec_ScanlineDecoder> m_pDecoder;
-  CCodec_Jbig2Context* m_pJbig2Context;
   CPDF_DIBSource* m_pMask;
   std::unique_ptr<CPDF_StreamAcc> m_pGlobalStream;
+  std::unique_ptr<CCodec_Jbig2Context> m_pJbig2Context;
   CPDF_Stream* m_pMaskStream;
   int m_Status;
 };
diff --git a/core/fxcodec/codec/ccodec_jbig2module.h b/core/fxcodec/codec/ccodec_jbig2module.h
index 63e63af..5d77d53 100644
--- a/core/fxcodec/codec/ccodec_jbig2module.h
+++ b/core/fxcodec/codec/ccodec_jbig2module.h
@@ -20,7 +20,7 @@
 class CCodec_Jbig2Context {
  public:
   CCodec_Jbig2Context();
-  ~CCodec_Jbig2Context() {}
+  ~CCodec_Jbig2Context();
 
   uint32_t m_width;
   uint32_t m_height;
@@ -29,8 +29,7 @@
   uint8_t* m_dest_buf;
   uint32_t m_dest_pitch;
   IFX_Pause* m_pPause;
-  CJBig2_Context* m_pContext;
-  CJBig2_Image* m_dest_image;
+  std::unique_ptr<CJBig2_Context> m_pContext;
 };
 
 class CCodec_Jbig2Module {
@@ -38,7 +37,6 @@
   CCodec_Jbig2Module() {}
   ~CCodec_Jbig2Module();
 
-  CCodec_Jbig2Context* CreateJbig2Context();
   FXCODEC_STATUS StartDecode(CCodec_Jbig2Context* pJbig2Context,
                              std::unique_ptr<CFX_Deletable>* pContextHolder,
                              uint32_t width,
@@ -50,7 +48,6 @@
                              IFX_Pause* pPause);
   FXCODEC_STATUS ContinueDecode(CCodec_Jbig2Context* pJbig2Context,
                                 IFX_Pause* pPause);
-  void DestroyJbig2Context(CCodec_Jbig2Context* pJbig2Context);
 };
 
 #endif  // CORE_FXCODEC_CODEC_CCODEC_JBIG2MODULE_H_
diff --git a/core/fxcodec/codec/fx_codec_jbig.cpp b/core/fxcodec/codec/fx_codec_jbig.cpp
index 4876cef..8f2d07f 100644
--- a/core/fxcodec/codec/fx_codec_jbig.cpp
+++ b/core/fxcodec/codec/fx_codec_jbig.cpp
@@ -37,25 +37,19 @@
   return static_cast<JBig2DocumentContext*>(pContextHolder->get());
 }
 
-CCodec_Jbig2Context::CCodec_Jbig2Context() {
-  FXSYS_memset(this, 0, sizeof(CCodec_Jbig2Context));
-}
+CCodec_Jbig2Context::CCodec_Jbig2Context()
+    : m_width(0),
+      m_height(0),
+      m_pGlobalStream(nullptr),
+      m_pSrcStream(nullptr),
+      m_dest_buf(0),
+      m_dest_pitch(0),
+      m_pPause(nullptr) {}
+
+CCodec_Jbig2Context::~CCodec_Jbig2Context() {}
 
 CCodec_Jbig2Module::~CCodec_Jbig2Module() {}
 
-CCodec_Jbig2Context* CCodec_Jbig2Module::CreateJbig2Context() {
-  return new CCodec_Jbig2Context();
-}
-
-void CCodec_Jbig2Module::DestroyJbig2Context(
-    CCodec_Jbig2Context* pJbig2Context) {
-  if (pJbig2Context) {
-    CJBig2_Context::DestroyContext(pJbig2Context->m_pContext);
-    delete pJbig2Context;
-  }
-  pJbig2Context = nullptr;
-}
-
 FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(
     CCodec_Jbig2Context* pJbig2Context,
     std::unique_ptr<CFX_Deletable>* pContextHolder,
@@ -79,9 +73,9 @@
   pJbig2Context->m_dest_pitch = dest_pitch;
   pJbig2Context->m_pPause = pPause;
   FXSYS_memset(dest_buf, 0, height * dest_pitch);
-  pJbig2Context->m_pContext = CJBig2_Context::CreateContext(
+  pJbig2Context->m_pContext.reset(new CJBig2_Context(
       global_stream, src_stream, pJBig2DocumentContext->GetSymbolDictCache(),
-      pPause);
+      pPause, false));
   if (!pJbig2Context->m_pContext)
     return FXCODEC_STATUS_ERROR;
 
@@ -89,8 +83,7 @@
                                                     dest_pitch, pPause);
   if (pJbig2Context->m_pContext->GetProcessingStatus() ==
       FXCODEC_STATUS_DECODE_FINISH) {
-    CJBig2_Context::DestroyContext(pJbig2Context->m_pContext);
-    pJbig2Context->m_pContext = nullptr;
+    pJbig2Context->m_pContext.reset();
     if (ret != JBIG2_SUCCESS)
       return FXCODEC_STATUS_ERROR;
 
@@ -111,8 +104,7 @@
       FXCODEC_STATUS_DECODE_FINISH) {
     return pJbig2Context->m_pContext->GetProcessingStatus();
   }
-  CJBig2_Context::DestroyContext(pJbig2Context->m_pContext);
-  pJbig2Context->m_pContext = nullptr;
+  pJbig2Context->m_pContext.reset();
   if (ret != JBIG2_SUCCESS)
     return FXCODEC_STATUS_ERROR;
 
diff --git a/core/fxcodec/jbig2/JBig2_Context.cpp b/core/fxcodec/jbig2/JBig2_Context.cpp
index 8dde290..1e4c35a 100644
--- a/core/fxcodec/jbig2/JBig2_Context.cpp
+++ b/core/fxcodec/jbig2/JBig2_Context.cpp
@@ -45,19 +45,6 @@
 // difference for typical JBIG2 documents.
 static const int kSymbolDictCacheMaxSize = 2;
 
-CJBig2_Context* CJBig2_Context::CreateContext(
-    CPDF_StreamAcc* pGlobalStream,
-    CPDF_StreamAcc* pSrcStream,
-    std::list<CJBig2_CachePair>* pSymbolDictCache,
-    IFX_Pause* pPause) {
-  return new CJBig2_Context(pGlobalStream, pSrcStream, pSymbolDictCache, pPause,
-                            false);
-}
-
-void CJBig2_Context::DestroyContext(CJBig2_Context* pContext) {
-  delete pContext;
-}
-
 CJBig2_Context::CJBig2_Context(CPDF_StreamAcc* pGlobalStream,
                                CPDF_StreamAcc* pSrcStream,
                                std::list<CJBig2_CachePair>* pSymbolDictCache,
@@ -69,26 +56,17 @@
       m_PauseStep(10),
       m_pPause(pPause),
       m_ProcessingStatus(FXCODEC_STATUS_FRAME_READY),
-      m_gbContext(nullptr),
       m_dwOffset(0),
       m_pSymbolDictCache(pSymbolDictCache),
       m_bIsGlobal(bIsGlobal) {
   if (pGlobalStream && (pGlobalStream->GetSize() > 0)) {
-    m_pGlobalContext = new CJBig2_Context(nullptr, pGlobalStream,
-                                          pSymbolDictCache, pPause, true);
-  } else {
-    m_pGlobalContext = nullptr;
+    m_pGlobalContext.reset(new CJBig2_Context(nullptr, pGlobalStream,
+                                              pSymbolDictCache, pPause, true));
   }
-
   m_pStream.reset(new CJBig2_BitStream(pSrcStream));
 }
 
-CJBig2_Context::~CJBig2_Context() {
-  FX_Free(m_gbContext);
-  m_gbContext = nullptr;
-  delete m_pGlobalContext;
-  m_pGlobalContext = nullptr;
-}
+CJBig2_Context::~CJBig2_Context() {}
 
 int32_t CJBig2_Context::decode_SquentialOrgnazation(IFX_Pause* pPause) {
   int32_t nRet;
@@ -135,9 +113,11 @@
   }
   return JBIG2_SUCCESS;
 }
+
 int32_t CJBig2_Context::decode_EmbedOrgnazation(IFX_Pause* pPause) {
   return decode_SquentialOrgnazation(pPause);
 }
+
 int32_t CJBig2_Context::decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause) {
   int32_t nRet;
   while (m_pStream->getByteLeft() > JBIG2_MIN_SEGMENT_SIZE) {
@@ -158,6 +138,7 @@
   m_nSegmentDecoded = 0;
   return decode_RandomOrgnazation(pPause);
 }
+
 int32_t CJBig2_Context::decode_RandomOrgnazation(IFX_Pause* pPause) {
   for (; m_nSegmentDecoded < m_SegmentList.size(); ++m_nSegmentDecoded) {
     int32_t nRet =
@@ -176,6 +157,7 @@
   }
   return JBIG2_SUCCESS;
 }
+
 int32_t CJBig2_Context::getFirstPage(uint8_t* pBuf,
                                      int32_t width,
                                      int32_t height,
@@ -199,6 +181,7 @@
   }
   return Continue(pPause);
 }
+
 int32_t CJBig2_Context::Continue(IFX_Pause* pPause) {
   m_ProcessingStatus = FXCODEC_STATUS_DECODE_READY;
   int32_t nRet = 0;
@@ -245,6 +228,7 @@
   }
   return nullptr;
 }
+
 CJBig2_Segment* CJBig2_Context::findReferredSegmentByTypeAndIndex(
     CJBig2_Segment* pSegment,
     uint8_t cType,
@@ -261,6 +245,7 @@
   }
   return nullptr;
 }
+
 int32_t CJBig2_Context::parseSegmentHeader(CJBig2_Segment* pSegment) {
   if (m_pStream->readInteger(&pSegment->m_dwNumber) != 0 ||
       m_pStream->read1Byte(&pSegment->m_cFlags.c) != 0) {
@@ -1106,15 +1091,15 @@
   }
   pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
   if (m_pGRD->MMR == 0) {
-    if (!m_gbContext) {
+    if (m_gbContext.empty()) {
       const size_t size = GetHuffContextSize(m_pGRD->GBTEMPLATE);
-      m_gbContext = FX_Alloc(JBig2ArithCtx, size);
-      JBIG2_memset(m_gbContext, 0, sizeof(JBig2ArithCtx) * size);
+      m_gbContext.resize(size);
     }
     if (!m_pArithDecoder) {
       m_pArithDecoder.reset(new CJBig2_ArithDecoder(m_pStream.get()));
-      m_ProcessingStatus = m_pGRD->Start_decode_Arith(
-          &pSegment->m_Result.im, m_pArithDecoder.get(), m_gbContext, pPause);
+      m_ProcessingStatus = m_pGRD->Start_decode_Arith(&pSegment->m_Result.im,
+                                                      m_pArithDecoder.get(),
+                                                      &m_gbContext[0], pPause);
     } else {
       m_ProcessingStatus = m_pGRD->Continue_decode(pPause);
     }
@@ -1136,8 +1121,7 @@
       return JBIG2_SUCCESS;
     } else {
       m_pArithDecoder.reset();
-      FX_Free(m_gbContext);
-      m_gbContext = nullptr;
+      m_gbContext.clear();
       if (!pSegment->m_Result.im) {
         m_ProcessingStatus = FXCODEC_STATUS_ERROR;
         m_pGRD.reset();
@@ -1373,6 +1357,7 @@
   FX_Free(LENCOUNT);
   FX_Free(FIRSTCODE);
 }
+
 void CJBig2_Context::huffman_assign_code(JBig2HuffmanCode* SBSYMCODES,
                                          int NTEMP) {
   int CURLEN, LENMAX, CURCODE, CURTEMP, i;
diff --git a/core/fxcodec/jbig2/JBig2_Context.h b/core/fxcodec/jbig2/JBig2_Context.h
index 950ad13..1577b22 100644
--- a/core/fxcodec/jbig2/JBig2_Context.h
+++ b/core/fxcodec/jbig2/JBig2_Context.h
@@ -10,6 +10,7 @@
 #include <list>
 #include <memory>
 #include <utility>
+#include <vector>
 
 #include "core/fpdfapi/fpdf_parser/include/cpdf_object.h"
 #include "core/fxcodec/include/fx_codec_def.h"
@@ -40,13 +41,12 @@
 
 class CJBig2_Context {
  public:
-  static CJBig2_Context* CreateContext(
-      CPDF_StreamAcc* pGlobalStream,
-      CPDF_StreamAcc* pSrcStream,
-      std::list<CJBig2_CachePair>* pSymbolDictCache,
-      IFX_Pause* pPause = nullptr);
-
-  static void DestroyContext(CJBig2_Context* pContext);
+  CJBig2_Context(CPDF_StreamAcc* pGlobalStream,
+                 CPDF_StreamAcc* pSrcStream,
+                 std::list<CJBig2_CachePair>* pSymbolDictCache,
+                 IFX_Pause* pPause,
+                 bool bIsGlobal);
+  ~CJBig2_Context();
 
   int32_t getFirstPage(uint8_t* pBuf,
                        int32_t width,
@@ -58,59 +58,36 @@
   FXCODEC_STATUS GetProcessingStatus() { return m_ProcessingStatus; }
 
  private:
-  CJBig2_Context(CPDF_StreamAcc* pGlobalStream,
-                 CPDF_StreamAcc* pSrcStream,
-                 std::list<CJBig2_CachePair>* pSymbolDictCache,
-                 IFX_Pause* pPause,
-                 bool bIsGlobal);
-
-  ~CJBig2_Context();
-
   int32_t decode_SquentialOrgnazation(IFX_Pause* pPause);
-
   int32_t decode_EmbedOrgnazation(IFX_Pause* pPause);
-
   int32_t decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
-
   int32_t decode_RandomOrgnazation(IFX_Pause* pPause);
 
   CJBig2_Segment* findSegmentByNumber(uint32_t dwNumber);
-
   CJBig2_Segment* findReferredSegmentByTypeAndIndex(CJBig2_Segment* pSegment,
                                                     uint8_t cType,
                                                     int32_t nIndex);
 
   int32_t parseSegmentHeader(CJBig2_Segment* pSegment);
-
   int32_t parseSegmentData(CJBig2_Segment* pSegment, IFX_Pause* pPause);
   int32_t ProcessingParseSegmentData(CJBig2_Segment* pSegment,
                                      IFX_Pause* pPause);
-
   int32_t parseSymbolDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
-
   int32_t parseTextRegion(CJBig2_Segment* pSegment);
-
   int32_t parsePatternDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
-
   int32_t parseHalftoneRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
-
   int32_t parseGenericRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
-
   int32_t parseGenericRefinementRegion(CJBig2_Segment* pSegment);
-
   int32_t parseTable(CJBig2_Segment* pSegment);
-
   int32_t parseRegionInfo(JBig2RegionInfo* pRI);
 
   JBig2HuffmanCode* decodeSymbolIDHuffmanTable(CJBig2_BitStream* pStream,
                                                uint32_t SBNUMSYMS);
 
   void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);
-
   void huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, int NTEMP);
 
- private:
-  CJBig2_Context* m_pGlobalContext;
+  std::unique_ptr<CJBig2_Context> m_pGlobalContext;
   std::unique_ptr<CJBig2_BitStream> m_pStream;
   CJBig2_List<CJBig2_Segment> m_SegmentList;
   CJBig2_List<JBig2PageInfo> m_PageInfoList;
@@ -119,11 +96,11 @@
   bool m_bInPage;
   bool m_bBufSpecified;
   int32_t m_PauseStep;
-  IFX_Pause* m_pPause;
+  IFX_Pause* const m_pPause;
   FXCODEC_STATUS m_ProcessingStatus;
+  std::vector<JBig2ArithCtx> m_gbContext;
   std::unique_ptr<CJBig2_ArithDecoder> m_pArithDecoder;
   std::unique_ptr<CJBig2_GRDProc> m_pGRD;
-  JBig2ArithCtx* m_gbContext;
   std::unique_ptr<CJBig2_Segment> m_pSegment;
   uint32_t m_dwOffset;
   JBig2RegionInfo m_ri;
diff --git a/core/fxge/dib/fx_dib_composite.cpp b/core/fxge/dib/fx_dib_composite.cpp
index afb0551..04387db 100644
--- a/core/fxge/dib/fx_dib_composite.cpp
+++ b/core/fxge/dib/fx_dib_composite.cpp
@@ -4693,7 +4693,7 @@
     int index = 0;
     if (m_pPalette) {
       for (int i = 0; i < 2; i++) {
-        if (m_pPalette[i] == color) {
+        if (m_pPalette.get()[i] == color) {
           index = i;
         }
       }
diff --git a/core/fxge/dib/fx_dib_convert.cpp b/core/fxge/dib/fx_dib_convert.cpp
index a998613..1faa44d 100644
--- a/core/fxge/dib/fx_dib_convert.cpp
+++ b/core/fxge/dib/fx_dib_convert.cpp
@@ -48,6 +48,7 @@
   clut[l] = p_c;
   return l;
 }
+
 void _Qsort(uint32_t* alut, uint32_t* clut, int l, int r) {
   if (l < r) {
     int pI = _Partition(alut, clut, l, r);
@@ -55,11 +56,13 @@
     _Qsort(alut, clut, pI + 1, r);
   }
 }
+
 void _ColorDecode(uint32_t pal_v, uint8_t& r, uint8_t& g, uint8_t& b) {
   r = (uint8_t)((pal_v & 0xf00) >> 4);
   g = (uint8_t)(pal_v & 0x0f0);
   b = (uint8_t)((pal_v & 0x00f) << 4);
 }
+
 void _Obtain_Pal(uint32_t* aLut,
                  uint32_t* cLut,
                  uint32_t* dest_pal,
@@ -86,12 +89,14 @@
   m_aLut = nullptr;
   m_lut = 0;
 }
+
 CFX_Palette::~CFX_Palette() {
   FX_Free(m_pPalette);
   FX_Free(m_cLut);
   FX_Free(m_aLut);
   m_lut = 0;
 }
+
 FX_BOOL CFX_Palette::BuildPalette(const CFX_DIBSource* pBitmap) {
   if (!pBitmap) {
     return FALSE;
@@ -131,6 +136,7 @@
   _Obtain_Pal(m_aLut, m_cLut, m_pPalette, m_lut);
   return TRUE;
 }
+
 FX_BOOL ConvertBuffer_1bppMask2Gray(uint8_t* dest_buf,
                                     int dest_pitch,
                                     int width,
@@ -154,6 +160,7 @@
   }
   return TRUE;
 }
+
 FX_BOOL ConvertBuffer_8bppMask2Gray(uint8_t* dest_buf,
                                     int dest_pitch,
                                     int width,
@@ -168,6 +175,7 @@
   }
   return TRUE;
 }
+
 FX_BOOL ConvertBuffer_1bppPlt2Gray(uint8_t* dest_buf,
                                    int dest_pitch,
                                    int width,
@@ -649,7 +657,7 @@
                       const CFX_DIBSource* pSrcBitmap,
                       int src_left,
                       int src_top,
-                      uint32_t*& d_pal) {
+                      std::unique_ptr<uint32_t, FxFreeDeleter>* p_pal) {
   FXDIB_Format src_format = pSrcBitmap->GetFormat();
   switch (dest_format) {
     case FXDIB_Invalid:
@@ -685,17 +693,19 @@
     case FXDIB_8bppRgba: {
       if ((src_format & 0xff) == 8 && !pSrcBitmap->GetPalette()) {
         return ConvertBuffer(FXDIB_8bppMask, dest_buf, dest_pitch, width,
-                             height, pSrcBitmap, src_left, src_top, d_pal);
+                             height, pSrcBitmap, src_left, src_top, p_pal);
       }
-      d_pal = FX_Alloc(uint32_t, 256);
+      p_pal->reset(FX_Alloc(uint32_t, 256));
       if (((src_format & 0xff) == 1 || (src_format & 0xff) == 8) &&
           pSrcBitmap->GetPalette()) {
         return ConvertBuffer_Plt2PltRgb8(dest_buf, dest_pitch, width, height,
-                                         pSrcBitmap, src_left, src_top, d_pal);
+                                         pSrcBitmap, src_left, src_top,
+                                         p_pal->get());
       }
       if ((src_format & 0xff) >= 24) {
         return ConvertBuffer_Rgb2PltRgb8(dest_buf, dest_pitch, width, height,
-                                         pSrcBitmap, src_left, src_top, d_pal);
+                                         pSrcBitmap, src_left, src_top,
+                                         p_pal->get());
       }
       return FALSE;
     }
@@ -800,16 +810,13 @@
   if (!ret)
     return nullptr;
 
-  uint32_t* pal_8bpp = nullptr;
+  std::unique_ptr<uint32_t, FxFreeDeleter> pal_8bpp;
   if (!ConvertBuffer(dest_format, pClone->GetBuffer(), pClone->GetPitch(),
-                     m_Width, m_Height, this, 0, 0, pal_8bpp)) {
-    FX_Free(pal_8bpp);
+                     m_Width, m_Height, this, 0, 0, &pal_8bpp)) {
     return nullptr;
   }
-  if (pal_8bpp) {
-    pClone->CopyPalette(pal_8bpp);
-    FX_Free(pal_8bpp);
-  }
+  if (pal_8bpp)
+    pClone->CopyPalette(pal_8bpp.get());
   return pClone.release();
 }
 
@@ -874,26 +881,21 @@
     }
   }
   FX_BOOL ret = FALSE;
-  uint32_t* pal_8bpp = nullptr;
+  std::unique_ptr<uint32_t, FxFreeDeleter> pal_8bpp;
   ret = ConvertBuffer(dest_format, dest_buf, dest_pitch, m_Width, m_Height,
-                      this, 0, 0, pal_8bpp);
+                      this, 0, 0, &pal_8bpp);
   if (!ret) {
-    FX_Free(pal_8bpp);
-    if (pAlphaMask != m_pAlphaMask) {
+    if (pAlphaMask != m_pAlphaMask)
       delete pAlphaMask;
-    }
     FX_Free(dest_buf);
     return FALSE;
   }
-  if (m_pAlphaMask && pAlphaMask != m_pAlphaMask) {
+  if (m_pAlphaMask && pAlphaMask != m_pAlphaMask)
     delete m_pAlphaMask;
-  }
   m_pAlphaMask = pAlphaMask;
-  FX_Free(m_pPalette);
-  m_pPalette = pal_8bpp;
-  if (!m_bExtBuf) {
+  m_pPalette = std::move(pal_8bpp);
+  if (!m_bExtBuf)
     FX_Free(m_pBuffer);
-  }
   m_bExtBuf = FALSE;
   m_pBuffer = dest_buf;
   m_bpp = (uint8_t)dest_format;
diff --git a/core/fxge/dib/fx_dib_main.cpp b/core/fxge/dib/fx_dib_main.cpp
index bda5ee5..b977a62 100644
--- a/core/fxge/dib/fx_dib_main.cpp
+++ b/core/fxge/dib/fx_dib_main.cpp
@@ -6,6 +6,7 @@
 
 #include "core/fxge/include/fx_dib.h"
 
+#include <algorithm>
 #include <limits.h>
 
 #include "core/fxcodec/include/fx_codec.h"
@@ -18,32 +19,33 @@
   y = FXSYS_GetYValue(cmyk);
   k = FXSYS_GetKValue(cmyk);
 }
+
 void ArgbDecode(uint32_t argb, int& a, int& r, int& g, int& b) {
   a = FXARGB_A(argb);
   r = FXARGB_R(argb);
   g = FXARGB_G(argb);
   b = FXARGB_B(argb);
 }
+
 void ArgbDecode(uint32_t argb, int& a, FX_COLORREF& rgb) {
   a = FXARGB_A(argb);
   rgb = FXSYS_RGB(FXARGB_R(argb), FXARGB_G(argb), FXARGB_B(argb));
 }
+
 uint32_t ArgbEncode(int a, FX_COLORREF rgb) {
   return FXARGB_MAKE(a, FXSYS_GetRValue(rgb), FXSYS_GetGValue(rgb),
                      FXSYS_GetBValue(rgb));
 }
 
-CFX_DIBSource::CFX_DIBSource() {
-  m_bpp = 0;
-  m_AlphaFlag = 0;
-  m_Width = m_Height = 0;
-  m_Pitch = 0;
-  m_pPalette = nullptr;
-  m_pAlphaMask = nullptr;
-}
+CFX_DIBSource::CFX_DIBSource()
+    : m_pAlphaMask(nullptr),
+      m_Width(0),
+      m_Height(0),
+      m_bpp(0),
+      m_AlphaFlag(0),
+      m_Pitch(0) {}
 
 CFX_DIBSource::~CFX_DIBSource() {
-  FX_Free(m_pPalette);
   delete m_pAlphaMask;
 }
 
@@ -150,13 +152,11 @@
   if (!m_bExtBuf)
     FX_Free(m_pBuffer);
 
-  FX_Free(m_pPalette);
   delete m_pAlphaMask;
   m_pBuffer = pSrcBitmap->m_pBuffer;
-  m_pPalette = pSrcBitmap->m_pPalette;
+  m_pPalette = std::move(pSrcBitmap->m_pPalette);
   m_pAlphaMask = pSrcBitmap->m_pAlphaMask;
   pSrcBitmap->m_pBuffer = nullptr;
-  pSrcBitmap->m_pPalette = nullptr;
   pSrcBitmap->m_pAlphaMask = nullptr;
   m_bpp = pSrcBitmap->m_bpp;
   m_bExtBuf = pSrcBitmap->m_bExtBuf;
@@ -174,12 +174,11 @@
       return nullptr;
     }
   }
-  CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap;
-  if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat())) {
-    delete pNewBitmap;
+  std::unique_ptr<CFX_DIBitmap> pNewBitmap(new CFX_DIBitmap);
+  if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat()))
     return nullptr;
-  }
-  pNewBitmap->CopyPalette(m_pPalette);
+
+  pNewBitmap->CopyPalette(m_pPalette.get());
   pNewBitmap->CopyAlphaMask(m_pAlphaMask, pClip);
   if (GetBPP() == 1 && rect.left % 8 != 0) {
     int left_shift = rect.left % 32;
@@ -204,34 +203,36 @@
       FXSYS_memcpy(dest_scan, src_scan, copy_len);
     }
   }
-  return pNewBitmap;
+  return pNewBitmap.release();
 }
+
 void CFX_DIBSource::BuildPalette() {
   if (m_pPalette) {
     return;
   }
   if (GetBPP() == 1) {
-    m_pPalette = FX_Alloc(uint32_t, 2);
+    m_pPalette.reset(FX_Alloc(uint32_t, 2));
     if (IsCmykImage()) {
-      m_pPalette[0] = 0xff;
-      m_pPalette[1] = 0;
+      m_pPalette.get()[0] = 0xff;
+      m_pPalette.get()[1] = 0;
     } else {
-      m_pPalette[0] = 0xff000000;
-      m_pPalette[1] = 0xffffffff;
+      m_pPalette.get()[0] = 0xff000000;
+      m_pPalette.get()[1] = 0xffffffff;
     }
   } else if (GetBPP() == 8) {
-    m_pPalette = FX_Alloc(uint32_t, 256);
+    m_pPalette.reset(FX_Alloc(uint32_t, 256));
     if (IsCmykImage()) {
       for (int i = 0; i < 256; i++) {
-        m_pPalette[i] = 0xff - i;
+        m_pPalette.get()[i] = 0xff - i;
       }
     } else {
       for (int i = 0; i < 256; i++) {
-        m_pPalette[i] = 0xff000000 | (i * 0x10101);
+        m_pPalette.get()[i] = 0xff000000 | (i * 0x10101);
       }
     }
   }
 }
+
 FX_BOOL CFX_DIBSource::BuildAlphaMask() {
   if (m_pAlphaMask) {
     return TRUE;
@@ -246,10 +247,11 @@
                m_pAlphaMask->GetHeight() * m_pAlphaMask->GetPitch());
   return TRUE;
 }
+
 uint32_t CFX_DIBSource::GetPaletteEntry(int index) const {
   ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
   if (m_pPalette) {
-    return m_pPalette[index];
+    return m_pPalette.get()[index];
   }
   if (IsCmykImage()) {
     if (GetBPP() == 1) {
@@ -262,13 +264,15 @@
   }
   return index * 0x10101 | 0xff000000;
 }
+
 void CFX_DIBSource::SetPaletteEntry(int index, uint32_t color) {
   ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
   if (!m_pPalette) {
     BuildPalette();
   }
-  m_pPalette[index] = color;
+  m_pPalette.get()[index] = color;
 }
+
 int CFX_DIBSource::FindPalette(uint32_t color) const {
   ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
   if (!m_pPalette) {
@@ -285,11 +289,12 @@
   }
   int palsize = (1 << GetBPP());
   for (int i = 0; i < palsize; i++)
-    if (m_pPalette[i] == color) {
+    if (m_pPalette.get()[i] == color) {
       return i;
     }
   return -1;
 }
+
 void CFX_DIBitmap::Clear(uint32_t color) {
   if (!m_pBuffer) {
     return;
@@ -351,6 +356,7 @@
       break;
   }
 }
+
 void CFX_DIBSource::GetOverlapRect(int& dest_left,
                                    int& dest_top,
                                    int& width,
@@ -441,9 +447,9 @@
 
     uint8_t* dest_buf =
         m_pBuffer + dest_top * m_Pitch + dest_left * GetBPP() / 8;
-    uint32_t* d_plt = nullptr;
+    std::unique_ptr<uint32_t, FxFreeDeleter> d_plt;
     if (!ConvertBuffer(dest_format, dest_buf, m_Pitch, width, height,
-                       pSrcBitmap, src_left, src_top, d_plt)) {
+                       pSrcBitmap, src_left, src_top, &d_plt)) {
       return FALSE;
     }
   }
@@ -559,33 +565,33 @@
   }
   return TRUE;
 }
-void CFX_DIBSource::CopyPalette(const uint32_t* pSrc, uint32_t size) {
+
+void CFX_DIBSource::CopyPalette(const uint32_t* pSrc) {
+  static const uint32_t kPaletteSize = 256;
+
   if (!pSrc || GetBPP() > 8) {
-    FX_Free(m_pPalette);
-    m_pPalette = nullptr;
+    m_pPalette.reset();
   } else {
     uint32_t pal_size = 1 << GetBPP();
-    if (!m_pPalette) {
-      m_pPalette = FX_Alloc(uint32_t, pal_size);
-    }
-    if (pal_size > size) {
-      pal_size = size;
-    }
-    FXSYS_memcpy(m_pPalette, pSrc, pal_size * sizeof(uint32_t));
+    if (!m_pPalette)
+      m_pPalette.reset(FX_Alloc(uint32_t, pal_size));
+    pal_size = std::min(pal_size, kPaletteSize);
+    FXSYS_memcpy(m_pPalette.get(), pSrc, pal_size * sizeof(uint32_t));
   }
 }
+
 void CFX_DIBSource::GetPalette(uint32_t* pal, int alpha) const {
   ASSERT(GetBPP() <= 8 && !IsCmykImage());
   if (GetBPP() == 1) {
-    pal[0] =
-        ((m_pPalette ? m_pPalette[0] : 0xff000000) & 0xffffff) | (alpha << 24);
-    pal[1] =
-        ((m_pPalette ? m_pPalette[1] : 0xffffffff) & 0xffffff) | (alpha << 24);
+    pal[0] = ((m_pPalette ? m_pPalette.get()[0] : 0xff000000) & 0xffffff) |
+             (alpha << 24);
+    pal[1] = ((m_pPalette ? m_pPalette.get()[1] : 0xffffffff) & 0xffffff) |
+             (alpha << 24);
     return;
   }
   if (m_pPalette) {
     for (int i = 0; i < 256; i++) {
-      pal[i] = (m_pPalette[i] & 0x00ffffff) | (alpha << 24);
+      pal[i] = (m_pPalette.get()[i] & 0x00ffffff) | (alpha << 24);
     }
   } else {
     for (int i = 0; i < 256; i++) {
@@ -593,6 +599,7 @@
     }
   }
 }
+
 CFX_DIBitmap* CFX_DIBSource::GetAlphaMask(const FX_RECT* pClip) const {
   ASSERT(GetFormat() == FXDIB_Argb);
   FX_RECT rect(0, 0, m_Width, m_Height);
@@ -617,6 +624,7 @@
   }
   return pMask;
 }
+
 FX_BOOL CFX_DIBSource::CopyAlphaMask(const CFX_DIBSource* pAlphaMask,
                                      const FX_RECT* pClip) {
   if (!HasAlpha() || GetFormat() == FXDIB_Argb) {
@@ -644,6 +652,7 @@
   }
   return TRUE;
 }
+
 const int g_ChannelOffset[] = {0, 2, 1, 0, 0, 1, 2, 3, 3};
 FX_BOOL CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel,
                                   const CFX_DIBSource* pSrcBitmap,
@@ -787,6 +796,7 @@
   }
   return TRUE;
 }
+
 FX_BOOL CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, int value) {
   if (!m_pBuffer) {
     return FALSE;
@@ -845,6 +855,7 @@
   }
   return TRUE;
 }
+
 FX_BOOL CFX_DIBitmap::MultiplyAlpha(const CFX_DIBSource* pSrcBitmap) {
   if (!m_pBuffer) {
     return FALSE;
@@ -912,6 +923,7 @@
   }
   return TRUE;
 }
+
 FX_BOOL CFX_DIBitmap::GetGrayData(void* pIccTransform) {
   if (!m_pBuffer) {
     return FALSE;
@@ -923,10 +935,10 @@
       }
       uint8_t gray[2];
       for (int i = 0; i < 2; i++) {
-        int r = (uint8_t)(m_pPalette[i] >> 16);
-        int g = (uint8_t)(m_pPalette[i] >> 8);
-        int b = (uint8_t)m_pPalette[i];
-        gray[i] = (uint8_t)FXRGB2GRAY(r, g, b);
+        int r = static_cast<uint8_t>(m_pPalette.get()[i] >> 16);
+        int g = static_cast<uint8_t>(m_pPalette.get()[i] >> 8);
+        int b = static_cast<uint8_t>(m_pPalette.get()[i]);
+        gray[i] = static_cast<uint8_t>(FXRGB2GRAY(r, g, b));
       }
       CFX_DIBitmap* pMask = new CFX_DIBitmap;
       if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
@@ -954,10 +966,10 @@
       }
       uint8_t gray[256];
       for (int i = 0; i < 256; i++) {
-        int r = (uint8_t)(m_pPalette[i] >> 16);
-        int g = (uint8_t)(m_pPalette[i] >> 8);
-        int b = (uint8_t)m_pPalette[i];
-        gray[i] = (uint8_t)FXRGB2GRAY(r, g, b);
+        int r = static_cast<uint8_t>(m_pPalette.get()[i] >> 16);
+        int g = static_cast<uint8_t>(m_pPalette.get()[i] >> 8);
+        int b = static_cast<uint8_t>(m_pPalette.get()[i]);
+        gray[i] = static_cast<uint8_t>(FXRGB2GRAY(r, g, b));
       }
       CFX_DIBitmap* pMask = new CFX_DIBitmap;
       if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
@@ -1016,6 +1028,7 @@
   }
   return TRUE;
 }
+
 FX_BOOL CFX_DIBitmap::MultiplyAlpha(int alpha) {
   if (!m_pBuffer) {
     return FALSE;
@@ -1064,6 +1077,7 @@
   }
   return TRUE;
 }
+
 uint32_t CFX_DIBitmap::GetPixel(int x, int y) const {
   if (!m_pBuffer) {
     return 0;
@@ -1078,14 +1092,15 @@
     }
     case FXDIB_1bppRgb: {
       if ((*pos) & (1 << (7 - x % 8))) {
-        return m_pPalette ? m_pPalette[1] : 0xffffffff;
+        return m_pPalette ? m_pPalette.get()[1] : 0xffffffff;
       }
-      return m_pPalette ? m_pPalette[0] : 0xff000000;
+      return m_pPalette ? m_pPalette.get()[0] : 0xff000000;
     }
     case FXDIB_8bppMask:
       return (*pos) << 24;
     case FXDIB_8bppRgb:
-      return m_pPalette ? m_pPalette[*pos] : (0xff000000 | ((*pos) * 0x10101));
+      return m_pPalette ? m_pPalette.get()[*pos]
+                        : (0xff000000 | ((*pos) * 0x10101));
     case FXDIB_Rgb:
     case FXDIB_Rgba:
     case FXDIB_Rgb32:
@@ -1097,6 +1112,7 @@
   }
   return 0;
 }
+
 void CFX_DIBitmap::SetPixel(int x, int y, uint32_t color) {
   if (!m_pBuffer) {
     return;
@@ -1115,7 +1131,7 @@
       break;
     case FXDIB_1bppRgb:
       if (m_pPalette) {
-        if (color == m_pPalette[1]) {
+        if (color == m_pPalette.get()[1]) {
           *pos |= 1 << (7 - x % 8);
         } else {
           *pos &= ~(1 << (7 - x % 8));
@@ -1134,7 +1150,7 @@
     case FXDIB_8bppRgb: {
       if (m_pPalette) {
         for (int i = 0; i < 256; i++) {
-          if (m_pPalette[i] == color) {
+          if (m_pPalette.get()[i] == color) {
             *pos = (uint8_t)i;
             return;
           }
@@ -1166,6 +1182,7 @@
       break;
   }
 }
+
 void CFX_DIBitmap::DownSampleScanline(int line,
                                       uint8_t* dest_scan,
                                       int dest_bpp,
@@ -1200,13 +1217,13 @@
       if (m_pPalette) {
         if (!IsCmykImage()) {
           dest_pos *= 3;
-          FX_ARGB argb = m_pPalette[scanline[src_x]];
+          FX_ARGB argb = m_pPalette.get()[scanline[src_x]];
           dest_scan[dest_pos] = FXARGB_B(argb);
           dest_scan[dest_pos + 1] = FXARGB_G(argb);
           dest_scan[dest_pos + 2] = FXARGB_R(argb);
         } else {
           dest_pos *= 4;
-          FX_CMYK cmyk = m_pPalette[scanline[src_x]];
+          FX_CMYK cmyk = m_pPalette.get()[scanline[src_x]];
           dest_scan[dest_pos] = FXSYS_GetCValue(cmyk);
           dest_scan[dest_pos + 1] = FXSYS_GetMValue(cmyk);
           dest_scan[dest_pos + 2] = FXSYS_GetYValue(cmyk);
@@ -1288,22 +1305,23 @@
     if (isCmykImage) {
       for (int i = 0; i < size; i++) {
         uint8_t b, g, r;
-        AdobeCMYK_to_sRGB1(FXSYS_GetCValue(m_pPalette[i]),
-                           FXSYS_GetMValue(m_pPalette[i]),
-                           FXSYS_GetYValue(m_pPalette[i]),
-                           FXSYS_GetKValue(m_pPalette[i]), r, g, b);
+        AdobeCMYK_to_sRGB1(FXSYS_GetCValue(m_pPalette.get()[i]),
+                           FXSYS_GetMValue(m_pPalette.get()[i]),
+                           FXSYS_GetYValue(m_pPalette.get()[i]),
+                           FXSYS_GetKValue(m_pPalette.get()[i]), r, g, b);
         int gray = 255 - FXRGB2GRAY(r, g, b);
-        m_pPalette[i] = CmykEncode(
+        m_pPalette.get()[i] = CmykEncode(
             bc + (fc - bc) * gray / 255, bm + (fm - bm) * gray / 255,
             by + (fy - by) * gray / 255, bk + (fk - bk) * gray / 255);
       }
     } else {
       for (int i = 0; i < size; i++) {
-        int gray = FXRGB2GRAY(FXARGB_R(m_pPalette[i]), FXARGB_G(m_pPalette[i]),
-                              FXARGB_B(m_pPalette[i]));
-        m_pPalette[i] = FXARGB_MAKE(0xff, br + (fr - br) * gray / 255,
-                                    bg + (fg - bg) * gray / 255,
-                                    bb + (fb - bb) * gray / 255);
+        int gray = FXRGB2GRAY(FXARGB_R(m_pPalette.get()[i]),
+                              FXARGB_G(m_pPalette.get()[i]),
+                              FXARGB_B(m_pPalette.get()[i]));
+        m_pPalette.get()[i] = FXARGB_MAKE(0xff, br + (fr - br) * gray / 255,
+                                          bg + (fg - bg) * gray / 255,
+                                          bb + (fb - bb) * gray / 255);
       }
     }
     return TRUE;
@@ -1374,7 +1392,7 @@
     delete pFlipped;
     return nullptr;
   }
-  pFlipped->CopyPalette(m_pPalette);
+  pFlipped->CopyPalette(m_pPalette.get());
   uint8_t* pDestBuffer = pFlipped->GetBuffer();
   int Bpp = m_bpp / 8;
   for (int row = 0; row < m_Height; row++) {
@@ -1439,6 +1457,7 @@
   }
   return pFlipped;
 }
+
 CFX_DIBExtractor::CFX_DIBExtractor(const CFX_DIBSource* pSrc) {
   m_pBitmap = nullptr;
   if (pSrc->GetBuffer()) {
@@ -1455,19 +1474,23 @@
     m_pBitmap = pSrc->Clone();
   }
 }
+
 CFX_DIBExtractor::~CFX_DIBExtractor() {
   delete m_pBitmap;
 }
+
 CFX_FilteredDIB::CFX_FilteredDIB() {
   m_pScanline = nullptr;
   m_pSrc = nullptr;
 }
+
 CFX_FilteredDIB::~CFX_FilteredDIB() {
   if (m_bAutoDropSrc) {
     delete m_pSrc;
   }
   FX_Free(m_pScanline);
 }
+
 void CFX_FilteredDIB::LoadSrc(const CFX_DIBSource* pSrc, FX_BOOL bAutoDropSrc) {
   m_pSrc = pSrc;
   m_bAutoDropSrc = bAutoDropSrc;
@@ -1477,13 +1500,15 @@
   m_bpp = (uint8_t)format;
   m_AlphaFlag = (uint8_t)(format >> 8);
   m_Pitch = (m_Width * (format & 0xff) + 31) / 32 * 4;
-  m_pPalette = GetDestPalette();
+  m_pPalette.reset(GetDestPalette());
   m_pScanline = FX_Alloc(uint8_t, m_Pitch);
 }
+
 const uint8_t* CFX_FilteredDIB::GetScanline(int line) const {
   TranslateScanline(m_pScanline, m_pSrc->GetScanline(line));
   return m_pScanline;
 }
+
 void CFX_FilteredDIB::DownSampleScanline(int line,
                                          uint8_t* dest_scan,
                                          int dest_bpp,
@@ -1495,12 +1520,14 @@
                              clip_left, clip_width);
   TranslateDownSamples(dest_scan, dest_scan, clip_width, dest_bpp);
 }
+
 CFX_ImageRenderer::CFX_ImageRenderer() {
   m_Status = 0;
   m_pTransformer = nullptr;
   m_bRgbByteOrder = FALSE;
   m_BlendType = FXDIB_BLEND_NORMAL;
 }
+
 CFX_ImageRenderer::~CFX_ImageRenderer() {
   delete m_pTransformer;
 }
@@ -1658,6 +1685,7 @@
                  m_pBitmap->m_pAlphaMask->GetPitch());
   }
 }
+
 FX_BOOL CFX_BitmapStorer::SetInfo(int width,
                                   int height,
                                   FXDIB_Format src_format,
diff --git a/core/fxge/dib/fx_dib_transform.cpp b/core/fxge/dib/fx_dib_transform.cpp
index 82b9cf7..d997a8f 100644
--- a/core/fxge/dib/fx_dib_transform.cpp
+++ b/core/fxge/dib/fx_dib_transform.cpp
@@ -190,7 +190,7 @@
     delete pTransBitmap;
     return nullptr;
   }
-  pTransBitmap->CopyPalette(m_pPalette);
+  pTransBitmap->CopyPalette(m_pPalette.get());
   int dest_pitch = pTransBitmap->GetPitch();
   uint8_t* dest_buf = pTransBitmap->GetBuffer();
   int row_start = bXFlip ? m_Height - dest_clip.right : dest_clip.left;
diff --git a/core/fxge/include/fx_dib.h b/core/fxge/include/fx_dib.h
index 8d9f27e..c2ca859 100644
--- a/core/fxge/include/fx_dib.h
+++ b/core/fxge/include/fx_dib.h
@@ -164,21 +164,12 @@
                       const CFX_DIBSource* pSrcBitmap,
                       int src_left,
                       int src_top,
-                      uint32_t*& pal);
+                      std::unique_ptr<uint32_t, FxFreeDeleter>* pal);
 
 class CFX_DIBSource {
  public:
   virtual ~CFX_DIBSource();
 
-  int GetWidth() const { return m_Width; }
-  int GetHeight() const { return m_Height; }
-
-  FXDIB_Format GetFormat() const {
-    return (FXDIB_Format)(m_AlphaFlag * 0x100 + m_bpp);
-  }
-  uint32_t GetPitch() const { return m_Pitch; }
-  uint32_t* GetPalette() const { return m_pPalette; }
-
   virtual uint8_t* GetBuffer() const;
   virtual const uint8_t* GetScanline(int line) const = 0;
   virtual FX_BOOL SkipToScanline(int line, IFX_Pause* pPause) const;
@@ -190,6 +181,14 @@
                                   int clip_left,
                                   int clip_width) const = 0;
 
+  int GetWidth() const { return m_Width; }
+  int GetHeight() const { return m_Height; }
+
+  FXDIB_Format GetFormat() const {
+    return (FXDIB_Format)(m_AlphaFlag * 0x100 + m_bpp);
+  }
+  uint32_t GetPitch() const { return m_Pitch; }
+  uint32_t* GetPalette() const { return m_pPalette.get(); }
   int GetBPP() const { return m_bpp; }
 
   // TODO(thestig): Investigate this. Given the possible values of FXDIB_Format,
@@ -212,7 +211,7 @@
     SetPaletteEntry(index, color);
   }
 
-  void CopyPalette(const uint32_t* pSrcPal, uint32_t size = 256);
+  void CopyPalette(const uint32_t* pSrcPal);
 
   CFX_DIBitmap* Clone(const FX_RECT* pClip = nullptr) const;
   CFX_DIBitmap* CloneConvert(FXDIB_Format format) const;
@@ -256,17 +255,18 @@
  protected:
   CFX_DIBSource();
 
+  void BuildPalette();
+  FX_BOOL BuildAlphaMask();
+  int FindPalette(uint32_t color) const;
+  void GetPalette(uint32_t* pal, int alpha) const;
+
   int m_Width;
   int m_Height;
   int m_bpp;
   uint32_t m_AlphaFlag;
   uint32_t m_Pitch;
-  uint32_t* m_pPalette;
-
-  void BuildPalette();
-  FX_BOOL BuildAlphaMask();
-  int FindPalette(uint32_t color) const;
-  void GetPalette(uint32_t* pal, int alpha) const;
+  // TODO(weili): Use std::vector for this.
+  std::unique_ptr<uint32_t, FxFreeDeleter> m_pPalette;
 };
 
 class CFX_DIBitmap : public CFX_DIBSource {
diff --git a/core/fxge/include/fx_ge_win32.h b/core/fxge/include/fx_ge_win32.h
index 5b57863..e53ee2e 100644
--- a/core/fxge/include/fx_ge_win32.h
+++ b/core/fxge/include/fx_ge_win32.h
@@ -44,10 +44,6 @@
   static CFX_ByteString GetBitmapInfo(const CFX_DIBitmap* pBitmap);
   static CFX_DIBitmap* LoadFromBuf(BITMAPINFO* pbmi, void* pData);
   static HBITMAP GetDDBitmap(const CFX_DIBitmap* pBitmap, HDC hDC);
-  static CFX_DIBitmap* LoadFromDDB(HDC hDC,
-                                   HBITMAP hBitmap,
-                                   uint32_t* pPalette = nullptr,
-                                   uint32_t size = 256);
   static CFX_DIBitmap* LoadFromFile(const FX_WCHAR* filename);
   static CFX_DIBitmap* LoadFromFile(const FX_CHAR* filename);
   static CFX_DIBitmap* LoadDIBitmap(WINDIB_Open_Args_ args);
diff --git a/core/fxge/win32/fx_win32_dib.cpp b/core/fxge/win32/fx_win32_dib.cpp
index 0e79399..ae28a34 100644
--- a/core/fxge/win32/fx_win32_dib.cpp
+++ b/core/fxge/win32/fx_win32_dib.cpp
@@ -189,88 +189,6 @@
   return pDIBitmap;
 }
 
-CFX_DIBitmap* CFX_WindowsDIB::LoadFromDDB(HDC hDC,
-                                          HBITMAP hBitmap,
-                                          uint32_t* pPalette,
-                                          uint32_t palsize) {
-  FX_BOOL bCreatedDC = !hDC;
-  if (bCreatedDC) {
-    hDC = CreateCompatibleDC(nullptr);
-  }
-  BITMAPINFOHEADER bmih;
-  FXSYS_memset(&bmih, 0, sizeof bmih);
-  bmih.biSize = sizeof bmih;
-  GetDIBits(hDC, hBitmap, 0, 0, nullptr, (BITMAPINFO*)&bmih, DIB_RGB_COLORS);
-  int width = bmih.biWidth;
-  int height = abs(bmih.biHeight);
-  bmih.biHeight = -height;
-  bmih.biCompression = BI_RGB;
-  CFX_DIBitmap* pDIBitmap = new CFX_DIBitmap;
-  int ret = 0;
-  if (bmih.biBitCount == 1 || bmih.biBitCount == 8) {
-    int size = sizeof(BITMAPINFOHEADER) + 8;
-    if (bmih.biBitCount == 8) {
-      size += sizeof(uint32_t) * 254;
-    }
-    BITMAPINFO* pbmih = (BITMAPINFO*)FX_Alloc(uint8_t, size);
-    pbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
-    pbmih->bmiHeader.biBitCount = bmih.biBitCount;
-    pbmih->bmiHeader.biCompression = BI_RGB;
-    pbmih->bmiHeader.biHeight = -height;
-    pbmih->bmiHeader.biPlanes = 1;
-    pbmih->bmiHeader.biWidth = bmih.biWidth;
-    if (!pDIBitmap->Create(bmih.biWidth, height, bmih.biBitCount == 1
-                                                     ? FXDIB_1bppRgb
-                                                     : FXDIB_8bppRgb)) {
-      delete pDIBitmap;
-      FX_Free(pbmih);
-      if (bCreatedDC) {
-        DeleteDC(hDC);
-      }
-      return nullptr;
-    }
-    ret = GetDIBits(hDC, hBitmap, 0, height, pDIBitmap->GetBuffer(),
-                    (BITMAPINFO*)pbmih, DIB_RGB_COLORS);
-    FX_Free(pbmih);
-    pbmih = nullptr;
-    pDIBitmap->CopyPalette(pPalette, palsize);
-  } else {
-    if (bmih.biBitCount <= 24) {
-      bmih.biBitCount = 24;
-    } else {
-      bmih.biBitCount = 32;
-    }
-    if (!pDIBitmap->Create(bmih.biWidth, height,
-                           bmih.biBitCount == 24 ? FXDIB_Rgb : FXDIB_Rgb32)) {
-      delete pDIBitmap;
-      if (bCreatedDC) {
-        DeleteDC(hDC);
-      }
-      return nullptr;
-    }
-    ret = GetDIBits(hDC, hBitmap, 0, height, pDIBitmap->GetBuffer(),
-                    (BITMAPINFO*)&bmih, DIB_RGB_COLORS);
-    if (ret != 0 && bmih.biBitCount == 32) {
-      int pitch = pDIBitmap->GetPitch();
-      for (int row = 0; row < height; row++) {
-        uint8_t* dest_scan = (uint8_t*)(pDIBitmap->GetBuffer() + row * pitch);
-        for (int col = 0; col < width; col++) {
-          dest_scan[3] = 255;
-          dest_scan += 4;
-        }
-      }
-    }
-  }
-  if (ret == 0) {
-    delete pDIBitmap;
-    pDIBitmap = nullptr;
-  }
-  if (bCreatedDC) {
-    DeleteDC(hDC);
-  }
-  return pDIBitmap;
-}
-
 CFX_WindowsDIB::CFX_WindowsDIB(HDC hDC, int width, int height) {
   Create(width, height, FXDIB_Rgb, (uint8_t*)1);
   BITMAPINFOHEADER bmih;