mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2024-09-21 14:24:19 +00:00
0eb096334e
1999-10-27 Federico Mena Quintero <federico@redhat.com> * src/gdk-pixbuf-render.c (gdk_pixbuf_render_threshold_alpha): New function to threshold a pixbuf's alpha channel into a bitmap. * src/gdk-pixbuf.c (gdk_pixbuf_new): Fixed so that it will contemplate future extensions to ArtPixBuf's formats. * src/io-png.c (png_info_callback): Use the new API of gdk_pixbuf_new(). * src/gdk-pixbuf.h (gdk_pixbuf_get_height): Added some convenience macros to fetch the ArtPixBuf's fields.
150 lines
3.8 KiB
C
150 lines
3.8 KiB
C
/* GdkPixbuf library - Basic memory management
|
||
*
|
||
* Copyright (C) 1999 The Free Software Foundation
|
||
*
|
||
* Authors: Mark Crichton <crichton@gimp.org>
|
||
* Miguel de Icaza <miguel@gnu.org>
|
||
* Federico Mena-Quintero <federico@gimp.org>
|
||
*
|
||
* This library is free software; you can redistribute it and/or
|
||
* modify it under the terms of the GNU Library General Public
|
||
* License as published by the Free Software Foundation; either
|
||
* version 2 of the License, or (at your option) any later version.
|
||
*
|
||
* This library is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
* Library General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU Library General Public
|
||
* License along with this library; if not, write to the
|
||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||
* Boston, MA 02111-1307, USA.
|
||
*/
|
||
|
||
#include <config.h>
|
||
#include <math.h>
|
||
#include <libart_lgpl/art_misc.h>
|
||
#include <libart_lgpl/art_affine.h>
|
||
#include <libart_lgpl/art_pixbuf.h>
|
||
#include <libart_lgpl/art_rgb_pixbuf_affine.h>
|
||
#include <libart_lgpl/art_alphagamma.h>
|
||
#include "gdk-pixbuf.h"
|
||
|
||
|
||
|
||
/* Reference counting */
|
||
|
||
/**
|
||
* gdk_pixbuf_ref:
|
||
* @pixbuf: A pixbuf.
|
||
*
|
||
* Adds a reference to a pixbuf. It must be released afterwards using
|
||
* gdk_pixbuf_unref().
|
||
**/
|
||
void
|
||
gdk_pixbuf_ref (GdkPixbuf *pixbuf)
|
||
{
|
||
g_return_if_fail (pixbuf != NULL);
|
||
g_return_if_fail (pixbuf->ref_count > 0);
|
||
|
||
pixbuf->ref_count++;
|
||
}
|
||
|
||
/**
|
||
* gdk_pixbuf_unref:
|
||
* @pixbuf: A pixbuf.
|
||
*
|
||
* Removes a reference from a pixbuf. It will be destroyed when the reference
|
||
* count drops to zero.
|
||
**/
|
||
void
|
||
gdk_pixbuf_unref (GdkPixbuf *pixbuf)
|
||
{
|
||
g_return_if_fail (pixbuf != NULL);
|
||
g_return_if_fail (pixbuf->ref_count > 0);
|
||
|
||
pixbuf->ref_count--;
|
||
|
||
if (pixbuf->ref_count == 0) {
|
||
art_pixbuf_free (pixbuf->art_pixbuf);
|
||
pixbuf->art_pixbuf = NULL;
|
||
g_free (pixbuf);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/* Wrap a libart pixbuf */
|
||
|
||
/**
|
||
* gdk_pixbuf_new_from_art_pixbuf:
|
||
* @art_pixbuf: A libart pixbuf.
|
||
*
|
||
* Creates a &GdkPixbuf by wrapping a libart pixbuf.
|
||
*
|
||
* Return value: A newly-created &GdkPixbuf structure with a reference count of
|
||
* 1.
|
||
**/
|
||
GdkPixbuf *
|
||
gdk_pixbuf_new_from_art_pixbuf (ArtPixBuf *art_pixbuf)
|
||
{
|
||
GdkPixbuf *pixbuf;
|
||
|
||
g_return_val_if_fail (art_pixbuf != NULL, NULL);
|
||
|
||
pixbuf = g_new (GdkPixbuf, 1);
|
||
pixbuf->ref_count = 1;
|
||
pixbuf->art_pixbuf = art_pixbuf;
|
||
|
||
return pixbuf;
|
||
}
|
||
|
||
/* Destroy notification function for gdk_pixbuf_new() */
|
||
static void
|
||
free_buffer (gpointer user_data, gpointer data)
|
||
{
|
||
free (data);
|
||
}
|
||
|
||
/**
|
||
* gdk_pixbuf_new:
|
||
* @format: Image format.
|
||
* @has_alpha: Whether the image should have transparency information.
|
||
* @bits_per_sample: Number of bits per color sample.
|
||
* @width: Width of image in pixels.
|
||
* @height: Height of image in pixels.
|
||
*
|
||
* Creates a new &GdkPixbuf structure and allocates a buffer for it. The buffer
|
||
* has an optimal rowstride. Note that the buffer is not cleared; you will have
|
||
* to fill it completely.
|
||
*
|
||
* Return value: A newly-created &GdkPixbuf, or NULL if not enough memory
|
||
* could be allocated for the image buffer.
|
||
**/
|
||
GdkPixbuf *
|
||
gdk_pixbuf_new (ArtPixFormat format, gboolean has_alpha, int bits_per_sample,
|
||
int width, int height)
|
||
{
|
||
guchar *buf;
|
||
int channels;
|
||
int rowstride;
|
||
|
||
g_return_val_if_fail (format == ART_PIX_RGB, NULL);
|
||
g_return_val_if_fail (bits_per_sample == 8, NULL);
|
||
g_return_val_if_fail (width > 0, NULL);
|
||
g_return_val_if_fail (height > 0, NULL);
|
||
|
||
/* Always align rows to 32-bit boundaries */
|
||
|
||
channels = has_alpha ? 4 : 3;
|
||
rowstride = 4 * ((channels * width + 3) / 4);
|
||
|
||
buf = malloc (height * rowstride);
|
||
if (!buf)
|
||
return NULL;
|
||
|
||
return gdk_pixbuf_new_from_data (buf, format, has_alpha, width, height, rowstride,
|
||
free_buffer, NULL);
|
||
}
|