PageRenderTime 24ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/FreeImage/Source/FreeImage/ConversionFloat.cpp

https://bitbucket.org/cabalistic/ogredeps/
C++ | 192 lines | 130 code | 27 blank | 35 comment | 24 complexity | 1487a7a054814e8fe8edf26eb59c9b55 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 Float
 27// ----------------------------------------------------------
 28
 29FIBITMAP * DLL_CALLCONV
 30FreeImage_ConvertToFloat(FIBITMAP *dib) {
 31	FIBITMAP *src = NULL;
 32	FIBITMAP *dst = NULL;
 33
 34	if(!FreeImage_HasPixels(dib)) return NULL;
 35
 36	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 8-bit
 43			if((FreeImage_GetBPP(dib) == 8) && (FreeImage_GetColorType(dib) == FIC_MINISBLACK)) {
 44				src = dib;
 45			} else {
 46				src = FreeImage_ConvertToGreyscale(dib);
 47				if(!src) return NULL;
 48			}
 49			break;
 50		}
 51		case FIT_UINT16:
 52		case FIT_RGB16:
 53		case FIT_RGBA16:
 54		case FIT_RGBF:
 55		case FIT_RGBAF:
 56			src = dib;
 57			break;
 58		case FIT_FLOAT:
 59			// float type : clone the src
 60			return FreeImage_Clone(dib);
 61		default:
 62			return NULL;
 63	}
 64
 65	// allocate dst image
 66
 67	const unsigned width = FreeImage_GetWidth(src);
 68	const unsigned height = FreeImage_GetHeight(src);
 69
 70	dst = FreeImage_AllocateT(FIT_FLOAT, width, height);
 71	if(!dst) {
 72		if(src != dib) {
 73			FreeImage_Unload(src);
 74		}
 75		return NULL;
 76	}
 77
 78	// copy metadata from src to dst
 79	FreeImage_CloneMetadata(dst, src);
 80
 81	// convert from src type to float
 82
 83	const unsigned src_pitch = FreeImage_GetPitch(src);
 84	const unsigned dst_pitch = FreeImage_GetPitch(dst);
 85
 86	const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
 87	BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
 88
 89	switch(src_type) {
 90		case FIT_BITMAP:
 91		{
 92			for(unsigned y = 0; y < height; y++) {
 93				const BYTE *src_pixel = (BYTE*)src_bits;
 94				float *dst_pixel = (float*)dst_bits;
 95				for(unsigned x = 0; x < width; x++) {
 96					// convert and scale to the range [0..1]
 97					dst_pixel[x] = (float)(src_pixel[x]) / 255;
 98				}
 99				src_bits += src_pitch;
100				dst_bits += dst_pitch;
101			}
102		}
103		break;
104
105		case FIT_UINT16:
106		{
107			for(unsigned y = 0; y < height; y++) {
108				const WORD *src_pixel = (WORD*)src_bits;
109				float *dst_pixel = (float*)dst_bits;
110
111				for(unsigned x = 0; x < width; x++) {
112					// convert and scale to the range [0..1]
113					dst_pixel[x] = (float)(src_pixel[x]) / 65535;
114				}
115				src_bits += src_pitch;
116				dst_bits += dst_pitch;
117			}
118		}
119		break;
120
121		case FIT_RGB16:
122		{
123			for(unsigned y = 0; y < height; y++) {
124				const FIRGB16 *src_pixel = (FIRGB16*)src_bits;
125				float *dst_pixel = (float*)dst_bits;
126
127				for(unsigned x = 0; x < width; x++) {
128					// convert and scale to the range [0..1]
129					dst_pixel[x] = LUMA_REC709(src_pixel[x].red, src_pixel[x].green, src_pixel[x].blue) / 65535.0F;
130				}
131				src_bits += src_pitch;
132				dst_bits += dst_pitch;
133			}
134		}
135		break;
136
137		case FIT_RGBA16:
138		{
139			for(unsigned y = 0; y < height; y++) {
140				const FIRGBA16 *src_pixel = (FIRGBA16*)src_bits;
141				float *dst_pixel = (float*)dst_bits;
142
143				for(unsigned x = 0; x < width; x++) {
144					// convert and scale to the range [0..1]
145					dst_pixel[x] = LUMA_REC709(src_pixel[x].red, src_pixel[x].green, src_pixel[x].blue) / 65535.0F;
146				}
147				src_bits += src_pitch;
148				dst_bits += dst_pitch;
149			}
150		}
151		break;
152
153		case FIT_RGBF:
154		{
155			for(unsigned y = 0; y < height; y++) {
156				const FIRGBF *src_pixel = (FIRGBF*)src_bits;
157				float *dst_pixel = (float*)dst_bits;
158
159				for(unsigned x = 0; x < width; x++) {
160					// convert (assume pixel values are in the range [0..1])
161					dst_pixel[x] = LUMA_REC709(src_pixel[x].red, src_pixel[x].green, src_pixel[x].blue);
162				}
163				src_bits += src_pitch;
164				dst_bits += dst_pitch;
165			}
166		}
167		break;
168
169		case FIT_RGBAF:
170		{
171			for(unsigned y = 0; y < height; y++) {
172				const FIRGBAF *src_pixel = (FIRGBAF*)src_bits;
173				float *dst_pixel = (float*)dst_bits;
174
175				for(unsigned x = 0; x < width; x++) {
176					// convert (assume pixel values are in the range [0..1])
177					dst_pixel[x] = LUMA_REC709(src_pixel[x].red, src_pixel[x].green, src_pixel[x].blue);
178				}
179				src_bits += src_pitch;
180				dst_bits += dst_pitch;
181			}
182		}
183		break;
184	}
185
186	if(src != dib) {
187		FreeImage_Unload(src);
188	}
189
190	return dst;
191}
192