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
)
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
->format
==RRGBAFormat
);
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
;
81 if (image
->format
== RRGBAFormat
) {
84 for (y
=0; y
<new_height
; y
++) {
85 t
= image
->width
*(py
>>16);
92 for (x
=0; x
<new_width
; x
++) {
110 for (y
=0; y
<new_height
; y
++) {
111 t
= image
->width
*(py
>>16);
118 for (x
=0; x
<new_width
; x
++) {
128 s
+= (t
<<1)+t
; /* t*3 */
139 RScaleImage(RImage
*src
, unsigned new_width
, unsigned new_height
)
147 unsigned char *sr
, *sg
, *sb
, *sa
;
148 unsigned char *dr
, *dg
, *db
, *da
;
153 dst
= RCreateImage(new_width
, new_height
, src
->data
[3]!=NULL
);
156 ee
= (ddy
/2) - dst
->height
;
161 e
= (src
->width
/2)-xd
;
175 for (yd
= 0; yd
< new_height
; yd
++) {
178 sr
= src
->data
[0] + ys
* src
->width
;
179 sg
= src
->data
[1] + ys
* src
->width
;
180 sb
= src
->data
[2] + ys
* src
->width
;
182 for (x
= 0; x
< xd
; x
++) {
202 for (yd
= 0; yd
< new_height
; yd
++) {
205 sr
= src
->data
[0] + ys
* src
->width
;
206 sg
= src
->data
[1] + ys
* src
->width
;
207 sb
= src
->data
[2] + ys
* src
->width
;
208 sa
= src
->data
[3] + ys
* src
->width
;
210 for (x
= 0; x
< xd
; x
++) {
240 * Filtered Image Rescaling code copy/pasted from
242 * Public Domain 1991 by Dale Schumacher
247 * filter function definitions
250 #define filter_support (1.0)
256 /* f(t) = 2|t|^3 - 3|t|^2 + 1, -1 <= t <= 1 */
258 if(t
< 1.0) return((2.0 * t
- 3.0) * t
* t
+ 1.0);
262 #define box_support (0.5)
268 if((t
> -0.5) && (t
<= 0.5)) return(1.0);
272 #define triangle_support (1.0)
279 if(t
< 1.0) return(1.0 - t
);
283 #define bell_support (1.5)
286 bell_filter(t
) /* box (*) box (*) box */
290 if(t
< .5) return(.75 - (t
* t
));
293 return(.5 * (t
* t
));
298 #define B_spline_support (2.0)
301 B_spline_filter(t
) /* box (*) box (*) box (*) box */
309 return((.5 * tt
* t
) - tt
+ (2.0 / 3.0));
312 return((1.0 / 6.0) * (t
* t
* t
));
322 if(x
!= 0) return(sin(x
) / x
);
326 #define Lanczos3_support (3.0)
333 if(t
< 3.0) return(sinc(t
) * sinc(t
/3.0));
337 #define Mitchell_support (2.0)
339 #define B (1.0 / 3.0)
340 #define C (1.0 / 3.0)
351 t
= (((12.0 - 9.0 * B
- 6.0 * C
) * (t
* tt
))
352 + ((-18.0 + 12.0 * B
+ 6.0 * C
) * tt
)
356 t
= (((-1.0 * B
- 6.0 * C
) * (t
* tt
))
357 + ((6.0 * B
+ 30.0 * C
) * tt
)
358 + ((-12.0 * B
- 48.0 * C
) * t
)
359 + (8.0 * B
+ 24 * C
));
365 static double (*filterf
)() = Mitchell_filter
;
366 static double fwidth
= Mitchell_support
;
369 _wraster_change_filter(int type
)
373 filterf
= box_filter
;
374 fwidth
= box_support
;
376 case RTriangleFilter
:
377 filterf
= triangle_filter
;
378 fwidth
= triangle_support
;
381 filterf
= bell_filter
;
382 fwidth
= bell_support
;
385 filterf
= B_spline_filter
;
386 fwidth
= B_spline_support
;
388 case RLanczos3Filter
:
389 filterf
= Lanczos3_filter
;
390 fwidth
= Lanczos3_support
;
393 case RMitchellFilter
:
394 filterf
= Mitchell_filter
;
395 fwidth
= Mitchell_support
;
402 * image rescaling routine
411 int n
; /* number of contributors */
412 CONTRIB
*p
; /* pointer to list of contributions */
415 CLIST
*contrib
; /* array of contribution lists */
417 /* clamp the input to the specified range */
418 #define CLAMP(v,l,h) ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
425 RSmoothScaleImage(RImage
*src
, unsigned new_width
, unsigned new_height
)
427 RImage
*tmp
; /* intermediate image */
428 double xscale
, yscale
; /* zoom scale factors */
429 int i
, j
, k
; /* loop variables */
430 int n
; /* pixel number */
431 double center
, left
, right
; /* filter calculation variables */
432 double width
, fscale
; /* filter calculation variables */
433 double rweight
, gweight
, bweight
;
437 int sch
= src
->format
== RRGBAFormat
? 4 : 3;
440 dst
= RCreateImage(new_width
, new_height
, False
);
442 /* create intermediate image to hold horizontal zoom */
443 tmp
= RCreateImage(dst
->width
, src
->height
, False
);
444 xscale
= (double)new_width
/ (double)src
->width
;
445 yscale
= (double)new_height
/ (double)src
->height
;
447 /* pre-calculate filter contributions for a row */
448 contrib
= (CLIST
*)calloc(new_width
, sizeof(CLIST
));
450 width
= fwidth
/ xscale
;
451 fscale
= 1.0 / xscale
;
452 for (i
= 0; i
< new_width
; ++i
) {
454 contrib
[i
].p
= (CONTRIB
*)calloc((int)(width
* 2 + 1),
456 center
= (double) i
/ xscale
;
457 left
= ceil(center
- width
);
458 right
= floor(center
+ width
);
459 for(j
= left
; j
<= right
; ++j
) {
460 rweight
= center
- (double) j
;
461 rweight
= (*filterf
)(rweight
/ fscale
) / fscale
;
464 } else if(j
>= src
->width
) {
465 n
= (src
->width
- j
) + src
->width
- 1;
470 contrib
[i
].p
[k
].pixel
= n
*sch
;
471 contrib
[i
].p
[k
].weight
= rweight
;
476 for(i
= 0; i
< new_width
; ++i
) {
478 contrib
[i
].p
= (CONTRIB
*)calloc((int) (fwidth
* 2 + 1),
480 center
= (double) i
/ xscale
;
481 left
= ceil(center
- fwidth
);
482 right
= floor(center
+ fwidth
);
483 for(j
= left
; j
<= right
; ++j
) {
484 rweight
= center
- (double) j
;
485 rweight
= (*filterf
)(rweight
);
488 } else if(j
>= src
->width
) {
489 n
= (src
->width
- j
) + src
->width
- 1;
494 contrib
[i
].p
[k
].pixel
= n
*sch
;
495 contrib
[i
].p
[k
].weight
= rweight
;
500 /* apply filter to zoom horizontally from src to tmp */
504 for(k
= 0; k
< tmp
->height
; ++k
) {
505 sp
= src
->data
+ src
->width
*k
*sch
;
507 for(i
= 0; i
< tmp
->width
; ++i
) {
508 rweight
= gweight
= bweight
= 0.0;
509 for(j
= 0; j
< contrib
[i
].n
; ++j
) {
510 rweight
+= sp
[contrib
[i
].p
[j
].pixel
] * contrib
[i
].p
[j
].weight
;
511 gweight
+= sp
[contrib
[i
].p
[j
].pixel
+1] * contrib
[i
].p
[j
].weight
;
512 bweight
+= sp
[contrib
[i
].p
[j
].pixel
+2] * contrib
[i
].p
[j
].weight
;
514 *p
++ = CLAMP(rweight
, 0, 255);
515 *p
++ = CLAMP(gweight
, 0, 255);
516 *p
++ = CLAMP(bweight
, 0, 255);
520 /* free the memory allocated for horizontal filter weights */
521 for(i
= 0; i
< tmp
->width
; ++i
) {
526 /* pre-calculate filter contributions for a column */
527 contrib
= (CLIST
*)calloc(dst
->height
, sizeof(CLIST
));
529 width
= fwidth
/ yscale
;
530 fscale
= 1.0 / yscale
;
531 for(i
= 0; i
< dst
->height
; ++i
) {
533 contrib
[i
].p
= (CONTRIB
*)calloc((int) (width
* 2 + 1),
535 center
= (double) i
/ yscale
;
536 left
= ceil(center
- width
);
537 right
= floor(center
+ width
);
538 for(j
= left
; j
<= right
; ++j
) {
539 rweight
= center
- (double) j
;
540 rweight
= (*filterf
)(rweight
/ fscale
) / fscale
;
543 } else if(j
>= tmp
->height
) {
544 n
= (tmp
->height
- j
) + tmp
->height
- 1;
549 contrib
[i
].p
[k
].pixel
= n
*3;
550 contrib
[i
].p
[k
].weight
= rweight
;
554 for(i
= 0; i
< dst
->height
; ++i
) {
556 contrib
[i
].p
= (CONTRIB
*)calloc((int) (fwidth
* 2 + 1),
558 center
= (double) i
/ yscale
;
559 left
= ceil(center
- fwidth
);
560 right
= floor(center
+ fwidth
);
561 for(j
= left
; j
<= right
; ++j
) {
562 rweight
= center
- (double) j
;
563 rweight
= (*filterf
)(rweight
);
566 } else if(j
>= tmp
->height
) {
567 n
= (tmp
->height
- j
) + tmp
->height
- 1;
572 contrib
[i
].p
[k
].pixel
= n
*3;
573 contrib
[i
].p
[k
].weight
= rweight
;
578 /* apply filter to zoom vertically from tmp to dst */
579 sp
= malloc(tmp
->height
*3);
581 for(k
= 0; k
< new_width
; ++k
) {
584 /* copy a column into a row */
587 unsigned char *p
, *d
;
590 for(i
= tmp
->height
, p
= tmp
->data
+ k
*3; i
-- > 0;
597 for(i
= 0; i
< new_height
; ++i
) {
598 rweight
= gweight
= bweight
= 0.0;
599 for(j
= 0; j
< contrib
[i
].n
; ++j
) {
600 rweight
+= sp
[contrib
[i
].p
[j
].pixel
] * contrib
[i
].p
[j
].weight
;
601 gweight
+= sp
[contrib
[i
].p
[j
].pixel
+1] * contrib
[i
].p
[j
].weight
;
602 bweight
+= sp
[contrib
[i
].p
[j
].pixel
+2] * contrib
[i
].p
[j
].weight
;
604 *p
= CLAMP(rweight
, 0, 255);
605 *(p
+1) = CLAMP(gweight
, 0, 255);
606 *(p
+2) = CLAMP(bweight
, 0, 255);
612 /* free the memory allocated for vertical filter weights */
613 for(i
= 0; i
< dst
->height
; ++i
) {