summaryrefslogtreecommitdiff
path: root/src/ui/gradient.c
blob: 78a5a9eb22dba5d41ec1896c4bd0534d38c31d47 (plain)
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/* Marco gradient rendering */

/*
 * Copyright (C) 2001 Havoc Pennington, 99% copied from wrlib in
 * WindowMaker, Copyright (C) 1997-2000 Dan Pascu and Alfredo Kojima
 * Copyright (C) 2005 Elijah Newren
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.  */

#include "gradient.h"
#include "util.h"
#include <string.h>

/* This is all Alfredo's and Dan's usual very nice WindowMaker code,
 * slightly GTK-ized
 */
static GdkPixbuf* meta_gradient_create_horizontal       (int             width,
                                                         int             height,
                                                         const GdkColor *from,
                                                         const GdkColor *to);
static GdkPixbuf* meta_gradient_create_vertical         (int             width,
                                                         int             height,
                                                         const GdkColor *from,
                                                         const GdkColor *to);
static GdkPixbuf* meta_gradient_create_diagonal         (int             width,
                                                         int             height,
                                                         const GdkColor *from,
                                                         const GdkColor *to);
static GdkPixbuf* meta_gradient_create_multi_horizontal (int             width,
                                                         int             height,
                                                         const GdkColor *colors,
                                                         int             count);
static GdkPixbuf* meta_gradient_create_multi_vertical   (int             width,
                                                         int             height,
                                                         const GdkColor *colors,
                                                         int             count);
static GdkPixbuf* meta_gradient_create_multi_diagonal   (int             width,
                                                         int             height,
                                                         const GdkColor *colors,
                                                         int             count);


/* Used as the destroy notification function for gdk_pixbuf_new() */
static void
free_buffer (guchar *pixels, gpointer data)
{
  g_free (pixels);
}

static GdkPixbuf*
blank_pixbuf (int width, int height, gboolean no_padding)
{
  guchar *buf;
  int rowstride;

  g_return_val_if_fail (width > 0, NULL);
  g_return_val_if_fail (height > 0, NULL);

  if (no_padding)
    rowstride = width * 3;
  else
    /* Always align rows to 32-bit boundaries */
    rowstride = 4 * ((3 * width + 3) / 4);

  buf = g_try_malloc (height * rowstride);
  if (!buf)
    return NULL;

  return gdk_pixbuf_new_from_data (buf, GDK_COLORSPACE_RGB,
                                   FALSE, 8,
                                   width, height, rowstride,
                                   free_buffer, NULL);
}

GdkPixbuf*
meta_gradient_create_simple (int              width,
                             int              height,
                             const GdkColor  *from,
                             const GdkColor  *to,
                             MetaGradientType style)
{
  switch (style)
    {
    case META_GRADIENT_HORIZONTAL:
      return meta_gradient_create_horizontal (width, height,
                                              from, to);
    case META_GRADIENT_VERTICAL:
      return meta_gradient_create_vertical (width, height,
                                            from, to);

    case META_GRADIENT_DIAGONAL:
      return meta_gradient_create_diagonal (width, height,
                                            from, to);
    case META_GRADIENT_LAST:
      break;
    }
  g_assert_not_reached ();
  return NULL;
}

GdkPixbuf*
meta_gradient_create_multi (int              width,
                            int              height,
                            const GdkColor  *colors,
                            int              n_colors,
                            MetaGradientType style)
{

  if (n_colors > 2)
    {
      switch (style)
        {
        case META_GRADIENT_HORIZONTAL:
          return meta_gradient_create_multi_horizontal (width, height, colors, n_colors);
        case META_GRADIENT_VERTICAL:
          return meta_gradient_create_multi_vertical (width, height, colors, n_colors);
        case META_GRADIENT_DIAGONAL:
          return meta_gradient_create_multi_diagonal (width, height, colors, n_colors);
        case META_GRADIENT_LAST:
          g_assert_not_reached ();
          break;
        }
    }
  else if (n_colors > 1)
    {
      return meta_gradient_create_simple (width, height, &colors[0], &colors[1],
                                          style);
    }
  else if (n_colors > 0)
    {
      return meta_gradient_create_simple (width, height, &colors[0], &colors[0],
                                          style);
    }
  g_assert_not_reached ();
  return NULL;
}

/* Interwoven essentially means we have two vertical gradients,
 * cut into horizontal strips of the given thickness, and then the strips
 * are alternated. I'm not sure what it's good for, just copied since
 * WindowMaker had it.
 */
GdkPixbuf*
meta_gradient_create_interwoven (int            width,
                                 int            height,
                                 const GdkColor colors1[2],
                                 int            thickness1,
                                 const GdkColor colors2[2],
                                 int            thickness2)
{

  int i, j, k, l, ll;
  long r1, g1, b1, dr1, dg1, db1;
  long r2, g2, b2, dr2, dg2, db2;
  GdkPixbuf *pixbuf;
  unsigned char *ptr;
  unsigned char *pixels;
  int rowstride;

  pixbuf = blank_pixbuf (width, height, FALSE);
  if (pixbuf == NULL)
    return NULL;

  pixels = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  r1 = colors1[0].red<<8;
  g1 = colors1[0].green<<8;
  b1 = colors1[0].blue<<8;

  r2 = colors2[0].red<<8;
  g2 = colors2[0].green<<8;
  b2 = colors2[0].blue<<8;

  dr1 = ((colors1[1].red-colors1[0].red)<<8)/(int)height;
  dg1 = ((colors1[1].green-colors1[0].green)<<8)/(int)height;
  db1 = ((colors1[1].blue-colors1[0].blue)<<8)/(int)height;

  dr2 = ((colors2[1].red-colors2[0].red)<<8)/(int)height;
  dg2 = ((colors2[1].green-colors2[0].green)<<8)/(int)height;
  db2 = ((colors2[1].blue-colors2[0].blue)<<8)/(int)height;

  for (i=0,k=0,l=0,ll=thickness1; i<height; i++)
    {
      ptr = pixels + i * rowstride;

      if (k == 0)
        {
          ptr[0] = (unsigned char) (r1>>16);
          ptr[1] = (unsigned char) (g1>>16);
          ptr[2] = (unsigned char) (b1>>16);
        }
      else
        {
          ptr[0] = (unsigned char) (r2>>16);
          ptr[1] = (unsigned char) (g2>>16);
          ptr[2] = (unsigned char) (b2>>16);
        }

      for (j=1; j <= width/2; j *= 2)
        memcpy (&(ptr[j*3]), ptr, j*3);
      memcpy (&(ptr[j*3]), ptr, (width - j)*3);

      if (++l == ll)
        {
          if (k == 0)
            {
              k = 1;
              ll = thickness2;
            }
          else
            {
              k = 0;
              ll = thickness1;
            }
          l = 0;
        }
      r1+=dr1;
      g1+=dg1;
      b1+=db1;

      r2+=dr2;
      g2+=dg2;
      b2+=db2;
    }

  return pixbuf;
}

/*
 *----------------------------------------------------------------------
 * meta_gradient_create_horizontal--
 * 	Renders a horizontal linear gradient of the specified size in the
 * GdkPixbuf format with a border of the specified type.
 *
 * Returns:
 * 	A 24bit GdkPixbuf with the gradient (no alpha channel).
 *
 * Side effects:
 * 	None
 *----------------------------------------------------------------------
 */
static GdkPixbuf*
meta_gradient_create_horizontal (int width, int height,
                                 const GdkColor *from,
                                 const GdkColor *to)
{
  int i;
  long r, g, b, dr, dg, db;
  GdkPixbuf *pixbuf;
  unsigned char *ptr;
  unsigned char *pixels;
  int r0, g0, b0;
  int rf, gf, bf;
  int rowstride;

  pixbuf = blank_pixbuf (width, height, FALSE);
  if (pixbuf == NULL)
    return NULL;

  pixels = gdk_pixbuf_get_pixels (pixbuf);
  ptr = pixels;
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  r0 = (guchar) (from->red / 256.0);
  g0 = (guchar) (from->green / 256.0);
  b0 = (guchar) (from->blue / 256.0);
  rf = (guchar) (to->red / 256.0);
  gf = (guchar) (to->green / 256.0);
  bf = (guchar) (to->blue / 256.0);

  r = r0 << 16;
  g = g0 << 16;
  b = b0 << 16;

  dr = ((rf-r0)<<16)/(int)width;
  dg = ((gf-g0)<<16)/(int)width;
  db = ((bf-b0)<<16)/(int)width;
  /* render the first line */
  for (i=0; i<width; i++)
    {
      *(ptr++) = (unsigned char)(r>>16);
      *(ptr++) = (unsigned char)(g>>16);
      *(ptr++) = (unsigned char)(b>>16);
      r += dr;
      g += dg;
      b += db;
    }

  /* copy the first line to the other lines */
  for (i=1; i<height; i++)
    {
      memcpy (&(pixels[i*rowstride]), pixels, rowstride);
    }
  return pixbuf;
}

/*
 *----------------------------------------------------------------------
 * meta_gradient_create_vertical--
 *      Renders a vertical linear gradient of the specified size in the
 * GdkPixbuf format with a border of the specified type.
 *
 * Returns:
 *      A 24bit GdkPixbuf with the gradient (no alpha channel).
 *
 * Side effects:
 *      None
 *----------------------------------------------------------------------
 */
static GdkPixbuf*
meta_gradient_create_vertical (int width, int height,
                               const GdkColor *from,
                               const GdkColor *to)
{
  int i, j;
  long r, g, b, dr, dg, db;
  GdkPixbuf *pixbuf;
  unsigned char *ptr;
  int r0, g0, b0;
  int rf, gf, bf;
  int rowstride;
  unsigned char *pixels;

  pixbuf = blank_pixbuf (width, height, FALSE);
  if (pixbuf == NULL)
    return NULL;

  pixels = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  r0 = (guchar) (from->red / 256.0);
  g0 = (guchar) (from->green / 256.0);
  b0 = (guchar) (from->blue / 256.0);
  rf = (guchar) (to->red / 256.0);
  gf = (guchar) (to->green / 256.0);
  bf = (guchar) (to->blue / 256.0);

  r = r0<<16;
  g = g0<<16;
  b = b0<<16;

  dr = ((rf-r0)<<16)/(int)height;
  dg = ((gf-g0)<<16)/(int)height;
  db = ((bf-b0)<<16)/(int)height;

  for (i=0; i<height; i++)
    {
      ptr = pixels + i * rowstride;

      ptr[0] = (unsigned char)(r>>16);
      ptr[1] = (unsigned char)(g>>16);
      ptr[2] = (unsigned char)(b>>16);

      for (j=1; j <= width/2; j *= 2)
        memcpy (&(ptr[j*3]), ptr, j*3);
      memcpy (&(ptr[j*3]), ptr, (width - j)*3);

      r+=dr;
      g+=dg;
      b+=db;
    }
  return pixbuf;
}


/*
 *----------------------------------------------------------------------
 * meta_gradient_create_diagonal--
 *      Renders a diagonal linear gradient of the specified size in the
 * GdkPixbuf format with a border of the specified type.
 *
 * Returns:
 *      A 24bit GdkPixbuf with the gradient (no alpha channel).
 *
 * Side effects:
 *      None
 *----------------------------------------------------------------------
 */


static GdkPixbuf*
meta_gradient_create_diagonal (int width, int height,
                               const GdkColor *from,
                               const GdkColor *to)
{
  GdkPixbuf *pixbuf, *tmp;
  int j;
  float a, offset;
  unsigned char *ptr;
  unsigned char *pixels;
  int rowstride;

  if (width == 1)
    return meta_gradient_create_vertical (width, height, from, to);
  else if (height == 1)
    return meta_gradient_create_horizontal (width, height, from, to);

  pixbuf = blank_pixbuf (width, height, FALSE);
  if (pixbuf == NULL)
    return NULL;

  pixels = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  tmp = meta_gradient_create_horizontal (2*width-1, 1, from, to);
  if (!tmp)
    {
      g_object_unref (G_OBJECT (pixbuf));
      return NULL;
    }

  ptr = gdk_pixbuf_get_pixels (tmp);

  a = ((float)(width - 1))/((float)(height - 1));
  width = width * 3;

  /* copy the first line to the other lines with corresponding offset */
  for (j=0, offset=0.0; j<rowstride*height; j += rowstride)
    {
      memcpy (&(pixels[j]), &ptr[3*(int)offset], width);
      offset += a;
    }

  g_object_unref (G_OBJECT (tmp));
  return pixbuf;
}


static GdkPixbuf*
meta_gradient_create_multi_horizontal (int width, int height,
                                       const GdkColor *colors,
                                       int count)
{
  int i, j, k;
  long r, g, b, dr, dg, db;
  GdkPixbuf *pixbuf;
  unsigned char *ptr;
  unsigned char *pixels;
  int width2;
  int rowstride;

  g_return_val_if_fail (count > 2, NULL);

  pixbuf = blank_pixbuf (width, height, FALSE);
  if (pixbuf == NULL)
    return NULL;

  pixels = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  ptr = pixels;

  if (count > width)
    count = width;

  if (count > 1)
    width2 = width/(count-1);
  else
    width2 = width;

  k = 0;

  r = colors[0].red << 8;
  g = colors[0].green << 8;
  b = colors[0].blue << 8;

  /* render the first line */
  for (i=1; i<count; i++)
    {
      dr = ((int)(colors[i].red   - colors[i-1].red)  <<8)/(int)width2;
      dg = ((int)(colors[i].green - colors[i-1].green)<<8)/(int)width2;
      db = ((int)(colors[i].blue  - colors[i-1].blue) <<8)/(int)width2;
      for (j=0; j<width2; j++)
        {
          *ptr++ = (unsigned char)(r>>16);
          *ptr++ = (unsigned char)(g>>16);
          *ptr++ = (unsigned char)(b>>16);
          r += dr;
          g += dg;
          b += db;
          k++;
	}
      r = colors[i].red << 8;
      g = colors[i].green << 8;
      b = colors[i].blue << 8;
    }
  for (j=k; j<width; j++)
    {
      *ptr++ = (unsigned char)(r>>16);
      *ptr++ = (unsigned char)(g>>16);
      *ptr++ = (unsigned char)(b>>16);
    }

  /* copy the first line to the other lines */
  for (i=1; i<height; i++)
    {
      memcpy (&(pixels[i*rowstride]), pixels, rowstride);
    }
  return pixbuf;
}

static GdkPixbuf*
meta_gradient_create_multi_vertical (int width, int height,
                                     const GdkColor *colors,
                                     int count)
{
  int i, j, k;
  long r, g, b, dr, dg, db;
  GdkPixbuf *pixbuf;
  unsigned char *ptr, *tmp, *pixels;
  int height2;
  int x;
  int rowstride;

  g_return_val_if_fail (count > 2, NULL);

  pixbuf = blank_pixbuf (width, height, FALSE);
  if (pixbuf == NULL)
    return NULL;

  pixels = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  ptr = pixels;

  if (count > height)
    count = height;

  if (count > 1)
    height2 = height/(count-1);
  else
    height2 = height;

  k = 0;

  r = colors[0].red << 8;
  g = colors[0].green << 8;
  b = colors[0].blue << 8;

  for (i=1; i<count; i++)
    {
      dr = ((int)(colors[i].red   - colors[i-1].red)  <<8)/(int)height2;
      dg = ((int)(colors[i].green - colors[i-1].green)<<8)/(int)height2;
      db = ((int)(colors[i].blue  - colors[i-1].blue) <<8)/(int)height2;

      for (j=0; j<height2; j++)
        {
          ptr[0] = (unsigned char)(r>>16);
          ptr[1] = (unsigned char)(g>>16);
          ptr[2] = (unsigned char)(b>>16);

          for (x=1; x <= width/2; x *= 2)
            memcpy (&(ptr[x*3]), ptr, x*3);
          memcpy (&(ptr[x*3]), ptr, (width - x)*3);

          ptr += rowstride;

          r += dr;
          g += dg;
          b += db;
          k++;
	}
      r = colors[i].red << 8;
      g = colors[i].green << 8;
      b = colors[i].blue << 8;
    }

  if (k<height)
    {
      tmp = ptr;

      ptr[0] = (unsigned char) (r>>16);
      ptr[1] = (unsigned char) (g>>16);
      ptr[2] = (unsigned char) (b>>16);

      for (x=1; x <= width/2; x *= 2)
        memcpy (&(ptr[x*3]), ptr, x*3);
      memcpy (&(ptr[x*3]), ptr, (width - x)*3);

      ptr += rowstride;

      for (j=k+1; j<height; j++)
        {
          memcpy (ptr, tmp, rowstride);
          ptr += rowstride;
        }
    }

  return pixbuf;
}


static GdkPixbuf*
meta_gradient_create_multi_diagonal (int width, int height,
                                     const GdkColor *colors,
                                     int count)
{
  GdkPixbuf *pixbuf, *tmp;
  float a, offset;
  int j;
  unsigned char *ptr;
  unsigned char *pixels;
  int rowstride;

  g_return_val_if_fail (count > 2, NULL);

  if (width == 1)
    return meta_gradient_create_multi_vertical (width, height, colors, count);
  else if (height == 1)
    return meta_gradient_create_multi_horizontal (width, height, colors, count);

  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
                           width, height);
  if (pixbuf == NULL)
    return NULL;

  pixels = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  if (count > width)
    count = width;
  if (count > height)
    count = height;

  if (count > 2)
    tmp = meta_gradient_create_multi_horizontal (2*width-1, 1, colors, count);
  else
    /* wrlib multiplies these colors by 256 before passing them in, but
     * I think it's a bug in wrlib, so changed here. I could be wrong
     * though, if we notice two-color multi diagonals not working.
     */
    tmp = meta_gradient_create_horizontal (2*width-1, 1,
                                           &colors[0], &colors[1]);

  if (!tmp)
    {
      g_object_unref (G_OBJECT (pixbuf));
      return NULL;
    }
  ptr = gdk_pixbuf_get_pixels (tmp);

  a = ((float)(width - 1))/((float)(height - 1));
  width = width * 3;

  /* copy the first line to the other lines with corresponding offset */
  for (j=0, offset=0; j<rowstride*height; j += rowstride)
    {
      memcpy (&(pixels[j]), &ptr[3*(int)offset], width);
      offset += a;
    }

  g_object_unref (G_OBJECT (tmp));
  return pixbuf;
}

static void
simple_multiply_alpha (GdkPixbuf *pixbuf,
                       guchar     alpha)
{
  guchar *pixels;
  int rowstride;
  int height;
  int row;

  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));

  if (alpha == 255)
    return;

  g_assert (gdk_pixbuf_get_has_alpha (pixbuf));

  pixels = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  height = gdk_pixbuf_get_height (pixbuf);

  row = 0;
  while (row < height)
    {
      guchar *p;
      guchar *end;

      p = pixels + row * rowstride;
      end = p + rowstride;

      while (p != end)
        {
          p += 3; /* skip RGB */

          /* multiply the two alpha channels. not sure this is right.
           * but some end cases are that if the pixbuf contains 255,
           * then it should be modified to contain "alpha"; if the
           * pixbuf contains 0, it should remain 0.
           */
          /* ((*p / 255.0) * (alpha / 255.0)) * 255; */
          *p = (guchar) (((int) *p * (int) alpha) / (int) 255);

          ++p; /* skip A */
        }

      ++row;
    }
}

static void
meta_gradient_add_alpha_horizontal (GdkPixbuf           *pixbuf,
                                    const unsigned char *alphas,
                                    int                  n_alphas)
{
  int i, j;
  long a, da;
  unsigned char *p;
  unsigned char *pixels;
  int width2;
  int rowstride;
  int width, height;
  unsigned char *gradient;
  unsigned char *gradient_p;
  unsigned char *gradient_end;

  g_return_if_fail (n_alphas > 0);

  if (n_alphas == 1)
    {
      /* Optimize this */
      simple_multiply_alpha (pixbuf, alphas[0]);
      return;
    }

  width = gdk_pixbuf_get_width (pixbuf);
  height = gdk_pixbuf_get_height (pixbuf);

  gradient = g_new (unsigned char, width);
  gradient_end = gradient + width;

  if (n_alphas > width)
    n_alphas = width;

  if (n_alphas > 1)
    width2 = width / (n_alphas - 1);
  else
    width2 = width;

  a = alphas[0] << 8;
  gradient_p = gradient;

  /* render the gradient into an array */
  for (i = 1; i < n_alphas; i++)
    {
      da = (((int)(alphas[i] - (int) alphas[i-1])) << 8) / (int) width2;

      for (j = 0; j < width2; j++)
        {
          *gradient_p++ = (a >> 8);

          a += da;
	}

      a = alphas[i] << 8;
    }

  /* get leftover pixels */
  while (gradient_p != gradient_end)
    {
      *gradient_p++ = a >> 8;
    }

  /* Now for each line of the pixbuf, fill in with the gradient */
  pixels = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  p = pixels;
  i = 0;
  while (i < height)
    {
      unsigned char *row_end = p + rowstride;
      gradient_p = gradient;

      p += 3;
      while (gradient_p != gradient_end)
        {
          /* multiply the two alpha channels. not sure this is right.
           * but some end cases are that if the pixbuf contains 255,
           * then it should be modified to contain "alpha"; if the
           * pixbuf contains 0, it should remain 0.
           */
          /* ((*p / 255.0) * (alpha / 255.0)) * 255; */
          *p = (guchar) (((int) *p * (int) *gradient_p) / (int) 255);

          p += 4;
          ++gradient_p;
        }

      p = row_end;
      ++i;
    }

  g_free (gradient);
}

void
meta_gradient_add_alpha (GdkPixbuf       *pixbuf,
                         const guchar    *alphas,
                         int              n_alphas,
                         MetaGradientType type)
{
  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
  g_return_if_fail (gdk_pixbuf_get_has_alpha (pixbuf));
  g_return_if_fail (n_alphas > 0);

  switch (type)
    {
    case META_GRADIENT_HORIZONTAL:
      meta_gradient_add_alpha_horizontal (pixbuf, alphas, n_alphas);
      break;

    case META_GRADIENT_VERTICAL:
      g_printerr ("marco: vertical alpha channel gradient not implemented yet\n");
      break;

    case META_GRADIENT_DIAGONAL:
      g_printerr ("marco: diagonal alpha channel gradient not implemented yet\n");
      break;

    case META_GRADIENT_LAST:
      g_assert_not_reached ();
      break;
    }
}