Initial revision
[wmaker-crm.git] / wrlib / scale.c
blobdf181d6d809cd0925fc18761994d6b09cd05cfb0
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;