/src/freeimage-svn/FreeImage/trunk/Source/FreeImage/Conversion32.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // ========================================================== |
2 | | // Bitmap conversion routines |
3 | | // |
4 | | // Design and implementation by |
5 | | // - Floris van den Berg (flvdberg@wxs.nl) |
6 | | // - Hervé Drolon (drolon@infonie.fr) |
7 | | // - Jani Kajala (janik@remedy.fi) |
8 | | // - Detlev Vendt (detlev.vendt@brillit.de) |
9 | | // |
10 | | // This file is part of FreeImage 3 |
11 | | // |
12 | | // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY |
13 | | // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES |
14 | | // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE |
15 | | // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED |
16 | | // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT |
17 | | // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY |
18 | | // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL |
19 | | // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
20 | | // THIS DISCLAIMER. |
21 | | // |
22 | | // Use at your own risk! |
23 | | // ========================================================== |
24 | | |
25 | | #include "FreeImage.h" |
26 | | #include "Utilities.h" |
27 | | |
28 | | // ---------------------------------------------------------- |
29 | | // internal conversions X to 32 bits |
30 | | // ---------------------------------------------------------- |
31 | | |
32 | | void DLL_CALLCONV |
33 | 0 | FreeImage_ConvertLine1To32(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette) { |
34 | 0 | for (int cols = 0; cols < width_in_pixels; cols++) { |
35 | 0 | int index = (source[cols>>3] & (0x80 >> (cols & 0x07))) != 0 ? 1 : 0; |
36 | |
|
37 | 0 | target[FI_RGBA_BLUE] = palette[index].rgbBlue; |
38 | 0 | target[FI_RGBA_GREEN] = palette[index].rgbGreen; |
39 | 0 | target[FI_RGBA_RED] = palette[index].rgbRed; |
40 | 0 | target[FI_RGBA_ALPHA] = 0xFF; |
41 | 0 | target += 4; |
42 | 0 | } |
43 | 0 | } |
44 | | |
45 | | void DLL_CALLCONV |
46 | 0 | FreeImage_ConvertLine4To32(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette) { |
47 | 0 | BOOL low_nibble = FALSE; |
48 | 0 | int x = 0; |
49 | |
|
50 | 0 | for (int cols = 0 ; cols < width_in_pixels ; ++cols) { |
51 | 0 | if (low_nibble) { |
52 | 0 | target[FI_RGBA_BLUE] = palette[LOWNIBBLE(source[x])].rgbBlue; |
53 | 0 | target[FI_RGBA_GREEN] = palette[LOWNIBBLE(source[x])].rgbGreen; |
54 | 0 | target[FI_RGBA_RED] = palette[LOWNIBBLE(source[x])].rgbRed; |
55 | |
|
56 | 0 | x++; |
57 | 0 | } else { |
58 | 0 | target[FI_RGBA_BLUE] = palette[HINIBBLE(source[x]) >> 4].rgbBlue; |
59 | 0 | target[FI_RGBA_GREEN] = palette[HINIBBLE(source[x]) >> 4].rgbGreen; |
60 | 0 | target[FI_RGBA_RED] = palette[HINIBBLE(source[x]) >> 4].rgbRed; |
61 | 0 | } |
62 | |
|
63 | 0 | low_nibble = !low_nibble; |
64 | |
|
65 | 0 | target[FI_RGBA_ALPHA] = 0xFF; |
66 | 0 | target += 4; |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | void DLL_CALLCONV |
71 | 0 | FreeImage_ConvertLine8To32(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette) { |
72 | 0 | for (int cols = 0; cols < width_in_pixels; cols++) { |
73 | 0 | target[FI_RGBA_BLUE] = palette[source[cols]].rgbBlue; |
74 | 0 | target[FI_RGBA_GREEN] = palette[source[cols]].rgbGreen; |
75 | 0 | target[FI_RGBA_RED] = palette[source[cols]].rgbRed; |
76 | 0 | target[FI_RGBA_ALPHA] = 0xFF; |
77 | 0 | target += 4; |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | void DLL_CALLCONV |
82 | 0 | FreeImage_ConvertLine16To32_555(BYTE *target, BYTE *source, int width_in_pixels) { |
83 | 0 | WORD *bits = (WORD *)source; |
84 | |
|
85 | 0 | for (int cols = 0; cols < width_in_pixels; cols++) { |
86 | 0 | target[FI_RGBA_RED] = (BYTE)((((bits[cols] & FI16_555_RED_MASK) >> FI16_555_RED_SHIFT) * 0xFF) / 0x1F); |
87 | 0 | target[FI_RGBA_GREEN] = (BYTE)((((bits[cols] & FI16_555_GREEN_MASK) >> FI16_555_GREEN_SHIFT) * 0xFF) / 0x1F); |
88 | 0 | target[FI_RGBA_BLUE] = (BYTE)((((bits[cols] & FI16_555_BLUE_MASK) >> FI16_555_BLUE_SHIFT) * 0xFF) / 0x1F); |
89 | 0 | target[FI_RGBA_ALPHA] = 0xFF; |
90 | 0 | target += 4; |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | | void DLL_CALLCONV |
95 | 0 | FreeImage_ConvertLine16To32_565(BYTE *target, BYTE *source, int width_in_pixels) { |
96 | 0 | WORD *bits = (WORD *)source; |
97 | |
|
98 | 0 | for (int cols = 0; cols < width_in_pixels; cols++) { |
99 | 0 | target[FI_RGBA_RED] = (BYTE)((((bits[cols] & FI16_565_RED_MASK) >> FI16_565_RED_SHIFT) * 0xFF) / 0x1F); |
100 | 0 | target[FI_RGBA_GREEN] = (BYTE)((((bits[cols] & FI16_565_GREEN_MASK) >> FI16_565_GREEN_SHIFT) * 0xFF) / 0x3F); |
101 | 0 | target[FI_RGBA_BLUE] = (BYTE)((((bits[cols] & FI16_565_BLUE_MASK) >> FI16_565_BLUE_SHIFT) * 0xFF) / 0x1F); |
102 | 0 | target[FI_RGBA_ALPHA] = 0xFF; |
103 | 0 | target += 4; |
104 | 0 | } |
105 | 0 | } |
106 | | /* |
107 | | void DLL_CALLCONV |
108 | | FreeImage_ConvertLine24To32(BYTE *target, BYTE *source, int width_in_pixels) { |
109 | | for (int cols = 0; cols < width_in_pixels; cols++) { |
110 | | *(DWORD *)target = (*(DWORD *) source & FI_RGBA_RGB_MASK) | FI_RGBA_ALPHA_MASK; |
111 | | target += 4; |
112 | | source += 3; |
113 | | } |
114 | | } |
115 | | */ |
116 | | /** |
117 | | This unoptimized version of the conversion function avoid an undetermined bug with VC++ SP6. |
118 | | The bug occurs in release mode only, when the image height is equal to 537 |
119 | | (try e.g. a size of 432x537 to reproduce the bug with the optimized function). |
120 | | */ |
121 | | void DLL_CALLCONV |
122 | 0 | FreeImage_ConvertLine24To32(BYTE *target, BYTE *source, int width_in_pixels) { |
123 | 0 | for (int cols = 0; cols < width_in_pixels; cols++) { |
124 | 0 | target[FI_RGBA_RED] = source[FI_RGBA_RED]; |
125 | 0 | target[FI_RGBA_GREEN] = source[FI_RGBA_GREEN]; |
126 | 0 | target[FI_RGBA_BLUE] = source[FI_RGBA_BLUE]; |
127 | 0 | target[FI_RGBA_ALPHA] = 0xFF; |
128 | 0 | target += 4; |
129 | 0 | source += 3; |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | | // ---------------------------------------------------------- |
134 | | |
135 | | void DLL_CALLCONV |
136 | 0 | FreeImage_ConvertLine1To32MapTransparency(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette, BYTE *table, int transparent_pixels) { |
137 | 0 | for (int cols = 0; cols < width_in_pixels; cols++) { |
138 | 0 | int index = (source[cols>>3] & (0x80 >> (cols & 0x07))) != 0 ? 1 : 0; |
139 | |
|
140 | 0 | target[FI_RGBA_BLUE] = palette[index].rgbBlue; |
141 | 0 | target[FI_RGBA_GREEN] = palette[index].rgbGreen; |
142 | 0 | target[FI_RGBA_RED] = palette[index].rgbRed; |
143 | 0 | target[FI_RGBA_ALPHA] = (index < transparent_pixels) ? table[index] : 255; |
144 | 0 | target += 4; |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | | void DLL_CALLCONV |
149 | 0 | FreeImage_ConvertLine4To32MapTransparency(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette, BYTE *table, int transparent_pixels) { |
150 | 0 | BOOL low_nibble = FALSE; |
151 | 0 | int x = 0; |
152 | |
|
153 | 0 | for (int cols = 0 ; cols < width_in_pixels ; ++cols) { |
154 | 0 | if (low_nibble) { |
155 | 0 | target[FI_RGBA_BLUE] = palette[LOWNIBBLE(source[x])].rgbBlue; |
156 | 0 | target[FI_RGBA_GREEN] = palette[LOWNIBBLE(source[x])].rgbGreen; |
157 | 0 | target[FI_RGBA_RED] = palette[LOWNIBBLE(source[x])].rgbRed; |
158 | 0 | target[FI_RGBA_ALPHA] = (LOWNIBBLE(source[x]) < transparent_pixels) ? table[LOWNIBBLE(source[x])] : 255; |
159 | |
|
160 | 0 | x++; |
161 | 0 | } else { |
162 | 0 | target[FI_RGBA_BLUE] = palette[HINIBBLE(source[x]) >> 4].rgbBlue; |
163 | 0 | target[FI_RGBA_GREEN] = palette[HINIBBLE(source[x]) >> 4].rgbGreen; |
164 | 0 | target[FI_RGBA_RED] = palette[HINIBBLE(source[x]) >> 4].rgbRed; |
165 | 0 | target[FI_RGBA_ALPHA] = (HINIBBLE(source[x] >> 4) < transparent_pixels) ? table[HINIBBLE(source[x]) >> 4] : 255; |
166 | 0 | } |
167 | |
|
168 | 0 | low_nibble = !low_nibble; |
169 | | |
170 | 0 | target += 4; |
171 | 0 | } |
172 | 0 | } |
173 | | |
174 | | void DLL_CALLCONV |
175 | 0 | FreeImage_ConvertLine8To32MapTransparency(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette, BYTE *table, int transparent_pixels) { |
176 | 0 | for (int cols = 0; cols < width_in_pixels; cols++) { |
177 | 0 | target[FI_RGBA_BLUE] = palette[source[cols]].rgbBlue; |
178 | 0 | target[FI_RGBA_GREEN] = palette[source[cols]].rgbGreen; |
179 | 0 | target[FI_RGBA_RED] = palette[source[cols]].rgbRed; |
180 | 0 | target[FI_RGBA_ALPHA] = (source[cols] < transparent_pixels) ? table[source[cols]] : 255; |
181 | 0 | target += 4; |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | | // ---------------------------------------------------------- |
186 | | |
187 | | FIBITMAP * DLL_CALLCONV |
188 | 0 | FreeImage_ConvertTo32Bits(FIBITMAP *dib) { |
189 | 0 | if(!FreeImage_HasPixels(dib)) return NULL; |
190 | | |
191 | 0 | const int bpp = FreeImage_GetBPP(dib); |
192 | 0 | const FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib); |
193 | | |
194 | 0 | if((image_type != FIT_BITMAP) && (image_type != FIT_RGB16) && (image_type != FIT_RGBA16)) { |
195 | 0 | return NULL; |
196 | 0 | } |
197 | | |
198 | 0 | const int width = FreeImage_GetWidth(dib); |
199 | 0 | const int height = FreeImage_GetHeight(dib); |
200 | |
|
201 | 0 | if(image_type == FIT_BITMAP) { |
202 | |
|
203 | 0 | if(bpp == 32) { |
204 | 0 | return FreeImage_Clone(dib); |
205 | 0 | } |
206 | | |
207 | 0 | FIBITMAP *new_dib = FreeImage_Allocate(width, height, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); |
208 | 0 | if(new_dib == NULL) { |
209 | 0 | return NULL; |
210 | 0 | } |
211 | | |
212 | | // copy metadata from src to dst |
213 | 0 | FreeImage_CloneMetadata(new_dib, dib); |
214 | |
|
215 | 0 | BOOL bIsTransparent = FreeImage_IsTransparent(dib); |
216 | |
|
217 | 0 | switch(bpp) { |
218 | 0 | case 1: |
219 | 0 | { |
220 | 0 | if(bIsTransparent) { |
221 | 0 | for (int rows = 0; rows < height; rows++) { |
222 | 0 | FreeImage_ConvertLine1To32MapTransparency(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width, FreeImage_GetPalette(dib), FreeImage_GetTransparencyTable(dib), FreeImage_GetTransparencyCount(dib)); |
223 | 0 | } |
224 | 0 | } else { |
225 | 0 | for (int rows = 0; rows < height; rows++) { |
226 | 0 | FreeImage_ConvertLine1To32(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width, FreeImage_GetPalette(dib)); |
227 | 0 | } |
228 | 0 | } |
229 | |
|
230 | 0 | return new_dib; |
231 | 0 | } |
232 | | |
233 | 0 | case 4: |
234 | 0 | { |
235 | 0 | if(bIsTransparent) { |
236 | 0 | for (int rows = 0; rows < height; rows++) { |
237 | 0 | FreeImage_ConvertLine4To32MapTransparency(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width, FreeImage_GetPalette(dib), FreeImage_GetTransparencyTable(dib), FreeImage_GetTransparencyCount(dib)); |
238 | 0 | } |
239 | 0 | } else { |
240 | 0 | for (int rows = 0; rows < height; rows++) { |
241 | 0 | FreeImage_ConvertLine4To32(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width, FreeImage_GetPalette(dib)); |
242 | 0 | } |
243 | 0 | } |
244 | |
|
245 | 0 | return new_dib; |
246 | 0 | } |
247 | | |
248 | 0 | case 8: |
249 | 0 | { |
250 | 0 | if(bIsTransparent) { |
251 | 0 | for (int rows = 0; rows < height; rows++) { |
252 | 0 | FreeImage_ConvertLine8To32MapTransparency(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width, FreeImage_GetPalette(dib), FreeImage_GetTransparencyTable(dib), FreeImage_GetTransparencyCount(dib)); |
253 | 0 | } |
254 | 0 | } else { |
255 | 0 | for (int rows = 0; rows < height; rows++) { |
256 | 0 | FreeImage_ConvertLine8To32(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width, FreeImage_GetPalette(dib)); |
257 | 0 | } |
258 | 0 | } |
259 | |
|
260 | 0 | return new_dib; |
261 | 0 | } |
262 | | |
263 | 0 | case 16: |
264 | 0 | { |
265 | 0 | for (int rows = 0; rows < height; rows++) { |
266 | 0 | if ((FreeImage_GetRedMask(dib) == FI16_565_RED_MASK) && (FreeImage_GetGreenMask(dib) == FI16_565_GREEN_MASK) && (FreeImage_GetBlueMask(dib) == FI16_565_BLUE_MASK)) { |
267 | 0 | FreeImage_ConvertLine16To32_565(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width); |
268 | 0 | } else { |
269 | | // includes case where all the masks are 0 |
270 | 0 | FreeImage_ConvertLine16To32_555(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width); |
271 | 0 | } |
272 | 0 | } |
273 | |
|
274 | 0 | return new_dib; |
275 | 0 | } |
276 | | |
277 | 0 | case 24: |
278 | 0 | { |
279 | 0 | for (int rows = 0; rows < height; rows++) { |
280 | 0 | FreeImage_ConvertLine24To32(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width); |
281 | 0 | } |
282 | |
|
283 | 0 | return new_dib; |
284 | 0 | } |
285 | 0 | } |
286 | |
|
287 | 0 | } else if(image_type == FIT_RGB16) { |
288 | 0 | FIBITMAP *new_dib = FreeImage_Allocate(width, height, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); |
289 | 0 | if(new_dib == NULL) { |
290 | 0 | return NULL; |
291 | 0 | } |
292 | | |
293 | | // copy metadata from src to dst |
294 | 0 | FreeImage_CloneMetadata(new_dib, dib); |
295 | |
|
296 | 0 | const unsigned src_pitch = FreeImage_GetPitch(dib); |
297 | 0 | const unsigned dst_pitch = FreeImage_GetPitch(new_dib); |
298 | 0 | const BYTE *src_bits = FreeImage_GetBits(dib); |
299 | 0 | BYTE *dst_bits = FreeImage_GetBits(new_dib); |
300 | 0 | for (int rows = 0; rows < height; rows++) { |
301 | 0 | const FIRGB16 *src_pixel = (FIRGB16*)src_bits; |
302 | 0 | RGBQUAD *dst_pixel = (RGBQUAD*)dst_bits; |
303 | 0 | for(int cols = 0; cols < width; cols++) { |
304 | 0 | dst_pixel[cols].rgbRed = (BYTE)(src_pixel[cols].red >> 8); |
305 | 0 | dst_pixel[cols].rgbGreen = (BYTE)(src_pixel[cols].green >> 8); |
306 | 0 | dst_pixel[cols].rgbBlue = (BYTE)(src_pixel[cols].blue >> 8); |
307 | 0 | dst_pixel[cols].rgbReserved = (BYTE)0xFF; |
308 | 0 | } |
309 | 0 | src_bits += src_pitch; |
310 | 0 | dst_bits += dst_pitch; |
311 | 0 | } |
312 | |
|
313 | 0 | return new_dib; |
314 | |
|
315 | 0 | } else if(image_type == FIT_RGBA16) { |
316 | 0 | FIBITMAP *new_dib = FreeImage_Allocate(width, height, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); |
317 | 0 | if(new_dib == NULL) { |
318 | 0 | return NULL; |
319 | 0 | } |
320 | | |
321 | | // copy metadata from src to dst |
322 | 0 | FreeImage_CloneMetadata(new_dib, dib); |
323 | |
|
324 | 0 | const unsigned src_pitch = FreeImage_GetPitch(dib); |
325 | 0 | const unsigned dst_pitch = FreeImage_GetPitch(new_dib); |
326 | 0 | const BYTE *src_bits = FreeImage_GetBits(dib); |
327 | 0 | BYTE *dst_bits = FreeImage_GetBits(new_dib); |
328 | 0 | for (int rows = 0; rows < height; rows++) { |
329 | 0 | const FIRGBA16 *src_pixel = (FIRGBA16*)src_bits; |
330 | 0 | RGBQUAD *dst_pixel = (RGBQUAD*)dst_bits; |
331 | 0 | for(int cols = 0; cols < width; cols++) { |
332 | 0 | dst_pixel[cols].rgbRed = (BYTE)(src_pixel[cols].red >> 8); |
333 | 0 | dst_pixel[cols].rgbGreen = (BYTE)(src_pixel[cols].green >> 8); |
334 | 0 | dst_pixel[cols].rgbBlue = (BYTE)(src_pixel[cols].blue >> 8); |
335 | 0 | dst_pixel[cols].rgbReserved = (BYTE)(src_pixel[cols].alpha >> 8); |
336 | 0 | } |
337 | 0 | src_bits += src_pitch; |
338 | 0 | dst_bits += dst_pitch; |
339 | 0 | } |
340 | |
|
341 | 0 | return new_dib; |
342 | 0 | } |
343 | | |
344 | 0 | return NULL; |
345 | 0 | } |