~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/paint-funcs/paint-funcs-generic.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
/*
 
20
 * This file is supposed to contain the generic (read: C) implementation
 
21
 * of the pixel fiddling paint-functions.
 
22
 */
 
23
 
 
24
#ifndef __PAINT_FUNCS_GENERIC_H__
 
25
#define __PAINT_FUNCS_GENERIC_H__
 
26
 
 
27
 
 
28
#define INT_MULT(a,b,t)  ((t) = (a) * (b) + 0x80, ((((t) >> 8) + (t)) >> 8))
 
29
 
 
30
/* This version of INT_MULT3 is very fast, but suffers from some
 
31
   slight roundoff errors.  It returns the correct result 99.987
 
32
   percent of the time */
 
33
#define INT_MULT3(a,b,c,t)  ((t) = (a) * (b) * (c) + 0x7F5B, \
 
34
                            ((((t) >> 7) + (t)) >> 16))
 
35
/*
 
36
  This version of INT_MULT3 always gives the correct result, but runs at
 
37
  approximatly one third the speed. */
 
38
/*  #define INT_MULT3(a,b,c,t) (((a) * (b) * (c) + 32512) / 65025.0)
 
39
 */
 
40
 
 
41
#define INT_BLEND(a,b,alpha,tmp)  (INT_MULT((a) - (b), alpha, tmp) + (b))
 
42
 
 
43
#define RANDOM_TABLE_SIZE  4096
 
44
 
 
45
/* A drawable has an alphachannel if contains either 4 or 2 bytes data
 
46
 * aka GRAYA and RGBA and thus the macro below works. This will have
 
47
 * to change if we support bigger formats. We'll do it so for now because
 
48
 * masking is always cheaper than passing parameters over the stack.      */
 
49
/* FIXME: Move to a global place */
 
50
#define HAS_ALPHA(bytes) (~bytes & 1)
 
51
 
 
52
/* FIXME: Move to a more global place */
 
53
struct apply_layer_mode_struct
 
54
{
 
55
  guint              bytes1 : 3;
 
56
  guint              bytes2 : 3;
 
57
  guchar            *src1;
 
58
  guchar            *src2;
 
59
  guchar            *mask;
 
60
  guchar           **dest;
 
61
  gint               x;
 
62
  gint               y;
 
63
  guint              opacity;
 
64
  guint              length;
 
65
  CombinationMode    combine;
 
66
};
 
67
 
 
68
static guchar  add_lut[511];
 
69
static gint32  random_table[RANDOM_TABLE_SIZE];
 
70
 
 
71
void
 
72
color_pixels (guchar       *dest,
 
73
              const guchar *color,
 
74
              guint         w,
 
75
              guint         bytes)
 
76
{
 
77
  switch (bytes)
 
78
    {
 
79
    case 1:
 
80
      memset (dest, *color, w);
 
81
      break;
 
82
 
 
83
    case 2:
 
84
#if defined(sparc) || defined(__sparc__)
 
85
      {
 
86
        const guchar c0 = color[0];
 
87
        const guchar c1 = color[1];
 
88
 
 
89
        while (w--)
 
90
          {
 
91
            dest[0] = c0;
 
92
            dest[1] = c1;
 
93
            dest += 2;
 
94
          }
 
95
      }
 
96
#else
 
97
      {
 
98
        const guint16   shortc = ((const guint16 *) color)[0];
 
99
        guint16        *shortd = (guint16 *) dest;
 
100
 
 
101
        while (w--)
 
102
          {
 
103
            *shortd = shortc;
 
104
            shortd++;
 
105
          }
 
106
      }
 
107
#endif /* sparc || __sparc__ */
 
108
      break;
 
109
 
 
110
    case 3:
 
111
      {
 
112
        const guchar c0 = color[0];
 
113
        const guchar c1 = color[1];
 
114
        const guchar c2 = color[2];
 
115
 
 
116
        while (w--)
 
117
          {
 
118
            dest[0] = c0;
 
119
            dest[1] = c1;
 
120
            dest[2] = c2;
 
121
            dest += 3;
 
122
          }
 
123
      }
 
124
      break;
 
125
 
 
126
    case 4:
 
127
#if defined(sparc) || defined(__sparc__)
 
128
      {
 
129
        const guchar c0 = color[0];
 
130
        const guchar c1 = color[1];
 
131
        const guchar c2 = color[2];
 
132
        const guchar c3 = color[3];
 
133
 
 
134
        while (w--)
 
135
          {
 
136
            dest[0] = c0;
 
137
            dest[1] = c1;
 
138
            dest[2] = c2;
 
139
            dest[3] = c3;
 
140
            dest += 4;
 
141
          }
 
142
      }
 
143
#else
 
144
      {
 
145
        const guint32   longc = ((const guint32 *) color)[0];
 
146
        guint32        *longd = (guint32 *) dest;
 
147
 
 
148
        while (w--)
 
149
          {
 
150
            *longd = longc;
 
151
            longd++;
 
152
          }
 
153
      }
 
154
#endif /* sparc || __sparc__ */
 
155
      break;
 
156
 
 
157
    default:
 
158
      while (w--)
 
159
        {
 
160
          memcpy (dest, color, bytes);
 
161
          dest += bytes;
 
162
        }
 
163
    }
 
164
}
 
165
 
 
166
void
 
167
color_pixels_mask (guchar       *dest,
 
168
                   guchar       *mask,
 
169
                   const guchar *color,
 
170
                   guint         w,
 
171
                   guint         bytes)
 
172
{
 
173
  guchar c0, c1, c2;
 
174
  gint   alpha;
 
175
 
 
176
  alpha = HAS_ALPHA (bytes) ? bytes - 1 : bytes;
 
177
 
 
178
  switch (bytes)
 
179
    {
 
180
    case 1:
 
181
      memset (dest, *color, w);
 
182
      break;
 
183
 
 
184
    case 2:
 
185
      c0 = color[0];
 
186
      while (w--)
 
187
        {
 
188
          dest[0] = c0;
 
189
          dest[1] = *mask++;
 
190
          dest += 2;
 
191
        }
 
192
      break;
 
193
 
 
194
    case 3:
 
195
      c0 = color[0];
 
196
      c1 = color[1];
 
197
      c2 = color[2];
 
198
      while (w--)
 
199
        {
 
200
          dest[0] = c0;
 
201
          dest[1] = c1;
 
202
          dest[2] = c2;
 
203
          dest += 3;
 
204
        }
 
205
      break;
 
206
 
 
207
    case 4:
 
208
      c0 = color[0];
 
209
      c1 = color[1];
 
210
      c2 = color[2];
 
211
      while (w--)
 
212
        {
 
213
          dest[0] = c0;
 
214
          dest[1] = c1;
 
215
          dest[2] = c2;
 
216
          dest[3] = *mask++;
 
217
          dest += 4;
 
218
        }
 
219
      break;
 
220
    }
 
221
}
 
222
 
 
223
void
 
224
pattern_pixels_mask (guchar  *dest,
 
225
                     guchar  *mask,
 
226
                     TempBuf *pattern,
 
227
                     guint    w,
 
228
                     guint    bytes,
 
229
                     gint     x,
 
230
                     gint     y)
 
231
{
 
232
  guchar *pat, *p;
 
233
  gint    alpha, b;
 
234
  gint    i;
 
235
 
 
236
  /*  Get a pointer to the appropriate scanline of the pattern buffer  */
 
237
  pat = (temp_buf_data (pattern) +
 
238
         (y % pattern->height) * pattern->width * pattern->bytes);
 
239
 
 
240
  alpha = HAS_ALPHA (bytes) ? bytes - 1 : bytes;
 
241
 
 
242
  /*
 
243
   * image data = pattern data for all but alpha
 
244
   *
 
245
   * If (image has alpha)
 
246
   *   if (there's a mask)
 
247
   *     image data = mask for alpha;
 
248
   *   else
 
249
   *     image data = opaque for alpha.
 
250
   *
 
251
   *   if (pattern has alpha)
 
252
   *     multiply existing alpha channel by pattern alpha
 
253
   *     (normalised to (0..1))
 
254
   */
 
255
 
 
256
  for (i = 0; i < w; i++)
 
257
    {
 
258
      p = pat + ((i + x) % pattern->width) * pattern->bytes;
 
259
 
 
260
      for (b = 0; b < alpha; b++)
 
261
        dest[b] = p[b];
 
262
 
 
263
      if (HAS_ALPHA (bytes))
 
264
        {
 
265
          if (mask)
 
266
            dest[alpha] = *mask++;
 
267
          else
 
268
            dest[alpha] = OPAQUE_OPACITY;
 
269
 
 
270
          if (HAS_ALPHA (pattern->bytes))
 
271
            dest[alpha] = (guchar) (dest[alpha] *
 
272
                                    p[alpha] / (gdouble) OPAQUE_OPACITY);
 
273
        }
 
274
 
 
275
      dest += bytes;
 
276
    }
 
277
}
 
278
 
 
279
inline void
 
280
blend_pixels (const guchar *src1,
 
281
              const guchar *src2,
 
282
              guchar       *dest,
 
283
              guchar        blend,
 
284
              guint         w,
 
285
              guint         bytes)
 
286
{
 
287
  guint b;
 
288
 
 
289
  if (HAS_ALPHA (bytes))
 
290
    {
 
291
      const guint blend1 = 256 - blend;
 
292
      const guint blend2 = blend + 1;
 
293
      const guint c      = bytes - 1;
 
294
 
 
295
      while (w--)
 
296
        {
 
297
          guint a1 = blend1 * src1[c];
 
298
          guint a2 = blend2 * src2[c];
 
299
          guint a  = a1 + a2;
 
300
 
 
301
          if (!a)
 
302
            {
 
303
              for (b = 0; b < bytes; b++)
 
304
                dest[b] = 0;
 
305
            }
 
306
          else
 
307
            {
 
308
              for (b = 0; b < c; b++)
 
309
                dest[b] = (src1[b] * a1 + src2[b] * a2) / a;
 
310
 
 
311
              dest[c] = a >> 8;
 
312
            }
 
313
 
 
314
          src1 += bytes;
 
315
          src2 += bytes;
 
316
          dest += bytes;
 
317
        }
 
318
    }
 
319
  else
 
320
    {
 
321
      const guchar blend1 = 255 - blend;
 
322
 
 
323
      while (w--)
 
324
        {
 
325
          for (b = 0; b < bytes; b++)
 
326
            dest[b] = (src1[b] * blend1 + src2[b] * blend) / 255;
 
327
 
 
328
          src1 += bytes;
 
329
          src2 += bytes;
 
330
          dest += bytes;
 
331
        }
 
332
    }
 
333
}
 
334
 
 
335
inline void
 
336
shade_pixels (const guchar *src,
 
337
              guchar       *dest,
 
338
              const guchar *col,
 
339
              guchar            blend,
 
340
              guint         w,
 
341
              guint         bytes,
 
342
              gboolean      has_alpha)
 
343
{
 
344
  const guchar blend2 = (255 - blend);
 
345
  const guint  alpha = (has_alpha) ? bytes - 1 : bytes;
 
346
  guint b;
 
347
 
 
348
  while (w--)
 
349
    {
 
350
      for (b = 0; b < alpha; b++)
 
351
        dest[b] = (src[b] * blend2 + col[b] * blend) / 255;
 
352
 
 
353
      if (has_alpha)
 
354
        dest[alpha] = src[alpha];  /* alpha channel */
 
355
 
 
356
      src += bytes;
 
357
      dest += bytes;
 
358
    }
 
359
}
 
360
 
 
361
 
 
362
inline void
 
363
extract_alpha_pixels (const guchar *src,
 
364
                      const guchar *mask,
 
365
                      guchar       *dest,
 
366
                      guint         w,
 
367
                      guint         bytes)
 
368
{
 
369
  const guint alpha = bytes - 1;
 
370
  gint          tmp;
 
371
 
 
372
  if (mask)
 
373
    {
 
374
      const guchar *m = mask;
 
375
      while (w--)
 
376
        {
 
377
          *dest++ = INT_MULT(src[alpha], *m, tmp);
 
378
          m++;
 
379
          src += bytes;
 
380
        }
 
381
    }
 
382
  else
 
383
    {
 
384
      while (w--)
 
385
        {
 
386
          *dest++ = INT_MULT(src[alpha], OPAQUE_OPACITY, tmp);
 
387
          src += bytes;
 
388
        }
 
389
    }
 
390
}
 
391
 
 
392
 
 
393
static inline void
 
394
darken_pixels (const guchar *src1,
 
395
               const guchar *src2,
 
396
               guchar       *dest,
 
397
               guint         length,
 
398
               guint         bytes1,
 
399
               guint         bytes2)
 
400
{
 
401
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
402
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
403
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
404
  guint b;
 
405
  guchar s1, s2;
 
406
 
 
407
  while (length--)
 
408
    {
 
409
      for (b = 0; b < alpha; b++)
 
410
        {
 
411
          s1 = src1[b];
 
412
          s2 = src2[b];
 
413
          dest[b] = (s1 < s2) ? s1 : s2;
 
414
        }
 
415
 
 
416
      if (has_alpha1 && has_alpha2)
 
417
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
418
      else if (has_alpha2)
 
419
        dest[alpha] = src2[alpha];
 
420
 
 
421
      src1 += bytes1;
 
422
      src2 += bytes2;
 
423
      dest += bytes2;
 
424
    }
 
425
}
 
426
 
 
427
 
 
428
static inline void
 
429
lighten_pixels (const guchar *src1,
 
430
                const guchar *src2,
 
431
                guchar       *dest,
 
432
                guint         length,
 
433
                guint         bytes1,
 
434
                guint         bytes2)
 
435
{
 
436
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
437
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
438
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
439
  guint b;
 
440
  guchar s1, s2;
 
441
 
 
442
  while (length--)
 
443
    {
 
444
      for (b = 0; b < alpha; b++)
 
445
        {
 
446
          s1 = src1[b];
 
447
          s2 = src2[b];
 
448
          dest[b] = (s1 < s2) ? s2 : s1;
 
449
        }
 
450
 
 
451
      if (has_alpha1 && has_alpha2)
 
452
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
453
      else if (has_alpha2)
 
454
        dest[alpha] = src2[alpha];
 
455
 
 
456
      src1 += bytes1;
 
457
      src2 += bytes2;
 
458
      dest += bytes2;
 
459
    }
 
460
}
 
461
 
 
462
 
 
463
static inline void
 
464
hue_only_pixels (const guchar *src1,
 
465
                 const guchar *src2,
 
466
                 guchar       *dest,
 
467
                 guint         length,
 
468
                 guint         bytes1,
 
469
                 guint         bytes2)
 
470
{
 
471
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
472
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
473
  gint r1, g1, b1;
 
474
  gint r2, g2, b2;
 
475
 
 
476
  /*  assumes inputs are only 4 byte RGBA pixels  */
 
477
  while (length--)
 
478
    {
 
479
      r1 = src1[0]; g1 = src1[1]; b1 = src1[2];
 
480
      r2 = src2[0]; g2 = src2[1]; b2 = src2[2];
 
481
      gimp_rgb_to_hsv_int (&r1, &g1, &b1);
 
482
      gimp_rgb_to_hsv_int (&r2, &g2, &b2);
 
483
 
 
484
      r1 = r2;
 
485
 
 
486
      /*  set the destination  */
 
487
      gimp_hsv_to_rgb_int (&r1, &g1, &b1);
 
488
 
 
489
      dest[0] = r1; dest[1] = g1; dest[2] = b1;
 
490
 
 
491
      if (has_alpha1 && has_alpha2)
 
492
        dest[3] = MIN (src1[3], src2[3]);
 
493
      else if (has_alpha2)
 
494
        dest[3] = src2[3];
 
495
 
 
496
      src1 += bytes1;
 
497
      src2 += bytes2;
 
498
      dest += bytes2;
 
499
    }
 
500
}
 
501
 
 
502
 
 
503
static inline void
 
504
saturation_only_pixels (const guchar *src1,
 
505
                        const guchar *src2,
 
506
                        guchar       *dest,
 
507
                        guint         length,
 
508
                        guint         bytes1,
 
509
                        guint         bytes2)
 
510
{
 
511
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
512
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
513
  guint r1, g1, b1;
 
514
  guint r2, g2, b2;
 
515
 
 
516
  /*  assumes inputs are only 4 byte RGBA pixels  */
 
517
  while (length--)
 
518
    {
 
519
      r1 = src1[0]; g1 = src1[1]; b1 = src1[2];
 
520
      r2 = src2[0]; g2 = src2[1]; b2 = src2[2];
 
521
      gimp_rgb_to_hsv_int (&r1, &g1, &b1);
 
522
      gimp_rgb_to_hsv_int (&r2, &g2, &b2);
 
523
 
 
524
      g1 = g2;
 
525
 
 
526
      /*  set the destination  */
 
527
      gimp_hsv_to_rgb_int (&r1, &g1, &b1);
 
528
 
 
529
      dest[0] = r1; dest[1] = g1; dest[2] = b1;
 
530
 
 
531
      if (has_alpha1 && has_alpha2)
 
532
        dest[3] = MIN (src1[3], src2[3]);
 
533
      else if (has_alpha2)
 
534
        dest[3] = src2[3];
 
535
 
 
536
      src1 += bytes1;
 
537
      src2 += bytes2;
 
538
      dest += bytes2;
 
539
    }
 
540
}
 
541
 
 
542
 
 
543
static inline void
 
544
value_only_pixels (const guchar *src1,
 
545
                   const guchar *src2,
 
546
                   guchar       *dest,
 
547
                   guint         length,
 
548
                   guint         bytes1,
 
549
                   guint         bytes2)
 
550
{
 
551
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
552
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
553
  guint r1, g1, b1;
 
554
  guint r2, g2, b2;
 
555
 
 
556
  /*  assumes inputs are only 4 byte RGBA pixels  */
 
557
  while (length--)
 
558
    {
 
559
      r1 = src1[0]; g1 = src1[1]; b1 = src1[2];
 
560
      r2 = src2[0]; g2 = src2[1]; b2 = src2[2];
 
561
      gimp_rgb_to_hsv_int (&r1, &g1, &b1);
 
562
      gimp_rgb_to_hsv_int (&r2, &g2, &b2);
 
563
 
 
564
      b1 = b2;
 
565
 
 
566
      /*  set the destination  */
 
567
      gimp_hsv_to_rgb_int (&r1, &g1, &b1);
 
568
 
 
569
      dest[0] = r1; dest[1] = g1; dest[2] = b1;
 
570
 
 
571
      if (has_alpha1 && has_alpha2)
 
572
        dest[3] = MIN (src1[3], src2[3]);
 
573
      else if (has_alpha2)
 
574
        dest[3] = src2[3];
 
575
 
 
576
      src1 += bytes1;
 
577
      src2 += bytes2;
 
578
      dest += bytes2;
 
579
    }
 
580
}
 
581
 
 
582
 
 
583
static inline void
 
584
color_only_pixels (const guchar *src1,
 
585
                   const guchar *src2,
 
586
                   guchar       *dest,
 
587
                   guint         length,
 
588
                   guint         bytes1,
 
589
                   guint         bytes2)
 
590
{
 
591
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
592
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
593
  guint r1, g1, b1;
 
594
  guint r2, g2, b2;
 
595
 
 
596
  /*  assumes inputs are only 4 byte RGBA pixels  */
 
597
  while (length--)
 
598
    {
 
599
      r1 = src1[0]; g1 = src1[1]; b1 = src1[2];
 
600
      r2 = src2[0]; g2 = src2[1]; b2 = src2[2];
 
601
      gimp_rgb_to_hsl_int (&r1, &g1, &b1);
 
602
      gimp_rgb_to_hsl_int (&r2, &g2, &b2);
 
603
 
 
604
      /*  transfer hue and saturation to the source pixel  */
 
605
      r1 = r2;
 
606
      g1 = g2;
 
607
 
 
608
      /*  set the destination  */
 
609
      gimp_hsl_to_rgb_int (&r1, &g1, &b1);
 
610
 
 
611
      dest[0] = r1; dest[1] = g1; dest[2] = b1;
 
612
 
 
613
      if (has_alpha1 && has_alpha2)
 
614
        dest[3] = MIN (src1[3], src2[3]);
 
615
      else if (has_alpha2)
 
616
        dest[3] = src2[3];
 
617
 
 
618
      src1 += bytes1;
 
619
      src2 += bytes2;
 
620
      dest += bytes2;
 
621
    }
 
622
}
 
623
 
 
624
 
 
625
static inline void
 
626
multiply_pixels (const guchar *src1,
 
627
                 const guchar *src2,
 
628
                 guchar       *dest,
 
629
                 guint         length,
 
630
                 guint         bytes1,
 
631
                 guint         bytes2)
 
632
{
 
633
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
634
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
635
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
636
  guint b, tmp;
 
637
 
 
638
  if (has_alpha1 && has_alpha2)
 
639
    {
 
640
      while (length --)
 
641
        {
 
642
          for (b = 0; b < alpha; b++)
 
643
            dest[b] = INT_MULT(src1[b], src2[b], tmp);
 
644
 
 
645
          dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
646
 
 
647
          src1 += bytes1;
 
648
          src2 += bytes2;
 
649
          dest += bytes2;
 
650
        }
 
651
    }
 
652
  else if (has_alpha2)
 
653
    {
 
654
      while (length --)
 
655
        {
 
656
          for (b = 0; b < alpha; b++)
 
657
            dest[b] = INT_MULT(src1[b], src2[b], tmp);
 
658
 
 
659
          dest[alpha] = src2[alpha];
 
660
 
 
661
          src1 += bytes1;
 
662
          src2 += bytes2;
 
663
          dest += bytes2;
 
664
        }
 
665
    }
 
666
  else
 
667
    {
 
668
      while (length --)
 
669
        {
 
670
          for (b = 0; b < alpha; b++)
 
671
            dest[b] = INT_MULT(src1[b], src2[b], tmp);
 
672
 
 
673
          src1 += bytes1;
 
674
          src2 += bytes2;
 
675
          dest += bytes2;
 
676
        }
 
677
    }
 
678
}
 
679
 
 
680
 
 
681
static inline void
 
682
divide_pixels (const guchar *src1,
 
683
               const guchar *src2,
 
684
               guchar       *dest,
 
685
               guint         length,
 
686
               guint         bytes1,
 
687
               guint         bytes2)
 
688
{
 
689
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
690
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
691
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
692
  guint b, result;
 
693
 
 
694
  while (length--)
 
695
    {
 
696
      for (b = 0; b < alpha; b++)
 
697
        {
 
698
          result = ((src1[b] * 256) / (1+src2[b]));
 
699
          dest[b] = MIN (result, 255);
 
700
        }
 
701
 
 
702
      if (has_alpha1 && has_alpha2)
 
703
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
704
      else if (has_alpha2)
 
705
        dest[alpha] = src2[alpha];
 
706
 
 
707
      src1 += bytes1;
 
708
      src2 += bytes2;
 
709
      dest += bytes2;
 
710
    }
 
711
}
 
712
 
 
713
 
 
714
static inline void
 
715
screen_pixels (const guchar *src1,
 
716
               const guchar *src2,
 
717
               guchar       *dest,
 
718
               guint         length,
 
719
               guint         bytes1,
 
720
               guint         bytes2)
 
721
{
 
722
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
723
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
724
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
725
  guint b, tmp;
 
726
 
 
727
  while (length --)
 
728
    {
 
729
      for (b = 0; b < alpha; b++)
 
730
        dest[b] = 255 - INT_MULT((255 - src1[b]), (255 - src2[b]), tmp);
 
731
 
 
732
      if (has_alpha1 && has_alpha2)
 
733
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
734
      else if (has_alpha2)
 
735
        dest[alpha] = src2[alpha];
 
736
 
 
737
      src1 += bytes1;
 
738
      src2 += bytes2;
 
739
      dest += bytes2;
 
740
    }
 
741
}
 
742
 
 
743
 
 
744
static inline void
 
745
overlay_pixels (const guchar *src1,
 
746
                const guchar *src2,
 
747
                guchar       *dest,
 
748
                guint         length,
 
749
                guint         bytes1,
 
750
                guint         bytes2)
 
751
{
 
752
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
753
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
754
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
755
  guint b, tmp, tmpM;
 
756
 
 
757
  while (length --)
 
758
    {
 
759
      for (b = 0; b < alpha; b++)
 
760
        {
 
761
          dest[b] = INT_MULT(src1[b], src1[b] + INT_MULT(2 * src2[b],
 
762
                                                         255 - src1[b],
 
763
                                                         tmpM), tmp);
 
764
        }
 
765
 
 
766
      if (has_alpha1 && has_alpha2)
 
767
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
768
      else if (has_alpha2)
 
769
        dest[alpha] = src2[alpha];
 
770
 
 
771
      src1 += bytes1;
 
772
      src2 += bytes2;
 
773
      dest += bytes2;
 
774
    }
 
775
}
 
776
 
 
777
 
 
778
static inline void
 
779
dodge_pixels (const guchar *src1,
 
780
              const guchar *src2,
 
781
              guchar           *dest,
 
782
              guint         length,
 
783
              guint         bytes1,
 
784
              guint         bytes2)
 
785
{
 
786
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
787
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
788
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
789
  guint b, tmp;
 
790
 
 
791
  while (length --)
 
792
    {
 
793
      for (b = 0; b < alpha; b++)
 
794
        {
 
795
          tmp = src1[b] << 8;
 
796
          tmp /= 256 - src2[b];
 
797
          dest[b] = (guchar) MIN (tmp, 255);
 
798
        }
 
799
 
 
800
      if (has_alpha1 && has_alpha2)
 
801
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
802
      else if (has_alpha2)
 
803
        dest[alpha] = src2[alpha];
 
804
 
 
805
      src1 += bytes1;
 
806
      src2 += bytes2;
 
807
      dest += bytes2;
 
808
    }
 
809
}
 
810
 
 
811
 
 
812
static inline void
 
813
burn_pixels (const guchar *src1,
 
814
             const guchar *src2,
 
815
             guchar       *dest,
 
816
             guint         length,
 
817
             guint         bytes1,
 
818
             guint         bytes2)
 
819
{
 
820
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
821
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
822
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
823
  guint b;
 
824
 
 
825
  /* FIXME: Is the burn effect supposed to be dependant on the sign of this
 
826
   * temporary variable? */
 
827
  gint tmp;
 
828
 
 
829
  while (length --)
 
830
    {
 
831
      for (b = 0; b < alpha; b++)
 
832
        {
 
833
            tmp = (255 - src1[b]) << 8;
 
834
            tmp /= src2[b] + 1;
 
835
            dest[b] = (guchar) CLAMP (255 - tmp, 0, 255);
 
836
        }
 
837
 
 
838
      if (has_alpha1 && has_alpha2)
 
839
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
840
      else if (has_alpha2)
 
841
        dest[alpha] = src2[alpha];
 
842
 
 
843
      src1 += bytes1;
 
844
      src2 += bytes2;
 
845
      dest += bytes2;
 
846
    }
 
847
}
 
848
 
 
849
 
 
850
static inline void
 
851
hardlight_pixels (const guchar *src1,
 
852
                  const guchar *src2,
 
853
                  guchar       *dest,
 
854
                  guint         length,
 
855
                  guint         bytes1,
 
856
                  guint         bytes2)
 
857
{
 
858
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
859
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
860
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
861
  guint b, tmp;
 
862
 
 
863
  while (length --)
 
864
    {
 
865
      for (b = 0; b < alpha; b++)
 
866
        {
 
867
          if (src2[b] > 128) {
 
868
            tmp = ((gint)255 - src1[b]) * ((gint)255 - ((src2[b] - 128) << 1));
 
869
            dest[b] = (guchar) MIN (255 - (tmp >> 8), 255);
 
870
          } else {
 
871
            tmp = (gint)src1[b] * ((gint)src2[b] << 1);
 
872
            dest[b] = (guchar) MIN (tmp >> 8, 255);
 
873
          }
 
874
        }
 
875
 
 
876
      if (has_alpha1 && has_alpha2)
 
877
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
878
      else if (has_alpha2)
 
879
        dest[alpha] = src2[alpha];
 
880
 
 
881
      src1 += bytes1;
 
882
      src2 += bytes2;
 
883
      dest += bytes2;
 
884
    }
 
885
}
 
886
 
 
887
 
 
888
static inline void
 
889
softlight_pixels (const guchar *src1,
 
890
                  const guchar *src2,
 
891
                  guchar       *dest,
 
892
                  guint         length,
 
893
                  guint         bytes1,
 
894
                  guint         bytes2)
 
895
{
 
896
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
897
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
898
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
899
  guint b, tmpS, tmpM, tmp1, tmp2, tmp3;
 
900
 
 
901
  while (length --)
 
902
    {
 
903
      for (b = 0; b < alpha; b++)
 
904
        {
 
905
          /* Mix multiply and screen */
 
906
          tmpM = INT_MULT (src1[b], src2[b], tmpM);
 
907
          tmpS = 255 - INT_MULT((255 - src1[b]), (255 - src2[b]), tmp1);
 
908
          dest[b] = INT_MULT ((255 - src1[b]), tmpM, tmp2) +
 
909
            INT_MULT (src1[b], tmpS, tmp3);
 
910
        }
 
911
 
 
912
      if (has_alpha1 && has_alpha2)
 
913
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
914
      else if (has_alpha2)
 
915
        dest[alpha] = src2[alpha];
 
916
 
 
917
      src1 += bytes1;
 
918
      src2 += bytes2;
 
919
      dest += bytes2;
 
920
    }
 
921
}
 
922
 
 
923
 
 
924
static inline void
 
925
grain_extract_pixels (const guchar *src1,
 
926
                      const guchar *src2,
 
927
                      guchar       *dest,
 
928
                      guint         length,
 
929
                      guint         bytes1,
 
930
                      guint         bytes2)
 
931
{
 
932
  guint alpha, b;
 
933
  gint diff;
 
934
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
935
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
936
 
 
937
  alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
938
 
 
939
  while (length --)
 
940
    {
 
941
      for (b = 0; b < alpha; b++)
 
942
        {
 
943
          diff = src1[b] - src2[b] + 128;
 
944
          dest[b] = (guchar) CLAMP (diff, 0, 255);
 
945
        }
 
946
 
 
947
      if (has_alpha1 && has_alpha2)
 
948
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
949
      else if (has_alpha2)
 
950
        dest[alpha] = src2[alpha];
 
951
 
 
952
      src1 += bytes1;
 
953
      src2 += bytes2;
 
954
      dest += bytes2;
 
955
    }
 
956
}
 
957
 
 
958
 
 
959
static inline void
 
960
grain_merge_pixels (const guchar *src1,
 
961
                    const guchar *src2,
 
962
                    guchar       *dest,
 
963
                    guint         length,
 
964
                    guint         bytes1,
 
965
                    guint         bytes2)
 
966
{
 
967
  gint alpha, b;
 
968
  gint sum;
 
969
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
970
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
971
 
 
972
  alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
973
 
 
974
  while (length --)
 
975
    {
 
976
      for (b = 0; b < alpha; b++)
 
977
        {
 
978
          /* Add, re-center and clip. */
 
979
          sum = src1[b] + src2[b] - 128;
 
980
          dest[b] = (guchar) CLAMP (sum, 0, 255);
 
981
        }
 
982
 
 
983
      if (has_alpha1 && has_alpha2)
 
984
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
985
      else if (has_alpha2)
 
986
        dest[alpha] = src2[alpha];
 
987
 
 
988
      src1 += bytes1;
 
989
      src2 += bytes2;
 
990
      dest += bytes2;
 
991
    }
 
992
}
 
993
 
 
994
 
 
995
static inline void
 
996
add_pixels (const guchar *src1,
 
997
            const guchar *src2,
 
998
            guchar       *dest,
 
999
            guint         length,
 
1000
            guint         bytes1,
 
1001
            guint         bytes2)
 
1002
{
 
1003
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
1004
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
1005
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
1006
  guint b;
 
1007
 
 
1008
  while (length --)
 
1009
    {
 
1010
      for (b = 0; b < alpha; b++)
 
1011
          dest[b] = add_lut[src1[b] + src2[b]];
 
1012
 
 
1013
      if (has_alpha1 && has_alpha2)
 
1014
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
1015
      else if (has_alpha2)
 
1016
        dest[alpha] = src2[alpha];
 
1017
 
 
1018
      src1 += bytes1;
 
1019
      src2 += bytes2;
 
1020
      dest += bytes2;
 
1021
    }
 
1022
}
 
1023
 
 
1024
 
 
1025
static inline void
 
1026
subtract_pixels (const guchar *src1,
 
1027
                 const guchar *src2,
 
1028
                 guchar       *dest,
 
1029
                 guint         length,
 
1030
                 guint         bytes1,
 
1031
                 guint         bytes2)
 
1032
{
 
1033
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
1034
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
1035
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
1036
  guint b;
 
1037
  gint diff;
 
1038
 
 
1039
  while (length --)
 
1040
    {
 
1041
      for (b = 0; b < alpha; b++)
 
1042
        {
 
1043
          diff = src1[b] - src2[b];
 
1044
          dest[b] = (diff < 0) ? 0 : diff;
 
1045
        }
 
1046
 
 
1047
      if (has_alpha1 && has_alpha2)
 
1048
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
1049
      else if (has_alpha2)
 
1050
        dest[alpha] = src2[alpha];
 
1051
 
 
1052
      src1 += bytes1;
 
1053
      src2 += bytes2;
 
1054
      dest += bytes2;
 
1055
    }
 
1056
}
 
1057
 
 
1058
 
 
1059
static inline void
 
1060
difference_pixels (const guchar *src1,
 
1061
                   const guchar *src2,
 
1062
                   guchar       *dest,
 
1063
                   guint         length,
 
1064
                   guint         bytes1,
 
1065
                   guint         bytes2)
 
1066
{
 
1067
  const guint has_alpha1 = HAS_ALPHA (bytes1);
 
1068
  const guint has_alpha2 = HAS_ALPHA (bytes2);
 
1069
  const guint alpha = (has_alpha1 || has_alpha2) ? MAX (bytes1, bytes2) - 1 : bytes1;
 
1070
  guint b;
 
1071
  gint diff;
 
1072
 
 
1073
  while (length --)
 
1074
    {
 
1075
      for (b = 0; b < alpha; b++)
 
1076
        {
 
1077
          diff = src1[b] - src2[b];
 
1078
          dest[b] = (diff < 0) ? -diff : diff;
 
1079
        }
 
1080
 
 
1081
      if (has_alpha1 && has_alpha2)
 
1082
        dest[alpha] = MIN (src1[alpha], src2[alpha]);
 
1083
      else if (has_alpha2)
 
1084
        dest[alpha] = src2[alpha];
 
1085
 
 
1086
      src1 += bytes1;
 
1087
      src2 += bytes2;
 
1088
      dest += bytes2;
 
1089
    }
 
1090
}
 
1091
 
 
1092
 
 
1093
static inline void
 
1094
dissolve_pixels (const guchar *src,
 
1095
                 guchar       *mask,
 
1096
                 guchar       *dest,
 
1097
                 gint          x,
 
1098
                 gint          y,
 
1099
                 gint          opacity,
 
1100
                 gint          length,
 
1101
                 gint          sb,
 
1102
                 gint          db,
 
1103
                 guint               has_alpha)
 
1104
{
 
1105
  gint    alpha, b;
 
1106
  gint32  rand_val;
 
1107
  gint    combined_opacity;
 
1108
  GRand  *gr;
 
1109
 
 
1110
  gr = g_rand_new_with_seed (random_table[y % RANDOM_TABLE_SIZE]);
 
1111
 
 
1112
  /* Ignore x random values so we get a deterministic result */
 
1113
  for (b = 0; b < x; b ++)
 
1114
    g_rand_int (gr);
 
1115
 
 
1116
  alpha = db - 1;
 
1117
 
 
1118
  while (length--)
 
1119
    {
 
1120
      /*  preserve the intensity values  */
 
1121
      for (b = 0; b < alpha; b++)
 
1122
        dest[b] = src[b];
 
1123
 
 
1124
      /*  dissolve if random value is >= opacity  */
 
1125
      rand_val = g_rand_int_range (gr, 0, 255);
 
1126
 
 
1127
      if (mask)
 
1128
        {
 
1129
          if (has_alpha)
 
1130
            combined_opacity = opacity * src[alpha] * *mask / (255 * 255);
 
1131
          else
 
1132
            combined_opacity = opacity * *mask / 255;
 
1133
 
 
1134
          mask++;
 
1135
        }
 
1136
      else
 
1137
        {
 
1138
          if (has_alpha)
 
1139
            combined_opacity = opacity * src[alpha] / 255;
 
1140
          else
 
1141
            combined_opacity = opacity;
 
1142
        }
 
1143
 
 
1144
      dest[alpha] = (rand_val >= combined_opacity) ? 0 : OPAQUE_OPACITY;
 
1145
 
 
1146
      src  += sb;
 
1147
      dest += db;
 
1148
    }
 
1149
 
 
1150
  g_rand_free (gr);
 
1151
 
 
1152
}
 
1153
 
 
1154
static inline void
 
1155
replace_pixels (guchar         *src1,
 
1156
                guchar         *src2,
 
1157
                guchar         *dest,
 
1158
                guchar         *mask,
 
1159
                gint            length,
 
1160
                gint            opacity,
 
1161
                const gboolean *affect,
 
1162
                gint            bytes1,
 
1163
                gint            bytes2)
 
1164
{
 
1165
  gint    alpha;
 
1166
  gint    b;
 
1167
  gdouble a_val, a_recip, mask_val;
 
1168
  gdouble norm_opacity;
 
1169
  gint    s1_a, s2_a;
 
1170
  gint    new_val;
 
1171
 
 
1172
  if (bytes1 != bytes2)
 
1173
    {
 
1174
      g_warning ("replace_pixels only works on commensurate pixel regions");
 
1175
      return;
 
1176
    }
 
1177
 
 
1178
  alpha = bytes1 - 1;
 
1179
  norm_opacity = opacity * (1.0 / 65536.0);
 
1180
 
 
1181
  while (length --)
 
1182
    {
 
1183
      mask_val = mask[0] * norm_opacity;
 
1184
      /* calculate new alpha first. */
 
1185
      s1_a = src1[alpha];
 
1186
      s2_a = src2[alpha];
 
1187
      a_val = s1_a + mask_val * (s2_a - s1_a);
 
1188
 
 
1189
      if (a_val == 0) /* In any case, write out versions of the blending function */
 
1190
                      /* that result when combinations of s1_a, s2_a, and         */
 
1191
                      /* mask_val --> 0 (or mask_val -->1)                        */
 
1192
        {
 
1193
          /* Case 1: s1_a, s2_a, AND mask_val all approach 0+:               */
 
1194
          /* Case 2: s1_a AND s2_a both approach 0+, regardless of mask_val: */
 
1195
 
 
1196
          if (s1_a + s2_a == 0.0)
 
1197
            {
 
1198
              for (b = 0; b < alpha; b++)
 
1199
                {
 
1200
                  new_val = 0.5 + (gdouble) src1[b] +
 
1201
                    mask_val * ((gdouble) src2[b] - (gdouble) src1[b]);
 
1202
 
 
1203
                  dest[b] = affect[b] ? MIN (new_val, 255) : src1[b];
 
1204
                }
 
1205
            }
 
1206
 
 
1207
          /* Case 3: mask_val AND s1_a both approach 0+, regardless of s2_a  */
 
1208
          else if (s1_a + mask_val == 0.0)
 
1209
            {
 
1210
              for (b = 0; b < alpha; b++)
 
1211
                {
 
1212
                  dest[b] = src1[b];
 
1213
                }
 
1214
            }
 
1215
 
 
1216
          /* Case 4: mask_val -->1 AND s2_a -->0, regardless of s1_a         */
 
1217
          else if (1.0 - mask_val + s2_a == 0.0)
 
1218
            {
 
1219
              for (b = 0; b < alpha; b++)
 
1220
                {
 
1221
                  dest[b] = affect[b] ? src2[b] : src1[b];
 
1222
                }
 
1223
            }
 
1224
        }
 
1225
      else
 
1226
        {
 
1227
          a_recip = 1.0 / a_val;
 
1228
          /* possible optimization: fold a_recip into s1_a and s2_a              */
 
1229
          for (b = 0; b < alpha; b++)
 
1230
            {
 
1231
              new_val = 0.5 + a_recip * (src1[b] * s1_a + mask_val *
 
1232
                                         (src2[b] * s2_a - src1[b] * s1_a));
 
1233
              dest[b] = affect[b] ? MIN (new_val, 255) : src1[b];
 
1234
            }
 
1235
        }
 
1236
 
 
1237
      dest[alpha] = affect[alpha] ? a_val + 0.5: s1_a;
 
1238
      src1 += bytes1;
 
1239
      src2 += bytes2;
 
1240
      dest += bytes2;
 
1241
      mask++;
 
1242
    }
 
1243
}
 
1244
 
 
1245
inline void
 
1246
swap_pixels (guchar *src,
 
1247
             guchar *dest,
 
1248
             guint   length)
 
1249
{
 
1250
  while (length--)
 
1251
    {
 
1252
      *src = *src ^ *dest;
 
1253
      *dest = *dest ^ *src;
 
1254
      *src = *src ^ *dest;
 
1255
      src++;
 
1256
      dest++;
 
1257
    }
 
1258
}
 
1259
 
 
1260
inline void
 
1261
scale_pixels (const guchar *src,
 
1262
              guchar       *dest,
 
1263
              guint         length,
 
1264
              gint          scale)
 
1265
{
 
1266
  gint tmp;
 
1267
 
 
1268
  while (length --)
 
1269
    {
 
1270
      *dest++ = (guchar) INT_MULT (*src, scale, tmp);
 
1271
      src++;
 
1272
    }
 
1273
}
 
1274
 
 
1275
inline void
 
1276
add_alpha_pixels (const guchar *src,
 
1277
                  guchar       *dest,
 
1278
                  guint         length,
 
1279
                  guint         bytes)
 
1280
{
 
1281
  gint alpha, b;
 
1282
 
 
1283
  alpha = bytes + 1;
 
1284
 
 
1285
  while (length --)
 
1286
    {
 
1287
      for (b = 0; b < bytes; b++)
 
1288
        dest[b] = src[b];
 
1289
 
 
1290
      dest[b] = OPAQUE_OPACITY;
 
1291
 
 
1292
      src += bytes;
 
1293
      dest += alpha;
 
1294
    }
 
1295
}
 
1296
 
 
1297
 
 
1298
inline void
 
1299
flatten_pixels (const guchar *src,
 
1300
                guchar       *dest,
 
1301
                const guchar *bg,
 
1302
                guint         length,
 
1303
                guint         bytes)
 
1304
{
 
1305
  gint alpha, b;
 
1306
  gint t1, t2;
 
1307
 
 
1308
  alpha = bytes - 1;
 
1309
  while (length --)
 
1310
    {
 
1311
      for (b = 0; b < alpha; b++)
 
1312
        dest[b] = INT_MULT (src[b], src[alpha], t1) +
 
1313
                  INT_MULT (bg[b], (255 - src[alpha]), t2);
 
1314
 
 
1315
      src += bytes;
 
1316
      dest += alpha;
 
1317
    }
 
1318
}
 
1319
 
 
1320
 
 
1321
inline void
 
1322
gray_to_rgb_pixels (const guchar *src,
 
1323
                    guchar       *dest,
 
1324
                    guint         length,
 
1325
                    guint         bytes)
 
1326
{
 
1327
  gint     b;
 
1328
  gint     dest_bytes;
 
1329
  gboolean has_alpha;
 
1330
 
 
1331
  has_alpha = (bytes == 2) ? TRUE : FALSE;
 
1332
  dest_bytes = (has_alpha) ? 4 : 3;
 
1333
 
 
1334
  while (length --)
 
1335
    {
 
1336
      for (b = 0; b < bytes; b++)
 
1337
        dest[b] = src[0];
 
1338
 
 
1339
      if (has_alpha)
 
1340
        dest[3] = src[1];
 
1341
 
 
1342
      src += bytes;
 
1343
      dest += dest_bytes;
 
1344
    }
 
1345
}
 
1346
 
 
1347
 
 
1348
inline void
 
1349
apply_mask_to_alpha_channel (guchar       *src,
 
1350
                             const guchar *mask,
 
1351
                             guint         opacity,
 
1352
                             guint         length,
 
1353
                             guint         bytes)
 
1354
{
 
1355
  glong tmp;
 
1356
 
 
1357
  src += bytes - 1;
 
1358
 
 
1359
  if (opacity == 255)
 
1360
    {
 
1361
      while (length --)
 
1362
        {
 
1363
          *src = INT_MULT(*src, *mask, tmp);
 
1364
          mask++;
 
1365
          src += bytes;
 
1366
        }
 
1367
    }
 
1368
  else
 
1369
    {
 
1370
      while (length --)
 
1371
        {
 
1372
          *src = INT_MULT3(*src, *mask, opacity, tmp);
 
1373
          mask++;
 
1374
          src += bytes;
 
1375
        }
 
1376
    }
 
1377
}
 
1378
 
 
1379
 
 
1380
inline void
 
1381
combine_mask_and_alpha_channel_stipple (guchar       *src,
 
1382
                                        const guchar *mask,
 
1383
                                        guint         opacity,
 
1384
                                        guint         length,
 
1385
                                        guint         bytes)
 
1386
{
 
1387
  gint mask_val;
 
1388
  gint tmp;
 
1389
 
 
1390
  /* align with alpha channel */
 
1391
  src += bytes - 1;
 
1392
 
 
1393
  if (opacity != 255)
 
1394
    while (length --)
 
1395
      {
 
1396
        mask_val = INT_MULT(*mask, opacity, tmp);
 
1397
        *src = *src + INT_MULT((255 - *src) , mask_val, tmp);
 
1398
 
 
1399
        src += bytes;
 
1400
        mask++;
 
1401
      }
 
1402
  else
 
1403
    while (length --)
 
1404
      {
 
1405
        *src = *src + INT_MULT((255 - *src) , *mask, tmp);
 
1406
 
 
1407
        src += bytes;
 
1408
        mask++;
 
1409
      }
 
1410
}
 
1411
 
 
1412
 
 
1413
inline void
 
1414
combine_mask_and_alpha_channel_stroke (guchar       *src,
 
1415
                                       const guchar *mask,
 
1416
                                       guint         opacity,
 
1417
                                       guint         length,
 
1418
                                       guint         bytes)
 
1419
{
 
1420
  gint mask_val;
 
1421
  gint tmp;
 
1422
 
 
1423
  /* align with alpha channel */
 
1424
  src += bytes - 1;
 
1425
 
 
1426
  if (opacity != 255)
 
1427
    while (length --)
 
1428
      {
 
1429
        if (opacity > *src)
 
1430
          {
 
1431
            mask_val = INT_MULT(*mask, opacity, tmp);
 
1432
            *src = *src + INT_MULT((opacity - *src) , mask_val, tmp);
 
1433
          }
 
1434
 
 
1435
        src += bytes;
 
1436
        mask++;
 
1437
      }
 
1438
  else
 
1439
    while (length --)
 
1440
      {
 
1441
        *src = *src + INT_MULT((255 - *src) , *mask, tmp);
 
1442
 
 
1443
        src += bytes;
 
1444
        mask++;
 
1445
      }
 
1446
}
 
1447
 
 
1448
 
 
1449
inline void
 
1450
copy_gray_to_inten_a_pixels (const guchar *src,
 
1451
                             guchar       *dest,
 
1452
                             guint         length,
 
1453
                             guint         bytes)
 
1454
{
 
1455
  gint b;
 
1456
  gint alpha;
 
1457
 
 
1458
  alpha = bytes - 1;
 
1459
 
 
1460
  while (length --)
 
1461
    {
 
1462
      for (b = 0; b < alpha; b++)
 
1463
        dest[b] = *src;
 
1464
      dest[b] = OPAQUE_OPACITY;
 
1465
 
 
1466
      src ++;
 
1467
      dest += bytes;
 
1468
    }
 
1469
}
 
1470
 
 
1471
 
 
1472
inline void
 
1473
initial_channel_pixels (const guchar *src,
 
1474
                        guchar       *dest,
 
1475
                        guint         length,
 
1476
                        guint         bytes)
 
1477
{
 
1478
  gint alpha, b;
 
1479
 
 
1480
  alpha = bytes - 1;
 
1481
 
 
1482
  while (length --)
 
1483
    {
 
1484
      for (b = 0; b < alpha; b++)
 
1485
        dest[b] = src[0];
 
1486
 
 
1487
      dest[alpha] = OPAQUE_OPACITY;
 
1488
 
 
1489
      dest += bytes;
 
1490
      src ++;
 
1491
    }
 
1492
}
 
1493
 
 
1494
 
 
1495
inline void
 
1496
initial_indexed_pixels (const guchar *src,
 
1497
                        guchar       *dest,
 
1498
                        const guchar *cmap,
 
1499
                        guint         length)
 
1500
{
 
1501
  gint col_index;
 
1502
 
 
1503
  /*  This function assumes always that we're mapping from
 
1504
   *  an RGB colormap to an RGBA image...
 
1505
   */
 
1506
  while (length--)
 
1507
    {
 
1508
      col_index = *src++ * 3;
 
1509
      *dest++ = cmap[col_index++];
 
1510
      *dest++ = cmap[col_index++];
 
1511
      *dest++ = cmap[col_index++];
 
1512
      *dest++ = OPAQUE_OPACITY;
 
1513
    }
 
1514
}
 
1515
 
 
1516
 
 
1517
inline void
 
1518
initial_indexed_a_pixels (const guchar *src,
 
1519
                          guchar       *dest,
 
1520
                          const guchar *mask,
 
1521
                          const guchar *no_mask,
 
1522
                          const guchar *cmap,
 
1523
                          guint         opacity,
 
1524
                          guint         length)
 
1525
{
 
1526
  gint          col_index;
 
1527
  guchar        new_alpha;
 
1528
  const guchar *m;
 
1529
  glong         tmp;
 
1530
 
 
1531
  if (mask)
 
1532
    m = mask;
 
1533
  else
 
1534
    m = no_mask;
 
1535
 
 
1536
  while (length --)
 
1537
    {
 
1538
      col_index = *src++ * 3;
 
1539
      new_alpha = INT_MULT3(*src, *m, opacity, tmp);
 
1540
      src++;
 
1541
      *dest++ = cmap[col_index++];
 
1542
      *dest++ = cmap[col_index++];
 
1543
      *dest++ = cmap[col_index++];
 
1544
      /*  Set the alpha channel  */
 
1545
      *dest++ = (new_alpha > 127) ? OPAQUE_OPACITY : TRANSPARENT_OPACITY;
 
1546
 
 
1547
      if (mask)
 
1548
        m++;
 
1549
    }
 
1550
}
 
1551
 
 
1552
 
 
1553
inline void
 
1554
initial_inten_pixels (const guchar   *src,
 
1555
                      guchar         *dest,
 
1556
                      const guchar   *mask,
 
1557
                      const guchar   *no_mask,
 
1558
                      guint           opacity,
 
1559
                      const gboolean *affect,
 
1560
                      guint           length,
 
1561
                      guint           bytes)
 
1562
{
 
1563
  gint  b;
 
1564
  gint  tmp;
 
1565
  gint  l;
 
1566
  const guchar *m;
 
1567
  guchar       *destp;
 
1568
  const guchar *srcp;
 
1569
  const gint    dest_bytes = bytes + 1;
 
1570
 
 
1571
  if (mask)
 
1572
    {
 
1573
      m = mask;
 
1574
 
 
1575
      /*  This function assumes the source has no alpha channel and
 
1576
       *  the destination has an alpha channel.  So dest_bytes = bytes + 1
 
1577
       */
 
1578
 
 
1579
      if (bytes == 3 && affect[0] && affect[1] && affect[2])
 
1580
        {
 
1581
          if (!affect[bytes])
 
1582
            opacity = 0;
 
1583
 
 
1584
          destp = dest + bytes;
 
1585
 
 
1586
          if (opacity != 0)
 
1587
            while(length--)
 
1588
              {
 
1589
                dest[0] = src[0];
 
1590
                dest[1] = src[1];
 
1591
                dest[2] = src[2];
 
1592
                dest[3] = INT_MULT(opacity, *m, tmp);
 
1593
                src  += bytes;
 
1594
                dest += dest_bytes;
 
1595
                m++;
 
1596
              }
 
1597
          else
 
1598
            while(length--)
 
1599
              {
 
1600
                dest[0] = src[0];
 
1601
                dest[1] = src[1];
 
1602
                dest[2] = src[2];
 
1603
                dest[3] = opacity;
 
1604
                src  += bytes;
 
1605
                dest += dest_bytes;
 
1606
              }
 
1607
          return;
 
1608
        }
 
1609
 
 
1610
      for (b =0; b < bytes; b++)
 
1611
        {
 
1612
          destp = dest + b;
 
1613
          srcp = src + b;
 
1614
          l = length;
 
1615
 
 
1616
          if (affect[b])
 
1617
            while(l--)
 
1618
              {
 
1619
                *destp = *srcp;
 
1620
                srcp  += bytes;
 
1621
                destp += dest_bytes;
 
1622
              }
 
1623
          else
 
1624
            while(l--)
 
1625
              {
 
1626
                *destp = 0;
 
1627
                destp += dest_bytes;
 
1628
              }
 
1629
        }
 
1630
 
 
1631
      /* fill the alpha channel */
 
1632
      if (!affect[bytes])
 
1633
        opacity = 0;
 
1634
 
 
1635
      destp = dest + bytes;
 
1636
 
 
1637
      if (opacity != 0)
 
1638
        while (length--)
 
1639
          {
 
1640
            *destp = INT_MULT(opacity , *m, tmp);
 
1641
            destp += dest_bytes;
 
1642
            m++;
 
1643
          }
 
1644
      else
 
1645
        while (length--)
 
1646
          {
 
1647
            *destp = opacity;
 
1648
            destp += dest_bytes;
 
1649
          }
 
1650
    }
 
1651
  else  /* If no mask */
 
1652
    {
 
1653
      /*  This function assumes the source has no alpha channel and
 
1654
       *  the destination has an alpha channel.  So dest_bytes = bytes + 1
 
1655
       */
 
1656
 
 
1657
      if (bytes == 3 && affect[0] && affect[1] && affect[2])
 
1658
        {
 
1659
          if (!affect[bytes])
 
1660
            opacity = 0;
 
1661
 
 
1662
          destp = dest + bytes;
 
1663
 
 
1664
          while(length--)
 
1665
            {
 
1666
              dest[0] = src[0];
 
1667
              dest[1] = src[1];
 
1668
              dest[2] = src[2];
 
1669
              dest[3] = opacity;
 
1670
              src  += bytes;
 
1671
              dest += dest_bytes;
 
1672
            }
 
1673
          return;
 
1674
        }
 
1675
 
 
1676
      for (b = 0; b < bytes; b++)
 
1677
        {
 
1678
          destp = dest + b;
 
1679
          srcp = src + b;
 
1680
          l = length;
 
1681
 
 
1682
          if (affect[b])
 
1683
            while(l--)
 
1684
              {
 
1685
                *destp = *srcp;
 
1686
                srcp  += bytes;
 
1687
                destp += dest_bytes;
 
1688
              }
 
1689
          else
 
1690
            while(l--)
 
1691
              {
 
1692
                *destp = 0;
 
1693
                destp += dest_bytes;
 
1694
              }
 
1695
      }
 
1696
 
 
1697
      /* fill the alpha channel */
 
1698
      if (!affect[bytes])
 
1699
        opacity = 0;
 
1700
 
 
1701
      destp = dest + bytes;
 
1702
 
 
1703
      while (length--)
 
1704
        {
 
1705
          *destp = opacity;
 
1706
          destp += dest_bytes;
 
1707
        }
 
1708
    }
 
1709
}
 
1710
 
 
1711
 
 
1712
inline void
 
1713
initial_inten_a_pixels (const guchar   *src,
 
1714
                        guchar         *dest,
 
1715
                        const guchar   *mask,
 
1716
                        guint           opacity,
 
1717
                        const gboolean *affect,
 
1718
                        guint           length,
 
1719
                        guint           bytes)
 
1720
{
 
1721
  gint          alpha, b;
 
1722
  const guchar *m;
 
1723
  glong         tmp;
 
1724
 
 
1725
  alpha = bytes - 1;
 
1726
  if (mask)
 
1727
    {
 
1728
      m = mask;
 
1729
      while (length --)
 
1730
        {
 
1731
          for (b = 0; b < alpha; b++)
 
1732
            dest[b] = src[b] * affect[b];
 
1733
 
 
1734
          /*  Set the alpha channel  */
 
1735
          dest[alpha] = affect [alpha] ? INT_MULT3(opacity, src[alpha], *m, tmp)
 
1736
            : 0;
 
1737
 
 
1738
          m++;
 
1739
 
 
1740
          dest += bytes;
 
1741
          src += bytes;
 
1742
        }
 
1743
    }
 
1744
  else
 
1745
    {
 
1746
      while (length --)
 
1747
        {
 
1748
          for (b = 0; b < alpha; b++)
 
1749
            dest[b] = src[b] * affect[b];
 
1750
 
 
1751
          /*  Set the alpha channel  */
 
1752
          dest[alpha] = affect [alpha] ? INT_MULT(opacity , src[alpha], tmp) : 0;
 
1753
 
 
1754
          dest += bytes;
 
1755
          src += bytes;
 
1756
        }
 
1757
    }
 
1758
}
 
1759
 
 
1760
inline void
 
1761
component_pixels (const guchar *src,
 
1762
                  guchar       *dest,
 
1763
                  guint         length,
 
1764
                  guint         bytes,
 
1765
                  guint         pixel)
 
1766
{
 
1767
  src += pixel;
 
1768
 
 
1769
  while (length --)
 
1770
    {
 
1771
      *dest = *src;
 
1772
 
 
1773
      src += bytes;
 
1774
      dest++;
 
1775
    }
 
1776
}
 
1777
 
 
1778
 
 
1779
static void
 
1780
layer_normal_mode (struct apply_layer_mode_struct *alms)
 
1781
{
 
1782
  /*  assumes we're applying src2 TO src1  */
 
1783
  *(alms->dest) = alms->src2;
 
1784
}
 
1785
 
 
1786
static void
 
1787
layer_dissolve_mode (struct apply_layer_mode_struct *alms)
 
1788
{
 
1789
  const guint has_alpha1 = HAS_ALPHA (alms->bytes1);
 
1790
  const guint has_alpha2 = HAS_ALPHA (alms->bytes2);
 
1791
  guint dest_bytes;
 
1792
 
 
1793
  /*  Since dissolve requires an alpha channel...  */
 
1794
  if (has_alpha2)
 
1795
    dest_bytes = alms->bytes2;
 
1796
  else
 
1797
    dest_bytes = alms->bytes2 + 1;
 
1798
 
 
1799
  dissolve_pixels (alms->src2, alms->mask, *(alms->dest),
 
1800
                   alms->x, alms->y,
 
1801
                   alms->opacity, alms->length,
 
1802
                   alms->bytes2, dest_bytes,
 
1803
                   has_alpha2);
 
1804
 
 
1805
  alms->combine = has_alpha1 ? COMBINE_INTEN_A_INTEN_A : COMBINE_INTEN_INTEN_A;
 
1806
}
 
1807
 
 
1808
static void
 
1809
layer_multiply_mode (struct apply_layer_mode_struct *alms)
 
1810
{
 
1811
  multiply_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1812
                   alms->bytes1, alms->bytes2);
 
1813
}
 
1814
 
 
1815
static void
 
1816
layer_divide_mode (struct apply_layer_mode_struct *alms)
 
1817
{
 
1818
  divide_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1819
                 alms->bytes1, alms->bytes2);
 
1820
}
 
1821
 
 
1822
static void
 
1823
layer_screen_mode (struct apply_layer_mode_struct *alms)
 
1824
{
 
1825
  screen_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1826
                 alms->bytes1, alms->bytes2);
 
1827
}
 
1828
 
 
1829
static void
 
1830
layer_overlay_mode (struct apply_layer_mode_struct *alms)
 
1831
{
 
1832
  overlay_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1833
                  alms->bytes1, alms->bytes2);
 
1834
}
 
1835
 
 
1836
static void
 
1837
layer_difference_mode (struct apply_layer_mode_struct *alms)
 
1838
{
 
1839
  difference_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1840
                     alms->bytes1, alms->bytes2);
 
1841
}
 
1842
 
 
1843
static void
 
1844
layer_addition_mode (struct apply_layer_mode_struct *alms)
 
1845
{
 
1846
  add_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1847
              alms->bytes1, alms->bytes2);
 
1848
}
 
1849
 
 
1850
static void
 
1851
layer_subtract_mode (struct apply_layer_mode_struct *alms)
 
1852
{
 
1853
  subtract_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1854
                   alms->bytes1, alms->bytes2);
 
1855
}
 
1856
 
 
1857
static void
 
1858
layer_darken_only_mode (struct apply_layer_mode_struct *alms)
 
1859
{
 
1860
  darken_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1861
                 alms->bytes1, alms->bytes2);
 
1862
}
 
1863
 
 
1864
static void
 
1865
layer_lighten_only_mode (struct apply_layer_mode_struct *alms)
 
1866
{
 
1867
  lighten_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1868
                  alms->bytes1, alms->bytes2);
 
1869
}
 
1870
 
 
1871
static void
 
1872
layer_hue_mode (struct apply_layer_mode_struct *alms)
 
1873
{
 
1874
  /*  only works on RGB color images  */
 
1875
  if (alms->bytes1 > 2)
 
1876
    hue_only_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1877
                     alms->bytes1, alms->bytes2);
 
1878
  else
 
1879
    *(alms->dest) = alms->src2;
 
1880
}
 
1881
 
 
1882
static void
 
1883
layer_saturation_mode (struct apply_layer_mode_struct *alms)
 
1884
{
 
1885
  /*  only works on RGB color images  */
 
1886
  if (alms->bytes1 > 2)
 
1887
    saturation_only_pixels (alms->src1, alms->src2, *(alms->dest),
 
1888
                            alms->length, alms->bytes1, alms->bytes2);
 
1889
  else
 
1890
    *(alms->dest) = alms->src2;
 
1891
}
 
1892
 
 
1893
static void
 
1894
layer_value_mode (struct apply_layer_mode_struct *alms)
 
1895
{
 
1896
  /*  only works on RGB color images  */
 
1897
  if (alms->bytes1 > 2)
 
1898
    value_only_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1899
                       alms->bytes1, alms->bytes2);
 
1900
  else
 
1901
    *(alms->dest) = alms->src2;
 
1902
}
 
1903
 
 
1904
static void
 
1905
layer_color_mode (struct apply_layer_mode_struct *alms)
 
1906
{
 
1907
  /*  only works on RGB color images  */
 
1908
  if (alms->bytes1 > 2)
 
1909
    color_only_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1910
                       alms->bytes1, alms->bytes2);
 
1911
  else
 
1912
    *(alms->dest) = alms->src2;
 
1913
}
 
1914
 
 
1915
static void
 
1916
layer_behind_mode (struct apply_layer_mode_struct *alms)
 
1917
{
 
1918
  *(alms->dest) = alms->src2;
 
1919
  if (HAS_ALPHA (alms->bytes1))
 
1920
    alms->combine = BEHIND_INTEN;
 
1921
  else
 
1922
    alms->combine = NO_COMBINATION;
 
1923
}
 
1924
 
 
1925
static void
 
1926
layer_replace_mode (struct apply_layer_mode_struct *alms)
 
1927
{
 
1928
  *(alms->dest) = alms->src2;
 
1929
  alms->combine = REPLACE_INTEN;
 
1930
}
 
1931
 
 
1932
static void
 
1933
layer_erase_mode (struct apply_layer_mode_struct *alms)
 
1934
{
 
1935
  *(alms->dest) = alms->src2;
 
1936
  /*  If both sources have alpha channels, call erase function.
 
1937
   *  Otherwise, just combine in the normal manner
 
1938
   */
 
1939
  alms->combine =
 
1940
    (HAS_ALPHA (alms->bytes1) && HAS_ALPHA (alms->bytes2)) ?  ERASE_INTEN : 0;
 
1941
}
 
1942
 
 
1943
static void
 
1944
layer_anti_erase_mode (struct apply_layer_mode_struct *alms)
 
1945
{
 
1946
  *(alms->dest) = alms->src2;
 
1947
  alms->combine =
 
1948
    (HAS_ALPHA (alms->bytes1) && HAS_ALPHA (alms->bytes2)) ? ANTI_ERASE_INTEN : 0;
 
1949
}
 
1950
 
 
1951
static void
 
1952
layer_color_erase_mode (struct apply_layer_mode_struct *alms)
 
1953
{
 
1954
  *(alms->dest) = alms->src2;
 
1955
  alms->combine =
 
1956
    (HAS_ALPHA (alms->bytes1) && HAS_ALPHA (alms->bytes2)) ? COLOR_ERASE_INTEN : 0;
 
1957
}
 
1958
 
 
1959
static void
 
1960
layer_dodge_mode (struct apply_layer_mode_struct *alms)
 
1961
{
 
1962
  dodge_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1963
                alms->bytes1, alms->bytes2);
 
1964
}
 
1965
 
 
1966
static void
 
1967
layer_burn_mode (struct apply_layer_mode_struct *alms)
 
1968
{
 
1969
  burn_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1970
               alms->bytes1, alms->bytes2);
 
1971
}
 
1972
 
 
1973
static void
 
1974
layer_hardlight_mode (struct apply_layer_mode_struct *alms)
 
1975
{
 
1976
  hardlight_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1977
                    alms->bytes1, alms->bytes2);
 
1978
}
 
1979
 
 
1980
static void
 
1981
layer_softlight_mode (struct apply_layer_mode_struct *alms)
 
1982
{
 
1983
  softlight_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1984
                    alms->bytes1, alms->bytes2);
 
1985
}
 
1986
 
 
1987
static void
 
1988
layer_grain_extract_mode (struct apply_layer_mode_struct *alms)
 
1989
{
 
1990
  grain_extract_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1991
                        alms->bytes1, alms->bytes2);
 
1992
}
 
1993
 
 
1994
static void
 
1995
layer_grain_merge_mode (struct apply_layer_mode_struct *alms)
 
1996
{
 
1997
  grain_merge_pixels (alms->src1, alms->src2, *(alms->dest), alms->length,
 
1998
                      alms->bytes1, alms->bytes2);
 
1999
}
 
2000
 
 
2001
#endif  /*  __PAINT_FUNCS_GENERIC_H__  */