Pack motion notify in dematerialize icon.
[wmaker-crm.git] / wrlib / scale.c
blob4e05291af576b3c9151a770e741864854edae8c9
1 /* scale.c - image scaling
2 *
3 * Raster graphics library
4 *
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.
22 #include <config.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <X11/Xlib.h>
30 #include <assert.h>
32 #include "wraster.h"
36 *----------------------------------------------------------------------
37 * RScaleImage--
38 * Creates a scaled copy of an image.
40 * Returns:
41 * The new scaled image.
43 *----------------------------------------------------------------------
45 RImage*
46 RScaleImage(RImage *image, unsigned new_width, unsigned new_height)
48 int ox;
49 int px, py;
50 register int x, y, t;
51 int dx, dy;
52 unsigned char *sr, *sg, *sb, *sa;
53 unsigned char *dr, *dg, *db, *da;
54 RImage *img;
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);
63 if (!img)
64 return 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;
71 py = 0;
73 dr = img->data[0];
74 dg = img->data[1];
75 db = img->data[2];
76 da = img->data[3];
78 if (image->data[3]!=NULL) {
79 int ot;
80 ot = -1;
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;
89 ot = t;
90 ox = 0;
91 px = 0;
92 for (x=0; x<new_width; x++) {
93 px += dx;
95 *(dr++) = *sr;
96 *(dg++) = *sg;
97 *(db++) = *sb;
98 *(da++) = *sa;
100 t = (px - ox)>>16;
101 ox += t<<16;
103 sr += t;
104 sg += t;
105 sb += t;
106 sa += t;
108 py += dy;
110 } else {
111 int ot;
112 ot = -1;
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;
120 ot = t;
121 ox = 0;
122 px = 0;
123 for (x=0; x<new_width; x++) {
124 px += dx;
126 *(dr++) = *sr;
127 *(dg++) = *sg;
128 *(db++) = *sb;
130 t = (px-ox)>>16;
131 ox += t<<16;
133 sr += t;
134 sg += t;
135 sb += t;
137 py += dy;
141 return img;
146 RImage*
147 RSmoothScaleImage(RImage *image, unsigned new_width, unsigned new_height)
149 int ox;
150 int px, py;
151 register int x, y, t;
152 int dx, dy;
153 unsigned char *sr, *sg, *sb, *sa;
154 unsigned char *dr, *dg, *db, *da;
155 RImage *img;
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);
164 if (!img)
165 return NULL;
167 dx = (image->width<<16)/new_width;
168 dy = (image->height<<16)/new_height;
170 py = 0;
172 dr = img->data[0];
173 dg = img->data[1];
174 db = img->data[2];
175 da = img->data[3];
177 if (image->data[3]!=NULL) {
178 int ot;
179 ot = -1;
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;
188 ot = t;
189 ox = 0;
190 px = 0;
191 for (x=0; x<new_width; x++) {
192 px += dx;
194 *(dr++) = *sr;
195 *(dg++) = *sg;
196 *(db++) = *sb;
197 *(da++) = *sa;
199 t = (px - ox)>>16;
200 ox += t<<16;
202 sr += t;
203 sg += t;
204 sb += t;
205 sa += t;
207 py += dy;
209 } else {
210 int ot;
211 ot = -1;
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;
219 ot = t;
220 ox = 0;
221 px = 0;
222 for (x=0; x<new_width; x++) {
223 px += dx;
225 *(dr++) = *sr;
226 *(dg++) = *sg;
227 *(db++) = *sb;
229 t = (px-ox)>>16;
230 ox += t<<16;
232 sr += t;
233 sg += t;
234 sb += t;
236 py += dy;
240 return img;