4 img16.c - implements 16-bit images
8 i_img *im = i_img_16_new(int x, int y, int channels);
9 # use like a normal image
13 Implements 16-bit/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_d16(i_img *im, int x, int y, i_color *val);
27 static int i_gpix_d16(i_img *im, int x, int y, i_color *val);
28 static int i_glin_d16(i_img *im, int l, int r, int y, i_color *vals);
29 static int i_plin_d16(i_img *im, int l, int r, int y, i_color *vals);
30 static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val);
31 static int i_gpixf_d16(i_img *im, int x, int y, i_fcolor *val);
32 static int i_glinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals);
33 static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals);
34 static int i_gsamp_d16(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_d16(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_16bit_direct =
51 0, 0, 0, /* xsize, ysize, bytes */
54 i_direct_type, /* type */
57 { 0, 0, NULL }, /* tags */
60 i_ppix_d16, /* i_f_ppix */
61 i_ppixf_d16, /* i_f_ppixf */
62 i_plin_d16, /* i_f_plin */
63 i_plinf_d16, /* i_f_plinf */
64 i_gpix_d16, /* i_f_gpix */
65 i_gpixf_d16, /* i_f_gpixf */
66 i_glin_d16, /* i_f_glin */
67 i_glinf_d16, /* i_f_glinf */
68 i_gsamp_d16, /* i_f_gsamp */
69 i_gsampf_d16, /* i_f_gsampf */
73 NULL, /* i_f_addcolor */
74 NULL, /* i_f_getcolor */
75 NULL, /* i_f_colorcount */
76 NULL, /* i_f_findcolor */
78 NULL, /* i_f_destroy */
81 /* it's possible some platforms won't have a 16-bit integer type,
82 so we check for one otherwise we work by bytes directly
84 We do assume 8-bit char
86 #if __STDC_VERSION__ >= 199901L
87 /* C99 should define something useful */
90 typedef uint16_t i_sample16_t;
95 /* check out unsigned short */
98 #if USHRT_MAX == 65535
99 typedef unsigned short i_sample16_t;
106 /* we have a real 16-bit unsigned integer */
107 #define STORE16(bytes, offset, word) \
108 (((i_sample16_t *)(bytes))[offset] = (word))
109 #define STORE8as16(bytes, offset, byte) \
110 (((i_sample16_t *)(bytes))[offset] = (byte) * 256)
111 #define GET16(bytes, offset) \
112 (((i_sample16_t *)(bytes))[offset])
113 #define GET16as8(bytes, offset) \
114 (((i_sample16_t *)(bytes))[offset] / 256)
118 /* we have to do this the hard way */
119 #define STORE16(bytes, offset, word) \
120 ((((unsigned char *)(bytes))[(offset)*2] = (word) >> 8), \
121 (((unsigned char *)(bytes))[(offset)*2+1] = (word) & 0xFF))
122 #define STORE8as16(bytes, offset, byte) \
123 ((((unsigned char *)(bytes))[(offset)*2] = (byte)), \
124 (((unsigned char *)(bytes))[(offset)*2+1] = 0))
126 #define GET16(bytes, offset) \
127 (((unsigned char *)(bytes))[(offset)*2] * 256 \
128 + ((unsigned char *)(bytes))[(offset)*2+1])
129 #define GET16as8(bytes, offset) \
130 (((unsigned char *)(bytes))[(offset)*2] << 8)
135 =item i_img_16_new(int x, int y, int ch)
137 Creates a new 16-bit per sample image.
141 i_img *i_img_16_new_low(i_img *im, int x, int y, int ch) {
142 mm_log((1,"i_img_16_new(x %d, y %d, ch %d)\n", x, y, ch));
144 *im = IIM_base_16bit_direct;
145 i_tags_new(&im->tags);
149 im->bytes = x * y * ch * 2;
151 im->idata = mymalloc(im->bytes);
153 memset(im->idata, 0, im->bytes);
156 i_tags_destroy(&im->tags);
163 i_img *i_img_16_new(int x, int y, int ch) {
166 im = mymalloc(sizeof(i_img));
168 if (!i_img_16_new_low(im, x, y, ch)) {
174 mm_log((1, "(%p) <- i_img_16_new\n", im));
179 static int i_ppix_d16(i_img *im, int x, int y, i_color *val) {
182 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
185 off = (x + y * im->xsize) * im->channels;
186 for (ch = 0; ch < im->channels; ++ch)
187 STORE8as16(im->idata, off+ch, val->channel[ch]);
192 static int i_gpix_d16(i_img *im, int x, int y, i_color *val) {
195 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
198 off = (x + y * im->xsize) * im->channels;
199 for (ch = 0; ch < im->channels; ++ch)
200 val->channel[ch] = GET16as8(im->idata, off+ch);
205 static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val) {
208 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
211 off = (x + y * im->xsize) * im->channels;
212 for (ch = 0; ch < im->channels; ++ch)
213 STORE16(im->idata, off+ch, SampleFTo16(val->channel[ch]));
218 static int i_gpixf_d16(i_img *im, int x, int y, i_fcolor *val) {
221 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
224 off = (x + y * im->xsize) * im->channels;
225 for (ch = 0; ch < im->channels; ++ch)
226 val->channel[ch] = Sample16ToF(GET16(im->idata, off+ch));
231 static int i_glin_d16(i_img *im, int l, int r, int y, i_color *vals) {
234 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
237 off = (l+y*im->xsize) * im->channels;
239 for (i = 0; i < count; ++i) {
240 for (ch = 0; ch < im->channels; ++ch) {
241 vals[i].channel[ch] = GET16as8(im->idata, off);
252 static int i_plin_d16(i_img *im, int l, int r, int y, i_color *vals) {
255 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
258 off = (l+y*im->xsize) * im->channels;
260 for (i = 0; i < count; ++i) {
261 for (ch = 0; ch < im->channels; ++ch) {
262 STORE8as16(im->idata, off, vals[i].channel[ch]);
273 static int i_glinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
276 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
279 off = (l+y*im->xsize) * im->channels;
281 for (i = 0; i < count; ++i) {
282 for (ch = 0; ch < im->channels; ++ch) {
283 vals[i].channel[ch] = Sample16ToF(GET16(im->idata, off));
294 static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
297 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
300 off = (l+y*im->xsize) * im->channels;
302 for (i = 0; i < count; ++i) {
303 for (ch = 0; ch < im->channels; ++ch) {
304 STORE16(im->idata, off, SampleFTo16(vals[i].channel[ch]));
315 static int i_gsamp_d16(i_img *im, int l, int r, int y, i_sample_t *samps,
316 int const *chans, int chan_count) {
320 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
323 off = (l+y*im->xsize) * im->channels;
328 /* make sure we have good channel numbers */
329 for (ch = 0; ch < chan_count; ++ch) {
330 if (chans[ch] < 0 || chans[ch] >= im->channels) {
331 i_push_errorf(0, "No channel %d in this image", chans[ch]);
335 for (i = 0; i < w; ++i) {
336 for (ch = 0; ch < chan_count; ++ch) {
337 *samps++ = GET16as8(im->idata, off+chans[ch]);
344 for (i = 0; i < w; ++i) {
345 for (ch = 0; ch < chan_count; ++ch) {
346 *samps++ = GET16as8(im->idata, off+ch);
360 static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps,
361 int const *chans, int chan_count) {
365 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
368 off = (l+y*im->xsize) * im->channels;
373 /* make sure we have good channel numbers */
374 for (ch = 0; ch < chan_count; ++ch) {
375 if (chans[ch] < 0 || chans[ch] >= im->channels) {
376 i_push_errorf(0, "No channel %d in this image", chans[ch]);
380 for (i = 0; i < w; ++i) {
381 for (ch = 0; ch < chan_count; ++ch) {
382 *samps++ = Sample16ToF(GET16(im->idata, off+chans[ch]));
389 for (i = 0; i < w; ++i) {
390 for (ch = 0; ch < chan_count; ++ch) {
391 *samps++ = Sample16ToF(GET16(im->idata, off+ch));
410 Tony Cook <tony@develop-help.com>