PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/FreeImage/Source/FreeImage/ConversionRGBF.cpp

https://bitbucket.org/cabalistic/ogredeps/
C++ | 241 lines | 166 code | 34 blank | 41 comment | 24 complexity | d4e6e234933b05cd09d6f107ea3225ef MD5 | raw file
  1// ==========================================================
  2// Bitmap conversion routines
  3//
  4// Design and implementation by
  5// - Hervé Drolon (drolon@infonie.fr)
  6//
  7// This file is part of FreeImage 3
  8//
  9// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
 10// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
 11// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
 12// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
 13// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
 14// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
 15// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
 16// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
 17// THIS DISCLAIMER.
 18//
 19// Use at your own risk!
 20// ==========================================================
 21
 22#include "FreeImage.h"
 23#include "Utilities.h"
 24
 25// ----------------------------------------------------------
 26//   smart convert X to RGBF
 27// ----------------------------------------------------------
 28
 29FIBITMAP * DLL_CALLCONV
 30FreeImage_ConvertToRGBF(FIBITMAP *dib) {
 31	FIBITMAP *src = NULL;
 32	FIBITMAP *dst = NULL;
 33
 34	if(!FreeImage_HasPixels(dib)) return NULL;
 35
 36	const FREE_IMAGE_TYPE src_type = FreeImage_GetImageType(dib);
 37
 38	// check for allowed conversions 
 39	switch(src_type) {
 40		case FIT_BITMAP:
 41		{
 42			// allow conversion from 24- and 32-bit
 43			const FREE_IMAGE_COLOR_TYPE color_type = FreeImage_GetColorType(dib);
 44			if((color_type != FIC_RGB) && (color_type != FIC_RGBALPHA)) {
 45				src = FreeImage_ConvertTo24Bits(dib);
 46				if(!src) return NULL;
 47			} else {
 48				src = dib;
 49			}
 50			break;
 51		}
 52		case FIT_UINT16:
 53			// allow conversion from 16-bit
 54			src = dib;
 55			break;
 56		case FIT_RGB16:
 57			// allow conversion from 48-bit RGB
 58			src = dib;
 59			break;
 60		case FIT_RGBA16:
 61			// allow conversion from 64-bit RGBA (ignore the alpha channel)
 62			src = dib;
 63			break;
 64		case FIT_FLOAT:
 65			// allow conversion from 32-bit float
 66			src = dib;
 67			break;
 68		case FIT_RGBAF:
 69			// allow conversion from 128-bit RGBAF
 70			src = dib;
 71			break;
 72		case FIT_RGBF:
 73			// RGBF type : clone the src
 74			return FreeImage_Clone(dib);
 75			break;
 76		default:
 77			return NULL;
 78	}
 79
 80	// allocate dst image
 81
 82	const unsigned width = FreeImage_GetWidth(src);
 83	const unsigned height = FreeImage_GetHeight(src);
 84
 85	dst = FreeImage_AllocateT(FIT_RGBF, width, height);
 86	if(!dst) {
 87		if(src != dib) {
 88			FreeImage_Unload(src);
 89		}
 90		return NULL;
 91	}
 92
 93	// copy metadata from src to dst
 94	FreeImage_CloneMetadata(dst, src);
 95
 96	// convert from src type to RGBF
 97
 98	const unsigned src_pitch = FreeImage_GetPitch(src);
 99	const unsigned dst_pitch = FreeImage_GetPitch(dst);
100
101	switch(src_type) {
102		case FIT_BITMAP:
103		{
104			// calculate the number of bytes per pixel (3 for 24-bit or 4 for 32-bit)
105			const unsigned bytespp = FreeImage_GetLine(src) / FreeImage_GetWidth(src);
106
107			const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
108			BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
109
110			for(unsigned y = 0; y < height; y++) {
111				const BYTE   *src_pixel = (BYTE*)src_bits;
112				FIRGBF *dst_pixel = (FIRGBF*)dst_bits;
113				for(unsigned x = 0; x < width; x++) {
114					// convert and scale to the range [0..1]
115					dst_pixel->red   = (float)(src_pixel[FI_RGBA_RED])   / 255.0F;
116					dst_pixel->green = (float)(src_pixel[FI_RGBA_GREEN]) / 255.0F;
117					dst_pixel->blue  = (float)(src_pixel[FI_RGBA_BLUE])  / 255.0F;
118
119					src_pixel += bytespp;
120					dst_pixel ++;
121				}
122				src_bits += src_pitch;
123				dst_bits += dst_pitch;
124			}
125		}
126		break;
127
128		case FIT_UINT16:
129		{
130			const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
131			BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
132
133			for(unsigned y = 0; y < height; y++) {
134				const WORD *src_pixel = (WORD*)src_bits;
135				FIRGBF *dst_pixel = (FIRGBF*)dst_bits;
136
137				for(unsigned x = 0; x < width; x++) {
138					// convert and scale to the range [0..1]
139					const float dst_value = (float)src_pixel[x] / 65535.0F;
140					dst_pixel[x].red   = dst_value;
141					dst_pixel[x].green = dst_value;
142					dst_pixel[x].blue  = dst_value;
143				}
144				src_bits += src_pitch;
145				dst_bits += dst_pitch;
146			}
147		}
148		break;
149
150		case FIT_RGB16:
151		{
152			const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
153			BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
154
155			for(unsigned y = 0; y < height; y++) {
156				const FIRGB16 *src_pixel = (FIRGB16*) src_bits;
157				FIRGBF  *dst_pixel = (FIRGBF*)  dst_bits;
158
159				for(unsigned x = 0; x < width; x++) {
160					// convert and scale to the range [0..1]
161					dst_pixel[x].red   = (float)(src_pixel[x].red)   / 65535.0F;
162					dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.0F;
163					dst_pixel[x].blue  = (float)(src_pixel[x].blue)  / 65535.0F;
164				}
165				src_bits += src_pitch;
166				dst_bits += dst_pitch;
167			}
168		}
169		break;
170
171		case FIT_RGBA16:
172		{
173			const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
174			BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
175
176			for(unsigned y = 0; y < height; y++) {
177				const FIRGBA16 *src_pixel = (FIRGBA16*) src_bits;
178				FIRGBF  *dst_pixel = (FIRGBF*)  dst_bits;
179
180				for(unsigned x = 0; x < width; x++) {
181					// convert and scale to the range [0..1]
182					dst_pixel[x].red   = (float)(src_pixel[x].red)   / 65535.0F;
183					dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.0F;
184					dst_pixel[x].blue  = (float)(src_pixel[x].blue)  / 65535.0F;
185				}
186				src_bits += src_pitch;
187				dst_bits += dst_pitch;
188			}
189		}
190		break;
191
192		case FIT_FLOAT:
193		{
194			const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
195			BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
196
197			for(unsigned y = 0; y < height; y++) {
198				const float *src_pixel = (float*) src_bits;
199				FIRGBF  *dst_pixel = (FIRGBF*)  dst_bits;
200
201				for(unsigned x = 0; x < width; x++) {
202					// convert by copying greyscale channel to each R, G, B channels
203					dst_pixel[x].red   = src_pixel[x];
204					dst_pixel[x].green = src_pixel[x];
205					dst_pixel[x].blue  = src_pixel[x];
206				}
207				src_bits += src_pitch;
208				dst_bits += dst_pitch;
209			}
210		}
211		break;
212
213		case FIT_RGBAF:
214		{
215			const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
216			BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
217
218			for(unsigned y = 0; y < height; y++) {
219				const FIRGBAF *src_pixel = (FIRGBAF*) src_bits;
220				FIRGBF  *dst_pixel = (FIRGBF*)  dst_bits;
221
222				for(unsigned x = 0; x < width; x++) {
223					// convert and skip alpha channel
224					dst_pixel[x].red   = src_pixel[x].red;
225					dst_pixel[x].green = src_pixel[x].green;
226					dst_pixel[x].blue  = src_pixel[x].blue;
227				}
228				src_bits += src_pitch;
229				dst_bits += dst_pitch;
230			}
231		}
232		break;
233	}
234
235	if(src != dib) {
236		FreeImage_Unload(src);
237	}
238
239	return dst;
240}
241