-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimg8.c
626 lines (509 loc) · 14.2 KB
/
img8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#include "imager.h"
#include "imageri.h"
static int i_ppix_d(i_img *im, i_img_dim x, i_img_dim y, const i_color *val);
static int i_gpix_d(i_img *im, i_img_dim x, i_img_dim y, i_color *val);
static i_img_dim i_glin_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals);
static i_img_dim i_plin_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals);
static int i_ppixf_d(i_img *im, i_img_dim x, i_img_dim y, const i_fcolor *val);
static int i_gpixf_d(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *val);
static i_img_dim i_glinf_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *vals);
static i_img_dim i_plinf_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fcolor *vals);
static i_img_dim i_gsamp_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, const int *chans, int chan_count);
static i_img_dim i_gsampf_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samps, const int *chans, int chan_count);
/*
=item IIM_base_8bit_direct (static)
A static i_img object used to initialize direct 8-bit per sample images.
=cut
*/
static i_img IIM_base_8bit_direct =
{
0, /* channels set */
0, 0, 0, /* xsize, ysize, bytes */
~0U, /* ch_mask */
i_8_bits, /* bits */
i_direct_type, /* type */
0, /* virtual */
NULL, /* idata */
{ 0, 0, NULL }, /* tags */
NULL, /* ext_data */
i_ppix_d, /* i_f_ppix */
i_ppixf_d, /* i_f_ppixf */
i_plin_d, /* i_f_plin */
i_plinf_d, /* i_f_plinf */
i_gpix_d, /* i_f_gpix */
i_gpixf_d, /* i_f_gpixf */
i_glin_d, /* i_f_glin */
i_glinf_d, /* i_f_glinf */
i_gsamp_d, /* i_f_gsamp */
i_gsampf_d, /* i_f_gsampf */
NULL, /* i_f_gpal */
NULL, /* i_f_ppal */
NULL, /* i_f_addcolors */
NULL, /* i_f_getcolors */
NULL, /* i_f_colorcount */
NULL, /* i_f_maxcolors */
NULL, /* i_f_findcolor */
NULL, /* i_f_setcolors */
NULL, /* i_f_destroy */
i_gsamp_bits_fb,
NULL, /* i_f_psamp_bits */
};
/*static void set_8bit_direct(i_img *im) {
im->i_f_ppix = i_ppix_d;
im->i_f_ppixf = i_ppixf_d;
im->i_f_plin = i_plin_d;
im->i_f_plinf = i_plinf_d;
im->i_f_gpix = i_gpix_d;
im->i_f_gpixf = i_gpixf_d;
im->i_f_glin = i_glin_d;
im->i_f_glinf = i_glinf_d;
im->i_f_gpal = NULL;
im->i_f_ppal = NULL;
im->i_f_addcolor = NULL;
im->i_f_getcolor = NULL;
im->i_f_colorcount = NULL;
im->i_f_findcolor = NULL;
}*/
/*
=item IIM_new(x, y, ch)
=item i_img_8_new(x, y, ch)
=category Image creation/destruction
=synopsis i_img *img = i_img_8_new(width, height, channels);
Creates a new image object I<x> pixels wide, and I<y> pixels high with
I<ch> channels.
=cut
*/
i_img *
IIM_new(i_img_dim x,i_img_dim y,int ch) {
i_img *im;
mm_log((1,"IIM_new(x %" i_DF ", y %" i_DF ", ch %d)\n",
i_DFc(x), i_DFc(y), ch));
im=i_img_empty_ch(NULL,x,y,ch);
mm_log((1,"(%p) <- IIM_new\n",im));
return im;
}
void
IIM_DESTROY(i_img *im) {
mm_log((1,"IIM_DESTROY(im* %p)\n",im));
i_img_destroy(im);
/* myfree(cl); */
}
/*
=item i_img_new()
Create new image reference - notice that this isn't an object yet and
this should be fixed asap.
=cut
*/
i_img *
i_img_new() {
i_img *im;
mm_log((1,"i_img_struct()\n"));
im = i_img_alloc();
*im = IIM_base_8bit_direct;
im->xsize=0;
im->ysize=0;
im->channels=3;
im->ch_mask=MAXINT;
im->bytes=0;
im->idata=NULL;
i_img_init(im);
mm_log((1,"(%p) <- i_img_struct\n",im));
return im;
}
/*
=item i_img_empty(im, x, y)
Re-new image reference (assumes 3 channels)
im - Image pointer
x - xsize of destination image
y - ysize of destination image
**FIXME** what happens if a live image is passed in here?
Should this just call i_img_empty_ch()?
=cut
*/
i_img *
i_img_empty(i_img *im,i_img_dim x,i_img_dim y) {
mm_log((1,"i_img_empty(*im %p, x %" i_DF ", y %" i_DF ")\n",
im, i_DFc(x), i_DFc(y)));
return i_img_empty_ch(im, x, y, 3);
}
/*
=item i_img_empty_ch(im, x, y, ch)
Re-new image reference
im - Image pointer
x - xsize of destination image
y - ysize of destination image
ch - number of channels
=cut
*/
i_img *
i_img_empty_ch(i_img *im,i_img_dim x,i_img_dim y,int ch) {
size_t bytes;
mm_log((1,"i_img_empty_ch(*im %p, x %" i_DF ", y %" i_DF ", ch %d)\n",
im, i_DFc(x), i_DFc(y), ch));
if (x < 1 || y < 1) {
i_push_error(0, "Image sizes must be positive");
return NULL;
}
if (ch < 1 || ch > MAXCHANNELS) {
i_push_errorf(0, "channels must be between 1 and %d", MAXCHANNELS);
return NULL;
}
/* check this multiplication doesn't overflow */
bytes = x*y*ch;
if (bytes / y / ch != x) {
i_push_errorf(0, "integer overflow calculating image allocation");
return NULL;
}
if (im == NULL)
im = i_img_alloc();
memcpy(im, &IIM_base_8bit_direct, sizeof(i_img));
i_tags_new(&im->tags);
im->xsize = x;
im->ysize = y;
im->channels = ch;
im->ch_mask = MAXINT;
im->bytes=bytes;
if ( (im->idata=mymalloc(im->bytes)) == NULL)
i_fatal(2,"malloc() error\n");
memset(im->idata,0,(size_t)im->bytes);
im->ext_data = NULL;
i_img_init(im);
mm_log((1,"(%p) <- i_img_empty_ch\n",im));
return im;
}
/*
=head2 8-bit per sample image internal functions
These are the functions installed in an 8-bit per sample image.
=over
=item i_ppix_d(im, x, y, col)
Internal function.
This is the function kept in the i_f_ppix member of an i_img object.
It does a normal store of a pixel into the image with range checking.
Returns 0 if the pixel could be set, -1 otherwise.
=cut
*/
static
int
i_ppix_d(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) {
int ch;
if ( x>-1 && x<im->xsize && y>-1 && y<im->ysize ) {
for(ch=0;ch<im->channels;ch++)
if (im->ch_mask&(1<<ch))
im->idata[(x+y*im->xsize)*im->channels+ch]=val->channel[ch];
return 0;
}
return -1; /* error was clipped */
}
/*
=item i_gpix_d(im, x, y, &col)
Internal function.
This is the function kept in the i_f_gpix member of an i_img object.
It does normal retrieval of a pixel from the image with range checking.
Returns 0 if the pixel could be set, -1 otherwise.
=cut
*/
static
int
i_gpix_d(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
int ch;
if (x>-1 && x<im->xsize && y>-1 && y<im->ysize) {
for(ch=0;ch<im->channels;ch++)
val->channel[ch]=im->idata[(x+y*im->xsize)*im->channels+ch];
return 0;
}
for(ch=0;ch<im->channels;ch++) val->channel[ch] = 0;
return -1; /* error was cliped */
}
/*
=item i_glin_d(im, l, r, y, vals)
Reads a line of data from the image, storing the pixels at vals.
The line runs from (l,y) inclusive to (r,y) non-inclusive
vals should point at space for (r-l) pixels.
l should never be less than zero (to avoid confusion about where to
put the pixels in vals).
Returns the number of pixels copied (eg. if r, l or y is out of range)
=cut
*/
static
i_img_dim
i_glin_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) {
int ch;
i_img_dim count, i;
unsigned char *data;
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
if (r > im->xsize)
r = im->xsize;
data = im->idata + (l+y*im->xsize) * im->channels;
count = r - l;
for (i = 0; i < count; ++i) {
for (ch = 0; ch < im->channels; ++ch)
vals[i].channel[ch] = *data++;
}
return count;
}
else {
return 0;
}
}
/*
=item i_plin_d(im, l, r, y, vals)
Writes a line of data into the image, using the pixels at vals.
The line runs from (l,y) inclusive to (r,y) non-inclusive
vals should point at (r-l) pixels.
l should never be less than zero (to avoid confusion about where to
get the pixels in vals).
Returns the number of pixels copied (eg. if r, l or y is out of range)
=cut
*/
static
i_img_dim
i_plin_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
int ch;
i_img_dim count, i;
unsigned char *data;
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
if (r > im->xsize)
r = im->xsize;
data = im->idata + (l+y*im->xsize) * im->channels;
count = r - l;
for (i = 0; i < count; ++i) {
for (ch = 0; ch < im->channels; ++ch) {
if (im->ch_mask & (1 << ch))
*data = vals[i].channel[ch];
++data;
}
}
return count;
}
else {
return 0;
}
}
/*
=item i_ppixf_d(im, x, y, val)
=cut
*/
static
int
i_ppixf_d(i_img *im, i_img_dim x, i_img_dim y, const i_fcolor *val) {
int ch;
if ( x>-1 && x<im->xsize && y>-1 && y<im->ysize ) {
for(ch=0;ch<im->channels;ch++)
if (im->ch_mask&(1<<ch)) {
im->idata[(x+y*im->xsize)*im->channels+ch] =
SampleFTo8(val->channel[ch]);
}
return 0;
}
return -1; /* error was clipped */
}
/*
=item i_gpixf_d(im, x, y, val)
=cut
*/
static
int
i_gpixf_d(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *val) {
int ch;
if (x>-1 && x<im->xsize && y>-1 && y<im->ysize) {
for(ch=0;ch<im->channels;ch++) {
val->channel[ch] =
Sample8ToF(im->idata[(x+y*im->xsize)*im->channels+ch]);
}
return 0;
}
return -1; /* error was cliped */
}
/*
=item i_glinf_d(im, l, r, y, vals)
Reads a line of data from the image, storing the pixels at vals.
The line runs from (l,y) inclusive to (r,y) non-inclusive
vals should point at space for (r-l) pixels.
l should never be less than zero (to avoid confusion about where to
put the pixels in vals).
Returns the number of pixels copied (eg. if r, l or y is out of range)
=cut
*/
static
i_img_dim
i_glinf_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *vals) {
int ch;
i_img_dim count, i;
unsigned char *data;
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
if (r > im->xsize)
r = im->xsize;
data = im->idata + (l+y*im->xsize) * im->channels;
count = r - l;
for (i = 0; i < count; ++i) {
for (ch = 0; ch < im->channels; ++ch)
vals[i].channel[ch] = Sample8ToF(*data++);
}
return count;
}
else {
return 0;
}
}
/*
=item i_plinf_d(im, l, r, y, vals)
Writes a line of data into the image, using the pixels at vals.
The line runs from (l,y) inclusive to (r,y) non-inclusive
vals should point at (r-l) pixels.
l should never be less than zero (to avoid confusion about where to
get the pixels in vals).
Returns the number of pixels copied (eg. if r, l or y is out of range)
=cut
*/
static
i_img_dim
i_plinf_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fcolor *vals) {
int ch;
i_img_dim count, i;
unsigned char *data;
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
if (r > im->xsize)
r = im->xsize;
data = im->idata + (l+y*im->xsize) * im->channels;
count = r - l;
for (i = 0; i < count; ++i) {
for (ch = 0; ch < im->channels; ++ch) {
if (im->ch_mask & (1 << ch))
*data = SampleFTo8(vals[i].channel[ch]);
++data;
}
}
return count;
}
else {
return 0;
}
}
/*
=item i_gsamp_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, int *chans, int chan_count)
Reads sample values from im for the horizontal line (l, y) to (r-1,y)
for the channels specified by chans, an array of int with chan_count
elements.
Returns the number of samples read (which should be (r-l) * bits_set(chan_mask)
=cut
*/
static
i_img_dim
i_gsamp_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps,
const int *chans, int chan_count) {
int ch;
i_img_dim count, i, w;
unsigned char *data;
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
if (r > im->xsize)
r = im->xsize;
data = im->idata + (l+y*im->xsize) * im->channels;
w = r - l;
count = 0;
if (chans) {
/* make sure we have good channel numbers */
for (ch = 0; ch < chan_count; ++ch) {
if (chans[ch] < 0 || chans[ch] >= im->channels) {
i_push_errorf(0, "No channel %d in this image", chans[ch]);
return 0;
}
}
for (i = 0; i < w; ++i) {
for (ch = 0; ch < chan_count; ++ch) {
*samps++ = data[chans[ch]];
++count;
}
data += im->channels;
}
}
else {
if (chan_count <= 0 || chan_count > im->channels) {
i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
chan_count);
return 0;
}
for (i = 0; i < w; ++i) {
for (ch = 0; ch < chan_count; ++ch) {
*samps++ = data[ch];
++count;
}
data += im->channels;
}
}
return count;
}
else {
return 0;
}
}
/*
=item i_gsampf_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samps, int *chans, int chan_count)
Reads sample values from im for the horizontal line (l, y) to (r-1,y)
for the channels specified by chan_mask, where bit 0 is the first
channel.
Returns the number of samples read (which should be (r-l) * bits_set(chan_mask)
=cut
*/
static
i_img_dim
i_gsampf_d(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samps,
const int *chans, int chan_count) {
int ch;
i_img_dim count, i, w;
unsigned char *data;
for (ch = 0; ch < chan_count; ++ch) {
if (chans[ch] < 0 || chans[ch] >= im->channels) {
i_push_errorf(0, "No channel %d in this image", chans[ch]);
}
}
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
if (r > im->xsize)
r = im->xsize;
data = im->idata + (l+y*im->xsize) * im->channels;
w = r - l;
count = 0;
if (chans) {
/* make sure we have good channel numbers */
for (ch = 0; ch < chan_count; ++ch) {
if (chans[ch] < 0 || chans[ch] >= im->channels) {
i_push_errorf(0, "No channel %d in this image", chans[ch]);
return 0;
}
}
for (i = 0; i < w; ++i) {
for (ch = 0; ch < chan_count; ++ch) {
*samps++ = Sample8ToF(data[chans[ch]]);
++count;
}
data += im->channels;
}
}
else {
if (chan_count <= 0 || chan_count > im->channels) {
i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
chan_count);
return 0;
}
for (i = 0; i < w; ++i) {
for (ch = 0; ch < chan_count; ++ch) {
*samps++ = Sample8ToF(data[ch]);
++count;
}
data += im->channels;
}
}
return count;
}
else {
return 0;
}
}
/*
=back
=head1 AUTHOR
Arnar M. Hrafnkelsson <addi@umich.edu>
Tony Cook <tony@develop-help.com>
=head1 SEE ALSO
L<Imager>
=cut
*/