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);
28 accum_output_row(i_fcolor *accum, double fraction, i_fcolor const *in,
29 int width, int channels);
31 horizontal_scale(i_fcolor *out, int out_width,
32 i_fcolor const *in, int in_width,
38 Returns a new image scaled to the given size.
40 Unlike i_scale_axis() this does a simple coverage of pixels from
41 source to target and doesn't resample.
43 Adapted from pnmscale.
48 i_scale_mixing(i_img *src, int x_out, int y_out) {
50 i_fcolor *in_row = NULL;
51 i_fcolor *xscale_row = NULL;
52 i_fcolor *accum_row = NULL;
54 int in_row_bytes, out_row_bytes;
55 double rowsleft, fracrowtofill;
59 mm_log((1, "i_scale_mixing(src %p, x_out %d, y_out %d)\n",
65 i_push_errorf(0, "output width %d invalid", x_out);
69 i_push_errorf(0, "output height %d invalid", y_out);
73 in_row_bytes = sizeof(i_fcolor) * src->xsize;
74 if (in_row_bytes / sizeof(i_fcolor) != src->xsize) {
75 i_push_error(0, "integer overflow allocating input row buffer");
78 out_row_bytes = sizeof(i_fcolor) * x_out;
79 if (out_row_bytes / sizeof(i_fcolor) != x_out) {
80 i_push_error(0, "integer overflow allocating output row buffer");
84 if (x_out == src->xsize && y_out == src->ysize) {
88 y_scale = y_out / (double)src->ysize;
90 result = i_sametype_chans(src, x_out, y_out, src->channels);
94 in_row = mymalloc(in_row_bytes);
95 accum_row = mymalloc(in_row_bytes);
96 xscale_row = mymalloc(out_row_bytes);
100 for (y = 0; y < y_out; ++y) {
101 if (y_out == src->ysize) {
102 i_glinf(src, 0, src->xsize, y, accum_row);
106 zero_row(accum_row, src->xsize, src->channels);
107 while (fracrowtofill > 0) {
109 if (rowsread < src->ysize) {
110 i_glinf(src, 0, src->xsize, rowsread, in_row);
113 /* else just use the last row read */
117 if (rowsleft < fracrowtofill) {
118 accum_output_row(accum_row, rowsleft, in_row, src->xsize,
120 fracrowtofill -= rowsleft;
124 accum_output_row(accum_row, fracrowtofill, in_row, src->xsize,
126 rowsleft -= fracrowtofill;
130 /* we've accumulated a vertically scaled row */
131 if (x_out == src->xsize) {
132 /* no need to scale */
133 i_plinf(result, 0, x_out, y, accum_row);
136 horizontal_scale(xscale_row, x_out, accum_row, src->xsize,
138 i_plinf(result, 0, x_out, y, xscale_row);
151 zero_row(i_fcolor *row, int width, int channels) {
155 /* with IEEE floats we could just use memset() but that's not
156 safe in general under ANSI C */
157 for (x = 0; x < width; ++x) {
158 for (ch = 0; ch < channels; ++ch)
159 row[x].channel[ch] = 0.0;
164 accum_output_row(i_fcolor *accum, double fraction, i_fcolor const *in,
165 int width, int channels) {
168 for (x = 0; x < width; ++x) {
169 for (ch = 0; ch < channels; ++ch) {
170 accum[x].channel[ch] += in[x].channel[ch] * fraction;
176 horizontal_scale(i_fcolor *out, int out_width,
177 i_fcolor const *in, int in_width,
179 double frac_col_to_fill, frac_col_left;
182 double x_scale = (double)out_width / in_width;
184 double accum[MAXCHANNELS] = { 0 };
186 frac_col_to_fill = 1.0;
188 for (in_x = 0; in_x < in_width; ++in_x) {
189 frac_col_left = x_scale;
190 while (frac_col_left >= frac_col_to_fill) {
191 for (ch = 0; ch < channels; ++ch)
192 accum[ch] += frac_col_to_fill * in[in_x].channel[ch];
194 for (ch = 0; ch < channels; ++ch) {
195 out[out_x].channel[ch] = accum[ch];
198 frac_col_left -= frac_col_to_fill;
199 frac_col_to_fill = 1.0;
203 if (frac_col_left > 0) {
204 for (ch = 0; ch < channels; ++ch) {
205 accum[ch] += frac_col_left * in[in_x].channel[ch];
207 frac_col_to_fill -= frac_col_left;
211 if (out_x < out_width-1 || out_x > out_width) {
212 i_fatal(3, "Internal error: out_x %d out of range (width %d)", out_x, out_width);
215 if (out_x < out_width) {
216 for (ch = 0; ch < channels; ++ch) {
217 accum[ch] += frac_col_to_fill * in[in_width-1].channel[ch];
218 out[out_x].channel[ch] = accum[ch];