fills.c

filter: Statementstmt 82.8% 241 / 291 Branchbran 69.4% 107 / 154 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.2% 348 / 445 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« pnm.c ↑ Summary rotate.im »
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 4 static i_color fcolor_to_color(const i_fcolor *c) {
111 int ch;
112 i_color out;
113  
114 20 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) 16 4 20
115 16 out.channel[ch] = SampleFTo8(c->channel[ch]);
116  
117 4 return out;
118 }
119  
120 58 static i_fcolor color_to_fcolor(const i_color *c) {
121 int ch;
122 i_fcolor out;
123  
124 290 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) 232 58 290
125 232 out.channel[ch] = Sample8ToF(c->channel[ch]);
126  
127 58 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 74 i_fill_destroy(i_fill_t *fill) {
195 74 ▶ if (fill->destroy)
Branch: 1 branch
BranchTrueFalseTotal
if (fill->destroy) 5 69 74
196 5 (fill->destroy)(fill);
197 74 myfree(fill);
198 74 }
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 1 i_new_fill_solidf(const i_fcolor *c, int combine) {
215 int ch;
216 1 i_fill_solid_t *fill = mymalloc(sizeof(i_fill_solid_t)); /* checked 14jul05 tonyc */
217 1 memset(fill, 0, sizeof(*fill));
218
219 1 fill->base = base_solid_fill;
220 1 ▶ if (combine) {
Branch: 1 branch
BranchTrueFalseTotal
if (combine) { 0 1 1
221 0 i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
222 }
223  
224 1 fill->fc = *c;
225 5 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) { 4 1 5
226 4 fill->c.channel[ch] = SampleFTo8(c->channel[ch]);
227 }
228
229 1 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 23 i_new_fill_solid(const i_color *c, int combine) {
247 int ch;
248 23 i_fill_solid_t *fill = mymalloc(sizeof(i_fill_solid_t)); /* checked 14jul05 tonyc */
249  
250 23 memset(fill, 0, sizeof(*fill));
251 23 fill->base = base_solid_fill;
252 23 ▶ if (combine) {
Branch: 1 branch
BranchTrueFalseTotal
if (combine) { 17 6 23
253 17 i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
254 }
255  
256 23 fill->c = *c;
257 115 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) { 92 23 115
258 92 fill->fc.channel[ch] = Sample8ToF(c->channel[ch]);
259 }
260
261 23 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 29 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 29 ▶ assert(fg);
Branch: 1 branch
BranchTrueFalseTotal
assert(fg); 0 29 29
441 29 ▶ assert(bg);
Branch: 1 branch
BranchTrueFalseTotal
assert(bg); 0 29 29
442 29 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 2 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 2 ▶ assert(fg);
Branch: 1 branch
BranchTrueFalseTotal
assert(fg); 0 2 2
471 2 ▶ assert(bg);
Branch: 1 branch
BranchTrueFalseTotal
assert(bg); 0 2 2
472 2 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 9 i_new_fill_image(i_img *im, const double *matrix, i_img_dim xoff, i_img_dim yoff, int combine) {
513 9 struct i_fill_image_t *fill = mymalloc(sizeof(*fill)); /* checked 14jul05 tonyc */
514  
515 9 memset(fill, 0, sizeof(*fill));
516 9 fill->base = image_fill_proto;
517  
518 9 ▶ if (combine) {
Branch: 1 branch
BranchTrueFalseTotal
if (combine) { 8 1 9
519 8 i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
520 }
521 else {
522 1 fill->base.combine = NULL;
523 1 fill->base.combinef = NULL;
524 }
525 9 fill->src = im;
526 9 ▶ if (xoff < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (xoff < 0) 0 9 9
527 0 xoff += im->xsize;
528 9 fill->xoff = xoff;
529 9 ▶ if (yoff < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (yoff < 0) 0 9 9
530 0 yoff += im->ysize;
531 9 fill->yoff = yoff;
532 9 ▶ if (matrix) {
Branch: 1 branch
BranchTrueFalseTotal
if (matrix) { 1 8 9
533 1 fill->has_matrix = 1;
534 1 memcpy(fill->matrix, matrix, sizeof(fill->matrix));
535 }
536 else
537 8 fill->has_matrix = 0;
538  
539 9 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 5 i_new_fill_opacity(i_fill_t *base_fill, double alpha_mult) {
564 5 struct i_fill_opacity_t *fill = mymalloc(sizeof(*fill));
565  
566 5 memset(fill, 0, sizeof(*fill));
567 5 fill->base = opacity_fill_proto;
568  
569 5 fill->base.combine = base_fill->combine;
570 5 fill->base.combinef = base_fill->combinef;
571  
572 5 fill->other_fill = base_fill;
573 5 fill->alpha_mult = alpha_mult;
574  
575 5 ▶ if (!base_fill->f_fill_with_color) {
Branch: 1 branch
BranchTrueFalseTotal
if (!base_fill->f_fill_with_color) { 1 4 5
576 /* base fill only does floating, so we only do that too */
577 1 fill->base.f_fill_with_color = NULL;
578 }
579  
580 5 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 729 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 729 i_color c = T_SOLID_FILL(fill)->c;
602  
603 (void)x;
604 (void)y;
605
606 729 ▶ i_adapt_colors(channels > 2 ? 4 : 2, 4, &c, 1);
Branch: 1 branch
BranchTrueFalseTotal
i_adapt_colors(channels > 2 ? 4 : 2, 4, &c, 1); 729 0 729
607 39919 ▶ while (width-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (width-- > 0) { 39190 729 39919
608 39190 *data++ = c;
609 }
610 729 }
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 156 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 156 i_fcolor c = T_SOLID_FILL(fill)->fc;
623  
624 (void)x;
625 (void)y;
626
627 156 ▶ i_adapt_fcolors(channels > 2 ? 4 : 2, 4, &c, 1);
Branch: 1 branch
BranchTrueFalseTotal
i_adapt_fcolors(channels > 2 ? 4 : 2, 4, &c, 1); 156 0 156
628 780 ▶ while (width-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (width-- > 0) { 624 156 780
629 624 *data++ = c;
630 }
631 156 }
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 31 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 31 i_fill_hatch_t *fill = mymalloc(sizeof(i_fill_hatch_t)); /* checked 14jul05 tonyc */
656  
657 31 memset(fill, 0, sizeof(*fill));
658 31 fill->base = hatch_fill_proto;
659 31 ▶ if (fg && bg) {
Branch: 2 branches
BranchTrueFalseTotal
if (fg && bg) { 29 2 31
if (fg && bg) { 29 0 29
660 29 fill->fg = *fg;
661 29 fill->bg = *bg;
662 29 fill->ffg = color_to_fcolor(fg);
663 29 fill->fbg = color_to_fcolor(bg);
664 }
665 2 ▶ else if (ffg && fbg) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ffg && fbg) { 2 0 2
else if (ffg && fbg) { 2 0 2
666 2 fill->fg = fcolor_to_color(ffg);
667 2 fill->bg = fcolor_to_color(fbg);
668 2 fill->ffg = *ffg;
669 2 fill->fbg = *fbg;
670 }
671 else {
672 0 assert(0);
673 /* NOTREACHED */
674 }
675
676 31 ▶ if (combine) {
Branch: 1 branch
BranchTrueFalseTotal
if (combine) { 3 28 31
677 3 i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
678 }
679 else {
680 28 fill->base.combine = NULL;
681 28 fill->base.combinef = NULL;
682 }
683 31 ▶ if (cust_hatch) {
Branch: 1 branch
BranchTrueFalseTotal
if (cust_hatch) { 1 30 31
684 1 memcpy(fill->hatch, cust_hatch, 8);
685 }
686 else {
687 30 ▶ if (hatch < 0
Branch: 1 branch
BranchTrueFalseTotal
if (hatch < 0 30 0 30
688 30 ▶ || (size_t)hatch >= sizeof(builtin_hatches)/sizeof(*builtin_hatches)) {
Branch: 1 branch
BranchTrueFalseTotal
|| (size_t)hatch >= sizeof(builtin_hatches)/sizeof(*builtin_hatches)) { 0 30 30
689 0 hatch = 0;
690 }
691 30 memcpy(fill->hatch, builtin_hatches[hatch], 8);
692 }
693 31 fill->dx = dx & 7;
694 31 fill->dy = dy & 7;
695  
696 31 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 133458 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 133458 i_fill_hatch_t *f = (i_fill_hatch_t *)fill;
710 133458 int byte = f->hatch[(y + f->dy) & 7];
711 133458 int xpos = (x + f->dx) & 7;
712 133458 int mask = 128 >> xpos;
713 133458 i_color fg = f->fg;
714 133458 i_color bg = f->bg;
715  
716 133458 ▶ if (channels < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (channels < 3) { 50 133408 133458
717 50 i_adapt_colors(2, 4, &fg, 1);
718 50 i_adapt_colors(2, 4, &bg, 1);
719 }
720  
721 589752 ▶ while (width-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (width-- > 0) { 456294 133458 589752
722 456294 ▶ if (byte & mask)
Branch: 1 branch
BranchTrueFalseTotal
if (byte & mask) 219546 236748 456294
723 219546 *data++ = fg;
724 else
725 236748 *data++ = bg;
726
727 456294 ▶ if ((mask >>= 1) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((mask >>= 1) == 0) 40181 416113 456294
728 40181 mask = 128;
729 }
730 133458 }
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 488 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 488 i_fill_hatch_t *f = (i_fill_hatch_t *)fill;
743 488 int byte = f->hatch[(y + f->dy) & 7];
744 488 int xpos = (x + f->dx) & 7;
745 488 int mask = 128 >> xpos;
746 488 i_fcolor fg = f->ffg;
747 488 i_fcolor bg = f->fbg;
748  
749 488 ▶ if (channels < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (channels < 3) { 10 478 488
750 10 i_adapt_fcolors(2, 4, &fg, 1);
751 10 i_adapt_fcolors(2, 4, &bg, 1);
752 }
753
754 50675 ▶ while (width-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (width-- > 0) { 50187 488 50675
755 50187 ▶ if (byte & mask)
Branch: 1 branch
BranchTrueFalseTotal
if (byte & mask) 16687 33500 50187
756 16687 *data++ = fg;
757 else
758 33500 *data++ = bg;
759
760 50187 ▶ if ((mask >>= 1) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((mask >>= 1) == 0) 6171 44016 50187
761 6171 mask = 128;
762 }
763 488 }
764  
765 /* hopefully this will be inlined (it is with -O3 with gcc 2.95.4) */
766 /* linear interpolation */
767 15555 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 15555 pos -= floor(pos);
773 77775 ▶ for (ch = 0; ch < channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < channels; ++ch) 62220 15555 77775
774 62220 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
775 15555 ▶ if (channels > 3 && out.channel[3]) {
Branch: 2 branches
BranchTrueFalseTotal
if (channels > 3 && out.channel[3]) { 15555 0 15555
if (channels > 3 && out.channel[3]) { 9702 5853 15555
776 48510 ▶ for (ch = 0; ch < channels; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < channels; ++ch) { 38808 9702 48510
777 38808 ▶ if (ch != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch != 3) { 29106 9702 38808
778 29106 int temp = out.channel[ch] * 255 / out.channel[3];
779 29106 ▶ if (temp > 255)
Branch: 1 branch
BranchTrueFalseTotal
if (temp > 255) 6953 22153 29106
780 6953 temp = 255;
781 29106 out.channel[ch] = temp;
782 }
783 }
784 }
785  
786 15555 return out;
787 }
788  
789 /* hopefully this will be inlined (it is with -O3 with gcc 2.95.4) */
790 /* linear interpolation */
791 0 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 0 pos -= floor(pos);
797 0 ▶ for (ch = 0; ch < channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < channels; ++ch) 0 0 0
798 0 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
799 0 ▶ if (out.channel[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (out.channel[3]) { 0 0 0
800 0 ▶ for (ch = 0; ch < channels; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < channels; ++ch) { 0 0 0
801 0 ▶ if (ch != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch != 3) { 0 0 0
802 0 int temp = out.channel[ch] / out.channel[3];
803 0 ▶ if (temp > 1.0)
Branch: 1 branch
BranchTrueFalseTotal
if (temp > 1.0) 0 0 0
804 0 temp = 1.0;
805 0 out.channel[ch] = temp;
806 }
807 }
808 }
809  
810 0 return out;
811 }
812  
813 /*
814 =item fill_image(fill, x, y, width, channels, data, work)
815  
816 =cut
817 */
818 static void
819 372 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 372 struct i_fill_image_t *f = (struct i_fill_image_t *)fill;
822 372 i_img_dim i = 0;
823 372 i_color *out = data;
824 372 ▶ int want_channels = channels > 2 ? 4 : 2;
Branch: 1 branch
BranchTrueFalseTotal
int want_channels = channels > 2 ? 4 : 2; 332 40 372
825
826 372 ▶ if (f->has_matrix) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->has_matrix) { 81 291 372
827 /* the hard way */
828 5266 ▶ while (i < width) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < width) { 5185 81 5266
829 5185 double rx = f->matrix[0] * (x+i) + f->matrix[1] * y + f->matrix[2];
830 5185 double ry = f->matrix[3] * (x+i) + f->matrix[4] * y + f->matrix[5];
831 5185 double ix = floor(rx / f->src->xsize);
832 5185 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 5185 ▶ if (f->xoff) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->xoff) { 5185 0 5185
838 5185 rx += iy * f->xoff;
839 5185 ix = floor(rx / f->src->xsize);
840 }
841 0 ▶ else if (f->yoff) {
Branch: 1 branch
BranchTrueFalseTotal
else if (f->yoff) { 0 0 0
842 0 ry += ix * f->yoff;
843 0 iy = floor(ry / f->src->ysize);
844 }
845 5185 rx -= ix * f->src->xsize;
846 5185 ry -= iy * f->src->ysize;
847  
848 15555 ▶ for (dy = 0; dy < 2; ++dy) {
Branch: 1 branch
BranchTrueFalseTotal
for (dy = 0; dy < 2; ++dy) { 10370 5185 15555
849 10370 ▶ if ((i_img_dim)rx == f->src->xsize-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((i_img_dim)rx == f->src->xsize-1) { 224 10146 10370
850 224 i_gpix(f->src, f->src->xsize-1, ((i_img_dim)ry+dy) % f->src->ysize, &c[dy][0]);
851 224 i_gpix(f->src, 0, ((i_img_dim)ry+dy) % f->src->xsize, &c[dy][1]);
852 }
853 else {
854 10146 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 10370 c2[dy] = interp_i_color(c[dy][0], c[dy][1], rx, f->src->channels);
858 }
859 5185 *out++ = interp_i_color(c2[0], c2[1], ry, f->src->channels);
860 5185 ++i;
861 }
862 }
863 else {
864 /* the easy way */
865 /* this should be possible to optimize to use i_glin() */
866 17376 ▶ while (i < width) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < width) { 17085 291 17376
867 17085 i_img_dim rx = x+i;
868 17085 i_img_dim ry = y;
869 17085 i_img_dim ix = rx / f->src->xsize;
870 17085 i_img_dim iy = ry / f->src->ysize;
871  
872 17085 ▶ if (f->xoff) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->xoff) { 5185 11900 17085
873 5185 rx += iy * f->xoff;
874 5185 ix = rx / f->src->xsize;
875 }
876 11900 ▶ else if (f->yoff) {
Branch: 1 branch
BranchTrueFalseTotal
else if (f->yoff) { 10000 1900 11900
877 10000 ry += ix * f->yoff;
878 10000 iy = ry / f->src->ysize;
879 }
880 17085 rx -= ix * f->src->xsize;
881 17085 ry -= iy * f->src->ysize;
882 17085 i_gpix(f->src, rx, ry, out);
883 17085 ++out;
884 17085 ++i;
885 }
886 }
887 372 ▶ if (f->src->channels != want_channels)
Branch: 1 branch
BranchTrueFalseTotal
if (f->src->channels != want_channels) 180 192 372
888 180 i_adapt_colors(want_channels, f->src->channels, data, width);
889 372 }
890  
891 /*
892 =item fill_imagef(fill, x, y, width, channels, data, work)
893  
894 =cut
895 */
896 static void
897 100 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 100 struct i_fill_image_t *f = (struct i_fill_image_t *)fill;
900 100 i_img_dim i = 0;
901 100 ▶ int want_channels = channels > 2 ? 4 : 2;
Branch: 1 branch
BranchTrueFalseTotal
int want_channels = channels > 2 ? 4 : 2; 60 40 100
902
903 100 ▶ if (f->has_matrix) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->has_matrix) { 0 100 100
904 0 i_fcolor *work_data = data;
905 /* the hard way */
906 0 ▶ while (i < width) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < width) { 0 0 0
907 0 double rx = f->matrix[0] * (x+i) + f->matrix[1] * y + f->matrix[2];
908 0 double ry = f->matrix[3] * (x+i) + f->matrix[4] * y + f->matrix[5];
909 0 double ix = floor(rx / f->src->xsize);
910 0 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 0 ▶ if (f->xoff) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->xoff) { 0 0 0
916 0 rx += iy * f->xoff;
917 0 ix = floor(rx / f->src->xsize);
918 }
919 0 ▶ else if (f->yoff) {
Branch: 1 branch
BranchTrueFalseTotal
else if (f->yoff) { 0 0 0
920 0 ry += ix * f->yoff;
921 0 iy = floor(ry / f->src->ysize);
922 }
923 0 rx -= ix * f->src->xsize;
924 0 ry -= iy * f->src->ysize;
925  
926 0 ▶ for (dy = 0; dy < 2; ++dy) {
Branch: 1 branch
BranchTrueFalseTotal
for (dy = 0; dy < 2; ++dy) { 0 0 0
927 0 ▶ if ((i_img_dim)rx == f->src->xsize-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((i_img_dim)rx == f->src->xsize-1) { 0 0 0
928 0 i_gpixf(f->src, f->src->xsize-1, ((i_img_dim)ry+dy) % f->src->ysize, &c[dy][0]);
929 0 i_gpixf(f->src, 0, ((i_img_dim)ry+dy) % f->src->xsize, &c[dy][1]);
930 }
931 else {
932 0 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 0 c2[dy] = interp_i_fcolor(c[dy][0], c[dy][1], rx, f->src->channels);
936 }
937 0 *work_data++ = interp_i_fcolor(c2[0], c2[1], ry, f->src->channels);
938 0 ++i;
939 }
940 }
941 else {
942 100 i_fcolor *work_data = data;
943 /* the easy way */
944 /* this should be possible to optimize to use i_glin() */
945 1900 ▶ while (i < width) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < width) { 1800 100 1900
946 1800 i_img_dim rx = x+i;
947 1800 i_img_dim ry = y;
948 1800 i_img_dim ix = rx / f->src->xsize;
949 1800 i_img_dim iy = ry / f->src->ysize;
950  
951 1800 ▶ if (f->xoff) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->xoff) { 0 1800 1800
952 0 rx += iy * f->xoff;
953 0 ix = rx / f->src->xsize;
954 }
955 1800 ▶ else if (f->yoff) {
Branch: 1 branch
BranchTrueFalseTotal
else if (f->yoff) { 0 1800 1800
956 0 ry += ix * f->yoff;
957 0 iy = ry / f->src->xsize;
958 }
959 1800 rx -= ix * f->src->xsize;
960 1800 ry -= iy * f->src->ysize;
961 1800 i_gpixf(f->src, rx, ry, work_data);
962 1800 ++work_data;
963 1800 ++i;
964 }
965 }
966 100 ▶ if (f->src->channels != want_channels)
Branch: 1 branch
BranchTrueFalseTotal
if (f->src->channels != want_channels) 80 20 100
967 80 i_adapt_fcolors(want_channels, f->src->channels, data, width);
968 100 }
969  
970 static void
971 31 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 31 struct i_fill_opacity_t *f = (struct i_fill_opacity_t *)fill;
974 31 ▶ int alpha_chan = channels > 2 ? 3 : 1;
Branch: 1 branch
BranchTrueFalseTotal
int alpha_chan = channels > 2 ? 3 : 1; 31 0 31
975 31 i_color *datap = data;
976
977 31 (f->other_fill->f_fill_with_color)(f->other_fill, x, y, width, channels, data);
978 332 ▶ while (width--) {
Branch: 1 branch
BranchTrueFalseTotal
while (width--) { 301 31 332
979 301 double new_alpha = datap->channel[alpha_chan] * f->alpha_mult;
980 301 ▶ if (new_alpha < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (new_alpha < 0) 0 301 301
981 0 datap->channel[alpha_chan] = 0;
982 301 ▶ else if (new_alpha > 255)
Branch: 1 branch
BranchTrueFalseTotal
else if (new_alpha > 255) 0 301 301
983 0 datap->channel[alpha_chan] = 255;
984 301 else datap->channel[alpha_chan] = (int)(new_alpha + 0.5);
985  
986 301 ++datap;
987 }
988 31 }
989 static void
990 120 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 120 struct i_fill_opacity_t *f = (struct i_fill_opacity_t *)fill;
993 120 ▶ int alpha_chan = channels > 2 ? 3 : 1;
Branch: 1 branch
BranchTrueFalseTotal
int alpha_chan = channels > 2 ? 3 : 1; 120 0 120
994 120 i_fcolor *datap = data;
995
996 120 (f->other_fill->f_fill_with_fcolor)(f->other_fill, x, y, width, channels, data);
997
998 10320 ▶ while (width--) {
Branch: 1 branch
BranchTrueFalseTotal
while (width--) { 10200 120 10320
999 10200 double new_alpha = datap->channel[alpha_chan] * f->alpha_mult;
1000 10200 ▶ if (new_alpha < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (new_alpha < 0) 0 10200 10200
1001 0 datap->channel[alpha_chan] = 0;
1002 10200 ▶ else if (new_alpha > 1.0)
Branch: 1 branch
BranchTrueFalseTotal
else if (new_alpha > 1.0) 0 10200 10200
1003 0 datap->channel[alpha_chan] = 1.0;
1004 10200 else datap->channel[alpha_chan] = new_alpha;
1005  
1006 10200 ++datap;
1007 }
1008 120 }
1009  
1010 /*
1011 =back
1012  
1013 =head1 AUTHOR
1014  
1015 Tony Cook <tony@develop-help.com>
1016  
1017 =head1 SEE ALSO
1018  
1019 Imager(3)
1020  
1021 =cut
1022 */
« pnm.c ↑ Summary rotate.im »