]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-util.c
Include "config.h" instead of <config.h> Command used: find -name
[~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 "gdk-pixbuf-alias.h"
27 #include <string.h>
28
29 \f
30
31 /**
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.
39  *
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).
44  * 
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.
48  *
49  * Return value: A newly-created pixbuf with a reference count of 1.
50  **/
51 GdkPixbuf *
52 gdk_pixbuf_add_alpha (const GdkPixbuf *pixbuf,
53                       gboolean substitute_color, guchar r, guchar g, guchar b)
54 {
55         GdkPixbuf *new_pixbuf;
56         int x, y;
57
58         g_return_val_if_fail (pixbuf != NULL, 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);
62
63         if (pixbuf->has_alpha) {
64                 new_pixbuf = gdk_pixbuf_copy (pixbuf);
65                 if (!new_pixbuf)
66                         return NULL;
67
68                 if (!substitute_color)
69                         return new_pixbuf;
70         } else {
71                 new_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, pixbuf->width, pixbuf->height);
72         }
73         
74         if (!new_pixbuf)
75                 return NULL;
76
77         for (y = 0; y < pixbuf->height; y++) {
78                 guchar *src, *dest;
79                 guchar tr, tg, tb;
80
81                 src = pixbuf->pixels + y * pixbuf->rowstride;
82                 dest = new_pixbuf->pixels + y * new_pixbuf->rowstride;
83                 
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)
88                                         dest[3] = 0;
89                                 src += 4;
90                                 dest += 4;
91                         }
92                 } else {                        
93                         for (x = 0; x < pixbuf->width; x++) {
94                                 tr = *dest++ = *src++;
95                                 tg = *dest++ = *src++;
96                                 tb = *dest++ = *src++;
97                                 
98                                 if (substitute_color && tr == r && tg == g && tb == b)
99                                         *dest++ = 0;
100                                 else
101                                         *dest++ = 255;
102                         }
103                 }
104         }
105
106         return new_pixbuf;
107 }
108
109 /**
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.
119  *
120  * Copies a rectangular area from @src_pixbuf to @dest_pixbuf.  Conversion of
121  * pixbuf formats is done automatically.
122  *
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.
126  **/
127 void
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)
133 {
134         g_return_if_fail (src_pixbuf != NULL);
135         g_return_if_fail (dest_pixbuf != NULL);
136
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);
139
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);
142
143         g_return_if_fail (!(gdk_pixbuf_get_has_alpha (src_pixbuf) && !gdk_pixbuf_get_has_alpha (dest_pixbuf)));
144         
145         /* This will perform format conversions automatically */
146
147         gdk_pixbuf_scale (src_pixbuf,
148                           dest_pixbuf,
149                           dest_x, dest_y,
150                           width, height,
151                           (double) (dest_x - src_x),
152                           (double) (dest_y - src_y),
153                           1.0, 1.0,
154                           GDK_INTERP_NEAREST);
155 }
156
157
158
159 /**
160  * gdk_pixbuf_saturate_and_pixelate:
161  * @src: source image
162  * @dest: place to write modified version of @src
163  * @saturation: saturation factor
164  * @pixelate: whether to pixelate
165  *
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
173  * rowstride.
174  * 
175  **/
176 void
177 gdk_pixbuf_saturate_and_pixelate(const GdkPixbuf *src,
178                                  GdkPixbuf *dest,
179                                  gfloat saturation,
180                                  gboolean pixelate)
181 {
182         /* NOTE that src and dest MAY be the same pixbuf! */
183   
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));
190   
191         if (saturation == 1.0 && !pixelate) {
192                 if (dest != src)
193                         memcpy (gdk_pixbuf_get_pixels (dest),
194                                 gdk_pixbuf_get_pixels (src),
195                                 gdk_pixbuf_get_height (src) * gdk_pixbuf_get_rowstride (src));
196         } else {
197                 int i, j, t;
198                 int width, height, has_alpha, src_rowstride, dest_rowstride, bytes_per_pixel;
199                 guchar *src_line;
200                 guchar *dest_line;
201                 guchar *src_pixel;
202                 guchar *dest_pixel;
203                 guchar intensity;
204
205                 has_alpha = gdk_pixbuf_get_has_alpha (src);
206                 bytes_per_pixel = has_alpha ? 4 : 3;
207                 width = gdk_pixbuf_get_width (src);
208                 height = gdk_pixbuf_get_height (src);
209                 src_rowstride = gdk_pixbuf_get_rowstride (src);
210                 dest_rowstride = gdk_pixbuf_get_rowstride (dest);
211                 
212                 src_line = gdk_pixbuf_get_pixels (src);
213                 dest_line = gdk_pixbuf_get_pixels (dest);
214                 
215 #define DARK_FACTOR 0.7
216 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
217 #define CLAMP_UCHAR(v) (t = (v), CLAMP (t, 0, 255))
218 #define SATURATE(v) ((1.0 - saturation) * intensity + saturation * (v))
219
220                 for (i = 0 ; i < height ; i++) {
221                         src_pixel = src_line;
222                         src_line += src_rowstride;
223                         dest_pixel = dest_line;
224                         dest_line += dest_rowstride;
225
226                         for (j = 0 ; j < width ; j++) {
227                                 intensity = INTENSITY (src_pixel[0], src_pixel[1], src_pixel[2]);
228                                 if (pixelate && (i + j) % 2 == 0) {
229                                         dest_pixel[0] = intensity / 2 + 127;
230                                         dest_pixel[1] = intensity / 2 + 127;
231                                         dest_pixel[2] = intensity / 2 + 127;
232                                 } else if (pixelate) {
233                                         dest_pixel[0] = CLAMP_UCHAR ((SATURATE (src_pixel[0])) * DARK_FACTOR);
234                                         dest_pixel[1] = CLAMP_UCHAR ((SATURATE (src_pixel[1])) * DARK_FACTOR);
235                                         dest_pixel[2] = CLAMP_UCHAR ((SATURATE (src_pixel[2])) * DARK_FACTOR);
236                                 } else {
237                                         dest_pixel[0] = CLAMP_UCHAR (SATURATE (src_pixel[0]));
238                                         dest_pixel[1] = CLAMP_UCHAR (SATURATE (src_pixel[1]));
239                                         dest_pixel[2] = CLAMP_UCHAR (SATURATE (src_pixel[2]));
240                                 }
241                                 
242                                 if (has_alpha)
243                                         dest_pixel[3] = src_pixel[3];
244
245                                 src_pixel += bytes_per_pixel;
246                                 dest_pixel += bytes_per_pixel;
247                         }
248                 }
249         }
250 }
251
252
253 /**
254  * gdk_pixbuf_apply_embedded_orientation:
255  * @src: A #GdkPixbuf.
256  *
257  * Takes an existing pixbuf and checks for the presence of an
258  * associated "orientation" option, which may be provided by the 
259  * jpeg loader (which reads the exif orientation tag) or the 
260  * tiff loader (which reads the tiff orientation tag, and
261  * compensates it for the partial transforms performed by 
262  * libtiff). If an orientation option/tag is present, the
263  * appropriate transform will be performed so that the pixbuf
264  * is oriented correctly.
265  *
266  * Return value: A newly-created pixbuf, or a reference to the
267  * input pixbuf (with an increased reference count).
268  *
269  * Since 2.12
270  **/
271 GdkPixbuf *
272 gdk_pixbuf_apply_embedded_orientation (GdkPixbuf *src)
273 {
274         const gchar *orientation_string;
275         int          transform = 0;
276         GdkPixbuf   *temp;
277         GdkPixbuf   *dest;
278
279         g_return_val_if_fail (src != NULL, NULL);
280
281         /* Read the orientation option associated with the pixbuf */
282         orientation_string = gdk_pixbuf_get_option (src, "orientation");        
283
284         if (orientation_string) {
285                 /* If an orientation option was found, convert the 
286                    orientation string into an integer. */
287                 transform = (int) g_ascii_strtoll (orientation_string, NULL, 10);
288         }
289
290         /* Apply the actual transforms, which involve rotations and flips. 
291            The meaning of orientation values 1-8 and the required transforms
292            are defined by the TIFF and EXIF (for JPEGs) standards. */
293         switch (transform) {
294         case 1:
295                 dest = src;
296                 g_object_ref (dest);
297                 break;
298         case 2:
299                 dest = gdk_pixbuf_flip (src, TRUE);
300                 break;
301         case 3:
302                 dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
303                 break;
304         case 4:
305                 dest = gdk_pixbuf_flip (src, FALSE);
306                 break;
307         case 5:
308                 temp = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
309                 dest = gdk_pixbuf_flip (temp, TRUE);
310                 g_object_unref (temp);
311                 break;
312         case 6:
313                 dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
314                 break;
315         case 7:
316                 temp = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
317                 dest = gdk_pixbuf_flip (temp, FALSE);
318                 g_object_unref (temp);
319                 break;
320         case 8:
321                 dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
322                 break;
323         default:
324                 /* if no orientation tag was present */
325                 dest = src;
326                 g_object_ref (dest);
327                 break;
328         }
329
330         return dest;
331 }
332
333
334 #define __GDK_PIXBUF_UTIL_C__
335 #include "gdk-pixbuf-aliasdef.c"
336