Add pixbuf_duplicate,

Fix 'scale' API,
Add gdk_pixbuf_new,
clean io modules a tad.
This commit is contained in:
Michael Meeks 1999-09-22 22:30:51 +00:00
parent 372c4d2627
commit 6c13b03a45
9 changed files with 158 additions and 125 deletions

View File

@ -1,3 +1,29 @@
1999-09-22 Michael Meeks <michael@nuclecu.unam.mx>
* src/gdk-pixbuf.c (gdk_pixbuf_new): created.
(gdk_pixbuf_scale): use gdk_pixbuf_new + return a new scaled image.
* src/gdk-pixbuf.h (struct _GdkPixBuf): Re-organise struct, + add
GdkPixBufUnrefFunc + gdk_pixbuf_new.
* src/io-jpeg.c (image_load): clean to use gdk_pixbuf_new.
* src/io-xpm.c (_pixbuf_create_from_xpm): ditto.
* src/io-tiff.c (image_load): ditto + fix leak
* src/io-png.c (image_load): ditto + add more exit points; monitor.png
crashes this module ( add warning :-)
* src/io-bmp.c (image_load): ditto.
* src/io-gif.c (image_load): ditto.
1999-09-18 Michael Meeks <michael@nuclecu.unam.mx>
* src/gdk-pixbuf.c (gdk_pixbuf_scale): Hack rgba support in so
it doesn't crash scaling with alpha.
1999-09-17 Federico Mena Quintero <federico@redhat.com> 1999-09-17 Federico Mena Quintero <federico@redhat.com>
* src/io-bmp.c (image_load): Set the initial ref_count to 1. * src/io-bmp.c (image_load): Set the initial ref_count to 1.

View File

@ -21,9 +21,27 @@ void
gdk_pixbuf_destroy (GdkPixBuf *pixbuf) gdk_pixbuf_destroy (GdkPixBuf *pixbuf)
{ {
art_pixbuf_free (pixbuf->art_pixbuf); art_pixbuf_free (pixbuf->art_pixbuf);
pixbuf->art_pixbuf = NULL;
g_free (pixbuf); g_free (pixbuf);
} }
GdkPixBuf *
gdk_pixbuf_new (ArtPixBuf *art_pixbuf,
GdkPixBufUnrefFunc *unref_fn)
{
GdkPixBuf *pixbuf;
if (!art_pixbuf)
return NULL;
pixbuf = g_new (GdkPixBuf, 1);
pixbuf->ref_count = 1;
pixbuf->unref_fn = unref_fn;
pixbuf->art_pixbuf = art_pixbuf;
return pixbuf;
}
void void
gdk_pixbuf_ref (GdkPixBuf *pixbuf) gdk_pixbuf_ref (GdkPixBuf *pixbuf)
{ {
@ -46,19 +64,19 @@ gdk_pixbuf_unref (GdkPixBuf *pixbuf)
} }
GdkPixBuf * GdkPixBuf *
gdk_pixbuf_scale (GdkPixBuf *pixbuf, gint w, gint h) gdk_pixbuf_scale (const GdkPixBuf *pixbuf, gint w, gint h)
{ {
art_u8 *pixels; art_u8 *pixels;
gint rowstride; gint rowstride;
double affine[6]; double affine[6];
ArtAlphaGamma *alphagamma; ArtAlphaGamma *alphagamma;
ArtPixBuf *art_pixbuf = NULL; ArtPixBuf *art_pixbuf = NULL;
GdkPixBuf *copy = NULL;
alphagamma = NULL; alphagamma = NULL;
affine[1] = affine[2] = affine[4] = affine[5] = 0; affine[1] = affine[2] = affine[4] = affine[5] = 0;
affine[0] = w / (double)(pixbuf->art_pixbuf->width); affine[0] = w / (double)(pixbuf->art_pixbuf->width);
affine[3] = h / (double)(pixbuf->art_pixbuf->height); affine[3] = h / (double)(pixbuf->art_pixbuf->height);
@ -76,10 +94,24 @@ gdk_pixbuf_scale (GdkPixBuf *pixbuf, gint w, gint h)
else else
art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, rowstride); art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, rowstride);
art_pixbuf_free (pixbuf->art_pixbuf); copy = gdk_pixbuf_new (art_pixbuf, NULL);
pixbuf->art_pixbuf = art_pixbuf;
return pixbuf; if (!copy)
art_free (pixels);
return copy;
}
GdkPixBuf *
gdk_pixbuf_duplicate (const GdkPixBuf *pixbuf)
{
GdkPixBuf *copy = g_new (GdkPixBuf, 1);
copy->ref_count = 1;
copy->unref_fn = pixbuf->unref_fn;
copy->art_pixbuf = art_pixbuf_duplicate (pixbuf->art_pixbuf);
return copy;
} }
GdkPixBuf * GdkPixBuf *

View File

@ -5,18 +5,24 @@
#include <libart_lgpl/art_pixbuf.h> #include <libart_lgpl/art_pixbuf.h>
#include <glib.h> #include <glib.h>
typedef struct { typedef struct _GdkPixBuf GdkPixBuf;
typedef void (*GdkPixBufUnrefFunc) (GdkPixBuf *pixbuf);
struct _GdkPixBuf
{
int ref_count; int ref_count;
ArtPixBuf *art_pixbuf; ArtPixBuf *art_pixbuf;
void (*unref_func)(void *gdkpixbuf); GdkPixBufUnrefFunc *unref_fn;
} GdkPixBuf; };
GdkPixBuf *gdk_pixbuf_load_image (const char *file); GdkPixBuf *gdk_pixbuf_load_image (const char *file);
void gdk_pixbuf_save_image (const char *format_id, const char *file, ...); void gdk_pixbuf_save_image (const char *format_id, const char *file, ...);
GdkPixBuf *gdk_pixbuf_new (ArtPixBuf *art_pixbuf,
GdkPixBufUnrefFunc *unref_fn);
void gdk_pixbuf_ref (GdkPixBuf *pixbuf); void gdk_pixbuf_ref (GdkPixBuf *pixbuf);
void gdk_pixbuf_unref (GdkPixBuf *pixbuf); void gdk_pixbuf_unref (GdkPixBuf *pixbuf);
GdkPixBuf *gdk_pixbuf_duplicate (GdkPixBuf *pixbuf); GdkPixBuf *gdk_pixbuf_duplicate (const GdkPixBuf *pixbuf);
GdkPixBuf *gdk_pixbuf_scale (GdkPixBuf *pixbuf, gint w, gint h); GdkPixBuf *gdk_pixbuf_scale (const GdkPixBuf *pixbuf, gint w, gint h);
GdkPixBuf *gdk_pixbuf_rotate (GdkPixBuf *pixbuf, gdouble angle); GdkPixBuf *gdk_pixbuf_rotate (GdkPixBuf *pixbuf, gdouble angle);
void gdk_pixbuf_destroy (GdkPixBuf *pixbuf); void gdk_pixbuf_destroy (GdkPixBuf *pixbuf);

View File

@ -32,23 +32,16 @@
/* Shared library entry point */ /* Shared library entry point */
GdkPixBuf *image_load(FILE * f) GdkPixBuf *image_load(FILE * f)
{ {
GdkPixBuf *pixbuf;
art_u8 *pixels; art_u8 *pixels;
ArtPixBuf *art_pixbuf;
/* Ok, now stuff the GdkPixBuf with goodies */ /* Ok, now stuff the GdkPixBuf with goodies */
pixbuf = g_new(GdkPixBuf, 1);
if (is_trans) if (is_trans)
pixbuf->art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4)); art_pixbuf = art_pixbuf_new_rgba (pixels, w, h, (w * 4));
else else
pixbuf->art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3)); art_pixbuf = art_pixbuf_new_rgb (pixels, w, h, (w * 3));
/* Ok, I'm anal...shoot me */ /* Ok, I'm anal...shoot me */
if (!(pixbuf->art_pixbuf)) return gdk_pixbuf_new (art_pixbuf, NULL);
return NULL;
pixbuf->ref_count = 1;
pixbuf->unref_func = NULL;
return pixbuf;
} }

View File

@ -46,6 +46,7 @@ GdkPixBuf *image_load(FILE * f)
{8, 8, 4, 2}; {8, 8, 4, 2};
GdkPixBuf *pixbuf; GdkPixBuf *pixbuf;
ArtPixBuf *art_pixbuf;
g_return_val_if_fail(f != NULL, NULL); g_return_val_if_fail(f != NULL, NULL);
@ -158,24 +159,16 @@ GdkPixBuf *image_load(FILE * f)
} }
g_free(rows); g_free(rows);
/* Ok, now stuff the GdkPixBuf with goodies */
pixbuf = g_new(GdkPixBuf, 1);
if (is_trans) if (is_trans)
pixbuf->art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4)); art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4));
else else
pixbuf->art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3)); art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3));
pixbuf = gdk_pixbuf_new (art_pixbuf, NULL);
/* Ok, I'm anal...shoot me */ /* Ok, I'm anal...shoot me */
if (!(pixbuf->art_pixbuf)) { if (!pixbuf)
art_free(pixels); art_free(pixels);
g_free(pixbuf);
return NULL;
}
pixbuf->ref_count = 1;
pixbuf->unref_func = NULL;
return pixbuf; return pixbuf;
} }

View File

@ -114,15 +114,10 @@ GdkPixBuf *image_load(FILE *f)
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&cinfo);
/* finish off, create the pixbuf */ /* finish off, create the pixbuf */
pixbuf = g_new(GdkPixBuf, 1); pixbuf = gdk_pixbuf_new (art_pixbuf_new_rgb (pixels, w, h, (w * 3)),
pixbuf->art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3)); NULL);
if (!(pixbuf->art_pixbuf)) { if (!pixbuf)
art_free(pixels); art_free (pixels);
g_free(pixbuf);
return NULL;
}
pixbuf->ref_count = 1;
pixbuf->unref_func = NULL;
return pixbuf; return pixbuf;
} }

View File

@ -35,30 +35,35 @@ GdkPixBuf *image_load(FILE * f)
png_bytepp rows; png_bytepp rows;
art_u8 *pixels, *temp, *rowdata; art_u8 *pixels, *temp, *rowdata;
GdkPixBuf *pixbuf; GdkPixBuf *pixbuf;
ArtPixBuf *art_pixbuf;
g_return_val_if_fail(f != NULL, NULL); g_return_val_if_fail (f != NULL, NULL);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
NULL); NULL, NULL, NULL);
if (!png_ptr)
return NULL;
info_ptr = png_create_info_struct(png_ptr); info_ptr = png_create_info_struct (png_ptr);
if (!info_ptr) { if (!info_ptr) {
png_destroy_read_struct(&png_ptr, NULL, NULL); png_destroy_read_struct (&png_ptr, NULL, NULL);
return NULL; return NULL;
} }
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return NULL;
}
if (setjmp(png_ptr->jmpbuf)) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return NULL;
}
png_init_io(png_ptr, f);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &w, &h, &depth, &ctype, &inttype, end_info = png_create_info_struct (png_ptr);
if (!end_info) {
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
}
if (setjmp (png_ptr->jmpbuf)) {
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
return NULL;
}
png_init_io (png_ptr, f);
png_read_info (png_ptr, info_ptr);
png_get_IHDR (png_ptr, info_ptr, &w, &h, &depth, &ctype, &inttype,
NULL, NULL); NULL, NULL);
/* Ok, we want to work with 24 bit images. /* Ok, we want to work with 24 bit images.
@ -67,63 +72,64 @@ GdkPixBuf *image_load(FILE * f)
* everything into a format libart expects. * everything into a format libart expects.
* We also use png_set_strip_16 to reduce down to 8 bit/chan. * We also use png_set_strip_16 to reduce down to 8 bit/chan.
*/ */
if (ctype == PNG_COLOR_TYPE_PALETTE && depth <= 8) if (ctype == PNG_COLOR_TYPE_PALETTE && depth <= 8)
png_set_expand(png_ptr); png_set_expand (png_ptr);
if (ctype == PNG_COLOR_TYPE_GRAY && depth < 8) if (ctype == PNG_COLOR_TYPE_GRAY && depth < 8)
png_set_expand(png_ptr); png_set_expand (png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_expand(png_ptr); png_set_expand (png_ptr);
g_warning ("FIXME: We are going to crash");
}
if (depth == 16) if (depth == 16)
png_set_strip_16(png_ptr); png_set_strip_16 (png_ptr);
/* We also have png "packing" bits into bytes if < 8 */ /* We also have png "packing" bits into bytes if < 8 */
if (depth < 8) if (depth < 8)
png_set_packing(png_ptr); png_set_packing (png_ptr);
/* Lastly, if the PNG is greyscale, convert to RGB */ /* Lastly, if the PNG is greyscale, convert to RGB */
if (ctype == PNG_COLOR_TYPE_GRAY || ctype == PNG_COLOR_TYPE_GRAY_ALPHA) if (ctype == PNG_COLOR_TYPE_GRAY || ctype == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr); png_set_gray_to_rgb (png_ptr);
/* ...and if we're interlaced... */ /* ...and if we're interlaced... */
passes = png_set_interlace_handling(png_ptr); passes = png_set_interlace_handling (png_ptr);
/* Update our info structs */ /* Update our info structs */
png_read_update_info(png_ptr, info_ptr); png_read_update_info (png_ptr, info_ptr);
/* Allocate some memory and set up row array */ /* Allocate some memory and set up row array */
/* This "inhales vigirously"... */ /* This "inhales vigorously"... */
if (ctype & PNG_COLOR_MASK_ALPHA) if (ctype & PNG_COLOR_MASK_ALPHA)
bpp = 4; bpp = 4;
else else
bpp = 3; bpp = 3;
pixels = art_alloc(w * h * bpp); pixels = art_alloc (w * h * bpp);
rows = g_malloc(h * sizeof(png_bytep)); rows = g_malloc (h * sizeof(png_bytep));
if ((!pixels) || (!rows)) { if (!pixels || !rows) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
return NULL; return NULL;
} }
/* Icky code, but it has to be done... */ /* Icky code, but it has to be done... */
for (i = 0; i < h; i++) { for (i = 0; i < h; i++) {
if ((rows[i] = g_malloc(w * sizeof(art_u8) * bpp)) == NULL) { if ((rows[i] = g_malloc (w * sizeof (art_u8) * bpp)) == NULL) {
int n; int n;
for (n = 0; n < i; n++) for (n = 0; n < i; n++)
g_free(rows[i]); g_free (rows[i]);
g_free(rows); g_free (rows);
art_free(pixels); art_free (pixels);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
return NULL; return NULL;
} }
} }
/* And we FINALLY get here... */ /* And we FINALLY get here... */
png_read_image(png_ptr, rows); png_read_image (png_ptr, rows);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
/* Now stuff the bytes into pixels & free rows[y] */ /* Now stuff the bytes into pixels & free rows[y] */
@ -139,27 +145,19 @@ GdkPixBuf *image_load(FILE * f)
temp[3] = rowdata[(x * bpp) + 3]; temp[3] = rowdata[(x * bpp) + 3];
temp += bpp; temp += bpp;
} }
g_free(rows[y]); g_free (rows[y]);
} }
g_free(rows); g_free (rows);
/* Return the GdkPixBuf */
pixbuf = g_new(GdkPixBuf, 1);
if (ctype & PNG_COLOR_MASK_ALPHA) if (ctype & PNG_COLOR_MASK_ALPHA)
pixbuf->art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4)); art_pixbuf = art_pixbuf_new_rgba (pixels, w, h, (w * 4));
else else
pixbuf->art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3)); art_pixbuf = art_pixbuf_new_rgb (pixels, w, h, (w * 3));
/* Ok, I'm anal...shoot me */ pixbuf = gdk_pixbuf_new (art_pixbuf, NULL);
if (!(pixbuf->art_pixbuf)) {
art_free(pixels);
g_free(pixbuf);
return NULL;
}
pixbuf->ref_count = 1; if (!pixbuf)
pixbuf->unref_func = NULL; art_free (pixels);
return pixbuf; return pixbuf;
} }

View File

@ -87,15 +87,11 @@ GdkPixBuf *image_load(FILE * f)
_TIFFfree(rast); _TIFFfree(rast);
TIFFClose(tiff); TIFFClose(tiff);
/* Return the GdkPixBuf */ pixbuf = gdk_pixbuf_new (art_pixbuf_new_rgba (pixels, w, h, (w * 4)),
pixbuf = g_new(GdkPixBuf, 1); NULL);
pixbuf->art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4));
/* Ok, I'm anal...shoot me */ if (!pixbuf)
if (!(pixbuf->art_pixbuf)) art_free (pixels);
return NULL;
pixbuf->ref_count = 1;
pixbuf->unref_func = NULL;
return pixbuf; return pixbuf;
} }

View File

@ -306,6 +306,7 @@ static GdkPixBuf *
_XPMColor *colors, *color, *fallbackcolor; _XPMColor *colors, *color, *fallbackcolor;
art_u8 *pixels, *pixtmp; art_u8 *pixels, *pixtmp;
GdkPixBuf *pixbuf; GdkPixBuf *pixbuf;
ArtPixBuf *art_pixbuf;
buffer = (*get_buf) (op_header, handle); buffer = (*get_buf) (op_header, handle);
if (!buffer) { if (!buffer) {
@ -403,22 +404,15 @@ static GdkPixBuf *
/* Ok, now stuff the GdkPixBuf with goodies */ /* Ok, now stuff the GdkPixBuf with goodies */
pixbuf = g_new(GdkPixBuf, 1);
if (is_trans) if (is_trans)
pixbuf->art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4)); art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4));
else else
pixbuf->art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3)); art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3));
/* Ok, I'm anal...shoot me */ pixbuf = gdk_pixbuf_new (art_pixbuf, NULL);
if (!(pixbuf->art_pixbuf)) {
if (!pixbuf)
art_free(pixels); art_free(pixels);
g_free(pixbuf);
return NULL;
}
pixbuf->ref_count = 1;
pixbuf->unref_func = NULL;
return pixbuf; return pixbuf;
} }