1 /* GdkPixbuf library - Utilities and miscellaneous convenience functions
3 * Copyright (C) 1999 The Free Software Foundation
5 * Authors: Federico Mena-Quintero <federico@gimp.org>
6 * Cody Russell <bratsche@gnome.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 #include "gdk-pixbuf-private.h"
31 * gdk_pixbuf_add_alpha:
33 * @substitute_color: Whether to substitute a color for zero opacity. If this
34 * is #FALSE, then the (@r, @g, @b) arguments will be ignored.
35 * @r: Red value to substitute.
36 * @g: Green value to substitute.
37 * @b: Blue value to substitute.
39 * Takes an existing pixbuf and adds an alpha channel to it. If the original
40 * pixbuf already had alpha information, then the contents of the new pixbuf are
41 * exactly the same as the original's. Otherwise, the new pixbuf will have all
42 * pixels with full opacity if @substitute_color is #FALSE. If
43 * @substitute_color is #TRUE, then the color specified by (@r, @g, @b) will be
44 * substituted for zero opacity.
46 * Return value: A newly-created pixbuf with a reference count of 1.
49 gdk_pixbuf_add_alpha (const GdkPixbuf *pixbuf,
50 gboolean substitute_color, guchar r, guchar g, guchar b)
52 GdkPixbuf *new_pixbuf;
55 g_return_val_if_fail (pixbuf != NULL, NULL);
56 g_return_val_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB, NULL);
57 g_return_val_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4, NULL);
58 g_return_val_if_fail (pixbuf->bits_per_sample == 8, NULL);
60 if (pixbuf->has_alpha) {
61 new_pixbuf = gdk_pixbuf_copy (pixbuf);
68 new_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, pixbuf->width, pixbuf->height);
72 for (y = 0; y < pixbuf->height; y++) {
76 src = pixbuf->pixels + y * pixbuf->rowstride;
77 dest = new_pixbuf->pixels + y * new_pixbuf->rowstride;
79 for (x = 0; x < pixbuf->width; x++) {
80 tr = *dest++ = *src++;
81 tg = *dest++ = *src++;
82 tb = *dest++ = *src++;
84 if (substitute_color && tr == r && tg == g && tb == b)
95 * gdk_pixbuf_copy_area:
96 * @src_pixbuf: Source pixbuf.
97 * @src_x: Source X coordinate within @src_pixbuf.
98 * @src_y: Source Y coordinate within @src_pixbuf.
99 * @width: Width of the area to copy.
100 * @height: Height of the area to copy.
101 * @dest_pixbuf: Destination pixbuf.
102 * @dest_x: X coordinate within @dest_pixbuf.
103 * @dest_y: Y coordinate within @dest_pixbuf.
105 * Copies a rectangular area from @src_pixbuf to @dest_pixbuf. Conversion of
106 * pixbuf formats is done automatically.
109 gdk_pixbuf_copy_area (const GdkPixbuf *src_pixbuf,
110 int src_x, int src_y,
111 int width, int height,
112 GdkPixbuf *dest_pixbuf,
113 int dest_x, int dest_y)
115 g_return_if_fail (src_pixbuf != NULL);
116 g_return_if_fail (dest_pixbuf != NULL);
118 g_return_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width);
119 g_return_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height);
121 g_return_if_fail (dest_x >= 0 && dest_x + width <= dest_pixbuf->width);
122 g_return_if_fail (dest_y >= 0 && dest_y + height <= dest_pixbuf->height);
124 g_return_if_fail (!(gdk_pixbuf_get_has_alpha (src_pixbuf) && !gdk_pixbuf_get_has_alpha (dest_pixbuf)));
126 /* This will perform format conversions automatically */
128 gdk_pixbuf_scale (src_pixbuf,
132 (double) (dest_x - src_x),
133 (double) (dest_y - src_y),
140 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
143 * gdk_pixbuf_saturate_and_pixelate:
145 * @dest: place to write modified version of @src
146 * @saturation: saturation factor
147 * @pixelate: whether to pixelate
149 * Modifies saturation and optionally pixelates @src, placing the
150 * result in @dest. @src and @dest may be the same pixbuf with no ill
151 * effects. If @saturation is 1.0 then saturation is not changed. If
152 * it's less than 1.0, saturation is reduced (the image is darkened);
153 * if greater than 1.0, saturation is increased (the image is
154 * brightened). If @pixelate is TRUE, then pixels are faded in a
155 * checkerboard pattern to create a pixelated image. @src and @dest
156 * must have the same image format, size, and rowstride.
160 gdk_pixbuf_saturate_and_pixelate(const GdkPixbuf *src,
165 /* NOTE that src and dest MAY be the same pixbuf! */
167 g_return_if_fail (GDK_IS_PIXBUF (src));
168 g_return_if_fail (GDK_IS_PIXBUF (dest));
169 g_return_if_fail (gdk_pixbuf_get_height (src) == gdk_pixbuf_get_height (dest));
170 g_return_if_fail (gdk_pixbuf_get_width (src) == gdk_pixbuf_get_width (dest));
171 g_return_if_fail (gdk_pixbuf_get_rowstride (src) == gdk_pixbuf_get_rowstride (dest));
172 g_return_if_fail (gdk_pixbuf_get_colorspace (src) == gdk_pixbuf_get_colorspace (dest));
174 if (saturation == 1.0 && !pixelate) {
176 memcpy (gdk_pixbuf_get_pixels (dest),
177 gdk_pixbuf_get_pixels (src),
178 gdk_pixbuf_get_height (src) * gdk_pixbuf_get_rowstride (src));
183 gint width, height, has_alpha, rowstride;
184 guchar *target_pixels;
185 guchar *original_pixels;
186 guchar *current_pixel;
189 has_alpha = gdk_pixbuf_get_has_alpha (src);
190 width = gdk_pixbuf_get_width (src);
191 height = gdk_pixbuf_get_height (src);
192 rowstride = gdk_pixbuf_get_rowstride (src);
194 target_pixels = gdk_pixbuf_get_pixels (dest);
195 original_pixels = gdk_pixbuf_get_pixels (src);
197 for (i = 0; i < height; i++) {
198 for (j = 0; j < width; j++) {
199 current_pixel = original_pixels + i*rowstride + j*(has_alpha?4:3);
200 intensity = INTENSITY (*(current_pixel), *(current_pixel + 1), *(current_pixel + 2));
201 if (pixelate && (i+j)%2 == 0) {
202 *(target_pixels + i*rowstride + j*(has_alpha?4:3)) = intensity/2 + 127;
203 *(target_pixels + i*rowstride + j*(has_alpha?4:3) + 1) = intensity/2 + 127;
204 *(target_pixels + i*rowstride + j*(has_alpha?4:3) + 2) = intensity/2 + 127;
205 } else if (pixelate) {
206 #define DARK_FACTOR 0.7
207 *(target_pixels + i*rowstride + j*(has_alpha?4:3)) =
208 (guchar) (((1.0 - saturation) * intensity
209 + saturation * (*(current_pixel)))) * DARK_FACTOR;
210 *(target_pixels + i*rowstride + j*(has_alpha?4:3) + 1) =
211 (guchar) (((1.0 - saturation) * intensity
212 + saturation * (*(current_pixel + 1)))) * DARK_FACTOR;
213 *(target_pixels + i*rowstride + j*(has_alpha?4:3) + 2) =
214 (guchar) (((1.0 - saturation) * intensity
215 + saturation * (*(current_pixel + 2)))) * DARK_FACTOR;
217 *(target_pixels + i*rowstride + j*(has_alpha?4:3)) =
218 (guchar) ((1.0 - saturation) * intensity
219 + saturation * (*(current_pixel)));
220 *(target_pixels + i*rowstride + j*(has_alpha?4:3) + 1) =
221 (guchar) ((1.0 - saturation) * intensity
222 + saturation * (*(current_pixel + 1)));
223 *(target_pixels + i*rowstride + j*(has_alpha?4:3) + 2) =
224 (guchar) ((1.0 - saturation) * intensity
225 + saturation * (*(current_pixel + 2)));
229 *(target_pixels + i*rowstride + j*(has_alpha?4:3) + 3) = *(current_pixel + 3);