1 // ==========================================================
2 // Upsampling / downsampling routine
3 //
4 // Design and implementation by
5 // - Herv� Drolon (drolon@infonie.fr)
6 // - Carsten Klein (cklein05@users.sourceforge.net)
7 //
8 // This file is part of FreeImage 3
9 //
10 // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
11 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
12 // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
13 // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
14 // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
15 // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
16 // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
17 // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
18 // THIS DISCLAIMER.
19 //
20 // Use at your own risk!
21 // ==========================================================
22 
23 #include "Resize.h"
24 
25 FIBITMAP * DLL_CALLCONV
FreeImage_RescaleRect(FIBITMAP * src,int dst_width,int dst_height,int src_left,int src_top,int src_right,int src_bottom,FREE_IMAGE_FILTER filter,unsigned flags)26 FreeImage_RescaleRect(FIBITMAP *src, int dst_width, int dst_height, int src_left, int src_top, int src_right, int src_bottom, FREE_IMAGE_FILTER filter, unsigned flags) {
27 	FIBITMAP *dst = NULL;
28 
29 	const int src_width = FreeImage_GetWidth(src);
30 	const int src_height = FreeImage_GetHeight(src);
31 
32 	if (!FreeImage_HasPixels(src) || (dst_width <= 0) || (dst_height <= 0) || (src_width <= 0) || (src_height <= 0)) {
33 		return NULL;
34 	}
35 
36 	// normalize the rectangle
37 	if (src_right < src_left) {
38 		INPLACESWAP(src_left, src_right);
39 	}
40 	if (src_bottom < src_top) {
41 		INPLACESWAP(src_top, src_bottom);
42 	}
43 
44 	// check the size of the sub image
45 	if((src_left < 0) || (src_right > src_width) || (src_top < 0) || (src_bottom > src_height)) {
46 		return NULL;
47 	}
48 
49 	// select the filter
50 	CGenericFilter *pFilter = NULL;
51 	switch (filter) {
52 		case FILTER_BOX:
53 			pFilter = new(std::nothrow) CBoxFilter();
54 			break;
55 		case FILTER_BICUBIC:
56 			pFilter = new(std::nothrow) CBicubicFilter();
57 			break;
58 		case FILTER_BILINEAR:
59 			pFilter = new(std::nothrow) CBilinearFilter();
60 			break;
61 		case FILTER_BSPLINE:
62 			pFilter = new(std::nothrow) CBSplineFilter();
63 			break;
64 		case FILTER_CATMULLROM:
65 			pFilter = new(std::nothrow) CCatmullRomFilter();
66 			break;
67 		case FILTER_LANCZOS3:
68 			pFilter = new(std::nothrow) CLanczos3Filter();
69 			break;
70 	}
71 
72 	if (!pFilter) {
73 		return NULL;
74 	}
75 
76 	CResizeEngine Engine(pFilter);
77 
78 	dst = Engine.scale(src, dst_width, dst_height, src_left, src_top,
79 			src_right - src_left, src_bottom - src_top, flags);
80 
81 	delete pFilter;
82 
83 	if ((flags & FI_RESCALE_OMIT_METADATA) != FI_RESCALE_OMIT_METADATA) {
84 		// copy metadata from src to dst
85 		FreeImage_CloneMetadata(dst, src);
86 	}
87 
88 	return dst;
89 }
90 
91 FIBITMAP * DLL_CALLCONV
FreeImage_Rescale(FIBITMAP * src,int dst_width,int dst_height,FREE_IMAGE_FILTER filter)92 FreeImage_Rescale(FIBITMAP *src, int dst_width, int dst_height, FREE_IMAGE_FILTER filter) {
93 	return FreeImage_RescaleRect(src, dst_width, dst_height, 0, 0, FreeImage_GetWidth(src), FreeImage_GetHeight(src), filter, FI_RESCALE_DEFAULT);
94 }
95 
96 FIBITMAP * DLL_CALLCONV
FreeImage_MakeThumbnail(FIBITMAP * dib,int max_pixel_size,BOOL convert)97 FreeImage_MakeThumbnail(FIBITMAP *dib, int max_pixel_size, BOOL convert) {
98 	FIBITMAP *thumbnail = NULL;
99 	int new_width, new_height;
100 
101 	if(!FreeImage_HasPixels(dib) || (max_pixel_size <= 0)) return NULL;
102 
103 	int width	= FreeImage_GetWidth(dib);
104 	int height = FreeImage_GetHeight(dib);
105 
106 	if(max_pixel_size == 0) max_pixel_size = 1;
107 
108 	if((width < max_pixel_size) && (height < max_pixel_size)) {
109 		// image is smaller than the requested thumbnail
110 		return FreeImage_Clone(dib);
111 	}
112 
113 	if(width > height) {
114 		new_width = max_pixel_size;
115 		// change image height with the same ratio
116 		double ratio = ((double)new_width / (double)width);
117 		new_height = (int)(height * ratio + 0.5);
118 		if(new_height == 0) new_height = 1;
119 	} else {
120 		new_height = max_pixel_size;
121 		// change image width with the same ratio
122 		double ratio = ((double)new_height / (double)height);
123 		new_width = (int)(width * ratio + 0.5);
124 		if(new_width == 0) new_width = 1;
125 	}
126 
127 	const FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
128 
129 	// perform downsampling using a bilinear interpolation
130 
131 	switch(image_type) {
132 		case FIT_BITMAP:
133 		case FIT_UINT16:
134 		case FIT_RGB16:
135 		case FIT_RGBA16:
136 		case FIT_FLOAT:
137 		case FIT_RGBF:
138 		case FIT_RGBAF:
139 		{
140 			FREE_IMAGE_FILTER filter = FILTER_BILINEAR;
141 			thumbnail = FreeImage_Rescale(dib, new_width, new_height, filter);
142 		}
143 		break;
144 
145 		case FIT_INT16:
146 		case FIT_UINT32:
147 		case FIT_INT32:
148 		case FIT_DOUBLE:
149 		case FIT_COMPLEX:
150 		default:
151 			// cannot rescale this kind of image
152 			thumbnail = NULL;
153 			break;
154 	}
155 
156 	if((thumbnail != NULL) && (image_type != FIT_BITMAP) && convert) {
157 		// convert to a standard bitmap
158 		FIBITMAP *bitmap = NULL;
159 		switch(image_type) {
160 			case FIT_UINT16:
161 				bitmap = FreeImage_ConvertTo8Bits(thumbnail);
162 				break;
163 			case FIT_RGB16:
164 				bitmap = FreeImage_ConvertTo24Bits(thumbnail);
165 				break;
166 			case FIT_RGBA16:
167 				bitmap = FreeImage_ConvertTo32Bits(thumbnail);
168 				break;
169 			case FIT_FLOAT:
170 				bitmap = FreeImage_ConvertToStandardType(thumbnail, TRUE);
171 				break;
172 			case FIT_RGBF:
173 				bitmap = FreeImage_ToneMapping(thumbnail, FITMO_DRAGO03);
174 				break;
175 			case FIT_RGBAF:
176 				// no way to keep the transparency yet ...
177 				FIBITMAP *rgbf = FreeImage_ConvertToRGBF(thumbnail);
178 				bitmap = FreeImage_ToneMapping(rgbf, FITMO_DRAGO03);
179 				FreeImage_Unload(rgbf);
180 				break;
181 		}
182 		if(bitmap != NULL) {
183 			FreeImage_Unload(thumbnail);
184 			thumbnail = bitmap;
185 		}
186 	}
187 
188 	// copy metadata from src to dst
189 	FreeImage_CloneMetadata(thumbnail, dib);
190 
191 	return thumbnail;
192 }
193