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"
26 #include "gdk-pixbuf-alias.h"
32 * gdk_pixbuf_add_alpha:
33 * @pixbuf: A #GdkPixbuf.
34 * @substitute_color: Whether to set a color to zero opacity. If this
35 * is %FALSE, then the (@r, @g, @b) arguments will be ignored.
36 * @r: Red value to substitute.
37 * @g: Green value to substitute.
38 * @b: Blue value to substitute.
40 * Takes an existing pixbuf and adds an alpha channel to it.
41 * If the existing pixbuf already had an alpha channel, the channel
42 * values are copied from the original; otherwise, the alpha channel
43 * is initialized to 255 (full opacity).
45 * If @substitute_color is %TRUE, then the color specified by (@r, @g, @b) will be
46 * assigned zero opacity. That is, if you pass (255, 255, 255) for the
47 * substitute color, all white pixels will become fully transparent.
49 * Return value: A newly-created pixbuf with a reference count of 1.
52 gdk_pixbuf_add_alpha (const GdkPixbuf *pixbuf,
53 gboolean substitute_color, guchar r, guchar g, guchar b)
55 GdkPixbuf *new_pixbuf;
58 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
59 g_return_val_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB, NULL);
60 g_return_val_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4, NULL);
61 g_return_val_if_fail (pixbuf->bits_per_sample == 8, NULL);
63 if (pixbuf->has_alpha) {
64 new_pixbuf = gdk_pixbuf_copy (pixbuf);
68 if (!substitute_color)
71 new_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, pixbuf->width, pixbuf->height);
77 for (y = 0; y < pixbuf->height; y++) {
81 src = pixbuf->pixels + y * pixbuf->rowstride;
82 dest = new_pixbuf->pixels + y * new_pixbuf->rowstride;
84 if (pixbuf->has_alpha) {
85 /* Just subst color, we already copied everything else */
86 for (x = 0; x < pixbuf->width; x++) {
87 if (src[0] == r && src[1] == g && src[2] == b)
93 for (x = 0; x < pixbuf->width; x++) {
94 tr = *dest++ = *src++;
95 tg = *dest++ = *src++;
96 tb = *dest++ = *src++;
98 if (substitute_color && tr == r && tg == g && tb == b)
110 * gdk_pixbuf_copy_area:
111 * @src_pixbuf: Source pixbuf.
112 * @src_x: Source X coordinate within @src_pixbuf.
113 * @src_y: Source Y coordinate within @src_pixbuf.
114 * @width: Width of the area to copy.
115 * @height: Height of the area to copy.
116 * @dest_pixbuf: Destination pixbuf.
117 * @dest_x: X coordinate within @dest_pixbuf.
118 * @dest_y: Y coordinate within @dest_pixbuf.
120 * Copies a rectangular area from @src_pixbuf to @dest_pixbuf. Conversion of
121 * pixbuf formats is done automatically.
123 * If the source rectangle overlaps the destination rectangle on the
124 * same pixbuf, it will be overwritten during the copy operation.
125 * Therefore, you can not use this function to scroll a pixbuf.
128 gdk_pixbuf_copy_area (const GdkPixbuf *src_pixbuf,
129 int src_x, int src_y,
130 int width, int height,
131 GdkPixbuf *dest_pixbuf,
132 int dest_x, int dest_y)
134 g_return_if_fail (src_pixbuf != NULL);
135 g_return_if_fail (dest_pixbuf != NULL);
137 g_return_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width);
138 g_return_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height);
140 g_return_if_fail (dest_x >= 0 && dest_x + width <= dest_pixbuf->width);
141 g_return_if_fail (dest_y >= 0 && dest_y + height <= dest_pixbuf->height);
143 g_return_if_fail (!(gdk_pixbuf_get_has_alpha (src_pixbuf) && !gdk_pixbuf_get_has_alpha (dest_pixbuf)));
145 /* This will perform format conversions automatically */
147 gdk_pixbuf_scale (src_pixbuf,
151 (double) (dest_x - src_x),
152 (double) (dest_y - src_y),
160 * gdk_pixbuf_saturate_and_pixelate:
162 * @dest: place to write modified version of @src
163 * @saturation: saturation factor
164 * @pixelate: whether to pixelate
166 * Modifies saturation and optionally pixelates @src, placing the result in
167 * @dest. @src and @dest may be the same pixbuf with no ill effects. If
168 * @saturation is 1.0 then saturation is not changed. If it's less than 1.0,
169 * saturation is reduced (the image turns toward grayscale); if greater than
170 * 1.0, saturation is increased (the image gets more vivid colors). If @pixelate
171 * is %TRUE, then pixels are faded in a checkerboard pattern to create a
172 * pixelated image. @src and @dest must have the same image format, size, and
177 gdk_pixbuf_saturate_and_pixelate(const GdkPixbuf *src,
182 /* NOTE that src and dest MAY be the same pixbuf! */
184 g_return_if_fail (GDK_IS_PIXBUF (src));
185 g_return_if_fail (GDK_IS_PIXBUF (dest));
186 g_return_if_fail (gdk_pixbuf_get_height (src) == gdk_pixbuf_get_height (dest));
187 g_return_if_fail (gdk_pixbuf_get_width (src) == gdk_pixbuf_get_width (dest));
188 g_return_if_fail (gdk_pixbuf_get_has_alpha (src) == gdk_pixbuf_get_has_alpha (dest));
189 g_return_if_fail (gdk_pixbuf_get_colorspace (src) == gdk_pixbuf_get_colorspace (dest));
191 if (saturation == 1.0 && !pixelate) {
193 gdk_pixbuf_copy_area (src, 0, 0,
194 gdk_pixbuf_get_width (src),
195 gdk_pixbuf_get_height (src),
199 int width, height, has_alpha, src_rowstride, dest_rowstride, bytes_per_pixel;
206 has_alpha = gdk_pixbuf_get_has_alpha (src);
207 bytes_per_pixel = has_alpha ? 4 : 3;
208 width = gdk_pixbuf_get_width (src);
209 height = gdk_pixbuf_get_height (src);
210 src_rowstride = gdk_pixbuf_get_rowstride (src);
211 dest_rowstride = gdk_pixbuf_get_rowstride (dest);
213 src_line = gdk_pixbuf_get_pixels (src);
214 dest_line = gdk_pixbuf_get_pixels (dest);
216 #define DARK_FACTOR 0.7
217 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
218 #define CLAMP_UCHAR(v) (t = (v), CLAMP (t, 0, 255))
219 #define SATURATE(v) ((1.0 - saturation) * intensity + saturation * (v))
221 for (i = 0 ; i < height ; i++) {
222 src_pixel = src_line;
223 src_line += src_rowstride;
224 dest_pixel = dest_line;
225 dest_line += dest_rowstride;
227 for (j = 0 ; j < width ; j++) {
228 intensity = INTENSITY (src_pixel[0], src_pixel[1], src_pixel[2]);
229 if (pixelate && (i + j) % 2 == 0) {
230 dest_pixel[0] = intensity / 2 + 127;
231 dest_pixel[1] = intensity / 2 + 127;
232 dest_pixel[2] = intensity / 2 + 127;
233 } else if (pixelate) {
234 dest_pixel[0] = CLAMP_UCHAR ((SATURATE (src_pixel[0])) * DARK_FACTOR);
235 dest_pixel[1] = CLAMP_UCHAR ((SATURATE (src_pixel[1])) * DARK_FACTOR);
236 dest_pixel[2] = CLAMP_UCHAR ((SATURATE (src_pixel[2])) * DARK_FACTOR);
238 dest_pixel[0] = CLAMP_UCHAR (SATURATE (src_pixel[0]));
239 dest_pixel[1] = CLAMP_UCHAR (SATURATE (src_pixel[1]));
240 dest_pixel[2] = CLAMP_UCHAR (SATURATE (src_pixel[2]));
244 dest_pixel[3] = src_pixel[3];
246 src_pixel += bytes_per_pixel;
247 dest_pixel += bytes_per_pixel;
255 * gdk_pixbuf_apply_embedded_orientation:
256 * @src: A #GdkPixbuf.
258 * Takes an existing pixbuf and checks for the presence of an
259 * associated "orientation" option, which may be provided by the
260 * jpeg loader (which reads the exif orientation tag) or the
261 * tiff loader (which reads the tiff orientation tag, and
262 * compensates it for the partial transforms performed by
263 * libtiff). If an orientation option/tag is present, the
264 * appropriate transform will be performed so that the pixbuf
265 * is oriented correctly.
267 * Return value: A newly-created pixbuf, or a reference to the
268 * input pixbuf (with an increased reference count).
273 gdk_pixbuf_apply_embedded_orientation (GdkPixbuf *src)
275 const gchar *orientation_string;
280 g_return_val_if_fail (GDK_IS_PIXBUF (src), NULL);
282 /* Read the orientation option associated with the pixbuf */
283 orientation_string = gdk_pixbuf_get_option (src, "orientation");
285 if (orientation_string) {
286 /* If an orientation option was found, convert the
287 orientation string into an integer. */
288 transform = (int) g_ascii_strtoll (orientation_string, NULL, 10);
291 /* Apply the actual transforms, which involve rotations and flips.
292 The meaning of orientation values 1-8 and the required transforms
293 are defined by the TIFF and EXIF (for JPEGs) standards. */
300 dest = gdk_pixbuf_flip (src, TRUE);
303 dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
306 dest = gdk_pixbuf_flip (src, FALSE);
309 temp = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
310 dest = gdk_pixbuf_flip (temp, TRUE);
311 g_object_unref (temp);
314 dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
317 temp = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
318 dest = gdk_pixbuf_flip (temp, FALSE);
319 g_object_unref (temp);
322 dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
325 /* if no orientation tag was present */
335 #define __GDK_PIXBUF_UTIL_C__
336 #include "gdk-pixbuf-aliasdef.c"