#ifdef G_OS_WIN32
#include <fcntl.h>
+#include <io.h>
+#define lseek(a,b,c) _lseek(a,b,c)
#define O_RDWR _O_RDWR
#endif
\f
typedef struct _TiffContext TiffContext;
struct _TiffContext
{
- ModulePreparedNotifyFunc prepare_func;
- ModuleUpdatedNotifyFunc update_func;
+ GdkPixbufModuleSizeFunc size_func;
+ GdkPixbufModulePreparedFunc prepare_func;
+ GdkPixbufModuleUpdatedFunc update_func;
gpointer user_data;
guchar *buffer;
\f
-/* There's no user data for the error handlers, so we just have to
- * put a big-ass lock on the whole TIFF loader
- */
-G_LOCK_DEFINE_STATIC (tiff_loader);
static char *global_error = NULL;
static TIFFErrorHandler orig_error_handler = NULL;
static TIFFErrorHandler orig_warning_handler = NULL;
/* 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);
+ }
}
\f
+static void free_buffer (guchar *pixels, gpointer data)
+{
+ g_free (pixels);
+}
+
+#if TIFFLIB_VERSION >= 20031226
+static gboolean tifflibversion (int *major, int *minor, int *revision)
+{
+ if (sscanf (TIFFGetVersion(),
+ "LIBTIFF, Version %d.%d.%d",
+ major, minor, revision) < 3)
+ return FALSE;
+
+ return TRUE;
+}
+#endif
+
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;
+#if TIFFLIB_VERSION >= 20031226
+ gint major, minor, revision;
+#endif
/* 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,
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_INSUFFICIENT_MEMORY,
- _("Insufficient memory to open TIFF file"));
- return NULL;
+ 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;
}
+ if (context && context->size_func) {
+ gint w = width;
+ gint h = height;
+ (* context->size_func) (&w, &h, context->user_data);
+
+ if (w == 0 || h == 0)
+ 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) {
+ pixels = g_try_malloc (bytes);
+
+ if (!pixels) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Insufficient memory to open TIFF file"));
- g_object_unref (pixbuf);
-
return NULL;
- }
-
- if (!TIFFReadRGBAImage (tiff, w, h, rast, 1) || global_error) {
- tiff_set_error (error,
- GDK_PIXBUF_ERROR_FAILED,
- _("Failed to load RGB data from TIFF file"));
- g_object_unref (pixbuf);
- _TIFFfree (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;
}
-
- 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 (context && context->prepare_func)
+ (* context->prepare_func) (pixbuf, NULL, context->user_data);
+
+#if TIFFLIB_VERSION >= 20031226
+ if (tifflibversion(&major, &minor, &revision) && major == 3 &&
+ (minor > 6 || (minor == 6 && revision > 0))) {
+ if (!TIFFReadRGBAImageOriented (tiff, width, height, (uint32 *)pixels, ORIENTATION_TOPLEFT, 1) || global_error)
+ {
+ tiff_set_error (error,
+ GDK_PIXBUF_ERROR_FAILED,
+ _("Failed to load RGB data from TIFF file"));
+ g_object_unref (pixbuf);
+ return NULL;
}
- }
- _TIFFfree (rast);
+#if G_BYTE_ORDER == G_BIG_ENDIAN
+ /* 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
+ }
+ else
+#endif
+ {
+ uint32 *rast, *tmp_rast;
+ gint x, y;
+ guchar *tmppix;
+
+ /* Yes, it needs to be _TIFFMalloc... */
+ rast = (uint32 *) _TIFFmalloc (width * height * sizeof (uint32));
+ if (!rast) {
+ g_set_error (error,
+ GDK_PIXBUF_ERROR,
+ GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
+ _("Insufficient memory to open TIFF file"));
+ g_object_unref (pixbuf);
+
+ return NULL;
+ }
+ if (!TIFFReadRGBAImage (tiff, width, height, rast, 1) || global_error) {
+ tiff_set_error (error,
+ GDK_PIXBUF_ERROR_FAILED,
+ _("Failed to load RGB data from TIFF file"));
+ g_object_unref (pixbuf);
+ _TIFFfree (rast);
+
+ return NULL;
+ }
+
+ pixels = gdk_pixbuf_get_pixels (pixbuf);
+
+ g_assert (pixels);
+
+ tmppix = pixels;
+
+ for (y = 0; y < height; y++) {
+ /* Unexplainable...are tiffs backwards? */
+ /* Also looking at the GIMP plugin, this
+ * whole reading thing can be a bit more
+ * robust.
+ */
+ tmp_rast = rast + ((height - y - 1) * width);
+ for (x = 0; x < width; 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;
+ }
+ }
+
+ _TIFFfree (rast);
+ }
- G_UNLOCK (tiff_loader);
- if (context)
- (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
- G_LOCK (tiff_loader);
+ if (context && context->update_func)
+ (* context->update_func) (pixbuf, 0, 0, width, height, context->user_data);
return pixbuf;
}
g_return_val_if_fail (f != NULL, NULL);
- G_LOCK (tiff_loader);
-
tiff_push_handlers ();
fd = fileno (f);
_("Failed to open TIFF image"));
tiff_pop_handlers ();
- G_UNLOCK (tiff_loader);
return NULL;
}
tiff_pop_handlers ();
- G_UNLOCK (tiff_loader);
-
return pixbuf;
}
/* 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)
{
TiffContext *context;
context = g_new0 (TiffContext, 1);
+ context->size_func = size_func;
context->prepare_func = prepare_func;
context->update_func = update_func;
context->user_data = user_data;
}
static tsize_t
-tiff_read (thandle_t handle, tdata_t buf, tsize_t size)
+tiff_load_read (thandle_t handle, tdata_t buf, tsize_t size)
{
TiffContext *context = (TiffContext *)handle;
}
static tsize_t
-tiff_write (thandle_t handle, tdata_t buf, tsize_t size)
+tiff_load_write (thandle_t handle, tdata_t buf, tsize_t size)
{
return -1;
}
static toff_t
-tiff_seek (thandle_t handle, toff_t offset, int whence)
+tiff_load_seek (thandle_t handle, toff_t offset, int whence)
{
TiffContext *context = (TiffContext *)handle;
switch (whence) {
case SEEK_SET:
- if (offset > context->used || offset < 0)
+ if (offset > context->used)
return -1;
context->pos = offset;
break;
break;
default:
return -1;
- break;
}
return context->pos;
}
static int
-tiff_close (thandle_t context)
+tiff_load_close (thandle_t context)
{
return 0;
}
static toff_t
-tiff_size (thandle_t handle)
+tiff_load_size (thandle_t handle)
{
TiffContext *context = (TiffContext *)handle;
}
static int
-tiff_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
+tiff_load_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
{
TiffContext *context = (TiffContext *)handle;
}
static void
-tiff_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
+tiff_load_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
{
}
g_return_val_if_fail (data != NULL, FALSE);
- G_LOCK (tiff_loader);
-
tiff_push_handlers ();
tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data,
- tiff_read, tiff_write,
- tiff_seek, tiff_close,
- tiff_size,
- tiff_map_file, tiff_unmap_file);
+ tiff_load_read, tiff_load_write,
+ tiff_load_seek, tiff_load_close,
+ tiff_load_size,
+ tiff_load_map_file, tiff_load_unmap_file);
if (!tiff || global_error) {
tiff_set_error (error,
GDK_PIXBUF_ERROR_FAILED,
if (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);
tiff_pop_handlers ();
- G_UNLOCK (tiff_loader);
-
return retval;
}
return TRUE;
}
+typedef struct {
+ gchar *buffer;
+ guint allocated;
+ guint used;
+ guint pos;
+} TiffSaveContext;
+
+static tsize_t
+tiff_save_read (thandle_t handle, tdata_t buf, tsize_t size)
+{
+ return -1;
+}
+
+static tsize_t
+tiff_save_write (thandle_t handle, tdata_t buf, tsize_t size)
+{
+ TiffSaveContext *context = (TiffSaveContext *)handle;
+
+ /* Modify buffer length */
+ if (context->pos + size > context->used)
+ context->used = context->pos + size;
+
+ /* Realloc */
+ if (context->used > context->allocated) {
+ context->buffer = g_realloc (context->buffer, context->pos + size);
+ context->allocated = context->used;
+ }
+
+ /* Now copy the data */
+ memcpy (context->buffer + context->pos, buf, size);
+
+ /* Update pos */
+ context->pos += size;
+
+ return size;
+}
+
+static toff_t
+tiff_save_seek (thandle_t handle, toff_t offset, int whence)
+{
+ TiffSaveContext *context = (TiffSaveContext *)handle;
+
+ switch (whence) {
+ case SEEK_SET:
+ context->pos = offset;
+ break;
+ case SEEK_CUR:
+ context->pos += offset;
+ break;
+ case SEEK_END:
+ context->pos = context->used + offset;
+ break;
+ default:
+ return -1;
+ }
+ return context->pos;
+}
+
+static int
+tiff_save_close (thandle_t context)
+{
+ return 0;
+}
+
+static toff_t
+tiff_save_size (thandle_t handle)
+{
+ return -1;
+}
+
+static TiffSaveContext *
+create_save_context (void)
+{
+ TiffSaveContext *context;
+
+ context = g_new (TiffSaveContext, 1);
+ context->buffer = NULL;
+ context->allocated = 0;
+ context->used = 0;
+ context->pos = 0;
+
+ return context;
+}
+
+static void
+free_save_context (TiffSaveContext *context)
+{
+ g_free (context->buffer);
+ g_free (context);
+}
+
+static gboolean
+gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc save_func,
+ gpointer user_data,
+ GdkPixbuf *pixbuf,
+ gchar **keys,
+ gchar **values,
+ GError **error)
+{
+ TIFF *tiff;
+ gint width, height, rowstride;
+ guchar *pixels;
+ gboolean has_alpha;
+ gushort alpha_samples[1] = { EXTRASAMPLE_UNASSALPHA };
+ int y;
+ TiffSaveContext *context;
+ gboolean retval;
+
+ tiff_push_handlers ();
+
+ context = create_save_context ();
+ tiff = TIFFClientOpen ("libtiff-pixbuf", "w", context,
+ tiff_save_read, tiff_save_write,
+ tiff_save_seek, tiff_save_close,
+ tiff_save_size,
+ NULL, NULL);
+
+ if (!tiff || global_error) {
+ tiff_set_error (error,
+ GDK_PIXBUF_ERROR_FAILED,
+ _("Failed to save TIFF image"));
+
+ tiff_pop_handlers ();
+
+ free_save_context (context);
+ return FALSE;
+ }
+
+ rowstride = gdk_pixbuf_get_rowstride (pixbuf);
+ pixels = gdk_pixbuf_get_pixels (pixbuf);
+
+ has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
+
+ height = gdk_pixbuf_get_height (pixbuf);
+ width = gdk_pixbuf_get_width (pixbuf);
+
+ TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, width);
+ TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, height);
+ TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
+ TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, has_alpha ? 4 : 3);
+ TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, height);
+
+ if (has_alpha)
+ TIFFSetField (tiff, TIFFTAG_EXTRASAMPLES, 1, alpha_samples);
+
+ TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
+ TIFFSetField (tiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
+ TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+
+ for (y = 0; y < height; y++) {
+ if (TIFFWriteScanline (tiff, pixels + y * rowstride, y, 0) == -1 ||
+ global_error)
+ break;
+ }
+
+ if (global_error) {
+ tiff_set_error (error,
+ GDK_PIXBUF_ERROR_FAILED,
+ _("Failed to write TIFF data"));
+
+ TIFFClose (tiff);
+
+ free_save_context (context);
+ tiff_pop_handlers ();
+
+ return FALSE;
+ }
+
+ TIFFClose (tiff);
+ if (global_error) {
+ tiff_set_error (error,
+ GDK_PIXBUF_ERROR_FAILED,
+ _("TIFFClose operation failed"));
+
+ free_save_context (context);
+ tiff_pop_handlers ();
+
+ return FALSE;
+ }
+
+ tiff_pop_handlers ();
+
+ /* Now call the callback */
+ retval = save_func (context->buffer, context->used, error, user_data);
+
+ free_save_context (context);
+
+ return retval;
+}
+
+static gboolean
+save_to_file_cb (const gchar *buf,
+ gsize count,
+ GError **error,
+ gpointer data)
+{
+ gint bytes;
+
+ while (count > 0) {
+ bytes = fwrite (buf, sizeof (gchar), count, (FILE *) data);
+ if (bytes <= 0)
+ break;
+ count -= bytes;
+ buf += bytes;
+ }
+
+ if (count) {
+ g_set_error (error,
+ GDK_PIXBUF_ERROR,
+ GDK_PIXBUF_ERROR_FAILED,
+ _("Couldn't write to TIFF file"));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+gdk_pixbuf__tiff_image_save (FILE *f,
+ GdkPixbuf *pixbuf,
+ gchar **keys,
+ gchar **values,
+ GError **error)
+{
+ return gdk_pixbuf__tiff_image_save_to_callback (save_to_file_cb,
+ f, pixbuf, keys,
+ values, error);
+}
+
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;
+ module->save = gdk_pixbuf__tiff_image_save;
+ module->save_to_callback = gdk_pixbuf__tiff_image_save_to_callback;
+}
+
+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;
+ /* not threadsafe, due to the error handler handling */
+ info->flags = GDK_PIXBUF_FORMAT_WRITABLE;
+ info->license = "LGPL";
}