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

/src/FreeImage/Source/FreeImageToolkit/Rescale.cpp

https://bitbucket.org/cabalistic/ogredeps/
C++ | 231 lines | 159 code | 25 blank | 47 comment | 50 complexity | 86aab30835691fb04f7a66cbb1f8bab1 MD5 | raw file
  1// ==========================================================
  2// Upsampling / downsampling routine
  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 "Resize.h"
 23
 24FIBITMAP * DLL_CALLCONV 
 25FreeImage_Rescale(FIBITMAP *src, int dst_width, int dst_height, FREE_IMAGE_FILTER filter) {
 26	FIBITMAP *dst = NULL;
 27
 28	if (!FreeImage_HasPixels(src) || (dst_width <= 0) || (dst_height <= 0) || (FreeImage_GetWidth(src) <= 0) || (FreeImage_GetHeight(src) <= 0)) {
 29		return NULL;
 30	}
 31
 32	// select the filter
 33	CGenericFilter *pFilter = NULL;
 34	switch(filter) {
 35		case FILTER_BOX:
 36			pFilter = new(std::nothrow) CBoxFilter();
 37			break;
 38		case FILTER_BICUBIC:
 39			pFilter = new(std::nothrow) CBicubicFilter();
 40			break;
 41		case FILTER_BILINEAR:
 42			pFilter = new(std::nothrow) CBilinearFilter();
 43			break;
 44		case FILTER_BSPLINE:
 45			pFilter = new(std::nothrow) CBSplineFilter();
 46			break;
 47		case FILTER_CATMULLROM:
 48			pFilter = new(std::nothrow) CCatmullRomFilter();
 49			break;
 50		case FILTER_LANCZOS3:
 51			pFilter = new(std::nothrow) CLanczos3Filter();
 52			break;
 53	}
 54
 55	if(!pFilter) {
 56		return NULL;
 57	}
 58
 59	CResizeEngine Engine(pFilter);
 60
 61	// perform upsampling or downsampling
 62
 63	if((FreeImage_GetBPP(src) == 4) || (FreeImage_GetColorType(src) == FIC_PALETTE)) {
 64		// special case for 4-bit images or color map indexed images ...
 65		if(FreeImage_IsTransparent(src) == FALSE) {
 66			FIBITMAP *src24 = NULL;
 67			FIBITMAP *dst24 = NULL;
 68			try {
 69				// transparent conversion to 24-bit (any transparency table will be destroyed)
 70				src24 = FreeImage_ConvertTo24Bits(src);
 71				if(!src24) throw(1);
 72				// perform upsampling or downsampling
 73				dst24 = Engine.scale(src24, dst_width, dst_height);
 74				if(!dst24) throw(1);
 75				FreeImage_Unload(src24); src24 = NULL;
 76				// color quantize to 8-bit
 77				dst = FreeImage_ColorQuantize(dst24, FIQ_NNQUANT);
 78				// free and return
 79				FreeImage_Unload(dst24);
 80			} catch(int) {
 81				if(src24) FreeImage_Unload(src24);
 82				if(dst24) FreeImage_Unload(dst24);
 83			}
 84		} else {
 85			FIBITMAP *src32 = NULL;
 86			try {
 87				// transparent conversion to 32-bit (keep transparency)
 88				src32 = FreeImage_ConvertTo32Bits(src);
 89				if(!src32) throw(1);
 90				// perform upsampling or downsampling
 91				dst = Engine.scale(src32, dst_width, dst_height);
 92				if(!dst) throw(1);
 93				// free and return
 94				FreeImage_Unload(src32);
 95			} catch(int) {
 96				if(src32) FreeImage_Unload(src32);
 97				if(dst) FreeImage_Unload(dst);
 98			}
 99		}
100	}
101	else if((FreeImage_GetBPP(src) == 16) && (FreeImage_GetImageType(src) == FIT_BITMAP)) {
102		// convert 16-bit RGB to 24-bit
103		FIBITMAP *src24 = NULL;
104		try {
105			// transparent conversion to 24-bit (any transparency table will be destroyed)
106			src24 = FreeImage_ConvertTo24Bits(src);
107			if(!src24) throw(1);
108			// perform upsampling or downsampling
109			dst = Engine.scale(src24, dst_width, dst_height);
110			if(!dst) throw(1);
111			// free and return
112			FreeImage_Unload(src24);
113		} catch(int) {
114			if(src24) FreeImage_Unload(src24);
115			if(dst) FreeImage_Unload(dst);
116		}
117	}
118	else {
119		// normal case : 
120		// 1- or 8-bit greyscale, 24- or 32-bit RGB(A) images
121		// 16-bit greyscale, 48- or 64-bit RGB(A) images
122		// 32-bit float, 96- or 128-bit RGB(A) float images
123		dst = Engine.scale(src, dst_width, dst_height);
124	}
125
126
127	delete pFilter;
128
129	// copy metadata from src to dst
130	FreeImage_CloneMetadata(dst, src);
131	
132	return dst;
133}
134
135FIBITMAP * DLL_CALLCONV 
136FreeImage_MakeThumbnail(FIBITMAP *dib, int max_pixel_size, BOOL convert) {
137	FIBITMAP *thumbnail = NULL;
138	int new_width, new_height;
139
140	if(!FreeImage_HasPixels(dib) || (max_pixel_size <= 0)) return NULL;
141
142	int width	= FreeImage_GetWidth(dib);
143	int height = FreeImage_GetHeight(dib);
144
145	if(max_pixel_size == 0) max_pixel_size = 1;
146
147	if((width < max_pixel_size) && (height < max_pixel_size)) {
148		// image is smaller than the requested thumbnail
149		return FreeImage_Clone(dib);
150	}
151
152	if(width > height) {
153		new_width = max_pixel_size;
154		// change image height with the same ratio
155		double ratio = ((double)new_width / (double)width);
156		new_height = (int)(height * ratio + 0.5);
157		if(new_height == 0) new_height = 1;
158	} else {
159		new_height = max_pixel_size;
160		// change image width with the same ratio
161		double ratio = ((double)new_height / (double)height);
162		new_width = (int)(width * ratio + 0.5);
163		if(new_width == 0) new_width = 1;
164	}
165
166	const FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
167
168	// perform downsampling using a bilinear interpolation
169
170	switch(image_type) {
171		case FIT_BITMAP:
172		case FIT_UINT16:
173		case FIT_RGB16:
174		case FIT_RGBA16:
175		case FIT_FLOAT:
176		case FIT_RGBF:
177		case FIT_RGBAF:
178		{
179			FREE_IMAGE_FILTER filter = FILTER_BILINEAR;
180			thumbnail = FreeImage_Rescale(dib, new_width, new_height, filter);
181		}
182		break;
183
184		case FIT_INT16:
185		case FIT_UINT32:
186		case FIT_INT32:
187		case FIT_DOUBLE:
188		case FIT_COMPLEX:
189		default:
190			// cannot rescale this kind of image
191			thumbnail = NULL;
192			break;
193	}
194
195	if((thumbnail != NULL) && (image_type != FIT_BITMAP) && convert) {
196		// convert to a standard bitmap
197		FIBITMAP *bitmap = NULL;
198		switch(image_type) {
199			case FIT_UINT16:
200				bitmap = FreeImage_ConvertTo8Bits(thumbnail);
201				break;
202			case FIT_RGB16:
203				bitmap = FreeImage_ConvertTo24Bits(thumbnail);
204				break;
205			case FIT_RGBA16:
206				bitmap = FreeImage_ConvertTo32Bits(thumbnail);
207				break;
208			case FIT_FLOAT:
209				bitmap = FreeImage_ConvertToStandardType(thumbnail, TRUE);
210				break;
211			case FIT_RGBF:
212				bitmap = FreeImage_ToneMapping(thumbnail, FITMO_DRAGO03);
213				break;
214			case FIT_RGBAF:
215				// no way to keep the transparency yet ...
216				FIBITMAP *rgbf = FreeImage_ConvertToRGBF(thumbnail);
217				bitmap = FreeImage_ToneMapping(rgbf, FITMO_DRAGO03);
218				FreeImage_Unload(rgbf);
219				break;
220		}
221		if(bitmap != NULL) {
222			FreeImage_Unload(thumbnail);
223			thumbnail = bitmap;
224		}
225	}
226
227	// copy metadata from src to dst
228	FreeImage_CloneMetadata(thumbnail, dib);
229	
230	return thumbnail;
231}