Blame gdk-pixbuf/gdk-pixbuf-data.c

Packit a4058c
/* GdkPixbuf library - Image creation from in-memory buffers
Packit a4058c
 *
Packit a4058c
 * Copyright (C) 1999 The Free Software Foundation
Packit a4058c
 *
Packit a4058c
 * Author: Federico Mena-Quintero <federico@gimp.org>
Packit a4058c
 *
Packit a4058c
 * This library is free software; you can redistribute it and/or
Packit a4058c
 * modify it under the terms of the GNU Lesser General Public
Packit a4058c
 * License as published by the Free Software Foundation; either
Packit a4058c
 * version 2 of the License, or (at your option) any later version.
Packit a4058c
 *
Packit a4058c
 * This library is distributed in the hope that it will be useful,
Packit a4058c
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4058c
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4058c
 * Lesser General Public License for more details.
Packit a4058c
 *
Packit a4058c
 * You should have received a copy of the GNU Lesser General Public
Packit a4058c
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit a4058c
 */
Packit a4058c
Packit a4058c
#include "config.h"
Packit a4058c
#include "gdk-pixbuf.h"
Packit a4058c
#include "gdk-pixbuf-private.h"
Packit a4058c
#include <stdlib.h>
Packit a4058c
#include <string.h>
Packit a4058c
Packit a4058c

Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_new_from_data:
Packit a4058c
 * @data: (array): Image data in 8-bit/sample packed format
Packit a4058c
 * @colorspace: Colorspace for the image data
Packit a4058c
 * @has_alpha: Whether the data has an opacity channel
Packit a4058c
 * @bits_per_sample: Number of bits per sample
Packit a4058c
 * @width: Width of the image in pixels, must be > 0
Packit a4058c
 * @height: Height of the image in pixels, must be > 0
Packit a4058c
 * @rowstride: Distance in bytes between row starts
Packit a4058c
 * @destroy_fn: (scope async) (allow-none): Function used to free the data when the pixbuf's reference count
Packit a4058c
 * drops to zero, or %NULL if the data should not be freed
Packit a4058c
 * @destroy_fn_data: (closure): Closure data to pass to the destroy notification function
Packit a4058c
 * 
Packit a4058c
 * Creates a new #GdkPixbuf out of in-memory image data.  Currently only RGB
Packit a4058c
 * images with 8 bits per sample are supported.
Packit a4058c
 *
Packit a4058c
 * Since you are providing a pre-allocated pixel buffer, you must also
Packit a4058c
 * specify a way to free that data.  This is done with a function of
Packit a4058c
 * type #GdkPixbufDestroyNotify.  When a pixbuf created with is
Packit a4058c
 * finalized, your destroy notification function will be called, and
Packit a4058c
 * it is its responsibility to free the pixel array.
Packit a4058c
 *
Packit a4058c
 * See also gdk_pixbuf_new_from_bytes().
Packit a4058c
 *
Packit a4058c
 * Return value: (transfer full): A newly-created #GdkPixbuf structure with a reference count of 1.
Packit a4058c
 **/
Packit a4058c
GdkPixbuf *
Packit a4058c
gdk_pixbuf_new_from_data (const guchar *data, GdkColorspace colorspace, gboolean has_alpha,
Packit a4058c
			  int bits_per_sample, int width, int height, int rowstride,
Packit a4058c
	  GdkPixbufDestroyNotify destroy_fn, gpointer destroy_fn_data)
Packit a4058c
{
Packit a4058c
	GdkPixbuf *pixbuf;
Packit a4058c
Packit a4058c
	/* Only 8-bit/sample RGB buffers are supported for now */
Packit a4058c
Packit a4058c
	g_return_val_if_fail (data != NULL, NULL);
Packit a4058c
	g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
Packit a4058c
	g_return_val_if_fail (bits_per_sample == 8, NULL);
Packit a4058c
	g_return_val_if_fail (width > 0, NULL);
Packit a4058c
	g_return_val_if_fail (height > 0, NULL);
Packit a4058c
Packit a4058c
	pixbuf = g_object_new (GDK_TYPE_PIXBUF, 
Packit a4058c
			       "colorspace", colorspace,
Packit a4058c
			       "n-channels", has_alpha ? 4 : 3,
Packit a4058c
			       "bits-per-sample", bits_per_sample,
Packit a4058c
			       "has-alpha", has_alpha ? TRUE : FALSE,
Packit a4058c
			       "width", width,
Packit a4058c
			       "height", height,
Packit a4058c
			       "rowstride", rowstride,
Packit a4058c
			       "pixels", data,
Packit a4058c
			       NULL);
Packit a4058c
        
Packit a4058c
	pixbuf->destroy_fn = destroy_fn;
Packit a4058c
	pixbuf->destroy_fn_data = destroy_fn_data;
Packit a4058c
Packit a4058c
	return pixbuf;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_new_from_bytes:
Packit a4058c
 * @data: Image data in 8-bit/sample packed format inside a #GBytes
Packit a4058c
 * @colorspace: Colorspace for the image data
Packit a4058c
 * @has_alpha: Whether the data has an opacity channel
Packit a4058c
 * @bits_per_sample: Number of bits per sample
Packit a4058c
 * @width: Width of the image in pixels, must be > 0
Packit a4058c
 * @height: Height of the image in pixels, must be > 0
Packit a4058c
 * @rowstride: Distance in bytes between row starts
Packit a4058c
 * 
Packit a4058c
 * Creates a new #GdkPixbuf out of in-memory readonly image data.
Packit a4058c
 * Currently only RGB images with 8 bits per sample are supported.
Packit a4058c
 * This is the #GBytes variant of gdk_pixbuf_new_from_data().
Packit a4058c
 *
Packit a4058c
 * Return value: (transfer full): A newly-created #GdkPixbuf structure with a reference count of 1.
Packit a4058c
 * Since: 2.32
Packit a4058c
 **/
Packit a4058c
GdkPixbuf *
Packit a4058c
gdk_pixbuf_new_from_bytes (GBytes *data, GdkColorspace colorspace, gboolean has_alpha,
Packit a4058c
			   int bits_per_sample, int width, int height, int rowstride)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (data != NULL, NULL);
Packit a4058c
	g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
Packit a4058c
	g_return_val_if_fail (bits_per_sample == 8, NULL);
Packit a4058c
	g_return_val_if_fail (width > 0, NULL);
Packit a4058c
	g_return_val_if_fail (height > 0, NULL);
Packit a4058c
	g_return_val_if_fail (g_bytes_get_size (data) >= width * height * (has_alpha ? 4 : 3), NULL);
Packit a4058c
Packit a4058c
	return (GdkPixbuf*) g_object_new (GDK_TYPE_PIXBUF, 
Packit a4058c
					  "pixel-bytes", data,
Packit a4058c
					  "colorspace", colorspace,
Packit a4058c
					  "n-channels", has_alpha ? 4 : 3,
Packit a4058c
					  "bits-per-sample", bits_per_sample,
Packit a4058c
					  "has-alpha", has_alpha ? TRUE : FALSE,
Packit a4058c
					  "width", width,
Packit a4058c
					  "height", height,
Packit a4058c
					  "rowstride", rowstride,
Packit a4058c
					  NULL);
Packit a4058c
}