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
137 #define filter_support (1.0)
139 static double filter(double t
)
141 /* f(t) = 2|t|^3 - 3|t|^2 + 1, -1 <= t <= 1 */
145 return ((2.0 * t
- 3.0) * t
* t
+ 1.0);
149 #define box_support (0.5)
151 static double box_filter(double t
)
153 if ((t
> -0.5) && (t
<= 0.5))
158 #define triangle_support (1.0)
160 static double triangle_filter(double t
)
169 #define bell_support (1.5)
171 static double bell_filter(double t
) /* box (*) box (*) box */
176 return (.75 - (t
* t
));
179 return (.5 * (t
* t
));
184 #define B_spline_support (2.0)
186 static double B_spline_filter(double t
) /* box (*) box (*) box (*) box */
194 return ((.5 * tt
* t
) - tt
+ (2.0 / 3.0));
197 return ((1.0 / 6.0) * (t
* t
* t
));
202 static double sinc(double x
)
210 #define Lanczos3_support (3.0)
212 static double Lanczos3_filter(double t
)
217 return (sinc(t
) * sinc(t
/ 3.0));
221 #define Mitchell_support (2.0)
223 #define B (1.0 / 3.0)
224 #define C (1.0 / 3.0)
226 static double Mitchell_filter(double t
)
234 t
= (((12.0 - 9.0 * B
- 6.0 * C
) * (t
* tt
))
235 + ((-18.0 + 12.0 * B
+ 6.0 * C
) * tt
)
238 } else if (t
< 2.0) {
239 t
= (((-1.0 * B
- 6.0 * C
) * (t
* tt
))
240 + ((6.0 * B
+ 30.0 * C
) * tt
)
241 + ((-12.0 * B
- 48.0 * C
) * t
)
242 + (8.0 * B
+ 24 * C
));
248 static double (*filterf
)(double) = Mitchell_filter
;
249 static double fwidth
= Mitchell_support
;
251 void _wraster_change_filter(int type
)
255 filterf
= box_filter
;
256 fwidth
= box_support
;
258 case RTriangleFilter
:
259 filterf
= triangle_filter
;
260 fwidth
= triangle_support
;
263 filterf
= bell_filter
;
264 fwidth
= bell_support
;
267 filterf
= B_spline_filter
;
268 fwidth
= B_spline_support
;
270 case RLanczos3Filter
:
271 filterf
= Lanczos3_filter
;
272 fwidth
= Lanczos3_support
;
275 case RMitchellFilter
:
276 filterf
= Mitchell_filter
;
277 fwidth
= Mitchell_support
;
283 * image rescaling routine
292 int n
; /* number of contributors */
293 CONTRIB
*p
; /* pointer to list of contributions */
296 CLIST
*contrib
; /* array of contribution lists */
298 /* clamp the input to the specified range */
299 #define CLAMP(v,l,h) ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
301 /* return of calloc is not checked if NULL in the function below! */
302 RImage
*RSmoothScaleImage(RImage
* src
, unsigned new_width
, unsigned new_height
)
304 RImage
*tmp
; /* intermediate image */
305 double xscale
, yscale
; /* zoom scale factors */
306 int i
, j
, k
; /* loop variables */
307 int n
; /* pixel number */
308 double center
, left
, right
; /* filter calculation variables */
309 double width
, fscale
; /* filter calculation variables */
310 double rweight
, gweight
, bweight
;
314 int sch
= src
->format
== RRGBAFormat
? 4 : 3;
316 dst
= RCreateImage(new_width
, new_height
, False
);
318 /* create intermediate image to hold horizontal zoom */
319 tmp
= RCreateImage(dst
->width
, src
->height
, False
);
320 xscale
= (double)new_width
/ (double)src
->width
;
321 yscale
= (double)new_height
/ (double)src
->height
;
323 /* pre-calculate filter contributions for a row */
324 contrib
= (CLIST
*) calloc(new_width
, sizeof(CLIST
));
326 width
= fwidth
/ xscale
;
327 fscale
= 1.0 / xscale
;
328 for (i
= 0; i
< new_width
; ++i
) {
330 contrib
[i
].p
= (CONTRIB
*) calloc((int) ceil(width
* 2 + 1), sizeof(CONTRIB
));
331 center
= (double)i
/ xscale
;
332 left
= ceil(center
- width
);
333 right
= floor(center
+ width
);
334 for (j
= left
; j
<= right
; ++j
) {
335 rweight
= center
- (double)j
;
336 rweight
= (*filterf
) (rweight
/ fscale
) / fscale
;
339 } else if (j
>= src
->width
) {
340 n
= (src
->width
- j
) + src
->width
- 1;
345 contrib
[i
].p
[k
].pixel
= n
* sch
;
346 contrib
[i
].p
[k
].weight
= rweight
;
351 for (i
= 0; i
< new_width
; ++i
) {
353 contrib
[i
].p
= (CONTRIB
*) calloc((int) ceil(fwidth
* 2 + 1), sizeof(CONTRIB
));
354 center
= (double)i
/ xscale
;
355 left
= ceil(center
- fwidth
);
356 right
= floor(center
+ fwidth
);
357 for (j
= left
; j
<= right
; ++j
) {
358 rweight
= center
- (double)j
;
359 rweight
= (*filterf
) (rweight
);
362 } else if (j
>= src
->width
) {
363 n
= (src
->width
- j
) + src
->width
- 1;
368 contrib
[i
].p
[k
].pixel
= n
* sch
;
369 contrib
[i
].p
[k
].weight
= rweight
;
374 /* apply filter to zoom horizontally from src to tmp */
377 for (k
= 0; k
< tmp
->height
; ++k
) {
380 sp
= src
->data
+ src
->width
* k
* sch
;
382 for (i
= 0; i
< tmp
->width
; ++i
) {
383 rweight
= gweight
= bweight
= 0.0;
387 for (j
= 0; j
< contrib
[i
].n
; ++j
) {
388 rweight
+= sp
[pp
[j
].pixel
] * pp
[j
].weight
;
389 gweight
+= sp
[pp
[j
].pixel
+ 1] * pp
[j
].weight
;
390 bweight
+= sp
[pp
[j
].pixel
+ 2] * pp
[j
].weight
;
392 *p
++ = CLAMP(rweight
, 0, 255);
393 *p
++ = CLAMP(gweight
, 0, 255);
394 *p
++ = CLAMP(bweight
, 0, 255);
398 /* free the memory allocated for horizontal filter weights */
399 for (i
= 0; i
< tmp
->width
; ++i
) {
404 /* pre-calculate filter contributions for a column */
405 contrib
= (CLIST
*) calloc(dst
->height
, sizeof(CLIST
));
407 width
= fwidth
/ yscale
;
408 fscale
= 1.0 / yscale
;
409 for (i
= 0; i
< dst
->height
; ++i
) {
411 contrib
[i
].p
= (CONTRIB
*) calloc((int) ceil(width
* 2 + 1), sizeof(CONTRIB
));
412 center
= (double)i
/ yscale
;
413 left
= ceil(center
- width
);
414 right
= floor(center
+ width
);
415 for (j
= left
; j
<= right
; ++j
) {
416 rweight
= center
- (double)j
;
417 rweight
= (*filterf
) (rweight
/ fscale
) / fscale
;
420 } else if (j
>= tmp
->height
) {
421 n
= (tmp
->height
- j
) + tmp
->height
- 1;
426 contrib
[i
].p
[k
].pixel
= n
* 3;
427 contrib
[i
].p
[k
].weight
= rweight
;
431 for (i
= 0; i
< dst
->height
; ++i
) {
433 contrib
[i
].p
= (CONTRIB
*) calloc((int) ceil(fwidth
* 2 + 1), sizeof(CONTRIB
));
434 center
= (double)i
/ yscale
;
435 left
= ceil(center
- fwidth
);
436 right
= floor(center
+ fwidth
);
437 for (j
= left
; j
<= right
; ++j
) {
438 rweight
= center
- (double)j
;
439 rweight
= (*filterf
) (rweight
);
442 } else if (j
>= tmp
->height
) {
443 n
= (tmp
->height
- j
) + tmp
->height
- 1;
448 contrib
[i
].p
[k
].pixel
= n
* 3;
449 contrib
[i
].p
[k
].weight
= rweight
;
454 /* apply filter to zoom vertically from tmp to dst */
455 sp
= malloc(tmp
->height
* 3);
457 for (k
= 0; k
< new_width
; ++k
) {
460 p
= dst
->data
+ k
* 3;
462 /* copy a column into a row */
465 unsigned char *p
, *d
;
468 for (i
= tmp
->height
, p
= tmp
->data
+ k
* 3; i
-- > 0; p
+= tmp
->width
* 3) {
474 for (i
= 0; i
< new_height
; ++i
) {
475 rweight
= gweight
= bweight
= 0.0;
479 for (j
= 0; j
< contrib
[i
].n
; ++j
) {
480 rweight
+= sp
[pp
[j
].pixel
] * pp
[j
].weight
;
481 gweight
+= sp
[pp
[j
].pixel
+ 1] * pp
[j
].weight
;
482 bweight
+= sp
[pp
[j
].pixel
+ 2] * pp
[j
].weight
;
484 *p
= CLAMP(rweight
, 0, 255);
485 *(p
+ 1) = CLAMP(gweight
, 0, 255);
486 *(p
+ 2) = CLAMP(bweight
, 0, 255);
492 /* free the memory allocated for vertical filter weights */
493 for (i
= 0; i
< dst
->height
; ++i
) {