]> git.imager.perl.org - imager.git/blame - img16.c
Increased buffersize for iolayer buffer chains.
[imager.git] / img16.c
CommitLineData
faa9b3e7
TC
1/*
2=head1 NAME
3
4img16.c - implements 16-bit images
5
6=head1 SYNOPSIS
7
8 i_img *im = i_img_16_new(int x, int y, int channels);
9 # use like a normal image
10
11=head1 DESCRIPTION
12
13Implements 16-bit/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
23#include "image.h"
24#include "imagei.h"
25
26static int i_ppix_d16(i_img *im, int x, int y, i_color *val);
27static int i_gpix_d16(i_img *im, int x, int y, i_color *val);
28static int i_glin_d16(i_img *im, int l, int r, int y, i_color *vals);
29static int i_plin_d16(i_img *im, int l, int r, int y, i_color *vals);
30static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val);
31static int i_gpixf_d16(i_img *im, int x, int y, i_fcolor *val);
32static int i_glinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals);
33static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals);
34static int i_gsamp_d16(i_img *im, int l, int r, int y, i_sample_t *samps,
35 int *chans, int chan_count);
36static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps,
37 int *chans, int chan_count);
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_16bit_direct =
49{
50 0, /* channels set */
51 0, 0, 0, /* xsize, ysize, bytes */
9a88a5e6 52 ~0U, /* ch_mask */
faa9b3e7
TC
53 i_16_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_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 */
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/* 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
83
84 We do assume 8-bit char
85*/
86#if __STDC_VERSION__ >= 199901L
87/* C99 should define something useful */
88#include <stdint.h>
89#ifdef UINT16_MAX
90typedef uint16_t i_sample16_t;
91#define GOT16
92#endif
93#endif
94
95/* check out unsigned short */
96#ifndef GOT16
97#include <limits.h>
98#if USHRT_MAX == 65535
99typedef unsigned short i_sample16_t;
100#define GOT16
101#endif
102#endif
103
104#ifdef GOT16
105
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)
115
116#else
117
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))
125
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)
131
132#endif
133
134/*
135=item i_img_16_new(int x, int y, int ch)
136
137Creates a new 16-bit per sample image.
365ea842
TC
138
139=cut
faa9b3e7
TC
140*/
141i_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));
143
144 *im = IIM_base_16bit_direct;
145 i_tags_new(&im->tags);
146 im->xsize = x;
147 im->ysize = y;
148 im->channels = ch;
149 im->bytes = x * y * ch * 2;
150 im->ext_data = NULL;
151 im->idata = mymalloc(im->bytes);
152 if (im->idata) {
153 memset(im->idata, 0, im->bytes);
154 }
155 else {
156 i_tags_destroy(&im->tags);
157 im = NULL;
158 }
159
160 return im;
161}
162
163i_img *i_img_16_new(int x, int y, int ch) {
164 i_img *im;
165
166 im = mymalloc(sizeof(i_img));
167 if (im) {
168 if (!i_img_16_new_low(im, x, y, ch)) {
169 myfree(im);
170 im = NULL;
171 }
172 }
173
174 mm_log((1, "(%p) <- i_img_16_new\n", im));
175
176 return im;
177}
178
179static int i_ppix_d16(i_img *im, int x, int y, i_color *val) {
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;
186 for (ch = 0; ch < im->channels; ++ch)
187 STORE8as16(im->idata, off+ch, val->channel[ch]);
188
189 return 0;
190}
191
192static int i_gpix_d16(i_img *im, int x, int y, i_color *val) {
193 int off, ch;
194
195 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
196 return -1;
197
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);
201
202 return 0;
203}
204
205static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val) {
206 int off, ch;
207
208 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
209 return -1;
210
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]));
214
215 return 0;
216}
217
218static int i_gpixf_d16(i_img *im, int x, int y, i_fcolor *val) {
219 int off, ch;
220
221 if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize)
222 return -1;
223
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));
227
228 return 0;
229}
230
231static int i_glin_d16(i_img *im, int l, int r, int y, i_color *vals) {
232 int ch, count, i;
233 int off;
234 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
235 if (r > im->xsize)
236 r = im->xsize;
237 off = (l+y*im->xsize) * im->channels;
238 count = r - l;
239 for (i = 0; i < count; ++i) {
240 for (ch = 0; ch < im->channels; ++ch) {
241 vals[i].channel[ch] = GET16as8(im->idata, off);
242 ++off;
243 }
244 }
245 return count;
246 }
247 else {
248 return 0;
249 }
250}
251
252static int i_plin_d16(i_img *im, int l, int r, int y, i_color *vals) {
253 int ch, count, i;
254 int off;
255 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
256 if (r > im->xsize)
257 r = im->xsize;
258 off = (l+y*im->xsize) * im->channels;
259 count = r - l;
260 for (i = 0; i < count; ++i) {
261 for (ch = 0; ch < im->channels; ++ch) {
262 STORE8as16(im->idata, off, vals[i].channel[ch]);
263 ++off;
264 }
265 }
266 return count;
267 }
268 else {
269 return 0;
270 }
271}
272
273static int i_glinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
274 int ch, count, i;
275 int off;
276 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
277 if (r > im->xsize)
278 r = im->xsize;
279 off = (l+y*im->xsize) * im->channels;
280 count = r - l;
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));
284 ++off;
285 }
286 }
287 return count;
288 }
289 else {
290 return 0;
291 }
292}
293
294static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
295 int ch, count, i;
296 int off;
297 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
298 if (r > im->xsize)
299 r = im->xsize;
300 off = (l+y*im->xsize) * im->channels;
301 count = r - l;
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]));
305 ++off;
306 }
307 }
308 return count;
309 }
310 else {
311 return 0;
312 }
313}
314
315static int i_gsamp_d16(i_img *im, int l, int r, int y, i_sample_t *samps,
316 int *chans, int chan_count) {
317 int ch, count, i, w;
318 int off;
319
320 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
321 if (r > im->xsize)
322 r = im->xsize;
323 off = (l+y*im->xsize) * im->channels;
324 w = r - l;
325 count = 0;
326
327 if (chans) {
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]);
332 return 0;
333 }
334 }
335 for (i = 0; i < w; ++i) {
336 for (ch = 0; ch < chan_count; ++ch) {
337 *samps++ = GET16as8(im->idata, off+chans[ch]);
338 ++count;
339 }
340 off += im->channels;
341 }
342 }
343 else {
344 for (i = 0; i < w; ++i) {
345 for (ch = 0; ch < chan_count; ++ch) {
346 *samps++ = GET16as8(im->idata, off+ch);
347 ++count;
348 }
349 off += im->channels;
350 }
351 }
352
353 return count;
354 }
355 else {
356 return 0;
357 }
358}
359
360static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps,
361 int *chans, int chan_count) {
362 int ch, count, i, w;
363 int off;
364
365 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
366 if (r > im->xsize)
367 r = im->xsize;
368 off = (l+y*im->xsize) * im->channels;
369 w = r - l;
370 count = 0;
371
372 if (chans) {
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]);
377 return 0;
378 }
379 }
380 for (i = 0; i < w; ++i) {
381 for (ch = 0; ch < chan_count; ++ch) {
382 *samps++ = Sample16ToF(GET16(im->idata, off+chans[ch]));
383 ++count;
384 }
385 off += im->channels;
386 }
387 }
388 else {
389 for (i = 0; i < w; ++i) {
390 for (ch = 0; ch < chan_count; ++ch) {
391 *samps++ = Sample16ToF(GET16(im->idata, off+ch));
392 ++count;
393 }
394 off += im->channels;
395 }
396 }
397
398 return count;
399 }
400 else {
401 return 0;
402 }
403}
404
b8c2033e
AMH
405/*
406=back
407
408=head1 AUTHOR
409
410Tony Cook <tony@develop-help.com>
411
412=head1 SEE ALSO
413
414Imager(3)
415
416=cut
417*/