File: openexr2vips.c

package info (click to toggle)
vips 8.14.1-3%2Bdeb12u2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 35,912 kB
  • sloc: ansic: 165,449; cpp: 10,987; python: 4,462; xml: 4,212; sh: 471; perl: 40; makefile: 23
file content (451 lines) | stat: -rw-r--r-- 9,774 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* Convert OpenEXR to VIPS
 *
 * 1/5/06
 * 	- from im_png2vips.c
 * 17/5/06
 * 	- oops, buffer calcs were wrong
 * 19/5/06
 * 	- added tiled read, with a separate cache
 * 	- removed *255 we had before, better to do something clever with
 * 	  chromaticities
 * 4/2/10
 * 	- gtkdoc
 * 12/12/11
 * 	- redo as a set of fns ready for wrapping in a new-style class
 * 17/9/16
 * 	- tag output as scRGB
 * 16/8/18
 * 	- shut down the input file as soon as we can [kleisauke]
 * 19/8/18
 * 	- scale alpha up to 0 - 255 to match the rest of libvips
 */

/*

  TODO

	- colour management
	- attributes 
	- more of OpenEXR's pixel formats 
	- more than just RGBA channels
	- turn alpha to vips 0 - 255 from exr 0 - 1

	the openexr C API is very limited ... it seems RGBA half pixels is 
	all you can do

	openexr lets you have different formats in different channels :-(

	there's no API to read the "chromaticities" attribute :-(

	best redo with the C++ API now we support C++ operations

 */

/*

    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 <glib/gi18n-lib.h>

#ifdef HAVE_OPENEXR

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

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

#include <ImfCRgbaFile.h>

#include "pforeign.h"

/* What we track during a OpenEXR read.
 */
typedef struct {
	char *filename;
	VipsImage *out;

	ImfTiledInputFile *tiles;
	ImfInputFile *lines;
	const ImfHeader *header;
	VipsRect window;
	int tile_width;
	int tile_height;
} Read;

gboolean
vips__openexr_isexr( const char *filename )
{
	unsigned char buf[4];

	if( vips__get_bytes( filename, buf, 4 ) == 4 )
		if( buf[0] == 0x76 && buf[1] == 0x2f &&
			buf[2] == 0x31 && buf[3] == 0x01 )
			return( TRUE );

	return( FALSE );
}

static void
get_imf_error( void )
{
	vips_error( "exr2vips", _( "EXR error: %s" ), ImfErrorMessage() );
}

static void
read_close( Read *read )
{
	VIPS_FREEF( ImfCloseTiledInputFile, read->tiles );
	VIPS_FREEF( ImfCloseInputFile, read->lines );
}

static void
read_destroy( VipsImage *out, Read *read )
{
	VIPS_FREE( read->filename );

	read_close( read ); 

	g_free( read );
}

static Read *
read_new( const char *filename, VipsImage *out )
{
	Read *read;
	int xmin, ymin;
	int xmax, ymax;

	if( !(read = VIPS_NEW( NULL, Read )) )
		return( NULL );
	read->filename = vips_strdup( NULL, filename );
	read->out = out;
	read->tiles = NULL;
	read->lines = NULL;
	if( out ) 
		g_signal_connect( out, "close", 
			G_CALLBACK( read_destroy ), read ); 

	/* Try to open tiled first ... if that fails, fall back to scanlines.

	   	FIXME ... seems a bit ugly, but how else can you spot a tiled 
		EXR image?

	 */
	if( !(read->tiles = ImfOpenTiledInputFile( read->filename )) ) {
		if( !(read->lines = ImfOpenInputFile( read->filename )) ) {
			get_imf_error();
			return( NULL );
		}
	}

#ifdef DEBUG
	if( read->tiles )
		printf( "exr2vips: opening in tiled mode\n" );
	else
		printf( "exr2vips: opening in scanline mode\n" );
#endif /*DEBUG*/

	if( read->tiles ) {
		read->header = ImfTiledInputHeader( read->tiles );
		read->tile_width = ImfTiledInputTileXSize( read->tiles );
		read->tile_height = ImfTiledInputTileYSize( read->tiles );
	}
	else
		read->header = ImfInputHeader( read->lines );

	ImfHeaderDataWindow( read->header, &xmin, &ymin, &xmax, &ymax );
	read->window.left = xmin;
	read->window.top = ymin;
	read->window.width = xmax - xmin + 1;
	read->window.height = ymax - ymin + 1;

	return( read );
}

gboolean
vips__openexr_istiled( const char *filename )
{
	Read *read;
	gboolean tiled;

	if( !(read = read_new( filename, NULL )) )
		return( FALSE );
	tiled = read->tiles != NULL;
	read_destroy( NULL, read );

	return( tiled );
}

/* Read a OpenEXR file (header) into a VIPS (header).
 */
static void
read_header( Read *read, VipsImage *out )
{
	VipsDemandStyle hint;

	/* 

	   FIXME ... not really scRGB, you should get the chromaticities 
	   from the header and transform 

	 */
	vips_image_init_fields( out,
		 read->window.width, read->window.height, 4,
		 VIPS_FORMAT_FLOAT,
		 VIPS_CODING_NONE, VIPS_INTERPRETATION_scRGB, 1.0, 1.0 );

	if( read->tiles )
		/* Even though this is a tiled reader, we hint thinstrip 
		 * since with the cache we are quite happy serving that if 
		 * anything downstream would like it.
		 */
		hint = VIPS_DEMAND_STYLE_THINSTRIP;
	else
		hint = VIPS_DEMAND_STYLE_FATSTRIP;
	(void) vips_image_pipelinev( out, hint, NULL );
}

int
vips__openexr_read_header( const char *filename, VipsImage *out )
{
	Read *read;

	if( !(read = read_new( filename, out )) )
		return( -1 );
	read_header( read, out );
	read_close( read );

	return( 0 );
}

/* Allocate a tile buffer.
 */
static void *
vips__openexr_start( VipsImage *out, void *a, void *b )
{
	Read *read = (Read *) a;
	ImfRgba *imf_buffer;

	if( !(imf_buffer = VIPS_ARRAY( out,
		read->tile_width * read->tile_height, ImfRgba )) )
		return( NULL );

	return( imf_buffer );
}

static int
vips__openexr_generate( VipsRegion *out, 
	void *seq, void *a, void *b, gboolean *top )
{
	ImfRgba *imf_buffer = (ImfRgba *) seq;
	Read *read = (Read *) a;
	VipsRect *r = &out->valid;

	const int tw = read->tile_width;
	const int th = read->tile_height;

	/* Find top left of tiles we need.
	 */
	const int xs = (r->left / tw) * tw;
	const int ys = (r->top / th) * th;

	int x, y, z;
	VipsRect image;

	/* Area of image.
	 */
	image.left = 0;
	image.top = 0;
	image.width = read->out->Xsize;
	image.height = read->out->Ysize;

	for( y = ys; y < VIPS_RECT_BOTTOM( r ); y += th )
		for( x = xs; x < VIPS_RECT_RIGHT( r ); x += tw ) {
			VipsRect tile;
			VipsRect hit;
			int result;

			if( !ImfTiledInputSetFrameBuffer( read->tiles,
				imf_buffer - 
					(read->window.left + x) -
					(read->window.top + y) * tw,
				1, tw ) ) {
				vips_foreign_load_invalidate( read->out );
				get_imf_error();
				return( -1 );
			}

#ifdef DEBUG
			printf( "exr2vips: requesting tile %d x %d\n", 
				x / tw, y / th );
#endif /*DEBUG*/

			result = ImfTiledInputReadTile( read->tiles, 
				x / tw, y / th, 0, 0 );

			if( !result ) {
				get_imf_error();
				return( -1 );
			}

			/* The tile in the file, in VIPS coordinates.
			 */
			tile.left = x;
			tile.top = y;
			tile.width = tw;
			tile.height = th;
			vips_rect_intersectrect( &tile, &image, &tile );

			/* The part of this tile that hits the region.
			 */
			vips_rect_intersectrect( &tile, r, &hit );

			/* Convert to float and write to the region.
			 */
			for( z = 0; z < hit.height; z++ ) {
				ImfRgba *p = imf_buffer + 
					(hit.left - tile.left) +
					(hit.top - tile.top + z) * tw;
				float *q = (float *) VIPS_REGION_ADDR( out,
					hit.left, hit.top + z );

				int i;

				ImfHalfToFloatArray( 4 * hit.width, 
					(ImfHalf *) p, q );

				/* oexr uses 0 - 1 for alpha, but vips is 
				 * always 0 - 255, even for scrgb images.
				 */
				for( i = 0; i < hit.width; i++ ) 
					q[4 * i + 3] *= 255;
			}
		}

	return( 0 );
}

int
vips__openexr_read( const char *filename, VipsImage *out )
{
	Read *read;

	if( !(read = read_new( filename, out )) )
		return( -1 );

	if( read->tiles ) {
		VipsImage *raw;
		VipsImage *t;

		/* Tile cache: keep enough for two complete rows of tiles,
		 * plus 50%.
		 */
		raw = vips_image_new();
		vips_object_local( out, raw );

		read_header( read, raw );

		if( vips_image_generate( raw, 
			vips__openexr_start, vips__openexr_generate, NULL, 
			read, NULL ) )
			return( -1 );

		if( vips_tilecache( raw, &t, 
			"tile_width", read->tile_width, 
			"tile_height", read->tile_height,
			"max_tiles", (int) 
				(2.5 * (1 + raw->Xsize / read->tile_width)),
			NULL ) ) 
			return( -1 );
		if( vips_image_write( t, out ) ) {
			g_object_unref( t );
			return( -1 );
		}
		g_object_unref( t );
	}
	else {
		const int left = read->window.left;
		const int top = read->window.top;
		const int width = read->window.width;
		const int height = read->window.height;

		ImfRgba *imf_buffer;
		float *vips_buffer;
		int y;

		if( !(imf_buffer = VIPS_ARRAY( out, width, ImfRgba )) ||
			!(vips_buffer = VIPS_ARRAY( out, 4 * width, float )) )
			return( -1 );

		read_header( read, out );

		for( y = 0; y < height; y++ ) {
			int i;

			if( !ImfInputSetFrameBuffer( read->lines,
				imf_buffer - left - (top + y) * width,
				1, width ) ) {
				get_imf_error();
				return( -1 );
			}
			if( !ImfInputReadPixels( read->lines, 
				top + y, top + y ) ) {
				get_imf_error();
				return( -1 );
			}

			ImfHalfToFloatArray( 4 * width, 
				(ImfHalf *) imf_buffer, vips_buffer );

			/* oexr uses 0 - 1 for alpha, but vips is always 0 -
			 * 255, even for scrgb images.
			 */
			for( i = 0; i < width; i++ ) 
				vips_buffer[4 * i + 3] *= 255;

			if( vips_image_write_line( out, y, 
				(VipsPel *) vips_buffer ) )
				return( -1 );
		}

		read_close( read ); 
	}

	return( 0 );
}

#endif /*HAVE_OPENEXR*/