comparisons against $ExtUtils::MakeMaker::VERSION were warning when
[imager.git] / scale.c
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);
27static void
28accum_output_row(i_fcolor *accum, double fraction, i_fcolor const *in,
29 int width, int channels);
30static void
31horizontal_scale(i_fcolor *out, int out_width,
32 i_fcolor const *in, int in_width,
33 int channels);
34
35/*
36=item i_scale_mixing
37
38Returns a new image scaled to the given size.
39
40Unlike i_scale_axis() this does a simple coverage of pixels from
41source to target and doesn't resample.
42
43Adapted from pnmscale.
44
45=cut
46*/
47i_img *
48i_scale_mixing(i_img *src, int x_out, int y_out) {
49 i_img *result;
50 i_fcolor *in_row = NULL;
51 i_fcolor *xscale_row = NULL;
52 i_fcolor *accum_row = NULL;
53 int y;
54 int in_row_bytes, out_row_bytes;
55 double rowsleft, fracrowtofill;
56 int rowsread;
57 double y_scale;
58
59 mm_log((1, "i_scale_mixing(src %p, x_out %d, y_out %d)\n",
60 src, x_out, y_out));
61
62 i_clear_error();
63
64 if (x_out <= 0) {
65 i_push_errorf(0, "output width %d invalid", x_out);
66 return NULL;
67 }
68 if (y_out <= 0) {
69 i_push_errorf(0, "output height %d invalid", y_out);
70 return NULL;
71 }
72
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");
76 return NULL;
77 }
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");
81 return NULL;
82 }
83
84 if (x_out == src->xsize && y_out == src->ysize) {
85 return i_copy(src);
86 }
87
88 y_scale = y_out / (double)src->ysize;
89
90 result = i_sametype_chans(src, x_out, y_out, src->channels);
91 if (!result)
92 return NULL;
93
94 in_row = mymalloc(in_row_bytes);
95 accum_row = mymalloc(in_row_bytes);
96 xscale_row = mymalloc(out_row_bytes);
97
98 rowsread = 0;
99 rowsleft = 0.0;
100 for (y = 0; y < y_out; ++y) {
101 if (y_out == src->ysize) {
102 i_glinf(src, 0, src->xsize, y, accum_row);
103 }
104 else {
105 fracrowtofill = 1.0;
106 zero_row(accum_row, src->xsize, src->channels);
107 while (fracrowtofill > 0) {
108 if (rowsleft <= 0) {
109 if (rowsread < src->ysize) {
110 i_glinf(src, 0, src->xsize, rowsread, in_row);
111 ++rowsread;
112 }
113 /* else just use the last row read */
114
115 rowsleft = y_scale;
116 }
117 if (rowsleft < fracrowtofill) {
118 accum_output_row(accum_row, rowsleft, in_row, src->xsize,
119 src->channels);
120 fracrowtofill -= rowsleft;
121 rowsleft = 0;
122 }
123 else {
124 accum_output_row(accum_row, fracrowtofill, in_row, src->xsize,
125 src->channels);
126 rowsleft -= fracrowtofill;
127 fracrowtofill = 0;
128 }
129 }
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);
134 }
135 else {
136 horizontal_scale(xscale_row, x_out, accum_row, src->xsize,
137 src->channels);
138 i_plinf(result, 0, x_out, y, xscale_row);
139 }
140 }
141 }
142
143 myfree(in_row);
144 myfree(accum_row);
145 myfree(xscale_row);
146
147 return result;
148}
149
150static void
151zero_row(i_fcolor *row, int width, int channels) {
152 int x;
153 int ch;
154
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;
160 }
161}
162
163static void
164accum_output_row(i_fcolor *accum, double fraction, i_fcolor const *in,
165 int width, int channels) {
166 int x, ch;
167
168 for (x = 0; x < width; ++x) {
169 for (ch = 0; ch < channels; ++ch) {
170 accum[x].channel[ch] += in[x].channel[ch] * fraction;
171 }
172 }
173}
174
175static void
176horizontal_scale(i_fcolor *out, int out_width,
177 i_fcolor const *in, int in_width,
178 int channels) {
179 double frac_col_to_fill, frac_col_left;
180 int in_x;
181 int out_x;
182 double x_scale = (double)out_width / in_width;
183 int ch;
184 double accum[MAXCHANNELS] = { 0 };
185
186 frac_col_to_fill = 1.0;
187 out_x = 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];
193
194 for (ch = 0; ch < channels; ++ch) {
195 out[out_x].channel[ch] = accum[ch];
196 accum[ch] = 0;
197 }
198 frac_col_left -= frac_col_to_fill;
199 frac_col_to_fill = 1.0;
200 ++out_x;
201 }
202
203 if (frac_col_left > 0) {
204 for (ch = 0; ch < channels; ++ch) {
205 accum[ch] += frac_col_left * in[in_x].channel[ch];
206 }
207 frac_col_to_fill -= frac_col_left;
208 }
209 }
210
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);
213 }
214
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];
219 }
220 }
221}