4 imgdouble.c - implements double per sample images
8 i_img *im = i_img_double_new(int x, int y, int channels);
9 # use like a normal image
13 Implements double/sample images.
15 This basic implementation is required so that we have some larger
16 sample image type to work with.
26 static int i_ppix_ddoub(i_img *im, int x, int y, const i_color *val);
27 static int i_gpix_ddoub(i_img *im, int x, int y, i_color *val);
28 static int i_glin_ddoub(i_img *im, int l, int r, int y, i_color *vals);
29 static int i_plin_ddoub(i_img *im, int l, int r, int y, const i_color *vals);
30 static int i_ppixf_ddoub(i_img *im, int x, int y, const i_fcolor *val);
31 static int i_gpixf_ddoub(i_img *im, int x, int y, i_fcolor *val);
32 static int i_glinf_ddoub(i_img *im, int l, int r, int y, i_fcolor *vals);
33 static int i_plinf_ddoub(i_img *im, int l, int r, int y, const i_fcolor *vals);
34 static int i_gsamp_ddoub(i_img *im, int l, int r, int y, i_sample_t *samps,
35 int const *chans, int chan_count);
36 static int i_gsampf_ddoub(i_img *im, int l, int r, int y, i_fsample_t *samps,
37 int const *chans, int chan_count);
40 =item IIM_base_16bit_direct
42 Base structure used to initialize a 16-bit/sample image.
48 static i_img IIM_base_double_direct =
51 0, 0, 0, /* xsize, ysize, bytes */
53 i_double_bits, /* bits */
54 i_direct_type, /* type */
57 { 0, 0, NULL }, /* tags */
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 */
73 NULL, /* i_f_addcolors */
74 NULL, /* i_f_getcolors */
75 NULL, /* i_f_colorcount */
76 NULL, /* i_f_maxcolors */
77 NULL, /* i_f_findcolor */
78 NULL, /* i_f_setcolors */
80 NULL, /* i_f_destroy */
83 NULL, /* i_f_psamp_bits */
87 =item i_img_double_new(int x, int y, int ch)
88 =category Image creation/destruction
89 =synopsis i_img *img = i_img_double_new(width, height, channels);
91 Creates a new double per sample image.
95 i_img *i_img_double_new(int x, int y, int ch) {
99 mm_log((1,"i_img_double_new(x %d, y %d, ch %d)\n", x, y, ch));
101 if (x < 1 || y < 1) {
102 i_push_error(0, "Image sizes must be positive");
105 if (ch < 1 || ch > MAXCHANNELS) {
106 i_push_errorf(0, "channels must be between 1 and %d", MAXCHANNELS);
109 bytes = x * y * ch * sizeof(double);
110 if (bytes / y / ch / sizeof(double) != x) {
111 i_push_errorf(0, "integer overflow calculating image allocation");
116 *im = IIM_base_double_direct;
117 i_tags_new(&im->tags);
123 im->idata = mymalloc(im->bytes);
124 memset(im->idata, 0, im->bytes);
130 static int i_ppix_ddoub(i_img *im, int x, int y, const i_color *val) {
133 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
136 off = (x + y * im->xsize) * im->channels;
137 if (I_ALL_CHANNELS_WRITABLE(im)) {
138 for (ch = 0; ch < im->channels; ++ch)
139 ((double*)im->idata)[off+ch] = Sample8ToF(val->channel[ch]);
142 for (ch = 0; ch < im->channels; ++ch)
143 if (im->ch_mask & (1<<ch))
144 ((double*)im->idata)[off+ch] = Sample8ToF(val->channel[ch]);
150 static int i_gpix_ddoub(i_img *im, int x, int y, i_color *val) {
153 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
156 off = (x + y * im->xsize) * im->channels;
157 for (ch = 0; ch < im->channels; ++ch)
158 val->channel[ch] = SampleFTo8(((double *)im->idata)[off+ch]);
163 static int i_ppixf_ddoub(i_img *im, int x, int y, const i_fcolor *val) {
166 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
169 off = (x + y * im->xsize) * im->channels;
170 if (I_ALL_CHANNELS_WRITABLE(im)) {
171 for (ch = 0; ch < im->channels; ++ch)
172 ((double *)im->idata)[off+ch] = val->channel[ch];
175 for (ch = 0; ch < im->channels; ++ch)
176 if (im->ch_mask & (1 << ch))
177 ((double *)im->idata)[off+ch] = val->channel[ch];
183 static int i_gpixf_ddoub(i_img *im, int x, int y, i_fcolor *val) {
186 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
189 off = (x + y * im->xsize) * im->channels;
190 for (ch = 0; ch < im->channels; ++ch)
191 val->channel[ch] = ((double *)im->idata)[off+ch];
196 static int i_glin_ddoub(i_img *im, int l, int r, int y, i_color *vals) {
199 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
202 off = (l+y*im->xsize) * im->channels;
204 for (i = 0; i < count; ++i) {
205 for (ch = 0; ch < im->channels; ++ch) {
206 vals[i].channel[ch] = SampleFTo8(((double *)im->idata)[off]);
217 static int i_plin_ddoub(i_img *im, int l, int r, int y, const i_color *vals) {
220 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
223 off = (l+y*im->xsize) * im->channels;
225 if (I_ALL_CHANNELS_WRITABLE(im)) {
226 for (i = 0; i < count; ++i) {
227 for (ch = 0; ch < im->channels; ++ch) {
228 ((double *)im->idata)[off] = Sample8ToF(vals[i].channel[ch]);
234 for (i = 0; i < count; ++i) {
235 for (ch = 0; ch < im->channels; ++ch) {
236 if (im->ch_mask & (1 << ch))
237 ((double *)im->idata)[off] = Sample8ToF(vals[i].channel[ch]);
249 static int i_glinf_ddoub(i_img *im, int l, int r, int y, i_fcolor *vals) {
252 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
255 off = (l+y*im->xsize) * im->channels;
257 for (i = 0; i < count; ++i) {
258 for (ch = 0; ch < im->channels; ++ch) {
259 vals[i].channel[ch] = ((double *)im->idata)[off];
270 static int i_plinf_ddoub(i_img *im, int l, int r, int y, const i_fcolor *vals) {
273 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
276 off = (l+y*im->xsize) * im->channels;
278 if (I_ALL_CHANNELS_WRITABLE(im)) {
279 for (i = 0; i < count; ++i) {
280 for (ch = 0; ch < im->channels; ++ch) {
281 ((double *)im->idata)[off] = vals[i].channel[ch];
287 for (i = 0; i < count; ++i) {
288 for (ch = 0; ch < im->channels; ++ch) {
289 if (im->ch_mask & (1 << ch))
290 ((double *)im->idata)[off] = vals[i].channel[ch];
302 static int i_gsamp_ddoub(i_img *im, int l, int r, int y, i_sample_t *samps,
303 int const *chans, int chan_count) {
307 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
310 off = (l+y*im->xsize) * im->channels;
315 /* make sure we have good channel numbers */
316 for (ch = 0; ch < chan_count; ++ch) {
317 if (chans[ch] < 0 || chans[ch] >= im->channels) {
318 i_push_errorf(0, "No channel %d in this image", chans[ch]);
322 for (i = 0; i < w; ++i) {
323 for (ch = 0; ch < chan_count; ++ch) {
324 *samps++ = SampleFTo8(((double *)im->idata)[off+chans[ch]]);
331 if (chan_count <= 0 || chan_count > im->channels) {
332 i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
336 for (i = 0; i < w; ++i) {
337 for (ch = 0; ch < chan_count; ++ch) {
338 *samps++ = SampleFTo8(((double *)im->idata)[off+ch]);
352 static int i_gsampf_ddoub(i_img *im, int l, int r, int y, i_fsample_t *samps,
353 int const *chans, int chan_count) {
357 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
360 off = (l+y*im->xsize) * im->channels;
365 /* make sure we have good channel numbers */
366 for (ch = 0; ch < chan_count; ++ch) {
367 if (chans[ch] < 0 || chans[ch] >= im->channels) {
368 i_push_errorf(0, "No channel %d in this image", chans[ch]);
372 for (i = 0; i < w; ++i) {
373 for (ch = 0; ch < chan_count; ++ch) {
374 *samps++ = ((double *)im->idata)[off+chans[ch]];
381 if (chan_count <= 0 || chan_count > im->channels) {
382 i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
386 for (i = 0; i < w; ++i) {
387 for (ch = 0; ch < chan_count; ++ch) {
388 *samps++ = ((double *)im->idata)[off+ch];
403 =item i_img_to_drgb(im)
405 =category Image creation
407 Returns a double/sample version of the supplied image.
409 Returns the image on success, or NULL on failure.
415 i_img_to_drgb(i_img *im) {
420 targ = i_img_double_new(im->xsize, im->ysize, im->channels);
423 line = mymalloc(sizeof(i_fcolor) * im->xsize);
424 for (y = 0; y < im->ysize; ++y) {
425 i_glinf(im, 0, im->xsize, y, line);
426 i_plinf(targ, 0, im->xsize, y, line);
439 Tony Cook <tony@develop-help.com>