Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: core/src/fxge/dib/fx_dib_convert.cpp

Issue 1800523005: Move core/src/ up to core/. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/src/fxge/dib/fx_dib_composite.cpp ('k') | core/src/fxge/dib/fx_dib_engine.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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);
106 FX_DWORD* GetPalette() const { return m_pPalette; }
107
108 FX_DWORD* GetColorLut() const { return m_cLut; }
109 FX_DWORD* GetAmountLut() const { return m_aLut; }
110 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 };
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 }
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 }
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 }
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 }
199 CFX_Palette::CFX_Palette() {
200 m_pPalette = NULL;
201 m_cLut = NULL;
202 m_aLut = NULL;
203 m_lut = 0;
204 }
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 }
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 }
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 }
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 }
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 }
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 }
399 for (int row = 0; row < height; row++) {
400 uint8_t* dest_scan = dest_buf + row * dest_pitch;
401 const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
402 for (int col = 0; col < width; col++) {
403 *dest_scan++ = gray[*src_scan++];
404 }
405 }
406 return TRUE;
407 }
408 FX_BOOL ConvertBuffer_RgbOrCmyk2Gray(uint8_t* dest_buf,
409 int dest_pitch,
410 int width,
411 int height,
412 const CFX_DIBSource* pSrcBitmap,
413 int src_left,
414 int src_top,
415 void* pIccTransform) {
416 int Bpp = pSrcBitmap->GetBPP() / 8;
417 if (pIccTransform) {
418 ICodec_IccModule* pIccModule =
419 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
420 if (Bpp == 3 || pSrcBitmap->IsCmykImage()) {
421 for (int row = 0; row < height; row++) {
422 uint8_t* dest_scan = dest_buf + row * dest_pitch;
423 const uint8_t* src_scan =
424 pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
425 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan,
426 width);
427 }
428 } else {
429 for (int row = 0; row < height; row++) {
430 uint8_t* dest_scan = dest_buf + row * dest_pitch;
431 const uint8_t* src_scan =
432 pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
433 for (int col = 0; col < width; col++) {
434 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
435 dest_scan++;
436 src_scan += 4;
437 }
438 }
439 }
440 } else {
441 if (pSrcBitmap->IsCmykImage()) {
442 for (int row = 0; row < height; row++) {
443 uint8_t* dest_scan = dest_buf + row * dest_pitch;
444 const uint8_t* src_scan =
445 pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
446 for (int col = 0; col < width; col++) {
447 uint8_t r, g, b;
448 AdobeCMYK_to_sRGB1(FXSYS_GetCValue((FX_DWORD)src_scan[0]),
449 FXSYS_GetMValue((FX_DWORD)src_scan[1]),
450 FXSYS_GetYValue((FX_DWORD)src_scan[2]),
451 FXSYS_GetKValue((FX_DWORD)src_scan[3]), r, g, b);
452 *dest_scan++ = FXRGB2GRAY(r, g, b);
453 src_scan += 4;
454 }
455 }
456 } else {
457 for (int row = 0; row < height; row++) {
458 uint8_t* dest_scan = dest_buf + row * dest_pitch;
459 const uint8_t* src_scan =
460 pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
461 for (int col = 0; col < width; col++) {
462 *dest_scan++ = FXRGB2GRAY(src_scan[2], src_scan[1], src_scan[0]);
463 src_scan += Bpp;
464 }
465 }
466 }
467 }
468 return TRUE;
469 }
470 inline void ConvertBuffer_IndexCopy(uint8_t* dest_buf,
471 int dest_pitch,
472 int width,
473 int height,
474 const CFX_DIBSource* pSrcBitmap,
475 int src_left,
476 int src_top) {
477 if (pSrcBitmap->GetBPP() == 1) {
478 for (int row = 0; row < height; row++) {
479 uint8_t* dest_scan = dest_buf + row * dest_pitch;
480 FXSYS_memset(dest_scan, 0, width);
481 const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row);
482 for (int col = src_left; col < src_left + width; col++) {
483 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
484 *dest_scan = 1;
485 }
486 dest_scan++;
487 }
488 }
489 } else {
490 for (int row = 0; row < height; row++) {
491 uint8_t* dest_scan = dest_buf + row * dest_pitch;
492 const uint8_t* src_scan =
493 pSrcBitmap->GetScanline(src_top + row) + src_left;
494 FXSYS_memcpy(dest_scan, src_scan, width);
495 }
496 }
497 }
498 FX_BOOL ConvertBuffer_Plt2PltRgb8(uint8_t* dest_buf,
499 int dest_pitch,
500 int width,
501 int height,
502 const CFX_DIBSource* pSrcBitmap,
503 int src_left,
504 int src_top,
505 FX_DWORD* dst_plt,
506 void* pIccTransform) {
507 ConvertBuffer_IndexCopy(dest_buf, dest_pitch, width, height, pSrcBitmap,
508 src_left, src_top);
509 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
510 int plt_size = pSrcBitmap->GetPaletteSize();
511 if (pIccTransform) {
512 FX_DWORD plt[256];
513 uint8_t* bgr_ptr = (uint8_t*)plt;
514 if (pSrcBitmap->IsCmykImage()) {
515 for (int i = 0; i < plt_size; i++) {
516 plt[i] = FXCMYK_TODIB(src_plt[i]);
517 }
518 } else {
519 for (int i = 0; i < plt_size; i++) {
520 *bgr_ptr++ = FXARGB_B(src_plt[i]);
521 *bgr_ptr++ = FXARGB_G(src_plt[i]);
522 *bgr_ptr++ = FXARGB_R(src_plt[i]);
523 }
524 bgr_ptr = (uint8_t*)plt;
525 }
526 ICodec_IccModule* pIccModule =
527 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
528 pIccModule->TranslateScanline(pIccTransform, (uint8_t*)plt,
529 (const uint8_t*)plt, plt_size);
530 for (int i = 0; i < plt_size; i++) {
531 dst_plt[i] = FXARGB_MAKE(0xff, bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
532 bgr_ptr += 3;
533 }
534 } else {
535 if (pSrcBitmap->IsCmykImage()) {
536 for (int i = 0; i < plt_size; i++) {
537 uint8_t r, g, b;
538 AdobeCMYK_to_sRGB1(
539 FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]),
540 FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]), r, g, b);
541 dst_plt[i] = FXARGB_MAKE(0xff, r, g, b);
542 }
543 } else {
544 FXSYS_memcpy(dst_plt, src_plt, plt_size * 4);
545 }
546 }
547 return TRUE;
548 }
549 FX_BOOL ConvertBuffer_Rgb2PltRgb8_NoTransform(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 }
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 FX_BOOL ret = ConvertBuffer_Rgb2PltRgb8_NoTransform(
621 dest_buf, dest_pitch, width, height, pSrcBitmap, src_left, src_top,
622 dst_plt);
623 if (ret && pIccTransform) {
624 ICodec_IccModule* pIccModule =
625 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
626 for (int i = 0; i < 256; i++) {
627 FX_ARGB* plt = dst_plt + i;
628 FX_ARGB plt_entry = FXARGB_TODIB(*plt);
629 pIccModule->TranslateScanline(pIccTransform, (uint8_t*)&plt_entry,
630 (const uint8_t*)&plt_entry, 1);
631 *plt = FXARGB_TODIB(plt_entry);
632 }
633 }
634 return ret;
635 }
636 FX_BOOL ConvertBuffer_1bppMask2Rgb(FXDIB_Format dst_format,
637 uint8_t* dest_buf,
638 int dest_pitch,
639 int width,
640 int height,
641 const CFX_DIBSource* pSrcBitmap,
642 int src_left,
643 int src_top) {
644 int comps = (dst_format & 0xff) / 8;
645 uint8_t set_gray, reset_gray;
646 set_gray = 0xff;
647 reset_gray = 0x00;
648 for (int row = 0; row < height; row++) {
649 uint8_t* dest_scan = dest_buf + row * dest_pitch;
650 const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row);
651 for (int col = src_left; col < src_left + width; col++) {
652 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
653 dest_scan[0] = set_gray;
654 dest_scan[1] = set_gray;
655 dest_scan[2] = set_gray;
656 } else {
657 dest_scan[0] = reset_gray;
658 dest_scan[1] = reset_gray;
659 dest_scan[2] = reset_gray;
660 }
661 dest_scan += comps;
662 }
663 }
664 return TRUE;
665 }
666 FX_BOOL ConvertBuffer_8bppMask2Rgb(FXDIB_Format dst_format,
667 uint8_t* dest_buf,
668 int dest_pitch,
669 int width,
670 int height,
671 const CFX_DIBSource* pSrcBitmap,
672 int src_left,
673 int src_top) {
674 int comps = (dst_format & 0xff) / 8;
675 for (int row = 0; row < height; row++) {
676 uint8_t* dest_scan = dest_buf + row * dest_pitch;
677 const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
678 uint8_t src_pixel;
679 for (int col = 0; col < width; col++) {
680 src_pixel = *src_scan++;
681 *dest_scan++ = src_pixel;
682 *dest_scan++ = src_pixel;
683 *dest_scan = src_pixel;
684 dest_scan += comps - 2;
685 }
686 }
687 return TRUE;
688 }
689 FX_BOOL ConvertBuffer_1bppPlt2Rgb(FXDIB_Format dst_format,
690 uint8_t* dest_buf,
691 int dest_pitch,
692 int width,
693 int height,
694 const CFX_DIBSource* pSrcBitmap,
695 int src_left,
696 int src_top,
697 void* pIccTransform) {
698 int comps = (dst_format & 0xff) / 8;
699 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
700 FX_DWORD plt[2];
701 uint8_t* bgr_ptr = (uint8_t*)plt;
702 if (pSrcBitmap->IsCmykImage()) {
703 plt[0] = FXCMYK_TODIB(src_plt[0]);
704 plt[1] = FXCMYK_TODIB(src_plt[1]);
705 } else {
706 bgr_ptr[0] = FXARGB_B(src_plt[0]);
707 bgr_ptr[1] = FXARGB_G(src_plt[0]);
708 bgr_ptr[2] = FXARGB_R(src_plt[0]);
709 bgr_ptr[3] = FXARGB_B(src_plt[1]);
710 bgr_ptr[4] = FXARGB_G(src_plt[1]);
711 bgr_ptr[5] = FXARGB_R(src_plt[1]);
712 }
713 if (pIccTransform) {
714 ICodec_IccModule* pIccModule =
715 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
716 pIccModule->TranslateScanline(pIccTransform, (uint8_t*)plt,
717 (const uint8_t*)plt, 2);
718 } else {
719 if (pSrcBitmap->IsCmykImage()) {
720 AdobeCMYK_to_sRGB1(
721 FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]),
722 FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0]), bgr_ptr[2],
723 bgr_ptr[1], bgr_ptr[0]);
724 AdobeCMYK_to_sRGB1(
725 FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]),
726 FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1]), bgr_ptr[5],
727 bgr_ptr[4], bgr_ptr[3]);
728 }
729 }
730 for (int row = 0; row < height; row++) {
731 uint8_t* dest_scan = dest_buf + row * dest_pitch;
732 const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row);
733 for (int col = src_left; col < src_left + width; col++) {
734 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
735 *dest_scan++ = bgr_ptr[3];
736 *dest_scan++ = bgr_ptr[4];
737 *dest_scan = bgr_ptr[5];
738 } else {
739 *dest_scan++ = bgr_ptr[0];
740 *dest_scan++ = bgr_ptr[1];
741 *dest_scan = bgr_ptr[2];
742 }
743 dest_scan += comps - 2;
744 }
745 }
746 return TRUE;
747 }
748 FX_BOOL ConvertBuffer_8bppPlt2Rgb(FXDIB_Format dst_format,
749 uint8_t* dest_buf,
750 int dest_pitch,
751 int width,
752 int height,
753 const CFX_DIBSource* pSrcBitmap,
754 int src_left,
755 int src_top,
756 void* pIccTransform) {
757 int comps = (dst_format & 0xff) / 8;
758 FX_DWORD* src_plt = pSrcBitmap->GetPalette();
759 FX_DWORD plt[256];
760 uint8_t* bgr_ptr = (uint8_t*)plt;
761 if (!pSrcBitmap->IsCmykImage()) {
762 for (int i = 0; i < 256; i++) {
763 *bgr_ptr++ = FXARGB_B(src_plt[i]);
764 *bgr_ptr++ = FXARGB_G(src_plt[i]);
765 *bgr_ptr++ = FXARGB_R(src_plt[i]);
766 }
767 bgr_ptr = (uint8_t*)plt;
768 }
769 if (pIccTransform) {
770 if (pSrcBitmap->IsCmykImage()) {
771 for (int i = 0; i < 256; i++) {
772 plt[i] = FXCMYK_TODIB(src_plt[i]);
773 }
774 }
775 ICodec_IccModule* pIccModule =
776 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
777 pIccModule->TranslateScanline(pIccTransform, (uint8_t*)plt,
778 (const uint8_t*)plt, 256);
779 } else {
780 if (pSrcBitmap->IsCmykImage()) {
781 for (int i = 0; i < 256; i++) {
782 AdobeCMYK_to_sRGB1(
783 FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]),
784 FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i]),
785 bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]);
786 bgr_ptr += 3;
787 }
788 bgr_ptr = (uint8_t*)plt;
789 }
790 }
791 for (int row = 0; row < height; row++) {
792 uint8_t* dest_scan = dest_buf + row * dest_pitch;
793 const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left;
794 for (int col = 0; col < width; col++) {
795 uint8_t* src_pixel = bgr_ptr + 3 * (*src_scan++);
796 *dest_scan++ = *src_pixel++;
797 *dest_scan++ = *src_pixel++;
798 *dest_scan = *src_pixel++;
799 dest_scan += comps - 2;
800 }
801 }
802 return TRUE;
803 }
804 FX_BOOL ConvertBuffer_24bppRgb2Rgb24(uint8_t* dest_buf,
805 int dest_pitch,
806 int width,
807 int height,
808 const CFX_DIBSource* pSrcBitmap,
809 int src_left,
810 int src_top,
811 void* pIccTransform) {
812 if (pIccTransform) {
813 ICodec_IccModule* pIccModule =
814 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
815 for (int row = 0; row < height; row++) {
816 uint8_t* dest_scan = dest_buf + row * dest_pitch;
817 const uint8_t* src_scan =
818 pSrcBitmap->GetScanline(src_top + row) + src_left * 3;
819 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, width);
820 }
821 } else {
822 for (int row = 0; row < height; row++) {
823 uint8_t* dest_scan = dest_buf + row * dest_pitch;
824 const uint8_t* src_scan =
825 pSrcBitmap->GetScanline(src_top + row) + src_left * 3;
826 FXSYS_memcpy(dest_scan, src_scan, width * 3);
827 }
828 }
829 return TRUE;
830 }
831 FX_BOOL ConvertBuffer_32bppRgb2Rgb24(uint8_t* dest_buf,
832 int dest_pitch,
833 int width,
834 int height,
835 const CFX_DIBSource* pSrcBitmap,
836 int src_left,
837 int src_top,
838 void* pIccTransform) {
839 for (int row = 0; row < height; row++) {
840 uint8_t* dest_scan = dest_buf + row * dest_pitch;
841 const uint8_t* src_scan =
842 pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
843 for (int col = 0; col < width; col++) {
844 *dest_scan++ = *src_scan++;
845 *dest_scan++ = *src_scan++;
846 *dest_scan++ = *src_scan++;
847 src_scan++;
848 }
849 }
850 if (pIccTransform) {
851 ICodec_IccModule* pIccModule =
852 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
853 for (int row = 0; row < height; row++) {
854 uint8_t* dest_scan = dest_buf + row * dest_pitch;
855 pIccModule->TranslateScanline(pIccTransform, dest_scan, dest_scan, width);
856 }
857 }
858 return TRUE;
859 }
860 FX_BOOL ConvertBuffer_Rgb2Rgb32(uint8_t* dest_buf,
861 int dest_pitch,
862 int width,
863 int height,
864 const CFX_DIBSource* pSrcBitmap,
865 int src_left,
866 int src_top,
867 void* pIccTransform) {
868 int comps = pSrcBitmap->GetBPP() / 8;
869 if (pIccTransform) {
870 ICodec_IccModule* pIccModule =
871 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
872 for (int row = 0; row < height; row++) {
873 uint8_t* dest_scan = dest_buf + row * dest_pitch;
874 const uint8_t* src_scan =
875 pSrcBitmap->GetScanline(src_top + row) + src_left * comps;
876 for (int col = 0; col < width; col++) {
877 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
878 dest_scan += 4;
879 src_scan += comps;
880 }
881 }
882 } else {
883 for (int row = 0; row < height; row++) {
884 uint8_t* dest_scan = dest_buf + row * dest_pitch;
885 const uint8_t* src_scan =
886 pSrcBitmap->GetScanline(src_top + row) + src_left * comps;
887 for (int col = 0; col < width; col++) {
888 *dest_scan++ = *src_scan++;
889 *dest_scan++ = *src_scan++;
890 *dest_scan++ = *src_scan++;
891 dest_scan++;
892 src_scan += comps - 3;
893 }
894 }
895 }
896 return TRUE;
897 }
898 FX_BOOL ConvertBuffer_32bppCmyk2Rgb32(uint8_t* dest_buf,
899 int dest_pitch,
900 int width,
901 int height,
902 const CFX_DIBSource* pSrcBitmap,
903 int src_left,
904 int src_top,
905 void* pIccTransform) {
906 if (pIccTransform) {
907 ICodec_IccModule* pIccModule =
908 CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
909 for (int row = 0; row < height; row++) {
910 uint8_t* dest_scan = dest_buf + row * dest_pitch;
911 const uint8_t* src_scan =
912 pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
913 for (int col = 0; col < width; col++) {
914 pIccModule->TranslateScanline(pIccTransform, dest_scan, src_scan, 1);
915 dest_scan += 4;
916 src_scan += 4;
917 }
918 }
919 } else {
920 for (int row = 0; row < height; row++) {
921 uint8_t* dest_scan = dest_buf + row * dest_pitch;
922 const uint8_t* src_scan =
923 pSrcBitmap->GetScanline(src_top + row) + src_left * 4;
924 for (int col = 0; col < width; col++) {
925 AdobeCMYK_to_sRGB1(src_scan[0], src_scan[1], src_scan[2], src_scan[3],
926 dest_scan[2], dest_scan[1], dest_scan[0]);
927 dest_scan += 4;
928 src_scan += 4;
929 }
930 }
931 }
932 return TRUE;
933 }
934 FX_BOOL ConvertBuffer(FXDIB_Format dest_format,
935 uint8_t* dest_buf,
936 int dest_pitch,
937 int width,
938 int height,
939 const CFX_DIBSource* pSrcBitmap,
940 int src_left,
941 int src_top,
942 FX_DWORD*& d_pal,
943 void* pIccTransform) {
944 FXDIB_Format src_format = pSrcBitmap->GetFormat();
945 if (!CFX_GEModule::Get()->GetCodecModule() ||
946 !CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) {
947 pIccTransform = NULL;
948 }
949 switch (dest_format) {
950 case FXDIB_Invalid:
951 case FXDIB_1bppCmyk:
952 case FXDIB_1bppMask:
953 case FXDIB_1bppRgb:
954 ASSERT(FALSE);
955 return FALSE;
956 case FXDIB_8bppMask: {
957 if ((src_format & 0xff) == 1) {
958 if (pSrcBitmap->GetPalette()) {
959 return ConvertBuffer_1bppPlt2Gray(dest_buf, dest_pitch, width, height,
960 pSrcBitmap, src_left, src_top,
961 pIccTransform);
962 }
963 return ConvertBuffer_1bppMask2Gray(dest_buf, dest_pitch, width, height,
964 pSrcBitmap, src_left, src_top);
965 }
966 if ((src_format & 0xff) == 8) {
967 if (pSrcBitmap->GetPalette()) {
968 return ConvertBuffer_8bppPlt2Gray(dest_buf, dest_pitch, width, height,
969 pSrcBitmap, src_left, src_top,
970 pIccTransform);
971 }
972 return ConvertBuffer_8bppMask2Gray(dest_buf, dest_pitch, width, height,
973 pSrcBitmap, src_left, src_top);
974 }
975 if ((src_format & 0xff) >= 24) {
976 return ConvertBuffer_RgbOrCmyk2Gray(dest_buf, dest_pitch, width, height,
977 pSrcBitmap, src_left, src_top,
978 pIccTransform);
979 }
980 return FALSE;
981 }
982 case FXDIB_8bppRgb:
983 case FXDIB_8bppRgba: {
984 if ((src_format & 0xff) == 8 && !pSrcBitmap->GetPalette()) {
985 return ConvertBuffer(FXDIB_8bppMask, dest_buf, dest_pitch, width,
986 height, pSrcBitmap, src_left, src_top, d_pal,
987 pIccTransform);
988 }
989 d_pal = FX_Alloc(FX_DWORD, 256);
990 if (((src_format & 0xff) == 1 || (src_format & 0xff) == 8) &&
991 pSrcBitmap->GetPalette()) {
992 return ConvertBuffer_Plt2PltRgb8(dest_buf, dest_pitch, width, height,
993 pSrcBitmap, src_left, src_top, d_pal,
994 pIccTransform);
995 }
996 if ((src_format & 0xff) >= 24) {
997 return ConvertBuffer_Rgb2PltRgb8(dest_buf, dest_pitch, width, height,
998 pSrcBitmap, src_left, src_top, d_pal,
999 pIccTransform);
1000 }
1001 return FALSE;
1002 }
1003 case FXDIB_Rgb:
1004 case FXDIB_Rgba: {
1005 if ((src_format & 0xff) == 1) {
1006 if (pSrcBitmap->GetPalette()) {
1007 return ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch,
1008 width, height, pSrcBitmap, src_left,
1009 src_top, pIccTransform);
1010 }
1011 return ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch,
1012 width, height, pSrcBitmap, src_left,
1013 src_top);
1014 }
1015 if ((src_format & 0xff) == 8) {
1016 if (pSrcBitmap->GetPalette()) {
1017 return ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch,
1018 width, height, pSrcBitmap, src_left,
1019 src_top, pIccTransform);
1020 }
1021 return ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch,
1022 width, height, pSrcBitmap, src_left,
1023 src_top);
1024 }
1025 if ((src_format & 0xff) == 24) {
1026 return ConvertBuffer_24bppRgb2Rgb24(dest_buf, dest_pitch, width, height,
1027 pSrcBitmap, src_left, src_top,
1028 pIccTransform);
1029 }
1030 if ((src_format & 0xff) == 32) {
1031 return ConvertBuffer_32bppRgb2Rgb24(dest_buf, dest_pitch, width, height,
1032 pSrcBitmap, src_left, src_top,
1033 pIccTransform);
1034 }
1035 return FALSE;
1036 }
1037 case FXDIB_Argb:
1038 case FXDIB_Rgb32: {
1039 if ((src_format & 0xff) == 1) {
1040 if (pSrcBitmap->GetPalette()) {
1041 return ConvertBuffer_1bppPlt2Rgb(dest_format, dest_buf, dest_pitch,
1042 width, height, pSrcBitmap, src_left,
1043 src_top, pIccTransform);
1044 }
1045 return ConvertBuffer_1bppMask2Rgb(dest_format, dest_buf, dest_pitch,
1046 width, height, pSrcBitmap, src_left,
1047 src_top);
1048 }
1049 if ((src_format & 0xff) == 8) {
1050 if (pSrcBitmap->GetPalette()) {
1051 return ConvertBuffer_8bppPlt2Rgb(dest_format, dest_buf, dest_pitch,
1052 width, height, pSrcBitmap, src_left,
1053 src_top, pIccTransform);
1054 }
1055 return ConvertBuffer_8bppMask2Rgb(dest_format, dest_buf, dest_pitch,
1056 width, height, pSrcBitmap, src_left,
1057 src_top);
1058 }
1059 if ((src_format & 0xff) >= 24) {
1060 if (src_format & 0x0400) {
1061 return ConvertBuffer_32bppCmyk2Rgb32(dest_buf, dest_pitch, width,
1062 height, pSrcBitmap, src_left,
1063 src_top, pIccTransform);
1064 }
1065 return ConvertBuffer_Rgb2Rgb32(dest_buf, dest_pitch, width, height,
1066 pSrcBitmap, src_left, src_top,
1067 pIccTransform);
1068 }
1069 return FALSE;
1070 }
1071 default:
1072 return FALSE;
1073 }
1074 return FALSE;
1075 }
1076 CFX_DIBitmap* CFX_DIBSource::CloneConvert(FXDIB_Format dest_format,
1077 const FX_RECT* pClip,
1078 void* pIccTransform) const {
1079 if (dest_format == GetFormat() && !pIccTransform) {
1080 return Clone(pClip);
1081 }
1082 if (pClip) {
1083 CFX_DIBitmap* pClone = Clone(pClip);
1084 if (!pClone) {
1085 return NULL;
1086 }
1087 if (!pClone->ConvertFormat(dest_format, pIccTransform)) {
1088 delete pClone;
1089 return NULL;
1090 }
1091 return pClone;
1092 }
1093 CFX_DIBitmap* pClone = new CFX_DIBitmap;
1094 if (!pClone->Create(m_Width, m_Height, dest_format)) {
1095 delete pClone;
1096 return NULL;
1097 }
1098 FX_BOOL ret = TRUE;
1099 CFX_DIBitmap* pSrcAlpha = NULL;
1100 if (HasAlpha()) {
1101 pSrcAlpha = (GetFormat() == FXDIB_Argb) ? GetAlphaMask() : m_pAlphaMask;
1102 if (!pSrcAlpha) {
1103 delete pClone;
1104 return NULL;
1105 }
1106 }
1107 if (dest_format & 0x0200) {
1108 if (dest_format == FXDIB_Argb) {
1109 ret = pSrcAlpha ? pClone->LoadChannel(FXDIB_Alpha, pSrcAlpha, FXDIB_Alpha)
1110 : pClone->LoadChannel(FXDIB_Alpha, 0xff);
1111 } else {
1112 ret = pClone->CopyAlphaMask(pSrcAlpha);
1113 }
1114 }
1115 if (pSrcAlpha && pSrcAlpha != m_pAlphaMask) {
1116 delete pSrcAlpha;
1117 pSrcAlpha = NULL;
1118 }
1119 if (!ret) {
1120 delete pClone;
1121 return NULL;
1122 }
1123 FX_DWORD* pal_8bpp = NULL;
1124 ret = ConvertBuffer(dest_format, pClone->GetBuffer(), pClone->GetPitch(),
1125 m_Width, m_Height, this, 0, 0, pal_8bpp, pIccTransform);
1126 if (!ret) {
1127 FX_Free(pal_8bpp);
1128 delete pClone;
1129 return NULL;
1130 }
1131 if (pal_8bpp) {
1132 pClone->CopyPalette(pal_8bpp);
1133 FX_Free(pal_8bpp);
1134 pal_8bpp = NULL;
1135 }
1136 return pClone;
1137 }
1138 FX_BOOL CFX_DIBitmap::ConvertFormat(FXDIB_Format dest_format,
1139 void* pIccTransform) {
1140 FXDIB_Format src_format = GetFormat();
1141 if (dest_format == src_format && !pIccTransform) {
1142 return TRUE;
1143 }
1144 if (dest_format == FXDIB_8bppMask && src_format == FXDIB_8bppRgb &&
1145 !m_pPalette) {
1146 m_AlphaFlag = 1;
1147 return TRUE;
1148 }
1149 if (dest_format == FXDIB_Argb && src_format == FXDIB_Rgb32 &&
1150 !pIccTransform) {
1151 m_AlphaFlag = 2;
1152 for (int row = 0; row < m_Height; row++) {
1153 uint8_t* scanline = m_pBuffer + row * m_Pitch + 3;
1154 for (int col = 0; col < m_Width; col++) {
1155 *scanline = 0xff;
1156 scanline += 4;
1157 }
1158 }
1159 return TRUE;
1160 }
1161 int dest_bpp = dest_format & 0xff;
1162 int dest_pitch = (dest_bpp * m_Width + 31) / 32 * 4;
1163 uint8_t* dest_buf = FX_TryAlloc(uint8_t, dest_pitch * m_Height + 4);
1164 if (!dest_buf) {
1165 return FALSE;
1166 }
1167 CFX_DIBitmap* pAlphaMask = NULL;
1168 if (dest_format == FXDIB_Argb) {
1169 FXSYS_memset(dest_buf, 0xff, dest_pitch * m_Height + 4);
1170 if (m_pAlphaMask) {
1171 for (int row = 0; row < m_Height; row++) {
1172 uint8_t* pDstScanline = dest_buf + row * dest_pitch + 3;
1173 const uint8_t* pSrcScanline = m_pAlphaMask->GetScanline(row);
1174 for (int col = 0; col < m_Width; col++) {
1175 *pDstScanline = *pSrcScanline++;
1176 pDstScanline += 4;
1177 }
1178 }
1179 }
1180 } else if (dest_format & 0x0200) {
1181 if (src_format == FXDIB_Argb) {
1182 pAlphaMask = GetAlphaMask();
1183 if (!pAlphaMask) {
1184 FX_Free(dest_buf);
1185 return FALSE;
1186 }
1187 } else {
1188 if (!m_pAlphaMask) {
1189 if (!BuildAlphaMask()) {
1190 FX_Free(dest_buf);
1191 return FALSE;
1192 }
1193 pAlphaMask = m_pAlphaMask;
1194 m_pAlphaMask = NULL;
1195 } else {
1196 pAlphaMask = m_pAlphaMask;
1197 }
1198 }
1199 }
1200 FX_BOOL ret = FALSE;
1201 FX_DWORD* pal_8bpp = NULL;
1202 ret = ConvertBuffer(dest_format, dest_buf, dest_pitch, m_Width, m_Height,
1203 this, 0, 0, pal_8bpp, pIccTransform);
1204 if (!ret) {
1205 FX_Free(pal_8bpp);
1206 if (pAlphaMask != m_pAlphaMask) {
1207 delete pAlphaMask;
1208 }
1209 FX_Free(dest_buf);
1210 return FALSE;
1211 }
1212 if (m_pAlphaMask && pAlphaMask != m_pAlphaMask) {
1213 delete m_pAlphaMask;
1214 }
1215 m_pAlphaMask = pAlphaMask;
1216 FX_Free(m_pPalette);
1217 m_pPalette = pal_8bpp;
1218 if (!m_bExtBuf) {
1219 FX_Free(m_pBuffer);
1220 }
1221 m_bExtBuf = FALSE;
1222 m_pBuffer = dest_buf;
1223 m_bpp = (uint8_t)dest_format;
1224 m_AlphaFlag = (uint8_t)(dest_format >> 8);
1225 m_Pitch = dest_pitch;
1226 return TRUE;
1227 }
OLDNEW
« no previous file with comments | « core/src/fxge/dib/fx_dib_composite.cpp ('k') | core/src/fxge/dib/fx_dib_engine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698