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 "../../../include/fxge/fx_dib.h"
8 #include "../../../include/fxge/fx_ge.h"
9 #include "../../../include/fxcodec/fx_codec.h"
10 const FX_DWORD g_dwWinPalette[256] = {
11 0xff000000, 0xff800000, 0xff008000, 0xff808000, 0xff000080, 0xff800080,
12 0xff008080, 0xff808080, 0xffC0DCC0, 0xffA6CAF0, 0xff2A3FAA, 0xff2A3FFF,
13 0xff2A5F00, 0xff2A5F55, 0xff2A5FAA, 0xff2A5FFF, 0xff2A7F00, 0xff2A7F55,
14 0xff2A7FAA, 0xff2A7FFF, 0xff2A9F00, 0xff2A9F55, 0xff2A9FAA, 0xff2A9FFF,
15 0xff2ABF00, 0xff2ABF55, 0xff2ABFAA, 0xff2ABFFF, 0xff2ADF00, 0xff2ADF55,
16 0xff2ADFAA, 0xff2ADFFF, 0xff2AFF00, 0xff2AFF55, 0xff2AFFAA, 0xff2AFFFF,
17 0xff550000, 0xff550055, 0xff5500AA, 0xff5500FF, 0xff551F00, 0xff551F55,
18 0xff551FAA, 0xff551FFF, 0xff553F00, 0xff553F55, 0xff553FAA, 0xff553FFF,
19 0xff555F00, 0xff555F55, 0xff555FAA, 0xff555FFF, 0xff557F00, 0xff557F55,
20 0xff557FAA, 0xff557FFF, 0xff559F00, 0xff559F55, 0xff559FAA, 0xff559FFF,
21 0xff55BF00, 0xff55BF55, 0xff55BFAA, 0xff55BFFF, 0xff55DF00, 0xff55DF55,
22 0xff55DFAA, 0xff55DFFF, 0xff55FF00, 0xff55FF55, 0xff55FFAA, 0xff55FFFF,
23 0xff7F0000, 0xff7F0055, 0xff7F00AA, 0xff7F00FF, 0xff7F1F00, 0xff7F1F55,
24 0xff7F1FAA, 0xff7F1FFF, 0xff7F3F00, 0xff7F3F55, 0xff7F3FAA, 0xff7F3FFF,
25 0xff7F5F00, 0xff7F5F55, 0xff7F5FAA, 0xff7F5FFF, 0xff7F7F00, 0xff7F7F55,
26 0xff7F7FAA, 0xff7F7FFF, 0xff7F9F00, 0xff7F9F55, 0xff7F9FAA, 0xff7F9FFF,
27 0xff7FBF00, 0xff7FBF55, 0xff7FBFAA, 0xff7FBFFF, 0xff7FDF00, 0xff7FDF55,
28 0xff7FDFAA, 0xff7FDFFF, 0xff00FF7F, 0xff7FFF55, 0xff7FFFAA, 0xff7FFFFF,
29 0xffAA0000, 0xffAA0055, 0xffAA00AA, 0xffAA00FF, 0xffAA1F00, 0xffAA1F55,
30 0xffAA1FAA, 0xffAA1FFF, 0xffAA3F00, 0xffAA3F55, 0xffAA3FAA, 0xffAA3FFF,
31 0xffAA5F00, 0xffAA5F55, 0xffAA5FAA, 0xffAA5FFF, 0xffAA7F00, 0xffAA7F55,
32 0xffAA7FAA, 0xffAA7FFF, 0xffAA9F00, 0xffAA9F55, 0xffAA9FAA, 0xffAA9FFF,
33 0xffAABF00, 0xffAABF55, 0xffAABFAA, 0xffAABFFF, 0xffAADF00, 0xffAADF55,
34 0xffAADFAA, 0xffAADFFF, 0xffAAFF00, 0xffAAFF55, 0xffAAFFAA, 0xffAAFFFF,
35 0xffD40000, 0xffD40055, 0xffD400AA, 0xffD400FF, 0xffD41F00, 0xffD41F55,
36 0xffD41FAA, 0xffD41FFF, 0xffD43F00, 0xffD43F55, 0xffD43FAA, 0xffD43FFF,
37 0xffD45F00, 0xffD45F55, 0xffD45FAA, 0xffD45FFF, 0xffD47F00, 0xffD47F55,
38 0xffD47FAA, 0xffD4F7FF, 0xffD49F00, 0xffD49F55, 0xffD49FAA, 0xffD49FFF,
39 0xffD4BF00, 0xffD4BF55, 0xffD4BFAA, 0xffD4BFFF, 0xffD4DF00, 0xffD4DF55,
40 0xffD4DFAA, 0xffD4DFFF, 0xffD4FF00, 0xffD4FF55, 0xffD4FFAA, 0xffD4FFFF,
41 0xffFF0055, 0xffFF00AA, 0xffFF1F00, 0xffFF1F55, 0xffFF1FAA, 0xffFF1FFF,
42 0xffFF3F00, 0xffFF3F55, 0xffFF3FAA, 0xffFF3FFF, 0xffFF5F00, 0xffFF5F55,
43 0xffFF5FAA, 0xffFF5FFF, 0xffFF7F00, 0xffFF7F55, 0xffFF7FAA, 0xffFF7FFF,
44 0xffFF9F00, 0xffFF9F55, 0xffFF9FAA, 0xffFF9FFF, 0xffFFBF00, 0xffFFBF55,
45 0xffFFBFAA, 0xffFFBFFF, 0xffFFDF00, 0xffFFDF55, 0xffFFDFAA, 0xffFFDFFF,
46 0xffFFFF55, 0xffFFFFAA, 0xffCCCCFF, 0xffFFCCFF, 0xff33FFFF, 0xff66FFFF,
47 0xff99FFFF, 0xffCCFFFF, 0xff007F00, 0xff007F55, 0xff007FAA, 0xff007FFF,
48 0xff009F00, 0xff009F55, 0xff009FAA, 0xff009FFF, 0xff00BF00, 0xff00BF55,
49 0xff00BFAA, 0xff00BFFF, 0xff00DF00, 0xff00DF55, 0xff00DFAA, 0xff00DFFF,
50 0xff00FF55, 0xff00FFAA, 0xff2A0000, 0xff2A0055, 0xff2A00AA, 0xff2A00FF,
51 0xff2A1F00, 0xff2A1F55, 0xff2A1FAA, 0xff2A1FFF, 0xff2A3F00, 0xff2A3F55,
52 0xffFFFBF0, 0xffA0A0A4, 0xff808080, 0xffFF0000, 0xff00FF00, 0xffFF0000,
53 0xff0000FF, 0xffFF00FF, 0xff00FFFF, 0xffFFFFFF
54 };
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,
92 0xffEE0000, 0xffDD0000, 0xffBB0000, 0xffAA0000, 0xff880000, 0xff770000,
93 0xff550000, 0xff440000, 0xff220000, 0xff110000, 0xff00EE00, 0xff00DD00,
94 0xff00BB00, 0xff00AA00, 0xff008800, 0xff007700, 0xff005500, 0xff004400,
95 0xff002200, 0xff001100, 0xff0000EE, 0xff0000DD, 0xff0000BB, 0xff0000AA,
96 0xff000088, 0xff000077, 0xff000055, 0xff000044, 0xff000022, 0xff000011,
97 0xffEEEEEE, 0xffDDDDDD, 0xffBBBBBB, 0xffAAAAAA, 0xff888888, 0xff777777,
98 0xff555555, 0xff444444, 0xff222222, 0xff111111, 0xff000000
99 };
100 class CFX_Palette
101 {
102 public:
103 CFX_Palette();
104 ~CFX_Palette();
105 public:
106 FX_BOOL BuildPalette(const CFX_DIBSource* pBitmap, int dwPaletteType);
GetPalette() const107 FX_DWORD* GetPalette() const
108 {
109 return m_pPalette;
110 }
111
GetColorLut() const112 FX_DWORD* GetColorLut()const
113 {
114 return m_cLut;
115 }
GetAmountLut() const116 FX_DWORD* GetAmountLut()const
117 {
118 return m_aLut;
119 }
Getlut() const120 FX_INT32 Getlut()const
121 {
122 return m_lut;
123 }
124 protected:
125 FX_DWORD* m_pPalette;
126 FX_DWORD* m_cLut;
127 FX_DWORD* m_aLut;
128 int m_lut;
129 };
_Partition(FX_DWORD * alut,FX_DWORD * clut,int l,int r)130 int _Partition(FX_DWORD* alut, FX_DWORD* clut, int l, int r)
131 {
132 FX_DWORD p_a = alut[l];
133 FX_DWORD p_c = clut[l];
134 while(l < r) {
135 while(l < r && alut[r] >= p_a) {
136 r--;
137 }
138 if (l < r) {
139 alut[l] = alut[r];
140 clut[l++] = clut[r];
141 }
142 while(l < r && alut[l] <= p_a) {
143 l++;
144 }
145 if (l < r) {
146 alut[r] = alut[l];
147 clut[r--] = clut[l];
148 }
149 }
150 alut[l] = p_a;
151 clut[l] = p_c;
152 return l;
153 }
_Qsort(FX_DWORD * alut,FX_DWORD * clut,int l,int r)154 void _Qsort(FX_DWORD* alut, FX_DWORD* clut, int l, int r)
155 {
156 if(l < r) {
157 int pI = _Partition(alut, clut, l, r);
158 _Qsort(alut, clut, l, pI - 1);
159 _Qsort(alut, clut, pI + 1, r);
160 }
161 }
_ColorDecode(FX_DWORD pal_v,FX_BYTE & r,FX_BYTE & g,FX_BYTE & b)162 void _ColorDecode(FX_DWORD pal_v, FX_BYTE& r, FX_BYTE& g, FX_BYTE& b)
163 {
164 r = (FX_BYTE)((pal_v & 0xf00) >> 4);
165 g = (FX_BYTE)(pal_v & 0x0f0);
166 b = (FX_BYTE)((pal_v & 0x00f) << 4);
167 }
_Obtain_Pal(FX_DWORD * aLut,FX_DWORD * cLut,FX_DWORD * dest_pal,int pal_type,FX_DWORD * win_mac_pal,FX_DWORD lut)168 void _Obtain_Pal(FX_DWORD* aLut, FX_DWORD*cLut, FX_DWORD* dest_pal, int pal_type, FX_DWORD* win_mac_pal, FX_DWORD lut)
169 {
170 int row, col;
171 FX_DWORD lut_1 = lut - 1;
172 if (pal_type == FXDIB_PALETTE_LOC) {
173 for (row = 0; row < 256; row++) {
174 int lut_offset = lut_1 - row;
175 if (lut_offset < 0) {
176 lut_offset += 256;
177 }
178 FX_DWORD color = cLut[lut_offset];
179 FX_BYTE r, g, b;
180 _ColorDecode(color, r, g, b);
181 dest_pal[row] = ((FX_DWORD)r << 16) | ((FX_DWORD)g << 8) | b | 0xff000000;
182 aLut[lut_offset] = row;
183 }
184 } else {
185 for (row = 0; row < 256; row++) {
186 int lut_offset = lut_1 - row;
187 if (lut_offset < 0) {
188 lut_offset += 256;
189 }
190 FX_BYTE r, g, b;
191 _ColorDecode(cLut[lut_offset], r, g, b);
192 int error, min_error = 1000000;
193 int c_index = 0;
194 for (col = 0; col < 256; col++) {
195 FX_DWORD p_color = win_mac_pal[col];
196 int d_r = r - (FX_BYTE)(p_color >> 16);
197 int d_g = g - (FX_BYTE)(p_color >> 8);
198 int d_b = b - (FX_BYTE)p_color;
199 error = d_r * d_r + d_g * d_g + d_b * d_b;
200 if (error < min_error) {
201 min_error = error;
202 c_index = col;
203 }
204 }
205 dest_pal[row] = win_mac_pal[c_index];
206 aLut[lut_offset] = row;
207 }
208 }
209 }
CFX_Palette()210 CFX_Palette::CFX_Palette()
211 {
212 m_pPalette = NULL;
213 m_cLut = NULL;
214 m_aLut = NULL;
215 m_lut = 0;
216 }
~CFX_Palette()217 CFX_Palette::~CFX_Palette()
218 {
219 if (m_pPalette) {
220 FX_Free(m_pPalette);
221 }
222 if (m_cLut) {
223 FX_Free(m_cLut);
224 }
225 if (m_aLut) {
226 FX_Free(m_aLut);
227 }
228 m_lut = 0;
229 }
BuildPalette(const CFX_DIBSource * pBitmap,int pal_type)230 FX_BOOL CFX_Palette::BuildPalette(const CFX_DIBSource* pBitmap, int pal_type)
231 {
232 if (pBitmap == NULL) {
233 return FALSE;
234 }
235 if (m_pPalette != NULL) {
236 FX_Free(m_pPalette);
237 }
238 m_pPalette = FX_Alloc(FX_DWORD, 256);
239 int bpp = pBitmap->GetBPP() / 8;
240 int width = pBitmap->GetWidth();
241 int height = pBitmap->GetHeight();
242 if (m_cLut) {
243 FX_Free(m_cLut);
244 m_cLut = NULL;
245 }
246 if (m_aLut) {
247 FX_Free(m_aLut);
248 m_aLut = NULL;
249 }
250 m_cLut = FX_Alloc(FX_DWORD, 4096);
251 m_aLut = FX_Alloc(FX_DWORD, 4096);
252 int row, col;
253 m_lut = 0;
254 for (row = 0; row < height; row++) {
255 FX_BYTE* scan_line = (FX_BYTE*)pBitmap->GetScanline(row);
256 for (col = 0; col < width; col++) {
257 FX_BYTE* src_port = scan_line + col * bpp;
258 FX_DWORD b = src_port[0] & 0xf0;
259 FX_DWORD g = src_port[1] & 0xf0;
260 FX_DWORD r = src_port[2] & 0xf0;
261 FX_DWORD index = (r << 4) + g + (b >> 4);
262 m_aLut[index]++;
263 }
264 }
265 for (row = 0; row < 4096; row++) {
266 if (m_aLut[row] != 0) {
267 m_aLut[m_lut] = m_aLut[row];
268 m_cLut[m_lut] = row;
269 m_lut++;
270 }
271 }
272 _Qsort(m_aLut, m_cLut, 0, m_lut - 1);
273 FX_DWORD* win_mac_pal = NULL;
274 if (pal_type == FXDIB_PALETTE_WIN) {
275 win_mac_pal = (FX_DWORD*)g_dwWinPalette;
276 } else if (pal_type == FXDIB_PALETTE_MAC) {
277 win_mac_pal = (FX_DWORD*)g_dwMacPalette;
278 }
279 _Obtain_Pal(m_aLut, m_cLut, m_pPalette, pal_type, win_mac_pal, m_lut);
280 return TRUE;
281 }
_ConvertBuffer_1bppMask2Gray(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)282 FX_BOOL _ConvertBuffer_1bppMask2Gray(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
283 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
284 {
285 FX_BYTE set_gray, reset_gray;
286 set_gray = 0xff;
287 reset_gray = 0x00;
288 for (int row = 0; row < height; row ++) {
289 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
290 FXSYS_memset8(dest_scan, reset_gray, width);
291 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
292 for (int col = src_left; col < src_left + width; col ++) {
293 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
294 *dest_scan = set_gray;
295 }
296 dest_scan ++;
297 }
298 }
299 return TRUE;
300 }
_ConvertBuffer_8bppMask2Gray(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)301 FX_BOOL _ConvertBuffer_8bppMask2Gray(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
302 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
303 {
304 for (int row = 0; row < height; row ++) {
305 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
306 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
307 FXSYS_memcpy32(dest_scan, src_scan, width);
308 }
309 return TRUE;
310 }
_ConvertBuffer_1bppPlt2Gray(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)311 FX_BOOL _ConvertBuffer_1bppPlt2Gray(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
312 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
313 {
314 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
315 FX_BYTE gray[2];
316 if (pIccTransform) {
317 FX_DWORD plt[2];
318 if (pSrcBitmap->IsCmykImage()) {
319 plt[0] = FXCMYK_TODIB(src_plt[0]);
320 plt[1] = FXCMYK_TODIB(src_plt[1]);
321 } else {
322 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
323 bgr_ptr[0] = FXARGB_B(src_plt[0]);
324 bgr_ptr[1] = FXARGB_G(src_plt[0]);
325 bgr_ptr[2] = FXARGB_R(src_plt[0]);
326 bgr_ptr[3] = FXARGB_B(src_plt[1]);
327 bgr_ptr[4] = FXARGB_G(src_plt[1]);
328 bgr_ptr[5] = FXARGB_R(src_plt[1]);
329 }
330 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
331 pIccModule->TranslateScanline(pIccTransform, gray, (FX_LPCBYTE)plt, 2);
332 } else {
333 FX_BYTE reset_r, reset_g, reset_b,
334 set_r, set_g, set_b;
335 if (pSrcBitmap->IsCmykImage()) {
336 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0]),
337 reset_r, reset_g, reset_b);
338 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1]),
339 set_r, set_g, set_b);
340 } else {
341 reset_r = FXARGB_R(src_plt[0]);
342 reset_g = FXARGB_G(src_plt[0]);
343 reset_b = FXARGB_B(src_plt[0]);
344 set_r = FXARGB_R(src_plt[1]);
345 set_g = FXARGB_G(src_plt[1]);
346 set_b = FXARGB_B(src_plt[1]);
347 }
348 gray[0] = FXRGB2GRAY(reset_r, reset_g, reset_b);
349 gray[1] = FXRGB2GRAY(set_r, set_g, set_b);
350 }
351 for (int row = 0; row < height; row ++) {
352 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
353 FXSYS_memset8(dest_scan, gray[0], width);
354 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
355 for (int col = src_left; col < src_left + width; col ++) {
356 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
357 *dest_scan = gray[1];
358 }
359 dest_scan ++;
360 }
361 }
362 return TRUE;
363 }
_ConvertBuffer_8bppPlt2Gray(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)364 FX_BOOL _ConvertBuffer_8bppPlt2Gray(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
365 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
366 {
367 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
368 FX_BYTE gray[256];
369 if (pIccTransform) {
370 FX_DWORD plt[256];
371 if (pSrcBitmap->IsCmykImage()) {
372 for (int i = 0; i < 256; i ++) {
373 plt[i] = FXCMYK_TODIB(src_plt[i]);
374 }
375 } else {
376 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
377 for (int i = 0; i < 256; i ++) {
378 *bgr_ptr++ = FXARGB_B(src_plt[i]);
379 *bgr_ptr++ = FXARGB_G(src_plt[i]);
380 *bgr_ptr++ = FXARGB_R(src_plt[i]);
381 }
382 }
383 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
384 pIccModule->TranslateScanline(pIccTransform, gray, (FX_LPCBYTE)plt, 256);
385 } else {
386 if (pSrcBitmap->IsCmykImage()) {
387 FX_BYTE r, g, b;
388 for (int i = 0; i < 256; i ++) {
389 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]),
390 r, g, b);
391 gray[i] = FXRGB2GRAY(r, g, b);
392 }
393 } else
394 for (int i = 0; i < 256; i ++) {
395 gray[i] = FXRGB2GRAY(FXARGB_R(src_plt[i]), FXARGB_G(src_plt[i]), FXARGB_B(src_plt[i]));
396 }
397 }
398 for (int row = 0; row < height; row ++) {
399 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
400 FX_LPCBYTE 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(FX_LPBYTE 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(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
408 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
409 {
410 int Bpp = pSrcBitmap->GetBPP() / 8;
411 if (pIccTransform) {
412 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
413 if (Bpp == 3 || pSrcBitmap->IsCmykImage()) {
414 for (int row = 0; row < height; row ++) {
415 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
416 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
417 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, width);
418 }
419 } else {
420 for (int row = 0; row < height; row ++) {
421 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
422 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
423 for (int col = 0; col < width; col ++) {
424 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
425 dest_scan++;
426 src_scan += 4;
427 }
428 }
429 }
430 } else {
431 if (pSrcBitmap->IsCmykImage()) {
432 for (int row = 0; row < height; row ++) {
433 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
434 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
435 for (int col = 0; col < width; col ++) {
436 FX_BYTE r, g, b;
437 AdobeCMYK_to_sRGB1(FXSYS_GetCValue((FX_DWORD)src_scan[0]), FXSYS_GetMValue((FX_DWORD)src_scan[1]), FXSYS_GetYValue((FX_DWORD)src_scan[2]), FXSYS_GetKValue((FX_DWORD)src_scan[3]),
438 r, g, b);
439 *dest_scan++ = FXRGB2GRAY(r, g, b);
440 src_scan += 4;
441 }
442 }
443 } else {
444 for (int row = 0; row < height; row ++) {
445 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
446 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
447 for (int col = 0; col < width; col ++) {
448 *dest_scan++ = FXRGB2GRAY(src_scan[2], src_scan[1], src_scan[0]);
449 src_scan += Bpp;
450 }
451 }
452 }
453 }
454 return TRUE;
455 }
_ConvertBuffer_IndexCopy(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)456 inline void _ConvertBuffer_IndexCopy(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
457 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
458 {
459 if (pSrcBitmap->GetBPP() == 1) {
460 for (int row = 0; row < height; row ++) {
461 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
462 FXSYS_memset32(dest_scan, 0, width);
463 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
464 for (int col = src_left; col < src_left + width; col ++) {
465 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
466 *dest_scan = 1;
467 }
468 dest_scan ++;
469 }
470 }
471 } else {
472 for (int row = 0; row < height; row ++) {
473 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
474 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
475 FXSYS_memcpy32(dest_scan, src_scan, width);
476 }
477 }
478 }
_ConvertBuffer_Plt2PltRgb8(FX_LPBYTE 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)479 FX_BOOL _ConvertBuffer_Plt2PltRgb8(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
480 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, FX_DWORD* dst_plt, void* pIccTransform)
481 {
482 _ConvertBuffer_IndexCopy(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
483 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
484 int plt_size = pSrcBitmap->GetPaletteSize();
485 if (pIccTransform) {
486 FX_DWORD plt[256];
487 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
488 if (pSrcBitmap->IsCmykImage()) {
489 for (int i = 0; i < plt_size; i ++) {
490 plt[i] = FXCMYK_TODIB(src_plt[i]);
491 }
492 } else {
493 for (int i = 0; i < plt_size; i ++) {
494 *bgr_ptr++ = FXARGB_B(src_plt[i]);
495 *bgr_ptr++ = FXARGB_G(src_plt[i]);
496 *bgr_ptr++ = FXARGB_R(src_plt[i]);
497 }
498 bgr_ptr = (FX_LPBYTE)plt;
499 }
500 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
501 pIccModule->TranslateScanline(pIccTransform, (FX_LPBYTE)plt, (FX_LPCBYTE)plt, plt_size);
502 for (int i = 0; i < plt_size; i ++) {
503 dst_plt[i] = FXARGB_MAKE(0xff, bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
504 bgr_ptr += 3;
505 }
506 } else {
507 if (pSrcBitmap->IsCmykImage()) {
508 for (int i = 0; i < plt_size; i ++) {
509 FX_BYTE r, g, b;
510 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]),
511 r, g, b);
512 dst_plt[i] = FXARGB_MAKE(0xff, r, g, b);
513 }
514 } else {
515 FXSYS_memcpy32(dst_plt, src_plt, plt_size * 4);
516 }
517 }
518 return TRUE;
519 }
_ConvertBuffer_Rgb2PltRgb8_NoTransform(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,FX_DWORD * dst_plt)520 inline FX_BOOL _ConvertBuffer_Rgb2PltRgb8_NoTransform(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
521 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, FX_DWORD* dst_plt)
522 {
523 int bpp = pSrcBitmap->GetBPP() / 8;
524 int row, col;
525 CFX_Palette palette;
526 palette.BuildPalette(pSrcBitmap, FXDIB_PALETTE_LOC);
527 FX_DWORD* cLut = palette.GetColorLut();
528 FX_DWORD* aLut = palette.GetAmountLut();
529 if (cLut == NULL || aLut == NULL) {
530 return FALSE;
531 }
532 int lut = palette.Getlut();
533 FX_DWORD* pPalette = palette.GetPalette();
534 if (lut > 256) {
535 int err, min_err;
536 int lut_256 = lut - 256;
537 for (row = 0; row < lut_256; row++) {
538 min_err = 1000000;
539 FX_BYTE r, g, b;
540 _ColorDecode(cLut[row], r, g, b);
541 int clrindex = 0;
542 for (int col = 0; col < 256; col++) {
543 FX_DWORD p_color = *(pPalette + col);
544 int d_r = r - (FX_BYTE)(p_color >> 16);
545 int d_g = g - (FX_BYTE)(p_color >> 8);
546 int d_b = b - (FX_BYTE)(p_color);
547 err = d_r * d_r + d_g * d_g + d_b * d_b;
548 if (err < min_err) {
549 min_err = err;
550 clrindex = col;
551 }
552 }
553 aLut[row] = clrindex;
554 }
555 }
556 FX_INT32 lut_1 = lut - 1;
557 for (row = 0; row < height; row ++) {
558 FX_BYTE* src_scan = (FX_BYTE*)pSrcBitmap->GetScanline(src_top + row) + src_left;
559 FX_BYTE* dest_scan = dest_buf + row * dest_pitch;
560 for (col = 0; col < width; col++) {
561 FX_BYTE* src_port = src_scan + col * bpp;
562 int r = src_port[2] & 0xf0;
563 int g = src_port[1] & 0xf0;
564 int b = src_port[0] & 0xf0;
565 FX_DWORD clrindex = (r << 4) + g + (b >> 4);
566 for (int i = lut_1; i >= 0; i--)
567 if (clrindex == cLut[i]) {
568 *(dest_scan + col) = (FX_BYTE)(aLut[i]);
569 break;
570 }
571 }
572 }
573 FXSYS_memcpy32(dst_plt, pPalette, sizeof(FX_DWORD) * 256);
574 return TRUE;
575 }
_ConvertBuffer_Rgb2PltRgb8(FX_LPBYTE 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)576 FX_BOOL _ConvertBuffer_Rgb2PltRgb8(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
577 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, FX_DWORD* dst_plt, void* pIccTransform)
578 {
579 FX_BOOL ret = _ConvertBuffer_Rgb2PltRgb8_NoTransform(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, dst_plt);
580 if (ret && pIccTransform) {
581 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
582 for (int i = 0; i < 256; i++) {
583 FX_ARGB* plt = dst_plt + i;
584 FX_ARGB plt_entry = FXARGB_TODIB(*plt);
585 pIccModule->TranslateScanline(pIccTransform, (FX_LPBYTE)&plt_entry, (FX_LPCBYTE)&plt_entry, 1);
586 *plt = FXARGB_TODIB(plt_entry);
587 }
588 }
589 return ret;
590 }
_ConvertBuffer_1bppMask2Rgb(FXDIB_Format dst_format,FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)591 FX_BOOL _ConvertBuffer_1bppMask2Rgb(FXDIB_Format dst_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
592 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
593 {
594 int comps = (dst_format & 0xff) / 8;
595 FX_BYTE set_gray, reset_gray;
596 set_gray = 0xff;
597 reset_gray = 0x00;
598 for (int row = 0; row < height; row ++) {
599 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
600 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
601 for (int col = src_left; col < src_left + width; col ++) {
602 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
603 dest_scan[0] = set_gray;
604 dest_scan[1] = set_gray;
605 dest_scan[2] = set_gray;
606 } else {
607 dest_scan[0] = reset_gray;
608 dest_scan[1] = reset_gray;
609 dest_scan[2] = reset_gray;
610 }
611 dest_scan += comps;
612 }
613 }
614 return TRUE;
615 }
_ConvertBuffer_8bppMask2Rgb(FXDIB_Format dst_format,FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top)616 FX_BOOL _ConvertBuffer_8bppMask2Rgb(FXDIB_Format dst_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
617 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
618 {
619 int comps = (dst_format & 0xff) / 8;
620 for (int row = 0; row < height; row ++) {
621 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
622 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
623 FX_BYTE src_pixel;
624 for (int col = 0; col < width; col ++) {
625 src_pixel = *src_scan++;
626 *dest_scan++ = src_pixel;
627 *dest_scan++ = src_pixel;
628 *dest_scan = src_pixel;
629 dest_scan += comps - 2;
630 }
631 }
632 return TRUE;
633 }
_ConvertBuffer_1bppPlt2Rgb(FXDIB_Format dst_format,FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)634 FX_BOOL _ConvertBuffer_1bppPlt2Rgb(FXDIB_Format dst_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
635 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
636 {
637 int comps = (dst_format & 0xff) / 8;
638 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
639 FX_DWORD plt[2];
640 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
641 if (pSrcBitmap->IsCmykImage()) {
642 plt[0] = FXCMYK_TODIB(src_plt[0]);
643 plt[1] = FXCMYK_TODIB(src_plt[1]);
644 } else {
645 bgr_ptr[0] = FXARGB_B(src_plt[0]);
646 bgr_ptr[1] = FXARGB_G(src_plt[0]);
647 bgr_ptr[2] = FXARGB_R(src_plt[0]);
648 bgr_ptr[3] = FXARGB_B(src_plt[1]);
649 bgr_ptr[4] = FXARGB_G(src_plt[1]);
650 bgr_ptr[5] = FXARGB_R(src_plt[1]);
651 }
652 if (pIccTransform) {
653 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
654 pIccModule->TranslateScanline(pIccTransform, (FX_LPBYTE)plt, (FX_LPCBYTE)plt, 2);
655 } else {
656 if (pSrcBitmap->IsCmykImage()) {
657 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0]),
658 bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
659 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1]),
660 bgr_ptr[5], bgr_ptr[4], bgr_ptr[3]);
661 }
662 }
663 for (int row = 0; row < height; row ++) {
664 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
665 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
666 for (int col = src_left; col < src_left + width; col ++) {
667 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
668 *dest_scan++ = bgr_ptr[3];
669 *dest_scan++ = bgr_ptr[4];
670 *dest_scan = bgr_ptr[5];
671 } else {
672 *dest_scan++ = bgr_ptr[0];
673 *dest_scan++ = bgr_ptr[1];
674 *dest_scan = bgr_ptr[2];
675 }
676 dest_scan += comps - 2;
677 }
678 }
679 return TRUE;
680 }
_ConvertBuffer_8bppPlt2Rgb(FXDIB_Format dst_format,FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)681 FX_BOOL _ConvertBuffer_8bppPlt2Rgb(FXDIB_Format dst_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
682 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
683 {
684 int comps = (dst_format & 0xff) / 8;
685 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
686 FX_DWORD plt[256];
687 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
688 if (!pSrcBitmap->IsCmykImage()) {
689 for (int i = 0; i < 256; i++) {
690 *bgr_ptr++ = FXARGB_B(src_plt[i]);
691 *bgr_ptr++ = FXARGB_G(src_plt[i]);
692 *bgr_ptr++ = FXARGB_R(src_plt[i]);
693 }
694 bgr_ptr = (FX_LPBYTE)plt;
695 }
696 if (pIccTransform) {
697 if (pSrcBitmap->IsCmykImage()) {
698 for (int i = 0; i < 256; i++) {
699 plt[i] = FXCMYK_TODIB(src_plt[i]);
700 }
701 }
702 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
703 pIccModule->TranslateScanline(pIccTransform, (FX_LPBYTE)plt, (FX_LPCBYTE)plt, 256);
704 } else {
705 if (pSrcBitmap->IsCmykImage()) {
706 for (int i = 0; i < 256; i++) {
707 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]),
708 bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
709 bgr_ptr += 3;
710 }
711 bgr_ptr = (FX_LPBYTE)plt;
712 }
713 }
714 for (int row = 0; row < height; row ++) {
715 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
716 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
717 for (int col = 0; col < width; col ++) {
718 FX_LPBYTE src_pixel = bgr_ptr + 3 * (*src_scan++);
719 *dest_scan++ = *src_pixel++;
720 *dest_scan++ = *src_pixel++;
721 *dest_scan = *src_pixel++;
722 dest_scan += comps - 2;
723 }
724 }
725 return TRUE;
726 }
_ConvertBuffer_24bppRgb2Rgb24(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)727 FX_BOOL _ConvertBuffer_24bppRgb2Rgb24(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
728 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
729 {
730 if (pIccTransform) {
731 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
732 for (int row = 0; row < height; row ++) {
733 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
734 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 3;
735 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, width);
736 }
737 } else {
738 for (int row = 0; row < height; row ++) {
739 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
740 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 3;
741 FXSYS_memcpy32(dest_scan, src_scan, width * 3);
742 }
743 }
744 return TRUE;
745 }
_ConvertBuffer_32bppRgb2Rgb24(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)746 FX_BOOL _ConvertBuffer_32bppRgb2Rgb24(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
747 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
748 {
749 for (int row = 0; row < height; row ++) {
750 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
751 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
752 for (int col = 0; col < width; col ++) {
753 *dest_scan++ = *src_scan++;
754 *dest_scan++ = *src_scan++;
755 *dest_scan++ = *src_scan++;
756 src_scan++;
757 }
758 }
759 if (pIccTransform) {
760 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
761 for (int row = 0; row < height; row ++) {
762 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
763 pIccModule->TranslateScanline(pIccTransform, dest_scan, dest_scan, width);
764 }
765 }
766 return TRUE;
767 }
_ConvertBuffer_Rgb2Rgb32(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)768 FX_BOOL _ConvertBuffer_Rgb2Rgb32(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
769 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
770 {
771 int comps = pSrcBitmap->GetBPP() / 8;
772 if (pIccTransform) {
773 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
774 for (int row = 0; row < height; row ++) {
775 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
776 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * comps;
777 for (int col = 0; col < width; col ++) {
778 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
779 dest_scan += 4;
780 src_scan += comps;
781 }
782 }
783 } else {
784 for (int row = 0; row < height; row ++) {
785 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
786 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * comps;
787 for (int col = 0; col < width; col ++) {
788 *dest_scan++ = *src_scan++;
789 *dest_scan++ = *src_scan++;
790 *dest_scan++ = *src_scan++;
791 dest_scan++;
792 src_scan += comps - 3;
793 }
794 }
795 }
796 return TRUE;
797 }
_ConvertBuffer_32bppCmyk2Rgb32(FX_LPBYTE dest_buf,int dest_pitch,int width,int height,const CFX_DIBSource * pSrcBitmap,int src_left,int src_top,void * pIccTransform)798 FX_BOOL _ConvertBuffer_32bppCmyk2Rgb32(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
799 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
800 {
801 if (pIccTransform) {
802 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
803 for (int row = 0; row < height; row ++) {
804 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
805 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
806 for (int col = 0; col < width; col ++) {
807 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
808 dest_scan += 4;
809 src_scan += 4;
810 }
811 }
812 } else {
813 for (int row = 0; row < height; row ++) {
814 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
815 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
816 for (int col = 0; col < width; col ++) {
817 AdobeCMYK_to_sRGB1(src_scan[0], src_scan[1], src_scan[2], src_scan[3],
818 dest_scan[2], dest_scan[1], dest_scan[0]);
819 dest_scan += 4;
820 src_scan += 4;
821 }
822 }
823 }
824 return TRUE;
825 }
ConvertBuffer(FXDIB_Format dest_format,FX_LPBYTE 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)826 FX_BOOL ConvertBuffer(FXDIB_Format dest_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
827 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, FX_DWORD*& d_pal, void* pIccTransform)
828 {
829 FXDIB_Format src_format = pSrcBitmap->GetFormat();
830 if (!CFX_GEModule::Get()->GetCodecModule() || !CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) {
831 pIccTransform = NULL;
832 }
833 switch (dest_format) {
834 case FXDIB_Invalid:
835 case FXDIB_1bppCmyk:
836 case FXDIB_1bppMask:
837 case FXDIB_1bppRgb:
838 ASSERT(FALSE);
839 return FALSE;
840 case FXDIB_8bppMask: {
841 if ((src_format & 0xff) == 1) {
842 if (pSrcBitmap->GetPalette()) {
843 return _ConvertBuffer_1bppPlt2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
844 }
845 return _ConvertBuffer_1bppMask2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
846 } else if ((src_format & 0xff) == 8) {
847 if (pSrcBitmap->GetPalette()) {
848 return _ConvertBuffer_8bppPlt2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
849 }
850 return _ConvertBuffer_8bppMask2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
851 } else if ((src_format & 0xff) >= 24) {
852 return _ConvertBuffer_RgbOrCmyk2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
853 }
854 return FALSE;
855 }
856 case FXDIB_8bppRgb:
857 case FXDIB_8bppRgba: {
858 if ((src_format & 0xff) == 8 && pSrcBitmap->GetPalette() == NULL) {
859 return ConvertBuffer(FXDIB_8bppMask, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, d_pal, pIccTransform);
860 }
861 d_pal = FX_Alloc(FX_DWORD, 256);
862 if (((src_format & 0xff) == 1 || (src_format & 0xff) == 8) && pSrcBitmap->GetPalette()) {
863 return _ConvertBuffer_Plt2PltRgb8(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, d_pal, pIccTransform);
864 } else if ((src_format & 0xff) >= 24) {
865 return _ConvertBuffer_Rgb2PltRgb8(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, d_pal, pIccTransform);
866 }
867 return FALSE;
868 }
869 case FXDIB_Rgb:
870 case FXDIB_Rgba: {
871 if ((src_format & 0xff) == 1) {
872 if (pSrcBitmap->GetPalette()) {
873 return _ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
874 }
875 return _ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
876 } else if ((src_format & 0xff) == 8) {
877 if (pSrcBitmap->GetPalette()) {
878 return _ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
879 }
880 return _ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
881 } else if ((src_format & 0xff) == 24) {
882 return _ConvertBuffer_24bppRgb2Rgb24(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
883 } else if ((src_format & 0xff) == 32) {
884 return _ConvertBuffer_32bppRgb2Rgb24(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
885 }
886 return FALSE;
887 }
888 case FXDIB_Argb:
889 case FXDIB_Rgb32: {
890 if ((src_format & 0xff) == 1) {
891 if (pSrcBitmap->GetPalette()) {
892 return _ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
893 }
894 return _ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
895 } else if ((src_format & 0xff) == 8) {
896 if (pSrcBitmap->GetPalette()) {
897 return _ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
898 }
899 return _ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
900 } else if ((src_format & 0xff) >= 24) {
901 if (src_format & 0x0400) {
902 return _ConvertBuffer_32bppCmyk2Rgb32(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
903 }
904 return _ConvertBuffer_Rgb2Rgb32(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
905 }
906 return FALSE;
907 }
908 default:
909 return FALSE;
910 }
911 return FALSE;
912 }
CloneConvert(FXDIB_Format dest_format,const FX_RECT * pClip,void * pIccTransform) const913 CFX_DIBitmap* CFX_DIBSource::CloneConvert(FXDIB_Format dest_format, const FX_RECT* pClip, void* pIccTransform) const
914 {
915 if(dest_format == GetFormat() && pIccTransform == NULL) {
916 return Clone(pClip);
917 }
918 if (pClip) {
919 CFX_DIBitmap* pClone = Clone(pClip);
920 if (pClone == NULL) {
921 return NULL;
922 }
923 if(!pClone->ConvertFormat(dest_format, pIccTransform)) {
924 delete pClone;
925 return NULL;
926 }
927 return pClone;
928 }
929 CFX_DIBitmap* pClone = new CFX_DIBitmap;
930 if(!pClone->Create(m_Width, m_Height, dest_format)) {
931 delete pClone;
932 return NULL;
933 }
934 FX_BOOL ret = TRUE;
935 CFX_DIBitmap* pSrcAlpha = NULL;
936 if (m_AlphaFlag & 2) {
937 pSrcAlpha = (GetFormat() == FXDIB_Argb) ? GetAlphaMask() : m_pAlphaMask;
938 if (pSrcAlpha == NULL) {
939 delete pClone;
940 return NULL;
941 }
942 }
943 if (dest_format & 0x0200) {
944 if (dest_format == FXDIB_Argb)
945 ret = pSrcAlpha ?
946 pClone->LoadChannel(FXDIB_Alpha, pSrcAlpha, FXDIB_Alpha) :
947 pClone->LoadChannel(FXDIB_Alpha, 0xff);
948 else {
949 ret = pClone->CopyAlphaMask(pSrcAlpha);
950 }
951 }
952 if (pSrcAlpha && pSrcAlpha != m_pAlphaMask) {
953 delete pSrcAlpha;
954 pSrcAlpha = NULL;
955 }
956 if (!ret) {
957 delete pClone;
958 return NULL;
959 }
960 FX_DWORD* pal_8bpp = NULL;
961 ret = ConvertBuffer(dest_format, pClone->GetBuffer(), pClone->GetPitch(), m_Width, m_Height, this, 0, 0, pal_8bpp, pIccTransform);
962 if (!ret) {
963 if (pal_8bpp) {
964 FX_Free(pal_8bpp);
965 }
966 delete pClone;
967 return NULL;
968 }
969 if (pal_8bpp) {
970 pClone->CopyPalette(pal_8bpp);
971 FX_Free(pal_8bpp);
972 pal_8bpp = NULL;
973 }
974 return pClone;
975 }
ConvertFormat(FXDIB_Format dest_format,void * pIccTransform)976 FX_BOOL CFX_DIBitmap::ConvertFormat(FXDIB_Format dest_format, void* pIccTransform)
977 {
978 FXDIB_Format src_format = GetFormat();
979 if (dest_format == src_format && pIccTransform == NULL) {
980 return TRUE;
981 }
982 if (dest_format == FXDIB_8bppMask && src_format == FXDIB_8bppRgb && m_pPalette == NULL) {
983 m_AlphaFlag = 1;
984 return TRUE;
985 }
986 if (dest_format == FXDIB_Argb && src_format == FXDIB_Rgb32 && pIccTransform == NULL) {
987 m_AlphaFlag = 2;
988 for (int row = 0; row < m_Height; row ++) {
989 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch + 3;
990 for (int col = 0; col < m_Width; col ++) {
991 *scanline = 0xff;
992 scanline += 4;
993 }
994 }
995 return TRUE;
996 }
997 int dest_bpp = dest_format & 0xff;
998 int dest_pitch = (dest_bpp * m_Width + 31) / 32 * 4;
999 FX_LPBYTE dest_buf = FX_TryAlloc(FX_BYTE, dest_pitch * m_Height + 4);
1000 if (dest_buf == NULL) {
1001 return FALSE;
1002 }
1003 CFX_DIBitmap* pAlphaMask = NULL;
1004 if (dest_format == FXDIB_Argb) {
1005 FXSYS_memset8(dest_buf, 0xff, dest_pitch * m_Height + 4);
1006 if (m_pAlphaMask) {
1007 for (int row = 0; row < m_Height; row ++) {
1008 FX_LPBYTE pDstScanline = dest_buf + row * dest_pitch + 3;
1009 FX_LPCBYTE pSrcScanline = m_pAlphaMask->GetScanline(row);
1010 for (int col = 0; col < m_Width; col ++) {
1011 *pDstScanline = *pSrcScanline++;
1012 pDstScanline += 4;
1013 }
1014 }
1015 }
1016 } else if (dest_format & 0x0200) {
1017 if (src_format == FXDIB_Argb) {
1018 pAlphaMask = GetAlphaMask();
1019 if (pAlphaMask == NULL) {
1020 FX_Free(dest_buf);
1021 return FALSE;
1022 }
1023 } else {
1024 if (m_pAlphaMask == NULL) {
1025 if (!BuildAlphaMask()) {
1026 FX_Free(dest_buf);
1027 return FALSE;
1028 }
1029 pAlphaMask = m_pAlphaMask;
1030 m_pAlphaMask = NULL;
1031 } else {
1032 pAlphaMask = m_pAlphaMask;
1033 }
1034 }
1035 }
1036 FX_BOOL ret = FALSE;
1037 FX_DWORD* pal_8bpp = NULL;
1038 ret = ConvertBuffer(dest_format, dest_buf, dest_pitch, m_Width, m_Height, this, 0, 0, pal_8bpp, pIccTransform);
1039 if (!ret) {
1040 if (pal_8bpp) {
1041 FX_Free(pal_8bpp);
1042 }
1043 if (pAlphaMask != m_pAlphaMask) {
1044 delete pAlphaMask;
1045 }
1046 if (dest_buf) {
1047 FX_Free(dest_buf);
1048 }
1049 return FALSE;
1050 }
1051 if (m_pAlphaMask && pAlphaMask != m_pAlphaMask) {
1052 delete m_pAlphaMask;
1053 }
1054 m_pAlphaMask = pAlphaMask;
1055 if (m_pPalette) {
1056 FX_Free(m_pPalette);
1057 }
1058 m_pPalette = pal_8bpp;
1059 if (!m_bExtBuf) {
1060 FX_Free(m_pBuffer);
1061 }
1062 m_bExtBuf = FALSE;
1063 m_pBuffer = dest_buf;
1064 m_bpp = (FX_BYTE)dest_format;
1065 m_AlphaFlag = (FX_BYTE)(dest_format >> 8);
1066 m_Pitch = dest_pitch;
1067 return TRUE;
1068 }
1069