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 *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 *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.
139 i_img *i_img_16_new_low(i_img *im, int x, int y, int ch) {
140 mm_log((1,"i_img_16_new(x %d, y %d, ch %d)\n", x, y, ch));
142 *im = IIM_base_16bit_direct;
143 i_tags_new(&im->tags);
147 im->bytes = x * y * ch * 2;
149 im->idata = mymalloc(im->bytes);
151 memset(im->idata, 0, im->bytes);
154 i_tags_destroy(&im->tags);
161 i_img *i_img_16_new(int x, int y, int ch) {
164 im = mymalloc(sizeof(i_img));
166 if (!i_img_16_new_low(im, x, y, ch)) {
172 mm_log((1, "(%p) <- i_img_16_new\n", im));
177 static int i_ppix_d16(i_img *im, int x, int y, i_color *val) {
180 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
183 off = (x + y * im->xsize) * im->channels;
184 for (ch = 0; ch < im->channels; ++ch)
185 STORE8as16(im->idata, off+ch, val->channel[ch]);
190 static int i_gpix_d16(i_img *im, int x, int y, i_color *val) {
193 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
196 off = (x + y * im->xsize) * im->channels;
197 for (ch = 0; ch < im->channels; ++ch)
198 val->channel[ch] = GET16as8(im->idata, off+ch);
203 static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val) {
206 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
209 off = (x + y * im->xsize) * im->channels;
210 for (ch = 0; ch < im->channels; ++ch)
211 STORE16(im->idata, off+ch, SampleFTo16(val->channel[ch]));
216 static int i_gpixf_d16(i_img *im, int x, int y, i_fcolor *val) {
219 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
222 off = (x + y * im->xsize) * im->channels;
223 for (ch = 0; ch < im->channels; ++ch)
224 val->channel[ch] = Sample16ToF(GET16(im->idata, off+ch));
229 static int i_glin_d16(i_img *im, int l, int r, int y, i_color *vals) {
232 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
235 off = (l+y*im->xsize) * im->channels;
237 for (i = 0; i < count; ++i) {
238 for (ch = 0; ch < im->channels; ++ch) {
239 vals[i].channel[ch] = GET16as8(im->idata, off);
250 static int i_plin_d16(i_img *im, int l, int r, int y, i_color *vals) {
253 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
256 off = (l+y*im->xsize) * im->channels;
258 for (i = 0; i < count; ++i) {
259 for (ch = 0; ch < im->channels; ++ch) {
260 STORE8as16(im->idata, off, vals[i].channel[ch]);
271 static int i_glinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
274 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
277 off = (l+y*im->xsize) * im->channels;
279 for (i = 0; i < count; ++i) {
280 for (ch = 0; ch < im->channels; ++ch) {
281 vals[i].channel[ch] = Sample16ToF(GET16(im->idata, off));
292 static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
295 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
298 off = (l+y*im->xsize) * im->channels;
300 for (i = 0; i < count; ++i) {
301 for (ch = 0; ch < im->channels; ++ch) {
302 STORE16(im->idata, off, SampleFTo16(vals[i].channel[ch]));
313 static int i_gsamp_d16(i_img *im, int l, int r, int y, i_sample_t *samps,
314 int *chans, int chan_count) {
318 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
321 off = (l+y*im->xsize) * im->channels;
326 /* make sure we have good channel numbers */
327 for (ch = 0; ch < chan_count; ++ch) {
328 if (chans[ch] < 0 || chans[ch] >= im->channels) {
329 i_push_errorf(0, "No channel %d in this image", chans[ch]);
333 for (i = 0; i < w; ++i) {
334 for (ch = 0; ch < chan_count; ++ch) {
335 *samps++ = GET16as8(im->idata, off+chans[ch]);
342 for (i = 0; i < w; ++i) {
343 for (ch = 0; ch < chan_count; ++ch) {
344 *samps++ = GET16as8(im->idata, off+ch);
358 static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps,
359 int *chans, int chan_count) {
363 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
366 off = (l+y*im->xsize) * im->channels;
371 /* make sure we have good channel numbers */
372 for (ch = 0; ch < chan_count; ++ch) {
373 if (chans[ch] < 0 || chans[ch] >= im->channels) {
374 i_push_errorf(0, "No channel %d in this image", chans[ch]);
378 for (i = 0; i < w; ++i) {
379 for (ch = 0; ch < chan_count; ++ch) {
380 *samps++ = Sample16ToF(GET16(im->idata, off+chans[ch]));
387 for (i = 0; i < w; ++i) {
388 for (ch = 0; ch < chan_count; ++ch) {
389 *samps++ = Sample16ToF(GET16(im->idata, off+ch));