-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathimage-load-tiff.cc
403 lines (337 loc) · 8.99 KB
/
image-load-tiff.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (C) 1999 Mark Crichton
* Copyright (C) 1999 The Free Software Foundation
* Copyright (C) 2004 John Ellis
* Copyright (C) 2008 - 2016 The Geeqie Team
*
* Authors: Mark Crichton <[email protected]>
* Federico Mena-Quintero <[email protected]>
* Jonathan Blandford <[email protected]>
* S�ren Sandmann <[email protected]>
* Vladimir Nadvornik
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "image-load-tiff.h"
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib-object.h>
#include <glib.h>
#include <tiff.h>
#include <tiffio.h>
#include "image-load.h"
namespace
{
struct ImageLoaderTiff : public ImageLoaderBackend
{
public:
~ImageLoaderTiff() override;
void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
void set_size(int width, int height) override;
gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
GdkPixbuf *get_pixbuf() override;
void abort() override;
gchar *get_format_name() override;
gchar **get_format_mime_types() override;
void set_page_num(gint page_num) override;
gint get_page_total() override;
private:
AreaUpdatedCb area_updated_cb;
SizePreparedCb size_prepared_cb;
AreaPreparedCb area_prepared_cb;
gpointer data;
GdkPixbuf *pixbuf;
guint requested_width;
guint requested_height;
gboolean aborted;
gint page_num;
gint page_total;
};
struct GqTiffContext
{
const guchar *buffer;
toff_t used;
toff_t pos;
};
void free_buffer (guchar *pixels, gpointer)
{
g_free (pixels);
}
tsize_t tiff_load_read (thandle_t handle, tdata_t buf, tsize_t size)
{
auto context = static_cast<GqTiffContext *>(handle);
if (context->pos + size > context->used)
return 0;
memcpy (buf, context->buffer + context->pos, size);
context->pos += size;
return size;
}
tsize_t tiff_load_write (thandle_t, tdata_t, tsize_t)
{
return -1;
}
toff_t tiff_load_seek (thandle_t handle, toff_t offset, int whence)
{
auto context = static_cast<GqTiffContext *>(handle);
switch (whence)
{
case SEEK_SET:
if (offset > context->used)
return -1;
context->pos = offset;
break;
case SEEK_CUR:
if (offset + context->pos >= context->used)
return -1;
context->pos += offset;
break;
case SEEK_END:
if (offset + context->used > context->used)
return -1;
context->pos = context->used + offset;
break;
default:
return -1;
}
return context->pos;
}
int tiff_load_close (thandle_t)
{
return 0;
}
toff_t tiff_load_size (thandle_t handle)
{
auto context = static_cast<GqTiffContext *>(handle);
return context->used;
}
int tiff_load_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
{
auto context = static_cast<GqTiffContext *>(handle);
*buf = const_cast<guchar *>(context->buffer);
*size = context->used;
return 0;
}
void tiff_load_unmap_file (thandle_t, tdata_t, toff_t)
{
}
gboolean ImageLoaderTiff::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
{
TIFF *tiff;
guchar *pixels = nullptr;
gint width;
gint height;
gint rowstride;
size_t bytes;
guint32 rowsperstrip;
gint dircount = 0;
TIFFSetWarningHandler(nullptr);
GqTiffContext context{buf, count, 0};
tiff = TIFFClientOpen ( "libtiff-geeqie", "r", &context,
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)
{
DEBUG_1("Failed to open TIFF image");
return FALSE;
}
do {
dircount++;
} while (TIFFReadDirectory(tiff));
page_total = dircount;
if (!TIFFSetDirectory(tiff, page_num))
{
DEBUG_1("Failed to open TIFF image");
TIFFClose(tiff);
return FALSE;
}
if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width))
{
DEBUG_1("Could not get image width (bad TIFF file)");
TIFFClose(tiff);
return FALSE;
}
if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height))
{
DEBUG_1("Could not get image height (bad TIFF file)");
TIFFClose(tiff);
return FALSE;
}
if (width <= 0 || height <= 0)
{
DEBUG_1("Width or height of TIFF image is zero");
TIFFClose(tiff);
return FALSE;
}
rowstride = width * 4;
if (rowstride / 4 != width)
{ /* overflow */
DEBUG_1("Dimensions of TIFF image too large: width %d", width);
TIFFClose(tiff);
return FALSE;
}
bytes = static_cast<size_t>(height) * rowstride;
if (bytes / rowstride != static_cast<size_t>(height))
{ /* overflow */
DEBUG_1("Dimensions of TIFF image too large: height %d", height);
TIFFClose(tiff);
return FALSE;
}
requested_width = width;
requested_height = height;
size_prepared_cb(nullptr, requested_width, requested_height, data);
pixels = static_cast<guchar *>(g_try_malloc (bytes));
if (!pixels)
{
DEBUG_1("Insufficient memory to open TIFF file: need %zu", bytes);
TIFFClose(tiff);
return FALSE;
}
pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
width, height, rowstride,
free_buffer, nullptr);
if (!pixbuf)
{
g_free (pixels);
DEBUG_1("Insufficient memory to open TIFF file");
TIFFClose(tiff);
return FALSE;
}
area_prepared_cb(nullptr, data);
if (TIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &rowsperstrip))
{
/* read by strip */
ptrdiff_t row;
const size_t line_bytes = width * sizeof(guint32);
g_autofree auto *wrk_line = static_cast<guchar *>(g_malloc(line_bytes));
for (row = 0; row < height; row += rowsperstrip)
{
int rows_to_write;
int i_row;
if (aborted) {
break;
}
/* Read the strip into an RGBA array */
if (!TIFFReadRGBAStrip(tiff, row, reinterpret_cast<guint32 *>(pixels + (row * rowstride)))) {
break;
}
/*
* Figure out the number of scanlines actually in this strip.
*/
if (row + static_cast<int>(rowsperstrip) > height)
rows_to_write = height - row;
else
rows_to_write = rowsperstrip;
/*
* For some reason the TIFFReadRGBAStrip() function chooses the
* lower left corner as the origin. Vertically mirror scanlines.
*/
for (i_row = 0; i_row < rows_to_write / 2; i_row++)
{
guchar *top_line;
guchar *bottom_line;
top_line = pixels + (row + i_row) * rowstride;
bottom_line = pixels + (row + rows_to_write - i_row - 1) * rowstride;
memcpy(wrk_line, top_line, line_bytes);
memcpy(top_line, bottom_line, line_bytes);
memcpy(bottom_line, wrk_line, line_bytes);
}
area_updated_cb(nullptr, 0, row, width, rows_to_write, data);
}
}
else
{
/* fallback, tiled tiff */
if (!TIFFReadRGBAImageOriented (tiff, width, height, reinterpret_cast<guint32 *>(pixels), ORIENTATION_TOPLEFT, 1))
{
TIFFClose(tiff);
return FALSE;
}
#if G_BYTE_ORDER == G_BIG_ENDIAN
/* Turns out that the packing used by TIFFRGBAImage depends on
* the host byte order...
*/
{
guchar *ptr = pixels;
while (ptr < pixels + bytes)
{
guint32 pixel = *(guint32 *)ptr;
int r = TIFFGetR(pixel);
int g = TIFFGetG(pixel);
int b = TIFFGetB(pixel);
int a = TIFFGetA(pixel);
*ptr++ = r;
*ptr++ = g;
*ptr++ = b;
*ptr++ = a;
}
}
#endif
area_updated_cb(nullptr, 0, 0, width, height, data);
}
TIFFClose(tiff);
chunk_size = count;
return TRUE;
}
void ImageLoaderTiff::init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data)
{
this->area_updated_cb = area_updated_cb;
this->size_prepared_cb = size_prepared_cb;
this->area_prepared_cb = area_prepared_cb;
this->data = data;
}
void ImageLoaderTiff::set_size(int width, int height)
{
requested_width = width;
requested_height = height;
}
GdkPixbuf *ImageLoaderTiff::get_pixbuf()
{
return pixbuf;
}
gchar *ImageLoaderTiff::get_format_name()
{
return g_strdup("tiff");
}
gchar **ImageLoaderTiff::get_format_mime_types()
{
static const gchar *mime[] = {"image/tiff", nullptr};
return g_strdupv(const_cast<gchar **>(mime));
}
void ImageLoaderTiff::abort()
{
aborted = TRUE;
}
ImageLoaderTiff::~ImageLoaderTiff()
{
if (pixbuf) g_object_unref(pixbuf);
}
void ImageLoaderTiff::set_page_num(gint page_num)
{
this->page_num = page_num;
}
gint ImageLoaderTiff::get_page_total()
{
return page_total;
}
} // namespace
std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_tiff()
{
return std::make_unique<ImageLoaderTiff>();
}
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */