add to_rgb16 method
[imager.git] / imgdouble.c
CommitLineData
2ce44e2a
TC
1/*
2=head1 NAME
3
4imgdouble.c - implements double per sample images
5
6=head1 SYNOPSIS
7
8 i_img *im = i_img_double_new(int x, int y, int channels);
9 # use like a normal image
10
11=head1 DESCRIPTION
12
13Implements double/sample images.
14
15This basic implementation is required so that we have some larger
16sample image type to work with.
17
18=over
19
20=cut
21*/
22
92bda632
TC
23#include "imager.h"
24#include "imageri.h"
2ce44e2a 25
97ac0a96 26static int i_ppix_ddoub(i_img *im, int x, int y, const i_color *val);
2ce44e2a
TC
27static int i_gpix_ddoub(i_img *im, int x, int y, i_color *val);
28static int i_glin_ddoub(i_img *im, int l, int r, int y, i_color *vals);
97ac0a96
TC
29static int i_plin_ddoub(i_img *im, int l, int r, int y, const i_color *vals);
30static int i_ppixf_ddoub(i_img *im, int x, int y, const i_fcolor *val);
2ce44e2a
TC
31static int i_gpixf_ddoub(i_img *im, int x, int y, i_fcolor *val);
32static int i_glinf_ddoub(i_img *im, int l, int r, int y, i_fcolor *vals);
97ac0a96 33static int i_plinf_ddoub(i_img *im, int l, int r, int y, const i_fcolor *vals);
2ce44e2a 34static int i_gsamp_ddoub(i_img *im, int l, int r, int y, i_sample_t *samps,
18accb2a 35 int const *chans, int chan_count);
2ce44e2a 36static int i_gsampf_ddoub(i_img *im, int l, int r, int y, i_fsample_t *samps,
18accb2a 37 int const *chans, int chan_count);
2ce44e2a
TC
38
39/*
40=item IIM_base_16bit_direct
41
42Base structure used to initialize a 16-bit/sample image.
43
44Internal.
45
46=cut
47*/
48static i_img IIM_base_double_direct =
49{
50 0, /* channels set */
51 0, 0, 0, /* xsize, ysize, bytes */
9a88a5e6 52 ~0U, /* ch_mask */
2ce44e2a
TC
53 i_double_bits, /* bits */
54 i_direct_type, /* type */
55 0, /* virtual */
56 NULL, /* idata */
57 { 0, 0, NULL }, /* tags */
58 NULL, /* ext_data */
59
60 i_ppix_ddoub, /* i_f_ppix */
61 i_ppixf_ddoub, /* i_f_ppixf */
62 i_plin_ddoub, /* i_f_plin */
63 i_plinf_ddoub, /* i_f_plinf */
64 i_gpix_ddoub, /* i_f_gpix */
65 i_gpixf_ddoub, /* i_f_gpixf */
66 i_glin_ddoub, /* i_f_glin */
67 i_glinf_ddoub, /* i_f_glinf */
68 i_gsamp_ddoub, /* i_f_gsamp */
69 i_gsampf_ddoub, /* i_f_gsampf */
70
71 NULL, /* i_f_gpal */
72 NULL, /* i_f_ppal */
73 NULL, /* i_f_addcolor */
74 NULL, /* i_f_getcolor */
75 NULL, /* i_f_colorcount */
76 NULL, /* i_f_findcolor */
77
78 NULL, /* i_f_destroy */
79};
80
81/*
82=item i_img_double_new(int x, int y, int ch)
83
92bda632
TC
84=category Image creation
85
2ce44e2a
TC
86Creates a new double per sample image.
87
88=cut
89*/
90i_img *i_img_double_new_low(i_img *im, int x, int y, int ch) {
653ea321
TC
91 int bytes;
92
2ce44e2a 93 mm_log((1,"i_img_double_new(x %d, y %d, ch %d)\n", x, y, ch));
1501d9b3
TC
94
95 if (x < 1 || y < 1) {
96 i_push_error(0, "Image sizes must be positive");
97 return NULL;
98 }
99 if (ch < 1 || ch > MAXCHANNELS) {
100 i_push_errorf(0, "channels must be between 1 and %d", MAXCHANNELS);
101 return NULL;
102 }
653ea321
TC
103 bytes = x * y * ch * sizeof(double);
104 if (bytes / y / ch / sizeof(double) != x) {
105 i_push_errorf(0, "integer overflow calculating image allocation");
106 return NULL;
107 }
2ce44e2a
TC
108
109 *im = IIM_base_double_direct;
110 i_tags_new(&im->tags);
111 im->xsize = x;
112 im->ysize = y;
113 im->channels = ch;
653ea321 114 im->bytes = bytes;
2ce44e2a
TC
115 im->ext_data = NULL;
116 im->idata = mymalloc(im->bytes);
117 if (im->idata) {
118 memset(im->idata, 0, im->bytes);
119 }
120 else {
121 i_tags_destroy(&im->tags);
122 im = NULL;
123 }
124
125 return im;
126}
127
128i_img *i_img_double_new(int x, int y, int ch) {
129 i_img *im;
130
1501d9b3
TC
131 i_clear_error();
132
2ce44e2a
TC
133 im = mymalloc(sizeof(i_img));
134 if (im) {
135 if (!i_img_double_new_low(im, x, y, ch)) {
136 myfree(im);
137 im = NULL;
138 }
139 }
140
141 mm_log((1, "(%p) <- i_img_double_new\n", im));
142
143 return im;
144}
145
97ac0a96 146static int i_ppix_ddoub(i_img *im, int x, int y, const i_color *val) {
2ce44e2a
TC
147 int off, ch;
148
149 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
150 return -1;
151
152 off = (x + y * im->xsize) * im->channels;
35f40526
TC
153 if (I_ALL_CHANNELS_WRITABLE(im)) {
154 for (ch = 0; ch < im->channels; ++ch)
155 ((double*)im->idata)[off+ch] = Sample8ToF(val->channel[ch]);
156 }
157 else {
158 for (ch = 0; ch < im->channels; ++ch)
159 if (im->ch_mask & (1<<ch))
160 ((double*)im->idata)[off+ch] = Sample8ToF(val->channel[ch]);
161 }
2ce44e2a
TC
162
163 return 0;
164}
165
166static int i_gpix_ddoub(i_img *im, int x, int y, i_color *val) {
167 int off, ch;
168
169 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
170 return -1;
171
172 off = (x + y * im->xsize) * im->channels;
173 for (ch = 0; ch < im->channels; ++ch)
174 val->channel[ch] = SampleFTo8(((double *)im->idata)[off+ch]);
175
176 return 0;
177}
178
97ac0a96 179static int i_ppixf_ddoub(i_img *im, int x, int y, const i_fcolor *val) {
2ce44e2a
TC
180 int off, ch;
181
182 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
183 return -1;
184
185 off = (x + y * im->xsize) * im->channels;
35f40526
TC
186 if (I_ALL_CHANNELS_WRITABLE(im)) {
187 for (ch = 0; ch < im->channels; ++ch)
188 ((double *)im->idata)[off+ch] = val->channel[ch];
189 }
190 else {
191 for (ch = 0; ch < im->channels; ++ch)
192 if (im->ch_mask & (1 << ch))
193 ((double *)im->idata)[off+ch] = val->channel[ch];
194 }
2ce44e2a
TC
195
196 return 0;
197}
198
199static int i_gpixf_ddoub(i_img *im, int x, int y, i_fcolor *val) {
200 int off, ch;
201
202 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
203 return -1;
204
205 off = (x + y * im->xsize) * im->channels;
206 for (ch = 0; ch < im->channels; ++ch)
207 val->channel[ch] = ((double *)im->idata)[off+ch];
208
209 return 0;
210}
211
212static int i_glin_ddoub(i_img *im, int l, int r, int y, i_color *vals) {
213 int ch, count, i;
214 int off;
215 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
216 if (r > im->xsize)
217 r = im->xsize;
218 off = (l+y*im->xsize) * im->channels;
219 count = r - l;
220 for (i = 0; i < count; ++i) {
221 for (ch = 0; ch < im->channels; ++ch) {
222 vals[i].channel[ch] = SampleFTo8(((double *)im->idata)[off]);
223 ++off;
224 }
225 }
226 return count;
227 }
228 else {
229 return 0;
230 }
231}
232
97ac0a96 233static int i_plin_ddoub(i_img *im, int l, int r, int y, const i_color *vals) {
2ce44e2a
TC
234 int ch, count, i;
235 int off;
236 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
237 if (r > im->xsize)
238 r = im->xsize;
239 off = (l+y*im->xsize) * im->channels;
240 count = r - l;
35f40526
TC
241 if (I_ALL_CHANNELS_WRITABLE(im)) {
242 for (i = 0; i < count; ++i) {
243 for (ch = 0; ch < im->channels; ++ch) {
244 ((double *)im->idata)[off] = Sample8ToF(vals[i].channel[ch]);
245 ++off;
246 }
247 }
248 }
249 else {
250 for (i = 0; i < count; ++i) {
251 for (ch = 0; ch < im->channels; ++ch) {
252 if (im->ch_mask & (1 << ch))
253 ((double *)im->idata)[off] = Sample8ToF(vals[i].channel[ch]);
254 ++off;
255 }
2ce44e2a
TC
256 }
257 }
258 return count;
259 }
260 else {
261 return 0;
262 }
263}
264
265static int i_glinf_ddoub(i_img *im, int l, int r, int y, i_fcolor *vals) {
266 int ch, count, i;
267 int off;
268 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
269 if (r > im->xsize)
270 r = im->xsize;
271 off = (l+y*im->xsize) * im->channels;
272 count = r - l;
273 for (i = 0; i < count; ++i) {
274 for (ch = 0; ch < im->channels; ++ch) {
275 vals[i].channel[ch] = ((double *)im->idata)[off];
276 ++off;
277 }
278 }
279 return count;
280 }
281 else {
282 return 0;
283 }
284}
285
97ac0a96 286static int i_plinf_ddoub(i_img *im, int l, int r, int y, const i_fcolor *vals) {
2ce44e2a
TC
287 int ch, count, i;
288 int off;
289 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
290 if (r > im->xsize)
291 r = im->xsize;
292 off = (l+y*im->xsize) * im->channels;
293 count = r - l;
35f40526
TC
294 if (I_ALL_CHANNELS_WRITABLE(im)) {
295 for (i = 0; i < count; ++i) {
296 for (ch = 0; ch < im->channels; ++ch) {
297 ((double *)im->idata)[off] = vals[i].channel[ch];
298 ++off;
299 }
300 }
301 }
302 else {
303 for (i = 0; i < count; ++i) {
304 for (ch = 0; ch < im->channels; ++ch) {
305 if (im->ch_mask & (1 << ch))
306 ((double *)im->idata)[off] = vals[i].channel[ch];
307 ++off;
308 }
2ce44e2a
TC
309 }
310 }
311 return count;
312 }
313 else {
314 return 0;
315 }
316}
317
318static int i_gsamp_ddoub(i_img *im, int l, int r, int y, i_sample_t *samps,
18accb2a 319 int const *chans, int chan_count) {
2ce44e2a
TC
320 int ch, count, i, w;
321 int off;
322
323 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
324 if (r > im->xsize)
325 r = im->xsize;
326 off = (l+y*im->xsize) * im->channels;
327 w = r - l;
328 count = 0;
329
330 if (chans) {
331 /* make sure we have good channel numbers */
332 for (ch = 0; ch < chan_count; ++ch) {
333 if (chans[ch] < 0 || chans[ch] >= im->channels) {
334 i_push_errorf(0, "No channel %d in this image", chans[ch]);
335 return 0;
336 }
337 }
338 for (i = 0; i < w; ++i) {
339 for (ch = 0; ch < chan_count; ++ch) {
340 *samps++ = SampleFTo8(((double *)im->idata)[off+chans[ch]]);
341 ++count;
342 }
343 off += im->channels;
344 }
345 }
346 else {
347 for (i = 0; i < w; ++i) {
348 for (ch = 0; ch < chan_count; ++ch) {
349 *samps++ = SampleFTo8(((double *)im->idata)[off+ch]);
350 ++count;
351 }
352 off += im->channels;
353 }
354 }
355
356 return count;
357 }
358 else {
359 return 0;
360 }
361}
362
363static int i_gsampf_ddoub(i_img *im, int l, int r, int y, i_fsample_t *samps,
18accb2a 364 int const *chans, int chan_count) {
2ce44e2a
TC
365 int ch, count, i, w;
366 int off;
367
368 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
369 if (r > im->xsize)
370 r = im->xsize;
371 off = (l+y*im->xsize) * im->channels;
372 w = r - l;
373 count = 0;
374
375 if (chans) {
376 /* make sure we have good channel numbers */
377 for (ch = 0; ch < chan_count; ++ch) {
378 if (chans[ch] < 0 || chans[ch] >= im->channels) {
379 i_push_errorf(0, "No channel %d in this image", chans[ch]);
380 return 0;
381 }
382 }
383 for (i = 0; i < w; ++i) {
384 for (ch = 0; ch < chan_count; ++ch) {
385 *samps++ = ((double *)im->idata)[off+chans[ch]];
386 ++count;
387 }
388 off += im->channels;
389 }
390 }
391 else {
392 for (i = 0; i < w; ++i) {
393 for (ch = 0; ch < chan_count; ++ch) {
394 *samps++ = ((double *)im->idata)[off+ch];
395 ++count;
396 }
397 off += im->channels;
398 }
399 }
400
401 return count;
402 }
403 else {
404 return 0;
405 }
406}
407
b8c2033e
AMH
408
409/*
410=back
411
412=head1 AUTHOR
413
414Tony Cook <tony@develop-help.com>
415
416=head1 SEE ALSO
417
418Imager(3)
419
420=cut
421*/