2 * Copyright (C) 1999 Mark Crichton
3 * Copyright (C) 1999 The Free Software Foundation
4 * Copyright (C) 2004 John Ellis
5 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * Authors: Mark Crichton <crichton@gimp.org>
8 * Federico Mena-Quintero <federico@gimp.org>
9 * Jonathan Blandford <jrb@redhat.com>
10 * S�ren Sandmann <sandmann@daimi.au.dk>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "image-load.h"
31 #include "image_load_tiff.h"
37 typedef struct _ImageLoaderTiff ImageLoaderTiff;
38 struct _ImageLoaderTiff {
39 ImageLoaderBackendCbAreaUpdated area_updated_cb;
40 ImageLoaderBackendCbSize size_cb;
41 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
46 guint requested_width;
47 guint requested_height;
58 static void free_buffer (guchar *pixels, gpointer data)
64 tiff_load_read (thandle_t handle, tdata_t buf, tsize_t size)
66 ImageLoaderTiff *context = (ImageLoaderTiff *)handle;
68 if (context->pos + size > context->used)
71 memcpy (buf, context->buffer + context->pos, size);
77 tiff_load_write (thandle_t handle, tdata_t buf, tsize_t size)
83 tiff_load_seek (thandle_t handle, toff_t offset, int whence)
85 ImageLoaderTiff *context = (ImageLoaderTiff *)handle;
90 if (offset > context->used)
92 context->pos = offset;
95 if (offset + context->pos >= context->used)
97 context->pos += offset;
100 if (offset + context->used > context->used)
102 context->pos = context->used + offset;
112 tiff_load_close (thandle_t context)
118 tiff_load_size (thandle_t handle)
120 ImageLoaderTiff *context = (ImageLoaderTiff *)handle;
121 return context->used;
125 tiff_load_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
127 ImageLoaderTiff *context = (ImageLoaderTiff *)handle;
129 *buf = (tdata_t *) context->buffer;
130 *size = context->used;
136 tiff_load_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
140 static gboolean image_loader_tiff_load (gpointer loader, const guchar *buf, gsize count, GError **error)
142 ImageLoaderTiff *lt = (ImageLoaderTiff *) loader;
145 guchar *pixels = NULL;
146 gint width, height, rowstride;
155 TIFFSetWarningHandler(NULL);
157 tiff = TIFFClientOpen ( "libtiff-geeqie", "r", lt,
158 tiff_load_read, tiff_load_write,
159 tiff_load_seek, tiff_load_close,
161 tiff_load_map_file, tiff_load_unmap_file);
164 DEBUG_1("Failed to open TIFF image");
172 } while (TIFFReadDirectory(tiff));
173 lt->page_total = dircount;
176 if (!TIFFSetDirectory(tiff, lt->page_num))
178 DEBUG_1("Failed to open TIFF image");
183 if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width))
185 DEBUG_1("Could not get image width (bad TIFF file)");
190 if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height))
192 DEBUG_1("Could not get image height (bad TIFF file)");
197 if (width <= 0 || height <= 0)
199 DEBUG_1("Width or height of TIFF image is zero");
204 rowstride = width * 4;
205 if (rowstride / 4 != width)
207 DEBUG_1("Dimensions of TIFF image too large: width %d", width);
212 bytes = (size_t) height * rowstride;
213 if (bytes / rowstride != (size_t) height)
215 DEBUG_1("Dimensions of TIFF image too large: height %d", height);
220 lt->requested_width = width;
221 lt->requested_height = height;
222 lt->size_cb(loader, lt->requested_width, lt->requested_height, lt->data);
224 pixels = g_try_malloc (bytes);
228 DEBUG_1("Insufficient memory to open TIFF file: need %zu", bytes);
233 lt->pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
234 width, height, rowstride,
239 DEBUG_1("Insufficient memory to open TIFF file");
244 lt->area_prepared_cb(loader, lt->data);
246 if (TIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &rowsperstrip))
250 const size_t line_bytes = width * sizeof(uint32);
251 guchar *wrk_line = (guchar *)g_malloc(line_bytes);
253 for (row = 0; row < height; row += rowsperstrip)
255 int rows_to_write, i_row;
261 /* Read the strip into an RGBA array */
262 if (!TIFFReadRGBAStrip(tiff, row, (uint32 *)(pixels + row * rowstride))) {
267 * Figure out the number of scanlines actually in this strip.
269 if (row + (int)rowsperstrip > height)
270 rows_to_write = height - row;
272 rows_to_write = rowsperstrip;
275 * For some reason the TIFFReadRGBAStrip() function chooses the
276 * lower left corner as the origin. Vertically mirror scanlines.
278 for (i_row = 0; i_row < rows_to_write / 2; i_row++)
280 guchar *top_line, *bottom_line;
282 top_line = pixels + (row + i_row) * rowstride;
283 bottom_line = pixels + (row + rows_to_write - i_row - 1) * rowstride;
285 memcpy(wrk_line, top_line, line_bytes);
286 memcpy(top_line, bottom_line, line_bytes);
287 memcpy(bottom_line, wrk_line, line_bytes);
289 lt->area_updated_cb(loader, 0, row, width, rows_to_write, lt->data);
295 /* fallback, tiled tiff */
296 if (!TIFFReadRGBAImageOriented (tiff, width, height, (uint32 *)pixels, ORIENTATION_TOPLEFT, 1))
302 #if G_BYTE_ORDER == G_BIG_ENDIAN
303 /* Turns out that the packing used by TIFFRGBAImage depends on
304 * the host byte order...
307 guchar *ptr = pixels;
308 while (ptr < pixels + bytes)
310 uint32 pixel = *(uint32 *)ptr;
311 int r = TIFFGetR(pixel);
312 int g = TIFFGetG(pixel);
313 int b = TIFFGetB(pixel);
314 int a = TIFFGetA(pixel);
323 lt->area_updated_cb(loader, 0, 0, width, height, lt->data);
331 static gpointer image_loader_tiff_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
333 ImageLoaderTiff *loader = g_new0(ImageLoaderTiff, 1);
335 loader->area_updated_cb = area_updated_cb;
336 loader->size_cb = size_cb;
337 loader->area_prepared_cb = area_prepared_cb;
339 return (gpointer) loader;
343 static void image_loader_tiff_set_size(gpointer loader, int width, int height)
345 ImageLoaderTiff *lt = (ImageLoaderTiff *) loader;
346 lt->requested_width = width;
347 lt->requested_height = height;
350 static GdkPixbuf* image_loader_tiff_get_pixbuf(gpointer loader)
352 ImageLoaderTiff *lt = (ImageLoaderTiff *) loader;
356 static gchar* image_loader_tiff_get_format_name(gpointer loader)
358 return g_strdup("tiff");
360 static gchar** image_loader_tiff_get_format_mime_types(gpointer loader)
362 static gchar *mime[] = {"image/tiff", NULL};
363 return g_strdupv(mime);
366 static gboolean image_loader_tiff_close(gpointer loader, GError **error)
371 static void image_loader_tiff_abort(gpointer loader)
373 ImageLoaderTiff *lt = (ImageLoaderTiff *) loader;
377 static void image_loader_tiff_free(gpointer loader)
379 ImageLoaderTiff *lt = (ImageLoaderTiff *) loader;
380 if (lt->pixbuf) g_object_unref(lt->pixbuf);
384 static void image_loader_tiff_set_page_num(gpointer loader, gint page_num)
386 ImageLoader *il = (ImageLoader *) loader;
387 ImageLoaderTiff *lt = (ImageLoaderTiff *) loader;
389 lt->page_num = page_num;
392 static gint image_loader_tiff_get_page_total(gpointer loader)
394 ImageLoaderTiff *lt = (ImageLoaderTiff *) loader;
396 return lt->page_total;
399 void image_loader_backend_set_tiff(ImageLoaderBackend *funcs)
401 funcs->loader_new = image_loader_tiff_new;
402 funcs->set_size = image_loader_tiff_set_size;
403 funcs->load = image_loader_tiff_load;
405 funcs->get_pixbuf = image_loader_tiff_get_pixbuf;
406 funcs->close = image_loader_tiff_close;
407 funcs->abort = image_loader_tiff_abort;
408 funcs->free = image_loader_tiff_free;
410 funcs->get_format_name = image_loader_tiff_get_format_name;
411 funcs->get_format_mime_types = image_loader_tiff_get_format_mime_types;
413 funcs->set_page_num = image_loader_tiff_set_page_num;
414 funcs->get_page_total = image_loader_tiff_get_page_total;
420 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */