Codebase list libimager-perl / debian/0.70-1 maskimg.c
debian/0.70-1

Tree @debian/0.70-1 (Download .tar.gz)

maskimg.c @debian/0.70-1raw · history · blame

  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
/*
=head1 NAME

maskimg.c - implements masked images/image subsets

=head1 SYNOPSIS

=head1 DESCRIPTION

=over
=cut
*/

#include "imager.h"
#include "imageri.h"

#include <stdio.h>
/*
=item i_img_mask_ext

A pointer to this type of object is kept in the ext_data of a masked 
image.

=cut
*/

typedef struct {
  i_img *targ;
  i_img *mask;
  int xbase, ybase;
  i_sample_t *samps; /* temp space */
} i_img_mask_ext;

#define MASKEXT(im) ((i_img_mask_ext *)((im)->ext_data))

static void i_destroy_masked(i_img *im);
static int i_ppix_masked(i_img *im, int x, int y, const i_color *pix);
static int i_ppixf_masked(i_img *im, int x, int y, const i_fcolor *pix);
static int i_plin_masked(i_img *im, int l, int r, int y, const i_color *vals);
static int i_plinf_masked(i_img *im, int l, int r, int y, const i_fcolor *vals);
static int i_gpix_masked(i_img *im, int x, int y, i_color *pix);
static int i_gpixf_masked(i_img *im, int x, int y, i_fcolor *pix);
static int i_glin_masked(i_img *im, int l, int r, int y, i_color *vals);
static int i_glinf_masked(i_img *im, int l, int r, int y, i_fcolor *vals);
static int i_gsamp_masked(i_img *im, int l, int r, int y, i_sample_t *samp, 
                          int const *chans, int chan_count);
static int i_gsampf_masked(i_img *im, int l, int r, int y, i_fsample_t *samp, 
                           int const *chans, int chan_count);
static int i_gpal_masked(i_img *im, int l, int r, int y, i_palidx *vals);
static int i_ppal_masked(i_img *im, int l, int r, int y, const i_palidx *vals);

/*
=item IIM_base_masked

The basic data we copy into a masked image.

=cut
*/
static i_img IIM_base_masked =
{
  0, /* channels set */
  0, 0, 0, /* xsize, ysize, bytes */
  ~0U, /* ch_mask */
  i_8_bits, /* bits */
  i_palette_type, /* type */
  1, /* virtual */
  NULL, /* idata */
  { 0, 0, NULL }, /* tags */
  NULL, /* ext_data */

  i_ppix_masked, /* i_f_ppix */
  i_ppixf_masked, /* i_f_ppixf */
  i_plin_masked, /* i_f_plin */
  i_plinf_masked, /* i_f_plinf */
  i_gpix_masked, /* i_f_gpix */
  i_gpixf_masked, /* i_f_gpixf */
  i_glin_masked, /* i_f_glin */
  i_glinf_masked, /* i_f_glinf */
  i_gsamp_masked, /* i_f_gsamp */
  i_gsampf_masked, /* i_f_gsampf */

  i_gpal_masked, /* i_f_gpal */
  i_ppal_masked, /* i_f_ppal */
  i_addcolors_forward, /* i_f_addcolors */
  i_getcolors_forward, /* i_f_getcolors */
  i_colorcount_forward, /* i_f_colorcount */
  i_maxcolors_forward, /* i_f_maxcolors */
  i_findcolor_forward, /* i_f_findcolor */
  i_setcolors_forward, /* i_f_setcolors */

  i_destroy_masked, /* i_f_destroy */
};

/*
=item i_img_masked_new(i_img *targ, i_img *mask, int xbase, int ybase, int w, int h)

Create a new masked image.

The image mask is optional, in which case the image is just a view of
a rectangular portion of the image.

The mask only has an effect of writing to the image, the entire view
of the underlying image is readable.

pixel access to mimg(x,y) is translated to targ(x+xbase, y+ybase), as long 
as (0 <= x < w) and (0 <= y < h).

For a pixel to be writable, the pixel mask(x,y) must have non-zero in
it's first channel.  No scaling of the pixel is done, the channel 
sample is treated as boolean.

=cut
*/

i_img *i_img_masked_new(i_img *targ, i_img *mask, int x, int y, int w, int h) {
  i_img *im;
  i_img_mask_ext *ext;

  i_clear_error();
  if (x >= targ->xsize || y >= targ->ysize) {
    i_push_error(0, "subset outside of target image");
    return NULL;
  }
  if (mask) {
    if (w > mask->xsize)
      w = mask->xsize;
    if (h > mask->ysize)
      h = mask->ysize;
  }
  if (x+w > targ->xsize)
    w = targ->xsize - x;
  if (y+h > targ->ysize)
    h = targ->ysize - y;

  im = mymalloc(sizeof(i_img));
  memcpy(im, &IIM_base_masked, sizeof(i_img));
  im->xsize = w;
  im->ysize = h;
  im->channels = targ->channels;
  im->bits = targ->bits;
  im->type = targ->type;
  ext = mymalloc(sizeof(*ext));
  ext->targ = targ;
  ext->mask = mask;
  ext->xbase = x;
  ext->ybase = y;
  ext->samps = mymalloc(sizeof(i_sample_t) * im->xsize);
  im->ext_data = ext;

  return im;
}

/*
=item i_destroy_masked(i_img *im)

The destruction handler for masked images.

Releases the ext_data.

Internal function.

=cut
*/

static void i_destroy_masked(i_img *im) {
  myfree(MASKEXT(im)->samps);
  myfree(im->ext_data);
}

/*
=item i_ppix_masked(i_img *im, int x, int y, const i_color *pix)

Write a pixel to a masked image.

Internal function.

=cut
*/
static int i_ppix_masked(i_img *im, int x, int y, const i_color *pix) {
  i_img_mask_ext *ext = MASKEXT(im);
  int result;

  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
    return -1;
  if (ext->mask) {
    i_sample_t samp;
    
    if (i_gsamp(ext->mask, x, x+1, y, &samp, NULL, 1) && !samp)
      return 0; /* pretend it was good */
  }
  result = i_ppix(ext->targ, x + ext->xbase, y + ext->ybase, pix);
  im->type = ext->targ->type;
  return result;
}

/*
=item i_ppixf_masked(i_img *im, int x, int y, const i_fcolor *pix)

Write a pixel to a masked image.

Internal function.

=cut
*/
static int i_ppixf_masked(i_img *im, int x, int y, const i_fcolor *pix) {
  i_img_mask_ext *ext = MASKEXT(im);
  int result;

  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
    return -1;
  if (ext->mask) {
    i_sample_t samp;
    
    if (i_gsamp(ext->mask, x, x+1, y, &samp, NULL, 1) && !samp)
      return 0; /* pretend it was good */
  }
  result = i_ppixf(ext->targ, x + ext->xbase, y + ext->ybase, pix);
  im->type = ext->targ->type;
  return result;
}

/*
=item i_plin_masked(i_img *im, int l, int r, int y, const i_color *vals)

Write a row of data to a masked image.

Internal function.

=cut
*/
static int i_plin_masked(i_img *im, int l, int r, int y, const i_color *vals) {
  i_img_mask_ext *ext = MASKEXT(im);

  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    if (ext->mask) {
      int i;
      int simple = 0;
      i_sample_t *samps = ext->samps;
      int w = r - l;

      i_gsamp(ext->mask, l, r, y, samps, NULL, 1);
      if (w < 10)
        simple = 0;
      else {
        /* the idea is to make a fast scan to see how often the state
           changes */
        int changes = 0;
        for (i = 0; i < w-1; ++i)
          if (!samps[i] != !samps[i+1])
            ++changes;
        if (changes > w/3) /* just rough */
          simple = 1;
      }
      if (simple) {
        /* we'd be calling a usually more complicated i_plin function
           almost as often as the usually simple i_ppix(), so just
           do a simple scan
        */
        for (i = 0; i < w; ++i) {
          if (samps[i])
            i_ppix(ext->targ, l + i + ext->xbase, y + ext->ybase, vals + i);
        }
        im->type = ext->targ->type;
        return r-l;
      }
      else {
        /* the scan above indicates there should be some contiguous 
           regions, look for them and render
        */
        int start;
        i = 0;
        while (i < w) {
          while (i < w && !samps[i])
            ++i;
          start = i;
          while (i < w && samps[i])
            ++i;
          if (i != start)
            i_plin(ext->targ, l + start + ext->xbase, l + i + ext->xbase, 
                   y + ext->ybase, vals + start);
        }
        im->type = ext->targ->type;
        return w;
      }
    }
    else {
      int result = i_plin(ext->targ, l + ext->xbase, r + ext->xbase, 
                          y + ext->ybase, vals);
      im->type = ext->targ->type;
      return result;
    }
  }
  else {
    return 0;
  }
}

/*
=item i_plinf_masked(i_img *im, int l, int r, int y, const i_fcolor *vals)

Write a row of data to a masked image.

Internal function.

=cut
*/
static int i_plinf_masked(i_img *im, int l, int r, int y, const i_fcolor *vals) {
  i_img_mask_ext *ext = MASKEXT(im);
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    if (ext->mask) {
      int i;
      int simple = 0;
      i_sample_t *samps = ext->samps;
      int w = r - l;

      i_gsamp(ext->mask, l, r, y, samps, NULL, 1);
      if (w < 10)
        simple = 0;
      else {
        /* the idea is to make a fast scan to see how often the state
           changes */
        int changes = 0;
        for (i = 0; i < w-1; ++i)
          if (!samps[i] != !samps[i+1])
            ++changes;
        if (changes > w/3) /* just rough */
          simple = 1;
      }
      if (simple) {
        /* we'd be calling a usually more complicated i_plin function
           almost as often as the usually simple i_ppix(), so just
           do a simple scan
        */
        for (i = 0; i < w; ++i) {
          if (samps[i])
            i_ppixf(ext->targ, l + i + ext->xbase, y + ext->ybase, vals+i);
        }
        im->type = ext->targ->type;
        return r-l;
      }
      else {
        /* the scan above indicates there should be some contiguous 
           regions, look for them and render
        */
        int start;
        i = 0;
        while (i < w) {
          while (i < w && !samps[i])
            ++i;
          start = i;
          while (i < w && samps[i])
            ++i;
          if (i != start)
            i_plinf(ext->targ, l + start + ext->xbase, l + i + ext->xbase, 
                    y + ext->ybase, vals + start);
        }
        im->type = ext->targ->type;
        return w;
      }
    }
    else {
      int result = i_plinf(ext->targ, l + ext->xbase, r + ext->xbase, 
                           y + ext->ybase, vals);
      im->type = ext->targ->type;
      return result;
    }
  }
  else {
    return 0;
  }
}

/*
=item i_gpix_masked(i_img *im, int x, int y, i_color *pix)

Read a pixel from a masked image.

Internal.

=cut
*/
static int i_gpix_masked(i_img *im, int x, int y, i_color *pix) {
  i_img_mask_ext *ext = MASKEXT(im);

  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
    return -1;

  return i_gpix(ext->targ, x + ext->xbase, y + ext->ybase, pix);
}

/*
=item i_gpixf_masked(i_img *im, int x, int y, i_fcolor *pix)

Read a pixel from a masked image.

Internal.

=cut
*/
static int i_gpixf_masked(i_img *im, int x, int y, i_fcolor *pix) {
  i_img_mask_ext *ext = MASKEXT(im);

  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
    return -1;

  return i_gpixf(ext->targ, x + ext->xbase, y + ext->ybase, pix);
}

static int i_glin_masked(i_img *im, int l, int r, int y, i_color *vals) {
  i_img_mask_ext *ext = MASKEXT(im);
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    return i_glin(ext->targ, l + ext->xbase, r + ext->xbase, 
                  y + ext->ybase, vals);
  }
  else {
    return 0;
  }
}

static int i_glinf_masked(i_img *im, int l, int r, int y, i_fcolor *vals) {
  i_img_mask_ext *ext = MASKEXT(im);
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    return i_glinf(ext->targ, l + ext->xbase, r + ext->xbase, 
                  y + ext->ybase, vals);
  }
  else {
    return 0;
  }
}

static int i_gsamp_masked(i_img *im, int l, int r, int y, i_sample_t *samp, 
                          int const *chans, int chan_count) {
  i_img_mask_ext *ext = MASKEXT(im);
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    return i_gsamp(ext->targ, l + ext->xbase, r + ext->xbase, 
                  y + ext->ybase, samp, chans, chan_count);
  }
  else {
    return 0;
  }
}

static int i_gsampf_masked(i_img *im, int l, int r, int y, i_fsample_t *samp, 
                          int const *chans, int chan_count) {
  i_img_mask_ext *ext = MASKEXT(im);
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    return i_gsampf(ext->targ, l + ext->xbase, r + ext->xbase, 
                    y + ext->ybase, samp, chans, chan_count);
  }
  else {
    return 0;
  }
}

static int i_gpal_masked(i_img *im, int l, int r, int y, i_palidx *vals) {
  i_img_mask_ext *ext = MASKEXT(im);
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    return i_gpal(ext->targ, l + ext->xbase, r + ext->xbase, 
                  y + ext->ybase, vals);
  }
  else {
    return 0;
  }
}

static int i_ppal_masked(i_img *im, int l, int r, int y, const i_palidx *vals) {
  i_img_mask_ext *ext = MASKEXT(im);
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    if (ext->mask) {
      int i;
      i_sample_t *samps = ext->samps;
      int w = r - l;
      int start;
      
      i = 0;
      while (i < w) {
        while (i < w && !samps[i])
          ++i;
        start = i;
        while (i < w && samps[i])
          ++i;
        if (i != start)
          i_ppal(ext->targ, l+start+ext->xbase, l+i+ext->xbase, 
                 y+ext->ybase, vals+start);
      }
      return w;
    }
    else {
      return i_ppal(ext->targ, l + ext->xbase, r + ext->xbase, 
                    y + ext->ybase, vals);
    }
  }
  else {
    return 0;
  }
}


/*
=back

=head1 AUTHOR

Tony Cook <tony@develop-help.com>

=head1 SEE ALSO

Imager(3)

=cut
*/