1 | /* GdkPixbuf library - Image creation from in-memory buffers |
2 | * |
3 | * Copyright (C) 1999 The Free Software Foundation |
4 | * |
5 | * Author: Federico Mena-Quintero <federico@gimp.org> |
6 | * |
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 2 of the License, or (at your option) any later version. |
11 | * |
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 | * Lesser General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Lesser General Public |
18 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | */ |
20 | |
21 | #include "config.h" |
22 | #include "gdk-pixbuf.h" |
23 | #include "gdk-pixbuf-private.h" |
24 | #include <stdlib.h> |
25 | #include <string.h> |
26 | |
27 | |
28 | |
29 | /** |
30 | * gdk_pixbuf_new_from_data: |
31 | * @data: (array): Image data in 8-bit/sample packed format |
32 | * @colorspace: Colorspace for the image data |
33 | * @has_alpha: Whether the data has an opacity channel |
34 | * @bits_per_sample: Number of bits per sample |
35 | * @width: Width of the image in pixels, must be > 0 |
36 | * @height: Height of the image in pixels, must be > 0 |
37 | * @rowstride: Distance in bytes between row starts |
38 | * @destroy_fn: (scope async) (allow-none): Function used to free the data when the pixbuf's reference count |
39 | * drops to zero, or %NULL if the data should not be freed |
40 | * @destroy_fn_data: (closure): Closure data to pass to the destroy notification function |
41 | * |
42 | * Creates a new #GdkPixbuf out of in-memory image data. |
43 | * |
44 | * Currently only RGB images with 8 bits per sample are supported. |
45 | * |
46 | * Since you are providing a pre-allocated pixel buffer, you must also |
47 | * specify a way to free that data. This is done with a function of |
48 | * type `GdkPixbufDestroyNotify`. When a pixbuf created with is |
49 | * finalized, your destroy notification function will be called, and |
50 | * it is its responsibility to free the pixel array. |
51 | * |
52 | * See also: [ctor@GdkPixbuf.Pixbuf.new_from_bytes] |
53 | * |
54 | * Return value: (transfer full): A newly-created pixbuf |
55 | **/ |
56 | GdkPixbuf * |
57 | gdk_pixbuf_new_from_data (const guchar *data, |
58 | GdkColorspace colorspace, |
59 | gboolean has_alpha, |
60 | int bits_per_sample, |
61 | int width, |
62 | int height, |
63 | int rowstride, |
64 | GdkPixbufDestroyNotify destroy_fn, |
65 | gpointer destroy_fn_data) |
66 | { |
67 | GdkPixbuf *pixbuf; |
68 | |
69 | /* Only 8-bit/sample RGB buffers are supported for now */ |
70 | |
71 | g_return_val_if_fail (data != NULL, NULL); |
72 | g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL); |
73 | g_return_val_if_fail (bits_per_sample == 8, NULL); |
74 | g_return_val_if_fail (width > 0, NULL); |
75 | g_return_val_if_fail (height > 0, NULL); |
76 | |
77 | pixbuf = g_object_new (GDK_TYPE_PIXBUF, |
78 | first_property_name: "colorspace" , colorspace, |
79 | "n-channels" , has_alpha ? 4 : 3, |
80 | "bits-per-sample" , bits_per_sample, |
81 | "has-alpha" , has_alpha ? TRUE : FALSE, |
82 | "width" , width, |
83 | "height" , height, |
84 | "rowstride" , rowstride, |
85 | "pixels" , data, |
86 | NULL); |
87 | g_assert (pixbuf->storage == STORAGE_PIXELS); |
88 | pixbuf->s.pixels.destroy_fn = destroy_fn; |
89 | pixbuf->s.pixels.destroy_fn_data = destroy_fn_data; |
90 | |
91 | return pixbuf; |
92 | } |
93 | |
94 | /** |
95 | * gdk_pixbuf_new_from_bytes: |
96 | * @data: Image data in 8-bit/sample packed format inside a #GBytes |
97 | * @colorspace: Colorspace for the image data |
98 | * @has_alpha: Whether the data has an opacity channel |
99 | * @bits_per_sample: Number of bits per sample |
100 | * @width: Width of the image in pixels, must be > 0 |
101 | * @height: Height of the image in pixels, must be > 0 |
102 | * @rowstride: Distance in bytes between row starts |
103 | * |
104 | * Creates a new #GdkPixbuf out of in-memory readonly image data. |
105 | * |
106 | * Currently only RGB images with 8 bits per sample are supported. |
107 | * |
108 | * This is the `GBytes` variant of gdk_pixbuf_new_from_data(), useful |
109 | * for language bindings. |
110 | * |
111 | * Return value: (transfer full): A newly-created pixbuf |
112 | * |
113 | * Since: 2.32 |
114 | **/ |
115 | GdkPixbuf * |
116 | gdk_pixbuf_new_from_bytes (GBytes *data, |
117 | GdkColorspace colorspace, |
118 | gboolean has_alpha, |
119 | int bits_per_sample, |
120 | int width, |
121 | int height, |
122 | int rowstride) |
123 | { |
124 | g_return_val_if_fail (data != NULL, NULL); |
125 | g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL); |
126 | g_return_val_if_fail (bits_per_sample == 8, NULL); |
127 | g_return_val_if_fail (width > 0, NULL); |
128 | g_return_val_if_fail (height > 0, NULL); |
129 | g_return_val_if_fail (g_bytes_get_size (data) >= width * height * (has_alpha ? 4 : 3), NULL); |
130 | |
131 | return g_object_new (GDK_TYPE_PIXBUF, |
132 | first_property_name: "pixel-bytes" , data, |
133 | "colorspace" , colorspace, |
134 | "n-channels" , has_alpha ? 4 : 3, |
135 | "bits-per-sample" , bits_per_sample, |
136 | "has-alpha" , has_alpha ? TRUE : FALSE, |
137 | "width" , width, |
138 | "height" , height, |
139 | "rowstride" , rowstride, |
140 | NULL); |
141 | } |
142 | |