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.
28 #include "image-load-tiff.h"
34 #include <gdk-pixbuf/gdk-pixbuf.h>
35 #include <glib-object.h>
41 #include "image-load.h"
46 struct ImageLoaderTiff : public ImageLoaderBackend
49 ~ImageLoaderTiff() override;
51 void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
52 void set_size(int width, int height) override;
53 gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
54 GdkPixbuf *get_pixbuf() override;
55 void abort() override;
56 gchar *get_format_name() override;
57 gchar **get_format_mime_types() override;
58 void set_page_num(gint page_num) override;
59 gint get_page_total() override;
62 AreaUpdatedCb area_updated_cb;
63 SizePreparedCb size_prepared_cb;
64 AreaPreparedCb area_prepared_cb;
68 guint requested_width;
69 guint requested_height;
84 void free_buffer (guchar *pixels, gpointer)
89 tsize_t tiff_load_read (thandle_t handle, tdata_t buf, tsize_t size)
91 auto context = static_cast<GqTiffContext *>(handle);
93 if (context->pos + size > context->used)
96 memcpy (buf, context->buffer + context->pos, size);
101 tsize_t tiff_load_write (thandle_t, tdata_t, tsize_t)
106 toff_t tiff_load_seek (thandle_t handle, toff_t offset, int whence)
108 auto context = static_cast<GqTiffContext *>(handle);
113 if (offset > context->used)
115 context->pos = offset;
118 if (offset + context->pos >= context->used)
120 context->pos += offset;
123 if (offset + context->used > context->used)
125 context->pos = context->used + offset;
134 int tiff_load_close (thandle_t)
139 toff_t tiff_load_size (thandle_t handle)
141 auto context = static_cast<GqTiffContext *>(handle);
142 return context->used;
145 int tiff_load_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
147 auto context = static_cast<GqTiffContext *>(handle);
149 *buf = const_cast<guchar *>(context->buffer);
150 *size = context->used;
155 void tiff_load_unmap_file (thandle_t, tdata_t, toff_t)
159 gboolean ImageLoaderTiff::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
162 guchar *pixels = nullptr;
167 guint32 rowsperstrip;
170 TIFFSetWarningHandler(nullptr);
172 GqTiffContext context{buf, count, 0};
173 tiff = TIFFClientOpen ( "libtiff-geeqie", "r", &context,
174 tiff_load_read, tiff_load_write,
175 tiff_load_seek, tiff_load_close,
177 tiff_load_map_file, tiff_load_unmap_file);
180 DEBUG_1("Failed to open TIFF image");
186 } while (TIFFReadDirectory(tiff));
188 page_total = dircount;
190 if (!TIFFSetDirectory(tiff, page_num))
192 DEBUG_1("Failed to open TIFF image");
197 if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width))
199 DEBUG_1("Could not get image width (bad TIFF file)");
204 if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height))
206 DEBUG_1("Could not get image height (bad TIFF file)");
211 if (width <= 0 || height <= 0)
213 DEBUG_1("Width or height of TIFF image is zero");
218 rowstride = width * 4;
219 if (rowstride / 4 != width)
221 DEBUG_1("Dimensions of TIFF image too large: width %d", width);
226 bytes = static_cast<size_t>(height) * rowstride;
227 if (bytes / rowstride != static_cast<size_t>(height))
229 DEBUG_1("Dimensions of TIFF image too large: height %d", height);
234 requested_width = width;
235 requested_height = height;
236 size_prepared_cb(nullptr, requested_width, requested_height, data);
238 pixels = static_cast<guchar *>(g_try_malloc (bytes));
242 DEBUG_1("Insufficient memory to open TIFF file: need %zu", bytes);
247 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
248 width, height, rowstride,
249 free_buffer, nullptr);
253 DEBUG_1("Insufficient memory to open TIFF file");
258 area_prepared_cb(nullptr, data);
260 if (TIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &rowsperstrip))
264 const size_t line_bytes = width * sizeof(guint32);
265 auto wrk_line = static_cast<guchar *>(g_malloc(line_bytes));
267 for (row = 0; row < height; row += rowsperstrip)
276 /* Read the strip into an RGBA array */
277 if (!TIFFReadRGBAStrip(tiff, row, reinterpret_cast<guint32 *>(pixels + row * rowstride))) {
282 * Figure out the number of scanlines actually in this strip.
284 if (row + static_cast<int>(rowsperstrip) > height)
285 rows_to_write = height - row;
287 rows_to_write = rowsperstrip;
290 * For some reason the TIFFReadRGBAStrip() function chooses the
291 * lower left corner as the origin. Vertically mirror scanlines.
293 for (i_row = 0; i_row < rows_to_write / 2; i_row++)
298 top_line = pixels + (row + i_row) * rowstride;
299 bottom_line = pixels + (row + rows_to_write - i_row - 1) * rowstride;
301 memcpy(wrk_line, top_line, line_bytes);
302 memcpy(top_line, bottom_line, line_bytes);
303 memcpy(bottom_line, wrk_line, line_bytes);
305 area_updated_cb(nullptr, 0, row, width, rows_to_write, data);
311 /* fallback, tiled tiff */
312 if (!TIFFReadRGBAImageOriented (tiff, width, height, reinterpret_cast<guint32 *>(pixels), ORIENTATION_TOPLEFT, 1))
318 #if G_BYTE_ORDER == G_BIG_ENDIAN
319 /* Turns out that the packing used by TIFFRGBAImage depends on
320 * the host byte order...
323 guchar *ptr = pixels;
324 while (ptr < pixels + bytes)
326 guint32 pixel = *(guint32 *)ptr;
327 int r = TIFFGetR(pixel);
328 int g = TIFFGetG(pixel);
329 int b = TIFFGetB(pixel);
330 int a = TIFFGetA(pixel);
339 area_updated_cb(nullptr, 0, 0, width, height, data);
348 void ImageLoaderTiff::init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data)
350 this->area_updated_cb = area_updated_cb;
351 this->size_prepared_cb = size_prepared_cb;
352 this->area_prepared_cb = area_prepared_cb;
356 void ImageLoaderTiff::set_size(int width, int height)
358 requested_width = width;
359 requested_height = height;
362 GdkPixbuf *ImageLoaderTiff::get_pixbuf()
367 gchar *ImageLoaderTiff::get_format_name()
369 return g_strdup("tiff");
372 gchar **ImageLoaderTiff::get_format_mime_types()
374 static const gchar *mime[] = {"image/tiff", nullptr};
375 return g_strdupv(const_cast<gchar **>(mime));
378 void ImageLoaderTiff::abort()
383 ImageLoaderTiff::~ImageLoaderTiff()
385 if (pixbuf) g_object_unref(pixbuf);
388 void ImageLoaderTiff::set_page_num(gint page_num)
390 this->page_num = page_num;
393 gint ImageLoaderTiff::get_page_total()
400 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_tiff()
402 return std::make_unique<ImageLoaderTiff>();
405 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */