1 /* GdkPixbuf library - Utilities and miscellaneous convenience functions
3 * Copyright (C) 1999 The Free Software Foundation
5 * Author: Federico Mena-Quintero <federico@gimp.org>
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.
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.
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.
24 #include "gdk-pixbuf.h"
29 * gdk_pixbuf_add_alpha:
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.
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.
44 * Return value: A newly-created pixbuf with a reference count of 1.
47 gdk_pixbuf_add_alpha (GdkPixbuf *pixbuf, gboolean substitute_color, guchar r, guchar g, guchar b)
51 GdkPixbuf *new_pixbuf;
54 g_return_val_if_fail (pixbuf != NULL, NULL);
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);
62 new_apb = art_pixbuf_duplicate (apb);
66 return gdk_pixbuf_new_from_art_pixbuf (new_apb);
69 new_pixbuf = gdk_pixbuf_new (ART_PIX_RGB, TRUE, 8, apb->width, apb->height);
73 new_apb = new_pixbuf->art_pixbuf;
75 for (y = 0; y < apb->height; y++) {
79 src = apb->pixels + y * apb->rowstride;
80 dest = new_apb->pixels + y * new_apb->rowstride;
82 for (x = 0; x < apb->width; x++) {
83 tr = *dest++ = *src++;
84 tg = *dest++ = *src++;
85 tb = *dest++ = *src++;
87 if (substitute_color && tr == r && tg == g && tb == b)