1 /* VIPS function dispatch tables for image format load/save.
2  */
3 
4 /*
5 
6     This file is part of VIPS.
7 
8     VIPS is free software; you can redistribute it and/or modify
9     it under the terms of the GNU Lesser General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21     02110-1301  USA
22 
23  */
24 
25 /*
26 
27     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
28 
29  */
30 
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif /*HAVE_CONFIG_H*/
34 #include <vips/intl.h>
35 
36 #include <stdio.h>
37 
38 #include <vips/vips.h>
39 #include <vips/vips7compat.h>
40 #include <vips/internal.h>
41 
42 /* To iterate over supported formats, we build a temp list of subclasses of
43  * VipsFormat, sort by priority, iterate, and free.
44  */
45 
46 static void *
format_add_class(VipsFormatClass * format,GSList ** formats)47 format_add_class( VipsFormatClass *format, GSList **formats )
48 {
49 	if( !G_TYPE_IS_ABSTRACT( G_OBJECT_CLASS_TYPE( format ) ) )
50 		/* Append so we don't reverse the list of formats.
51 		 */
52 		*formats = g_slist_append( *formats, format );
53 
54 	return( NULL );
55 }
56 
57 static gint
format_compare(VipsFormatClass * a,VipsFormatClass * b)58 format_compare( VipsFormatClass *a, VipsFormatClass *b )
59 {
60         return( b->priority - a->priority );
61 }
62 
63 /**
64  * vips_format_map: (skip)
65  * @fn: function to apply to each #VipsFormatClass
66  * @a: user data
67  * @b: user data
68  *
69  * Apply a function to every %VipsFormatClass that VIPS knows about. Formats
70  * are presented to the function in priority order.
71  *
72  * Like all VIPS map functions, if @fn returns %NULL, iteration continues. If
73  * it returns non-%NULL, iteration terminates and that value is returned. The
74  * map function returns %NULL if all calls return %NULL.
75  *
76  * See also: im_slist_map().
77  *
78  * Returns: the result of iteration
79  */
80 void *
vips_format_map(VSListMap2Fn fn,void * a,void * b)81 vips_format_map( VSListMap2Fn fn, void *a, void *b )
82 {
83 	GSList *formats;
84 	void *result;
85 
86 	formats = NULL;
87 	(void) vips_class_map_all( g_type_from_name( "VipsFormat" ),
88 		(VipsClassMapFn) format_add_class, (void *) &formats );
89 
90 	formats = g_slist_sort( formats, (GCompareFunc) format_compare );
91 	result = im_slist_map2( formats, fn, a, b );
92 	g_slist_free( formats );
93 
94 	return( result );
95 }
96 
97 /* Abstract base class for image formats.
98  */
99 
100 G_DEFINE_ABSTRACT_TYPE( VipsFormat, vips_format, VIPS_TYPE_OBJECT );
101 
102 static void
vips_format_summary_class(VipsObjectClass * object_class,VipsBuf * buf)103 vips_format_summary_class( VipsObjectClass *object_class, VipsBuf *buf )
104 {
105 	VipsFormatClass *class = VIPS_FORMAT_CLASS( object_class );
106 
107 	VIPS_OBJECT_CLASS( vips_format_parent_class )->
108 		summary_class( object_class, buf );
109 	vips_buf_appends( buf, ", " );
110 
111 	if( class->suffs ) {
112 		const char **p;
113 
114 		vips_buf_appends( buf, "(" );
115 		for( p = class->suffs; *p; p++ ) {
116 			vips_buf_appendf( buf, "%s", *p );
117 			if( p[1] )
118 				vips_buf_appends( buf, ", " );
119 		}
120 		vips_buf_appends( buf, ") " );
121 	}
122 
123 	if( class->is_a )
124 		vips_buf_appends( buf, "is_a " );
125 	if( class->header )
126 		vips_buf_appends( buf, "header " );
127 	if( class->load )
128 		vips_buf_appends( buf, "load " );
129 	if( class->save )
130 		vips_buf_appends( buf, "save " );
131 	if( class->get_flags )
132 		vips_buf_appends( buf, "get_flags " );
133 }
134 
135 static void
vips_format_class_init(VipsFormatClass * class)136 vips_format_class_init( VipsFormatClass *class )
137 {
138 	VipsObjectClass *object_class = (VipsObjectClass *) class;
139 
140 	object_class->nickname = "format";
141 	object_class->description = _( "VIPS file formats" );
142 	object_class->summary_class = vips_format_summary_class;
143 
144 	/* Hide from UI.
145 	 */
146 	object_class->deprecated = TRUE;
147 }
148 
149 static void
vips_format_init(VipsFormat * object)150 vips_format_init( VipsFormat *object )
151 {
152 }
153 
154 /**
155  * vips_format_get_flags:
156  * @format: format to test
157  * @filename: file to test
158  *
159  * Get a set of flags for this file.
160  *
161  * Returns: flags for this format and file
162  */
163 VipsFormatFlags
vips_format_get_flags(VipsFormatClass * format,const char * filename)164 vips_format_get_flags( VipsFormatClass *format, const char *filename )
165 {
166 	return( format->get_flags ? format->get_flags( filename ) : 0 );
167 }
168 
169 /* VIPS format class.
170  */
171 
172 static const char *vips_suffs[] = { ".v", ".vips", NULL };
173 
174 int
im_isvips(const char * filename)175 im_isvips( const char *filename )
176 {
177 	unsigned char buf[4];
178 
179 	if( im__get_bytes( filename, buf, 4 ) == 4 ) {
180 		if( buf[0] == 0x08 && buf[1] == 0xf2 &&
181 			buf[2] == 0xa6 && buf[3] == 0xb6 )
182 			/* SPARC-order VIPS image.
183 			 */
184 			return( 1 );
185 		else if( buf[3] == 0x08 && buf[2] == 0xf2 &&
186 			buf[1] == 0xa6 && buf[0] == 0xb6 )
187 			/* INTEL-order VIPS image.
188 			 */
189 			return( 1 );
190 	}
191 
192 	return( 0 );
193 }
194 
195 static int
file2vips(const char * filename,IMAGE * out)196 file2vips( const char *filename, IMAGE *out )
197 {
198 	IMAGE *im;
199 
200 	if( !(im = im_open_local( out, filename, "r" )) ||
201 		im_copy( im, out ) )
202 		return( -1 );
203 
204 	return( 0 );
205 }
206 
207 static VipsFormatFlags
vips_flags(const char * filename)208 vips_flags( const char *filename )
209 {
210 	VipsFormatFlags flags;
211 	unsigned char buf[4];
212 
213 	flags = VIPS_FORMAT_PARTIAL;
214 
215 	if( im__get_bytes( filename, buf, 4 ) == 4 &&
216 		buf[0] == 0x08 &&
217 		buf[1] == 0xf2 &&
218 		buf[2] == 0xa6 &&
219 		buf[3] == 0xb6 )
220 		flags |= VIPS_FORMAT_BIGENDIAN;
221 
222 	return( flags );
223 }
224 
225 /* Vips format adds no new members.
226  */
227 typedef VipsFormat VipsFormatVips;
228 typedef VipsFormatClass VipsFormatVipsClass;
229 
230 static int
vips_format_vips_save(VipsImage * image,const char * filename)231 vips_format_vips_save( VipsImage *image, const char *filename )
232 {
233 	return( vips_image_write_to_file( image, filename, NULL ) );
234 }
235 
236 static void
vips_format_vips_class_init(VipsFormatVipsClass * class)237 vips_format_vips_class_init( VipsFormatVipsClass *class )
238 {
239 	VipsObjectClass *object_class = (VipsObjectClass *) class;
240 	VipsFormatClass *format_class = (VipsFormatClass *) class;
241 
242 	object_class->nickname = "vips";
243 	object_class->description = _( "VIPS" );
244 
245 	format_class->priority = 200;
246 	format_class->is_a = im_isvips;
247 	format_class->header = file2vips;
248 	format_class->load = file2vips;
249 	format_class->save = vips_format_vips_save;
250 	format_class->get_flags = vips_flags;
251 	format_class->suffs = vips_suffs;
252 }
253 
254 static void
vips_format_vips_init(VipsFormatVips * object)255 vips_format_vips_init( VipsFormatVips *object )
256 {
257 }
258 
259 G_DEFINE_TYPE( VipsFormatVips, vips_format_vips, VIPS_TYPE_FORMAT );
260 
261 /* Called on startup: register the base vips formats.
262  */
263 void
im__format_init(void)264 im__format_init( void )
265 {
266 	extern GType vips_format_csv_get_type( void );
267 	extern GType vips_format_ppm_get_type( void );
268 	extern GType vips_format_analyze_get_type( void );
269 	extern GType vips_format_rad_get_type( void );
270 
271 	vips_format_vips_get_type();
272 #ifdef HAVE_JPEG
273 	extern GType vips_format_jpeg_get_type( void );
274 	vips_format_jpeg_get_type();
275 #endif /*HAVE_JPEG*/
276 #ifdef HAVE_PNG
277 	extern GType vips_format_png_get_type( void );
278 	vips_format_png_get_type();
279 #endif /*HAVE_PNG*/
280 #ifdef HAVE_LIBWEBP
281 	extern GType vips_format_webp_get_type( void );
282 	vips_format_webp_get_type();
283 #endif /*HAVE_LIBWEBP*/
284 	vips_format_csv_get_type();
285 	vips_format_ppm_get_type();
286 	vips_format_analyze_get_type();
287 #ifdef HAVE_OPENEXR
288 	extern GType vips_format_exr_get_type( void );
289 	vips_format_exr_get_type();
290 #endif /*HAVE_OPENEXR*/
291 #ifdef HAVE_MATIO
292 	extern GType vips_format_mat_get_type( void );
293 	vips_format_mat_get_type();
294 #endif /*HAVE_MATIO*/
295 #ifdef HAVE_CFITSIO
296 	extern GType vips_format_fits_get_type( void );
297 	vips_format_fits_get_type();
298 #endif /*HAVE_CFITSIO*/
299 	vips_format_rad_get_type();
300 #ifdef HAVE_MAGICK
301 	extern GType vips_format_magick_get_type( void );
302 	vips_format_magick_get_type();
303 #endif /*HAVE_MAGICK*/
304 #ifdef HAVE_TIFF
305 	extern GType vips_format_tiff_get_type( void );
306 	vips_format_tiff_get_type();
307 #endif /*HAVE_TIFF*/
308 	extern GType vips_format_openslide_get_type( void );
309 	vips_format_openslide_get_type();
310 	extern GType vips_format_nifti_get_type( void );
311 	vips_format_nifti_get_type();
312 }
313 
314 /* Can this format open this file?
315  */
316 static void *
format_for_file_sub(VipsFormatClass * format,const char * name,const char * filename)317 format_for_file_sub( VipsFormatClass *format,
318 	const char *name, const char *filename )
319 {
320 	if( format->is_a ) {
321 		if( format->is_a( filename ) )
322 			return( format );
323 	}
324 	else if( im_filename_suffix_match( filename, format->suffs ) )
325 		return( format );
326 
327 	return( NULL );
328 }
329 
330 /**
331  * vips_format_for_file:
332  * @filename: file to find a format for
333  *
334  * Searches for a format you could use to load a file.
335  *
336  * See also: vips_format_read(), vips_format_for_name().
337  *
338  * Returns: a format on success, %NULL on error
339  */
340 VipsFormatClass *
vips_format_for_file(const char * filename)341 vips_format_for_file( const char *filename )
342 {
343 	char name[FILENAME_MAX];
344 	char options[FILENAME_MAX];
345 	VipsFormatClass *format;
346 
347 	/* Break any options off the name ... eg. "fred.tif:jpeg,tile"
348 	 * etc.
349 	 */
350 	im_filename_split( filename, name, options );
351 
352 	if( !im_existsf( "%s", name ) ) {
353 		im_error( "VipsFormat", _( "file \"%s\" not found" ), name );
354 		return( NULL );
355 	}
356 
357 	if( !(format = (VipsFormatClass *) vips_format_map(
358 		(VSListMap2Fn) format_for_file_sub,
359 		(void *) filename, (void *) name )) ) {
360 		im_error( "VipsFormat",
361 			_( "file \"%s\" not a known format" ), name );
362 		return( NULL );
363 	}
364 
365 	return( format );
366 }
367 
368 /* Can we write this filename with this format? Ignore formats without a save
369  * method.
370  */
371 static void *
format_for_name_sub(VipsFormatClass * format,const char * name)372 format_for_name_sub( VipsFormatClass *format, const char *name )
373 {
374 	if( format->save &&
375 		im_filename_suffix_match( name, format->suffs ) )
376 		return( format );
377 
378 	return( NULL );
379 }
380 
381 /**
382  * vips_format_for_name:
383  * @filename: name to find a format for
384  *
385  * Searches for a format you could use to save a file.
386  *
387  * See also: vips_format_write(), vips_format_for_file().
388  *
389  * Returns: a format on success, %NULL on error
390  */
391 VipsFormatClass *
vips_format_for_name(const char * filename)392 vips_format_for_name( const char *filename )
393 {
394 	VipsFormatClass *format;
395 
396 	if( !(format = (VipsFormatClass *) vips_format_map(
397 		(VSListMap2Fn) format_for_name_sub,
398 		(void *) filename, NULL )) ) {
399 		im_error( "VipsFormat",
400 			_( "\"%s\" is not a supported image format." ),
401 			filename );
402 
403 		return( NULL );
404 	}
405 
406 	return( format );
407 }
408 
409 /**
410  * vips_format_read:
411  * @filename: file to load
412  * @out: write the file to this image
413  *
414  * Searches for a format for this file, then loads the file into @out.
415  *
416  * See also: vips_format_write().
417  *
418  * Returns: 0 on success, -1 on error
419  */
420 int
vips_format_read(const char * filename,IMAGE * out)421 vips_format_read( const char *filename, IMAGE *out )
422 {
423 	VipsFormatClass *format;
424 
425 	if( !(format = vips_format_for_file( filename )) ||
426 		format->load( filename, out ) )
427 		return( -1 );
428 
429 	return( 0 );
430 }
431 
432 /**
433  * vips_format_write:
434  * @in: image to write
435  * @filename: file to write to
436  *
437  * Searches for a format for this name, then saves @im to it.
438  *
439  * See also: vips_format_read().
440  *
441  * Returns: 0 on success, -1 on error
442  */
443 int
vips_format_write(IMAGE * in,const char * filename)444 vips_format_write( IMAGE *in, const char *filename )
445 {
446 	VipsFormatClass *format;
447 
448 	if( !(format = vips_format_for_name( filename )) ||
449 		format->save( in, filename ) )
450 		return( -1 );
451 
452 	return( 0 );
453 }
454