]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-util.c
[quartz] Delete the typedef of GdkDevicePrivate
[~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 (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);
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                         gdk_pixbuf_copy_area (src, 0, 0, 
194                                               gdk_pixbuf_get_width (src),
195                                               gdk_pixbuf_get_height (src),
196                                               dest, 0, 0);
197         } else {
198                 int i, j, t;
199                 int width, height, has_alpha, src_rowstride, dest_rowstride, bytes_per_pixel;
200                 guchar *src_line;
201                 guchar *dest_line;
202                 guchar *src_pixel;
203                 guchar *dest_pixel;
204                 guchar intensity;
205
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);
212                 
213                 src_line = gdk_pixbuf_get_pixels (src);
214                 dest_line = gdk_pixbuf_get_pixels (dest);
215                 
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))
220
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;
226
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);
237                                 } else {
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]));
241                                 }
242                                 
243                                 if (has_alpha)
244                                         dest_pixel[3] = src_pixel[3];
245
246                                 src_pixel += bytes_per_pixel;
247                                 dest_pixel += bytes_per_pixel;
248                         }
249                 }
250         }
251 }
252
253
254 /**
255  * gdk_pixbuf_apply_embedded_orientation:
256  * @src: A #GdkPixbuf.
257  *
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.
266  *
267  * Return value: A newly-created pixbuf, or a reference to the
268  * input pixbuf (with an increased reference count).
269  *
270  * Since: 2.12
271  **/
272 GdkPixbuf *
273 gdk_pixbuf_apply_embedded_orientation (GdkPixbuf *src)
274 {
275         const gchar *orientation_string;
276         int          transform = 0;
277         GdkPixbuf   *temp;
278         GdkPixbuf   *dest;
279
280         g_return_val_if_fail (GDK_IS_PIXBUF (src), NULL);
281
282         /* Read the orientation option associated with the pixbuf */
283         orientation_string = gdk_pixbuf_get_option (src, "orientation");        
284
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);
289         }
290
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. */
294         switch (transform) {
295         case 1:
296                 dest = src;
297                 g_object_ref (dest);
298                 break;
299         case 2:
300                 dest = gdk_pixbuf_flip (src, TRUE);
301                 break;
302         case 3:
303                 dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
304                 break;
305         case 4:
306                 dest = gdk_pixbuf_flip (src, FALSE);
307                 break;
308         case 5:
309                 temp = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
310                 dest = gdk_pixbuf_flip (temp, TRUE);
311                 g_object_unref (temp);
312                 break;
313         case 6:
314                 dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
315                 break;
316         case 7:
317                 temp = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_CLOCKWISE);
318                 dest = gdk_pixbuf_flip (temp, FALSE);
319                 g_object_unref (temp);
320                 break;
321         case 8:
322                 dest = gdk_pixbuf_rotate_simple (src, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
323                 break;
324         default:
325                 /* if no orientation tag was present */
326                 dest = src;
327                 g_object_ref (dest);
328                 break;
329         }
330
331         return dest;
332 }
333
334
335 #define __GDK_PIXBUF_UTIL_C__
336 #include "gdk-pixbuf-aliasdef.c"
337