1 /* scale.c - image scaling
3 * Raster graphics library
5 * Copyright (c) 1997, 1988, 1999 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.
41 *----------------------------------------------------------------------
43 * Creates a scaled copy of an image.
46 * The new scaled image.
48 *----------------------------------------------------------------------
52 RScaleImage(RImage
*image
, unsigned new_width
, unsigned new_height
)
58 unsigned char *sr
, *sg
, *sb
, *sa
;
59 unsigned char *dr
, *dg
, *db
, *da
;
62 assert(new_width
>= 0 && new_height
>= 0);
64 if (new_width
== image
->width
&& new_height
== image
->height
)
65 return RCloneImage(image
);
67 img
= RCreateImage(new_width
, new_height
, image
->data
[3]!=NULL
);
72 /* fixed point math idea taken from Imlib by
73 * Carsten Haitzler (Rasterman) */
74 dx
= (image
->width
<<16)/new_width
;
75 dy
= (image
->height
<<16)/new_height
;
84 if (image
->data
[3]!=NULL
) {
87 for (y
=0; y
<new_height
; y
++) {
88 t
= image
->width
*(py
>>16);
90 sr
= image
->data
[0]+t
;
91 sg
= image
->data
[1]+t
;
92 sb
= image
->data
[2]+t
;
93 sa
= image
->data
[3]+t
;
98 for (x
=0; x
<new_width
; x
++) {
119 for (y
=0; y
<new_height
; y
++) {
120 t
= image
->width
*(py
>>16);
122 sr
= image
->data
[0]+t
;
123 sg
= image
->data
[1]+t
;
124 sb
= image
->data
[2]+t
;
129 for (x
=0; x
<new_width
; x
++) {
152 RScaleImage(RImage
*src
, unsigned new_width
, unsigned new_height
)
160 unsigned char *sr
, *sg
, *sb
, *sa
;
161 unsigned char *dr
, *dg
, *db
, *da
;
166 dst
= RCreateImage(new_width
, new_height
, src
->data
[3]!=NULL
);
169 ee
= (ddy
/2) - dst
->height
;
174 e
= (src
->width
/2)-xd
;
188 for (yd
= 0; yd
< new_height
; yd
++) {
191 sr
= src
->data
[0] + ys
* src
->width
;
192 sg
= src
->data
[1] + ys
* src
->width
;
193 sb
= src
->data
[2] + ys
* src
->width
;
195 for (x
= 0; x
< xd
; x
++) {
215 for (yd
= 0; yd
< new_height
; yd
++) {
218 sr
= src
->data
[0] + ys
* src
->width
;
219 sg
= src
->data
[1] + ys
* src
->width
;
220 sb
= src
->data
[2] + ys
* src
->width
;
221 sa
= src
->data
[3] + ys
* src
->width
;
223 for (x
= 0; x
< xd
; x
++) {
253 * Filtered Image Rescaling code copy/pasted from
255 * Public Domain 1991 by Dale Schumacher
260 * filter function definitions
263 #define filter_support (1.0)
269 /* f(t) = 2|t|^3 - 3|t|^2 + 1, -1 <= t <= 1 */
271 if(t
< 1.0) return((2.0 * t
- 3.0) * t
* t
+ 1.0);
275 #define box_support (0.5)
281 if((t
> -0.5) && (t
<= 0.5)) return(1.0);
285 #define triangle_support (1.0)
292 if(t
< 1.0) return(1.0 - t
);
296 #define bell_support (1.5)
299 bell_filter(t
) /* box (*) box (*) box */
303 if(t
< .5) return(.75 - (t
* t
));
306 return(.5 * (t
* t
));
311 #define B_spline_support (2.0)
314 B_spline_filter(t
) /* box (*) box (*) box (*) box */
322 return((.5 * tt
* t
) - tt
+ (2.0 / 3.0));
325 return((1.0 / 6.0) * (t
* t
* t
));
335 if(x
!= 0) return(sin(x
) / x
);
339 #define Lanczos3_support (3.0)
346 if(t
< 3.0) return(sinc(t
) * sinc(t
/3.0));
350 #define Mitchell_support (2.0)
352 #define B (1.0 / 3.0)
353 #define C (1.0 / 3.0)
364 t
= (((12.0 - 9.0 * B
- 6.0 * C
) * (t
* tt
))
365 + ((-18.0 + 12.0 * B
+ 6.0 * C
) * tt
)
369 t
= (((-1.0 * B
- 6.0 * C
) * (t
* tt
))
370 + ((6.0 * B
+ 30.0 * C
) * tt
)
371 + ((-12.0 * B
- 48.0 * C
) * t
)
372 + (8.0 * B
+ 24 * C
));
378 static double (*filterf
)() = Mitchell_filter
;
379 static double fwidth
= Mitchell_support
;
382 _wraster_change_filter(int type
)
386 filterf
= box_filter
;
387 fwidth
= box_support
;
389 case RTriangleFilter
:
390 filterf
= triangle_filter
;
391 fwidth
= triangle_support
;
394 filterf
= bell_filter
;
395 fwidth
= bell_support
;
398 filterf
= B_spline_filter
;
399 fwidth
= B_spline_support
;
401 case RLanczos3Filter
:
402 filterf
= Lanczos3_filter
;
403 fwidth
= Lanczos3_support
;
406 case RMitchellFilter
:
407 filterf
= Mitchell_filter
;
408 fwidth
= Mitchell_support
;
415 * image rescaling routine
424 int n
; /* number of contributors */
425 CONTRIB
*p
; /* pointer to list of contributions */
428 CLIST
*contrib
; /* array of contribution lists */
430 /* clamp the input to the specified range */
431 #define CLAMP(v,l,h) ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
435 RSmoothScaleImage(RImage
*src
, unsigned new_width
, unsigned new_height
)
437 RImage
*tmp
; /* intermediate image */
438 double xscale
, yscale
; /* zoom scale factors */
439 int i
, j
, k
; /* loop variables */
440 int n
; /* pixel number */
441 double center
, left
, right
; /* filter calculation variables */
442 double width
, fscale
; /* filter calculation variables */
443 double rweight
, gweight
, bweight
;
445 unsigned char *rp
, *gp
, *bp
;
446 unsigned char *rsp
, *gsp
, *bsp
;
448 dst
= RCreateImage(new_width
, new_height
, False
);
450 /* create intermediate image to hold horizontal zoom */
451 tmp
= RCreateImage(dst
->width
, src
->height
, False
);
452 xscale
= (double)new_width
/ (double)src
->width
;
453 yscale
= (double)new_height
/ (double)src
->height
;
455 /* pre-calculate filter contributions for a row */
456 contrib
= (CLIST
*)calloc(new_width
, sizeof(CLIST
));
458 width
= fwidth
/ xscale
;
459 fscale
= 1.0 / xscale
;
460 for (i
= 0; i
< new_width
; ++i
) {
462 contrib
[i
].p
= (CONTRIB
*)calloc((int)(width
* 2 + 1),
464 center
= (double) i
/ xscale
;
465 left
= ceil(center
- width
);
466 right
= floor(center
+ width
);
467 for(j
= left
; j
<= right
; ++j
) {
468 rweight
= center
- (double) j
;
469 rweight
= (*filterf
)(rweight
/ fscale
) / fscale
;
472 } else if(j
>= src
->width
) {
473 n
= (src
->width
- j
) + src
->width
- 1;
478 contrib
[i
].p
[k
].pixel
= n
;
479 contrib
[i
].p
[k
].weight
= rweight
;
483 for(i
= 0; i
< new_width
; ++i
) {
485 contrib
[i
].p
= (CONTRIB
*)calloc((int) (fwidth
* 2 + 1),
487 center
= (double) i
/ xscale
;
488 left
= ceil(center
- fwidth
);
489 right
= floor(center
+ fwidth
);
490 for(j
= left
; j
<= right
; ++j
) {
491 rweight
= center
- (double) j
;
492 rweight
= (*filterf
)(rweight
);
495 } else if(j
>= src
->width
) {
496 n
= (src
->width
- j
) + src
->width
- 1;
501 contrib
[i
].p
[k
].pixel
= n
;
502 contrib
[i
].p
[k
].weight
= rweight
;
507 /* apply filter to zoom horizontally from src to tmp */
512 for(k
= 0; k
< tmp
->height
; ++k
) {
513 rsp
= src
->data
[0] + src
->width
*k
;
514 gsp
= src
->data
[1] + src
->width
*k
;
515 bsp
= src
->data
[2] + src
->width
*k
;
517 for(i
= 0; i
< tmp
->width
; ++i
) {
518 rweight
= gweight
= bweight
= 0.0;
519 for(j
= 0; j
< contrib
[i
].n
; ++j
) {
520 rweight
+= rsp
[contrib
[i
].p
[j
].pixel
] * contrib
[i
].p
[j
].weight
;
521 gweight
+= gsp
[contrib
[i
].p
[j
].pixel
] * contrib
[i
].p
[j
].weight
;
522 bweight
+= bsp
[contrib
[i
].p
[j
].pixel
] * contrib
[i
].p
[j
].weight
;
524 *rp
++ = CLAMP(rweight
, 0, 255);
525 *gp
++ = CLAMP(gweight
, 0, 255);
526 *bp
++ = CLAMP(bweight
, 0, 255);
530 /* free the memory allocated for horizontal filter weights */
531 for(i
= 0; i
< tmp
->width
; ++i
) {
536 /* pre-calculate filter contributions for a column */
537 contrib
= (CLIST
*)calloc(dst
->height
, sizeof(CLIST
));
539 width
= fwidth
/ yscale
;
540 fscale
= 1.0 / yscale
;
541 for(i
= 0; i
< dst
->height
; ++i
) {
543 contrib
[i
].p
= (CONTRIB
*)calloc((int) (width
* 2 + 1),
545 center
= (double) i
/ yscale
;
546 left
= ceil(center
- width
);
547 right
= floor(center
+ width
);
548 for(j
= left
; j
<= right
; ++j
) {
549 rweight
= center
- (double) j
;
550 rweight
= (*filterf
)(rweight
/ fscale
) / fscale
;
553 } else if(j
>= tmp
->height
) {
554 n
= (tmp
->height
- j
) + tmp
->height
- 1;
559 contrib
[i
].p
[k
].pixel
= n
;
560 contrib
[i
].p
[k
].weight
= rweight
;
564 for(i
= 0; i
< dst
->height
; ++i
) {
566 contrib
[i
].p
= (CONTRIB
*)calloc((int) (fwidth
* 2 + 1),
568 center
= (double) i
/ yscale
;
569 left
= ceil(center
- fwidth
);
570 right
= floor(center
+ fwidth
);
571 for(j
= left
; j
<= right
; ++j
) {
572 rweight
= center
- (double) j
;
573 rweight
= (*filterf
)(rweight
);
576 } else if(j
>= tmp
->height
) {
577 n
= (tmp
->height
- j
) + tmp
->height
- 1;
582 contrib
[i
].p
[k
].pixel
= n
;
583 contrib
[i
].p
[k
].weight
= rweight
;
588 /* apply filter to zoom vertically from tmp to dst */
589 rsp
= malloc(tmp
->height
);
590 gsp
= malloc(tmp
->height
);
591 bsp
= malloc(tmp
->height
);
593 for(k
= 0; k
< new_width
; ++k
) {
594 rp
= dst
->data
[0] + k
;
595 gp
= dst
->data
[1] + k
;
596 bp
= dst
->data
[2] + k
;
598 /* copy a column into a row */
601 unsigned char *p
, *d
;
604 for(i
= tmp
->height
, p
= tmp
->data
[0] + k
; i
-- > 0;
609 for(i
= tmp
->height
, p
= tmp
->data
[1] + k
; i
-- > 0;
614 for(i
= tmp
->height
, p
= tmp
->data
[2] + k
; i
-- > 0;
619 for(i
= 0; i
< new_height
; ++i
) {
620 rweight
= gweight
= bweight
= 0.0;
621 for(j
= 0; j
< contrib
[i
].n
; ++j
) {
622 rweight
+= rsp
[contrib
[i
].p
[j
].pixel
] * contrib
[i
].p
[j
].weight
;
623 gweight
+= gsp
[contrib
[i
].p
[j
].pixel
] * contrib
[i
].p
[j
].weight
;
624 bweight
+= bsp
[contrib
[i
].p
[j
].pixel
] * contrib
[i
].p
[j
].weight
;
626 *rp
= CLAMP(rweight
, 0, 255);
627 *gp
= CLAMP(gweight
, 0, 255);
628 *bp
= CLAMP(bweight
, 0, 255);
638 /* free the memory allocated for vertical filter weights */
639 for(i
= 0; i
< dst
->height
; ++i
) {