]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-util.c
fix some shell typos
[~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  * Authors: Federico Mena-Quintero <federico@gimp.org>
6  *          Cody Russell  <bratsche@gnome.org>
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include <config.h>
25 #include "gdk-pixbuf-private.h"
26 #include <string.h>
27
28 \f
29
30 /**
31  * gdk_pixbuf_add_alpha:
32  * @pixbuf: A pixbuf.
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.
38  *
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.
45  *
46  * Return value: A newly-created pixbuf with a reference count of 1.
47  **/
48 GdkPixbuf *
49 gdk_pixbuf_add_alpha (const GdkPixbuf *pixbuf,
50                       gboolean substitute_color, guchar r, guchar g, guchar b)
51 {
52         GdkPixbuf *new_pixbuf;
53         int x, y;
54
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);
59
60         if (pixbuf->has_alpha) {
61                 new_pixbuf = gdk_pixbuf_copy (pixbuf);
62                 if (!new_pixbuf)
63                         return NULL;
64
65                 return new_pixbuf;
66         }
67
68         new_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, pixbuf->width, pixbuf->height);
69         if (!new_pixbuf)
70                 return NULL;
71
72         for (y = 0; y < pixbuf->height; y++) {
73                 guchar *src, *dest;
74                 guchar tr, tg, tb;
75
76                 src = pixbuf->pixels + y * pixbuf->rowstride;
77                 dest = new_pixbuf->pixels + y * new_pixbuf->rowstride;
78
79                 for (x = 0; x < pixbuf->width; x++) {
80                         tr = *dest++ = *src++;
81                         tg = *dest++ = *src++;
82                         tb = *dest++ = *src++;
83
84                         if (substitute_color && tr == r && tg == g && tb == b)
85                                 *dest++ = 0;
86                         else
87                                 *dest++ = 255;
88                 }
89         }
90
91         return new_pixbuf;
92 }
93
94 /**
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.
104  *
105  * Copies a rectangular area from @src_pixbuf to @dest_pixbuf.  Conversion of
106  * pixbuf formats is done automatically.
107  **/
108 void
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)
114 {
115         g_return_if_fail (src_pixbuf != NULL);
116         g_return_if_fail (dest_pixbuf != NULL);
117
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);
120
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);
123
124         g_return_if_fail (!(gdk_pixbuf_get_has_alpha (src_pixbuf) && !gdk_pixbuf_get_has_alpha (dest_pixbuf)));
125         
126         /* This will perform format conversions automatically */
127
128         gdk_pixbuf_scale (src_pixbuf,
129                           dest_pixbuf,
130                           dest_x, dest_y,
131                           width, height,
132                           (double) (dest_x - src_x),
133                           (double) (dest_y - src_y),
134                           1.0, 1.0,
135                           GDK_INTERP_NEAREST);
136 }
137
138
139
140 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
141
142 /**
143  * gdk_pixbuf_saturate_and_pixelate:
144  * @src: source image
145  * @dest: place to write modified version of @src
146  * @saturation: saturation factor
147  * @pixelate: whether to pixelate
148  *
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.
157  * 
158  **/
159 void
160 gdk_pixbuf_saturate_and_pixelate(const GdkPixbuf *src,
161                                  GdkPixbuf *dest,
162                                  gfloat saturation,
163                                  gboolean pixelate)
164 {
165         /* NOTE that src and dest MAY be the same pixbuf! */
166   
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));
173   
174         if (saturation == 1.0 && !pixelate) {
175                 if (dest != src)
176                         memcpy (gdk_pixbuf_get_pixels (dest),
177                                 gdk_pixbuf_get_pixels (src),
178                                 gdk_pixbuf_get_height (src) * gdk_pixbuf_get_rowstride (src));
179
180                 return;
181         } else {
182                 gint i, j;
183                 gint width, height, has_alpha, rowstride;
184                 guchar *target_pixels;
185                 guchar *original_pixels;
186                 guchar *current_pixel;
187                 guchar intensity;
188
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);
193                 
194                 target_pixels = gdk_pixbuf_get_pixels (dest);
195                 original_pixels = gdk_pixbuf_get_pixels (src);
196
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;
216                                 } else {
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)));
226                                 }
227               
228                                 if (has_alpha)
229                                         *(target_pixels + i*rowstride + j*(has_alpha?4:3) + 3) = *(current_pixel + 3);
230                         }
231                 }
232
233                 return;
234         }
235 }