]> git.imager.perl.org - imager.git/blame - scale.im
add .perl files to the t94kwalitee struct tests and fix the non strict
[imager.git] / scale.im
CommitLineData
658f724e
TC
1#include "imager.h"
2
3/*
4 * i_scale_mixing() is based on code contained in pnmscale.c, part of
5 * the netpbm distribution. No code was copied from pnmscale but
6 * the algorthm was and for this I thank the netpbm crew.
7 *
8 * Tony
9 */
10
11/* pnmscale.c - read a portable anymap and scale it
12**
13** Copyright (C) 1989, 1991 by Jef Poskanzer.
14**
15** Permission to use, copy, modify, and distribute this software and its
16** documentation for any purpose and without fee is hereby granted, provided
17** that the above copyright notice appear in all copies and that both that
18** copyright notice and this permission notice appear in supporting
19** documentation. This software is provided "as is" without express or
20** implied warranty.
21**
22*/
23
24
25static void
26zero_row(i_fcolor *row, int width, int channels);
a10945af
TC
27
28#code
658f724e 29static void
a10945af 30IM_SUFFIX(accum_output_row)(i_fcolor *accum, double fraction, IM_COLOR const *in,
658f724e
TC
31 int width, int channels);
32static void
a10945af
TC
33IM_SUFFIX(horizontal_scale)(IM_COLOR *out, int out_width,
34 i_fcolor const *in, int in_width,
35 int channels);
36#/code
658f724e
TC
37
38/*
39=item i_scale_mixing
40
41Returns a new image scaled to the given size.
42
43Unlike i_scale_axis() this does a simple coverage of pixels from
44source to target and doesn't resample.
45
46Adapted from pnmscale.
47
48=cut
49*/
50i_img *
51i_scale_mixing(i_img *src, int x_out, int y_out) {
52 i_img *result;
658f724e
TC
53 i_fcolor *accum_row = NULL;
54 int y;
a10945af 55 int accum_row_bytes;
658f724e
TC
56 double rowsleft, fracrowtofill;
57 int rowsread;
58 double y_scale;
59
60 mm_log((1, "i_scale_mixing(src %p, x_out %d, y_out %d)\n",
61 src, x_out, y_out));
62
63 i_clear_error();
64
65 if (x_out <= 0) {
66 i_push_errorf(0, "output width %d invalid", x_out);
67 return NULL;
68 }
69 if (y_out <= 0) {
70 i_push_errorf(0, "output height %d invalid", y_out);
71 return NULL;
72 }
73
658f724e
TC
74 if (x_out == src->xsize && y_out == src->ysize) {
75 return i_copy(src);
76 }
77
78 y_scale = y_out / (double)src->ysize;
79
80 result = i_sametype_chans(src, x_out, y_out, src->channels);
81 if (!result)
82 return NULL;
83
a10945af
TC
84 accum_row_bytes = sizeof(i_fcolor) * src->xsize;
85 if (accum_row_bytes / sizeof(i_fcolor) != src->xsize) {
86 i_push_error(0, "integer overflow allocating accumulator row buffer");
87 return NULL;
88 }
89
90 accum_row = mymalloc(accum_row_bytes);
91
92#code src->bits <= 8
93 IM_COLOR *in_row = NULL;
94 IM_COLOR *xscale_row = NULL;
95 int in_row_bytes, out_row_bytes;
96
97 in_row_bytes = sizeof(IM_COLOR) * src->xsize;
98 if (in_row_bytes / sizeof(IM_COLOR) != src->xsize) {
99 i_push_error(0, "integer overflow allocating input row buffer");
100 return NULL;
101 }
102 out_row_bytes = sizeof(IM_COLOR) * x_out;
103 if (out_row_bytes / sizeof(IM_COLOR) != x_out) {
104 i_push_error(0, "integer overflow allocating output row buffer");
105 return NULL;
106 }
107
658f724e 108 in_row = mymalloc(in_row_bytes);
658f724e
TC
109 xscale_row = mymalloc(out_row_bytes);
110
111 rowsread = 0;
112 rowsleft = 0.0;
113 for (y = 0; y < y_out; ++y) {
114 if (y_out == src->ysize) {
a10945af
TC
115 /* no vertical scaling, just load it */
116 int x, ch;
117#ifdef IM_EIGHT_BIT
118 /* load and convert to doubles */
119 IM_GLIN(src, 0, src->xsize, y, in_row);
120 for (x = 0; x < src->xsize; ++x) {
121 for (ch = 0; ch < src->channels; ++ch) {
122 accum_row[x].channel[ch] = in_row[x].channel[ch];
123 }
124 }
125#else
126 IM_GLIN(src, 0, src->xsize, y, accum_row);
127#endif
658f724e
TC
128 }
129 else {
130 fracrowtofill = 1.0;
131 zero_row(accum_row, src->xsize, src->channels);
132 while (fracrowtofill > 0) {
133 if (rowsleft <= 0) {
134 if (rowsread < src->ysize) {
a10945af 135 IM_GLIN(src, 0, src->xsize, rowsread, in_row);
658f724e
TC
136 ++rowsread;
137 }
138 /* else just use the last row read */
139
140 rowsleft = y_scale;
141 }
142 if (rowsleft < fracrowtofill) {
a10945af
TC
143 IM_SUFFIX(accum_output_row)(accum_row, rowsleft, in_row,
144 src->xsize, src->channels);
658f724e
TC
145 fracrowtofill -= rowsleft;
146 rowsleft = 0;
147 }
148 else {
a10945af
TC
149 IM_SUFFIX(accum_output_row)(accum_row, fracrowtofill, in_row,
150 src->xsize, src->channels);
658f724e
TC
151 rowsleft -= fracrowtofill;
152 fracrowtofill = 0;
153 }
154 }
a10945af
TC
155 }
156 /* we've accumulated a vertically scaled row */
157 if (x_out == src->xsize) {
158 int x, ch;
159#if IM_EIGHT_BIT
160 /* no need to scale, but we need to convert it */
161 for (x = 0; x < x_out; ++x) {
162 for (ch = 0; ch < result->channels; ++ch)
163 xscale_row[x].channel[ch] = accum_row[x].channel[ch];
658f724e 164 }
a10945af
TC
165 IM_PLIN(result, 0, x_out, y, xscale_row);
166#else
167 IM_PLIN(result, 0, x_out, y, accum_row);
168#endif
169 }
170 else {
171 IM_SUFFIX(horizontal_scale)(xscale_row, x_out, accum_row,
172 src->xsize, src->channels);
173 IM_PLIN(result, 0, x_out, y, xscale_row);
658f724e
TC
174 }
175 }
658f724e 176 myfree(in_row);
658f724e 177 myfree(xscale_row);
a10945af
TC
178#/code
179 myfree(accum_row);
658f724e
TC
180
181 return result;
182}
183
184static void
185zero_row(i_fcolor *row, int width, int channels) {
186 int x;
187 int ch;
188
189 /* with IEEE floats we could just use memset() but that's not
a10945af
TC
190 safe in general under ANSI C.
191 memset() is slightly faster.
192 */
658f724e
TC
193 for (x = 0; x < width; ++x) {
194 for (ch = 0; ch < channels; ++ch)
195 row[x].channel[ch] = 0.0;
196 }
197}
198
a10945af
TC
199#code
200
658f724e 201static void
a10945af 202IM_SUFFIX(accum_output_row)(i_fcolor *accum, double fraction, IM_COLOR const *in,
658f724e
TC
203 int width, int channels) {
204 int x, ch;
205
a10945af
TC
206 /* it's tempting to change this into a pointer iteration loop but
207 modern CPUs do the indexing as part of the instruction */
658f724e
TC
208 for (x = 0; x < width; ++x) {
209 for (ch = 0; ch < channels; ++ch) {
210 accum[x].channel[ch] += in[x].channel[ch] * fraction;
211 }
212 }
213}
214
215static void
a10945af 216IM_SUFFIX(horizontal_scale)(IM_COLOR *out, int out_width,
658f724e
TC
217 i_fcolor const *in, int in_width,
218 int channels) {
219 double frac_col_to_fill, frac_col_left;
220 int in_x;
221 int out_x;
222 double x_scale = (double)out_width / in_width;
223 int ch;
224 double accum[MAXCHANNELS] = { 0 };
225
226 frac_col_to_fill = 1.0;
227 out_x = 0;
228 for (in_x = 0; in_x < in_width; ++in_x) {
229 frac_col_left = x_scale;
230 while (frac_col_left >= frac_col_to_fill) {
231 for (ch = 0; ch < channels; ++ch)
232 accum[ch] += frac_col_to_fill * in[in_x].channel[ch];
233
234 for (ch = 0; ch < channels; ++ch) {
235 out[out_x].channel[ch] = accum[ch];
236 accum[ch] = 0;
237 }
238 frac_col_left -= frac_col_to_fill;
239 frac_col_to_fill = 1.0;
240 ++out_x;
241 }
242
243 if (frac_col_left > 0) {
244 for (ch = 0; ch < channels; ++ch) {
245 accum[ch] += frac_col_left * in[in_x].channel[ch];
246 }
247 frac_col_to_fill -= frac_col_left;
248 }
249 }
250
251 if (out_x < out_width-1 || out_x > out_width) {
252 i_fatal(3, "Internal error: out_x %d out of range (width %d)", out_x, out_width);
253 }
254
255 if (out_x < out_width) {
256 for (ch = 0; ch < channels; ++ch) {
257 accum[ch] += frac_col_to_fill * in[in_width-1].channel[ch];
258 out[out_x].channel[ch] = accum[ch];
259 }
260 }
261}
a10945af
TC
262
263#/code