X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=gdk-pixbuf%2Fio-tiff.c;h=a91255b0a2e23f92f8e10dcb0489c6c11befa29e;hb=45f7541f42d696d02464973e74b2684f690a0cdd;hp=d8b799344be3fbe49f1282a94ea5754e4571ebec;hpb=49b3e05a30bb7de83f3c1016d11ee7e482f439a5;p=~andy%2Fgtk diff --git a/gdk-pixbuf/io-tiff.c b/gdk-pixbuf/io-tiff.c index d8b799344..a91255b0a 100644 --- a/gdk-pixbuf/io-tiff.c +++ b/gdk-pixbuf/io-tiff.c @@ -47,8 +47,8 @@ typedef struct _TiffContext TiffContext; struct _TiffContext { - ModulePreparedNotifyFunc prepare_func; - ModuleUpdatedNotifyFunc update_func; + GdkPixbufModulePreparedFunc prepare_func; + GdkPixbufModuleUpdatedFunc update_func; gpointer user_data; guchar *buffer; @@ -119,50 +119,87 @@ tiff_set_error (GError **error, /* Take the error message from libtiff and merge it with * some context we provide. */ - g_set_error (error, - GDK_PIXBUF_ERROR, - error_code, - "%s%s%s", - msg, global_error ? ": " : "", global_error); - if (global_error) { + g_set_error (error, + GDK_PIXBUF_ERROR, + error_code, + "%s%s%s", msg, ": ", global_error); + g_free (global_error); global_error = NULL; } + else { + g_set_error (error, + GDK_PIXBUF_ERROR, + error_code, msg); + } } +static void free_buffer (guchar *pixels, gpointer data) +{ + g_free (pixels); +} + +static tileContigRoutine tiff_put_contig; +static tileSeparateRoutine tiff_put_separate; + +/* We're lucky that TIFFRGBAImage uses the same RGBA packing + as gdk-pixbuf, thus we can simple reuse the default libtiff + put routines, only adjusting the coordinate system. + */ +static void +put_contig (TIFFRGBAImage *img, uint32 *raster, + uint32 x, uint32 y, uint32 w, uint32 h, + int32 fromskew, int32 toskew, unsigned char *cp) +{ + uint32 *data = raster - y * img->width - x; + + tiff_put_contig (img, data + img->width * (img->height - 1 - y) + x, + x, y, w, h, fromskew, -toskew - 2*(int32)w, cp); +} + +static void +put_separate (TIFFRGBAImage *img, uint32 *raster, + uint32 x, uint32 y, uint32 w, uint32 h, + int32 fromskew, int32 toskew, + unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a) +{ + uint32 *data = raster - y * img->width - x; + + tiff_put_separate (img, data + img->width * (img->height - 1 - y) + x, + x, y, w, h, fromskew, -toskew - 2*w, r, g, b, a); +} + static GdkPixbuf * tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error) { guchar *pixels = NULL; - guchar *tmppix; - uint32 *rast, *tmp_rast; - gint w, h, x, y, num_pixs; + gint width, height, rowstride, bytes; GdkPixbuf *pixbuf; + TIFFRGBAImage img; + gchar emsg[1024]; /* We're called with the lock held. */ g_return_val_if_fail (global_error == NULL, NULL); - if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w) || global_error) { + if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width) || global_error) { tiff_set_error (error, GDK_PIXBUF_ERROR_FAILED, _("Could not get image width (bad TIFF file)")); return NULL; } - if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h) || global_error) { + if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height) || global_error) { tiff_set_error (error, GDK_PIXBUF_ERROR_FAILED, _("Could not get image height (bad TIFF file)")); return NULL; } - num_pixs = w * h; - - if (num_pixs == 0) { + if (width <= 0 || height <= 0) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, @@ -170,8 +207,27 @@ tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error) return NULL; } - pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h); - if (!pixbuf) { + rowstride = width * 4; + if (rowstride / 4 != width) { /* overflow */ + g_set_error (error, + GDK_PIXBUF_ERROR, + GDK_PIXBUF_ERROR_CORRUPT_IMAGE, + _("Dimensions of TIFF image too large")); + return NULL; + } + + bytes = height * rowstride; + if (bytes / rowstride != height) { /* overflow */ + g_set_error (error, + GDK_PIXBUF_ERROR, + GDK_PIXBUF_ERROR_CORRUPT_IMAGE, + _("Dimensions of TIFF image too large")); + return NULL; + } + + pixels = g_try_malloc (bytes); + + if (!pixels) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, @@ -179,59 +235,69 @@ tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error) return NULL; } - - G_UNLOCK (tiff_loader); - if (context) - (* context->prepare_func) (pixbuf, NULL, context->user_data); - G_LOCK (tiff_loader); - - /* Yes, it needs to be _TIFFMalloc... */ - rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32)); - if (!rast) { + pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, + width, height, rowstride, + free_buffer, NULL); + if (!pixbuf) { + g_free (pixels); g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Insufficient memory to open TIFF file")); return NULL; - } - - if (!TIFFReadRGBAImage (tiff, w, h, rast, 0) || global_error) { + } + + G_UNLOCK (tiff_loader); + if (context) + (* context->prepare_func) (pixbuf, NULL, context->user_data); + G_LOCK (tiff_loader); + + if (!TIFFRGBAImageBegin (&img, tiff, 1, emsg) || global_error) { tiff_set_error (error, GDK_PIXBUF_ERROR_FAILED, _("Failed to load RGB data from TIFF file")); - _TIFFfree (rast); - + g_object_unref (pixbuf); return NULL; } + + if (img.put.any == NULL) { + tiff_set_error (error, + GDK_PIXBUF_ERROR_FAILED, + _("Unsupported TIFF variant")); + g_object_unref (pixbuf); + return NULL; + } - pixels = gdk_pixbuf_get_pixels (pixbuf); - - g_assert (pixels); - - tmppix = pixels; - - for (y = 0; y < h; y++) { - /* Unexplainable...are tiffs backwards? */ - /* Also looking at the GIMP plugin, this - * whole reading thing can be a bit more - * robust. - */ - tmp_rast = rast + ((h - y - 1) * w); - for (x = 0; x < w; x++) { - tmppix[0] = TIFFGetR (*tmp_rast); - tmppix[1] = TIFFGetG (*tmp_rast); - tmppix[2] = TIFFGetB (*tmp_rast); - tmppix[3] = TIFFGetA (*tmp_rast); - tmp_rast++; - tmppix += 4; - } + if (img.isContig) { + tiff_put_contig = img.put.contig; + img.put.contig = put_contig; + } + else { + tiff_put_separate = img.put.separate; + img.put.separate = put_separate; } - _TIFFfree (rast); + TIFFRGBAImageGet (&img, (uint32 *)pixels, width, height); + TIFFRGBAImageEnd (&img); + +#ifdef WORDS_BIGENDIAN +/* Turns out that the packing used by TIFFRGBAImage depends on the host byte order... */ + while (pixels < pixbuf->pixels + bytes) { + uint32 pixel = *(uint32 *)pixels; + int r = TIFFGetR(pixel); + int g = TIFFGetG(pixel); + int b = TIFFGetB(pixel); + int a = TIFFGetA(pixel); + *pixels++ = r; + *pixels++ = g; + *pixels++ = b; + *pixels++ = a; + } +#endif G_UNLOCK (tiff_loader); if (context) - (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data); + (* context->update_func) (pixbuf, 0, 0, width, height, context->user_data); G_LOCK (tiff_loader); return pixbuf; @@ -255,6 +321,13 @@ gdk_pixbuf__tiff_image_load (FILE *f, GError **error) tiff_push_handlers (); fd = fileno (f); + + /* On OSF, apparently fseek() works in some on-demand way, so + * the fseek gdk_pixbuf_new_from_file() doesn't work here + * since we are using the raw file descriptor. So, we call lseek() on the fd + * before using it. (#60840) + */ + lseek (fd, 0, SEEK_SET); tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r"); if (!tiff || global_error) { @@ -288,8 +361,9 @@ gdk_pixbuf__tiff_image_load (FILE *f, GError **error) /* Progressive loader */ static gpointer -gdk_pixbuf__tiff_image_begin_load (ModulePreparedNotifyFunc prepare_func, - ModuleUpdatedNotifyFunc update_func, +gdk_pixbuf__tiff_image_begin_load (GdkPixbufModuleSizeFunc size_func, + GdkPixbufModulePreparedFunc prepare_func, + GdkPixbufModuleUpdatedFunc update_func, gpointer user_data, GError **error) { @@ -413,18 +487,22 @@ gdk_pixbuf__tiff_image_stop_load (gpointer data, pixbuf = tiff_image_parse (tiff, context, error); if (pixbuf) - g_object_unref (G_OBJECT (pixbuf)); + g_object_unref (pixbuf); retval = pixbuf != NULL; - TIFFClose (tiff); if (global_error) { tiff_set_error (error, GDK_PIXBUF_ERROR_FAILED, _("Failed to load TIFF image")); + tiff_pop_handlers (); + retval = FALSE; } } + if (tiff) + TIFFClose (tiff); + g_assert (!global_error); g_free (context->buffer); @@ -482,10 +560,36 @@ gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf, } void -gdk_pixbuf__tiff_fill_vtable (GdkPixbufModule *module) +MODULE_ENTRY (tiff, fill_vtable) (GdkPixbufModule *module) { module->load = gdk_pixbuf__tiff_image_load; module->begin_load = gdk_pixbuf__tiff_image_begin_load; module->stop_load = gdk_pixbuf__tiff_image_stop_load; module->load_increment = gdk_pixbuf__tiff_image_load_increment; } + +void +MODULE_ENTRY (tiff, fill_info) (GdkPixbufFormat *info) +{ + static GdkPixbufModulePattern signature[] = { + { "MM \x2a", " z ", 100 }, + { "II\x2a ", " z", 100 }, + { NULL, NULL, 0 } + }; + static gchar * mime_types[] = { + "image/tiff", + NULL + }; + static gchar * extensions[] = { + "tiff", + "tif", + NULL + }; + + info->name = "tiff"; + info->signature = signature; + info->description = N_("The TIFF image format"); + info->mime_types = mime_types; + info->extensions = extensions; + info->flags = 0; +}