1 /* scale.c - image scaling
3 * Raster graphics library
5 * Copyright (c) 1997 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.
36 *----------------------------------------------------------------------
38 * Creates a scaled copy of an image.
41 * The new scaled image.
43 *----------------------------------------------------------------------
46 RScaleImage(RImage
*image
, unsigned new_width
, unsigned new_height
)
52 unsigned char *sr
, *sg
, *sb
, *sa
;
53 unsigned char *dr
, *dg
, *db
, *da
;
56 assert(new_width
>= 0 && new_height
>= 0);
58 if (new_width
== image
->width
&& new_height
== image
->height
)
59 return RCloneImage(image
);
61 img
= RCreateImage(new_width
, new_height
, image
->data
[3]!=NULL
);
66 /* fixed point math idea taken from Imlib by
67 * Carsten Haitzler (Rasterman) */
68 dx
= (image
->width
<<16)/new_width
;
69 dy
= (image
->height
<<16)/new_height
;
78 if (image
->data
[3]!=NULL
) {
81 for (y
=0; y
<new_height
; y
++) {
82 t
= image
->width
*(py
>>16);
84 sr
= image
->data
[0]+t
;
85 sg
= image
->data
[1]+t
;
86 sb
= image
->data
[2]+t
;
87 sa
= image
->data
[3]+t
;
92 for (x
=0; x
<new_width
; x
++) {
113 for (y
=0; y
<new_height
; y
++) {
114 t
= image
->width
*(py
>>16);
116 sr
= image
->data
[0]+t
;
117 sg
= image
->data
[1]+t
;
118 sb
= image
->data
[2]+t
;
123 for (x
=0; x
<new_width
; x
++) {
147 RSmoothScaleImage(RImage
*image
, unsigned new_width
, unsigned new_height
)
151 register int x
, y
, t
;
153 unsigned char *sr
, *sg
, *sb
, *sa
;
154 unsigned char *dr
, *dg
, *db
, *da
;
157 assert(new_width
>= 0 && new_height
>= 0);
159 if (new_width
== image
->width
&& new_height
== image
->height
)
160 return RCloneImage(image
);
162 img
= RCreateImage(new_width
, new_height
, image
->data
[3]!=NULL
);
167 dx
= (image
->width
<<16)/new_width
;
168 dy
= (image
->height
<<16)/new_height
;
177 if (image
->data
[3]!=NULL
) {
180 for (y
=0; y
<new_height
; y
++) {
181 t
= image
->width
*(py
>>16);
183 sr
= image
->data
[0]+t
;
184 sg
= image
->data
[1]+t
;
185 sb
= image
->data
[2]+t
;
186 sa
= image
->data
[3]+t
;
191 for (x
=0; x
<new_width
; x
++) {
212 for (y
=0; y
<new_height
; y
++) {
213 t
= image
->width
*(py
>>16);
215 sr
= image
->data
[0]+t
;
216 sg
= image
->data
[1]+t
;
217 sb
= image
->data
[2]+t
;
222 for (x
=0; x
<new_width
; x
++) {