File: tiff.c

package info (click to toggle)
vips 8.10.5-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 40,952 kB
  • sloc: ansic: 201,201; cpp: 9,766; sh: 5,031; xml: 4,191; python: 3,869; makefile: 1,033; perl: 40
file content (383 lines) | stat: -rw-r--r-- 7,810 bytes parent folder | download
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
/* Some shared TIFF utilities.
 *
 * 14/10/16
 * 	- from vips2tiff.c
 *
 * 26/8/17
 * 	- add openout_read, to help tiffsave_buffer for pyramids
 */

/*

    This file is part of VIPS.

    VIPS is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser 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

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

/*
#define DEBUG
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>

#ifdef HAVE_TIFF

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /*HAVE_UNISTD_H*/
#include <string.h>

#include <vips/vips.h>
#include <vips/internal.h>

#include <tiffio.h>

#include "tiff.h"

/* Handle TIFF errors here. Shared with vips2tiff.c. These can be called from
 * more than one thread.
 */
static void
vips__thandler_error( const char *module, const char *fmt, va_list ap )
{
	vips_verror( module, fmt, ap );
}

/* It'd be nice to be able to support the @fail option for the tiff loader, but
 * there's no easy way to do this, since libtiff has a global warning handler.
 */
static void
vips__thandler_warning( const char *module, const char *fmt, va_list ap )
{
	g_logv( G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, fmt, ap );
}

/* Called during library init.
 *
 * libtiff error and warning handlers may be called from other threads 
 * running in other libs. Other libs may install error handlers and capture 
 * messages caused by us.
 */
void
vips__tiff_init( void )
{
	TIFFSetErrorHandler( vips__thandler_error );
	TIFFSetWarningHandler( vips__thandler_warning );
}

/* Open TIFF for output.
 */
TIFF *
vips__tiff_openout( const char *path, gboolean bigtiff )
{
	TIFF *tif;
	const char *mode = bigtiff ? "w8" : "w";

#ifdef DEBUG
	printf( "vips__tiff_openout( \"%s\", \"%s\" )\n", path, mode );
#endif /*DEBUG*/

	/* Need the utf-16 version on Windows.
	 */
#ifdef OS_WIN32
{
	GError *error = NULL;
	wchar_t *path16;

	if( !(path16 = (wchar_t *)
		g_utf8_to_utf16( path, -1, NULL, NULL, &error )) ) {
		vips_g_error( &error );
		return( NULL );
	}

	tif = TIFFOpenW( path16, mode );

	g_free( path16 );
}
#else /*!OS_WIN32*/
	tif = TIFFOpen( path, mode );
#endif /*OS_WIN32*/

	if( !tif ) {
		vips_error( "tiff",
			_( "unable to open \"%s\" for output" ), path );
		return( NULL );
	}

	return( tif );
}

/* TIFF input from a vips source.
 */

static tsize_t
openin_source_read( thandle_t st, tdata_t data, tsize_t size )
{
	VipsSource *source = VIPS_SOURCE( st );

	return( vips_source_read( source, data, size ) );
}

static tsize_t
openin_source_write( thandle_t st, tdata_t buffer, tsize_t size )
{
	g_assert_not_reached();

	return( 0 );
}

static toff_t
openin_source_seek( thandle_t st, toff_t position, int whence )
{
	VipsSource *source = VIPS_SOURCE( st );

	/* toff_t is usually uint64, with -1 cast to uint64 to indicate error.
	 */
	return( (toff_t) vips_source_seek( source, position, whence ) );
}

static int
openin_source_close( thandle_t st )
{
	VipsSource *source = VIPS_SOURCE( st );

	VIPS_UNREF( source );

	return( 0 );
}

static toff_t
openin_source_length( thandle_t st )
{
	VipsSource *source = VIPS_SOURCE( st );

	/* libtiff will use this to get file size if tags like StripByteCounts
	 * are missing.
	 *
	 * toff_t is usually uint64, with -1 cast to uint64 to indicate error.
	 */
	return( (toff_t) vips_source_length( source ) );
}

static int
openin_source_map( thandle_t st, tdata_t *start, toff_t *len )
{
	g_assert_not_reached();

	return( 0 );
}

static void
openin_source_unmap( thandle_t st, tdata_t start, toff_t len )
{
	g_assert_not_reached();

	return;
}

TIFF *
vips__tiff_openin_source( VipsSource *source )
{
	TIFF *tiff;

#ifdef DEBUG
	printf( "vips__tiff_openin_source:\n" );
#endif /*DEBUG*/

	if( vips_source_rewind( source ) )
		return( NULL );

	if( !(tiff = TIFFClientOpen( "source input", "rm",
		(thandle_t) source,
		openin_source_read,
		openin_source_write,
		openin_source_seek,
		openin_source_close,
		openin_source_length,
		openin_source_map,
		openin_source_unmap )) ) {
		vips_error( "vips__tiff_openin_source", "%s",
			_( "unable to open source for input" ) );
		return( NULL );
	}

	/* Unreffed on close(), see above.
	 */
	g_object_ref( source );

	return( tiff );
}

/* TIFF output to a memory buffer.
 */

typedef struct _VipsTiffOpenoutBuffer {
	VipsDbuf dbuf;

	/* On close, consolidate and write the output here.
	 */
	void **out_data;
	size_t *out_length;
} VipsTiffOpenoutBuffer;

static tsize_t
openout_buffer_read( thandle_t st, tdata_t data, tsize_t size )
{
	VipsTiffOpenoutBuffer *buffer = (VipsTiffOpenoutBuffer *) st;

#ifdef DEBUG
	printf( "openout_buffer_read: %zd bytes\n", size );
#endif /*DEBUG*/

	return( vips_dbuf_read( &buffer->dbuf, data, size ) );
}

static tsize_t
openout_buffer_write( thandle_t st, tdata_t data, tsize_t size )
{
	VipsTiffOpenoutBuffer *buffer = (VipsTiffOpenoutBuffer *) st;

#ifdef DEBUG
	printf( "openout_buffer_write: %zd bytes\n", size );
#endif /*DEBUG*/

	vips_dbuf_write( &buffer->dbuf, data, size );

	return( size );
}

static int
openout_buffer_close( thandle_t st )
{
	VipsTiffOpenoutBuffer *buffer = (VipsTiffOpenoutBuffer *) st;

	*(buffer->out_data) = vips_dbuf_steal( &buffer->dbuf,
		buffer->out_length);

	return( 0 );
}

static toff_t
openout_buffer_seek( thandle_t st, toff_t position, int whence )
{
	VipsTiffOpenoutBuffer *buffer = (VipsTiffOpenoutBuffer *) st;

#ifdef DEBUG
	printf( "openout_buffer_seek: position %zd, whence %d ",
		position, whence );
	switch( whence ) {
	case SEEK_SET:
		printf( "set" ); 
		break;

	case SEEK_END:
		printf( "end" ); 
		break;

	case SEEK_CUR:
		printf( "cur" ); 
		break;

	default:
		printf( "unknown" ); 
		break;
	}
	printf( "\n" ); 
#endif /*DEBUG*/

	vips_dbuf_seek( &buffer->dbuf, position, whence );

	return( vips_dbuf_tell( &buffer->dbuf ) );
}

static toff_t
openout_buffer_length( thandle_t st )
{
	g_assert_not_reached();

	return( 0 );
}

static int
openout_buffer_map( thandle_t st, tdata_t *start, toff_t *len )
{
	g_assert_not_reached();

	return( 0 );
}

static void
openout_buffer_unmap( thandle_t st, tdata_t start, toff_t len )
{
	g_assert_not_reached();

	return;
}

/* On TIFFClose(), @data and @length are set to point to the output buffer.
 */
TIFF *
vips__tiff_openout_buffer( VipsImage *image,
	gboolean bigtiff, void **out_data, size_t *out_length )
{
	const char *mode = bigtiff ? "w8" : "w";

	VipsTiffOpenoutBuffer *buffer;
	TIFF *tiff;

#ifdef DEBUG
	printf( "vips__tiff_openout_buffer:\n" );
#endif /*DEBUG*/

	g_assert( out_data );
	g_assert( out_length );

	buffer = VIPS_NEW( image, VipsTiffOpenoutBuffer );
	vips_dbuf_init( &buffer->dbuf );
	buffer->out_data = out_data;
	buffer->out_length = out_length;

	if( !(tiff = TIFFClientOpen( "memory output", mode,
		(thandle_t) buffer,
		openout_buffer_read,
		openout_buffer_write,
		openout_buffer_seek,
		openout_buffer_close,
		openout_buffer_length,
		openout_buffer_map,
		openout_buffer_unmap )) ) {
		vips_error( "vips__tiff_openout_buffer", "%s",
			_( "unable to open memory buffer for output" ) );
		return( NULL );
	}

	return( tiff );
}

#endif /*HAVE_TIFF*/