]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-util.c
#include <config.h>, and fix includes.
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-util.c
1 /* GdkPixbuf library - Utilities and miscellaneous convenience functions
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Author: Federico Mena-Quintero <federico@gimp.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <config.h>
24 #include "gdk-pixbuf.h"
25
26 \f
27
28 /**
29  * gdk_pixbuf_add_alpha:
30  * @pixbuf: A pixbuf.
31  * @substitute_color: Whether to substitute a color for zero opacity.  If this
32  * is #FALSE, then the (@r, @g, @b) arguments will be ignored.
33  * @r: Red value to substitute.
34  * @g: Green value to substitute.
35  * @b: Blue value to substitute.
36  *
37  * Takes an existing pixbuf and adds an alpha channel to it.  If the original
38  * pixbuf already had alpha information, then the contents of the new pixbuf are
39  * exactly the same as the original's.  Otherwise, the new pixbuf will have all
40  * pixels with full opacity if @substitute_color is #FALSE.  If
41  * @substitute_color is #TRUE, then the color specified by (@r, @g, @b) will be
42  * substituted for zero opacity.
43  *
44  * Return value: A newly-created pixbuf with a reference count of 1.
45  **/
46 GdkPixbuf *
47 gdk_pixbuf_add_alpha (GdkPixbuf *pixbuf, gboolean substitute_color, guchar r, guchar g, guchar b)
48 {
49         ArtPixBuf *apb;
50         ArtPixBuf *new_apb;
51         GdkPixbuf *new_pixbuf;
52         int x, y;
53
54         g_return_val_if_fail (pixbuf != NULL, NULL);
55
56         apb = pixbuf->art_pixbuf;
57         g_return_val_if_fail (apb->format == ART_PIX_RGB, NULL);
58         g_return_val_if_fail (apb->n_channels == 3 || apb->n_channels == 4, NULL);
59         g_return_val_if_fail (apb->bits_per_sample == 8, NULL);
60
61         if (apb->has_alpha) {
62                 new_apb = art_pixbuf_duplicate (apb);
63                 if (!new_apb)
64                         return NULL;
65
66                 return gdk_pixbuf_new_from_art_pixbuf (new_apb);
67         }
68
69         new_pixbuf = gdk_pixbuf_new (ART_PIX_RGB, TRUE, 8, apb->width, apb->height);
70         if (!new_pixbuf)
71                 return NULL;
72
73         new_apb = new_pixbuf->art_pixbuf;
74
75         for (y = 0; y < apb->height; y++) {
76                 guchar *src, *dest;
77                 guchar tr, tg, tb;
78
79                 src = apb->pixels + y * apb->rowstride;
80                 dest = new_apb->pixels + y * new_apb->rowstride;
81
82                 for (x = 0; x < apb->width; x++) {
83                         tr = *dest++ = *src++;
84                         tg = *dest++ = *src++;
85                         tb = *dest++ = *src++;
86
87                         if (substitute_color && tr == r && tg == g && tb == b)
88                                 *dest++ = 0;
89                         else
90                                 *dest++ = 255;
91                 }
92         }
93
94         return new_pixbuf;
95 }