]>
Commit | Line | Data |
---|---|---|
720086df | 1 | /* |
71fe22b7 | 2 | * "$Id: image-zoom.c,v 1.6 2000/01/04 13:45:45 mike Exp $" |
720086df | 3 | * |
ed19bd98 | 4 | * Image zoom routines for the Common UNIX Printing System (CUPS). |
720086df | 5 | * |
71fe22b7 | 6 | * Copyright 1993-2000 by Easy Software Products. |
720086df | 7 | * |
ed19bd98 | 8 | * These coded instructions, statements, and computer programs are the |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
720086df | 23 | * |
24 | * Contents: | |
25 | * | |
26 | * ImageZoomAlloc() - Allocate a pixel zoom record... | |
27 | * ImageZoomFill() - Fill a zoom record with image data utilizing bilinear | |
28 | * interpolation. | |
29 | * ImageZoomQFill() - Fill a zoom record quickly using nearest-neighbor | |
30 | * sampling. | |
31 | * ImageZoomFree() - Free a zoom record... | |
720086df | 32 | */ |
33 | ||
34 | /* | |
35 | * Include necessary headers... | |
36 | */ | |
37 | ||
38 | #include "image.h" | |
39 | ||
40 | ||
41 | /* | |
42 | * 'ZoomAlloc()' - Allocate a pixel zoom record... | |
43 | */ | |
44 | ||
45 | izoom_t * | |
f9ccdd05 | 46 | ImageZoomAlloc(image_t *img, /* I - Image to zoom */ |
720086df | 47 | int x0, /* I - Upper-lefthand corner */ |
48 | int y0, /* I - ... */ | |
49 | int x1, /* I - Lower-righthand corner */ | |
50 | int y1, /* I - ... */ | |
51 | int xsize, /* I - Final width of image */ | |
52 | int ysize, /* I - Final height of image */ | |
53 | int rotated) /* I - Non-zero if image is rotated 90 degs */ | |
54 | { | |
55 | izoom_t *z; /* New zoom record */ | |
720086df | 56 | |
57 | ||
58 | if ((z = (izoom_t *)calloc(1, sizeof(izoom_t))) == NULL) | |
59 | return (NULL); | |
60 | ||
f9ccdd05 | 61 | z->img = img; |
62 | z->row = 0; | |
63 | z->depth = ImageGetDepth(img); | |
64 | z->rotated = rotated; | |
720086df | 65 | |
66 | if (rotated) | |
67 | { | |
f9ccdd05 | 68 | z->xorig = x1; |
69 | z->yorig = y0; | |
70 | z->width = y1 - y0 + 1; | |
71 | z->height = x1 - x0 + 1; | |
8db57375 | 72 | z->xsize = xsize; |
73 | z->ysize = ysize; | |
f9ccdd05 | 74 | z->xmod = z->width % z->xsize; |
75 | z->xstep = z->width / z->xsize; | |
76 | z->xincr = 1; | |
77 | z->ymod = z->height % z->ysize; | |
78 | z->ystep = z->height / z->ysize; | |
79 | z->yincr = 1; | |
80 | z->instep = z->xstep * z->depth; | |
81 | z->inincr = z->xincr * z->depth; | |
82 | ||
83 | if (z->width < img->ysize) | |
84 | z->xmax = z->width; | |
720086df | 85 | else |
f9ccdd05 | 86 | z->xmax = z->width - 1; |
720086df | 87 | |
f9ccdd05 | 88 | if (z->height < img->xsize) |
89 | z->ymax = z->height; | |
720086df | 90 | else |
f9ccdd05 | 91 | z->ymax = z->height - 1; |
720086df | 92 | } |
93 | else | |
94 | { | |
f9ccdd05 | 95 | z->xorig = x0; |
96 | z->yorig = y0; | |
97 | z->width = x1 - x0 + 1; | |
98 | z->height = y1 - y0 + 1; | |
99 | z->xsize = xsize; | |
100 | z->ysize = ysize; | |
101 | z->xmod = z->width % z->xsize; | |
102 | z->xstep = z->width / z->xsize; | |
103 | z->xincr = 1; | |
104 | z->ymod = z->height % z->ysize; | |
105 | z->ystep = z->height / z->ysize; | |
106 | z->yincr = 1; | |
107 | z->instep = z->xstep * z->depth; | |
108 | z->inincr = z->xincr * z->depth; | |
109 | ||
110 | if (z->width < img->xsize) | |
111 | z->xmax = z->width; | |
112 | else | |
113 | z->xmax = z->width - 1; | |
720086df | 114 | |
f9ccdd05 | 115 | if (z->height < img->ysize) |
116 | z->ymax = z->height; | |
117 | else | |
118 | z->ymax = z->height - 1; | |
ed19bd98 | 119 | } |
720086df | 120 | |
f9ccdd05 | 121 | if ((z->rows[0] = (ib_t *)malloc(z->xsize * z->depth)) == NULL) |
122 | { | |
123 | free(z); | |
124 | return (NULL); | |
ed19bd98 | 125 | } |
720086df | 126 | |
f9ccdd05 | 127 | if ((z->rows[1] = (ib_t *)malloc(z->xsize * z->depth)) == NULL) |
128 | { | |
129 | free(z->rows[0]); | |
130 | free(z); | |
131 | return (NULL); | |
ed19bd98 | 132 | } |
720086df | 133 | |
f9ccdd05 | 134 | if ((z->in = (ib_t *)malloc(z->width * z->depth)) == NULL) |
135 | { | |
136 | free(z->rows[0]); | |
137 | free(z->rows[1]); | |
138 | free(z); | |
139 | return (NULL); | |
ed19bd98 | 140 | } |
720086df | 141 | |
142 | return (z); | |
143 | } | |
144 | ||
145 | ||
146 | /* | |
147 | * 'ImageZoomFill()' - Fill a zoom record with image data utilizing bilinear | |
148 | * interpolation. | |
149 | */ | |
150 | ||
151 | void | |
152 | ImageZoomFill(izoom_t *z, /* I - Zoom record to fill */ | |
6de9968b | 153 | int iy) /* I - Zoom image row */ |
720086df | 154 | { |
f9ccdd05 | 155 | ib_t *r, /* Row pointer */ |
156 | *inptr; /* Pixel pointer */ | |
157 | int xerr0, /* X error counter */ | |
158 | xerr1; /* ... */ | |
159 | int ix, | |
160 | x, | |
161 | count, | |
162 | z_depth, | |
163 | z_xstep, | |
164 | z_xincr, | |
165 | z_instep, | |
166 | z_inincr, | |
167 | z_xmax, | |
168 | z_xmod, | |
169 | z_xsize; | |
720086df | 170 | |
171 | ||
172 | if (iy > z->ymax) | |
173 | iy = z->ymax; | |
174 | ||
175 | z->row ^= 1; | |
176 | ||
f9ccdd05 | 177 | z_depth = z->depth; |
720086df | 178 | z_xsize = z->xsize; |
f9ccdd05 | 179 | z_xmax = z->xmax; |
180 | z_xmod = z->xmod; | |
181 | z_xstep = z->xstep; | |
182 | z_xincr = z->xincr; | |
183 | z_instep = z->instep; | |
184 | z_inincr = z->inincr; | |
185 | ||
186 | if (z->rotated) | |
187 | ImageGetCol(z->img, z->xorig - iy, z->yorig, z->width, z->in); | |
188 | else | |
189 | ImageGetRow(z->img, z->xorig, z->yorig + iy, z->width, z->in); | |
190 | ||
f9ccdd05 | 191 | if (z_inincr < 0) |
192 | inptr = z->in + (z->width - 1) * z_depth; | |
193 | else | |
194 | inptr = z->in; | |
720086df | 195 | |
f9ccdd05 | 196 | for (x = z_xsize, xerr0 = z_xsize, xerr1 = 0, ix = 0, r = z->rows[z->row]; |
197 | x > 0; | |
198 | x --) | |
720086df | 199 | { |
f9ccdd05 | 200 | if (ix < z_xmax) |
201 | { | |
202 | for (count = 0; count < z_depth; count ++) | |
203 | *r++ = (inptr[count] * xerr0 + inptr[z_depth + count] * xerr1) / z_xsize; | |
204 | } | |
205 | else | |
206 | { | |
207 | for (count = 0; count < z_depth; count ++) | |
208 | *r++ = inptr[count]; | |
ed19bd98 | 209 | } |
720086df | 210 | |
f9ccdd05 | 211 | ix += z_xstep; |
212 | inptr += z_instep; | |
213 | xerr0 -= z_xmod; | |
214 | xerr1 += z_xmod; | |
215 | ||
216 | if (xerr0 <= 0) | |
720086df | 217 | { |
f9ccdd05 | 218 | xerr0 += z_xsize; |
219 | xerr1 -= z_xsize; | |
220 | ix += z_xincr; | |
221 | inptr += z_inincr; | |
ed19bd98 | 222 | } |
223 | } | |
720086df | 224 | } |
225 | ||
226 | ||
227 | /* | |
228 | * 'ImageZoomQFill()' - Fill a zoom record quickly using nearest-neighbor sampling. | |
229 | */ | |
230 | ||
231 | void | |
232 | ImageZoomQFill(izoom_t *z, /* I - Zoom record to fill */ | |
6de9968b | 233 | int iy) /* I - Zoom image row */ |
720086df | 234 | { |
f9ccdd05 | 235 | ib_t *r, /* Row pointer */ |
236 | *inptr; /* Pixel pointer */ | |
237 | int xerr0; /* X error counter */ | |
238 | int ix, | |
239 | x, | |
240 | count, | |
241 | z_depth, | |
242 | z_xstep, | |
243 | z_xincr, | |
244 | z_instep, | |
245 | z_inincr, | |
f9ccdd05 | 246 | z_xmod, |
247 | z_xsize; | |
720086df | 248 | |
249 | ||
250 | if (iy > z->ymax) | |
251 | iy = z->ymax; | |
252 | ||
f9ccdd05 | 253 | z->row ^= 1; |
720086df | 254 | |
f9ccdd05 | 255 | z_depth = z->depth; |
720086df | 256 | z_xsize = z->xsize; |
f9ccdd05 | 257 | z_xmod = z->xmod; |
258 | z_xstep = z->xstep; | |
259 | z_xincr = z->xincr; | |
260 | z_instep = z->instep; | |
261 | z_inincr = z->inincr; | |
262 | ||
263 | if (z->rotated) | |
264 | ImageGetCol(z->img, z->xorig - iy, z->yorig, z->width, z->in); | |
265 | else | |
266 | ImageGetRow(z->img, z->xorig, z->yorig + iy, z->width, z->in); | |
267 | ||
f9ccdd05 | 268 | if (z_inincr < 0) |
269 | inptr = z->in + (z->width - 1) * z_depth; | |
270 | else | |
271 | inptr = z->in; | |
272 | ||
273 | for (x = z_xsize, xerr0 = z_xsize, ix = 0, r = z->rows[z->row]; | |
274 | x > 0; | |
275 | x --) | |
720086df | 276 | { |
f9ccdd05 | 277 | for (count = 0; count < z_depth; count ++) |
278 | *r++ = inptr[count]; | |
279 | ||
280 | ix += z_xstep; | |
281 | inptr += z_instep; | |
282 | xerr0 -= z_xmod; | |
720086df | 283 | |
f9ccdd05 | 284 | if (xerr0 <= 0) |
720086df | 285 | { |
f9ccdd05 | 286 | xerr0 += z_xsize; |
287 | ix += z_xincr; | |
288 | inptr += z_inincr; | |
ed19bd98 | 289 | } |
290 | } | |
720086df | 291 | } |
292 | ||
293 | ||
294 | /* | |
295 | * 'ImageZoomFree()' - Free a zoom record... | |
296 | */ | |
297 | ||
298 | void | |
299 | ImageZoomFree(izoom_t *z) /* I - Zoom record to free */ | |
300 | { | |
301 | free(z->rows[0]); | |
302 | free(z->rows[1]); | |
f9ccdd05 | 303 | free(z->in); |
720086df | 304 | free(z); |
305 | } | |
306 | ||
307 | ||
308 | /* | |
71fe22b7 | 309 | * End of "$Id: image-zoom.c,v 1.6 2000/01/04 13:45:45 mike Exp $". |
720086df | 310 | */ |