Debian Codegraph @

fills.c

raw
1 #define IMAGER_NO_CONTEXT
2 #include "imager.h"
3 #include "imageri.h"
4 #include <assert.h>
5
6 /*
7 =head1 NAME
8
9 fills.c - implements the basic general fills
10
11 =head1 SYNOPSIS
12
13 i_fill_t *fill;
14 i_color c1, c2;
15 i_fcolor fc1, fc2;
16 int combine;
17 fill = i_new_fill_solidf(&fc1, combine);
18 fill = i_new_fill_solid(&c1, combine);
19 fill = i_new_fill_hatchf(&fc1, &fc2, combine, hatch, cust_hash, dx, dy);
20 fill = i_new_fill_hatch(&c1, &c2, combine, hatch, cust_hash, dx, dy);
21 fill = i_new_fill_image(im, matrix, xoff, yoff, combine);
22 fill = i_new_fill_opacity(fill, alpha_mult);
23 i_fill_destroy(fill);
24
25 =head1 DESCRIPTION
26
27 Implements the basic general fills, which can be used for filling some
28 shapes and for flood fills.
29
30 Each fill can implement up to 3 functions:
31
32 =over
33
34 =item fill_with_color
35
36 called for fills on 8-bit images. This can be NULL in which case the
37 fill_with_colorf function is called.
38
39 =item fill_with_fcolor
40
41 called for fills on non-8-bit images or when fill_with_color is NULL.
42
43 =item destroy
44
45 called by i_fill_destroy() if non-NULL, to release any extra resources
46 that the fill may need.
47
48 =back
49
50 fill_with_color and fill_with_fcolor are basically the same function
51 except that the first works with lines of i_color and the second with
52 lines of i_fcolor.
53
54 If the combines member if non-zero the line data is populated from the
55 target image before calling fill_with_*color.
56
57 fill_with_color needs to fill the I<data> parameter with the fill
58 pixels. If combines is non-zero it the fill pixels should be combined
59 with the existing data.
60
61 The current fills are:
62
63 =over
64
65 =item *
66
67 solid fill
68
69 =item *
70
71 hatched fill
72
73 =item *
74
75 fountain fill
76
77 =back
78
79 Fountain fill is implemented by L<filters.c>.
80
81 Other fills that could be implemented include:
82
83 =over
84
85 =item *
86
87 image - an image tiled over the fill area, with an offset either
88 horizontally or vertically.
89
90 =item *
91
92 checkerboard - combine 2 fills in a checkerboard
93
94 =item *
95
96 combine - combine the levels of 2 other fills based in the levels of
97 an image
98
99 =item *
100
101 regmach - use the register machine to generate colors
102
103 =back
104
105 =over
106
107 =cut
108 */
109
110 static i_color fcolor_to_color(const i_fcolor *c) {
111 int ch;
112 i_color out;
113
114 for (ch = 0; ch < MAXCHANNELS; ++ch)
115 out.channel[ch] = SampleFTo8(c->channel[ch]);
116
117 return out;
118 }
119
120 static i_fcolor color_to_fcolor(const i_color *c) {
121 int ch;
122 i_fcolor out;
123
124 for (ch = 0; ch < MAXCHANNELS; ++ch)
125 out.channel[ch] = Sample8ToF(c->channel[ch]);
126
127 return out;
128 }
129
130 /* alpha combine in with out */
131 #define COMBINE(out, in, channels) \
132 { \
133 int ch; \
134 for (ch = 0; ch < (channels); ++ch) { \
135 (out).channel[ch] = ((out).channel[ch] * (255 - (in).channel[3]) \
136 + (in).channel[ch] * (in).channel[3]) / 255; \
137 } \
138 }
139
140 /* alpha combine in with out, in this case in is a simple array of
141 samples, potentially not integers - the mult combiner uses doubles
142 for accuracy */
143 #define COMBINEA(out, in, channels) \
144 { \
145 int ch; \
146 for (ch = 0; ch < (channels); ++ch) { \
147 (out).channel[ch] = ((out).channel[ch] * (255 - (in)[3]) \
148 + (in)[ch] * (in)[3]) / 255; \
149 } \
150 }
151
152 #define COMBINEF(out, in, channels) \
153 { \
154 int ch; \
155 for (ch = 0; ch < (channels); ++ch) { \
156 (out).channel[ch] = (out).channel[ch] * (1.0 - (in).channel[3]) \
157 + (in).channel[ch] * (in).channel[3]; \
158 } \
159 }
160
161 typedef struct
162 {
163 i_fill_t base;
164 i_color c;
165 i_fcolor fc;
166 } i_fill_solid_t;
167
168 static void fill_solid(i_fill_t *, i_img_dim x, i_img_dim y, i_img_dim width,
169 int channels, i_color *);
170 static void fill_solidf(i_fill_t *, i_img_dim x, i_img_dim y, i_img_dim width,
171 int channels, i_fcolor *);
172
173 static i_fill_t base_solid_fill =
174 {
175 fill_solid,
176 fill_solidf,
177 NULL,
178 NULL,
179 NULL,
180 };
181
182 /*
183 =item i_fill_destroy(fill)
184 =order 90
185 =category Fills
186 =synopsis i_fill_destroy(fill);
187
188 Call to destroy any fill object.
189
190 =cut
191 */
192
193 void
194 i_fill_destroy(i_fill_t *fill) {
195 if (fill->destroy)
196 (fill->destroy)(fill);
197 myfree(fill);
198 }
199
200 /*
201 =item i_new_fill_solidf(color, combine)
202
203 =category Fills
204 =synopsis i_fill_t *fill = i_new_fill_solidf(&fcolor, combine);
205
206 Create a solid fill based on a float color.
207
208 If combine is non-zero then alpha values will be combined.
209
210 =cut
211 */
212
213 i_fill_t *
214 i_new_fill_solidf(const i_fcolor *c, int combine) {
215 int ch;
216 i_fill_solid_t *fill = mymalloc(sizeof(i_fill_solid_t)); /* checked 14jul05 tonyc */
217 memset(fill, 0, sizeof(*fill));
218
219 fill->base = base_solid_fill;
220 if (combine) {
221 i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
222 }
223
224 fill->fc = *c;
225 for (ch = 0; ch < MAXCHANNELS; ++ch) {
226 fill->c.channel[ch] = SampleFTo8(c->channel[ch]);
227 }
228
229 return &fill->base;
230 }
231
232 /*
233 =item i_new_fill_solid(color, combine)
234
235 =category Fills
236 =synopsis i_fill_t *fill = i_new_fill_solid(&color, combine);
237
238 Create a solid fill based on an 8-bit color.
239
240 If combine is non-zero then alpha values will be combined.
241
242 =cut
243 */
244
245 i_fill_t *
246 i_new_fill_solid(const i_color *c, int combine) {
247 int ch;
248 i_fill_solid_t *fill = mymalloc(sizeof(i_fill_solid_t)); /* checked 14jul05 tonyc */
249
250 memset(fill, 0, sizeof(*fill));
251 fill->base = base_solid_fill;
252 if (combine) {
253 i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
254 }
255
256 fill->c = *c;
257 for (ch = 0; ch < MAXCHANNELS; ++ch) {
258 fill->fc.channel[ch] = Sample8ToF(c->channel[ch]);
259 }
260
261 return &fill->base;
262 }
263
264 static unsigned char
265 builtin_hatches[][8] =
266 {
267 {
268 /* 1x1 checkerboard */
269 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55,
270 },
271 {
272 /* 2x2 checkerboard */
273 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33,
274 },
275 {
276 /* 4 x 4 checkerboard */
277 0xF0, 0xF0, 0xF0, 0xF0, 0x0F, 0x0F, 0x0F, 0x0F,
278 },
279 {
280 /* single vertical lines */
281 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
282 },
283 {
284 /* double vertical lines */
285 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
286 },
287 {
288 /* quad vertical lines */
289 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
290 },
291 {
292 /* single hlines */
293 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294 },
295 {
296 /* double hlines */
297 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
298 },
299 {
300 /* quad hlines */
301 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
302 },
303 {
304 /* single / */
305 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
306 },
307 {
308 /* single \ */
309 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01,
310 },
311 {
312 /* double / */
313 0x11, 0x22, 0x44, 0x88, 0x11, 0x22, 0x44, 0x88,
314 },
315 {
316 /* double \ */
317 0x88, 0x44, 0x22, 0x11, 0x88, 0x44, 0x22, 0x11,
318 },
319 {
320 /* single grid */
321 0xFF, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
322 },
323 {
324 /* double grid */
325 0xFF, 0x88, 0x88, 0x88, 0xFF, 0x88, 0x88, 0x88,
326 },
327 {
328 /* quad grid */
329 0xFF, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA,
330 },
331 {
332 /* single dots */
333 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
334 },
335 {
336 /* 4 dots */
337 0x88, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00,
338 },
339 {
340 /* 16 dots */
341 0xAA, 0x00, 0xAA, 0x00, 0xAA, 0x00, 0xAA, 0x00,
342 },
343 {
344 /* simple stipple */
345 0x48, 0x84, 0x00, 0x00, 0x84, 0x48, 0x00, 0x00,
346 },
347 {
348 /* weave */
349 0x55, 0xFD, 0x05, 0xFD, 0x55, 0xDF, 0x50, 0xDF,
350 },
351 {
352 /* single cross hatch */
353 0x82, 0x44, 0x28, 0x10, 0x28, 0x44, 0x82, 0x01,
354 },
355 {
356 /* double cross hatch */
357 0xAA, 0x44, 0xAA, 0x11, 0xAA, 0x44, 0xAA, 0x11,
358 },
359 {
360 /* vertical lozenge */
361 0x11, 0x11, 0x11, 0xAA, 0x44, 0x44, 0x44, 0xAA,
362 },
363 {
364 /* horizontal lozenge */
365 0x88, 0x70, 0x88, 0x07, 0x88, 0x70, 0x88, 0x07,
366 },
367 {
368 /* scales overlapping downwards */
369 0x80, 0x80, 0x41, 0x3E, 0x08, 0x08, 0x14, 0xE3,
370 },
371 {
372 /* scales overlapping upwards */
373 0xC7, 0x28, 0x10, 0x10, 0x7C, 0x82, 0x01, 0x01,
374 },
375 {
376 /* scales overlapping leftwards */
377 0x83, 0x84, 0x88, 0x48, 0x38, 0x48, 0x88, 0x84,
378 },
379 {
380 /* scales overlapping rightwards */
381 0x21, 0x11, 0x12, 0x1C, 0x12, 0x11, 0x21, 0xC1,
382 },
383 {
384 /* denser stipple */
385 0x44, 0x88, 0x22, 0x11, 0x44, 0x88, 0x22, 0x11,
386 },
387 {
388 /* L-shaped tiles */
389 0xFF, 0x84, 0x84, 0x9C, 0x94, 0x9C, 0x90, 0x90,
390 },
391 {
392 /* wider stipple */
393 0x80, 0x40, 0x20, 0x00, 0x02, 0x04, 0x08, 0x00,
394 },
395 };
396
397 typedef struct
398 {
399 i_fill_t base;
400 i_color fg, bg;
401 i_fcolor ffg, fbg;
402 unsigned char hatch[8];
403 i_img_dim dx, dy;
404 } i_fill_hatch_t;
405
406 static void fill_hatch(i_fill_t *fill, i_img_dim x, i_img_dim y,
407 i_img_dim width, int channels, i_color *data);
408 static void fill_hatchf(i_fill_t *fill, i_img_dim x, i_img_dim y,
409 i_img_dim width, int channels, i_fcolor *data);
410 static
411 i_fill_t *
412 i_new_hatch_low(const i_color *fg, const i_color *bg, const i_fcolor *ffg, const i_fcolor *fbg,
413 int combine, int hatch, const unsigned char *cust_hatch,
414 i_img_dim dx, i_img_dim dy);
415
416 /*
417 =item i_new_fill_hatch(C<fg>, C<bg>, C<combine>, C<hatch>, C<cust_hatch>, C<dx>, C<dy>)
418
419 =category Fills
420 =synopsis i_fill_t *fill = i_new_fill_hatch(&fg_color, &bg_color, combine, hatch, custom_hatch, dx, dy);
421
422 Creates a new hatched fill with the C<fg> color used for the 1 bits in
423 the hatch and C<bg> for the 0 bits. If C<combine> is non-zero alpha
424 values will be combined.
425
426 If C<cust_hatch> is non-NULL it should be a pointer to 8 bytes of the
427 hash definition, with the high-bits to the left.
428
429 If C<cust_hatch> is NULL then one of the standard hatches is used.
430
431 (C<dx>, C<dy>) are an offset into the hatch which can be used to hatch
432 adjoining areas out of alignment, or to align the origin of a hatch
433 with the side of a filled area.
434
435 =cut
436 */
437 i_fill_t *
438 i_new_fill_hatch(const i_color *fg, const i_color *bg, int combine, int hatch,
439 const unsigned char *cust_hatch, i_img_dim dx, i_img_dim dy) {
440 assert(fg);
441 assert(bg);
442 return i_new_hatch_low(fg, bg, NULL, NULL, combine, hatch, cust_hatch,
443 dx, dy);
444 }
445
446 /*
447 =item i_new_fill_hatchf(C<fg>, C<bg>, C<combine>, C<hatch>, C<cust_hatch>, C<dx>, C<dy>)
448
449 =category Fills
450 =synopsis i_fill_t *fill = i_new_fill_hatchf(&fg_fcolor, &bg_fcolor, combine, hatch, custom_hatch, dx, dy);
451
452 Creates a new hatched fill with the C<fg> color used for the 1 bits in
453 the hatch and C<bg> for the 0 bits. If C<combine> is non-zero alpha
454 values will be combined.
455
456 If C<cust_hatch> is non-NULL it should be a pointer to 8 bytes of the
457 hash definition, with the high-bits to the left.
458
459 If C<cust_hatch> is NULL then one of the standard hatches is used.
460
461 (C<dx>, C<dy>) are an offset into the hatch which can be used to hatch
462 adjoining areas out of alignment, or to align the origin of a hatch
463 with the side of a filled area.
464
465 =cut
466 */
467 i_fill_t *
468 i_new_fill_hatchf(const i_fcolor *fg, const i_fcolor *bg, int combine, int hatch,
469 const unsigned char *cust_hatch, i_img_dim dx, i_img_dim dy) {
470 assert(fg);
471 assert(bg);
472 return i_new_hatch_low(NULL, NULL, fg, bg, combine, hatch, cust_hatch,
473 dx, dy);
474 }
475
476 static void fill_image(i_fill_t *fill, i_img_dim x, i_img_dim y,
477 i_img_dim width, int channels, i_color *data);
478 static void fill_imagef(i_fill_t *fill, i_img_dim x, i_img_dim y,
479 i_img_dim width, int channels, i_fcolor *data);
480 struct i_fill_image_t {
481 i_fill_t base;
482 i_img *src;
483 i_img_dim xoff, yoff;
484 int has_matrix;
485 double matrix[9];
486 };
487
488 static i_fill_t
489 image_fill_proto = {
490 fill_image,
491 fill_imagef,
492 NULL,
493 NULL,
494 NULL
495 };
496
497 /*
498 =item i_new_fill_image(C<im>, C<matrix>, C<xoff>, C<yoff>, C<combine>)
499
500 =category Fills
501 =synopsis i_fill_t *fill = i_new_fill_image(src_img, matrix, x_offset, y_offset, combine);
502
503 Create an image based fill.
504
505 matrix is an array of 9 doubles representing a transformation matrix.
506
507 C<xoff> and C<yoff> are the offset into the image to start filling from.
508
509 =cut
510 */
511 i_fill_t *
512 i_new_fill_image(i_img *im, const double *matrix, i_img_dim xoff, i_img_dim yoff, int combine) {
513 struct i_fill_image_t *fill = mymalloc(sizeof(*fill)); /* checked 14jul05 tonyc */
514
515 memset(fill, 0, sizeof(*fill));
516 fill->base = image_fill_proto;
517
518 if (combine) {
519 i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
520 }
521 else {
522 fill->base.combine = NULL;
523 fill->base.combinef = NULL;
524 }
525 fill->src = im;
526 if (xoff < 0)
527 xoff += im->xsize;
528 fill->xoff = xoff;
529 if (yoff < 0)
530 yoff += im->ysize;
531 fill->yoff = yoff;
532 if (matrix) {
533 fill->has_matrix = 1;
534 memcpy(fill->matrix, matrix, sizeof(fill->matrix));
535 }
536 else
537 fill->has_matrix = 0;
538
539 return &fill->base;
540 }
541
542 static void fill_opacity(i_fill_t *fill, i_img_dim x, i_img_dim y,
543 i_img_dim width, int channels, i_color *data);
544 static void fill_opacityf(i_fill_t *fill, i_img_dim x, i_img_dim y,
545 i_img_dim width, int channels, i_fcolor *data);
546
547 struct i_fill_opacity_t {
548 i_fill_t base;
549 i_fill_t *other_fill;
550 double alpha_mult;
551 };
552
553 static i_fill_t
554 opacity_fill_proto = {
555 fill_opacity,
556 fill_opacityf,
557 NULL,
558 NULL,
559 NULL
560 };
561
562 i_fill_t *
563 i_new_fill_opacity(i_fill_t *base_fill, double alpha_mult) {
564 struct i_fill_opacity_t *fill = mymalloc(sizeof(*fill));
565
566 memset(fill, 0, sizeof(*fill));
567 fill->base = opacity_fill_proto;
568
569 fill->base.combine = base_fill->combine;
570 fill->base.combinef = base_fill->combinef;
571
572 fill->other_fill = base_fill;
573 fill->alpha_mult = alpha_mult;
574
575 if (!base_fill->f_fill_with_color) {
576 /* base fill only does floating, so we only do that too */
577 fill->base.f_fill_with_color = NULL;
578 }
579
580 return &fill->base;
581 }
582
583 #define T_SOLID_FILL(fill) ((i_fill_solid_t *)(fill))
584
585 /*
586 =back
587
588 =head1 INTERNAL FUNCTIONS
589
590 =over
591
592 =item fill_solid(fill, x, y, width, channels, data)
593
594 The 8-bit sample fill function for non-combining solid fills.
595
596 =cut
597 */
598 static void
599 fill_solid(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
600 int channels, i_color *data) {
601 i_color c = T_SOLID_FILL(fill)->c;
602
603 (void)x;
604 (void)y;
605
606 i_adapt_colors(channels > 2 ? 4 : 2, 4, &c, 1);
607 while (width-- > 0) {
608 *data++ = c;
609 }
610 }
611
612 /*
613 =item fill_solid(fill, x, y, width, channels, data)
614
615 The floating sample fill function for non-combining solid fills.
616
617 =cut
618 */
619 static void
620 fill_solidf(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
621 int channels, i_fcolor *data) {
622 i_fcolor c = T_SOLID_FILL(fill)->fc;
623
624 (void)x;
625 (void)y;
626
627 i_adapt_fcolors(channels > 2 ? 4 : 2, 4, &c, 1);
628 while (width-- > 0) {
629 *data++ = c;
630 }
631 }
632
633 static i_fill_t
634 hatch_fill_proto = {
635 fill_hatch,
636 fill_hatchf,
637 NULL,
638 NULL,
639 NULL
640 };
641
642 /*
643 =item i_new_hatch_low(fg, bg, ffg, fbg, combine, hatch, cust_hatch, dx, dy)
644
645 Implements creation of hatch fill objects.
646
647 =cut
648 */
649 static
650 i_fill_t *
651 i_new_hatch_low(const i_color *fg, const i_color *bg,
652 const i_fcolor *ffg, const i_fcolor *fbg,
653 int combine, int hatch, const unsigned char *cust_hatch,
654 i_img_dim dx, i_img_dim dy) {
655 i_fill_hatch_t *fill = mymalloc(sizeof(i_fill_hatch_t)); /* checked 14jul05 tonyc */
656
657 memset(fill, 0, sizeof(*fill));
658 fill->base = hatch_fill_proto;
659 if (fg && bg) {
660 fill->fg = *fg;
661 fill->bg = *bg;
662 fill->ffg = color_to_fcolor(fg);
663 fill->fbg = color_to_fcolor(bg);
664 }
665 else if (ffg && fbg) {
666 fill->fg = fcolor_to_color(ffg);
667 fill->bg = fcolor_to_color(fbg);
668 fill->ffg = *ffg;
669 fill->fbg = *fbg;
670 }
671 else {
672 assert(0);
673 /* NOTREACHED */
674 }
675
676 if (combine) {
677 i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
678 }
679 else {
680 fill->base.combine = NULL;
681 fill->base.combinef = NULL;
682 }
683 if (cust_hatch) {
684 memcpy(fill->hatch, cust_hatch, 8);
685 }
686 else {
687 if (hatch < 0
688 || (size_t)hatch >= sizeof(builtin_hatches)/sizeof(*builtin_hatches)) {
689 hatch = 0;
690 }
691 memcpy(fill->hatch, builtin_hatches[hatch], 8);
692 }
693 fill->dx = dx & 7;
694 fill->dy = dy & 7;
695
696 return &fill->base;
697 }
698
699 /*
700 =item fill_hatch(fill, x, y, width, channels, data)
701
702 The 8-bit sample fill function for hatched fills.
703
704 =cut
705 */
706 static void
707 fill_hatch(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
708 int channels, i_color *data) {
709 i_fill_hatch_t *f = (i_fill_hatch_t *)fill;
710 int byte = f->hatch[(y + f->dy) & 7];
711 int xpos = (x + f->dx) & 7;
712 int mask = 128 >> xpos;
713 i_color fg = f->fg;
714 i_color bg = f->bg;
715
716 if (channels < 3) {
717 i_adapt_colors(2, 4, &fg, 1);
718 i_adapt_colors(2, 4, &bg, 1);
719 }
720
721 while (width-- > 0) {
722 if (byte & mask)
723 *data++ = fg;
724 else
725 *data++ = bg;
726
727 if ((mask >>= 1) == 0)
728 mask = 128;
729 }
730 }
731
732 /*
733 =item fill_hatchf(fill, x, y, width, channels, data)
734
735 The floating sample fill function for hatched fills.
736
737 =cut
738 */
739 static void
740 fill_hatchf(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
741 int channels, i_fcolor *data) {
742 i_fill_hatch_t *f = (i_fill_hatch_t *)fill;
743 int byte = f->hatch[(y + f->dy) & 7];
744 int xpos = (x + f->dx) & 7;
745 int mask = 128 >> xpos;
746 i_fcolor fg = f->ffg;
747 i_fcolor bg = f->fbg;
748
749 if (channels < 3) {
750 i_adapt_fcolors(2, 4, &fg, 1);
751 i_adapt_fcolors(2, 4, &bg, 1);
752 }
753
754 while (width-- > 0) {
755 if (byte & mask)
756 *data++ = fg;
757 else
758 *data++ = bg;
759
760 if ((mask >>= 1) == 0)
761 mask = 128;
762 }
763 }
764
765 /* hopefully this will be inlined (it is with -O3 with gcc 2.95.4) */
766 /* linear interpolation */
767 static i_color interp_i_color(i_color before, i_color after, double pos,
768 int channels) {
769 i_color out;
770 int ch;
771
772 pos -= floor(pos);
773 for (ch = 0; ch < channels; ++ch)
774 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
775 if (channels > 3 && out.channel[3]) {
776 for (ch = 0; ch < channels; ++ch) {
777 if (ch != 3) {
778 int temp = out.channel[ch] * 255 / out.channel[3];
779 if (temp > 255)
780 temp = 255;
781 out.channel[ch] = temp;
782 }
783 }
784 }
785
786 return out;
787 }
788
789 /* hopefully this will be inlined (it is with -O3 with gcc 2.95.4) */
790 /* linear interpolation */
791 static i_fcolor interp_i_fcolor(i_fcolor before, i_fcolor after, double pos,
792 int channels) {
793 i_fcolor out;
794 int ch;
795
796 pos -= floor(pos);
797 for (ch = 0; ch < channels; ++ch)
798 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
799 if (out.channel[3]) {
800 for (ch = 0; ch < channels; ++ch) {
801 if (ch != 3) {
802 int temp = out.channel[ch] / out.channel[3];
803 if (temp > 1.0)
804 temp = 1.0;
805 out.channel[ch] = temp;
806 }
807 }
808 }
809
810 return out;
811 }
812
813 /*
814 =item fill_image(fill, x, y, width, channels, data, work)
815
816 =cut
817 */
818 static void
819 fill_image(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
820 int channels, i_color *data) {
821 struct i_fill_image_t *f = (struct i_fill_image_t *)fill;
822 i_img_dim i = 0;
823 i_color *out = data;
824 int want_channels = channels > 2 ? 4 : 2;
825
826 if (f->has_matrix) {
827 /* the hard way */
828 while (i < width) {
829 double rx = f->matrix[0] * (x+i) + f->matrix[1] * y + f->matrix[2];
830 double ry = f->matrix[3] * (x+i) + f->matrix[4] * y + f->matrix[5];
831 double ix = floor(rx / f->src->xsize);
832 double iy = floor(ry / f->src->ysize);
833 i_color c[2][2];
834 i_color c2[2];
835 i_img_dim dy;
836
837 if (f->xoff) {
838 rx += iy * f->xoff;
839 ix = floor(rx / f->src->xsize);
840 }
841 else if (f->yoff) {
842 ry += ix * f->yoff;
843 iy = floor(ry / f->src->ysize);
844 }
845 rx -= ix * f->src->xsize;
846 ry -= iy * f->src->ysize;
847
848 for (dy = 0; dy < 2; ++dy) {
849 if ((i_img_dim)rx == f->src->xsize-1) {
850 i_gpix(f->src, f->src->xsize-1, ((i_img_dim)ry+dy) % f->src->ysize, &c[dy][0]);
851 i_gpix(f->src, 0, ((i_img_dim)ry+dy) % f->src->xsize, &c[dy][1]);
852 }
853 else {
854 i_glin(f->src, (i_img_dim)rx, (i_img_dim)rx+2, ((i_img_dim)ry+dy) % f->src->ysize,
855 c[dy]);
856 }
857 c2[dy] = interp_i_color(c[dy][0], c[dy][1], rx, f->src->channels);
858 }
859 *out++ = interp_i_color(c2[0], c2[1], ry, f->src->channels);
860 ++i;
861 }
862 }
863 else {
864 /* the easy way */
865 /* this should be possible to optimize to use i_glin() */
866 while (i < width) {
867 i_img_dim rx = x+i;
868 i_img_dim ry = y;
869 i_img_dim ix = rx / f->src->xsize;
870 i_img_dim iy = ry / f->src->ysize;
871
872 if (f->xoff) {
873 rx += iy * f->xoff;
874 ix = rx / f->src->xsize;
875 }
876 else if (f->yoff) {
877 ry += ix * f->yoff;
878 iy = ry / f->src->ysize;
879 }
880 rx -= ix * f->src->xsize;
881 ry -= iy * f->src->ysize;
882 i_gpix(f->src, rx, ry, out);
883 ++out;
884 ++i;
885 }
886 }
887 if (f->src->channels != want_channels)
888 i_adapt_colors(want_channels, f->src->channels, data, width);
889 }
890
891 /*
892 =item fill_imagef(fill, x, y, width, channels, data, work)
893
894 =cut
895 */
896 static void
897 fill_imagef(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
898 int channels, i_fcolor *data) {
899 struct i_fill_image_t *f = (struct i_fill_image_t *)fill;
900 i_img_dim i = 0;
901 int want_channels = channels > 2 ? 4 : 2;
902
903 if (f->has_matrix) {
904 i_fcolor *work_data = data;
905 /* the hard way */
906 while (i < width) {
907 double rx = f->matrix[0] * (x+i) + f->matrix[1] * y + f->matrix[2];
908 double ry = f->matrix[3] * (x+i) + f->matrix[4] * y + f->matrix[5];
909 double ix = floor(rx / f->src->xsize);
910 double iy = floor(ry / f->src->ysize);
911 i_fcolor c[2][2];
912 i_fcolor c2[2];
913 i_img_dim dy;
914
915 if (f->xoff) {
916 rx += iy * f->xoff;
917 ix = floor(rx / f->src->xsize);
918 }
919 else if (f->yoff) {
920 ry += ix * f->yoff;
921 iy = floor(ry / f->src->ysize);
922 }
923 rx -= ix * f->src->xsize;
924 ry -= iy * f->src->ysize;
925
926 for (dy = 0; dy < 2; ++dy) {
927 if ((i_img_dim)rx == f->src->xsize-1) {
928 i_gpixf(f->src, f->src->xsize-1, ((i_img_dim)ry+dy) % f->src->ysize, &c[dy][0]);
929 i_gpixf(f->src, 0, ((i_img_dim)ry+dy) % f->src->xsize, &c[dy][1]);
930 }
931 else {
932 i_glinf(f->src, (i_img_dim)rx, (i_img_dim)rx+2, ((i_img_dim)ry+dy) % f->src->ysize,
933 c[dy]);
934 }
935 c2[dy] = interp_i_fcolor(c[dy][0], c[dy][1], rx, f->src->channels);
936 }
937 *work_data++ = interp_i_fcolor(c2[0], c2[1], ry, f->src->channels);
938 ++i;
939 }
940 }
941 else {
942 i_fcolor *work_data = data;
943 /* the easy way */
944 /* this should be possible to optimize to use i_glin() */
945 while (i < width) {
946 i_img_dim rx = x+i;
947 i_img_dim ry = y;
948 i_img_dim ix = rx / f->src->xsize;
949 i_img_dim iy = ry / f->src->ysize;
950
951 if (f->xoff) {
952 rx += iy * f->xoff;
953 ix = rx / f->src->xsize;
954 }
955 else if (f->yoff) {
956 ry += ix * f->yoff;
957 iy = ry / f->src->xsize;
958 }
959 rx -= ix * f->src->xsize;
960 ry -= iy * f->src->ysize;
961 i_gpixf(f->src, rx, ry, work_data);
962 ++work_data;
963 ++i;
964 }
965 }
966 if (f->src->channels != want_channels)
967 i_adapt_fcolors(want_channels, f->src->channels, data, width);
968 }
969
970 static void
971 fill_opacity(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
972 int channels, i_color *data) {
973 struct i_fill_opacity_t *f = (struct i_fill_opacity_t *)fill;
974 int alpha_chan = channels > 2 ? 3 : 1;
975 i_color *datap = data;
976
977 (f->other_fill->f_fill_with_color)(f->other_fill, x, y, width, channels, data);
978 while (width--) {
979 double new_alpha = datap->channel[alpha_chan] * f->alpha_mult;
980 if (new_alpha < 0)
981 datap->channel[alpha_chan] = 0;
982 else if (new_alpha > 255)
983 datap->channel[alpha_chan] = 255;
984 else datap->channel[alpha_chan] = (int)(new_alpha + 0.5);
985
986 ++datap;
987 }
988 }
989 static void
990 fill_opacityf(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
991 int channels, i_fcolor *data) {
992 struct i_fill_opacity_t *f = (struct i_fill_opacity_t *)fill;
993 int alpha_chan = channels > 2 ? 3 : 1;
994 i_fcolor *datap = data;
995
996 (f->other_fill->f_fill_with_fcolor)(f->other_fill, x, y, width, channels, data);
997
998 while (width--) {
999 double new_alpha = datap->channel[alpha_chan] * f->alpha_mult;
1000 if (new_alpha < 0)
1001 datap->channel[alpha_chan] = 0;
1002 else if (new_alpha > 1.0)
1003 datap->channel[alpha_chan] = 1.0;
1004 else datap->channel[alpha_chan] = new_alpha;
1005
1006 ++datap;
1007 }
1008 }
1009
1010 /*
1011 =back
1012
1013 =head1 AUTHOR
1014
1015 Tony Cook <[email protected]>
1016
1017 =head1 SEE ALSO
1018
1019 Imager(3)
1020
1021 =cut
1022 */