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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
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
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
100 class CFX_Palette : public CFX_Object
106 FX_BOOL BuildPalette(const CFX_DIBSource* pBitmap, int dwPaletteType);
107 FX_DWORD* GetPalette() const
112 FX_DWORD* GetColorLut()const
116 FX_DWORD* GetAmountLut()const
120 FX_INT32 Getlut()const
125 FX_DWORD* m_pPalette;
130 int _Partition(FX_DWORD* alut, FX_DWORD* clut, int l, int r)
132 FX_DWORD p_a = alut[l];
133 FX_DWORD p_c = clut[l];
135 while(l < r && alut[r] >= p_a) {
142 while(l < r && alut[l] <= p_a) {
154 void _Qsort(FX_DWORD* alut, FX_DWORD* clut, int l, int r)
157 int pI = _Partition(alut, clut, l, r);
158 _Qsort(alut, clut, l, pI - 1);
159 _Qsort(alut, clut, pI + 1, r);
162 void _ColorDecode(FX_DWORD pal_v, FX_BYTE& r, FX_BYTE& g, FX_BYTE& b)
164 r = (FX_BYTE)((pal_v & 0xf00) >> 4);
165 g = (FX_BYTE)(pal_v & 0x0f0);
166 b = (FX_BYTE)((pal_v & 0x00f) << 4);
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)
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) {
178 FX_DWORD color = cLut[lut_offset];
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;
185 for (row = 0; row < 256; row++) {
186 int lut_offset = lut_1 - row;
187 if (lut_offset < 0) {
191 _ColorDecode(cLut[lut_offset], r, g, b);
192 int error, min_error = 1000000;
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) {
205 dest_pal[row] = win_mac_pal[c_index];
206 aLut[lut_offset] = row;
210 CFX_Palette::CFX_Palette()
217 CFX_Palette::~CFX_Palette()
230 FX_BOOL CFX_Palette::BuildPalette(const CFX_DIBSource* pBitmap, int pal_type)
232 if (pBitmap == NULL) {
235 if (m_pPalette != NULL) {
238 m_pPalette = FX_Alloc(FX_DWORD, 256);
242 int bpp = pBitmap->GetBPP() / 8;
243 int width = pBitmap->GetWidth();
244 int height = pBitmap->GetHeight();
253 m_cLut = FX_Alloc(FX_DWORD, 4096);
257 m_aLut = FX_Alloc(FX_DWORD, 4096);
263 for (row = 0; row < height; row++) {
264 FX_BYTE* scan_line = (FX_BYTE*)pBitmap->GetScanline(row);
265 for (col = 0; col < width; col++) {
266 FX_BYTE* src_port = scan_line + col * bpp;
267 FX_DWORD b = src_port[0] & 0xf0;
268 FX_DWORD g = src_port[1] & 0xf0;
269 FX_DWORD r = src_port[2] & 0xf0;
270 FX_DWORD index = (r << 4) + g + (b >> 4);
274 for (row = 0; row < 4096; row++) {
275 if (m_aLut[row] != 0) {
276 m_aLut[m_lut] = m_aLut[row];
281 _Qsort(m_aLut, m_cLut, 0, m_lut - 1);
282 FX_DWORD* win_mac_pal = NULL;
283 if (pal_type == FXDIB_PALETTE_WIN) {
284 win_mac_pal = (FX_DWORD*)g_dwWinPalette;
285 } else if (pal_type == FXDIB_PALETTE_MAC) {
286 win_mac_pal = (FX_DWORD*)g_dwMacPalette;
288 _Obtain_Pal(m_aLut, m_cLut, m_pPalette, pal_type, win_mac_pal, m_lut);
291 FX_BOOL _ConvertBuffer_1bppMask2Gray(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
292 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
294 FX_BYTE set_gray, reset_gray;
297 for (int row = 0; row < height; row ++) {
298 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
299 FXSYS_memset8(dest_scan, reset_gray, width);
300 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
301 for (int col = src_left; col < src_left + width; col ++) {
302 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
303 *dest_scan = set_gray;
310 FX_BOOL _ConvertBuffer_8bppMask2Gray(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
311 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
313 for (int row = 0; row < height; row ++) {
314 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
315 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
316 FXSYS_memcpy32(dest_scan, src_scan, width);
320 FX_BOOL _ConvertBuffer_1bppPlt2Gray(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
321 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
323 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
327 if (pSrcBitmap->IsCmykImage()) {
328 plt[0] = FXCMYK_TODIB(src_plt[0]);
329 plt[1] = FXCMYK_TODIB(src_plt[1]);
331 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
332 bgr_ptr[0] = FXARGB_B(src_plt[0]);
333 bgr_ptr[1] = FXARGB_G(src_plt[0]);
334 bgr_ptr[2] = FXARGB_R(src_plt[0]);
335 bgr_ptr[3] = FXARGB_B(src_plt[1]);
336 bgr_ptr[4] = FXARGB_G(src_plt[1]);
337 bgr_ptr[5] = FXARGB_R(src_plt[1]);
339 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
340 pIccModule->TranslateScanline(pIccTransform, gray, (FX_LPCBYTE)plt, 2);
342 FX_BYTE reset_r, reset_g, reset_b,
344 if (pSrcBitmap->IsCmykImage()) {
345 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0]),
346 reset_r, reset_g, reset_b);
347 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1]),
348 set_r, set_g, set_b);
350 reset_r = FXARGB_R(src_plt[0]);
351 reset_g = FXARGB_G(src_plt[0]);
352 reset_b = FXARGB_B(src_plt[0]);
353 set_r = FXARGB_R(src_plt[1]);
354 set_g = FXARGB_G(src_plt[1]);
355 set_b = FXARGB_B(src_plt[1]);
357 gray[0] = FXRGB2GRAY(reset_r, reset_g, reset_b);
358 gray[1] = FXRGB2GRAY(set_r, set_g, set_b);
360 for (int row = 0; row < height; row ++) {
361 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
362 FXSYS_memset8(dest_scan, gray[0], width);
363 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
364 for (int col = src_left; col < src_left + width; col ++) {
365 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
366 *dest_scan = gray[1];
373 FX_BOOL _ConvertBuffer_8bppPlt2Gray(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
374 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
376 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
380 if (pSrcBitmap->IsCmykImage()) {
381 for (int i = 0; i < 256; i ++) {
382 plt[i] = FXCMYK_TODIB(src_plt[i]);
385 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
386 for (int i = 0; i < 256; i ++) {
387 *bgr_ptr++ = FXARGB_B(src_plt[i]);
388 *bgr_ptr++ = FXARGB_G(src_plt[i]);
389 *bgr_ptr++ = FXARGB_R(src_plt[i]);
392 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
393 pIccModule->TranslateScanline(pIccTransform, gray, (FX_LPCBYTE)plt, 256);
395 if (pSrcBitmap->IsCmykImage()) {
397 for (int i = 0; i < 256; i ++) {
398 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]),
400 gray[i] = FXRGB2GRAY(r, g, b);
403 for (int i = 0; i < 256; i ++) {
404 gray[i] = FXRGB2GRAY(FXARGB_R(src_plt[i]), FXARGB_G(src_plt[i]), FXARGB_B(src_plt[i]));
407 for (int row = 0; row < height; row ++) {
408 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
409 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
410 for (int col = 0; col < width; col ++) {
411 *dest_scan++ = gray[*src_scan++];
416 FX_BOOL _ConvertBuffer_RgbOrCmyk2Gray(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
417 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
419 int Bpp = pSrcBitmap->GetBPP() / 8;
421 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
422 if (Bpp == 3 || pSrcBitmap->IsCmykImage()) {
423 for (int row = 0; row < height; row ++) {
424 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
425 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
426 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, width);
429 for (int row = 0; row < height; row ++) {
430 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
431 FX_LPCBYTE src_scan = 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);
440 if (pSrcBitmap->IsCmykImage()) {
441 for (int row = 0; row < height; row ++) {
442 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
443 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
444 for (int col = 0; col < width; col ++) {
446 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]),
448 *dest_scan++ = FXRGB2GRAY(r, g, b);
453 for (int row = 0; row < height; row ++) {
454 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
455 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
456 for (int col = 0; col < width; col ++) {
457 *dest_scan++ = FXRGB2GRAY(src_scan[2], src_scan[1], src_scan[0]);
465 inline void _ConvertBuffer_IndexCopy(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
466 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
468 if (pSrcBitmap->GetBPP() == 1) {
469 for (int row = 0; row < height; row ++) {
470 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
471 FXSYS_memset32(dest_scan, 0, width);
472 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
473 for (int col = src_left; col < src_left + width; col ++) {
474 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
481 for (int row = 0; row < height; row ++) {
482 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
483 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
484 FXSYS_memcpy32(dest_scan, src_scan, width);
488 FX_BOOL _ConvertBuffer_Plt2PltRgb8(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
489 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, FX_DWORD* dst_plt, void* pIccTransform)
491 _ConvertBuffer_IndexCopy(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
492 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
493 int plt_size = pSrcBitmap->GetPaletteSize();
496 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
497 if (pSrcBitmap->IsCmykImage()) {
498 for (int i = 0; i < plt_size; i ++) {
499 plt[i] = FXCMYK_TODIB(src_plt[i]);
502 for (int i = 0; i < plt_size; i ++) {
503 *bgr_ptr++ = FXARGB_B(src_plt[i]);
504 *bgr_ptr++ = FXARGB_G(src_plt[i]);
505 *bgr_ptr++ = FXARGB_R(src_plt[i]);
507 bgr_ptr = (FX_LPBYTE)plt;
509 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
510 pIccModule->TranslateScanline(pIccTransform, (FX_LPBYTE)plt, (FX_LPCBYTE)plt, plt_size);
511 for (int i = 0; i < plt_size; i ++) {
512 dst_plt[i] = FXARGB_MAKE(0xff, bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
516 if (pSrcBitmap->IsCmykImage()) {
517 for (int i = 0; i < plt_size; i ++) {
519 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]),
521 dst_plt[i] = FXARGB_MAKE(0xff, r, g, b);
524 FXSYS_memcpy32(dst_plt, src_plt, plt_size * 4);
529 inline FX_BOOL _ConvertBuffer_Rgb2PltRgb8_NoTransform(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
530 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, FX_DWORD* dst_plt)
532 int bpp = pSrcBitmap->GetBPP() / 8;
535 palette.BuildPalette(pSrcBitmap, FXDIB_PALETTE_LOC);
536 FX_DWORD* cLut = palette.GetColorLut();
537 FX_DWORD* aLut = palette.GetAmountLut();
538 if (cLut == NULL || aLut == NULL) {
541 int lut = palette.Getlut();
542 FX_DWORD* pPalette = palette.GetPalette();
545 int lut_256 = lut - 256;
546 for (row = 0; row < lut_256; row++) {
549 _ColorDecode(cLut[row], r, g, b);
551 for (int col = 0; col < 256; col++) {
552 FX_DWORD p_color = *(pPalette + col);
553 int d_r = r - (FX_BYTE)(p_color >> 16);
554 int d_g = g - (FX_BYTE)(p_color >> 8);
555 int d_b = b - (FX_BYTE)(p_color);
556 err = d_r * d_r + d_g * d_g + d_b * d_b;
562 aLut[row] = clrindex;
565 FX_INT32 lut_1 = lut - 1;
566 for (row = 0; row < height; row ++) {
567 FX_BYTE* src_scan = (FX_BYTE*)pSrcBitmap->GetScanline(src_top + row) + src_left;
568 FX_BYTE* dest_scan = dest_buf + row * dest_pitch;
569 for (col = 0; col < width; col++) {
570 FX_BYTE* src_port = src_scan + col * bpp;
571 int r = src_port[2] & 0xf0;
572 int g = src_port[1] & 0xf0;
573 int b = src_port[0] & 0xf0;
574 FX_DWORD clrindex = (r << 4) + g + (b >> 4);
575 for (int i = lut_1; i >= 0; i--)
576 if (clrindex == cLut[i]) {
577 *(dest_scan + col) = (FX_BYTE)(aLut[i]);
582 FXSYS_memcpy32(dst_plt, pPalette, sizeof(FX_DWORD) * 256);
585 FX_BOOL _ConvertBuffer_Rgb2PltRgb8(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
586 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, FX_DWORD* dst_plt, void* pIccTransform)
588 ICodec_IccModule* pIccModule = NULL;
590 pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
592 FX_BOOL ret = _ConvertBuffer_Rgb2PltRgb8_NoTransform(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, dst_plt);
593 if (ret && pIccTransform) {
594 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
595 for (int i = 0; i < 256; i++) {
596 FX_ARGB* plt = dst_plt + i;
597 FX_ARGB plt_entry = FXARGB_TODIB(*plt);
598 pIccModule->TranslateScanline(pIccTransform, (FX_LPBYTE)&plt_entry, (FX_LPCBYTE)&plt_entry, 1);
599 *plt = FXARGB_TODIB(plt_entry);
604 FX_BOOL _ConvertBuffer_1bppMask2Rgb(FXDIB_Format dst_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
605 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
607 int comps = (dst_format & 0xff) / 8;
608 FX_BYTE set_gray, reset_gray;
611 for (int row = 0; row < height; row ++) {
612 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
613 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
614 for (int col = src_left; col < src_left + width; col ++) {
615 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
616 dest_scan[0] = set_gray;
617 dest_scan[1] = set_gray;
618 dest_scan[2] = set_gray;
620 dest_scan[0] = reset_gray;
621 dest_scan[1] = reset_gray;
622 dest_scan[2] = reset_gray;
629 FX_BOOL _ConvertBuffer_8bppMask2Rgb(FXDIB_Format dst_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
630 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top)
632 int comps = (dst_format & 0xff) / 8;
633 for (int row = 0; row < height; row ++) {
634 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
635 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
637 for (int col = 0; col < width; col ++) {
638 src_pixel = *src_scan++;
639 *dest_scan++ = src_pixel;
640 *dest_scan++ = src_pixel;
641 *dest_scan = src_pixel;
642 dest_scan += comps - 2;
647 FX_BOOL _ConvertBuffer_1bppPlt2Rgb(FXDIB_Format dst_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
648 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
650 int comps = (dst_format & 0xff) / 8;
651 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
653 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
654 if (pSrcBitmap->IsCmykImage()) {
655 plt[0] = FXCMYK_TODIB(src_plt[0]);
656 plt[1] = FXCMYK_TODIB(src_plt[1]);
658 bgr_ptr[0] = FXARGB_B(src_plt[0]);
659 bgr_ptr[1] = FXARGB_G(src_plt[0]);
660 bgr_ptr[2] = FXARGB_R(src_plt[0]);
661 bgr_ptr[3] = FXARGB_B(src_plt[1]);
662 bgr_ptr[4] = FXARGB_G(src_plt[1]);
663 bgr_ptr[5] = FXARGB_R(src_plt[1]);
666 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
667 pIccModule->TranslateScanline(pIccTransform, (FX_LPBYTE)plt, (FX_LPCBYTE)plt, 2);
669 if (pSrcBitmap->IsCmykImage()) {
670 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0]),
671 bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
672 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1]),
673 bgr_ptr[5], bgr_ptr[4], bgr_ptr[3]);
676 for (int row = 0; row < height; row ++) {
677 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
678 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
679 for (int col = src_left; col < src_left + width; col ++) {
680 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
681 *dest_scan++ = bgr_ptr[3];
682 *dest_scan++ = bgr_ptr[4];
683 *dest_scan = bgr_ptr[5];
685 *dest_scan++ = bgr_ptr[0];
686 *dest_scan++ = bgr_ptr[1];
687 *dest_scan = bgr_ptr[2];
689 dest_scan += comps - 2;
694 FX_BOOL _ConvertBuffer_8bppPlt2Rgb(FXDIB_Format dst_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
695 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
697 int comps = (dst_format & 0xff) / 8;
698 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
700 FX_LPBYTE bgr_ptr = (FX_LPBYTE)plt;
701 if (!pSrcBitmap->IsCmykImage()) {
702 for (int i = 0; i < 256; i++) {
703 *bgr_ptr++ = FXARGB_B(src_plt[i]);
704 *bgr_ptr++ = FXARGB_G(src_plt[i]);
705 *bgr_ptr++ = FXARGB_R(src_plt[i]);
707 bgr_ptr = (FX_LPBYTE)plt;
710 if (pSrcBitmap->IsCmykImage()) {
711 for (int i = 0; i < 256; i++) {
712 plt[i] = FXCMYK_TODIB(src_plt[i]);
715 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
716 pIccModule->TranslateScanline(pIccTransform, (FX_LPBYTE)plt, (FX_LPCBYTE)plt, 256);
718 if (pSrcBitmap->IsCmykImage()) {
719 for (int i = 0; i < 256; i++) {
720 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]),
721 bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
724 bgr_ptr = (FX_LPBYTE)plt;
727 for (int row = 0; row < height; row ++) {
728 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
729 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
730 for (int col = 0; col < width; col ++) {
731 FX_LPBYTE src_pixel = bgr_ptr + 3 * (*src_scan++);
732 *dest_scan++ = *src_pixel++;
733 *dest_scan++ = *src_pixel++;
734 *dest_scan = *src_pixel++;
735 dest_scan += comps - 2;
740 FX_BOOL _ConvertBuffer_24bppRgb2Rgb24(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
741 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
744 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
745 for (int row = 0; row < height; row ++) {
746 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
747 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 3;
748 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, width);
751 for (int row = 0; row < height; row ++) {
752 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
753 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 3;
754 FXSYS_memcpy32(dest_scan, src_scan, width * 3);
759 FX_BOOL _ConvertBuffer_32bppRgb2Rgb24(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
760 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
762 for (int row = 0; row < height; row ++) {
763 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
764 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
765 for (int col = 0; col < width; col ++) {
766 *dest_scan++ = *src_scan++;
767 *dest_scan++ = *src_scan++;
768 *dest_scan++ = *src_scan++;
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 pIccModule->TranslateScanline(pIccTransform, dest_scan, dest_scan, width);
781 FX_BOOL _ConvertBuffer_Rgb2Rgb32(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
782 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
784 int comps = pSrcBitmap->GetBPP() / 8;
786 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
787 for (int row = 0; row < height; row ++) {
788 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
789 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * comps;
790 for (int col = 0; col < width; col ++) {
791 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
797 for (int row = 0; row < height; row ++) {
798 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
799 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * comps;
800 for (int col = 0; col < width; col ++) {
801 *dest_scan++ = *src_scan++;
802 *dest_scan++ = *src_scan++;
803 *dest_scan++ = *src_scan++;
805 src_scan += comps - 3;
811 FX_BOOL _ConvertBuffer_32bppCmyk2Rgb32(FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
812 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
815 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
816 for (int row = 0; row < height; row ++) {
817 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
818 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
819 for (int col = 0; col < width; col ++) {
820 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
826 for (int row = 0; row < height; row ++) {
827 FX_LPBYTE dest_scan = dest_buf + row * dest_pitch;
828 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
829 for (int col = 0; col < width; col ++) {
830 AdobeCMYK_to_sRGB1(src_scan[0], src_scan[1], src_scan[2], src_scan[3],
831 dest_scan[2], dest_scan[1], dest_scan[0]);
839 FX_BOOL ConvertBuffer(FXDIB_Format dest_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
840 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, FX_DWORD*& d_pal, void* pIccTransform)
842 FXDIB_Format src_format = pSrcBitmap->GetFormat();
843 if (!CFX_GEModule::Get()->GetCodecModule() || !CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) {
844 pIccTransform = NULL;
846 switch (dest_format) {
853 case FXDIB_8bppMask: {
854 if ((src_format & 0xff) == 1) {
855 if (pSrcBitmap->GetPalette()) {
856 return _ConvertBuffer_1bppPlt2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
858 return _ConvertBuffer_1bppMask2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
859 } else if ((src_format & 0xff) == 8) {
860 if (pSrcBitmap->GetPalette()) {
861 return _ConvertBuffer_8bppPlt2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
863 return _ConvertBuffer_8bppMask2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
864 } else if ((src_format & 0xff) >= 24) {
865 return _ConvertBuffer_RgbOrCmyk2Gray(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
870 case FXDIB_8bppRgba: {
871 if ((src_format & 0xff) == 8 && pSrcBitmap->GetPalette() == NULL) {
872 return ConvertBuffer(FXDIB_8bppMask, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, d_pal, pIccTransform);
874 d_pal = FX_Alloc(FX_DWORD, 256);
878 if (((src_format & 0xff) == 1 || (src_format & 0xff) == 8) && pSrcBitmap->GetPalette()) {
879 return _ConvertBuffer_Plt2PltRgb8(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, d_pal, pIccTransform);
880 } else if ((src_format & 0xff) >= 24) {
881 return _ConvertBuffer_Rgb2PltRgb8(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, d_pal, pIccTransform);
887 if ((src_format & 0xff) == 1) {
888 if (pSrcBitmap->GetPalette()) {
889 return _ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
891 return _ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
892 } else if ((src_format & 0xff) == 8) {
893 if (pSrcBitmap->GetPalette()) {
894 return _ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
896 return _ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
897 } else if ((src_format & 0xff) == 24) {
898 return _ConvertBuffer_24bppRgb2Rgb24(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
899 } else if ((src_format & 0xff) == 32) {
900 return _ConvertBuffer_32bppRgb2Rgb24(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
906 if ((src_format & 0xff) == 1) {
907 if (pSrcBitmap->GetPalette()) {
908 return _ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
910 return _ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
911 } else if ((src_format & 0xff) == 8) {
912 if (pSrcBitmap->GetPalette()) {
913 return _ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
915 return _ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top);
916 } else if ((src_format & 0xff) >= 24) {
917 if (src_format & 0x0400) {
918 return _ConvertBuffer_32bppCmyk2Rgb32(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
920 return _ConvertBuffer_Rgb2Rgb32(dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top, pIccTransform);
929 CFX_DIBitmap* CFX_DIBSource::CloneConvert(FXDIB_Format dest_format, const FX_RECT* pClip, void* pIccTransform) const
931 if(dest_format == GetFormat() && pIccTransform == NULL) {
935 CFX_DIBitmap* pClone = Clone(pClip);
936 if (pClone == NULL) {
939 if(!pClone->ConvertFormat(dest_format, pIccTransform)) {
945 CFX_DIBitmap* pClone = FX_NEW CFX_DIBitmap;
949 if(!pClone->Create(m_Width, m_Height, dest_format)) {
954 CFX_DIBitmap* pSrcAlpha = NULL;
955 if (m_AlphaFlag & 2) {
956 pSrcAlpha = (GetFormat() == FXDIB_Argb) ? GetAlphaMask() : m_pAlphaMask;
957 if (pSrcAlpha == NULL) {
962 if (dest_format & 0x0200) {
963 if (dest_format == FXDIB_Argb)
965 pClone->LoadChannel(FXDIB_Alpha, pSrcAlpha, FXDIB_Alpha) :
966 pClone->LoadChannel(FXDIB_Alpha, 0xff);
968 ret = pClone->CopyAlphaMask(pSrcAlpha);
971 if (pSrcAlpha && pSrcAlpha != m_pAlphaMask) {
979 FX_DWORD* pal_8bpp = NULL;
980 ret = ConvertBuffer(dest_format, pClone->GetBuffer(), pClone->GetPitch(), m_Width, m_Height, this, 0, 0, pal_8bpp, pIccTransform);
989 pClone->CopyPalette(pal_8bpp);
995 FX_BOOL CFX_DIBitmap::ConvertFormat(FXDIB_Format dest_format, void* pIccTransform)
997 FXDIB_Format src_format = GetFormat();
998 if (dest_format == src_format && pIccTransform == NULL) {
1001 if (dest_format == FXDIB_8bppMask && src_format == FXDIB_8bppRgb && m_pPalette == NULL) {
1005 if (dest_format == FXDIB_Argb && src_format == FXDIB_Rgb32 && pIccTransform == NULL) {
1007 for (int row = 0; row < m_Height; row ++) {
1008 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch + 3;
1009 for (int col = 0; col < m_Width; col ++) {
1016 int dest_bpp = dest_format & 0xff;
1017 int dest_pitch = (dest_bpp * m_Width + 31) / 32 * 4;
1018 FX_LPBYTE dest_buf = FX_AllocNL(FX_BYTE, dest_pitch * m_Height + 4);
1019 if (dest_buf == NULL) {
1022 CFX_DIBitmap* pAlphaMask = NULL;
1023 if (dest_format == FXDIB_Argb) {
1024 FXSYS_memset8(dest_buf, 0xff, dest_pitch * m_Height + 4);
1026 for (int row = 0; row < m_Height; row ++) {
1027 FX_LPBYTE pDstScanline = dest_buf + row * dest_pitch + 3;
1028 FX_LPCBYTE pSrcScanline = m_pAlphaMask->GetScanline(row);
1029 for (int col = 0; col < m_Width; col ++) {
1030 *pDstScanline = *pSrcScanline++;
1035 } else if (dest_format & 0x0200) {
1036 if (src_format == FXDIB_Argb) {
1037 pAlphaMask = GetAlphaMask();
1038 if (pAlphaMask == NULL) {
1043 if (m_pAlphaMask == NULL) {
1044 if (!BuildAlphaMask()) {
1048 pAlphaMask = m_pAlphaMask;
1049 m_pAlphaMask = NULL;
1051 pAlphaMask = m_pAlphaMask;
1055 FX_BOOL ret = FALSE;
1056 FX_DWORD* pal_8bpp = NULL;
1057 ret = ConvertBuffer(dest_format, dest_buf, dest_pitch, m_Width, m_Height, this, 0, 0, pal_8bpp, pIccTransform);
1062 if (pAlphaMask != m_pAlphaMask) {
1070 if (m_pAlphaMask && pAlphaMask != m_pAlphaMask) {
1071 delete m_pAlphaMask;
1073 m_pAlphaMask = pAlphaMask;
1075 FX_Free(m_pPalette);
1077 m_pPalette = pal_8bpp;
1082 m_pBuffer = dest_buf;
1083 m_bpp = (FX_BYTE)dest_format;
1084 m_AlphaFlag = (FX_BYTE)(dest_format >> 8);
1085 m_Pitch = dest_pitch;