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.
11 /* pnmscale.c - read a portable anymap and scale it
13 ** Copyright (C) 1989, 1991 by Jef Poskanzer.
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
26 zero_row(i_fcolor *row, int width, int channels);
30 IM_SUFFIX(accum_output_row)(i_fcolor *accum, double fraction, IM_COLOR const *in,
31 int width, int channels);
33 IM_SUFFIX(horizontal_scale)(IM_COLOR *out, int out_width,
34 i_fcolor const *in, int in_width,
41 Returns a new image scaled to the given size.
43 Unlike i_scale_axis() this does a simple coverage of pixels from
44 source to target and doesn't resample.
46 Adapted from pnmscale.
51 i_scale_mixing(i_img *src, int x_out, int y_out) {
53 i_fcolor *accum_row = NULL;
56 double rowsleft, fracrowtofill;
60 mm_log((1, "i_scale_mixing(src %p, x_out %d, y_out %d)\n",
66 i_push_errorf(0, "output width %d invalid", x_out);
70 i_push_errorf(0, "output height %d invalid", y_out);
74 if (x_out == src->xsize && y_out == src->ysize) {
78 y_scale = y_out / (double)src->ysize;
80 result = i_sametype_chans(src, x_out, y_out, src->channels);
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");
90 accum_row = mymalloc(accum_row_bytes);
93 IM_COLOR *in_row = NULL;
94 IM_COLOR *xscale_row = NULL;
95 int in_row_bytes, out_row_bytes;
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");
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");
108 in_row = mymalloc(in_row_bytes);
109 xscale_row = mymalloc(out_row_bytes);
113 for (y = 0; y < y_out; ++y) {
114 if (y_out == src->ysize) {
115 /* no vertical scaling, just load it */
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];
126 IM_GLIN(src, 0, src->xsize, y, accum_row);
131 zero_row(accum_row, src->xsize, src->channels);
132 while (fracrowtofill > 0) {
134 if (rowsread < src->ysize) {
135 IM_GLIN(src, 0, src->xsize, rowsread, in_row);
138 /* else just use the last row read */
142 if (rowsleft < fracrowtofill) {
143 IM_SUFFIX(accum_output_row)(accum_row, rowsleft, in_row,
144 src->xsize, src->channels);
145 fracrowtofill -= rowsleft;
149 IM_SUFFIX(accum_output_row)(accum_row, fracrowtofill, in_row,
150 src->xsize, src->channels);
151 rowsleft -= fracrowtofill;
156 /* we've accumulated a vertically scaled row */
157 if (x_out == src->xsize) {
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];
165 IM_PLIN(result, 0, x_out, y, xscale_row);
167 IM_PLIN(result, 0, x_out, y, accum_row);
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);
185 zero_row(i_fcolor *row, int width, int channels) {
189 /* with IEEE floats we could just use memset() but that's not
190 safe in general under ANSI C.
191 memset() is slightly faster.
193 for (x = 0; x < width; ++x) {
194 for (ch = 0; ch < channels; ++ch)
195 row[x].channel[ch] = 0.0;
202 IM_SUFFIX(accum_output_row)(i_fcolor *accum, double fraction, IM_COLOR const *in,
203 int width, int channels) {
206 /* it's tempting to change this into a pointer iteration loop but
207 modern CPUs do the indexing as part of the instruction */
208 for (x = 0; x < width; ++x) {
209 for (ch = 0; ch < channels; ++ch) {
210 accum[x].channel[ch] += in[x].channel[ch] * fraction;
216 IM_SUFFIX(horizontal_scale)(IM_COLOR *out, int out_width,
217 i_fcolor const *in, int in_width,
219 double frac_col_to_fill, frac_col_left;
222 double x_scale = (double)out_width / in_width;
224 double accum[MAXCHANNELS] = { 0 };
226 frac_col_to_fill = 1.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];
234 for (ch = 0; ch < channels; ++ch) {
235 out[out_x].channel[ch] = accum[ch];
238 frac_col_left -= frac_col_to_fill;
239 frac_col_to_fill = 1.0;
243 if (frac_col_left > 0) {
244 for (ch = 0; ch < channels; ++ch) {
245 accum[ch] += frac_col_left * in[in_x].channel[ch];
247 frac_col_to_fill -= frac_col_left;
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);
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];