Debian Codegraph @

render.im

raw
1 /*
2 Render utilities
3 */
4 #include "imager.h"
5
6 #define RENDER_MAGIC 0x765AE
7
8 typedef void (*render_color_f)(i_render *, i_img_dim, i_img_dim, i_img_dim, unsigned char const *src, i_color const *color);
9
10 #define i_has_alpha(channels) ((channels) == 2 || (channels) == 4)
11
12 #define i_color_channels(channels) (i_has_alpha(channels) ? (channels)-1 : (channels))
13
14 #code
15
16 static void IM_SUFFIX(render_color_alpha)(i_render *r, i_img_dim x, i_img_dim y, i_img_dim width, unsigned char const *src, i_color const *color);
17 static void IM_SUFFIX(render_color_13)(i_render *r, i_img_dim x, i_img_dim y, i_img_dim width, unsigned char const *src, i_color const *color);
18
19 static render_color_f IM_SUFFIX(render_color_tab)[] =
20 {
21 NULL,
22 IM_SUFFIX(render_color_13),
23 IM_SUFFIX(render_color_alpha),
24 IM_SUFFIX(render_color_13),
25 IM_SUFFIX(render_color_alpha),
26 };
27
28 static void IM_SUFFIX(combine_line_noalpha)(IM_COLOR *out, IM_COLOR const *in, int channels, i_img_dim count);
29 static void IM_SUFFIX(combine_line_alpha)(IM_COLOR *out, IM_COLOR const *in, int channels, i_img_dim count);
30 /* the copy variant copies the source alpha to the the output alpha channel */
31 static void IM_SUFFIX(combine_line_alpha_na)(IM_COLOR *out, IM_COLOR const *in, int channels, i_img_dim count);
32
33 static void IM_SUFFIX(combine_line)(IM_COLOR *out, IM_COLOR const *in, int channels, i_img_dim count);
34 static void IM_SUFFIX(combine_line_na)(IM_COLOR *out, IM_COLOR const *in, int channels, i_img_dim count);
35
36 #/code
37
38 /*
39 =item i_render_new(im, width)
40 =category Blit tools
41
42 Allocate a new C<i_render> object and initialize it.
43
44 =cut
45 */
46
47 i_render *
48 i_render_new(i_img *im, i_img_dim width) {
49 i_render *r = mymalloc(sizeof(i_render));
50
51 i_render_init(r, im, width);
52
53 return r;
54 }
55
56 /*
57 =item i_render_delete(r)
58 =category Blit tools
59
60 Release an C<i_render> object.
61
62 =cut
63 */
64
65 void
66 i_render_delete(i_render *r) {
67 i_render_done(r);
68 myfree(r);
69 }
70
71 void
72 i_render_init(i_render *r, i_img *im, i_img_dim width) {
73 r->magic = RENDER_MAGIC;
74 r->im = im;
75 r->line_width = width;
76 r->line_8 = NULL;
77 r->line_double = NULL;
78 r->fill_width = width;
79 r->fill_line_8 = NULL;
80 r->fill_line_double = NULL;
81 }
82
83 void
84 i_render_done(i_render *r) {
85 if (r->line_8)
86 myfree(r->line_8);
87 if (r->line_double)
88 myfree(r->line_double);
89 if (r->fill_line_8)
90 myfree(r->fill_line_8);
91 if (r->fill_line_double)
92 myfree(r->fill_line_double);
93 r->magic = 0;
94 }
95
96 static void
97 alloc_line(i_render *r, i_img_dim width, i_img_dim eight_bit) {
98 if (width > r->line_width) {
99 i_img_dim new_width = r->line_width * 2;
100 if (new_width < width)
101 new_width = width;
102
103 if (eight_bit) {
104 if (r->line_8)
105 r->line_8 = myrealloc(r->line_8, sizeof(i_color) * new_width);
106 else
107 r->line_8 = mymalloc(sizeof(i_color) * new_width);
108 if (r->line_double) {
109 myfree(r->line_double);
110 r->line_double = NULL;
111 }
112 }
113 else {
114 if (r->line_double)
115 r->line_double = myrealloc(r->line_double, sizeof(i_fcolor) * new_width);
116 else
117 r->line_double = mymalloc(sizeof(i_fcolor) * new_width);
118 if (r->line_8) {
119 myfree(r->line_8);
120 r->line_double = NULL;
121 }
122 }
123
124 r->line_width = new_width;
125 }
126 else {
127 if (eight_bit) {
128 if (!r->line_8)
129 r->line_8 = mymalloc(sizeof(i_color) * r->line_width);
130 if (r->line_double) {
131 myfree(r->line_double);
132 r->line_double = NULL;
133 }
134 }
135 else {
136 if (!r->line_double)
137 r->line_double = mymalloc(sizeof(i_fcolor) * r->line_width);
138 if (r->line_8) {
139 myfree(r->line_8);
140 r->line_8 = NULL;
141 }
142 }
143 }
144 }
145
146 static void
147 alloc_fill_line(i_render *r, i_img_dim width, int eight_bit) {
148 if (width > r->fill_width) {
149 i_img_dim new_width = r->fill_width * 2;
150 if (new_width < width)
151 new_width = width;
152
153 if (eight_bit) {
154 if (r->line_8)
155 r->fill_line_8 = myrealloc(r->fill_line_8, sizeof(i_color) * new_width);
156 else
157 r->fill_line_8 = mymalloc(sizeof(i_color) * new_width);
158 if (r->fill_line_double) {
159 myfree(r->fill_line_double);
160 r->fill_line_double = NULL;
161 }
162 }
163 else {
164 if (r->fill_line_double)
165 r->fill_line_double = myrealloc(r->fill_line_double, sizeof(i_fcolor) * new_width);
166 else
167 r->fill_line_double = mymalloc(sizeof(i_fcolor) * new_width);
168 if (r->fill_line_8) {
169 myfree(r->fill_line_8);
170 r->fill_line_double = NULL;
171 }
172 }
173
174 r->fill_width = new_width;
175 }
176 else {
177 if (eight_bit) {
178 if (!r->fill_line_8)
179 r->fill_line_8 = mymalloc(sizeof(i_color) * r->fill_width);
180 if (r->fill_line_double) {
181 myfree(r->fill_line_double);
182 r->fill_line_double = NULL;
183 }
184 }
185 else {
186 if (!r->fill_line_double)
187 r->fill_line_double = mymalloc(sizeof(i_fcolor) * r->fill_width);
188 if (r->fill_line_8) {
189 myfree(r->fill_line_8);
190 r->fill_line_8 = NULL;
191 }
192 }
193 }
194 }
195
196 /*
197 =item i_render_color(r, x, y, width, source, color)
198 =category Blit tools
199
200 Render the given color with the coverage specified by C<source[0]> to
201 C<source[width-1]>.
202
203 Renders in normal combine mode.
204
205 =cut
206 */
207
208 void
209 i_render_color(i_render *r, i_img_dim x, i_img_dim y, i_img_dim width,
210 unsigned char const *src, i_color const *color) {
211 i_img *im = r->im;
212 if (y < 0 || y >= im->ysize)
213 return;
214 if (x < 0) {
215 width += x;
216 src -= x;
217 x = 0;
218 }
219 if (x + width > im->xsize) {
220 width = im->xsize - x;
221 }
222 if (x >= im->xsize || x + width <= 0 || width <= 0)
223 return;
224
225 /* avoid as much work as we can */
226 while (width > 0 && *src == 0) {
227 --width;
228 ++src;
229 ++x;
230 }
231 while (width > 0 && src[width-1] == 0) {
232 --width;
233 }
234 if (!width)
235 return;
236
237 alloc_line(r, width, r->im->bits <= 8);
238
239 #code r->im->bits <= 8
240 /*if (r->IM_SUFFIX(line) == NULL)
241 r->IM_SUFFIX(line) = mymalloc(sizeof(IM_COLOR) * r->width);*/
242 (IM_SUFFIX(render_color_tab)[im->channels])(r, x, y, width, src, color);
243 #/code
244 }
245
246 /*
247 =item i_render_fill(r, x, y, width, source, fill)
248 =category Blit tools
249
250 Render the given fill with the coverage in C<source[0]> through
251 C<source[width-1]>.
252
253 =cut
254 */
255
256 void
257 i_render_fill(i_render *r, i_img_dim x, i_img_dim y, i_img_dim width,
258 unsigned char const *src, i_fill_t *fill) {
259 i_img *im = r->im;
260 int fill_channels = im->channels;
261
262 if (fill_channels == 1 || fill_channels == 3)
263 ++fill_channels;
264
265 if (y < 0 || y >= im->ysize)
266 return;
267 if (x < 0) {
268 width += x;
269 src -= x;
270 x = 0;
271 }
272 if (x + width > im->xsize) {
273 width = im->xsize - x;
274 }
275 if (x >= im->xsize || x + width <= 0 || width <= 0)
276 return;
277
278 if (src) {
279 /* avoid as much work as we can */
280 while (width > 0 && *src == 0) {
281 --width;
282 ++src;
283 ++x;
284 }
285 while (width > 0 && src[width-1] == 0) {
286 --width;
287 }
288 }
289 if (!width)
290 return;
291
292 alloc_line(r, width, r->im->bits <= 8 && fill->f_fill_with_color != NULL);
293 alloc_fill_line(r, width, r->im->bits <= 8 && fill->f_fill_with_color != NULL);
294
295 #code r->im->bits <= 8 && fill->f_fill_with_color
296 if (IM_FILL_COMBINE(fill)) {
297 IM_COLOR *srcc = r->IM_SUFFIX(fill_line);
298 IM_COLOR *destc = r->IM_SUFFIX(line);
299 IM_FILL_FILLER(fill)(fill, x, y, width, fill_channels, r->IM_SUFFIX(fill_line));
300 if (src) {
301 unsigned char const *srcc = src;
302 IM_COLOR *fillc = r->IM_SUFFIX(fill_line);
303 i_img_dim work_width = width;
304 while (work_width) {
305 if (*srcc == 0) {
306 fillc->channel[fill_channels-1] = 0;
307 }
308 else if (*srcc != 255) {
309 fillc->channel[fill_channels-1] =
310 fillc->channel[fill_channels-1] * *srcc / 255;
311 }
312 --work_width;
313 ++srcc;
314 ++fillc;
315 }
316 }
317 IM_GLIN(r->im, x, x+width, y, r->IM_SUFFIX(line));
318 IM_FILL_COMBINE(fill)(destc, srcc, r->im->channels, width);
319 }
320 else {
321 if (src) {
322 i_img_dim work_width = width;
323 IM_COLOR *srcc = r->IM_SUFFIX(fill_line);
324 IM_COLOR *destc = r->IM_SUFFIX(line);
325 int ch;
326
327 IM_FILL_FILLER(fill)(fill, x, y, width, fill_channels, r->IM_SUFFIX(fill_line));
328 IM_GLIN(r->im, x, x+width, y, r->IM_SUFFIX(line));
329 while (work_width) {
330 if (*src == 255) {
331 /* just replace it */
332 *destc = *srcc;
333 }
334 else if (*src) {
335 for (ch = 0; ch < im->channels; ++ch) {
336 IM_WORK_T work = (destc->channel[ch] * (255 - *src)
337 + srcc->channel[ch] * *src) / 255.0;
338 destc->channel[ch] = IM_LIMIT(work);
339 }
340 }
341
342 ++srcc;
343 ++destc;
344 ++src;
345 --work_width;
346 }
347 }
348 else { /* if (src) */
349 IM_FILL_FILLER(fill)(fill, x, y, width, fill_channels, r->IM_SUFFIX(line));
350 }
351 }
352 IM_PLIN(im, x, x+width, y, r->IM_SUFFIX(line));
353 #/code
354 }
355
356 #if 0
357
358 /* for debuggin */
359
360 static void
361 dump_src(const char *note, unsigned char const *src, i_img_dim width) {
362 i_img_dim i;
363 printf("%s - %p/%" i_DF "\n", note, src, i_DFc(width));
364 for (i = 0; i < width; ++i) {
365 printf("%02x ", src[i]);
366 }
367 putchar('\n');
368 }
369
370 #endif
371
372 #code
373
374 /*
375 =item i_render_line(r, x, y, width, source, fill)
376 =category Blit tools
377
378 Render the given fill with the coverage in C<source[0]> through
379 C<source[width-1]>.
380
381 =cut
382
383 =item i_render_linef(r, x, y, width, source, fill)
384 =category Blit tools
385
386 Render the given fill with the coverage in C<source[0]> through
387 C<source[width-1]>.
388
389 =cut
390 */
391
392 void
393 IM_RENDER_LINE(i_render *r, i_img_dim x, i_img_dim y, i_img_dim width,
394 const IM_SAMPLE_T *src, IM_COLOR *line,
395 IM_FILL_COMBINE_F combine) {
396 i_img *im = r->im;
397 int src_chans = im->channels;
398
399 /* src must always have an alpha channel */
400 if (src_chans == 1 || src_chans == 3)
401 ++src_chans;
402
403 if (y < 0 || y >= im->ysize)
404 return;
405 if (x < 0) {
406 src -= x;
407 line -= x;
408 width += x;
409 x = 0;
410 }
411 if (x + width > im->xsize)
412 width = r->im->xsize - x;
413
414 #ifdef IM_EIGHT_BIT
415 alloc_line(r, width, 1);
416 #else
417 alloc_line(r, width, 0);
418 #endif
419
420 if (combine) {
421 if (src) {
422 i_img_dim work_width = width;
423 IM_COLOR *linep = line;
424 const IM_SAMPLE_T *srcp = src;
425 int alpha_chan = src_chans - 1;
426
427 while (work_width) {
428 if (*srcp) {
429 if (*srcp != IM_SAMPLE_MAX)
430 linep->channel[alpha_chan] =
431 linep->channel[alpha_chan] * *srcp / IM_SAMPLE_MAX;
432 }
433 else {
434 linep->channel[alpha_chan] = 0;
435 }
436 --work_width;
437 ++srcp;
438 ++linep;
439 }
440 }
441 IM_GLIN(im, x, x+width, y, r->IM_SUFFIX(line));
442 combine(r->IM_SUFFIX(line), line, im->channels, width);
443 IM_PLIN(im, x, x+width, y, r->IM_SUFFIX(line));
444 }
445 else {
446 if (src) {
447 i_img_dim work_width = width;
448 IM_COLOR *srcc = line;
449 IM_COLOR *destc = r->IM_SUFFIX(line);
450
451 IM_GLIN(im, x, x+width, y, r->IM_SUFFIX(line));
452 while (work_width) {
453 if (*src == 255) {
454 /* just replace it */
455 *destc = *srcc;
456 }
457 else if (*src) {
458 int ch;
459 for (ch = 0; ch < im->channels; ++ch) {
460 IM_WORK_T work = (destc->channel[ch] * (IM_SAMPLE_MAX - *src)
461 + srcc->channel[ch] * *src) / IM_SAMPLE_MAX;
462 destc->channel[ch] = IM_LIMIT(work);
463 }
464 }
465
466 ++srcc;
467 ++destc;
468 ++src;
469 --work_width;
470 }
471 IM_PLIN(im, x, x+width, y, r->IM_SUFFIX(line));
472 }
473 else {
474 IM_PLIN(im, x, x+width, y, line);
475 }
476 }
477 }
478
479 static
480 void
481 IM_SUFFIX(render_color_13)(i_render *r, i_img_dim x, i_img_dim y,
482 i_img_dim width, unsigned char const *src,
483 i_color const *color) {
484 i_img *im = r->im;
485 IM_COLOR *linep = r->IM_SUFFIX(line);
486 int ch, channels = im->channels;
487 i_img_dim fetch_offset;
488 int color_alpha = color->channel[im->channels];
489 #undef STORE_COLOR
490 #ifdef IM_EIGHT_BIT
491 #define STORE_COLOR (*color)
492 #else
493 i_fcolor fcolor;
494
495 for (ch = 0; ch < channels; ++ch) {
496 fcolor.channel[ch] = color->channel[ch] / 255.0;
497 }
498 #define STORE_COLOR fcolor
499 #endif
500
501 fetch_offset = 0;
502 if (color_alpha == 0xFF) {
503 while (fetch_offset < width && *src == 0xFF) {
504 *linep++ = STORE_COLOR;
505 ++src;
506 ++fetch_offset;
507 }
508 }
509 IM_GLIN(im, x+fetch_offset, x+width, y, linep);
510 while (fetch_offset < width) {
511 #ifdef IM_EIGHT_BIT
512 IM_WORK_T alpha = *src++ * color_alpha / 255;
513 #else
514 IM_WORK_T alpha = *src++ * color_alpha / (255.0 * 255.0);
515 #endif
516 if (alpha == IM_SAMPLE_MAX)
517 *linep = STORE_COLOR;
518 else if (alpha) {
519 for (ch = 0; ch < channels; ++ch) {
520 linep->channel[ch] = (linep->channel[ch] * (IM_SAMPLE_MAX - alpha)
521 + STORE_COLOR.channel[ch] * alpha) / IM_SAMPLE_MAX;
522 }
523 }
524 ++linep;
525 ++fetch_offset;
526 }
527 IM_PLIN(im, x, x+width, y, r->IM_SUFFIX(line));
528 }
529
530 static
531 void
532 IM_SUFFIX(render_color_alpha)(i_render *r, i_img_dim x, i_img_dim y,
533 i_img_dim width, unsigned char const *src,
534 i_color const *color) {
535 IM_COLOR *linep = r->IM_SUFFIX(line);
536 int ch;
537 int alpha_channel = r->im->channels - 1;
538 i_img_dim fetch_offset;
539 int color_alpha = color->channel[alpha_channel];
540 #undef STORE_COLOR
541 #ifdef IM_EIGHT_BIT
542 #define STORE_COLOR (*color)
543 #else
544 i_fcolor fcolor;
545
546 for (ch = 0; ch < r->im->channels; ++ch) {
547 fcolor.channel[ch] = color->channel[ch] / 255.0;
548 }
549 #define STORE_COLOR fcolor
550 #endif
551
552 fetch_offset = 0;
553 if (color->channel[alpha_channel] == 0xFF) {
554 while (fetch_offset < width && *src == 0xFF) {
555 *linep++ = STORE_COLOR;
556 ++src;
557 ++fetch_offset;
558 }
559 }
560 IM_GLIN(r->im, x+fetch_offset, x+width, y, linep);
561 while (fetch_offset < width) {
562 #ifdef IM_EIGHT_BIT
563 IM_WORK_T src_alpha = *src++ * color_alpha / 255;
564 #else
565 IM_WORK_T src_alpha = *src++ * color_alpha / (255.0 * 255.0);
566 #endif
567 if (src_alpha == IM_SAMPLE_MAX)
568 *linep = STORE_COLOR;
569 else if (src_alpha) {
570 IM_WORK_T remains = IM_SAMPLE_MAX - src_alpha;
571 IM_WORK_T orig_alpha = linep->channel[alpha_channel];
572 IM_WORK_T dest_alpha = src_alpha + (remains * orig_alpha) / IM_SAMPLE_MAX;
573 for (ch = 0; ch < alpha_channel; ++ch) {
574 linep->channel[ch] = ( src_alpha * STORE_COLOR.channel[ch]
575 + remains * linep->channel[ch] * orig_alpha / IM_SAMPLE_MAX
576 ) / dest_alpha;
577 }
578 linep->channel[alpha_channel] = dest_alpha;
579 }
580 ++linep;
581 ++fetch_offset;
582 }
583 IM_PLIN(r->im, x, x+width, y, r->IM_SUFFIX(line));
584 #undef STORE_COLOR
585 }
586
587 /* combine a line of image data with an output line, both the input
588 and output lines include an alpha channel.
589
590 Both input and output lines have I<channels> of data, channels
591 should be either 2 or 4.
592 */
593
594 static void
595 IM_SUFFIX(combine_line_alpha)(IM_COLOR *out, IM_COLOR const *in,
596 int channels, i_img_dim count) {
597 int ch;
598 int alpha_channel = channels - 1;
599
600 while (count) {
601 IM_WORK_T src_alpha = in->channel[alpha_channel];
602
603 if (src_alpha == IM_SAMPLE_MAX)
604 *out = *in;
605 else if (src_alpha) {
606 IM_WORK_T remains = IM_SAMPLE_MAX - src_alpha;
607 IM_WORK_T orig_alpha = out->channel[alpha_channel];
608 IM_WORK_T dest_alpha = src_alpha + (remains * orig_alpha) / IM_SAMPLE_MAX;
609
610 for (ch = 0; ch < alpha_channel; ++ch) {
611 out->channel[ch] = ( src_alpha * in->channel[ch]
612 + remains * out->channel[ch] * orig_alpha / IM_SAMPLE_MAX
613 ) / dest_alpha;
614 }
615 out->channel[alpha_channel] = dest_alpha;
616 }
617
618 ++out;
619 ++in;
620 --count;
621 }
622 }
623
624 /* combine a line of image data with an output line. The input line
625 includes an alpha channel, the output line has no alpha channel.
626
627 The input line has I<channels>+1 of color data. The output line
628 has I<channels> of color data.
629 */
630
631 static void
632 IM_SUFFIX(combine_line_noalpha)
633 (IM_COLOR *out, IM_COLOR const *in, int channels, i_img_dim count) {
634 int ch;
635
636 while (count) {
637 IM_WORK_T src_alpha = in->channel[channels];
638
639 if (src_alpha == IM_SAMPLE_MAX)
640 *out = *in;
641 else if (src_alpha) {
642 IM_WORK_T remains;
643
644 remains = IM_SAMPLE_MAX - src_alpha;
645 for (ch = 0; ch < channels; ++ch) {
646 out->channel[ch] = ( in->channel[ch] * src_alpha
647 + out->channel[ch] * remains) / IM_SAMPLE_MAX;
648 }
649 }
650
651 ++out;
652 ++in;
653 --count;
654 }
655 }
656
657 /* combine a line of image data with an output line, both the input
658 and output lines include an alpha channel.
659
660 Both input and output lines have I<channels> of data, channels
661 should be either 2 or 4.
662
663 This variant does not modify the output alpha channel.
664 */
665
666 static void
667 IM_SUFFIX(combine_line_alpha_na)(IM_COLOR *out, IM_COLOR const *in,
668 int channels, i_img_dim count) {
669 int ch;
670 int alpha_channel = channels - 1;
671
672 while (count) {
673 IM_WORK_T src_alpha = in->channel[alpha_channel];
674
675 if (src_alpha == IM_SAMPLE_MAX)
676 *out = *in;
677 else if (src_alpha) {
678 IM_WORK_T remains = IM_SAMPLE_MAX - src_alpha;
679 IM_WORK_T orig_alpha = out->channel[alpha_channel];
680 IM_WORK_T dest_alpha = src_alpha + (remains * orig_alpha) / IM_SAMPLE_MAX;
681
682 for (ch = 0; ch < alpha_channel; ++ch) {
683 out->channel[ch] = ( src_alpha * in->channel[ch]
684 + remains * out->channel[ch] * orig_alpha / IM_SAMPLE_MAX
685 ) / dest_alpha;
686 }
687 }
688
689 ++out;
690 ++in;
691 --count;
692 }
693 }
694
695 static void
696 IM_SUFFIX(combine_line)(IM_COLOR *out, IM_COLOR const *in, int channels, i_img_dim count) {
697 if (channels == 2 || channels == 4)
698 IM_SUFFIX(combine_line_alpha)(out, in, channels, count);
699 else
700 IM_SUFFIX(combine_line_noalpha)(out, in, channels, count);
701 }
702
703 static void
704 IM_SUFFIX(combine_line_na)(IM_COLOR *out, IM_COLOR const *in, int channels, i_img_dim count) {
705 if (channels == 2 || channels == 4)
706 IM_SUFFIX(combine_line_alpha_na)(out, in, channels, count);
707 else
708 IM_SUFFIX(combine_line_noalpha)(out, in, channels, count);
709 }
710
711 static void IM_SUFFIX(combine_alphablend)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
712 static void IM_SUFFIX(combine_mult)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
713 static void IM_SUFFIX(combine_dissolve)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
714 static void IM_SUFFIX(combine_add)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
715 static void IM_SUFFIX(combine_subtract)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
716 static void IM_SUFFIX(combine_diff)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
717 static void IM_SUFFIX(combine_darken)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
718 static void IM_SUFFIX(combine_lighten)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
719 static void IM_SUFFIX(combine_hue)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
720 static void IM_SUFFIX(combine_sat)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
721 static void IM_SUFFIX(combine_value)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
722 static void IM_SUFFIX(combine_color)(IM_COLOR *, IM_COLOR *, int, i_img_dim);
723
724 static const IM_FILL_COMBINE_F IM_SUFFIX(combines)[] =
725 {
726 NULL,
727 IM_SUFFIX(combine_alphablend),
728 IM_SUFFIX(combine_mult),
729 IM_SUFFIX(combine_dissolve),
730 IM_SUFFIX(combine_add),
731 IM_SUFFIX(combine_subtract),
732 IM_SUFFIX(combine_diff),
733 IM_SUFFIX(combine_lighten),
734 IM_SUFFIX(combine_darken),
735 IM_SUFFIX(combine_hue),
736 IM_SUFFIX(combine_sat),
737 IM_SUFFIX(combine_value),
738 IM_SUFFIX(combine_color)
739 };
740
741 #/code
742
743 /*
744 =item i_get_combine(combine, color_func, fcolor_func)
745
746 =cut
747 */
748
749 void i_get_combine(int combine, i_fill_combine_f *color_func,
750 i_fill_combinef_f *fcolor_func) {
751 if (combine < 0
752 || (size_t)combine >= sizeof(combines_8) / sizeof(*combines_8)) {
753 combine = 0;
754 }
755
756 *color_func = combines_8[combine];
757 *fcolor_func = combines_double[combine];
758 }
759
760 #code
761
762 /*
763 Three good references for implementing combining modes:
764
765 http://www.w3.org/TR/2004/WD-SVG12-20041027/rendering.html
766 referenced as [svg1.2]
767
768 http://gimp-savvy.com/BOOK/index.html?node55.html
769 ("The Blending Modes", if it changes)
770 referenced as [savvy]
771
772 http://www.pegtop.net/delphi/articles/blendmodes/
773 referenced as [pegtop]
774
775 Where differences exist, I follow the SVG practice, the gimp
776 practice, and lastly pegtop.
777 */
778
779
780 static void
781 IM_SUFFIX(combine_alphablend)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
782 IM_SUFFIX(combine_line)(out, in, channels, count);
783 }
784
785 /*
786 Dca' = Sca.Dca + Sca.(1 - Da) + Dca.(1 - Sa)
787 Da' = Sa.Da + Sa.(1 - Da) + Da.(1 - Sa)
788 = Sa + Da - Sa.Da
789
790 When Da=1
791
792 Dc' = Sc.Sa.Dc + Dc.(1 - Sa)
793 */
794 static void
795 IM_SUFFIX(combine_mult)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
796 int ch;
797 IM_COLOR *inp = in;
798 IM_COLOR *outp = out;
799 i_img_dim work_count = count;
800 int color_channels = i_color_channels(channels);
801
802 if (i_has_alpha(channels)) {
803 while (work_count--) {
804 IM_WORK_T orig_alpha = outp->channel[color_channels];
805 IM_WORK_T src_alpha = inp->channel[color_channels];
806
807 if (src_alpha) {
808 IM_WORK_T dest_alpha = src_alpha + orig_alpha
809 - (src_alpha * orig_alpha) / IM_SAMPLE_MAX;
810
811 for (ch = 0; ch < color_channels; ++ch) {
812 outp->channel[ch] =
813 (inp->channel[ch] * src_alpha * outp->channel[ch] / IM_SAMPLE_MAX
814 * orig_alpha
815 + inp->channel[ch] * src_alpha * (IM_SAMPLE_MAX - orig_alpha)
816 + outp->channel[ch] * orig_alpha * (IM_SAMPLE_MAX - src_alpha))
817 / IM_SAMPLE_MAX / dest_alpha;
818 }
819 outp->channel[color_channels] = dest_alpha;
820 }
821 ++outp;
822 ++inp;
823 }
824 }
825 else {
826 while (work_count--) {
827 IM_WORK_T src_alpha = inp->channel[color_channels];
828 IM_WORK_T remains = IM_SAMPLE_MAX - src_alpha;
829
830 if (src_alpha) {
831 for (ch = 0; ch < color_channels; ++ch) {
832 outp->channel[ch] =
833 (src_alpha * inp->channel[ch] * outp->channel[ch] / IM_SAMPLE_MAX
834 + outp->channel[ch] * remains) / IM_SAMPLE_MAX;
835 }
836 }
837 ++outp;
838 ++inp;
839 }
840 }
841 }
842
843 static void
844 IM_SUFFIX(combine_dissolve)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
845 int color_channels = i_color_channels(channels);
846 int ch;
847
848 if (i_has_alpha(channels)) {
849 while (count--) {
850 if (in->channel[channels-1] > rand() * ((double)IM_SAMPLE_MAX / RAND_MAX)) {
851 for (ch = 0; ch < color_channels; ++ch) {
852 out->channel[ch] = in->channel[ch];
853 }
854 out->channel[color_channels] = IM_SAMPLE_MAX;
855 }
856 ++out;
857 ++in;
858 }
859 }
860 else {
861 while (count--) {
862 if (in->channel[channels] > rand() * ((double)IM_SAMPLE_MAX / RAND_MAX)) {
863 for (ch = 0; ch < color_channels; ++ch) {
864 out->channel[ch] = in->channel[ch];
865 }
866 }
867 ++out;
868 ++in;
869 }
870 }
871 }
872
873 /*
874 Dca' = Sca.Da + Dca.Sa + Sca.(1 - Da) + Dca.(1 - Sa)
875 = Sca + Dca
876 Da' = Sa.Da + Da.Sa + Sa.(1 - Da) + Da.(1 - Sa)
877 = Sa + Da
878 */
879
880 static void
881 IM_SUFFIX(combine_add)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
882 int ch;
883 int color_channels = i_color_channels(channels);
884 i_img_dim work_count = count;
885 IM_COLOR *inp = in;
886 IM_COLOR *outp = out;
887
888 if (i_has_alpha(channels)) {
889 while (work_count--) {
890 IM_WORK_T src_alpha = inp->channel[color_channels];
891 if (src_alpha) {
892 IM_WORK_T orig_alpha = outp->channel[color_channels];
893 IM_WORK_T dest_alpha = src_alpha + orig_alpha;
894 if (dest_alpha > IM_SAMPLE_MAX)
895 dest_alpha = IM_SAMPLE_MAX;
896 for (ch = 0; ch < color_channels; ++ch) {
897 IM_WORK_T total = (outp->channel[ch] * orig_alpha + inp->channel[ch] * src_alpha) / dest_alpha;
898 if (total > IM_SAMPLE_MAX)
899 total = IM_SAMPLE_MAX;
900 outp->channel[ch] = total;
901 }
902 outp->channel[color_channels] = dest_alpha;
903 }
904
905 ++outp;
906 ++inp;
907 }
908 }
909 else {
910 while (work_count--) {
911 IM_WORK_T src_alpha = inp->channel[color_channels];
912 if (src_alpha) {
913 for (ch = 0; ch < color_channels; ++ch) {
914 IM_WORK_T total = outp->channel[ch] + inp->channel[ch] * src_alpha / IM_SAMPLE_MAX;
915 if (total > IM_SAMPLE_MAX)
916 total = IM_SAMPLE_MAX;
917 outp->channel[ch] = total;
918 }
919 }
920
921 ++outp;
922 ++inp;
923 }
924 }
925 }
926
927 /*
928 [pegtop] documents this as max(A+B-256, 0) while [savvy] documents
929 it as max(A-B, 0). [svg1.2] doesn't cover it.
930
931 [savvy] doesn't document how it works with an alpha channel. GIMP
932 actually seems to calculate the final value then use the alpha
933 channel to apply that to the target.
934 */
935 static void
936 IM_SUFFIX(combine_subtract)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
937 int ch;
938 IM_COLOR const *inp = in;
939 IM_COLOR *outp = out;
940 i_img_dim work_count = count;
941 int color_channels = i_color_channels(channels);
942
943 if (i_has_alpha(channels)) {
944 while (work_count--) {
945 IM_WORK_T src_alpha = inp->channel[color_channels];
946 if (src_alpha) {
947 IM_WORK_T orig_alpha = outp->channel[color_channels];
948 IM_WORK_T dest_alpha = src_alpha + orig_alpha;
949 if (dest_alpha > IM_SAMPLE_MAX)
950 dest_alpha = IM_SAMPLE_MAX;
951 for (ch = 0; ch < color_channels; ++ch) {
952 IM_WORK_T total =
953 (outp->channel[ch] * orig_alpha - inp->channel[ch] * src_alpha)
954 / dest_alpha;
955 if (total < 0)
956 total = 0;
957 outp->channel[ch] = total;
958 }
959 outp->channel[color_channels] = dest_alpha;
960 }
961 ++outp;
962 ++inp;
963 }
964 }
965 else {
966 while (work_count--) {
967 IM_WORK_T src_alpha = inp->channel[color_channels];
968 if (src_alpha) {
969 for (ch = 0; ch < color_channels; ++ch) {
970 IM_WORK_T total = outp->channel[ch] - inp->channel[ch] * src_alpha / IM_SAMPLE_MAX;
971 if (total < 0)
972 total = 0;
973 outp->channel[ch] = total;
974 }
975 }
976 ++outp;
977 ++inp;
978 }
979 }
980 }
981
982 #ifdef IM_EIGHT_BIT
983 #define IM_abs(x) abs(x)
984 #else
985 #define IM_abs(x) fabs(x)
986 #endif
987
988 /*
989 Dca' = abs(Dca.Sa - Sca.Da) + Sca.(1 - Da) + Dca.(1 - Sa)
990 = Sca + Dca - 2.min(Sca.Da, Dca.Sa)
991 Da' = Sa + Da - Sa.Da
992 */
993 static void
994 IM_SUFFIX(combine_diff)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
995 int ch;
996 IM_COLOR const *inp = in;
997 IM_COLOR *outp = out;
998 i_img_dim work_count = count;
999 int color_channels = i_color_channels(channels);
1000
1001 if (i_has_alpha(channels)) {
1002 while (work_count--) {
1003 IM_WORK_T src_alpha = inp->channel[color_channels];
1004 if (src_alpha) {
1005 IM_WORK_T orig_alpha = outp->channel[color_channels];
1006 IM_WORK_T dest_alpha = src_alpha + orig_alpha
1007 - src_alpha * orig_alpha / IM_SAMPLE_MAX;
1008 for (ch = 0; ch < color_channels; ++ch) {
1009 IM_WORK_T src = inp->channel[ch] * src_alpha;
1010 IM_WORK_T orig = outp->channel[ch] * orig_alpha;
1011 IM_WORK_T src_da = src * orig_alpha;
1012 IM_WORK_T dest_sa = orig * src_alpha;
1013 IM_WORK_T diff = src_da < dest_sa ? src_da : dest_sa;
1014 outp->channel[ch] = (src + orig - 2 * diff / IM_SAMPLE_MAX) / dest_alpha;
1015 }
1016 outp->channel[color_channels] = dest_alpha;
1017 }
1018 ++inp;
1019 ++outp;
1020 }
1021 }
1022 else {
1023 while (work_count--) {
1024 IM_WORK_T src_alpha = inp->channel[color_channels];
1025 if (src_alpha) {
1026 for (ch = 0; ch < color_channels; ++ch) {
1027 IM_WORK_T src = inp->channel[ch] * src_alpha;
1028 IM_WORK_T orig = outp->channel[ch] * IM_SAMPLE_MAX;
1029 IM_WORK_T src_da = src * IM_SAMPLE_MAX;
1030 IM_WORK_T dest_sa = orig * src_alpha;
1031 IM_WORK_T diff = src_da < dest_sa ? src_da : dest_sa;
1032 outp->channel[ch] = (src + orig - 2 * diff / IM_SAMPLE_MAX) / IM_SAMPLE_MAX;
1033 }
1034 }
1035 ++inp;
1036 ++outp;
1037 }
1038 }
1039 }
1040
1041 #undef IM_abs
1042
1043 /*
1044 Dca' = min(Sca.Da, Dca.Sa) + Sca.(1 - Da) + Dca(1 - Sa)
1045 Da' = Sa + Da - Sa.Da
1046
1047 To hoist some code:
1048
1049 Dca' = min(Sc.Sa.Da, Dc.Da.Sa) + Sca - Sca.Da + Dca - Dca.Sa
1050 = Sa.Da.min(Sc, Dc) + Sca - Sca.Da + Dca - Dca.Sa
1051
1052 When Da=1:
1053
1054 Dca' = min(Sca.1, Dc.1.Sa) + Sca.(1 - 1) + Dc.1(1 - Sa)
1055 = min(Sca, Dc.Sa) + Dc(1-Sa)
1056 = Sa.min(Sc, Dc) + Dc - Dc.Sa
1057 Da' = Sa + 1 - Sa.1
1058 = 1
1059 */
1060 static void
1061 IM_SUFFIX(combine_darken)(IM_COLOR *out, IM_COLOR *in, int channels,
1062 i_img_dim count) {
1063 int ch;
1064 IM_COLOR const *inp = in;
1065 IM_COLOR *outp = out;
1066 i_img_dim work_count = count;
1067 int color_channels = i_color_channels(channels);
1068
1069 if (i_has_alpha(channels)) {
1070 while (work_count--) {
1071 IM_WORK_T src_alpha = inp->channel[color_channels];
1072
1073 if (src_alpha) {
1074 IM_WORK_T orig_alpha = outp->channel[color_channels];
1075 IM_WORK_T dest_alpha = src_alpha + orig_alpha
1076 - src_alpha * orig_alpha / IM_SAMPLE_MAX;
1077 for (ch = 0; ch < color_channels; ++ch) {
1078 IM_WORK_T Sca = inp->channel[ch] * src_alpha;
1079 IM_WORK_T Dca = outp->channel[ch] * orig_alpha;
1080 IM_WORK_T ScaDa = Sca * orig_alpha;
1081 IM_WORK_T DcaSa = Dca * src_alpha;
1082 IM_WORK_T minc = ScaDa < DcaSa ? ScaDa : DcaSa;
1083 outp->channel[ch] =
1084 (
1085 minc + (Sca + Dca) * IM_SAMPLE_MAX
1086 - ScaDa - DcaSa
1087 ) / (IM_SAMPLE_MAX * dest_alpha);
1088 }
1089 outp->channel[color_channels] = dest_alpha;
1090 }
1091 ++outp;
1092 ++inp;
1093 }
1094 }
1095 else {
1096 while (work_count--) {
1097 IM_WORK_T src_alpha = inp->channel[color_channels];
1098
1099 if (src_alpha) {
1100 for (ch = 0; ch < color_channels; ++ch) {
1101 IM_WORK_T minc = outp->channel[ch] < inp->channel[ch]
1102 ? outp->channel[ch] : inp->channel[ch];
1103 outp->channel[ch] =
1104 (
1105 src_alpha * minc +
1106 outp->channel[ch] * ( IM_SAMPLE_MAX - src_alpha )
1107 ) / IM_SAMPLE_MAX;
1108 }
1109 }
1110 ++outp;
1111 ++inp;
1112 }
1113 }
1114 }
1115
1116 static void
1117 IM_SUFFIX(combine_lighten)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
1118 int ch;
1119 IM_COLOR const *inp = in;
1120 IM_COLOR *outp = out;
1121 i_img_dim work_count = count;
1122 int color_channels = i_color_channels(channels);
1123
1124 if (i_has_alpha(channels)) {
1125 while (work_count--) {
1126 IM_WORK_T src_alpha = inp->channel[color_channels];
1127
1128 if (src_alpha) {
1129 IM_WORK_T orig_alpha = outp->channel[color_channels];
1130 IM_WORK_T dest_alpha = src_alpha + orig_alpha
1131 - src_alpha * orig_alpha / IM_SAMPLE_MAX;
1132 for (ch = 0; ch < color_channels; ++ch) {
1133 IM_WORK_T Sca = inp->channel[ch] * src_alpha;
1134 IM_WORK_T Dca = outp->channel[ch] * orig_alpha;
1135 IM_WORK_T ScaDa = Sca * orig_alpha;
1136 IM_WORK_T DcaSa = Dca * src_alpha;
1137 IM_WORK_T maxc = ScaDa > DcaSa ? ScaDa : DcaSa;
1138 outp->channel[ch] =
1139 (
1140 maxc + (Sca + Dca) * IM_SAMPLE_MAX
1141 - ScaDa - DcaSa
1142 ) / (IM_SAMPLE_MAX * dest_alpha);
1143 }
1144 outp->channel[color_channels] = dest_alpha;
1145 }
1146 ++outp;
1147 ++inp;
1148 }
1149 }
1150 else {
1151 while (work_count--) {
1152 IM_WORK_T src_alpha = inp->channel[color_channels];
1153
1154 if (src_alpha) {
1155 for (ch = 0; ch < color_channels; ++ch) {
1156 IM_WORK_T maxc = outp->channel[ch] > inp->channel[ch]
1157 ? outp->channel[ch] : inp->channel[ch];
1158 outp->channel[ch] =
1159 (
1160 src_alpha * maxc +
1161 outp->channel[ch] * ( IM_SAMPLE_MAX - src_alpha )
1162 ) / IM_SAMPLE_MAX;
1163 }
1164 }
1165 ++outp;
1166 ++inp;
1167 }
1168 }
1169 }
1170
1171 #if IM_EIGHT_BIT
1172 #define IM_RGB_TO_HSV i_rgb_to_hsv
1173 #define IM_HSV_TO_RGB i_hsv_to_rgb
1174 #else
1175 #define IM_RGB_TO_HSV i_rgb_to_hsvf
1176 #define IM_HSV_TO_RGB i_hsv_to_rgbf
1177 #endif
1178
1179 static void
1180 IM_SUFFIX(combine_hue)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
1181 if (channels > 2) {
1182 IM_COLOR *inp = in;
1183 IM_COLOR const *outp = out;
1184 i_img_dim work_count = count;
1185
1186 if (i_has_alpha(channels)) {
1187 while (work_count--) {
1188 IM_COLOR c = *inp;
1189 IM_RGB_TO_HSV(&c);
1190 /* only transfer hue if there's saturation */
1191 if (c.channel[1] && inp->channel[3] && outp->channel[3]) {
1192 *inp = *outp;
1193 IM_RGB_TO_HSV(inp);
1194 /* and no point in setting the target hue if the target has no sat */
1195 if (inp->channel[1]) {
1196 inp->channel[0] = c.channel[0];
1197 IM_HSV_TO_RGB(inp);
1198 inp->channel[3] = c.channel[3];
1199 }
1200 else {
1201 inp->channel[3] = 0;
1202 }
1203 }
1204 else {
1205 inp->channel[3] = 0;
1206 }
1207
1208 ++outp;
1209 ++inp;
1210 }
1211 }
1212 else {
1213 while (work_count--) {
1214 IM_COLOR c = *inp;
1215 IM_RGB_TO_HSV(&c);
1216 /* only transfer hue if there's saturation */
1217 if (c.channel[1] && inp->channel[3]) {
1218 *inp = *outp;
1219 IM_RGB_TO_HSV(inp);
1220 /* and no point in setting the target hue if the target has no sat */
1221 if (inp->channel[1]) {
1222 inp->channel[0] = c.channel[0];
1223 IM_HSV_TO_RGB(inp);
1224 inp->channel[3] = c.channel[3];
1225 }
1226 }
1227 else {
1228 inp->channel[3] = 0;
1229 }
1230
1231 ++outp;
1232 ++inp;
1233 }
1234 }
1235
1236 IM_SUFFIX(combine_line_na)(out, in, channels, count);
1237 }
1238 }
1239
1240 static void
1241 IM_SUFFIX(combine_sat)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
1242 if (channels > 2) {
1243 IM_COLOR *inp = in;
1244 IM_COLOR const *outp = out;
1245 i_img_dim work_count = count;
1246
1247 while (work_count--) {
1248 IM_COLOR c = *inp;
1249 *inp = *outp;
1250 IM_RGB_TO_HSV(&c);
1251 IM_RGB_TO_HSV(inp);
1252 inp->channel[1] = c.channel[1];
1253 IM_HSV_TO_RGB(inp);
1254 inp->channel[3] = c.channel[3];
1255 ++outp;
1256 ++inp;
1257 }
1258
1259 IM_SUFFIX(combine_line_na)(out, in, channels, count);
1260 }
1261 }
1262
1263 static void
1264 IM_SUFFIX(combine_value)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
1265 if (channels > 2) {
1266 IM_COLOR *inp = in;
1267 IM_COLOR const *outp = out;
1268 i_img_dim work_count = count;
1269
1270 while (work_count--) {
1271 IM_COLOR c = *inp;
1272 *inp = *outp;
1273 IM_RGB_TO_HSV(&c);
1274 IM_RGB_TO_HSV(inp);
1275 inp->channel[2] = c.channel[2];
1276 IM_HSV_TO_RGB(inp);
1277 inp->channel[3] = c.channel[3];
1278 ++outp;
1279 ++inp;
1280 }
1281 }
1282
1283 /* all images have a "value channel" - for greyscale it's the only
1284 colour channel */
1285 IM_SUFFIX(combine_line_na)(out, in, channels, count);
1286 }
1287
1288 static void
1289 IM_SUFFIX(combine_color)(IM_COLOR *out, IM_COLOR *in, int channels, i_img_dim count) {
1290 if (channels > 2) {
1291 IM_COLOR *inp = in;
1292 IM_COLOR const *outp = out;
1293 i_img_dim work_count = count;
1294
1295 while (work_count--) {
1296 IM_COLOR c = *inp;
1297 *inp = *outp;
1298 IM_RGB_TO_HSV(&c);
1299 IM_RGB_TO_HSV(inp);
1300 inp->channel[0] = c.channel[0];
1301 inp->channel[1] = c.channel[1];
1302 IM_HSV_TO_RGB(inp);
1303 inp->channel[3] = c.channel[3];
1304 ++outp;
1305 ++inp;
1306 }
1307
1308 IM_SUFFIX(combine_line_na)(out, in, channels, count);
1309 }
1310 }
1311
1312 #undef IM_RGB_TO_HSV
1313 #undef IM_HSV_TO_RGB
1314
1315 #/code