• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/include/fxcodec/fx_codec.h"
8 #include "core/include/fxge/fx_dib.h"
9 #include "core/include/fxge/fx_ge.h"
10 
11 const FX_DWORD g_dwWinPalette[256] = {
12     0xff000000, 0xff800000, 0xff008000, 0xff808000, 0xff000080, 0xff800080,
13     0xff008080, 0xff808080, 0xffC0DCC0, 0xffA6CAF0, 0xff2A3FAA, 0xff2A3FFF,
14     0xff2A5F00, 0xff2A5F55, 0xff2A5FAA, 0xff2A5FFF, 0xff2A7F00, 0xff2A7F55,
15     0xff2A7FAA, 0xff2A7FFF, 0xff2A9F00, 0xff2A9F55, 0xff2A9FAA, 0xff2A9FFF,
16     0xff2ABF00, 0xff2ABF55, 0xff2ABFAA, 0xff2ABFFF, 0xff2ADF00, 0xff2ADF55,
17     0xff2ADFAA, 0xff2ADFFF, 0xff2AFF00, 0xff2AFF55, 0xff2AFFAA, 0xff2AFFFF,
18     0xff550000, 0xff550055, 0xff5500AA, 0xff5500FF, 0xff551F00, 0xff551F55,
19     0xff551FAA, 0xff551FFF, 0xff553F00, 0xff553F55, 0xff553FAA, 0xff553FFF,
20     0xff555F00, 0xff555F55, 0xff555FAA, 0xff555FFF, 0xff557F00, 0xff557F55,
21     0xff557FAA, 0xff557FFF, 0xff559F00, 0xff559F55, 0xff559FAA, 0xff559FFF,
22     0xff55BF00, 0xff55BF55, 0xff55BFAA, 0xff55BFFF, 0xff55DF00, 0xff55DF55,
23     0xff55DFAA, 0xff55DFFF, 0xff55FF00, 0xff55FF55, 0xff55FFAA, 0xff55FFFF,
24     0xff7F0000, 0xff7F0055, 0xff7F00AA, 0xff7F00FF, 0xff7F1F00, 0xff7F1F55,
25     0xff7F1FAA, 0xff7F1FFF, 0xff7F3F00, 0xff7F3F55, 0xff7F3FAA, 0xff7F3FFF,
26     0xff7F5F00, 0xff7F5F55, 0xff7F5FAA, 0xff7F5FFF, 0xff7F7F00, 0xff7F7F55,
27     0xff7F7FAA, 0xff7F7FFF, 0xff7F9F00, 0xff7F9F55, 0xff7F9FAA, 0xff7F9FFF,
28     0xff7FBF00, 0xff7FBF55, 0xff7FBFAA, 0xff7FBFFF, 0xff7FDF00, 0xff7FDF55,
29     0xff7FDFAA, 0xff7FDFFF, 0xff00FF7F, 0xff7FFF55, 0xff7FFFAA, 0xff7FFFFF,
30     0xffAA0000, 0xffAA0055, 0xffAA00AA, 0xffAA00FF, 0xffAA1F00, 0xffAA1F55,
31     0xffAA1FAA, 0xffAA1FFF, 0xffAA3F00, 0xffAA3F55, 0xffAA3FAA, 0xffAA3FFF,
32     0xffAA5F00, 0xffAA5F55, 0xffAA5FAA, 0xffAA5FFF, 0xffAA7F00, 0xffAA7F55,
33     0xffAA7FAA, 0xffAA7FFF, 0xffAA9F00, 0xffAA9F55, 0xffAA9FAA, 0xffAA9FFF,
34     0xffAABF00, 0xffAABF55, 0xffAABFAA, 0xffAABFFF, 0xffAADF00, 0xffAADF55,
35     0xffAADFAA, 0xffAADFFF, 0xffAAFF00, 0xffAAFF55, 0xffAAFFAA, 0xffAAFFFF,
36     0xffD40000, 0xffD40055, 0xffD400AA, 0xffD400FF, 0xffD41F00, 0xffD41F55,
37     0xffD41FAA, 0xffD41FFF, 0xffD43F00, 0xffD43F55, 0xffD43FAA, 0xffD43FFF,
38     0xffD45F00, 0xffD45F55, 0xffD45FAA, 0xffD45FFF, 0xffD47F00, 0xffD47F55,
39     0xffD47FAA, 0xffD4F7FF, 0xffD49F00, 0xffD49F55, 0xffD49FAA, 0xffD49FFF,
40     0xffD4BF00, 0xffD4BF55, 0xffD4BFAA, 0xffD4BFFF, 0xffD4DF00, 0xffD4DF55,
41     0xffD4DFAA, 0xffD4DFFF, 0xffD4FF00, 0xffD4FF55, 0xffD4FFAA, 0xffD4FFFF,
42     0xffFF0055, 0xffFF00AA, 0xffFF1F00, 0xffFF1F55, 0xffFF1FAA, 0xffFF1FFF,
43     0xffFF3F00, 0xffFF3F55, 0xffFF3FAA, 0xffFF3FFF, 0xffFF5F00, 0xffFF5F55,
44     0xffFF5FAA, 0xffFF5FFF, 0xffFF7F00, 0xffFF7F55, 0xffFF7FAA, 0xffFF7FFF,
45     0xffFF9F00, 0xffFF9F55, 0xffFF9FAA, 0xffFF9FFF, 0xffFFBF00, 0xffFFBF55,
46     0xffFFBFAA, 0xffFFBFFF, 0xffFFDF00, 0xffFFDF55, 0xffFFDFAA, 0xffFFDFFF,
47     0xffFFFF55, 0xffFFFFAA, 0xffCCCCFF, 0xffFFCCFF, 0xff33FFFF, 0xff66FFFF,
48     0xff99FFFF, 0xffCCFFFF, 0xff007F00, 0xff007F55, 0xff007FAA, 0xff007FFF,
49     0xff009F00, 0xff009F55, 0xff009FAA, 0xff009FFF, 0xff00BF00, 0xff00BF55,
50     0xff00BFAA, 0xff00BFFF, 0xff00DF00, 0xff00DF55, 0xff00DFAA, 0xff00DFFF,
51     0xff00FF55, 0xff00FFAA, 0xff2A0000, 0xff2A0055, 0xff2A00AA, 0xff2A00FF,
52     0xff2A1F00, 0xff2A1F55, 0xff2A1FAA, 0xff2A1FFF, 0xff2A3F00, 0xff2A3F55,
53     0xffFFFBF0, 0xffA0A0A4, 0xff808080, 0xffFF0000, 0xff00FF00, 0xffFF0000,
54     0xff0000FF, 0xffFF00FF, 0xff00FFFF, 0xffFFFFFF};
55 const FX_DWORD g_dwMacPalette[256] = {
56     0xffFFFFFF, 0xffFFFFCC, 0xffFFFF99, 0xffFFFF66, 0xffFFFF33, 0xffFFFF00,
57     0xffFFCCFF, 0xffFFCCCC, 0xffFFCC99, 0xffFFCC66, 0xffFFCC33, 0xffFFCC00,
58     0xffFF99FF, 0xffFF99CC, 0xffFF9999, 0xffFF9966, 0xffFF9933, 0xffFF9900,
59     0xffFF66FF, 0xffFF66CC, 0xffFF6699, 0xffFF6666, 0xffFF6633, 0xffFF6600,
60     0xffFF33FF, 0xffFF33CC, 0xffFF3399, 0xffFF3366, 0xffFF3333, 0xffFF3300,
61     0xffFF00FF, 0xffFF00CC, 0xffFF0099, 0xffFF0066, 0xffFF0033, 0xffFF0000,
62     0xffCCFFFF, 0xffCCFFCC, 0xffCCFF99, 0xffCCFF66, 0xffCCFF33, 0xffCCFF00,
63     0xffCCCCFF, 0xffCCCCCC, 0xffCCCC99, 0xffCCCC66, 0xffCCCC33, 0xffCCCC00,
64     0xffCC99FF, 0xffCC99CC, 0xffCC9999, 0xffCC9966, 0xffCC9933, 0xffCC9900,
65     0xffCC66FF, 0xffCC66CC, 0xffCC6699, 0xffCC6666, 0xffCC6633, 0xffCC6600,
66     0xffCC33FF, 0xffCC33CC, 0xffCC3399, 0xffCC3366, 0xffCC3333, 0xffCC3300,
67     0xffCC00FF, 0xffCC00CC, 0xffCC0099, 0xffCC0066, 0xffCC0033, 0xffCC0000,
68     0xff99FFFF, 0xff99FFCC, 0xff99FF99, 0xff99FF66, 0xff99FF33, 0xff99FF00,
69     0xff99CCFF, 0xff99CCCC, 0xff99CC99, 0xff99CC66, 0xff99CC33, 0xff99CC00,
70     0xff9999FF, 0xff9999CC, 0xff999999, 0xff999966, 0xff999933, 0xff999900,
71     0xff9966FF, 0xff9966CC, 0xff996699, 0xff996666, 0xff996633, 0xff996600,
72     0xff9933FF, 0xff9933CC, 0xff993399, 0xff993366, 0xff993333, 0xff993300,
73     0xff9900FF, 0xff9900CC, 0xff990099, 0xff990066, 0xff990033, 0xff990000,
74     0xff66FFFF, 0xff66FFCC, 0xff66FF99, 0xff66FF66, 0xff66FF33, 0xff66FF00,
75     0xff66CCFF, 0xff66CCCC, 0xff66CC99, 0xff66CC66, 0xff66CC33, 0xff66CC00,
76     0xff6699FF, 0xff6699CC, 0xff669999, 0xff669966, 0xff669933, 0xff669900,
77     0xff6666FF, 0xff6666CC, 0xff666699, 0xff666666, 0xff666633, 0xff666600,
78     0xff6633FF, 0xff6633CC, 0xff663399, 0xff663366, 0xff663333, 0xff663300,
79     0xff6600FF, 0xff6600CC, 0xff660099, 0xff660066, 0xff660033, 0xff660000,
80     0xff33FFFF, 0xff33FFCC, 0xff33FF99, 0xff33FF66, 0xff33FF33, 0xff33FF00,
81     0xff33CCFF, 0xff33CCCC, 0xff33CC99, 0xff33CC66, 0xff33CC33, 0xff33CC00,
82     0xff3399FF, 0xff3399CC, 0xff339999, 0xff339966, 0xff339933, 0xff339900,
83     0xff3366FF, 0xff3366CC, 0xff336699, 0xff336666, 0xff336633, 0xff336600,
84     0xff3333FF, 0xff3333CC, 0xff333399, 0xff333366, 0xff333333, 0xff333300,
85     0xff3300FF, 0xff3300CC, 0xff330099, 0xff330066, 0xff330033, 0xff330000,
86     0xff00FFFF, 0xff00FFCC, 0xff00FF99, 0xff00FF66, 0xff00FF33, 0xff00FF00,
87     0xff00CCFF, 0xff00CCCC, 0xff00CC99, 0xff00CC66, 0xff00CC33, 0xff00CC00,
88     0xff0099FF, 0xff0099CC, 0xff009999, 0xff009966, 0xff009933, 0xff009900,
89     0xff0066FF, 0xff0066CC, 0xff006699, 0xff006666, 0xff006633, 0xff006600,
90     0xff0033FF, 0xff0033CC, 0xff003399, 0xff003366, 0xff003333, 0xff003300,
91     0xff0000FF, 0xff0000CC, 0xff000099, 0xff000066, 0xff000033, 0xffEE0000,
92     0xffDD0000, 0xffBB0000, 0xffAA0000, 0xff880000, 0xff770000, 0xff550000,
93     0xff440000, 0xff220000, 0xff110000, 0xff00EE00, 0xff00DD00, 0xff00BB00,
94     0xff00AA00, 0xff008800, 0xff007700, 0xff005500, 0xff004400, 0xff002200,
95     0xff001100, 0xff0000EE, 0xff0000DD, 0xff0000BB, 0xff0000AA, 0xff000088,
96     0xff000077, 0xff000055, 0xff000044, 0xff000022, 0xff000011, 0xffEEEEEE,
97     0xffDDDDDD, 0xffBBBBBB, 0xffAAAAAA, 0xff888888, 0xff777777, 0xff555555,
98     0xff444444, 0xff222222, 0xff111111, 0xff000000};
99 class CFX_Palette {
100  public:
101   CFX_Palette();
102   ~CFX_Palette();
103 
104  public:
105   FX_BOOL BuildPalette(const CFX_DIBSource* pBitmap, int dwPaletteType);
GetPalette() const106   FX_DWORD* GetPalette() const { return m_pPalette; }
107 
GetColorLut() const108   FX_DWORD* GetColorLut() const { return m_cLut; }
GetAmountLut() const109   FX_DWORD* GetAmountLut() const { return m_aLut; }
Getlut() const110   int32_t Getlut() const { return m_lut; }
111 
112  protected:
113   FX_DWORD* m_pPalette;
114   FX_DWORD* m_cLut;
115   FX_DWORD* m_aLut;
116   int m_lut;
117 };
_Partition(FX_DWORD * alut,FX_DWORD * clut,int l,int r)118 int _Partition(FX_DWORD* alut, FX_DWORD* clut, int l, int r) {
119   FX_DWORD p_a = alut[l];
120   FX_DWORD p_c = clut[l];
121   while (l < r) {
122     while (l < r && alut[r] >= p_a) {
123       r--;
124     }
125     if (l < r) {
126       alut[l] = alut[r];
127       clut[l++] = clut[r];
128     }
129     while (l < r && alut[l] <= p_a) {
130       l++;
131     }
132     if (l < r) {
133       alut[r] = alut[l];
134       clut[r--] = clut[l];
135     }
136   }
137   alut[l] = p_a;
138   clut[l] = p_c;
139   return l;
140 }
_Qsort(FX_DWORD * alut,FX_DWORD * clut,int l,int r)141 void _Qsort(FX_DWORD* alut, FX_DWORD* clut, int l, int r) {
142   if (l < r) {
143     int pI = _Partition(alut, clut, l, r);
144     _Qsort(alut, clut, l, pI - 1);
145     _Qsort(alut, clut, pI + 1, r);
146   }
147 }
_ColorDecode(FX_DWORD pal_v,uint8_t & r,uint8_t & g,uint8_t & b)148 void _ColorDecode(FX_DWORD pal_v, uint8_t& r, uint8_t& g, uint8_t& b) {
149   r = (uint8_t)((pal_v & 0xf00) >> 4);
150   g = (uint8_t)(pal_v & 0x0f0);
151   b = (uint8_t)((pal_v & 0x00f) << 4);
152 }
_Obtain_Pal(FX_DWORD * aLut,FX_DWORD * cLut,FX_DWORD * dest_pal,int pal_type,FX_DWORD * win_mac_pal,FX_DWORD lut)153 void _Obtain_Pal(FX_DWORD* aLut,
154                  FX_DWORD* cLut,
155                  FX_DWORD* dest_pal,
156                  int pal_type,
157                  FX_DWORD* win_mac_pal,
158                  FX_DWORD lut) {
159   int row, col;
160   FX_DWORD lut_1 = lut - 1;
161   if (pal_type == FXDIB_PALETTE_LOC) {
162     for (row = 0; row < 256; row++) {
163       int lut_offset = lut_1 - row;
164       if (lut_offset < 0) {
165         lut_offset += 256;
166       }
167       FX_DWORD color = cLut[lut_offset];
168       uint8_t r, g, b;
169       _ColorDecode(color, r, g, b);
170       dest_pal[row] = ((FX_DWORD)r << 16) | ((FX_DWORD)g << 8) | b | 0xff000000;
171       aLut[lut_offset] = row;
172     }
173   } else {
174     for (row = 0; row < 256; row++) {
175       int lut_offset = lut_1 - row;
176       if (lut_offset < 0) {
177         lut_offset += 256;
178       }
179       uint8_t r, g, b;
180       _ColorDecode(cLut[lut_offset], r, g, b);
181       int error, min_error = 1000000;
182       int c_index = 0;
183       for (col = 0; col < 256; col++) {
184         FX_DWORD p_color = win_mac_pal[col];
185         int d_r = r - (uint8_t)(p_color >> 16);
186         int d_g = g - (uint8_t)(p_color >> 8);
187         int d_b = b - (uint8_t)p_color;
188         error = d_r * d_r + d_g * d_g + d_b * d_b;
189         if (error < min_error) {
190           min_error = error;
191           c_index = col;
192         }
193       }
194       dest_pal[row] = win_mac_pal[c_index];
195       aLut[lut_offset] = row;
196     }
197   }
198 }
CFX_Palette()199 CFX_Palette::CFX_Palette() {
200   m_pPalette = NULL;
201   m_cLut = NULL;
202   m_aLut = NULL;
203   m_lut = 0;
204 }
~CFX_Palette()205 CFX_Palette::~CFX_Palette() {
206   FX_Free(m_pPalette);
207   FX_Free(m_cLut);
208   FX_Free(m_aLut);
209   m_lut = 0;
210 }
BuildPalette(const CFX_DIBSource * pBitmap,int pal_type)211 FX_BOOL CFX_Palette::BuildPalette(const CFX_DIBSource* pBitmap, int pal_type) {
212   if (!pBitmap) {
213     return FALSE;
214   }
215   FX_Free(m_pPalette);
216   m_pPalette = FX_Alloc(FX_DWORD, 256);
217   int bpp = pBitmap->GetBPP() / 8;
218   int width = pBitmap->GetWidth();
219   int height = pBitmap->GetHeight();
220   FX_Free(m_cLut);
221   m_cLut = NULL;
222   FX_Free(m_aLut);
223   m_aLut = NULL;
224   m_cLut = FX_Alloc(FX_DWORD, 4096);
225   m_aLut = FX_Alloc(FX_DWORD, 4096);
226   int row, col;
227   m_lut = 0;
228   for (row = 0; row < height; row++) {
229     uint8_t* scan_line = (uint8_t*)pBitmap->GetScanline(row);
230     for (col = 0; col < width; col++) {
231       uint8_t* src_port = scan_line + col * bpp;
232       FX_DWORD b = src_port[0] & 0xf0;
233       FX_DWORD g = src_port[1] & 0xf0;
234       FX_DWORD r = src_port[2] & 0xf0;
235       FX_DWORD index = (r << 4) + g + (b >> 4);
236       m_aLut[index]++;
237     }
238   }
239   for (row = 0; row < 4096; row++) {
240     if (m_aLut[row] != 0) {
241       m_aLut[m_lut] = m_aLut[row];
242       m_cLut[m_lut] = row;
243       m_lut++;
244     }
245   }
246   _Qsort(m_aLut, m_cLut, 0, m_lut - 1);
247   FX_DWORD* win_mac_pal = NULL;
248   if (pal_type == FXDIB_PALETTE_WIN) {
249     win_mac_pal = (FX_DWORD*)g_dwWinPalette;
250   } else if (pal_type == FXDIB_PALETTE_MAC) {
251     win_mac_pal = (FX_DWORD*)g_dwMacPalette;
252   }
253   _Obtain_Pal(m_aLut, m_cLut, m_pPalette, pal_type, win_mac_pal, m_lut);
254   return TRUE;
255 }
_ConvertBuffer_1bppMask2Gray(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)256 FX_BOOL _ConvertBuffer_1bppMask2Gray(uint8_t* dest_buf,
257                                      int dest_pitch,
258                                      int width,
259                                      int height,
260                                      const CFX_DIBSource* pSrcBitmap,
261                                      int src_left,
262                                      int src_top) {
263   uint8_t set_gray, reset_gray;
264   set_gray = 0xff;
265   reset_gray = 0x00;
266   for (int row = 0; row < height; row++) {
267     uint8_t* dest_scan = dest_buf + row * dest_pitch;
268     FXSYS_memset(dest_scan, reset_gray, width);
269     const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row);
270     for (int col = src_left; col < src_left + width; col++) {
271       if (src_scan[col / 8] & (1 << (7 - col % 8))) {
272         *dest_scan = set_gray;
273       }
274       dest_scan++;
275     }
276   }
277   return TRUE;
278 }
_ConvertBuffer_8bppMask2Gray(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)279 FX_BOOL _ConvertBuffer_8bppMask2Gray(uint8_t* dest_buf,
280                                      int dest_pitch,
281                                      int width,
282                                      int height,
283                                      const CFX_DIBSource* pSrcBitmap,
284                                      int src_left,
285                                      int src_top) {
286   for (int row = 0; row < height; row++) {
287     uint8_t* dest_scan = dest_buf + row * dest_pitch;
288     const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
289     FXSYS_memcpy(dest_scan, src_scan, width);
290   }
291   return TRUE;
292 }
_ConvertBuffer_1bppPlt2Gray(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)293 FX_BOOL _ConvertBuffer_1bppPlt2Gray(uint8_t* dest_buf,
294                                     int dest_pitch,
295                                     int width,
296                                     int height,
297                                     const CFX_DIBSource* pSrcBitmap,
298                                     int src_left,
299                                     int src_top,
300                                     void* pIccTransform) {
301   FX_DWORD* src_plt = pSrcBitmap->GetPalette();
302   uint8_t gray[2];
303   if (pIccTransform) {
304     FX_DWORD plt[2];
305     if (pSrcBitmap->IsCmykImage()) {
306       plt[0] = FXCMYK_TODIB(src_plt[0]);
307       plt[1] = FXCMYK_TODIB(src_plt[1]);
308     } else {
309       uint8_t* bgr_ptr = (uint8_t*)plt;
310       bgr_ptr[0] = FXARGB_B(src_plt[0]);
311       bgr_ptr[1] = FXARGB_G(src_plt[0]);
312       bgr_ptr[2] = FXARGB_R(src_plt[0]);
313       bgr_ptr[3] = FXARGB_B(src_plt[1]);
314       bgr_ptr[4] = FXARGB_G(src_plt[1]);
315       bgr_ptr[5] = FXARGB_R(src_plt[1]);
316     }
317     ICodec_IccModule* pIccModule =
318         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
319     pIccModule->TranslateScanline(pIccTransform, gray, (const uint8_t*)plt, 2);
320   } else {
321     uint8_t reset_r, reset_g, reset_b, set_r, set_g, set_b;
322     if (pSrcBitmap->IsCmykImage()) {
323       AdobeCMYK_to_sRGB1(
324           FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]),
325           FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0]), reset_r,
326           reset_g, reset_b);
327       AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[1]),
328                          FXSYS_GetMValue(src_plt[1]),
329                          FXSYS_GetYValue(src_plt[1]),
330                          FXSYS_GetKValue(src_plt[1]), set_r, set_g, set_b);
331     } else {
332       reset_r = FXARGB_R(src_plt[0]);
333       reset_g = FXARGB_G(src_plt[0]);
334       reset_b = FXARGB_B(src_plt[0]);
335       set_r = FXARGB_R(src_plt[1]);
336       set_g = FXARGB_G(src_plt[1]);
337       set_b = FXARGB_B(src_plt[1]);
338     }
339     gray[0] = FXRGB2GRAY(reset_r, reset_g, reset_b);
340     gray[1] = FXRGB2GRAY(set_r, set_g, set_b);
341   }
342   for (int row = 0; row < height; row++) {
343     uint8_t* dest_scan = dest_buf + row * dest_pitch;
344     FXSYS_memset(dest_scan, gray[0], width);
345     const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row);
346     for (int col = src_left; col < src_left + width; col++) {
347       if (src_scan[col / 8] & (1 << (7 - col % 8))) {
348         *dest_scan = gray[1];
349       }
350       dest_scan++;
351     }
352   }
353   return TRUE;
354 }
_ConvertBuffer_8bppPlt2Gray(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)355 FX_BOOL _ConvertBuffer_8bppPlt2Gray(uint8_t* dest_buf,
356                                     int dest_pitch,
357                                     int width,
358                                     int height,
359                                     const CFX_DIBSource* pSrcBitmap,
360                                     int src_left,
361                                     int src_top,
362                                     void* pIccTransform) {
363   FX_DWORD* src_plt = pSrcBitmap->GetPalette();
364   uint8_t gray[256];
365   if (pIccTransform) {
366     FX_DWORD plt[256];
367     if (pSrcBitmap->IsCmykImage()) {
368       for (int i = 0; i < 256; i++) {
369         plt[i] = FXCMYK_TODIB(src_plt[i]);
370       }
371     } else {
372       uint8_t* bgr_ptr = (uint8_t*)plt;
373       for (int i = 0; i < 256; i++) {
374         *bgr_ptr++ = FXARGB_B(src_plt[i]);
375         *bgr_ptr++ = FXARGB_G(src_plt[i]);
376         *bgr_ptr++ = FXARGB_R(src_plt[i]);
377       }
378     }
379     ICodec_IccModule* pIccModule =
380         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
381     pIccModule->TranslateScanline(pIccTransform, gray, (const uint8_t*)plt,
382                                   256);
383   } else {
384     if (pSrcBitmap->IsCmykImage()) {
385       uint8_t r, g, b;
386       for (int i = 0; i < 256; i++) {
387         AdobeCMYK_to_sRGB1(
388             FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]),
389             FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]), r, g, b);
390         gray[i] = FXRGB2GRAY(r, g, b);
391       }
392     } else
393       for (int i = 0; i < 256; i++) {
394         gray[i] = FXRGB2GRAY(FXARGB_R(src_plt[i]), FXARGB_G(src_plt[i]),
395                              FXARGB_B(src_plt[i]));
396       }
397   }
398   for (int row = 0; row < height; row++) {
399     uint8_t* dest_scan = dest_buf + row * dest_pitch;
400     const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
401     for (int col = 0; col < width; col++) {
402       *dest_scan++ = gray[*src_scan++];
403     }
404   }
405   return TRUE;
406 }
_ConvertBuffer_RgbOrCmyk2Gray(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)407 FX_BOOL _ConvertBuffer_RgbOrCmyk2Gray(uint8_t* dest_buf,
408                                       int dest_pitch,
409                                       int width,
410                                       int height,
411                                       const CFX_DIBSource* pSrcBitmap,
412                                       int src_left,
413                                       int src_top,
414                                       void* pIccTransform) {
415   int Bpp = pSrcBitmap->GetBPP() / 8;
416   if (pIccTransform) {
417     ICodec_IccModule* pIccModule =
418         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
419     if (Bpp == 3 || pSrcBitmap->IsCmykImage()) {
420       for (int row = 0; row < height; row++) {
421         uint8_t* dest_scan = dest_buf + row * dest_pitch;
422         const uint8_t* src_scan =
423             pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
424         pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan,
425                                       width);
426       }
427     } else {
428       for (int row = 0; row < height; row++) {
429         uint8_t* dest_scan = dest_buf + row * dest_pitch;
430         const uint8_t* src_scan =
431             pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
432         for (int col = 0; col < width; col++) {
433           pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
434           dest_scan++;
435           src_scan += 4;
436         }
437       }
438     }
439   } else {
440     if (pSrcBitmap->IsCmykImage()) {
441       for (int row = 0; row < height; row++) {
442         uint8_t* dest_scan = dest_buf + row * dest_pitch;
443         const uint8_t* src_scan =
444             pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
445         for (int col = 0; col < width; col++) {
446           uint8_t r, g, b;
447           AdobeCMYK_to_sRGB1(FXSYS_GetCValue((FX_DWORD)src_scan[0]),
448                              FXSYS_GetMValue((FX_DWORD)src_scan[1]),
449                              FXSYS_GetYValue((FX_DWORD)src_scan[2]),
450                              FXSYS_GetKValue((FX_DWORD)src_scan[3]), r, g, b);
451           *dest_scan++ = FXRGB2GRAY(r, g, b);
452           src_scan += 4;
453         }
454       }
455     } else {
456       for (int row = 0; row < height; row++) {
457         uint8_t* dest_scan = dest_buf + row * dest_pitch;
458         const uint8_t* src_scan =
459             pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
460         for (int col = 0; col < width; col++) {
461           *dest_scan++ = FXRGB2GRAY(src_scan[2], src_scan[1], src_scan[0]);
462           src_scan += Bpp;
463         }
464       }
465     }
466   }
467   return TRUE;
468 }
_ConvertBuffer_IndexCopy(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)469 inline void _ConvertBuffer_IndexCopy(uint8_t* dest_buf,
470                                      int dest_pitch,
471                                      int width,
472                                      int height,
473                                      const CFX_DIBSource* pSrcBitmap,
474                                      int src_left,
475                                      int src_top) {
476   if (pSrcBitmap->GetBPP() == 1) {
477     for (int row = 0; row < height; row++) {
478       uint8_t* dest_scan = dest_buf + row * dest_pitch;
479       FXSYS_memset(dest_scan, 0, width);
480       const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row);
481       for (int col = src_left; col < src_left + width; col++) {
482         if (src_scan[col / 8] & (1 << (7 - col % 8))) {
483           *dest_scan = 1;
484         }
485         dest_scan++;
486       }
487     }
488   } else {
489     for (int row = 0; row < height; row++) {
490       uint8_t* dest_scan = dest_buf + row * dest_pitch;
491       const uint8_t* src_scan =
492           pSrcBitmap->GetScanline(src_top + row) + src_left;
493       FXSYS_memcpy(dest_scan, src_scan, width);
494     }
495   }
496 }
_ConvertBuffer_Plt2PltRgb8(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,FX_DWORD * dst_plt,void * pIccTransform)497 FX_BOOL _ConvertBuffer_Plt2PltRgb8(uint8_t* dest_buf,
498                                    int dest_pitch,
499                                    int width,
500                                    int height,
501                                    const CFX_DIBSource* pSrcBitmap,
502                                    int src_left,
503                                    int src_top,
504                                    FX_DWORD* dst_plt,
505                                    void* pIccTransform) {
506   _ConvertBuffer_IndexCopy(dest_buf, dest_pitch, width, height, pSrcBitmap,
507                            src_left, src_top);
508   FX_DWORD* src_plt = pSrcBitmap->GetPalette();
509   int plt_size = pSrcBitmap->GetPaletteSize();
510   if (pIccTransform) {
511     FX_DWORD plt[256];
512     uint8_t* bgr_ptr = (uint8_t*)plt;
513     if (pSrcBitmap->IsCmykImage()) {
514       for (int i = 0; i < plt_size; i++) {
515         plt[i] = FXCMYK_TODIB(src_plt[i]);
516       }
517     } else {
518       for (int i = 0; i < plt_size; i++) {
519         *bgr_ptr++ = FXARGB_B(src_plt[i]);
520         *bgr_ptr++ = FXARGB_G(src_plt[i]);
521         *bgr_ptr++ = FXARGB_R(src_plt[i]);
522       }
523       bgr_ptr = (uint8_t*)plt;
524     }
525     ICodec_IccModule* pIccModule =
526         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
527     pIccModule->TranslateScanline(pIccTransform, (uint8_t*)plt,
528                                   (const uint8_t*)plt, plt_size);
529     for (int i = 0; i < plt_size; i++) {
530       dst_plt[i] = FXARGB_MAKE(0xff, bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
531       bgr_ptr += 3;
532     }
533   } else {
534     if (pSrcBitmap->IsCmykImage()) {
535       for (int i = 0; i < plt_size; i++) {
536         uint8_t r, g, b;
537         AdobeCMYK_to_sRGB1(
538             FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]),
539             FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]), r, g, b);
540         dst_plt[i] = FXARGB_MAKE(0xff, r, g, b);
541       }
542     } else {
543       FXSYS_memcpy(dst_plt, src_plt, plt_size * 4);
544     }
545   }
546   return TRUE;
547 }
_ConvertBuffer_Rgb2PltRgb8_NoTransform(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,FX_DWORD * dst_plt)548 inline FX_BOOL _ConvertBuffer_Rgb2PltRgb8_NoTransform(
549     uint8_t* dest_buf,
550     int dest_pitch,
551     int width,
552     int height,
553     const CFX_DIBSource* pSrcBitmap,
554     int src_left,
555     int src_top,
556     FX_DWORD* dst_plt) {
557   int bpp = pSrcBitmap->GetBPP() / 8;
558   int row, col;
559   CFX_Palette palette;
560   palette.BuildPalette(pSrcBitmap, FXDIB_PALETTE_LOC);
561   FX_DWORD* cLut = palette.GetColorLut();
562   FX_DWORD* aLut = palette.GetAmountLut();
563   if (!cLut || !aLut) {
564     return FALSE;
565   }
566   int lut = palette.Getlut();
567   FX_DWORD* pPalette = palette.GetPalette();
568   if (lut > 256) {
569     int err, min_err;
570     int lut_256 = lut - 256;
571     for (row = 0; row < lut_256; row++) {
572       min_err = 1000000;
573       uint8_t r, g, b;
574       _ColorDecode(cLut[row], r, g, b);
575       int clrindex = 0;
576       for (int col = 0; col < 256; col++) {
577         FX_DWORD p_color = *(pPalette + col);
578         int d_r = r - (uint8_t)(p_color >> 16);
579         int d_g = g - (uint8_t)(p_color >> 8);
580         int d_b = b - (uint8_t)(p_color);
581         err = d_r * d_r + d_g * d_g + d_b * d_b;
582         if (err < min_err) {
583           min_err = err;
584           clrindex = col;
585         }
586       }
587       aLut[row] = clrindex;
588     }
589   }
590   int32_t lut_1 = lut - 1;
591   for (row = 0; row < height; row++) {
592     uint8_t* src_scan =
593         (uint8_t*)pSrcBitmap->GetScanline(src_top + row) + src_left;
594     uint8_t* dest_scan = dest_buf + row * dest_pitch;
595     for (col = 0; col < width; col++) {
596       uint8_t* src_port = src_scan + col * bpp;
597       int r = src_port[2] & 0xf0;
598       int g = src_port[1] & 0xf0;
599       int b = src_port[0] & 0xf0;
600       FX_DWORD clrindex = (r << 4) + g + (b >> 4);
601       for (int i = lut_1; i >= 0; i--)
602         if (clrindex == cLut[i]) {
603           *(dest_scan + col) = (uint8_t)(aLut[i]);
604           break;
605         }
606     }
607   }
608   FXSYS_memcpy(dst_plt, pPalette, sizeof(FX_DWORD) * 256);
609   return TRUE;
610 }
_ConvertBuffer_Rgb2PltRgb8(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,FX_DWORD * dst_plt,void * pIccTransform)611 FX_BOOL _ConvertBuffer_Rgb2PltRgb8(uint8_t* dest_buf,
612                                    int dest_pitch,
613                                    int width,
614                                    int height,
615                                    const CFX_DIBSource* pSrcBitmap,
616                                    int src_left,
617                                    int src_top,
618                                    FX_DWORD* dst_plt,
619                                    void* pIccTransform) {
620 #ifdef PDF_ENABLE_XFA
621   ICodec_IccModule* pIccModule = NULL;
622   if (pIccTransform) {
623     pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
624   }
625 #endif  // PDF_ENABLE_XFA
626   FX_BOOL ret = _ConvertBuffer_Rgb2PltRgb8_NoTransform(
627       dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top,
628       dst_plt);
629   if (ret && pIccTransform) {
630     ICodec_IccModule* pIccModule =
631         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
632     for (int i = 0; i < 256; i++) {
633       FX_ARGB* plt = dst_plt + i;
634       FX_ARGB plt_entry = FXARGB_TODIB(*plt);
635       pIccModule->TranslateScanline(pIccTransform, (uint8_t*)&plt_entry,
636                                     (const uint8_t*)&plt_entry, 1);
637       *plt = FXARGB_TODIB(plt_entry);
638     }
639   }
640   return ret;
641 }
_ConvertBuffer_1bppMask2Rgb(FXDIB_Format dst_format,uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)642 FX_BOOL _ConvertBuffer_1bppMask2Rgb(FXDIB_Format dst_format,
643                                     uint8_t* dest_buf,
644                                     int dest_pitch,
645                                     int width,
646                                     int height,
647                                     const CFX_DIBSource* pSrcBitmap,
648                                     int src_left,
649                                     int src_top) {
650   int comps = (dst_format & 0xff) / 8;
651   uint8_t set_gray, reset_gray;
652   set_gray = 0xff;
653   reset_gray = 0x00;
654   for (int row = 0; row < height; row++) {
655     uint8_t* dest_scan = dest_buf + row * dest_pitch;
656     const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row);
657     for (int col = src_left; col < src_left + width; col++) {
658       if (src_scan[col / 8] & (1 << (7 - col % 8))) {
659         dest_scan[0] = set_gray;
660         dest_scan[1] = set_gray;
661         dest_scan[2] = set_gray;
662       } else {
663         dest_scan[0] = reset_gray;
664         dest_scan[1] = reset_gray;
665         dest_scan[2] = reset_gray;
666       }
667       dest_scan += comps;
668     }
669   }
670   return TRUE;
671 }
_ConvertBuffer_8bppMask2Rgb(FXDIB_Format dst_format,uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)672 FX_BOOL _ConvertBuffer_8bppMask2Rgb(FXDIB_Format dst_format,
673                                     uint8_t* dest_buf,
674                                     int dest_pitch,
675                                     int width,
676                                     int height,
677                                     const CFX_DIBSource* pSrcBitmap,
678                                     int src_left,
679                                     int src_top) {
680   int comps = (dst_format & 0xff) / 8;
681   for (int row = 0; row < height; row++) {
682     uint8_t* dest_scan = dest_buf + row * dest_pitch;
683     const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
684     uint8_t src_pixel;
685     for (int col = 0; col < width; col++) {
686       src_pixel = *src_scan++;
687       *dest_scan++ = src_pixel;
688       *dest_scan++ = src_pixel;
689       *dest_scan = src_pixel;
690       dest_scan += comps - 2;
691     }
692   }
693   return TRUE;
694 }
_ConvertBuffer_1bppPlt2Rgb(FXDIB_Format dst_format,uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)695 FX_BOOL _ConvertBuffer_1bppPlt2Rgb(FXDIB_Format dst_format,
696                                    uint8_t* dest_buf,
697                                    int dest_pitch,
698                                    int width,
699                                    int height,
700                                    const CFX_DIBSource* pSrcBitmap,
701                                    int src_left,
702                                    int src_top,
703                                    void* pIccTransform) {
704   int comps = (dst_format & 0xff) / 8;
705   FX_DWORD* src_plt = pSrcBitmap->GetPalette();
706   FX_DWORD plt[2];
707   uint8_t* bgr_ptr = (uint8_t*)plt;
708   if (pSrcBitmap->IsCmykImage()) {
709     plt[0] = FXCMYK_TODIB(src_plt[0]);
710     plt[1] = FXCMYK_TODIB(src_plt[1]);
711   } else {
712     bgr_ptr[0] = FXARGB_B(src_plt[0]);
713     bgr_ptr[1] = FXARGB_G(src_plt[0]);
714     bgr_ptr[2] = FXARGB_R(src_plt[0]);
715     bgr_ptr[3] = FXARGB_B(src_plt[1]);
716     bgr_ptr[4] = FXARGB_G(src_plt[1]);
717     bgr_ptr[5] = FXARGB_R(src_plt[1]);
718   }
719   if (pIccTransform) {
720     ICodec_IccModule* pIccModule =
721         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
722     pIccModule->TranslateScanline(pIccTransform, (uint8_t*)plt,
723                                   (const uint8_t*)plt, 2);
724   } else {
725     if (pSrcBitmap->IsCmykImage()) {
726       AdobeCMYK_to_sRGB1(
727           FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]),
728           FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0]), bgr_ptr[2],
729           bgr_ptr[1], bgr_ptr[0]);
730       AdobeCMYK_to_sRGB1(
731           FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]),
732           FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1]), bgr_ptr[5],
733           bgr_ptr[4], bgr_ptr[3]);
734     }
735   }
736   for (int row = 0; row < height; row++) {
737     uint8_t* dest_scan = dest_buf + row * dest_pitch;
738     const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row);
739     for (int col = src_left; col < src_left + width; col++) {
740       if (src_scan[col / 8] & (1 << (7 - col % 8))) {
741         *dest_scan++ = bgr_ptr[3];
742         *dest_scan++ = bgr_ptr[4];
743         *dest_scan = bgr_ptr[5];
744       } else {
745         *dest_scan++ = bgr_ptr[0];
746         *dest_scan++ = bgr_ptr[1];
747         *dest_scan = bgr_ptr[2];
748       }
749       dest_scan += comps - 2;
750     }
751   }
752   return TRUE;
753 }
_ConvertBuffer_8bppPlt2Rgb(FXDIB_Format dst_format,uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)754 FX_BOOL _ConvertBuffer_8bppPlt2Rgb(FXDIB_Format dst_format,
755                                    uint8_t* dest_buf,
756                                    int dest_pitch,
757                                    int width,
758                                    int height,
759                                    const CFX_DIBSource* pSrcBitmap,
760                                    int src_left,
761                                    int src_top,
762                                    void* pIccTransform) {
763   int comps = (dst_format & 0xff) / 8;
764   FX_DWORD* src_plt = pSrcBitmap->GetPalette();
765   FX_DWORD plt[256];
766   uint8_t* bgr_ptr = (uint8_t*)plt;
767   if (!pSrcBitmap->IsCmykImage()) {
768     for (int i = 0; i < 256; i++) {
769       *bgr_ptr++ = FXARGB_B(src_plt[i]);
770       *bgr_ptr++ = FXARGB_G(src_plt[i]);
771       *bgr_ptr++ = FXARGB_R(src_plt[i]);
772     }
773     bgr_ptr = (uint8_t*)plt;
774   }
775   if (pIccTransform) {
776     if (pSrcBitmap->IsCmykImage()) {
777       for (int i = 0; i < 256; i++) {
778         plt[i] = FXCMYK_TODIB(src_plt[i]);
779       }
780     }
781     ICodec_IccModule* pIccModule =
782         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
783     pIccModule->TranslateScanline(pIccTransform, (uint8_t*)plt,
784                                   (const uint8_t*)plt, 256);
785   } else {
786     if (pSrcBitmap->IsCmykImage()) {
787       for (int i = 0; i < 256; i++) {
788         AdobeCMYK_to_sRGB1(
789             FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]),
790             FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]),
791             bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
792         bgr_ptr += 3;
793       }
794       bgr_ptr = (uint8_t*)plt;
795     }
796   }
797   for (int row = 0; row < height; row++) {
798     uint8_t* dest_scan = dest_buf + row * dest_pitch;
799     const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
800     for (int col = 0; col < width; col++) {
801       uint8_t* src_pixel = bgr_ptr + 3 * (*src_scan++);
802       *dest_scan++ = *src_pixel++;
803       *dest_scan++ = *src_pixel++;
804       *dest_scan = *src_pixel++;
805       dest_scan += comps - 2;
806     }
807   }
808   return TRUE;
809 }
_ConvertBuffer_24bppRgb2Rgb24(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)810 FX_BOOL _ConvertBuffer_24bppRgb2Rgb24(uint8_t* dest_buf,
811                                       int dest_pitch,
812                                       int width,
813                                       int height,
814                                       const CFX_DIBSource* pSrcBitmap,
815                                       int src_left,
816                                       int src_top,
817                                       void* pIccTransform) {
818   if (pIccTransform) {
819     ICodec_IccModule* pIccModule =
820         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
821     for (int row = 0; row < height; row++) {
822       uint8_t* dest_scan = dest_buf + row * dest_pitch;
823       const uint8_t* src_scan =
824           pSrcBitmap->GetScanline(src_top + row) + src_left * 3;
825       pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, width);
826     }
827   } else {
828     for (int row = 0; row < height; row++) {
829       uint8_t* dest_scan = dest_buf + row * dest_pitch;
830       const uint8_t* src_scan =
831           pSrcBitmap->GetScanline(src_top + row) + src_left * 3;
832       FXSYS_memcpy(dest_scan, src_scan, width * 3);
833     }
834   }
835   return TRUE;
836 }
_ConvertBuffer_32bppRgb2Rgb24(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)837 FX_BOOL _ConvertBuffer_32bppRgb2Rgb24(uint8_t* dest_buf,
838                                       int dest_pitch,
839                                       int width,
840                                       int height,
841                                       const CFX_DIBSource* pSrcBitmap,
842                                       int src_left,
843                                       int src_top,
844                                       void* pIccTransform) {
845   for (int row = 0; row < height; row++) {
846     uint8_t* dest_scan = dest_buf + row * dest_pitch;
847     const uint8_t* src_scan =
848         pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
849     for (int col = 0; col < width; col++) {
850       *dest_scan++ = *src_scan++;
851       *dest_scan++ = *src_scan++;
852       *dest_scan++ = *src_scan++;
853       src_scan++;
854     }
855   }
856   if (pIccTransform) {
857     ICodec_IccModule* pIccModule =
858         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
859     for (int row = 0; row < height; row++) {
860       uint8_t* dest_scan = dest_buf + row * dest_pitch;
861       pIccModule->TranslateScanline(pIccTransform, dest_scan, dest_scan, width);
862     }
863   }
864   return TRUE;
865 }
_ConvertBuffer_Rgb2Rgb32(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)866 FX_BOOL _ConvertBuffer_Rgb2Rgb32(uint8_t* dest_buf,
867                                  int dest_pitch,
868                                  int width,
869                                  int height,
870                                  const CFX_DIBSource* pSrcBitmap,
871                                  int src_left,
872                                  int src_top,
873                                  void* pIccTransform) {
874   int comps = pSrcBitmap->GetBPP() / 8;
875   if (pIccTransform) {
876     ICodec_IccModule* pIccModule =
877         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
878     for (int row = 0; row < height; row++) {
879       uint8_t* dest_scan = dest_buf + row * dest_pitch;
880       const uint8_t* src_scan =
881           pSrcBitmap->GetScanline(src_top + row) + src_left * comps;
882       for (int col = 0; col < width; col++) {
883         pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
884         dest_scan += 4;
885         src_scan += comps;
886       }
887     }
888   } else {
889     for (int row = 0; row < height; row++) {
890       uint8_t* dest_scan = dest_buf + row * dest_pitch;
891       const uint8_t* src_scan =
892           pSrcBitmap->GetScanline(src_top + row) + src_left * comps;
893       for (int col = 0; col < width; col++) {
894         *dest_scan++ = *src_scan++;
895         *dest_scan++ = *src_scan++;
896         *dest_scan++ = *src_scan++;
897         dest_scan++;
898         src_scan += comps - 3;
899       }
900     }
901   }
902   return TRUE;
903 }
_ConvertBuffer_32bppCmyk2Rgb32(uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)904 FX_BOOL _ConvertBuffer_32bppCmyk2Rgb32(uint8_t* dest_buf,
905                                        int dest_pitch,
906                                        int width,
907                                        int height,
908                                        const CFX_DIBSource* pSrcBitmap,
909                                        int src_left,
910                                        int src_top,
911                                        void* pIccTransform) {
912   if (pIccTransform) {
913     ICodec_IccModule* pIccModule =
914         CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
915     for (int row = 0; row < height; row++) {
916       uint8_t* dest_scan = dest_buf + row * dest_pitch;
917       const uint8_t* src_scan =
918           pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
919       for (int col = 0; col < width; col++) {
920         pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
921         dest_scan += 4;
922         src_scan += 4;
923       }
924     }
925   } else {
926     for (int row = 0; row < height; row++) {
927       uint8_t* dest_scan = dest_buf + row * dest_pitch;
928       const uint8_t* src_scan =
929           pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
930       for (int col = 0; col < width; col++) {
931         AdobeCMYK_to_sRGB1(src_scan[0], src_scan[1], src_scan[2], src_scan[3],
932                            dest_scan[2], dest_scan[1], dest_scan[0]);
933         dest_scan += 4;
934         src_scan += 4;
935       }
936     }
937   }
938   return TRUE;
939 }
ConvertBuffer(FXDIB_Format dest_format,uint8_t * dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,FX_DWORD * & d_pal,void * pIccTransform)940 FX_BOOL ConvertBuffer(FXDIB_Format dest_format,
941                       uint8_t* dest_buf,
942                       int dest_pitch,
943                       int width,
944                       int height,
945                       const CFX_DIBSource* pSrcBitmap,
946                       int src_left,
947                       int src_top,
948                       FX_DWORD*& d_pal,
949                       void* pIccTransform) {
950   FXDIB_Format src_format = pSrcBitmap->GetFormat();
951   if (!CFX_GEModule::Get()->GetCodecModule() ||
952       !CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) {
953     pIccTransform = NULL;
954   }
955   switch (dest_format) {
956     case FXDIB_Invalid:
957     case FXDIB_1bppCmyk:
958     case FXDIB_1bppMask:
959     case FXDIB_1bppRgb:
960       ASSERT(FALSE);
961       return FALSE;
962     case FXDIB_8bppMask: {
963       if ((src_format & 0xff) == 1) {
964         if (pSrcBitmap->GetPalette()) {
965           return _ConvertBuffer_1bppPlt2Gray(dest_buf, dest_pitch, width,
966                                              height, pSrcBitmap, src_left,
967                                              src_top, pIccTransform);
968         }
969         return _ConvertBuffer_1bppMask2Gray(dest_buf, dest_pitch, width, height,
970                                             pSrcBitmap, src_left, src_top);
971       }
972       if ((src_format & 0xff) == 8) {
973         if (pSrcBitmap->GetPalette()) {
974           return _ConvertBuffer_8bppPlt2Gray(dest_buf, dest_pitch, width,
975                                              height, pSrcBitmap, src_left,
976                                              src_top, pIccTransform);
977         }
978         return _ConvertBuffer_8bppMask2Gray(dest_buf, dest_pitch, width, height,
979                                             pSrcBitmap, src_left, src_top);
980       }
981       if ((src_format & 0xff) >= 24) {
982         return _ConvertBuffer_RgbOrCmyk2Gray(dest_buf, dest_pitch, width,
983                                              height, pSrcBitmap, src_left,
984                                              src_top, pIccTransform);
985       }
986       return FALSE;
987     }
988     case FXDIB_8bppRgb:
989     case FXDIB_8bppRgba: {
990       if ((src_format & 0xff) == 8 && !pSrcBitmap->GetPalette()) {
991         return ConvertBuffer(FXDIB_8bppMask, dest_buf, dest_pitch, width,
992                              height, pSrcBitmap, src_left, src_top, d_pal,
993                              pIccTransform);
994       }
995       d_pal = FX_Alloc(FX_DWORD, 256);
996       if (((src_format & 0xff) == 1 || (src_format & 0xff) == 8) &&
997           pSrcBitmap->GetPalette()) {
998         return _ConvertBuffer_Plt2PltRgb8(dest_buf, dest_pitch, width, height,
999                                           pSrcBitmap, src_left, src_top, d_pal,
1000                                           pIccTransform);
1001       }
1002       if ((src_format & 0xff) >= 24) {
1003         return _ConvertBuffer_Rgb2PltRgb8(dest_buf, dest_pitch, width, height,
1004                                           pSrcBitmap, src_left, src_top, d_pal,
1005                                           pIccTransform);
1006       }
1007       return FALSE;
1008     }
1009     case FXDIB_Rgb:
1010     case FXDIB_Rgba: {
1011       if ((src_format & 0xff) == 1) {
1012         if (pSrcBitmap->GetPalette()) {
1013           return _ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch,
1014                                             width, height, pSrcBitmap, src_left,
1015                                             src_top, pIccTransform);
1016         }
1017         return _ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch,
1018                                            width, height, pSrcBitmap, src_left,
1019                                            src_top);
1020       }
1021       if ((src_format & 0xff) == 8) {
1022         if (pSrcBitmap->GetPalette()) {
1023           return _ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch,
1024                                             width, height, pSrcBitmap, src_left,
1025                                             src_top, pIccTransform);
1026         }
1027         return _ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch,
1028                                            width, height, pSrcBitmap, src_left,
1029                                            src_top);
1030       }
1031       if ((src_format & 0xff) == 24) {
1032         return _ConvertBuffer_24bppRgb2Rgb24(dest_buf, dest_pitch, width,
1033                                              height, pSrcBitmap, src_left,
1034                                              src_top, pIccTransform);
1035       }
1036       if ((src_format & 0xff) == 32) {
1037         return _ConvertBuffer_32bppRgb2Rgb24(dest_buf, dest_pitch, width,
1038                                              height, pSrcBitmap, src_left,
1039                                              src_top, pIccTransform);
1040       }
1041       return FALSE;
1042     }
1043     case FXDIB_Argb:
1044     case FXDIB_Rgb32: {
1045       if ((src_format & 0xff) == 1) {
1046         if (pSrcBitmap->GetPalette()) {
1047           return _ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch,
1048                                             width, height, pSrcBitmap, src_left,
1049                                             src_top, pIccTransform);
1050         }
1051         return _ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch,
1052                                            width, height, pSrcBitmap, src_left,
1053                                            src_top);
1054       }
1055       if ((src_format & 0xff) == 8) {
1056         if (pSrcBitmap->GetPalette()) {
1057           return _ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch,
1058                                             width, height, pSrcBitmap, src_left,
1059                                             src_top, pIccTransform);
1060         }
1061         return _ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch,
1062                                            width, height, pSrcBitmap, src_left,
1063                                            src_top);
1064       }
1065       if ((src_format & 0xff) >= 24) {
1066         if (src_format & 0x0400) {
1067           return _ConvertBuffer_32bppCmyk2Rgb32(dest_buf, dest_pitch, width,
1068                                                 height, pSrcBitmap, src_left,
1069                                                 src_top, pIccTransform);
1070         }
1071         return _ConvertBuffer_Rgb2Rgb32(dest_buf, dest_pitch, width, height,
1072                                         pSrcBitmap, src_left, src_top,
1073                                         pIccTransform);
1074       }
1075       return FALSE;
1076     }
1077     default:
1078       return FALSE;
1079   }
1080   return FALSE;
1081 }
CloneConvert(FXDIB_Format dest_format,const FX_RECT * pClip,void * pIccTransform) const1082 CFX_DIBitmap* CFX_DIBSource::CloneConvert(FXDIB_Format dest_format,
1083                                           const FX_RECT* pClip,
1084                                           void* pIccTransform) const {
1085   if (dest_format == GetFormat() && !pIccTransform) {
1086     return Clone(pClip);
1087   }
1088   if (pClip) {
1089     CFX_DIBitmap* pClone = Clone(pClip);
1090     if (!pClone) {
1091       return NULL;
1092     }
1093     if (!pClone->ConvertFormat(dest_format, pIccTransform)) {
1094       delete pClone;
1095       return NULL;
1096     }
1097     return pClone;
1098   }
1099   CFX_DIBitmap* pClone = new CFX_DIBitmap;
1100   if (!pClone->Create(m_Width, m_Height, dest_format)) {
1101     delete pClone;
1102     return NULL;
1103   }
1104   FX_BOOL ret = TRUE;
1105   CFX_DIBitmap* pSrcAlpha = NULL;
1106   if (HasAlpha()) {
1107     pSrcAlpha = (GetFormat() == FXDIB_Argb) ? GetAlphaMask() : m_pAlphaMask;
1108     if (!pSrcAlpha) {
1109       delete pClone;
1110       return NULL;
1111     }
1112   }
1113   if (dest_format & 0x0200) {
1114     if (dest_format == FXDIB_Argb)
1115       ret = pSrcAlpha ? pClone->LoadChannel(FXDIB_Alpha, pSrcAlpha, FXDIB_Alpha)
1116                       : pClone->LoadChannel(FXDIB_Alpha, 0xff);
1117     else {
1118       ret = pClone->CopyAlphaMask(pSrcAlpha);
1119     }
1120   }
1121   if (pSrcAlpha && pSrcAlpha != m_pAlphaMask) {
1122     delete pSrcAlpha;
1123     pSrcAlpha = NULL;
1124   }
1125   if (!ret) {
1126     delete pClone;
1127     return NULL;
1128   }
1129   FX_DWORD* pal_8bpp = NULL;
1130   ret = ConvertBuffer(dest_format, pClone->GetBuffer(), pClone->GetPitch(),
1131                       m_Width, m_Height, this, 0, 0, pal_8bpp, pIccTransform);
1132   if (!ret) {
1133     FX_Free(pal_8bpp);
1134     delete pClone;
1135     return NULL;
1136   }
1137   if (pal_8bpp) {
1138     pClone->CopyPalette(pal_8bpp);
1139     FX_Free(pal_8bpp);
1140     pal_8bpp = NULL;
1141   }
1142   return pClone;
1143 }
ConvertFormat(FXDIB_Format dest_format,void * pIccTransform)1144 FX_BOOL CFX_DIBitmap::ConvertFormat(FXDIB_Format dest_format,
1145                                     void* pIccTransform) {
1146   FXDIB_Format src_format = GetFormat();
1147   if (dest_format == src_format && !pIccTransform) {
1148     return TRUE;
1149   }
1150   if (dest_format == FXDIB_8bppMask && src_format == FXDIB_8bppRgb &&
1151       !m_pPalette) {
1152     m_AlphaFlag = 1;
1153     return TRUE;
1154   }
1155   if (dest_format == FXDIB_Argb && src_format == FXDIB_Rgb32 &&
1156       !pIccTransform) {
1157     m_AlphaFlag = 2;
1158     for (int row = 0; row < m_Height; row++) {
1159       uint8_t* scanline = m_pBuffer + row * m_Pitch + 3;
1160       for (int col = 0; col < m_Width; col++) {
1161         *scanline = 0xff;
1162         scanline += 4;
1163       }
1164     }
1165     return TRUE;
1166   }
1167   int dest_bpp = dest_format & 0xff;
1168   int dest_pitch = (dest_bpp * m_Width + 31) / 32 * 4;
1169   uint8_t* dest_buf = FX_TryAlloc(uint8_t, dest_pitch * m_Height + 4);
1170   if (!dest_buf) {
1171     return FALSE;
1172   }
1173   CFX_DIBitmap* pAlphaMask = NULL;
1174   if (dest_format == FXDIB_Argb) {
1175     FXSYS_memset(dest_buf, 0xff, dest_pitch * m_Height + 4);
1176     if (m_pAlphaMask) {
1177       for (int row = 0; row < m_Height; row++) {
1178         uint8_t* pDstScanline = dest_buf + row * dest_pitch + 3;
1179         const uint8_t* pSrcScanline = m_pAlphaMask->GetScanline(row);
1180         for (int col = 0; col < m_Width; col++) {
1181           *pDstScanline = *pSrcScanline++;
1182           pDstScanline += 4;
1183         }
1184       }
1185     }
1186   } else if (dest_format & 0x0200) {
1187     if (src_format == FXDIB_Argb) {
1188       pAlphaMask = GetAlphaMask();
1189       if (!pAlphaMask) {
1190         FX_Free(dest_buf);
1191         return FALSE;
1192       }
1193     } else {
1194       if (!m_pAlphaMask) {
1195         if (!BuildAlphaMask()) {
1196           FX_Free(dest_buf);
1197           return FALSE;
1198         }
1199         pAlphaMask = m_pAlphaMask;
1200         m_pAlphaMask = NULL;
1201       } else {
1202         pAlphaMask = m_pAlphaMask;
1203       }
1204     }
1205   }
1206   FX_BOOL ret = FALSE;
1207   FX_DWORD* pal_8bpp = NULL;
1208   ret = ConvertBuffer(dest_format, dest_buf, dest_pitch, m_Width, m_Height,
1209                       this, 0, 0, pal_8bpp, pIccTransform);
1210   if (!ret) {
1211     FX_Free(pal_8bpp);
1212     if (pAlphaMask != m_pAlphaMask) {
1213       delete pAlphaMask;
1214     }
1215     FX_Free(dest_buf);
1216     return FALSE;
1217   }
1218   if (m_pAlphaMask && pAlphaMask != m_pAlphaMask) {
1219     delete m_pAlphaMask;
1220   }
1221   m_pAlphaMask = pAlphaMask;
1222   FX_Free(m_pPalette);
1223   m_pPalette = pal_8bpp;
1224   if (!m_bExtBuf) {
1225     FX_Free(m_pBuffer);
1226   }
1227   m_bExtBuf = FALSE;
1228   m_pBuffer = dest_buf;
1229   m_bpp = (uint8_t)dest_format;
1230   m_AlphaFlag = (uint8_t)(dest_format >> 8);
1231   m_Pitch = dest_pitch;
1232   return TRUE;
1233 }
1234