Debian Codegraph @

maskimg.c

raw
1 /*
2 =head1 NAME
3
4 maskimg.c - implements masked images/image subsets
5
6 =head1 SYNOPSIS
7
8 =head1 DESCRIPTION
9
10 =over
11 =cut
12 */
13
14 #define IMAGER_NO_CONTEXT
15
16 #include "imager.h"
17 #include "imageri.h"
18
19 #include <stdio.h>
20 /*
21 =item i_img_mask_ext
22
23 A pointer to this type of object is kept in the ext_data of a masked
24 image.
25
26 =cut
27 */
28
29 typedef struct {
30 i_img *targ;
31 i_img *mask;
32 i_img_dim xbase, ybase;
33 i_sample_t *samps; /* temp space */
34 } i_img_mask_ext;
35
36 #define MASKEXT(im) ((i_img_mask_ext *)((im)->ext_data))
37
38 static void i_destroy_masked(i_img *im);
39 static int i_ppix_masked(i_img *im, i_img_dim x, i_img_dim y, const i_color *pix);
40 static int i_ppixf_masked(i_img *im, i_img_dim x, i_img_dim y, const i_fcolor *pix);
41 static i_img_dim i_plin_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals);
42 static i_img_dim i_plinf_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fcolor *vals);
43 static int i_gpix_masked(i_img *im, i_img_dim x, i_img_dim y, i_color *pix);
44 static int i_gpixf_masked(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *pix);
45 static i_img_dim i_glin_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals);
46 static i_img_dim i_glinf_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *vals);
47 static i_img_dim i_gsamp_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samp,
48 int const *chans, int chan_count);
49 static i_img_dim i_gsampf_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samp,
50 int const *chans, int chan_count);
51 static i_img_dim i_gpal_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals);
52 static i_img_dim i_ppal_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals);
53 static i_img_dim
54 psamp_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
55 const i_sample_t *samples, const int *chans, int chan_count);
56 static i_img_dim
57 psampf_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
58 const i_fsample_t *samples, const int *chans, int chan_count);
59
60 /*
61 =item IIM_base_masked
62
63 The basic data we copy into a masked image.
64
65 =cut
66 */
67 static i_img IIM_base_masked =
68 {
69 0, /* channels set */
70 0, 0, 0, /* xsize, ysize, bytes */
71 ~0U, /* ch_mask */
72 i_8_bits, /* bits */
73 i_palette_type, /* type */
74 1, /* virtual */
75 NULL, /* idata */
76 { 0, 0, NULL }, /* tags */
77 NULL, /* ext_data */
78
79 i_ppix_masked, /* i_f_ppix */
80 i_ppixf_masked, /* i_f_ppixf */
81 i_plin_masked, /* i_f_plin */
82 i_plinf_masked, /* i_f_plinf */
83 i_gpix_masked, /* i_f_gpix */
84 i_gpixf_masked, /* i_f_gpixf */
85 i_glin_masked, /* i_f_glin */
86 i_glinf_masked, /* i_f_glinf */
87 i_gsamp_masked, /* i_f_gsamp */
88 i_gsampf_masked, /* i_f_gsampf */
89
90 i_gpal_masked, /* i_f_gpal */
91 i_ppal_masked, /* i_f_ppal */
92 i_addcolors_forward, /* i_f_addcolors */
93 i_getcolors_forward, /* i_f_getcolors */
94 i_colorcount_forward, /* i_f_colorcount */
95 i_maxcolors_forward, /* i_f_maxcolors */
96 i_findcolor_forward, /* i_f_findcolor */
97 i_setcolors_forward, /* i_f_setcolors */
98
99 i_destroy_masked, /* i_f_destroy */
100
101 NULL, /* i_f_gsamp_bits */
102 NULL, /* i_f_psamp_bits */
103
104 psamp_masked, /* i_f_psamp */
105 psampf_masked, /* i_f_psampf */
106
107 NULL,
108 NULL
109 };
110
111 /*
112 =item i_img_masked_new(i_img *targ, i_img *mask, i_img_dim xbase, i_img_dim ybase, i_img_dim w, i_img_dim h)
113
114 Create a new masked image.
115
116 The image mask is optional, in which case the image is just a view of
117 a rectangular portion of the image.
118
119 The mask only has an effect of writing to the image, the entire view
120 of the underlying image is readable.
121
122 pixel access to mimg(x,y) is translated to targ(x+xbase, y+ybase), as long
123 as (0 <= x < w) and (0 <= y < h).
124
125 For a pixel to be writable, the pixel mask(x,y) must have non-zero in
126 it's first channel. No scaling of the pixel is done, the channel
127 sample is treated as boolean.
128
129 =cut
130 */
131
132 i_img *
133 i_img_masked_new(i_img *targ, i_img *mask, i_img_dim x, i_img_dim y, i_img_dim w, i_img_dim h) {
134 i_img *im;
135 i_img_mask_ext *ext;
136 dIMCTXim(targ);
137
138 im_clear_error(aIMCTX);
139 if (x < 0 || x >= targ->xsize || y < 0 || y >= targ->ysize) {
140 im_push_error(aIMCTX, 0, "subset outside of target image");
141 return NULL;
142 }
143 if (mask) {
144 if (w > mask->xsize)
145 w = mask->xsize;
146 if (h > mask->ysize)
147 h = mask->ysize;
148 }
149 if (x+w > targ->xsize)
150 w = targ->xsize - x;
151 if (y+h > targ->ysize)
152 h = targ->ysize - y;
153
154 if (w < 1 || h < 1) {
155 im_push_error(aIMCTX, 0, "width and height must be greater than or equal to 1");
156 return NULL;
157 }
158
159 im = im_img_alloc(aIMCTX);
160
161 memcpy(im, &IIM_base_masked, sizeof(i_img));
162 i_tags_new(&im->tags);
163 im->xsize = w;
164 im->ysize = h;
165 im->channels = targ->channels;
166 im->bits = targ->bits;
167 im->type = targ->type;
168 ext = mymalloc(sizeof(*ext));
169 ext->targ = targ;
170 ext->mask = mask;
171 ext->xbase = x;
172 ext->ybase = y;
173 ext->samps = mymalloc(sizeof(i_sample_t) * im->xsize);
174 im->ext_data = ext;
175
176 im_img_init(aIMCTX, im);
177
178 return im;
179 }
180
181 /*
182 =item i_destroy_masked(i_img *im)
183
184 The destruction handler for masked images.
185
186 Releases the ext_data.
187
188 Internal function.
189
190 =cut
191 */
192
193 static void i_destroy_masked(i_img *im) {
194 myfree(MASKEXT(im)->samps);
195 myfree(im->ext_data);
196 }
197
198 /*
199 =item i_ppix_masked(i_img *im, i_img_dim x, i_img_dim y, const i_color *pix)
200
201 Write a pixel to a masked image.
202
203 Internal function.
204
205 =cut
206 */
207 static int i_ppix_masked(i_img *im, i_img_dim x, i_img_dim y, const i_color *pix) {
208 i_img_mask_ext *ext = MASKEXT(im);
209 int result;
210
211 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
212 return -1;
213 if (ext->mask) {
214 i_sample_t samp;
215
216 if (i_gsamp(ext->mask, x, x+1, y, &samp, NULL, 1) && !samp)
217 return 0; /* pretend it was good */
218 }
219 result = i_ppix(ext->targ, x + ext->xbase, y + ext->ybase, pix);
220 im->type = ext->targ->type;
221 return result;
222 }
223
224 /*
225 =item i_ppixf_masked(i_img *im, i_img_dim x, i_img_dim y, const i_fcolor *pix)
226
227 Write a pixel to a masked image.
228
229 Internal function.
230
231 =cut
232 */
233 static int i_ppixf_masked(i_img *im, i_img_dim x, i_img_dim y, const i_fcolor *pix) {
234 i_img_mask_ext *ext = MASKEXT(im);
235 int result;
236
237 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
238 return -1;
239 if (ext->mask) {
240 i_sample_t samp;
241
242 if (i_gsamp(ext->mask, x, x+1, y, &samp, NULL, 1) && !samp)
243 return 0; /* pretend it was good */
244 }
245 result = i_ppixf(ext->targ, x + ext->xbase, y + ext->ybase, pix);
246 im->type = ext->targ->type;
247 return result;
248 }
249
250 /*
251 =item i_plin_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)
252
253 Write a row of data to a masked image.
254
255 Internal function.
256
257 =cut
258 */
259 static i_img_dim i_plin_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
260 i_img_mask_ext *ext = MASKEXT(im);
261
262 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
263 if (r > im->xsize)
264 r = im->xsize;
265 if (ext->mask) {
266 i_img_dim i;
267 int simple = 0;
268 i_sample_t *samps = ext->samps;
269 i_img_dim w = r - l;
270
271 i_gsamp(ext->mask, l, r, y, samps, NULL, 1);
272 if (w < 10)
273 simple = 1;
274 else {
275 /* the idea is to make a fast scan to see how often the state
276 changes */
277 i_img_dim changes = 0;
278 for (i = 0; i < w-1; ++i)
279 if (!samps[i] != !samps[i+1])
280 ++changes;
281 if (changes > w/3) /* just rough */
282 simple = 1;
283 }
284 if (simple) {
285 /* we'd be calling a usually more complicated i_plin function
286 almost as often as the usually simple i_ppix(), so just
287 do a simple scan
288 */
289 for (i = 0; i < w; ++i) {
290 if (samps[i])
291 i_ppix(ext->targ, l + i + ext->xbase, y + ext->ybase, vals + i);
292 }
293 im->type = ext->targ->type;
294 return r-l;
295 }
296 else {
297 /* the scan above indicates there should be some contiguous
298 regions, look for them and render
299 */
300 i_img_dim start;
301 i = 0;
302 while (i < w) {
303 while (i < w && !samps[i])
304 ++i;
305 start = i;
306 while (i < w && samps[i])
307 ++i;
308 if (i != start)
309 i_plin(ext->targ, l + start + ext->xbase, l + i + ext->xbase,
310 y + ext->ybase, vals + start);
311 }
312 im->type = ext->targ->type;
313 return w;
314 }
315 }
316 else {
317 i_img_dim result = i_plin(ext->targ, l + ext->xbase, r + ext->xbase,
318 y + ext->ybase, vals);
319 im->type = ext->targ->type;
320 return result;
321 }
322 }
323 else {
324 return 0;
325 }
326 }
327
328 /*
329 =item i_plinf_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fcolor *vals)
330
331 Write a row of data to a masked image.
332
333 Internal function.
334
335 =cut
336 */
337 static i_img_dim i_plinf_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fcolor *vals) {
338 i_img_mask_ext *ext = MASKEXT(im);
339 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
340 if (r > im->xsize)
341 r = im->xsize;
342 if (ext->mask) {
343 i_img_dim i;
344 int simple = 0;
345 i_sample_t *samps = ext->samps;
346 i_img_dim w = r - l;
347
348 i_gsamp(ext->mask, l, r, y, samps, NULL, 1);
349 if (w < 10)
350 simple = 1;
351 else {
352 /* the idea is to make a fast scan to see how often the state
353 changes */
354 i_img_dim changes = 0;
355 for (i = 0; i < w-1; ++i)
356 if (!samps[i] != !samps[i+1])
357 ++changes;
358 if (changes > w/3) /* just rough */
359 simple = 1;
360 }
361 if (simple) {
362 /* we'd be calling a usually more complicated i_plin function
363 almost as often as the usually simple i_ppix(), so just
364 do a simple scan
365 */
366 for (i = 0; i < w; ++i) {
367 if (samps[i])
368 i_ppixf(ext->targ, l + i + ext->xbase, y + ext->ybase, vals+i);
369 }
370 im->type = ext->targ->type;
371 return r-l;
372 }
373 else {
374 /* the scan above indicates there should be some contiguous
375 regions, look for them and render
376 */
377 i_img_dim start;
378 i = 0;
379 while (i < w) {
380 while (i < w && !samps[i])
381 ++i;
382 start = i;
383 while (i < w && samps[i])
384 ++i;
385 if (i != start)
386 i_plinf(ext->targ, l + start + ext->xbase, l + i + ext->xbase,
387 y + ext->ybase, vals + start);
388 }
389 im->type = ext->targ->type;
390 return w;
391 }
392 }
393 else {
394 i_img_dim result = i_plinf(ext->targ, l + ext->xbase, r + ext->xbase,
395 y + ext->ybase, vals);
396 im->type = ext->targ->type;
397 return result;
398 }
399 }
400 else {
401 return 0;
402 }
403 }
404
405 /*
406 =item i_gpix_masked(i_img *im, i_img_dim x, i_img_dim y, i_color *pix)
407
408 Read a pixel from a masked image.
409
410 Internal.
411
412 =cut
413 */
414 static int i_gpix_masked(i_img *im, i_img_dim x, i_img_dim y, i_color *pix) {
415 i_img_mask_ext *ext = MASKEXT(im);
416
417 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
418 return -1;
419
420 return i_gpix(ext->targ, x + ext->xbase, y + ext->ybase, pix);
421 }
422
423 /*
424 =item i_gpixf_masked(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *pix)
425
426 Read a pixel from a masked image.
427
428 Internal.
429
430 =cut
431 */
432 static int i_gpixf_masked(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *pix) {
433 i_img_mask_ext *ext = MASKEXT(im);
434
435 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
436 return -1;
437
438 return i_gpixf(ext->targ, x + ext->xbase, y + ext->ybase, pix);
439 }
440
441 static i_img_dim i_glin_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) {
442 i_img_mask_ext *ext = MASKEXT(im);
443 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
444 if (r > im->xsize)
445 r = im->xsize;
446 return i_glin(ext->targ, l + ext->xbase, r + ext->xbase,
447 y + ext->ybase, vals);
448 }
449 else {
450 return 0;
451 }
452 }
453
454 static i_img_dim i_glinf_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *vals) {
455 i_img_mask_ext *ext = MASKEXT(im);
456 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
457 if (r > im->xsize)
458 r = im->xsize;
459 return i_glinf(ext->targ, l + ext->xbase, r + ext->xbase,
460 y + ext->ybase, vals);
461 }
462 else {
463 return 0;
464 }
465 }
466
467 static i_img_dim i_gsamp_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samp,
468 int const *chans, int chan_count) {
469 i_img_mask_ext *ext = MASKEXT(im);
470 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
471 if (r > im->xsize)
472 r = im->xsize;
473 return i_gsamp(ext->targ, l + ext->xbase, r + ext->xbase,
474 y + ext->ybase, samp, chans, chan_count);
475 }
476 else {
477 return 0;
478 }
479 }
480
481 static i_img_dim i_gsampf_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samp,
482 int const *chans, int chan_count) {
483 i_img_mask_ext *ext = MASKEXT(im);
484 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
485 if (r > im->xsize)
486 r = im->xsize;
487 return i_gsampf(ext->targ, l + ext->xbase, r + ext->xbase,
488 y + ext->ybase, samp, chans, chan_count);
489 }
490 else {
491 return 0;
492 }
493 }
494
495 static i_img_dim i_gpal_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals) {
496 i_img_mask_ext *ext = MASKEXT(im);
497 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
498 if (r > im->xsize)
499 r = im->xsize;
500 return i_gpal(ext->targ, l + ext->xbase, r + ext->xbase,
501 y + ext->ybase, vals);
502 }
503 else {
504 return 0;
505 }
506 }
507
508 static i_img_dim i_ppal_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals) {
509 i_img_mask_ext *ext = MASKEXT(im);
510 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
511 if (r > im->xsize)
512 r = im->xsize;
513 if (ext->mask) {
514 i_img_dim i;
515 i_sample_t *samps = ext->samps;
516 i_img_dim w = r - l;
517 i_img_dim start;
518
519 i_gsamp(ext->mask, l, r, y, samps, NULL, 1);
520 i = 0;
521 while (i < w) {
522 while (i < w && !samps[i])
523 ++i;
524 start = i;
525 while (i < w && samps[i])
526 ++i;
527 if (i != start)
528 i_ppal(ext->targ, l+start+ext->xbase, l+i+ext->xbase,
529 y+ext->ybase, vals+start);
530 }
531 return w;
532 }
533 else {
534 return i_ppal(ext->targ, l + ext->xbase, r + ext->xbase,
535 y + ext->ybase, vals);
536 }
537 }
538 else {
539 return 0;
540 }
541 }
542
543 /*
544 =item psamp_masked()
545
546 i_psamp() implementation for masked images.
547
548 =cut
549 */
550
551 static i_img_dim
552 psamp_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
553 const i_sample_t *samples, const int *chans, int chan_count) {
554 i_img_mask_ext *ext = MASKEXT(im);
555
556 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
557 unsigned old_ch_mask = ext->targ->ch_mask;
558 i_img_dim result = 0;
559 ext->targ->ch_mask = im->ch_mask;
560 if (r > im->xsize)
561 r = im->xsize;
562 if (ext->mask) {
563 i_img_dim w = r - l;
564 i_img_dim i = 0;
565 i_img_dim x = ext->xbase + l;
566 i_img_dim work_y = y + ext->ybase;
567 i_sample_t *mask_samps = ext->samps;
568
569 i_gsamp(ext->mask, l, r, y, mask_samps, NULL, 1);
570 /* not optimizing this yet */
571 while (i < w) {
572 if (mask_samps[i]) {
573 /* found a set mask value, try to do a run */
574 i_img_dim run_left = x;
575 const i_sample_t *run_samps = samples;
576 ++i;
577 ++x;
578 samples += chan_count;
579
580 while (i < w && mask_samps[i]) {
581 ++i;
582 ++x;
583 samples += chan_count;
584 }
585 result += i_psamp(ext->targ, run_left, x, work_y, run_samps, chans, chan_count);
586 }
587 else {
588 ++i;
589 ++x;
590 samples += chan_count;
591 result += chan_count; /* pretend we wrote masked off pixels */
592 }
593 }
594 }
595 else {
596 result = i_psamp(ext->targ, l + ext->xbase, r + ext->xbase,
597 y + ext->ybase, samples, chans, chan_count);
598 im->type = ext->targ->type;
599 }
600 ext->targ->ch_mask = old_ch_mask;
601 return result;
602 }
603 else {
604 dIMCTXim(im);
605 i_push_error(0, "Image position outside of image");
606 return -1;
607 }
608 }
609
610 /*
611 =item psampf_masked()
612
613 i_psampf() implementation for masked images.
614
615 =cut
616 */
617
618 static i_img_dim
619 psampf_masked(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
620 const i_fsample_t *samples, const int *chans, int chan_count) {
621 i_img_mask_ext *ext = MASKEXT(im);
622
623 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
624 i_img_dim result = 0;
625 unsigned old_ch_mask = ext->targ->ch_mask;
626 ext->targ->ch_mask = im->ch_mask;
627 if (r > im->xsize)
628 r = im->xsize;
629 if (ext->mask) {
630 i_img_dim w = r - l;
631 i_img_dim i = 0;
632 i_img_dim x = ext->xbase + l;
633 i_img_dim work_y = y + ext->ybase;
634 i_sample_t *mask_samps = ext->samps;
635
636 i_gsamp(ext->mask, l, r, y, mask_samps, NULL, 1);
637 /* not optimizing this yet */
638 while (i < w) {
639 if (mask_samps[i]) {
640 /* found a set mask value, try to do a run */
641 i_img_dim run_left = x;
642 const i_fsample_t *run_samps = samples;
643 ++i;
644 ++x;
645 samples += chan_count;
646
647 while (i < w && mask_samps[i]) {
648 ++i;
649 ++x;
650 samples += chan_count;
651 }
652 result += i_psampf(ext->targ, run_left, x, work_y, run_samps, chans, chan_count);
653 }
654 else {
655 ++i;
656 ++x;
657 samples += chan_count;
658 result += chan_count; /* pretend we wrote masked off pixels */
659 }
660 }
661 }
662 else {
663 result = i_psampf(ext->targ, l + ext->xbase, r + ext->xbase,
664 y + ext->ybase, samples,
665 chans, chan_count);
666 im->type = ext->targ->type;
667 }
668 ext->targ->ch_mask = old_ch_mask;
669 return result;
670 }
671 else {
672 dIMCTXim(im);
673 i_push_error(0, "Image position outside of image");
674 return -1;
675 }
676 }
677
678
679 /*
680 =back
681
682 =head1 AUTHOR
683
684 Tony Cook <[email protected]>
685
686 =head1 SEE ALSO
687
688 Imager(3)
689
690 =cut
691 */