]> git.imager.perl.org - imager.git/blob - scale.im
- attempt to work around the test failure at
[imager.git] / scale.im
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
25 static void
26 zero_row(i_fcolor *row, int width, int channels);
27
28 #code
29 static void
30 IM_SUFFIX(accum_output_row)(i_fcolor *accum, double fraction, IM_COLOR const *in,
31                  int width, int channels);
32 static void
33 IM_SUFFIX(horizontal_scale)(IM_COLOR *out, int out_width, 
34                             i_fcolor const *in, int in_width,
35                             int channels);
36 #/code
37
38 /*
39 =item i_scale_mixing
40
41 Returns a new image scaled to the given size.
42
43 Unlike i_scale_axis() this does a simple coverage of pixels from
44 source to target and doesn't resample.
45
46 Adapted from pnmscale.
47
48 =cut
49 */
50 i_img *
51 i_scale_mixing(i_img *src, int x_out, int y_out) {
52   i_img *result;
53   i_fcolor *accum_row = NULL;
54   int y;
55   int accum_row_bytes;
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
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
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
108   in_row     = mymalloc(in_row_bytes);
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) {
115       /* no vertical scaling, just load it */
116 #ifdef IM_EIGHT_BIT
117       int x, ch;
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
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) {
135             IM_GLIN(src, 0, src->xsize, rowsread, in_row);
136             ++rowsread;
137           }
138           /* else just use the last row read */
139
140           rowsleft = y_scale;
141         }
142         if (rowsleft < fracrowtofill) {
143           IM_SUFFIX(accum_output_row)(accum_row, rowsleft, in_row, 
144                                       src->xsize, src->channels);
145           fracrowtofill -= rowsleft;
146           rowsleft = 0;
147         }
148         else {
149           IM_SUFFIX(accum_output_row)(accum_row, fracrowtofill, in_row, 
150                                       src->xsize, src->channels);
151           rowsleft -= fracrowtofill;
152           fracrowtofill = 0;
153         }
154       }
155     }
156     /* we've accumulated a vertically scaled row */
157     if (x_out == src->xsize) {
158 #if IM_EIGHT_BIT
159       int x, ch;
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];
164       }
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);
174     }
175   }
176   myfree(in_row);
177   myfree(xscale_row);
178 #/code
179   myfree(accum_row);
180
181   return result;
182 }
183
184 static void
185 zero_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
190      safe in general under ANSI C.
191      memset() is slightly faster.
192   */
193   for (x = 0; x < width; ++x) {
194     for (ch = 0; ch < channels; ++ch)
195       row[x].channel[ch] = 0.0;
196   }
197 }
198
199 #code
200
201 static void
202 IM_SUFFIX(accum_output_row)(i_fcolor *accum, double fraction, IM_COLOR const *in,
203                  int width, int channels) {
204   int x, ch;
205
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;
211     }
212   }
213 }
214
215 static void
216 IM_SUFFIX(horizontal_scale)(IM_COLOR *out, int out_width, 
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 }
262
263 #/code