grim/guifications3

moved gflib-gtk to the new cmake module
cmake
2010-12-15, Gary Kramlich
b6418db658c1
moved gflib-gtk to the new cmake module
/*
* Guifications - The end-all, be-all notification framework
* Copyright (C) 2003-2009 Gary Kramlich <grim@reaperworld.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <gflib/gf_image.h>
#define GF_IMAGE_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE((obj), GF_TYPE_IMAGE, GfImagePrivate))
/******************************************************************************
* Structs
*****************************************************************************/
typedef struct {
guchar *data;
gsize data_sz;
} GfImagePrivate;
/******************************************************************************
* Globals
*****************************************************************************/
static GfObjectClass *parent_class = NULL;
/******************************************************************************
* Object Stuff
*****************************************************************************/
static void
gf_image_finalize(GObject *obj) {
GfImagePrivate *priv = GF_IMAGE_GET_PRIVATE(obj);
g_free(priv->data);
G_OBJECT_CLASS(parent_class)->finalize(obj);
}
static void
gf_image_class_init(GfImageClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
parent_class = g_type_class_peek_parent(klass);
g_type_class_add_private(klass, sizeof(GfImagePrivate));
obj_class->finalize = gf_image_finalize;
}
/******************************************************************************
* Image API
*****************************************************************************/
GType
gf_image_get_type(void) {
static GType type = 0;
if(type == 0) {
static const GTypeInfo info = {
sizeof(GfImageClass),
NULL,
NULL,
(GClassInitFunc)gf_image_class_init,
NULL,
NULL,
sizeof(GfImage),
0,
NULL,
NULL
};
type = g_type_register_static(GF_TYPE_OBJECT,
"GfImage",
&info, 0);
}
return type;
}
/**
* gf_image_new_from_file:
* @filename : The name of the file to create the image from.
*
* Creates a new #GfImage from a file.
*
* Return Value: The new #GfImage or NULL.
*/
GfImage *
gf_image_new_from_file(const gchar *filename) {
GfImage *image;
GfImagePrivate *priv = NULL;
FILE *fp;
gint fd;
struct stat s;
g_return_val_if_fail(filename, NULL);
fp = fopen(filename, "rb");
g_return_val_if_fail(fp, NULL);
fd = fileno(fp);
if(fstat(fd, &s) != 0) {
fclose(fp);
return NULL;
}
image = g_object_new(GF_TYPE_IMAGE, NULL);
priv = GF_IMAGE_GET_PRIVATE(image);
priv->data = g_new0(guchar, s.st_size);
priv->data_sz = s.st_size;
fread(priv->data, 1, s.st_size, fp);
fclose(fp);
return image;
}
/**
* gf_image_new_from_data:
* @data : The image data.
* @data_sz : The size of the image data.
*
* Creates a new #GfImage from @data.
*
* Return Value: The new #GfImage or NULL.
*/
GfImage *
gf_image_new_from_data(const guchar *data, gsize data_sz) {
GfImage *image;
g_return_val_if_fail(data, NULL);
g_return_val_if_fail(data_sz > 0, NULL);
image = g_object_new(GF_TYPE_IMAGE, NULL);
gf_image_set_data(image, data, data_sz);
return image;
}
/**
* gf_image_get_data:
* @image : The #GfImage instance.
* @data_sz : Return address for the size of the data or NULL.
*
* Gets the data from a #GfImage.
*
* Return Value: The image data.
*/
const guchar *
gf_image_get_data(const GfImage *image, gsize *data_sz) {
GfImagePrivate *priv = NULL;
g_return_val_if_fail(GF_IS_IMAGE(image), NULL);
priv = GF_IMAGE_GET_PRIVATE(image);
if(data_sz)
*data_sz = priv->data_sz;
return priv->data;
}
/**
* gf_image_set_data:
* @image : The #GfImage instance.
* @data : The data.
* @data_sz : The size of @data.
*
* Sets the data for a #GfImage.
*/
void
gf_image_set_data(GfImage *image, const guchar *data, gsize data_sz) {
GfImagePrivate *priv = NULL;
g_return_if_fail(GF_IS_IMAGE(image));
g_return_if_fail(data);
g_return_if_fail(data_sz > 0);
priv = GF_IMAGE_GET_PRIVATE(image);
g_free(priv->data);
priv->data = g_memdup(data, data_sz);
priv->data_sz = data_sz;
}