app: s/sprintf/g_snprintf/ in xcf_save_image()
[gimp.git] / libgimpcolor / gimppixbuf.c
blob235b84d7f646ec1ec2f2d13d051b1abfa946a370
1 /* LIBGIMP - The GIMP Library
2 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
4 * gimppixbuf.c
5 * Copyright (C) 2012 Michael Natterer <mitch@gimp.org>
7 * This library is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see
19 * <https://www.gnu.org/licenses/>.
22 #include "config.h"
24 #include <gegl.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include "gimpcolortypes.h"
29 #include "gimppixbuf.h"
32 /**
33 * SECTION: gimppixbuf
34 * @title: GimpPixbuf
35 * @short_description: Definitions and Functions relating to GdkPixbuf.
37 * Definitions and Functions relating to GdkPixbuf.
38 **/
40 /**
41 * gimp_pixbuf_get_format:
42 * @pixbuf: a #GdkPixbuf
44 * Returns the Babl format that corresponds to the @pixbuf's pixel format.
46 * Return value: the @pixbuf's pixel format
48 * Since: 2.10
49 **/
50 const Babl *
51 gimp_pixbuf_get_format (GdkPixbuf *pixbuf)
53 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
55 switch (gdk_pixbuf_get_n_channels (pixbuf))
57 case 3: return babl_format ("R'G'B' u8");
58 case 4: return babl_format ("R'G'B'A u8");
61 g_return_val_if_reached (NULL);
64 /**
65 * gimp_pixbuf_create_buffer:
66 * @pixbuf: a #GdkPixbuf
68 * Returns a #GeglBuffer that's either backed by the @pixbuf's pixels,
69 * or a copy of them. This function tries to not copy the @pixbuf's
70 * pixels. If the pixbuf's rowstride is a multiple of its bpp, a
71 * simple reference to the @pixbuf's pixels is made and @pixbuf will
72 * be kept around for as long as the buffer exists; otherwise the
73 * pixels are copied.
75 * Return value: a new #GeglBuffer.
77 * Since: 2.10
78 **/
79 GeglBuffer *
80 gimp_pixbuf_create_buffer (GdkPixbuf *pixbuf)
82 gint width;
83 gint height;
84 gint rowstride;
85 gint bpp;
87 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
89 width = gdk_pixbuf_get_width (pixbuf);
90 height = gdk_pixbuf_get_height (pixbuf);
91 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
92 bpp = gdk_pixbuf_get_n_channels (pixbuf);
94 if ((rowstride % bpp) == 0)
96 return gegl_buffer_linear_new_from_data (gdk_pixbuf_get_pixels (pixbuf),
97 gimp_pixbuf_get_format (pixbuf),
98 GEGL_RECTANGLE (0, 0,
99 width, height),
100 rowstride,
101 (GDestroyNotify) g_object_unref,
102 g_object_ref (pixbuf));
104 else
106 GeglBuffer *buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
107 width, height),
108 gimp_pixbuf_get_format (pixbuf));
110 gegl_buffer_set (buffer, NULL, 0, NULL,
111 gdk_pixbuf_get_pixels (pixbuf),
112 gdk_pixbuf_get_rowstride (pixbuf));
114 return buffer;
119 * gimp_pixbuf_get_icc_profile:
120 * @pixbuf: a #GdkPixbuf
121 * @length: return location for the ICC profile's length
123 * Returns the ICC profile attached to the @pixbuf, or %NULL if there
124 * is none.
126 * Return value: The ICC profile data, or %NULL. The value should be freed
127 * with g_free().
129 * Since: 2.10
131 guint8 *
132 gimp_pixbuf_get_icc_profile (GdkPixbuf *pixbuf,
133 gsize *length)
135 const gchar *icc_base64;
137 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
138 g_return_val_if_fail (length != NULL, NULL);
140 icc_base64 = gdk_pixbuf_get_option (pixbuf, "icc-profile");
142 if (icc_base64)
144 guint8 *icc_data;
146 icc_data = g_base64_decode (icc_base64, length);
148 return icc_data;
151 return NULL;