summaryrefslogtreecommitdiff
path: root/plugins/AdvaImg/src/FreeImage/Conversion8.cpp
blob: c4f9b22441dd90ce1bb9191e4f3f5924f84b4a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// ==========================================================
// Bitmap conversion routines
//
// Design and implementation by
// - Floris van den Berg (flvdberg@wxs.nl)
// - Hervé Drolon (drolon@infonie.fr)
// - Jani Kajala (janik@remedy.fi)
// - Karl-Heinz Bussian (khbussian@moss.de)
// - Carsten Klein (cklein05@users.sourceforge.net)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================

#include "FreeImage.h"
#include "Utilities.h"

// ----------------------------------------------------------
//  internal conversions X to 8 bits
// ----------------------------------------------------------

void DLL_CALLCONV
FreeImage_ConvertLine1To8(BYTE *target, BYTE *source, int width_in_pixels) {
	for (unsigned cols = 0; cols < (unsigned)width_in_pixels; cols++)
		target[cols] = (source[cols >> 3] & (0x80 >> (cols & 0x07))) != 0 ? 255 : 0;	
}

void DLL_CALLCONV
FreeImage_ConvertLine4To8(BYTE *target, BYTE *source, int width_in_pixels) {
	unsigned count_new = 0;
	unsigned count_org = 0;
	BOOL hinibble = TRUE;

	while (count_new < (unsigned)width_in_pixels) {
		if (hinibble) {
			target[count_new] = (source[count_org] >> 4);
		} else {
			target[count_new] = (source[count_org] & 0x0F);
			count_org++;
		}
		hinibble = !hinibble;
		count_new++;
	}
}

void DLL_CALLCONV
FreeImage_ConvertLine16To8_555(BYTE *target, BYTE *source, int width_in_pixels) {
	const WORD *const bits = (WORD *)source;
	for (unsigned cols = 0; cols < (unsigned)width_in_pixels; cols++) {
		target[cols] = GREY((((bits[cols] & FI16_555_RED_MASK) >> FI16_555_RED_SHIFT) * 0xFF) / 0x1F,
			                (((bits[cols] & FI16_555_GREEN_MASK) >> FI16_555_GREEN_SHIFT) * 0xFF) / 0x1F,
							(((bits[cols] & FI16_555_BLUE_MASK) >> FI16_555_BLUE_SHIFT) * 0xFF) / 0x1F);
	}
}

void DLL_CALLCONV
FreeImage_ConvertLine16To8_565(BYTE *target, BYTE *source, int width_in_pixels) {
	const WORD *const bits = (WORD *)source;
	for (unsigned cols = 0; cols < (unsigned)width_in_pixels; cols++) {
		target[cols] = GREY((((bits[cols] & FI16_565_RED_MASK) >> FI16_565_RED_SHIFT) * 0xFF) / 0x1F,
			        (((bits[cols] & FI16_565_GREEN_MASK) >> FI16_565_GREEN_SHIFT) * 0xFF) / 0x3F,
					(((bits[cols] & FI16_565_BLUE_MASK) >> FI16_565_BLUE_SHIFT) * 0xFF) / 0x1F);
	}
}

void DLL_CALLCONV
FreeImage_ConvertLine24To8(BYTE *target, BYTE *source, int width_in_pixels) {
	for (unsigned cols = 0; cols < (unsigned)width_in_pixels; cols++) {
		target[cols] = GREY(source[FI_RGBA_RED], source[FI_RGBA_GREEN], source[FI_RGBA_BLUE]);
		source += 3;
	}
}

void DLL_CALLCONV
FreeImage_ConvertLine32To8(BYTE *target, BYTE *source, int width_in_pixels) {
	for (unsigned cols = 0; cols < (unsigned)width_in_pixels; cols++) {
		target[cols] = GREY(source[FI_RGBA_RED], source[FI_RGBA_GREEN], source[FI_RGBA_BLUE]);
		source += 4;
	}
}

// ----------------------------------------------------------
//   smart convert X to 8 bits
// ----------------------------------------------------------

FIBITMAP * DLL_CALLCONV
FreeImage_ConvertTo8Bits(FIBITMAP *dib) {
	if (!FreeImage_HasPixels(dib)) {
		return NULL;
	}

	const FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
	if (image_type != FIT_BITMAP && image_type != FIT_UINT16) {
		return NULL;
	}

	const unsigned bpp = FreeImage_GetBPP(dib);

	if (bpp != 8) {

		const unsigned width = FreeImage_GetWidth(dib);
		const unsigned height = FreeImage_GetHeight(dib);

		// Allocate a destination image
		FIBITMAP *new_dib = FreeImage_Allocate(width, height, 8);
		if (new_dib == NULL) {
			return NULL;
		}

		// Copy metadata from src to dst
		FreeImage_CloneMetadata(new_dib, dib);

		// Palette of destination image has already been initialized
		RGBQUAD *new_pal = FreeImage_GetPalette(new_dib);

		const FREE_IMAGE_COLOR_TYPE color_type = FreeImage_GetColorType(dib);

		if (image_type == FIT_BITMAP) {

			switch(bpp) {
				case 1:
				{
					if (color_type == FIC_PALETTE) {
						// Copy the palette
						RGBQUAD *old_pal = FreeImage_GetPalette(dib);
						new_pal[0] = old_pal[0];
						new_pal[255] = old_pal[1];

					} else if (color_type == FIC_MINISWHITE) {
						// Create a reverse grayscale palette
						CREATE_GREYSCALE_PALETTE_REVERSE(new_pal, 256);
					}

					// Expand and copy the bitmap data
					for (unsigned rows = 0; rows < height; rows++) {
						FreeImage_ConvertLine1To8(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width);
					}
					return new_dib;
				}

				case 4 :
				{
					if (color_type == FIC_PALETTE) {
						// Copy the palette
						memcpy(new_pal, FreeImage_GetPalette(dib), 16 * sizeof(RGBQUAD));
					}

					// Expand and copy the bitmap data
					for (unsigned rows = 0; rows < height; rows++) {
						FreeImage_ConvertLine4To8(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width);					
					}
					return new_dib;
				}

				case 16 :
				{
					// Expand and copy the bitmap data
					if (IS_FORMAT_RGB565(dib)) {
						for (unsigned rows = 0; rows < height; rows++) {
							FreeImage_ConvertLine16To8_565(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width);
						}
					} else {
						for (unsigned rows = 0; rows < height; rows++) {
							FreeImage_ConvertLine16To8_555(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width);
						}
					}
					return new_dib;
				}

				case 24 :
				{
					// Expand and copy the bitmap data
					for (unsigned rows = 0; rows < height; rows++) {
						FreeImage_ConvertLine24To8(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width);					
					}
					return new_dib;
				}

				case 32 :
				{
					// Expand and copy the bitmap data
					for (unsigned rows = 0; rows < height; rows++) {
						FreeImage_ConvertLine32To8(FreeImage_GetScanLine(new_dib, rows), FreeImage_GetScanLine(dib, rows), width);
					}
					return new_dib;
				}
			}

		} else if (image_type == FIT_UINT16) {

			const unsigned src_pitch = FreeImage_GetPitch(dib);
			const unsigned dst_pitch = FreeImage_GetPitch(new_dib);
			const BYTE *src_bits = FreeImage_GetBits(dib);
			BYTE *dst_bits = FreeImage_GetBits(new_dib);

			for (unsigned rows = 0; rows < height; rows++) {
				const WORD *const src_pixel = (WORD*)src_bits;
				BYTE *dst_pixel = (BYTE*)dst_bits;
				for(unsigned cols = 0; cols < width; cols++) {
					dst_pixel[cols] = (BYTE)(src_pixel[cols] >> 8);
				}
				src_bits += src_pitch;
				dst_bits += dst_pitch;
			}
			return new_dib;
		} 

	} // bpp != 8

	return FreeImage_Clone(dib);
}

FIBITMAP * DLL_CALLCONV
FreeImage_ConvertToGreyscale(FIBITMAP *dib) {
	if (!FreeImage_HasPixels(dib)) {
		return NULL;
	}

	const FREE_IMAGE_COLOR_TYPE color_type = FreeImage_GetColorType(dib);

	if (color_type == FIC_PALETTE || color_type == FIC_MINISWHITE) {

		const unsigned bpp = FreeImage_GetBPP(dib);
		const unsigned width  = FreeImage_GetWidth(dib);
		const unsigned height = FreeImage_GetHeight(dib);

		FIBITMAP *new_dib = FreeImage_Allocate(width, height, 8);
		if (new_dib == NULL) {
			return NULL;
		}

		// Copy metadata from src to dst
		FreeImage_CloneMetadata(new_dib, dib);

		// Create a greyscale palette
		BYTE grey_pal[256];
		const RGBQUAD *pal = FreeImage_GetPalette(dib);
		const unsigned size = CalculateUsedPaletteEntries(bpp);
		for (unsigned i = 0; i < size; i++) {
			grey_pal[i] = GREY(pal->rgbRed, pal->rgbGreen, pal->rgbBlue);
			pal++;
		}

		const BYTE *src_bits = FreeImage_GetBits(dib);
		BYTE *dst_bits = FreeImage_GetBits(new_dib);

		const unsigned src_pitch = FreeImage_GetPitch(dib);
		const unsigned dst_pitch = FreeImage_GetPitch(new_dib);

		switch(bpp) {
			case 1:
			{
				for (unsigned y = 0; y < height; y++) {
					for (unsigned x = 0; x < width; x++) {
						const unsigned pixel = (src_bits[x >> 3] & (0x80 >> (x & 0x07))) != 0;
						dst_bits[x] = grey_pal[pixel];
					}
					src_bits += src_pitch;
					dst_bits += dst_pitch;
				}
			}
			break;

			case 4:
			{
				for (unsigned y = 0; y < height; y++) {
					for (unsigned x = 0; x < width; x++) {
						const unsigned pixel = x & 0x01 ? src_bits[x >> 1] & 0x0F : src_bits[x >> 1] >> 4;
						dst_bits[x] = grey_pal[pixel];
					}
					src_bits += src_pitch;
					dst_bits += dst_pitch;
				}
			}
			break;

			case 8:
			{
				for (unsigned y = 0; y < height; y++) {
					for (unsigned x = 0; x < width; x++) {
						dst_bits[x] = grey_pal[src_bits[x]];
					}
					src_bits += src_pitch;
					dst_bits += dst_pitch;
				}
			}
			break;
		}
		return new_dib;
	} 
	
	// Convert the bitmap to 8-bit greyscale
	return FreeImage_ConvertTo8Bits(dib);
}