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 *----------------------------------------------------------------------
49 RImage
*RScaleImage(RImage
* image
, unsigned new_width
, unsigned new_height
)
59 assert(new_width
>= 0 && new_height
>= 0);
61 if (new_width
== image
->width
&& new_height
== image
->height
)
62 return RCloneImage(image
);
64 img
= RCreateImage(new_width
, new_height
, image
->format
== RRGBAFormat
);
69 /* fixed point math idea taken from Imlib by
70 * Carsten Haitzler (Rasterman) */
71 dx
= (image
->width
<< 16) / new_width
;
72 dy
= (image
->height
<< 16) / new_height
;
78 if (image
->format
== RRGBAFormat
) {
79 for (y
= 0; y
< new_height
; y
++) {
80 t
= image
->width
* (py
>> 16);
82 s
= image
->data
+ (t
<< 2); /* image->data+t*4 */
86 for (x
= 0; x
< new_width
; x
++) {
97 s
+= t
<< 2; /* t*4 */
102 for (y
= 0; y
< new_height
; y
++) {
103 t
= image
->width
* (py
>> 16);
105 s
= image
->data
+ (t
<< 1) + t
; /* image->data+t*3 */
109 for (x
= 0; x
< new_width
; x
++) {
119 s
+= (t
<< 1) + t
; /* t*3 */
129 RImage
*RScaleImage(RImage
* src
, unsigned new_width
, unsigned new_height
)
137 unsigned char *sr
, *sg
, *sb
, *sa
;
138 unsigned char *dr
, *dg
, *db
, *da
;
141 dst
= RCreateImage(new_width
, new_height
, src
->data
[3] != NULL
);
143 ddy
= src
->height
/ 2;
144 ee
= (ddy
/ 2) - dst
->height
;
149 e
= (src
->width
/ 2) - xd
;
163 for (yd
= 0; yd
< new_height
; yd
++) {
166 sr
= src
->data
[0] + ys
* src
->width
;
167 sg
= src
->data
[1] + ys
* src
->width
;
168 sb
= src
->data
[2] + ys
* src
->width
;
170 for (x
= 0; x
< xd
; x
++) {
190 for (yd
= 0; yd
< new_height
; yd
++) {
193 sr
= src
->data
[0] + ys
* src
->width
;
194 sg
= src
->data
[1] + ys
* src
->width
;
195 sb
= src
->data
[2] + ys
* src
->width
;
196 sa
= src
->data
[3] + ys
* src
->width
;
198 for (x
= 0; x
< xd
; x
++) {
226 * Filtered Image Rescaling code copy/pasted from
228 * Public Domain 1991 by Dale Schumacher
232 * filter function definitions
235 #define filter_support (1.0)
237 static double filter(double t
)
239 /* f(t) = 2|t|^3 - 3|t|^2 + 1, -1 <= t <= 1 */
243 return ((2.0 * t
- 3.0) * t
* t
+ 1.0);
247 #define box_support (0.5)
249 static double box_filter(double t
)
251 if ((t
> -0.5) && (t
<= 0.5))
256 #define triangle_support (1.0)
258 static double triangle_filter(double t
)
267 #define bell_support (1.5)
269 static double bell_filter(double t
) /* box (*) box (*) box */
274 return (.75 - (t
* t
));
277 return (.5 * (t
* t
));
282 #define B_spline_support (2.0)
284 static double B_spline_filter(double t
) /* box (*) box (*) box (*) box */
292 return ((.5 * tt
* t
) - tt
+ (2.0 / 3.0));
295 return ((1.0 / 6.0) * (t
* t
* t
));
300 static double sinc(double x
)
308 #define Lanczos3_support (3.0)
310 static double Lanczos3_filter(double t
)
315 return (sinc(t
) * sinc(t
/ 3.0));
319 #define Mitchell_support (2.0)
321 #define B (1.0 / 3.0)
322 #define C (1.0 / 3.0)
324 static double Mitchell_filter(double t
)
332 t
= (((12.0 - 9.0 * B
- 6.0 * C
) * (t
* tt
))
333 + ((-18.0 + 12.0 * B
+ 6.0 * C
) * tt
)
336 } else if (t
< 2.0) {
337 t
= (((-1.0 * B
- 6.0 * C
) * (t
* tt
))
338 + ((6.0 * B
+ 30.0 * C
) * tt
)
339 + ((-12.0 * B
- 48.0 * C
) * t
)
340 + (8.0 * B
+ 24 * C
));
346 static double (*filterf
)(double) = Mitchell_filter
;
347 static double fwidth
= Mitchell_support
;
349 void _wraster_change_filter(int type
)
353 filterf
= box_filter
;
354 fwidth
= box_support
;
356 case RTriangleFilter
:
357 filterf
= triangle_filter
;
358 fwidth
= triangle_support
;
361 filterf
= bell_filter
;
362 fwidth
= bell_support
;
365 filterf
= B_spline_filter
;
366 fwidth
= B_spline_support
;
368 case RLanczos3Filter
:
369 filterf
= Lanczos3_filter
;
370 fwidth
= Lanczos3_support
;
373 case RMitchellFilter
:
374 filterf
= Mitchell_filter
;
375 fwidth
= Mitchell_support
;
381 * image rescaling routine
390 int n
; /* number of contributors */
391 CONTRIB
*p
; /* pointer to list of contributions */
394 CLIST
*contrib
; /* array of contribution lists */
396 /* clamp the input to the specified range */
397 #define CLAMP(v,l,h) ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
399 /* return of calloc is not checked if NULL in the function below! */
400 RImage
*RSmoothScaleImage(RImage
* src
, unsigned new_width
, unsigned new_height
)
402 RImage
*tmp
; /* intermediate image */
403 double xscale
, yscale
; /* zoom scale factors */
404 int i
, j
, k
; /* loop variables */
405 int n
; /* pixel number */
406 double center
, left
, right
; /* filter calculation variables */
407 double width
, fscale
; /* filter calculation variables */
408 double rweight
, gweight
, bweight
;
412 int sch
= src
->format
== RRGBAFormat
? 4 : 3;
414 dst
= RCreateImage(new_width
, new_height
, False
);
416 /* create intermediate image to hold horizontal zoom */
417 tmp
= RCreateImage(dst
->width
, src
->height
, False
);
418 xscale
= (double)new_width
/ (double)src
->width
;
419 yscale
= (double)new_height
/ (double)src
->height
;
421 /* pre-calculate filter contributions for a row */
422 contrib
= (CLIST
*) calloc(new_width
, sizeof(CLIST
));
424 width
= fwidth
/ xscale
;
425 fscale
= 1.0 / xscale
;
426 for (i
= 0; i
< new_width
; ++i
) {
428 contrib
[i
].p
= (CONTRIB
*) calloc((int)(width
* 2 + 1), sizeof(CONTRIB
));
429 center
= (double)i
/ xscale
;
430 left
= ceil(center
- width
);
431 right
= floor(center
+ width
);
432 for (j
= left
; j
<= right
; ++j
) {
433 rweight
= center
- (double)j
;
434 rweight
= (*filterf
) (rweight
/ fscale
) / fscale
;
437 } else if (j
>= src
->width
) {
438 n
= (src
->width
- j
) + src
->width
- 1;
443 contrib
[i
].p
[k
].pixel
= n
* sch
;
444 contrib
[i
].p
[k
].weight
= rweight
;
449 for (i
= 0; i
< new_width
; ++i
) {
451 contrib
[i
].p
= (CONTRIB
*) calloc((int)(fwidth
* 2 + 1), sizeof(CONTRIB
));
452 center
= (double)i
/ xscale
;
453 left
= ceil(center
- fwidth
);
454 right
= floor(center
+ fwidth
);
455 for (j
= left
; j
<= right
; ++j
) {
456 rweight
= center
- (double)j
;
457 rweight
= (*filterf
) (rweight
);
460 } else if (j
>= src
->width
) {
461 n
= (src
->width
- j
) + src
->width
- 1;
466 contrib
[i
].p
[k
].pixel
= n
* sch
;
467 contrib
[i
].p
[k
].weight
= rweight
;
472 /* apply filter to zoom horizontally from src to tmp */
475 for (k
= 0; k
< tmp
->height
; ++k
) {
478 sp
= src
->data
+ src
->width
* k
* sch
;
480 for (i
= 0; i
< tmp
->width
; ++i
) {
481 rweight
= gweight
= bweight
= 0.0;
485 for (j
= 0; j
< contrib
[i
].n
; ++j
) {
486 rweight
+= sp
[pp
[j
].pixel
] * pp
[j
].weight
;
487 gweight
+= sp
[pp
[j
].pixel
+ 1] * pp
[j
].weight
;
488 bweight
+= sp
[pp
[j
].pixel
+ 2] * pp
[j
].weight
;
490 *p
++ = CLAMP(rweight
, 0, 255);
491 *p
++ = CLAMP(gweight
, 0, 255);
492 *p
++ = CLAMP(bweight
, 0, 255);
496 /* free the memory allocated for horizontal filter weights */
497 for (i
= 0; i
< tmp
->width
; ++i
) {
502 /* pre-calculate filter contributions for a column */
503 contrib
= (CLIST
*) calloc(dst
->height
, sizeof(CLIST
));
505 width
= fwidth
/ yscale
;
506 fscale
= 1.0 / yscale
;
507 for (i
= 0; i
< dst
->height
; ++i
) {
509 contrib
[i
].p
= (CONTRIB
*) calloc((int)(width
* 2 + 1), sizeof(CONTRIB
));
510 center
= (double)i
/ yscale
;
511 left
= ceil(center
- width
);
512 right
= floor(center
+ width
);
513 for (j
= left
; j
<= right
; ++j
) {
514 rweight
= center
- (double)j
;
515 rweight
= (*filterf
) (rweight
/ fscale
) / fscale
;
518 } else if (j
>= tmp
->height
) {
519 n
= (tmp
->height
- j
) + tmp
->height
- 1;
524 contrib
[i
].p
[k
].pixel
= n
* 3;
525 contrib
[i
].p
[k
].weight
= rweight
;
529 for (i
= 0; i
< dst
->height
; ++i
) {
531 contrib
[i
].p
= (CONTRIB
*) calloc((int)(fwidth
* 2 + 1), sizeof(CONTRIB
));
532 center
= (double)i
/ yscale
;
533 left
= ceil(center
- fwidth
);
534 right
= floor(center
+ fwidth
);
535 for (j
= left
; j
<= right
; ++j
) {
536 rweight
= center
- (double)j
;
537 rweight
= (*filterf
) (rweight
);
540 } else if (j
>= tmp
->height
) {
541 n
= (tmp
->height
- j
) + tmp
->height
- 1;
546 contrib
[i
].p
[k
].pixel
= n
* 3;
547 contrib
[i
].p
[k
].weight
= rweight
;
552 /* apply filter to zoom vertically from tmp to dst */
553 sp
= malloc(tmp
->height
* 3);
555 for (k
= 0; k
< new_width
; ++k
) {
558 p
= dst
->data
+ k
* 3;
560 /* copy a column into a row */
563 unsigned char *p
, *d
;
566 for (i
= tmp
->height
, p
= tmp
->data
+ k
* 3; i
-- > 0; p
+= tmp
->width
* 3) {
572 for (i
= 0; i
< new_height
; ++i
) {
573 rweight
= gweight
= bweight
= 0.0;
577 for (j
= 0; j
< contrib
[i
].n
; ++j
) {
578 rweight
+= sp
[pp
[j
].pixel
] * pp
[j
].weight
;
579 gweight
+= sp
[pp
[j
].pixel
+ 1] * pp
[j
].weight
;
580 bweight
+= sp
[pp
[j
].pixel
+ 2] * pp
[j
].weight
;
582 *p
= CLAMP(rweight
, 0, 255);
583 *(p
+ 1) = CLAMP(gweight
, 0, 255);
584 *(p
+ 2) = CLAMP(bweight
, 0, 255);
590 /* free the memory allocated for vertical filter weights */
591 for (i
= 0; i
< dst
->height
; ++i
) {