aboutsummaryrefslogtreecommitdiffstats
path: root/libart_lgpl/art_render.c
blob: 6c71e4a260bb25b6570c4e51174671ba1910337f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*
 * art_render.c: Modular rendering architecture.
 *
 * Libart_LGPL - library of basic graphic primitives
 * Copyright (C) 2000 Raph Levien
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include "art_render.h"

#include "art_rgb.h"

typedef struct _ArtRenderPriv ArtRenderPriv;

struct _ArtRenderPriv {
  ArtRender super;

  ArtImageSource *image_source;

  gint n_mask_source;
  ArtMaskSource **mask_source;

  gint n_callbacks;
  ArtRenderCallback **callbacks;
};

static void
art_render_nop_done (ArtRenderCallback *self, ArtRender *render)
{
}

static void
art_render_clear_render_rgb8 (ArtRenderCallback *self, ArtRender *render,
                  art_u8 *dest, gint y)
{
  gint width = render->x1 - render->x0;
  art_u8 r, g, b;
  ArtPixMaxDepth color_max;

  color_max = render->clear_color[0];
  r = ART_PIX_8_FROM_MAX (color_max);
  color_max = render->clear_color[1];
  g = ART_PIX_8_FROM_MAX (color_max);
  color_max = render->clear_color[2];
  b = ART_PIX_8_FROM_MAX (color_max);

  art_rgb_fill_run (dest, r, g, b, width);
}

static void
art_render_clear_render_8 (ArtRenderCallback *self, ArtRender *render,
               art_u8 *dest, gint y)
{
  gint width = render->x1 - render->x0;
  gint i, j;
  gint n_ch = render->n_chan + (render->alpha_type != ART_ALPHA_NONE);
  gint ix;
  art_u8 color[ART_MAX_CHAN + 1];

  for (j = 0; j < n_ch; j++)
    {
      ArtPixMaxDepth color_max = render->clear_color[j];
      color[j] = ART_PIX_8_FROM_MAX (color_max);
    }

  ix = 0;
  for (i = 0; i < width; i++)
    for (j = 0; j < n_ch; j++)
      dest[ix++] = color[j];
}

const ArtRenderCallback art_render_clear_rgb8_obj =
{
  art_render_clear_render_rgb8,
  art_render_nop_done
};

const ArtRenderCallback art_render_clear_8_obj =
{
  art_render_clear_render_8,
  art_render_nop_done
};

#if ART_MAX_DEPTH >= 16

static void
art_render_clear_render_16 (ArtRenderCallback *self, ArtRender *render,
                art_u8 *dest, gint y)
{
  gint width = render->x1 - render->x0;
  gint i, j;
  gint n_ch = render->n_chan + (render->alpha_type != ART_ALPHA_NONE);
  gint ix;
  art_u16 *dest_16 = (art_u16 *)dest;
  art_u8 color[ART_MAX_CHAN + 1];

  for (j = 0; j < n_ch; j++)
    {
      gint color_16 = render->clear_color[j];
      color[j] = color_16;
    }

  ix = 0;
  for (i = 0; i < width; i++)
    for (j = 0; j < n_ch; j++)
      dest_16[ix++] = color[j];
}

const ArtRenderCallback art_render_clear_16_obj =
{
  art_render_clear_render_16,
  art_render_nop_done
};

#endif /* ART_MAX_DEPTH >= 16 */

/* This is the most general form of the function. It is slow but
   (hopefully) correct. Actually, I'm still worried about roundoff
   errors in the premul case - it seems to me that an off-by-one could
   lead to overflow. */
static void
art_render_composite (ArtRenderCallback *self, ArtRender *render,
                    art_u8 *dest, gint y)
{
  ArtRenderMaskRun *run = render->run;
  art_u32 depth = render->depth;
  gint n_run = render->n_run;
  gint x0 = render->x0;
  gint x;
  gint run_x0, run_x1;
  art_u8 *alpha_buf = render->alpha_buf;
  art_u8 *image_buf = render->image_buf;
  gint i, j;
  art_u32 tmp;
  art_u32 run_alpha;
  art_u32 alpha;
  gint image_ix;
  art_u16 src[ART_MAX_CHAN + 1];
  art_u16 dst[ART_MAX_CHAN + 1];
  gint n_chan = render->n_chan;
  ArtAlphaType alpha_type = render->alpha_type;
  gint n_ch = n_chan + (alpha_type != ART_ALPHA_NONE);
  gint dst_pixstride = n_ch * (depth >> 3);
  gint buf_depth = render->buf_depth;
  ArtAlphaType buf_alpha = render->buf_alpha;
  gint buf_n_ch = n_chan + (buf_alpha != ART_ALPHA_NONE);
  gint buf_pixstride = buf_n_ch * (buf_depth >> 3);
  art_u8 *bufptr;
  art_u32 src_alpha;
  art_u32 src_mul;
  art_u8 *dstptr;
  art_u32 dst_alpha;
  art_u32 dst_mul;

  image_ix = 0;
  for (i = 0; i < n_run - 1; i++)
    {
      run_x0 = run[i].x;
      run_x1 = run[i + 1].x;
      tmp = run[i].alpha;
      if (tmp < 0x8100)
    continue;

      run_alpha = (tmp + (tmp >> 8) + (tmp >> 16) - 0x8000) >> 8;
      bufptr = image_buf + (run_x0 - x0) * buf_pixstride;
      dstptr = dest + (run_x0 - x0) * dst_pixstride;
      for (x = run_x0; x < run_x1; x++)
    {
      if (alpha_buf)
        {
          if (depth == 8)
        {
          tmp = run_alpha * alpha_buf[x - x0] + 0x80;
          /* range 0x80 .. 0xff0080 */
          alpha = (tmp + (tmp >> 8) + (tmp >> 16)) >> 8;
        }
          else /* (depth == 16) */
        {
          tmp = ((art_u16 *)alpha_buf)[x - x0];
          tmp = (run_alpha * tmp + 0x8000) >> 8;
          /* range 0x80 .. 0xffff80 */
          alpha = (tmp + (tmp >> 16)) >> 8;
        }
        }
      else
        alpha = run_alpha;
      /* alpha is run_alpha * alpha_buf[x], range 0 .. 0x10000 */

      /* convert (src pixel * alpha) to premul alpha form,
         store in src as 0..0xffff range */
      if (buf_alpha == ART_ALPHA_NONE)
        {
          src_alpha = alpha;
          src_mul = src_alpha;
        }
      else
        {
          if (buf_depth == 8)
        {
          tmp = alpha * bufptr[n_chan] + 0x80;
          /* range 0x80 .. 0xff0080 */
          src_alpha = (tmp + (tmp >> 8) + (tmp >> 16)) >> 8;
        }
          else /* (depth == 16) */
        {
          tmp = ((art_u16 *)bufptr)[n_chan];
          tmp = (alpha * tmp + 0x8000) >> 8;
          /* range 0x80 .. 0xffff80 */
          src_alpha = (tmp + (tmp >> 16)) >> 8;
        }
          if (buf_alpha == ART_ALPHA_SEPARATE)
        src_mul = src_alpha;
          else /* buf_alpha == (ART_ALPHA_PREMUL) */
        src_mul = alpha;
        }
      /* src_alpha is the (alpha of the source pixel * alpha),
         range 0..0x10000 */

      if (buf_depth == 8)
        {
          src_mul *= 0x101;
          for (j = 0; j < n_chan; j++)
        src[j] = (bufptr[j] * src_mul + 0x8000) >> 16;
        }
      else if (buf_depth == 16)
        {
          for (j = 0; j < n_chan; j++)
        src[j] = (((art_u16 *)bufptr)[j] * src_mul + 0x8000) >> 16;
        }
      bufptr += buf_pixstride;

      /* src[0..n_chan - 1] (range 0..0xffff) and src_alpha (range
             0..0x10000) now contain the source pixel with
             premultiplied alpha */

      /* convert dst pixel to premul alpha form,
         store in dst as 0..0xffff range */
      if (alpha_type == ART_ALPHA_NONE)
        {
          dst_alpha = 0x10000;
          dst_mul = dst_alpha;
        }
      else
        {
          if (depth == 8)
        {
          tmp = dstptr[n_chan];
          /* range 0..0xff */
          dst_alpha = (tmp << 8) + tmp + (tmp >> 7);
        }
          else /* (depth == 16) */
        {
          tmp = ((art_u16 *)dstptr)[n_chan];
          dst_alpha = (tmp + (tmp >> 15));
        }
          if (alpha_type == ART_ALPHA_SEPARATE)
        dst_mul = dst_alpha;
          else /* (alpha_type == ART_ALPHA_PREMUL) */
        dst_mul = 0x10000;
        }
      /* dst_alpha is the alpha of the dest pixel,
         range 0..0x10000 */

      if (depth == 8)
        {
          dst_mul *= 0x101;
          for (j = 0; j < n_chan; j++)
        dst[j] = (dstptr[j] * dst_mul + 0x8000) >> 16;
        }
      else if (buf_depth == 16)
        {
          for (j = 0; j < n_chan; j++)
        dst[j] = (((art_u16 *)dstptr)[j] * dst_mul + 0x8000) >> 16;
        }

      /* do the compositing, dst = (src over dst) */
      for (j = 0; j < n_chan; j++)
        {
          art_u32 srcv, dstv;
          art_u32 tmp;

          srcv = src[j];
          dstv = dst[j];
          tmp = ((dstv * (0x10000 - src_alpha) + 0x8000) >> 16) + srcv;
          tmp -= tmp >> 16;
          dst[j] = tmp;
        }

      if (alpha_type == ART_ALPHA_NONE)
        {
          if (depth == 8)
        dst_mul = 0xff;
          else /* (depth == 16) */
        dst_mul = 0xffff;
        }
      else
        {
          if (src_alpha >= 0x10000)
        dst_alpha = 0x10000;
          else
        dst_alpha += ((((0x10000 - dst_alpha) * src_alpha) >> 8) + 0x80) >> 8;
          if (alpha_type == ART_ALPHA_PREMUL || dst_alpha == 0)
        {
          if (depth == 8)
            dst_mul = 0xff;
          else /* (depth == 16) */
            dst_mul = 0xffff;
        }
          else /* (ALPHA_TYPE == ART_ALPHA_SEPARATE && dst_alpha != 0) */
        {
          if (depth == 8)
            dst_mul = 0xff0000 / dst_alpha;
          else /* (depth == 16) */
            dst_mul = 0xffff0000 / dst_alpha;
        }
        }
      if (depth == 8)
        {
          for (j = 0; j < n_chan; j++)
        dstptr[j] = (dst[j] * dst_mul + 0x8000) >> 16;
          if (alpha_type != ART_ALPHA_NONE)
        dstptr[n_chan] = (dst_alpha * 0xff + 0x8000) >> 16;
        }
      else if (depth == 16)
        {
          for (j = 0; j < n_chan; j++)
        ((art_u16 *)dstptr)[j] = (dst[j] * dst_mul + 0x8000) >> 16;
          if (alpha_type != ART_ALPHA_NONE)
        ((art_u16 *)dstptr)[n_chan] = (dst_alpha * 0xffff + 0x8000) >> 16;
        }
      dstptr += dst_pixstride;
    }
    }
}

const ArtRenderCallback art_render_composite_obj =
{
  art_render_composite,
  art_render_nop_done
};

static void
art_render_composite_8 (ArtRenderCallback *self, ArtRender *render,
            art_u8 *dest, gint y)
{
  ArtRenderMaskRun *run = render->run;
  gint n_run = render->n_run;
  gint x0 = render->x0;
  gint x;
  gint run_x0, run_x1;
  art_u8 *alpha_buf = render->alpha_buf;
  art_u8 *image_buf = render->image_buf;
  gint i, j;
  art_u32 tmp;
  art_u32 run_alpha;
  art_u32 alpha;
  gint image_ix;
  gint n_chan = render->n_chan;
  ArtAlphaType alpha_type = render->alpha_type;
  gint n_ch = n_chan + (alpha_type != ART_ALPHA_NONE);
  gint dst_pixstride = n_ch;
  ArtAlphaType buf_alpha = render->buf_alpha;
  gint buf_n_ch = n_chan + (buf_alpha != ART_ALPHA_NONE);
  gint buf_pixstride = buf_n_ch;
  art_u8 *bufptr;
  art_u32 src_alpha;
  art_u32 src_mul;
  art_u8 *dstptr;
  art_u32 dst_alpha;
  art_u32 dst_mul, dst_save_mul;

  image_ix = 0;
  for (i = 0; i < n_run - 1; i++)
    {
      run_x0 = run[i].x;
      run_x1 = run[i + 1].x;
      tmp = run[i].alpha;
      if (tmp < 0x10000)
    continue;

      run_alpha = (tmp + (tmp >> 8) + (tmp >> 16) - 0x8000) >> 8;
      bufptr = image_buf + (run_x0 - x0) * buf_pixstride;
      dstptr = dest + (run_x0 - x0) * dst_pixstride;
      for (x = run_x0; x < run_x1; x++)
    {
      if (alpha_buf)
        {
          tmp = run_alpha * alpha_buf[x - x0] + 0x80;
          /* range 0x80 .. 0xff0080 */
          alpha = (tmp + (tmp >> 8) + (tmp >> 16)) >> 8;
        }
      else
        alpha = run_alpha;
      /* alpha is run_alpha * alpha_buf[x], range 0 .. 0x10000 */

      /* convert (src pixel * alpha) to premul alpha form,
         store in src as 0..0xffff range */
      if (buf_alpha == ART_ALPHA_NONE)
        {
          src_alpha = alpha;
          src_mul = src_alpha;
        }
      else
        {
          tmp = alpha * bufptr[n_chan] + 0x80;
          /* range 0x80 .. 0xff0080 */
          src_alpha = (tmp + (tmp >> 8) + (tmp >> 16)) >> 8;

          if (buf_alpha == ART_ALPHA_SEPARATE)
        src_mul = src_alpha;
          else /* buf_alpha == (ART_ALPHA_PREMUL) */
        src_mul = alpha;
        }
      /* src_alpha is the (alpha of the source pixel * alpha),
         range 0..0x10000 */

      src_mul *= 0x101;

      if (alpha_type == ART_ALPHA_NONE)
        {
          dst_alpha = 0x10000;
          dst_mul = dst_alpha;
        }
      else
        {
          tmp = dstptr[n_chan];
          /* range 0..0xff */
          dst_alpha = (tmp << 8) + tmp + (tmp >> 7);
          if (alpha_type == ART_ALPHA_SEPARATE)
        dst_mul = dst_alpha;
          else /* (alpha_type == ART_ALPHA_PREMUL) */
        dst_mul = 0x10000;
        }
      /* dst_alpha is the alpha of the dest pixel,
         range 0..0x10000 */

      dst_mul *= 0x101;

      if (alpha_type == ART_ALPHA_NONE)
        {
          dst_save_mul = 0xff;
        }
      else
        {
          if (src_alpha >= 0x10000)
        dst_alpha = 0x10000;
          else
        dst_alpha += ((((0x10000 - dst_alpha) * src_alpha) >> 8) + 0x80) >> 8;
          if (alpha_type == ART_ALPHA_PREMUL || dst_alpha == 0)
        {
          dst_save_mul = 0xff;
        }
          else /* (ALPHA_TYPE == ART_ALPHA_SEPARATE && dst_alpha != 0) */
        {
          dst_save_mul = 0xff0000 / dst_alpha;
        }
        }

      for (j = 0; j < n_chan; j++)
        {
          art_u32 src, dst;
          art_u32 tmp;

          src = (bufptr[j] * src_mul + 0x8000) >> 16;
          dst = (dstptr[j] * dst_mul + 0x8000) >> 16;
          tmp = ((dst * (0x10000 - src_alpha) + 0x8000) >> 16) + src;
          tmp -= tmp >> 16;
          dstptr[j] = (tmp * dst_save_mul + 0x8000) >> 16;
        }
      if (alpha_type != ART_ALPHA_NONE)
        dstptr[n_chan] = (dst_alpha * 0xff + 0x8000) >> 16;

      bufptr += buf_pixstride;
      dstptr += dst_pixstride;
    }
    }
}

const ArtRenderCallback art_render_composite_8_obj =
{
  art_render_composite_8,
  art_render_nop_done
};

/* Assumes:
 * alpha_buf is NULL
 * buf_alpha = ART_ALPHA_NONE  (source)
 * alpha_type = ART_ALPHA_SEPARATE (dest)
 * n_chan = 3;
 */
static void
art_render_composite_8_opt1 (ArtRenderCallback *self, ArtRender *render,
                 art_u8 *dest, gint y)
{
  ArtRenderMaskRun *run = render->run;
  gint n_run = render->n_run;
  gint x0 = render->x0;
  gint x;
  gint run_x0, run_x1;
  art_u8 *image_buf = render->image_buf;
  gint i, j;
  art_u32 tmp;
  art_u32 run_alpha;
  gint image_ix;
  art_u8 *bufptr;
  art_u32 src_mul;
  art_u8 *dstptr;
  art_u32 dst_alpha;
  art_u32 dst_mul, dst_save_mul;

  image_ix = 0;
  for (i = 0; i < n_run - 1; i++)
    {
      run_x0 = run[i].x;
      run_x1 = run[i + 1].x;
      tmp = run[i].alpha;
      if (tmp < 0x10000)
    continue;

      run_alpha = (tmp + (tmp >> 8) + (tmp >> 16) - 0x8000) >> 8;
      bufptr = image_buf + (run_x0 - x0) * 3;
      dstptr = dest + (run_x0 - x0) * 4;
      if (run_alpha == 0x10000)
    {
      for (x = run_x0; x < run_x1; x++)
        {
          *dstptr++ = *bufptr++;
          *dstptr++ = *bufptr++;
          *dstptr++ = *bufptr++;
          *dstptr++ = 0xff;
        }
    }
      else
    {
      for (x = run_x0; x < run_x1; x++)
        {
          src_mul = run_alpha * 0x101;

          tmp = dstptr[3];
          /* range 0..0xff */
          dst_alpha = (tmp << 8) + tmp + (tmp >> 7);
          dst_mul = dst_alpha;
          /* dst_alpha is the alpha of the dest pixel,
         range 0..0x10000 */

          dst_mul *= 0x101;

          dst_alpha += ((((0x10000 - dst_alpha) * run_alpha) >> 8) + 0x80) >> 8;
          if (dst_alpha == 0)
          dst_save_mul = 0xff;
          else /* (dst_alpha != 0) */
          dst_save_mul = 0xff0000 / dst_alpha;

          for (j = 0; j < 3; j++)
        {
          art_u32 src, dst;
          art_u32 tmp;

          src = (bufptr[j] * src_mul + 0x8000) >> 16;
          dst = (dstptr[j] * dst_mul + 0x8000) >> 16;
          tmp = ((dst * (0x10000 - run_alpha) + 0x8000) >> 16) + src;
          tmp -= tmp >> 16;
          dstptr[j] = (tmp * dst_save_mul + 0x8000) >> 16;
        }
          dstptr[3] = (dst_alpha * 0xff + 0x8000) >> 16;

          bufptr += 3;
          dstptr += 4;
        }
    }
    }
}

const ArtRenderCallback art_render_composite_8_opt1_obj =
{
  art_render_composite_8_opt1,
  art_render_nop_done
};

/* Assumes:
 * alpha_buf is NULL
 * buf_alpha = ART_ALPHA_PREMUL  (source)
 * alpha_type = ART_ALPHA_SEPARATE (dest)
 * n_chan = 3;
 */
static void
art_render_composite_8_opt2 (ArtRenderCallback *self, ArtRender *render,
                 art_u8 *dest, gint y)
{
  ArtRenderMaskRun *run = render->run;
  gint n_run = render->n_run;
  gint x0 = render->x0;
  gint x;
  gint run_x0, run_x1;
  art_u8 *image_buf = render->image_buf;
  gint i, j;
  art_u32 tmp;
  art_u32 run_alpha;
  gint image_ix;
  art_u8 *bufptr;
  art_u32 src_alpha;
  art_u32 src_mul;
  art_u8 *dstptr;
  art_u32 dst_alpha;
  art_u32 dst_mul, dst_save_mul;

  image_ix = 0;
  for (i = 0; i < n_run - 1; i++)
    {
      run_x0 = run[i].x;
      run_x1 = run[i + 1].x;
      tmp = run[i].alpha;
      if (tmp < 0x10000)
    continue;

      run_alpha = (tmp + (tmp >> 8) + (tmp >> 16) - 0x8000) >> 8;
      bufptr = image_buf + (run_x0 - x0) * 4;
      dstptr = dest + (run_x0 - x0) * 4;
      if (run_alpha == 0x10000)
    {
      for (x = run_x0; x < run_x1; x++)
        {
          src_alpha = (bufptr[3] << 8) + bufptr[3] + (bufptr[3] >> 7);
          /* src_alpha is the (alpha of the source pixel),
         range 0..0x10000 */

          dst_alpha = (dstptr[3] << 8) + dstptr[3] + (dstptr[3] >> 7);
          /* dst_alpha is the alpha of the dest pixel,
         range 0..0x10000 */

          dst_mul = dst_alpha*0x101;

          if (src_alpha >= 0x10000)
        dst_alpha = 0x10000;
          else
        dst_alpha += ((((0x10000 - dst_alpha) * src_alpha) >> 8) + 0x80) >> 8;

          if (dst_alpha == 0)
          dst_save_mul = 0xff;
          else /* dst_alpha != 0) */
          dst_save_mul = 0xff0000 / dst_alpha;

          for (j = 0; j < 3; j++)
        {
          art_u32 src, dst;
          art_u32 tmp;

          src = (bufptr[j] << 8) |  bufptr[j];
          dst = (dstptr[j] * dst_mul + 0x8000) >> 16;
          tmp = ((dst * (0x10000 - src_alpha) + 0x8000) >> 16) + src;
          tmp -= tmp >> 16;
          dstptr[j] = (tmp * dst_save_mul + 0x8000) >> 16;
        }
          dstptr[3] = (dst_alpha * 0xff + 0x8000) >> 16;

          bufptr += 4;
          dstptr += 4;
        }
    }
      else
    {
      for (x = run_x0; x < run_x1; x++)
        {
          tmp = run_alpha * bufptr[3] + 0x80;
          /* range 0x80 .. 0xff0080 */
          src_alpha = (tmp + (tmp >> 8) + (tmp >> 16)) >> 8;
          /* src_alpha is the (alpha of the source pixel * alpha),
         range 0..0x10000 */

          src_mul = run_alpha * 0x101;

          tmp = dstptr[3];
          /* range 0..0xff */
          dst_alpha = (tmp << 8) + tmp + (tmp >> 7);
          dst_mul = dst_alpha;
          /* dst_alpha is the alpha of the dest pixel,
         range 0..0x10000 */

          dst_mul *= 0x101;

          if (src_alpha >= 0x10000)
        dst_alpha = 0x10000;
          else
        dst_alpha += ((((0x10000 - dst_alpha) * src_alpha) >> 8) + 0x80) >> 8;

          if (dst_alpha == 0)
        {
          dst_save_mul = 0xff;
        }
          else /* dst_alpha != 0) */
        {
          dst_save_mul = 0xff0000 / dst_alpha;
        }

          for (j = 0; j < 3; j++)
        {
          art_u32 src, dst;
          art_u32 tmp;

          src = (bufptr[j] * src_mul + 0x8000) >> 16;
          dst = (dstptr[j] * dst_mul + 0x8000) >> 16;
          tmp = ((dst * (0x10000 - src_alpha) + 0x8000) >> 16) + src;
          tmp -= tmp >> 16;
          dstptr[j] = (tmp * dst_save_mul + 0x8000) >> 16;
        }
          dstptr[3] = (dst_alpha * 0xff + 0x8000) >> 16;

          bufptr += 4;
          dstptr += 4;
        }
    }
    }
}

const ArtRenderCallback art_render_composite_8_opt2_obj =
{
  art_render_composite_8_opt2,
  art_render_nop_done
};