1 /* scale.c - image scaling
3 * Raster graphics library
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #define PI 3.14159265358979323846
39 *----------------------------------------------------------------------
41 * Creates a scaled copy of an image.
44 * The new scaled image.
46 *----------------------------------------------------------------------
48 RImage
*RScaleImage(RImage
* image
, unsigned new_width
, unsigned new_height
)
58 assert(new_width
>= 0 && new_height
>= 0);
60 if (new_width
== image
->width
&& new_height
== image
->height
)
61 return RCloneImage(image
);
63 img
= RCreateImage(new_width
, new_height
, image
->format
== RRGBAFormat
);
68 /* fixed point math idea taken from Imlib by
69 * Carsten Haitzler (Rasterman) */
70 dx
= (image
->width
<< 16) / new_width
;
71 dy
= (image
->height
<< 16) / new_height
;
77 if (image
->format
== RRGBAFormat
) {
78 for (y
= 0; y
< new_height
; y
++) {
79 t
= image
->width
* (py
>> 16);
81 s
= image
->data
+ (t
<< 2); /* image->data+t*4 */
85 for (x
= 0; x
< new_width
; x
++) {
96 s
+= t
<< 2; /* t*4 */
101 for (y
= 0; y
< new_height
; y
++) {
102 t
= image
->width
* (py
>> 16);
104 s
= image
->data
+ (t
<< 1) + t
; /* image->data+t*3 */
108 for (x
= 0; x
< new_width
; x
++) {
118 s
+= (t
<< 1) + t
; /* t*3 */
128 * Filtered Image Rescaling code copy/pasted from
130 * Public Domain 1991 by Dale Schumacher
134 * filter function definitions
136 #define box_support (0.5)
138 static double box_filter(double t
)
140 if ((t
> -0.5) && (t
<= 0.5))
145 #define triangle_support (1.0)
147 static double triangle_filter(double t
)
156 #define bell_support (1.5)
158 static double bell_filter(double t
) /* box (*) box (*) box */
163 return (.75 - (t
* t
));
166 return (.5 * (t
* t
));
171 #define B_spline_support (2.0)
173 static double B_spline_filter(double t
) /* box (*) box (*) box (*) box */
181 return ((.5 * tt
* t
) - tt
+ (2.0 / 3.0));
184 return ((1.0 / 6.0) * (t
* t
* t
));
189 static double sinc(double x
)
197 #define Lanczos3_support (3.0)
199 static double Lanczos3_filter(double t
)
204 return (sinc(t
) * sinc(t
/ 3.0));
208 #define Mitchell_support (2.0)
210 #define B (1.0 / 3.0)
211 #define C (1.0 / 3.0)
213 static double Mitchell_filter(double t
)
221 t
= (((12.0 - 9.0 * B
- 6.0 * C
) * (t
* tt
))
222 + ((-18.0 + 12.0 * B
+ 6.0 * C
) * tt
)
225 } else if (t
< 2.0) {
226 t
= (((-1.0 * B
- 6.0 * C
) * (t
* tt
))
227 + ((6.0 * B
+ 30.0 * C
) * tt
)
228 + ((-12.0 * B
- 48.0 * C
) * t
)
229 + (8.0 * B
+ 24 * C
));
235 static double (*filterf
)(double) = Mitchell_filter
;
236 static double fwidth
= Mitchell_support
;
238 void _wraster_change_filter(int type
)
242 filterf
= box_filter
;
243 fwidth
= box_support
;
245 case RTriangleFilter
:
246 filterf
= triangle_filter
;
247 fwidth
= triangle_support
;
250 filterf
= bell_filter
;
251 fwidth
= bell_support
;
254 filterf
= B_spline_filter
;
255 fwidth
= B_spline_support
;
257 case RLanczos3Filter
:
258 filterf
= Lanczos3_filter
;
259 fwidth
= Lanczos3_support
;
262 case RMitchellFilter
:
263 filterf
= Mitchell_filter
;
264 fwidth
= Mitchell_support
;
270 * image rescaling routine
279 int n
; /* number of contributors */
280 CONTRIB
*p
; /* pointer to list of contributions */
283 CLIST
*contrib
; /* array of contribution lists */
285 /* clamp the input to the specified range */
286 #define CLAMP(v,l,h) ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
288 /* return of calloc is not checked if NULL in the function below! */
289 RImage
*RSmoothScaleImage(RImage
* src
, unsigned new_width
, unsigned new_height
)
291 RImage
*tmp
; /* intermediate image */
292 double xscale
, yscale
; /* zoom scale factors */
293 int i
, j
, k
; /* loop variables */
294 int n
; /* pixel number */
295 double center
, left
, right
; /* filter calculation variables */
296 double width
, fscale
; /* filter calculation variables */
297 double rweight
, gweight
, bweight
;
301 int sch
= src
->format
== RRGBAFormat
? 4 : 3;
303 dst
= RCreateImage(new_width
, new_height
, False
);
305 /* create intermediate image to hold horizontal zoom */
306 tmp
= RCreateImage(dst
->width
, src
->height
, False
);
307 xscale
= (double)new_width
/ (double)src
->width
;
308 yscale
= (double)new_height
/ (double)src
->height
;
310 /* pre-calculate filter contributions for a row */
311 contrib
= (CLIST
*) calloc(new_width
, sizeof(CLIST
));
313 width
= fwidth
/ xscale
;
314 fscale
= 1.0 / xscale
;
315 for (i
= 0; i
< new_width
; ++i
) {
317 contrib
[i
].p
= (CONTRIB
*) calloc((int) ceil(width
* 2 + 1), sizeof(CONTRIB
));
318 center
= (double)i
/ xscale
;
319 left
= ceil(center
- width
);
320 right
= floor(center
+ width
);
321 for (j
= left
; j
<= right
; ++j
) {
322 rweight
= center
- (double)j
;
323 rweight
= (*filterf
) (rweight
/ fscale
) / fscale
;
326 } else if (j
>= src
->width
) {
327 n
= (src
->width
- j
) + src
->width
- 1;
332 contrib
[i
].p
[k
].pixel
= n
* sch
;
333 contrib
[i
].p
[k
].weight
= rweight
;
338 for (i
= 0; i
< new_width
; ++i
) {
340 contrib
[i
].p
= (CONTRIB
*) calloc((int) ceil(fwidth
* 2 + 1), sizeof(CONTRIB
));
341 center
= (double)i
/ xscale
;
342 left
= ceil(center
- fwidth
);
343 right
= floor(center
+ fwidth
);
344 for (j
= left
; j
<= right
; ++j
) {
345 rweight
= center
- (double)j
;
346 rweight
= (*filterf
) (rweight
);
349 } else if (j
>= src
->width
) {
350 n
= (src
->width
- j
) + src
->width
- 1;
355 contrib
[i
].p
[k
].pixel
= n
* sch
;
356 contrib
[i
].p
[k
].weight
= rweight
;
361 /* apply filter to zoom horizontally from src to tmp */
364 for (k
= 0; k
< tmp
->height
; ++k
) {
367 sp
= src
->data
+ src
->width
* k
* sch
;
369 for (i
= 0; i
< tmp
->width
; ++i
) {
370 rweight
= gweight
= bweight
= 0.0;
374 for (j
= 0; j
< contrib
[i
].n
; ++j
) {
375 rweight
+= sp
[pp
[j
].pixel
] * pp
[j
].weight
;
376 gweight
+= sp
[pp
[j
].pixel
+ 1] * pp
[j
].weight
;
377 bweight
+= sp
[pp
[j
].pixel
+ 2] * pp
[j
].weight
;
379 *p
++ = CLAMP(rweight
, 0, 255);
380 *p
++ = CLAMP(gweight
, 0, 255);
381 *p
++ = CLAMP(bweight
, 0, 255);
385 /* free the memory allocated for horizontal filter weights */
386 for (i
= 0; i
< tmp
->width
; ++i
) {
391 /* pre-calculate filter contributions for a column */
392 contrib
= (CLIST
*) calloc(dst
->height
, sizeof(CLIST
));
394 width
= fwidth
/ yscale
;
395 fscale
= 1.0 / yscale
;
396 for (i
= 0; i
< dst
->height
; ++i
) {
398 contrib
[i
].p
= (CONTRIB
*) calloc((int) ceil(width
* 2 + 1), sizeof(CONTRIB
));
399 center
= (double)i
/ yscale
;
400 left
= ceil(center
- width
);
401 right
= floor(center
+ width
);
402 for (j
= left
; j
<= right
; ++j
) {
403 rweight
= center
- (double)j
;
404 rweight
= (*filterf
) (rweight
/ fscale
) / fscale
;
407 } else if (j
>= tmp
->height
) {
408 n
= (tmp
->height
- j
) + tmp
->height
- 1;
413 contrib
[i
].p
[k
].pixel
= n
* 3;
414 contrib
[i
].p
[k
].weight
= rweight
;
418 for (i
= 0; i
< dst
->height
; ++i
) {
420 contrib
[i
].p
= (CONTRIB
*) calloc((int) ceil(fwidth
* 2 + 1), sizeof(CONTRIB
));
421 center
= (double)i
/ yscale
;
422 left
= ceil(center
- fwidth
);
423 right
= floor(center
+ fwidth
);
424 for (j
= left
; j
<= right
; ++j
) {
425 rweight
= center
- (double)j
;
426 rweight
= (*filterf
) (rweight
);
429 } else if (j
>= tmp
->height
) {
430 n
= (tmp
->height
- j
) + tmp
->height
- 1;
435 contrib
[i
].p
[k
].pixel
= n
* 3;
436 contrib
[i
].p
[k
].weight
= rweight
;
441 /* apply filter to zoom vertically from tmp to dst */
442 sp
= malloc(tmp
->height
* 3);
444 for (k
= 0; k
< new_width
; ++k
) {
447 p
= dst
->data
+ k
* 3;
449 /* copy a column into a row */
452 unsigned char *p
, *d
;
455 for (i
= tmp
->height
, p
= tmp
->data
+ k
* 3; i
-- > 0; p
+= tmp
->width
* 3) {
461 for (i
= 0; i
< new_height
; ++i
) {
462 rweight
= gweight
= bweight
= 0.0;
466 for (j
= 0; j
< contrib
[i
].n
; ++j
) {
467 rweight
+= sp
[pp
[j
].pixel
] * pp
[j
].weight
;
468 gweight
+= sp
[pp
[j
].pixel
+ 1] * pp
[j
].weight
;
469 bweight
+= sp
[pp
[j
].pixel
+ 2] * pp
[j
].weight
;
471 *p
= CLAMP(rweight
, 0, 255);
472 *(p
+ 1) = CLAMP(gweight
, 0, 255);
473 *(p
+ 2) = CLAMP(bweight
, 0, 255);
479 /* free the memory allocated for vertical filter weights */
480 for (i
= 0; i
< dst
->height
; ++i
) {