2016-07-30 11:51:54 +02:00
|
|
|
/* Read and write a vips file.
|
2009-08-16 17:00:08 +02:00
|
|
|
*
|
|
|
|
* 22/5/08
|
|
|
|
* - from im_open.c, im_openin.c, im_desc_hd.c, im_readhist.c,
|
|
|
|
* im_openout.c
|
|
|
|
* 19/3/09
|
|
|
|
* - block mmaps of nodata images
|
|
|
|
* 12/5/09
|
|
|
|
* - fix signed/unsigned warnings
|
2009-10-12 18:16:25 +02:00
|
|
|
* 12/10/09
|
|
|
|
* - heh argh reading history always stopped after the first line
|
2009-12-10 11:58:10 +01:00
|
|
|
* 9/12/09
|
|
|
|
* - only wholly map input files on im_incheck() ... this reduces VM use,
|
|
|
|
* especially with large numbers of small files
|
2011-02-14 19:23:01 +01:00
|
|
|
* 14/2/11
|
|
|
|
* - renamed to vips.c from im_open_vips.c, some stuff chopped out for
|
|
|
|
* image.c ... this file now just does read / write to disc
|
2011-03-28 15:57:43 +02:00
|
|
|
* 28/3/11
|
|
|
|
* - moved to vips_ namespace
|
2017-02-25 14:07:43 +01:00
|
|
|
* 25/2/17
|
2017-02-25 18:28:48 +01:00
|
|
|
* - use expat for xml read, printf for xml write
|
2017-08-16 19:36:25 +02:00
|
|
|
* 16/8/17
|
|
|
|
* - validate strs as being utf-8 before we write
|
2018-04-09 14:27:57 +02:00
|
|
|
* 9/4/18 Alexander--
|
|
|
|
* - use O_TMPFILE, if available
|
2018-07-23 15:55:37 +02:00
|
|
|
* 23/7/18
|
|
|
|
* - escape ASCII control characters in XML
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
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
|
2013-03-07 06:40:19 +01:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
02110-1301 USA
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-12-01 19:03:41 +01:00
|
|
|
#define SHOW_HEADER
|
2017-02-26 18:37:46 +01:00
|
|
|
#define DEBUG
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-09 14:27:57 +02:00
|
|
|
/* Enable linux extensions like O_TMPFILE, if available.
|
|
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
#include <vips/intl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2018-03-30 12:59:57 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2009-08-16 17:00:08 +02:00
|
|
|
#ifdef HAVE_SYS_FILE_H
|
|
|
|
#include <sys/file.h>
|
|
|
|
#endif /*HAVE_SYS_FILE_H*/
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif /*HAVE_UNISTD_H*/
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif /*HAVE_IO_H*/
|
2017-02-24 15:57:20 +01:00
|
|
|
#include <expat.h>
|
2009-08-16 17:00:08 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef OS_WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif /*OS_WIN32*/
|
|
|
|
|
|
|
|
#include <vips/vips.h>
|
|
|
|
#include <vips/internal.h>
|
|
|
|
#include <vips/debug.h>
|
|
|
|
|
2014-08-09 11:25:34 +02:00
|
|
|
/**
|
|
|
|
* SECTION: vips
|
|
|
|
* @short_description: startup, shutdown, version
|
|
|
|
* @stability: Stable
|
2014-08-19 17:57:37 +02:00
|
|
|
* @see_also: <link linkend="VipsOperation">VipsOperation</link>
|
2014-08-09 11:25:34 +02:00
|
|
|
* @include: vips/vips.h
|
|
|
|
*
|
|
|
|
* Start VIPS up, shut VIPS down, get version information, relocation.
|
|
|
|
*
|
|
|
|
* VIPS is a relocatable package, meaning you can move the directory tree you
|
|
|
|
* compiled it to at runtime and it will still be able to find all data files.
|
|
|
|
* This is required for OS X and Windows, but slightly unusual in the Unix
|
|
|
|
* world. See vips_init() and vips_guess_prefix().
|
|
|
|
*/
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
/* Try to make an O_BINARY ... sometimes need the leading '_'.
|
|
|
|
*/
|
|
|
|
#ifdef BINARY_OPEN
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#ifdef _O_BINARY
|
|
|
|
#define O_BINARY _O_BINARY
|
|
|
|
#endif /*_O_BINARY*/
|
|
|
|
#endif /*!O_BINARY*/
|
|
|
|
#endif /*BINARY_OPEN*/
|
|
|
|
|
2011-02-14 19:23:01 +01:00
|
|
|
/* If we have O_BINARY, add it to a mode flags set.
|
|
|
|
*/
|
|
|
|
#ifdef O_BINARY
|
|
|
|
#define BINARYIZE(M) ((M) | O_BINARY)
|
|
|
|
#else /*!O_BINARY*/
|
|
|
|
#define BINARYIZE(M) (M)
|
|
|
|
#endif /*O_BINARY*/
|
|
|
|
|
|
|
|
/* Open mode for image write ... on some systems, have to set BINARY too.
|
2012-07-05 15:40:18 +02:00
|
|
|
*
|
|
|
|
* We use O_RDWR not O_WRONLY since after writing we may want to rewind the
|
|
|
|
* image and read from it.
|
2011-02-14 19:23:01 +01:00
|
|
|
*/
|
2012-07-05 15:40:18 +02:00
|
|
|
#define MODE_WRITE BINARYIZE (O_RDWR | O_CREAT | O_TRUNC)
|
2011-02-14 19:23:01 +01:00
|
|
|
|
|
|
|
/* Mode for read/write. This is if we might later want to mmaprw () the file.
|
|
|
|
*/
|
|
|
|
#define MODE_READWRITE BINARYIZE (O_RDWR)
|
|
|
|
|
|
|
|
/* Mode for read only. This is the fallback if READWRITE fails.
|
|
|
|
*/
|
|
|
|
#define MODE_READONLY BINARYIZE (O_RDONLY)
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
/* Our XML namespace.
|
|
|
|
*/
|
2017-02-25 19:10:42 +01:00
|
|
|
#define NAMESPACE_URI "http://www.vips.ecs.soton.ac.uk/"
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2011-03-28 15:57:43 +02:00
|
|
|
/* Open for read for image files.
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
2011-03-23 19:28:48 +01:00
|
|
|
int
|
|
|
|
vips__open_image_read( const char *filename )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2011-03-28 15:57:43 +02:00
|
|
|
/* Try to open read-write, so that calls to vips_image_inplace() will
|
|
|
|
* work. When we later mmap this file, we set read-only, so there
|
2009-08-16 17:00:08 +02:00
|
|
|
* is little danger of scrubbing over files we own.
|
|
|
|
*/
|
2011-11-02 15:51:39 +01:00
|
|
|
fd = vips_tracked_open( filename, MODE_READWRITE );
|
|
|
|
if( fd == -1 )
|
2009-08-16 17:00:08 +02:00
|
|
|
/* Open read-write failed. Fall back to open read-only.
|
|
|
|
*/
|
2011-11-02 15:51:39 +01:00
|
|
|
fd = vips_tracked_open( filename, MODE_READONLY );
|
|
|
|
|
|
|
|
if( fd == -1 ) {
|
|
|
|
vips_error_system( errno, "VipsImage",
|
|
|
|
_( "unable to open \"%s\"" ), filename );
|
|
|
|
return( -1 );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( fd );
|
|
|
|
}
|
|
|
|
|
2011-12-15 11:59:05 +01:00
|
|
|
/* Open for write for image files.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips__open_image_write( const char *filename, gboolean temp )
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
int fd;
|
|
|
|
|
2018-11-04 20:51:36 +01:00
|
|
|
fd = -1;
|
2011-12-15 11:59:05 +01:00
|
|
|
|
2018-11-21 15:26:52 +01:00
|
|
|
#ifndef O_TMPFILE
|
|
|
|
if( temp )
|
|
|
|
g_info( "vips__open_image_write: O_TMPFILE not available" );
|
|
|
|
#endif /*!O_TMPFILE*/
|
|
|
|
|
2018-04-09 14:27:57 +02:00
|
|
|
#ifdef O_TMPFILE
|
|
|
|
/* Linux-only extension creates an unlinked file. CREAT and TRUNC must
|
|
|
|
* be clear. The filename arg to open() must name a directory.
|
2018-11-05 11:38:15 +01:00
|
|
|
*
|
|
|
|
* This can fail since not all filesystems support it. In this case,
|
|
|
|
* we open as a regular file and rely on the delete-on-close
|
|
|
|
* mechanism, see vips_image_delete().
|
2018-04-09 14:27:57 +02:00
|
|
|
*/
|
|
|
|
if( temp ) {
|
|
|
|
char *dirname;
|
|
|
|
|
2018-11-21 15:26:52 +01:00
|
|
|
g_info( "vips__open_image_write: opening with O_TMPFILE" );
|
2018-04-09 14:27:57 +02:00
|
|
|
dirname = g_path_get_dirname( filename );
|
2018-11-04 20:51:36 +01:00
|
|
|
fd = vips_tracked_open( dirname, O_TMPFILE | O_RDWR , 0666 );
|
2018-04-09 14:27:57 +02:00
|
|
|
g_free( dirname );
|
2018-11-21 15:26:52 +01:00
|
|
|
|
|
|
|
if( fd < 0 )
|
|
|
|
g_info( "vips__open_image_write: O_TMPFILE failed!" );
|
2018-04-09 14:27:57 +02:00
|
|
|
}
|
2018-11-04 20:51:36 +01:00
|
|
|
#endif /*O_TMPFILE*/
|
2018-06-13 22:33:52 +02:00
|
|
|
|
2011-12-15 11:59:05 +01:00
|
|
|
flags = MODE_WRITE;
|
|
|
|
|
|
|
|
#ifdef _O_TEMPORARY
|
2018-11-05 11:38:15 +01:00
|
|
|
/* On Windows, setting _O_TEMPORARY will delete the file automatically
|
|
|
|
* on process exit, even if the processes crashes.
|
2011-12-15 11:59:05 +01:00
|
|
|
*/
|
2018-11-21 15:26:52 +01:00
|
|
|
if( temp ) {
|
|
|
|
g_info( "vips__open_image_write: setting _O_TEMPORARY" );
|
2011-12-15 11:59:05 +01:00
|
|
|
flags |= _O_TEMPORARY;
|
2018-11-21 15:26:52 +01:00
|
|
|
}
|
2011-12-15 11:59:05 +01:00
|
|
|
#endif /*_O_TEMPORARY*/
|
|
|
|
|
2018-11-21 15:26:52 +01:00
|
|
|
if( fd < 0 ) {
|
|
|
|
g_info( "vips__open_image_write: simple open" );
|
2018-11-04 20:51:36 +01:00
|
|
|
fd = vips_tracked_open( filename, flags, 0666 );
|
2018-11-21 15:26:52 +01:00
|
|
|
}
|
2018-03-30 12:59:57 +02:00
|
|
|
|
2018-04-09 14:27:57 +02:00
|
|
|
if( fd < 0 ) {
|
2018-11-21 15:26:52 +01:00
|
|
|
g_info( "vips__open_image_write: failed!" );
|
2011-12-15 11:59:05 +01:00
|
|
|
vips_error_system( errno, "VipsImage",
|
2018-04-09 14:27:57 +02:00
|
|
|
_( "unable to write to \"%s\"" ), filename );
|
2011-12-15 11:59:05 +01:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( fd );
|
|
|
|
}
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
/* Predict the size of the header plus pixel data. Don't use off_t,
|
|
|
|
* it's sometimes only 32 bits (eg. on many windows build environments) and we
|
|
|
|
* want to always be 64 bit.
|
|
|
|
*/
|
2011-03-28 15:57:43 +02:00
|
|
|
static gint64
|
|
|
|
image_pixel_length( VipsImage *image )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
|
|
|
gint64 psize;
|
|
|
|
|
2011-02-26 15:46:38 +01:00
|
|
|
switch( image->Coding ) {
|
|
|
|
case VIPS_CODING_LABQ:
|
|
|
|
case VIPS_CODING_RAD:
|
|
|
|
case VIPS_CODING_NONE:
|
2011-03-23 15:25:34 +01:00
|
|
|
psize = VIPS_IMAGE_SIZEOF_IMAGE( image );
|
2009-08-16 17:00:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-02-26 15:46:38 +01:00
|
|
|
psize = image->Length;
|
2009-08-16 17:00:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-02-26 15:46:38 +01:00
|
|
|
return( psize + image->sizeof_header );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
2011-12-01 19:03:41 +01:00
|
|
|
/* Copy 2 and 4 bytes, optionally swapping byte order.
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
|
|
|
void
|
2011-12-01 19:03:41 +01:00
|
|
|
vips__copy_4byte( int swap, unsigned char *to, unsigned char *from )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2011-12-01 19:03:41 +01:00
|
|
|
guint32 *in = (guint32 *) from;
|
|
|
|
guint32 *out = (guint32 *) to;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2011-12-01 19:03:41 +01:00
|
|
|
if( swap )
|
|
|
|
*out = GUINT32_SWAP_LE_BE( *in );
|
2009-08-16 17:00:08 +02:00
|
|
|
else
|
2011-12-01 19:03:41 +01:00
|
|
|
*out = *in;
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-12-01 19:03:41 +01:00
|
|
|
vips__copy_2byte( gboolean swap, unsigned char *to, unsigned char *from )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2011-12-01 19:03:41 +01:00
|
|
|
guint16 *in = (guint16 *) from;
|
|
|
|
guint16 *out = (guint16 *) to;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2011-12-01 19:03:41 +01:00
|
|
|
if( swap )
|
|
|
|
*out = GUINT16_SWAP_LE_BE( *in );
|
2009-08-16 17:00:08 +02:00
|
|
|
else
|
2011-12-01 19:03:41 +01:00
|
|
|
*out = *in;
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
2011-11-26 13:41:54 +01:00
|
|
|
guint32
|
|
|
|
vips__file_magic( const char *filename )
|
|
|
|
{
|
|
|
|
guint32 magic;
|
|
|
|
|
2018-03-22 13:08:39 +01:00
|
|
|
if( vips__get_bytes( filename, (unsigned char *) &magic, 4 ) == 4 &&
|
2011-11-26 13:41:54 +01:00
|
|
|
(magic == VIPS_MAGIC_INTEL ||
|
2018-03-22 13:08:39 +01:00
|
|
|
magic == VIPS_MAGIC_SPARC) )
|
2011-11-26 13:41:54 +01:00
|
|
|
return( magic );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
/* offset, read, write functions.
|
|
|
|
*/
|
|
|
|
typedef struct _FieldIO {
|
|
|
|
glong offset;
|
2011-12-01 15:52:49 +01:00
|
|
|
int size;
|
2011-12-01 19:03:41 +01:00
|
|
|
void (*copy)( gboolean swap, unsigned char *to, unsigned char *from );
|
2009-08-16 17:00:08 +02:00
|
|
|
} FieldIO;
|
|
|
|
|
|
|
|
static FieldIO fields[] = {
|
2011-12-01 15:52:49 +01:00
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Xsize ), 4, vips__copy_4byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Ysize ), 4, vips__copy_4byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Bands ), 4, vips__copy_4byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Bbits ), 4, vips__copy_4byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, BandFmt ), 4, vips__copy_4byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Coding ), 4, vips__copy_4byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Type ), 4, vips__copy_4byte },
|
2012-03-02 13:54:50 +01:00
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Xres_float ), 4, vips__copy_4byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Yres_float ), 4, vips__copy_4byte },
|
2011-12-01 15:52:49 +01:00
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Length ), 4, vips__copy_4byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Compression ), 2, vips__copy_2byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Level ), 2, vips__copy_2byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Xoffset ), 4, vips__copy_4byte },
|
|
|
|
{ G_STRUCT_OFFSET( VipsImage, Yoffset ), 4, vips__copy_4byte }
|
2009-08-16 17:00:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2011-03-28 15:57:43 +02:00
|
|
|
vips__read_header_bytes( VipsImage *im, unsigned char *from )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2011-12-01 19:03:41 +01:00
|
|
|
gboolean swap;
|
2009-08-16 17:00:08 +02:00
|
|
|
int i;
|
|
|
|
|
2011-12-01 19:03:41 +01:00
|
|
|
#ifdef SHOW_HEADER
|
|
|
|
printf( "vips__read_header_bytes: file bytes:\n" );
|
|
|
|
for( i = 0; i < im->sizeof_header; i++ )
|
|
|
|
printf( "%2d - 0x%02x\n", i, from[i] );
|
|
|
|
#endif /*SHOW_HEADER*/
|
|
|
|
|
|
|
|
/* The magic number is always written MSB first, we may need to swap.
|
|
|
|
*/
|
|
|
|
vips__copy_4byte( !vips_amiMSBfirst(),
|
|
|
|
(unsigned char *) &im->magic, from );
|
2011-12-01 15:52:49 +01:00
|
|
|
from += 4;
|
2011-02-26 15:46:38 +01:00
|
|
|
if( im->magic != VIPS_MAGIC_INTEL &&
|
|
|
|
im->magic != VIPS_MAGIC_SPARC ) {
|
|
|
|
vips_error( "VipsImage", _( "\"%s\" is not a VIPS image" ),
|
2009-08-16 17:00:08 +02:00
|
|
|
im->filename );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2011-12-01 15:52:49 +01:00
|
|
|
|
2011-12-01 19:03:41 +01:00
|
|
|
/* We need to swap for other fields if the file byte order is
|
|
|
|
* different from ours.
|
|
|
|
*/
|
|
|
|
swap = vips_amiMSBfirst() != (im->magic == VIPS_MAGIC_SPARC);
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2011-12-01 15:52:49 +01:00
|
|
|
for( i = 0; i < VIPS_NUMBER( fields ); i++ ) {
|
2011-12-01 19:03:41 +01:00
|
|
|
fields[i].copy( swap,
|
2009-08-16 17:00:08 +02:00
|
|
|
&G_STRUCT_MEMBER( unsigned char, im, fields[i].offset ),
|
2011-12-01 15:52:49 +01:00
|
|
|
from );
|
|
|
|
from += fields[i].size;
|
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
/* Set this ourselves ... bbits is deprecated in the file format.
|
|
|
|
*/
|
2011-03-02 15:13:05 +01:00
|
|
|
im->Bbits = vips_format_sizeof( im->BandFmt ) << 3;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2012-03-02 13:54:50 +01:00
|
|
|
/* We read xres/yres as floats to a staging area, then copy to double
|
|
|
|
* in the main fields.
|
|
|
|
*/
|
|
|
|
im->Xres = im->Xres_float;
|
|
|
|
im->Yres = im->Yres_float;
|
|
|
|
|
2016-07-30 11:51:54 +02:00
|
|
|
/* Some protection against malicious files. We also check predicted
|
|
|
|
* (based on these values) against real file length, see below.
|
|
|
|
*/
|
|
|
|
im->Xsize = VIPS_CLIP( 1, im->Xsize, VIPS_MAX_COORD );
|
|
|
|
im->Ysize = VIPS_CLIP( 1, im->Ysize, VIPS_MAX_COORD );
|
|
|
|
im->Bands = VIPS_CLIP( 1, im->Bands, VIPS_MAX_COORD );
|
|
|
|
im->BandFmt = VIPS_CLIP( 0, im->BandFmt, VIPS_FORMAT_LAST - 1 );
|
2019-08-27 11:49:37 +02:00
|
|
|
|
|
|
|
/* Coding and Type have missing values, so we look up in the enum.
|
|
|
|
*/
|
|
|
|
im->Type = g_enum_get_value(
|
|
|
|
g_type_class_ref( VIPS_TYPE_INTERPRETATION ),
|
|
|
|
im->Type ) ?
|
|
|
|
im->Type : VIPS_INTERPRETATION_ERROR;
|
|
|
|
im->Coding = g_enum_get_value(
|
|
|
|
g_type_class_ref( VIPS_TYPE_CODING ),
|
|
|
|
im->Coding ) ?
|
|
|
|
im->Coding : VIPS_CODING_ERROR;
|
2016-07-30 11:51:54 +02:00
|
|
|
|
2019-08-24 12:20:45 +02:00
|
|
|
/* Offset, Res, etc. don't affect vips file layout, just
|
2016-07-30 11:51:54 +02:00
|
|
|
* pixel interpretation, don't clip them.
|
|
|
|
*/
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-03-28 15:57:43 +02:00
|
|
|
vips__write_header_bytes( VipsImage *im, unsigned char *to )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2011-12-01 19:03:41 +01:00
|
|
|
/* Swap if the byte order we are asked to write the header in is
|
|
|
|
* different from ours.
|
|
|
|
*/
|
|
|
|
gboolean swap = vips_amiMSBfirst() != (im->magic == VIPS_MAGIC_SPARC);
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
int i;
|
|
|
|
unsigned char *q;
|
|
|
|
|
2012-03-02 13:54:50 +01:00
|
|
|
/* We set xres/yres as floats in a staging area, then copy those
|
|
|
|
* smaller values to the file.
|
|
|
|
*/
|
|
|
|
im->Xres_float = im->Xres;
|
|
|
|
im->Yres_float = im->Yres;
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
/* Always write the magic number MSB first.
|
|
|
|
*/
|
2011-12-01 19:03:41 +01:00
|
|
|
vips__copy_4byte( !vips_amiMSBfirst(),
|
|
|
|
to, (unsigned char *) &im->magic );
|
2009-08-16 17:00:08 +02:00
|
|
|
q = to + 4;
|
|
|
|
|
2011-12-01 15:52:49 +01:00
|
|
|
for( i = 0; i < VIPS_NUMBER( fields ); i++ ) {
|
2011-12-01 19:03:41 +01:00
|
|
|
fields[i].copy( swap,
|
2011-12-01 15:52:49 +01:00
|
|
|
q,
|
2009-08-16 17:00:08 +02:00
|
|
|
&G_STRUCT_MEMBER( unsigned char, im,
|
|
|
|
fields[i].offset ) );
|
2011-12-01 15:52:49 +01:00
|
|
|
q += fields[i].size;
|
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
/* Pad spares with zeros.
|
|
|
|
*/
|
|
|
|
while( q - to < im->sizeof_header )
|
|
|
|
*q++ = 0;
|
|
|
|
|
2011-12-01 19:03:41 +01:00
|
|
|
#ifdef SHOW_HEADER
|
|
|
|
printf( "vips__write_header_bytes: file bytes:\n" );
|
|
|
|
for( i = 0; i < im->sizeof_header; i++ )
|
|
|
|
printf( "%2d - 0x%02x\n", i, to[i] );
|
|
|
|
#endif /*SHOW_HEADER*/
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read a chunk of an fd into memory. Add a '\0' at the end.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
read_chunk( int fd, gint64 offset, size_t length )
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
2011-03-28 17:03:31 +02:00
|
|
|
if( vips__seek( fd, offset ) )
|
2009-08-16 17:00:08 +02:00
|
|
|
return( NULL );
|
2011-03-25 15:01:12 +01:00
|
|
|
if( !(buf = vips_malloc( NULL, length + 1 )) )
|
2009-08-16 17:00:08 +02:00
|
|
|
return( NULL );
|
|
|
|
if( read( fd, buf, length ) != (ssize_t) length ) {
|
2011-03-25 15:01:12 +01:00
|
|
|
vips_free( buf );
|
2011-03-24 12:21:24 +01:00
|
|
|
vips_error( "VipsImage", "%s", _( "unable to read history" ) );
|
2009-08-16 17:00:08 +02:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
buf[length] = '\0';
|
|
|
|
|
|
|
|
return( buf );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does it look like an image has an extension block?
|
|
|
|
*/
|
|
|
|
int
|
2011-03-28 15:57:43 +02:00
|
|
|
vips__has_extension_block( VipsImage *im )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
|
|
|
gint64 psize;
|
|
|
|
|
2011-03-28 15:57:43 +02:00
|
|
|
psize = image_pixel_length( im );
|
2009-08-16 17:00:08 +02:00
|
|
|
g_assert( im->file_length > 0 );
|
|
|
|
|
|
|
|
return( im->file_length - psize > 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read everything after the pixels into memory.
|
|
|
|
*/
|
|
|
|
void *
|
2011-03-28 15:57:43 +02:00
|
|
|
vips__read_extension_block( VipsImage *im, int *size )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
|
|
|
gint64 psize;
|
|
|
|
void *buf;
|
|
|
|
|
2011-03-28 15:57:43 +02:00
|
|
|
psize = image_pixel_length( im );
|
2009-08-16 17:00:08 +02:00
|
|
|
g_assert( im->file_length > 0 );
|
2017-02-25 14:07:43 +01:00
|
|
|
if( im->file_length - psize > 100 * 1024 * 1024 ) {
|
2011-03-24 12:21:24 +01:00
|
|
|
vips_error( "VipsImage",
|
2017-02-25 14:07:43 +01:00
|
|
|
"%s", _( "more than 100 megabytes of XML? "
|
2009-08-16 17:00:08 +02:00
|
|
|
"sufferin' succotash!" ) );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
if( im->file_length - psize == 0 )
|
|
|
|
return( NULL );
|
|
|
|
if( !(buf = read_chunk( im->fd, psize, im->file_length - psize )) )
|
|
|
|
return( NULL );
|
|
|
|
if( size )
|
|
|
|
*size = im->file_length - psize;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-03-28 15:57:43 +02:00
|
|
|
printf( "vips__read_extension_block: read %d bytes from %s\n",
|
2009-08-16 17:00:08 +02:00
|
|
|
(int) (im->file_length - psize), im->filename );
|
|
|
|
printf( "data: \"%s\"\n", (char *) buf );
|
|
|
|
#endif /*DEBUG*/
|
|
|
|
|
|
|
|
return( buf );
|
|
|
|
}
|
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
static int
|
|
|
|
parser_read_fd( XML_Parser parser, int fd )
|
|
|
|
{
|
|
|
|
const int chunk_size = 1024;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
ssize_t bytes_read;
|
2017-03-06 11:07:31 +01:00
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
bytes_read = 0;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
do {
|
|
|
|
void *buf;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
if( !(buf = XML_GetBuffer( parser, chunk_size )) ) {
|
|
|
|
vips_error( "VipsImage",
|
|
|
|
"%s", _( "unable to allocate read buffer" ) );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2017-03-06 11:07:31 +01:00
|
|
|
len = read( fd, buf, chunk_size );
|
|
|
|
if( len == (ssize_t) -1 ) {
|
2017-02-25 14:07:43 +01:00
|
|
|
vips_error( "VipsImage",
|
|
|
|
"%s", _( "read error while fetching XML" ) );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-03-06 11:07:31 +01:00
|
|
|
/* Allow missing XML block.
|
|
|
|
*/
|
|
|
|
if( bytes_read == 0 &&
|
|
|
|
len == 0 )
|
|
|
|
break;
|
|
|
|
bytes_read += len;
|
|
|
|
|
|
|
|
if( !XML_ParseBuffer( parser, len, len == 0 ) ) {
|
|
|
|
vips_error( "VipsImage", "%s", _( "XML parse error" ) );
|
2017-02-25 14:07:43 +01:00
|
|
|
return( -1 );
|
|
|
|
}
|
2017-03-06 11:07:31 +01:00
|
|
|
} while( len > 0 );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
return( 0 );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
#define MAX_PARSE_ATTR (256)
|
|
|
|
|
|
|
|
/* What we track during expat parse.
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
2017-02-25 14:07:43 +01:00
|
|
|
typedef struct _VipsExpatParse {
|
|
|
|
VipsImage *image;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
/* Set on error.
|
|
|
|
*/
|
|
|
|
gboolean error;
|
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
/* TRUE for in header section.
|
|
|
|
*/
|
|
|
|
gboolean header;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
/* For the current node, the type and name.
|
|
|
|
*/
|
|
|
|
XML_Char type[MAX_PARSE_ATTR];
|
|
|
|
XML_Char name[MAX_PARSE_ATTR];
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
/* Accumulate data here.
|
|
|
|
*/
|
2017-02-26 18:37:46 +01:00
|
|
|
VipsDbuf dbuf;
|
2017-02-25 14:07:43 +01:00
|
|
|
} VipsExpatParse;
|
|
|
|
|
|
|
|
static void
|
|
|
|
parser_element_start_handler( void *user_data,
|
|
|
|
const XML_Char *name, const XML_Char **atts )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2017-02-25 14:07:43 +01:00
|
|
|
VipsExpatParse *vep = (VipsExpatParse *) user_data;
|
|
|
|
const XML_Char **p;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf( "parser_element_start: %s\n", name );
|
|
|
|
for( p = atts; *p; p += 2 )
|
|
|
|
printf( "%s = %s\n", p[0], p[1] );
|
|
|
|
#endif /*DEBUG*/
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
if( strcmp( name, "field" ) == 0 ) {
|
|
|
|
for( p = atts; *p; p += 2 ) {
|
|
|
|
if( strcmp( p[0], "name" ) == 0 )
|
|
|
|
vips_strncpy( vep->name, p[1], MAX_PARSE_ATTR );
|
|
|
|
if( strcmp( p[0], "type" ) == 0 )
|
|
|
|
vips_strncpy( vep->type, p[1], MAX_PARSE_ATTR );
|
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-28 14:40:34 +01:00
|
|
|
vips_dbuf_reset( &vep->dbuf );
|
2017-02-25 14:07:43 +01:00
|
|
|
}
|
|
|
|
else if( strcmp( name, "header" ) == 0 )
|
|
|
|
vep->header = TRUE;
|
|
|
|
else if( strcmp( name, "meta" ) == 0 )
|
|
|
|
vep->header = FALSE;
|
|
|
|
else if( strcmp( name, "root" ) == 0 ) {
|
|
|
|
for( p = atts; *p; p += 2 )
|
|
|
|
if( strcmp( p[0], "xmlns" ) == 0 &&
|
2017-02-28 14:40:34 +01:00
|
|
|
!vips_isprefix( NAMESPACE_URI "vips", p[1] ) ) {
|
2017-02-25 14:07:43 +01:00
|
|
|
vips_error( "VipsImage", "%s",
|
|
|
|
_( "incorrect namespace in XML" ) );
|
|
|
|
vep->error = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
/* Chop history into lines, add each one as a refstring.
|
|
|
|
*/
|
|
|
|
static void
|
2011-03-28 15:57:43 +02:00
|
|
|
set_history( VipsImage *im, char *history )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
|
|
|
GSList *history_list;
|
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
/* There can be history there already if we're rewinding.
|
|
|
|
*/
|
2011-03-28 17:03:31 +02:00
|
|
|
VIPS_FREEF( vips__gslist_gvalue_free, im->history_list );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
history_list = NULL;
|
|
|
|
|
|
|
|
for( p = history; *p; p = q ) {
|
2009-10-12 18:16:25 +02:00
|
|
|
if( (q = strchr( p, '\n' )) ) {
|
2009-08-16 17:00:08 +02:00
|
|
|
*q = '\0';
|
2009-10-12 18:16:25 +02:00
|
|
|
q += 1;
|
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
else
|
|
|
|
q = p + strlen( p );
|
|
|
|
|
|
|
|
history_list = g_slist_prepend( history_list,
|
2011-03-28 17:03:31 +02:00
|
|
|
vips__gvalue_ref_string_new( p ) );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
im->history_list = g_slist_reverse( history_list );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-02-25 14:07:43 +01:00
|
|
|
set_meta( VipsImage *image, GType gtype, const char *name, const char *data )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2017-02-25 14:07:43 +01:00
|
|
|
GValue save_value = { 0 };
|
|
|
|
GValue value = { 0 };
|
|
|
|
|
|
|
|
g_value_init( &save_value, VIPS_TYPE_SAVE_STRING );
|
|
|
|
vips_value_set_save_string( &save_value, data );
|
|
|
|
|
|
|
|
g_value_init( &value, gtype );
|
|
|
|
if( !g_value_transform( &save_value, &value ) ) {
|
|
|
|
g_value_unset( &save_value );
|
|
|
|
vips_error( "VipsImage", "%s",
|
|
|
|
_( "error transforming from save format" ) );
|
|
|
|
return( -1 );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
vips_image_set( image, name, &value );
|
|
|
|
g_value_unset( &save_value );
|
|
|
|
g_value_unset( &value );
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
static void
|
|
|
|
parser_element_end_handler( void *user_data, const XML_Char *name )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2017-02-25 14:07:43 +01:00
|
|
|
VipsExpatParse *vep = (VipsExpatParse *) user_data;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf( "parser_element_end_handler: %s\n", name );
|
|
|
|
#endif /*DEBUG*/
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
if( strcmp( name, "field" ) == 0 ) {
|
|
|
|
if( vep->header ) {
|
|
|
|
if( strcmp( name, "Hist" ) == 0 )
|
2017-02-26 18:37:46 +01:00
|
|
|
set_history( vep->image,
|
2017-02-27 23:06:22 +01:00
|
|
|
(char *) vips_dbuf_string( &vep->dbuf,
|
|
|
|
NULL ) );
|
2017-02-25 14:07:43 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
GType gtype = g_type_from_name( vep->type );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
/* Can we convert from VIPS_SAVE_STRING to type?
|
|
|
|
*/
|
|
|
|
if( gtype &&
|
|
|
|
g_value_type_transformable(
|
|
|
|
VIPS_TYPE_SAVE_STRING, gtype ) &&
|
|
|
|
set_meta( vep->image,
|
2017-02-26 18:37:46 +01:00
|
|
|
gtype, vep->name,
|
2017-02-27 23:06:22 +01:00
|
|
|
(char *) vips_dbuf_string( &vep->dbuf,
|
|
|
|
NULL ) ) )
|
2017-02-25 14:07:43 +01:00
|
|
|
vep->error = TRUE;
|
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
static void
|
|
|
|
parser_data_handler( void *user_data, const XML_Char *data, int len )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2017-02-25 14:07:43 +01:00
|
|
|
VipsExpatParse *vep = (VipsExpatParse *) user_data;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf( "parser_data_handler: %d bytes\n", len );
|
|
|
|
#endif /*DEBUG*/
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_write( &vep->dbuf, (unsigned char *) data, len );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
2011-03-28 17:03:31 +02:00
|
|
|
/* Called at the end of vips open ... get any XML after the pixel data
|
2009-08-16 17:00:08 +02:00
|
|
|
* and read it in.
|
|
|
|
*/
|
|
|
|
static int
|
2011-03-28 15:57:43 +02:00
|
|
|
readhist( VipsImage *im )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2017-02-25 14:07:43 +01:00
|
|
|
XML_Parser parser;
|
|
|
|
VipsExpatParse vep;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
if( vips__seek( im->fd, image_pixel_length( im ) ) )
|
|
|
|
return( -1 );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
parser = XML_ParserCreate( "UTF-8" );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 14:07:43 +01:00
|
|
|
vep.image = im;
|
2017-02-26 18:37:46 +01:00
|
|
|
vips_dbuf_init( &vep.dbuf );
|
2017-02-25 14:07:43 +01:00
|
|
|
vep.error = FALSE;
|
|
|
|
XML_SetUserData( parser, &vep );
|
|
|
|
|
|
|
|
XML_SetElementHandler( parser,
|
|
|
|
parser_element_start_handler, parser_element_end_handler );
|
|
|
|
XML_SetCharacterDataHandler( parser, parser_data_handler );
|
|
|
|
|
|
|
|
if( parser_read_fd( parser, im->fd ) ||
|
|
|
|
vep.error ) {
|
2017-02-26 18:37:46 +01:00
|
|
|
vips_dbuf_destroy( &vep.dbuf );
|
2017-02-25 14:07:43 +01:00
|
|
|
XML_ParserFree( parser );
|
2009-08-16 17:00:08 +02:00
|
|
|
return( -1 );
|
2017-02-25 14:07:43 +01:00
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-26 18:37:46 +01:00
|
|
|
vips_dbuf_destroy( &vep.dbuf );
|
2017-02-25 14:07:43 +01:00
|
|
|
XML_ParserFree( parser );
|
|
|
|
|
|
|
|
return( 0 );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
int
|
|
|
|
vips__write_extension_block( VipsImage *im, void *buf, int size )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2017-02-25 18:28:48 +01:00
|
|
|
gint64 length;
|
|
|
|
gint64 psize;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
psize = image_pixel_length( im );
|
|
|
|
if( (length = vips_file_length( im->fd )) == -1 )
|
|
|
|
return( -1 );
|
|
|
|
if( length < psize ) {
|
|
|
|
vips_error( "VipsImage", "%s", _( "file has been truncated" ) );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
if( vips__ftruncate( im->fd, psize ) ||
|
|
|
|
vips__seek( im->fd, psize ) )
|
2009-08-16 17:00:08 +02:00
|
|
|
return( -1 );
|
2017-02-25 18:28:48 +01:00
|
|
|
if( vips__write( im->fd, buf, size ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf( "vips__write_extension_block: written %d bytes of XML to %s\n",
|
|
|
|
size, im->filename );
|
|
|
|
#endif /*DEBUG*/
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2017-02-26 18:37:46 +01:00
|
|
|
/* Append a string to a buffer, but escape " as \".
|
|
|
|
*/
|
|
|
|
static void
|
2017-02-28 17:44:12 +01:00
|
|
|
dbuf_write_quotes( VipsDbuf *dbuf, const char *str )
|
2017-02-26 18:37:46 +01:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
for( p = str; *p; p += len ) {
|
|
|
|
len = strcspn( p, "\"" );
|
|
|
|
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_write( dbuf, (unsigned char *) p, len );
|
2017-02-26 18:37:46 +01:00
|
|
|
if( p[len] == '"' )
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( dbuf, "\\" );
|
2017-02-26 18:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
static void *
|
2017-02-26 18:37:46 +01:00
|
|
|
build_xml_meta( VipsMeta *meta, VipsDbuf *dbuf )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
|
|
|
GType type = G_VALUE_TYPE( &meta->value );
|
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
const char *str;
|
|
|
|
|
2011-03-27 23:03:38 +02:00
|
|
|
/* If we can transform to VIPS_TYPE_SAVE_STRING and back, we can save
|
|
|
|
* and restore.
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
2011-03-27 23:03:38 +02:00
|
|
|
if( g_value_type_transformable( type, VIPS_TYPE_SAVE_STRING ) &&
|
|
|
|
g_value_type_transformable( VIPS_TYPE_SAVE_STRING, type ) ) {
|
2009-08-16 17:00:08 +02:00
|
|
|
GValue save_value = { 0 };
|
|
|
|
|
2011-03-27 23:03:38 +02:00
|
|
|
g_value_init( &save_value, VIPS_TYPE_SAVE_STRING );
|
2009-08-16 17:00:08 +02:00
|
|
|
if( !g_value_transform( &meta->value, &save_value ) ) {
|
2011-03-24 12:21:24 +01:00
|
|
|
vips_error( "VipsImage", "%s",
|
2009-08-16 17:00:08 +02:00
|
|
|
_( "error transforming to save format" ) );
|
2017-02-26 18:37:46 +01:00
|
|
|
return( meta );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
2017-02-25 18:28:48 +01:00
|
|
|
|
2017-08-16 19:36:25 +02:00
|
|
|
/* We need to validate the str to make sure we'll be able to
|
|
|
|
* read it back.
|
|
|
|
*/
|
2017-02-25 18:28:48 +01:00
|
|
|
str = vips_value_get_save_string( &save_value );
|
2017-08-16 19:36:25 +02:00
|
|
|
if( g_utf8_validate( str, -1, NULL ) ) {
|
|
|
|
vips_dbuf_writef( dbuf,
|
|
|
|
" <field type=\"%s\" name=\"",
|
|
|
|
g_type_name( type ) );
|
|
|
|
dbuf_write_quotes( dbuf, meta->name );
|
|
|
|
vips_dbuf_writef( dbuf, "\">" );
|
2018-01-26 18:20:58 +01:00
|
|
|
vips_dbuf_write_amp( dbuf, str );
|
2017-08-16 19:36:25 +02:00
|
|
|
vips_dbuf_writef( dbuf, "</field>\n" );
|
|
|
|
}
|
2017-02-25 18:28:48 +01:00
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
g_value_unset( &save_value );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2017-02-25 19:10:42 +01:00
|
|
|
/* Make the xml we append to vips images after the pixel data.
|
|
|
|
*/
|
2017-02-25 18:28:48 +01:00
|
|
|
static char *
|
|
|
|
build_xml( VipsImage *image )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2017-02-26 18:37:46 +01:00
|
|
|
VipsDbuf dbuf;
|
2017-02-25 18:28:48 +01:00
|
|
|
const char *str;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-26 18:37:46 +01:00
|
|
|
vips_dbuf_init( &dbuf );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( &dbuf, "<?xml version=\"1.0\"?>\n" );
|
|
|
|
vips_dbuf_writef( &dbuf, "<root xmlns=\"%svips/%d.%d.%d\">\n",
|
2017-02-25 19:10:42 +01:00
|
|
|
NAMESPACE_URI,
|
2017-02-25 18:28:48 +01:00
|
|
|
VIPS_MAJOR_VERSION, VIPS_MINOR_VERSION, VIPS_MICRO_VERSION );
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( &dbuf, " <header>\n" );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
str = vips_image_get_history( image );
|
2017-08-16 19:36:25 +02:00
|
|
|
if( g_utf8_validate( str, -1, NULL ) ) {
|
|
|
|
vips_dbuf_writef( &dbuf,
|
|
|
|
" <field type=\"%s\" name=\"Hist\">",
|
|
|
|
g_type_name( VIPS_TYPE_REF_STRING ) );
|
2018-01-26 18:20:58 +01:00
|
|
|
vips_dbuf_write_amp( &dbuf, str );
|
2017-08-16 19:36:25 +02:00
|
|
|
vips_dbuf_writef( &dbuf, "</field>\n" );
|
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( &dbuf, " </header>\n" );
|
|
|
|
vips_dbuf_writef( &dbuf, " <meta>\n" );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
if( vips_slist_map2( image->meta_traverse,
|
2017-02-26 18:37:46 +01:00
|
|
|
(VipsSListMap2Fn) build_xml_meta, &dbuf, NULL ) ) {
|
|
|
|
vips_dbuf_destroy( &dbuf );
|
2017-02-25 18:28:48 +01:00
|
|
|
return( NULL );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( &dbuf, " </meta>\n" );
|
|
|
|
vips_dbuf_writef( &dbuf, "</root>\n" );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-27 23:06:22 +01:00
|
|
|
return( (char *) vips_dbuf_steal( &dbuf, NULL ) );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
2017-02-25 19:10:42 +01:00
|
|
|
static void *
|
|
|
|
vips__xml_properties_meta( VipsImage *image,
|
|
|
|
const char *field, GValue *value, void *a )
|
|
|
|
{
|
2017-02-26 18:37:46 +01:00
|
|
|
VipsDbuf *dbuf = (VipsDbuf *) a;
|
2017-02-25 19:10:42 +01:00
|
|
|
GType type = G_VALUE_TYPE( value );
|
|
|
|
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
/* If we can transform to VIPS_TYPE_SAVE_STRING and back, we can save
|
|
|
|
* and restore.
|
|
|
|
*/
|
|
|
|
if( g_value_type_transformable( type, VIPS_TYPE_SAVE_STRING ) &&
|
|
|
|
g_value_type_transformable( VIPS_TYPE_SAVE_STRING, type ) ) {
|
|
|
|
GValue save_value = { 0 };
|
|
|
|
|
|
|
|
g_value_init( &save_value, VIPS_TYPE_SAVE_STRING );
|
|
|
|
if( !g_value_transform( value, &save_value ) ) {
|
|
|
|
vips_error( "VipsImage", "%s",
|
|
|
|
_( "error transforming to save format" ) );
|
2017-02-26 18:37:46 +01:00
|
|
|
return( dbuf );
|
2017-02-25 19:10:42 +01:00
|
|
|
}
|
|
|
|
str = vips_value_get_save_string( &save_value );
|
|
|
|
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( dbuf, " <property>\n" );
|
|
|
|
vips_dbuf_writef( dbuf, " <name>" );
|
2018-01-26 18:20:58 +01:00
|
|
|
vips_dbuf_write_amp( dbuf, field );
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( dbuf, "</name>\n" );
|
|
|
|
vips_dbuf_writef( dbuf, " <value type=\"%s\">",
|
2017-02-25 19:10:42 +01:00
|
|
|
g_type_name( type ) );
|
2018-01-26 18:20:58 +01:00
|
|
|
vips_dbuf_write_amp( dbuf, str );
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( dbuf, "</value>\n" );
|
|
|
|
vips_dbuf_writef( dbuf, " </property>\n" );
|
2017-02-28 18:17:23 +01:00
|
|
|
|
|
|
|
g_value_unset( &save_value );
|
2017-02-25 19:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the xml we write to vips-properties in dzsave, or to TIFF. A simple
|
|
|
|
* dump of all vips metadata. Free with g_free().
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
vips__xml_properties( VipsImage *image )
|
|
|
|
{
|
2017-02-26 18:37:46 +01:00
|
|
|
VipsDbuf dbuf;
|
2017-02-25 19:10:42 +01:00
|
|
|
GTimeVal now;
|
|
|
|
char *date;
|
|
|
|
|
2017-02-26 18:37:46 +01:00
|
|
|
vips_dbuf_init( &dbuf );
|
2017-02-25 19:10:42 +01:00
|
|
|
|
|
|
|
g_get_current_time( &now );
|
|
|
|
date = g_time_val_to_iso8601( &now );
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( &dbuf, "<?xml version=\"1.0\"?>\n" );
|
|
|
|
vips_dbuf_writef( &dbuf, "<image xmlns=\"%s/dzsave\" "
|
2017-02-25 19:10:42 +01:00
|
|
|
"date=\"%s\" version=\"%d.%d.%d\">\n",
|
|
|
|
NAMESPACE_URI,
|
|
|
|
date,
|
|
|
|
VIPS_MAJOR_VERSION, VIPS_MINOR_VERSION, VIPS_MICRO_VERSION );
|
|
|
|
g_free( date );
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( &dbuf, " <properties>\n" );
|
2017-02-25 19:10:42 +01:00
|
|
|
|
2017-02-26 18:37:46 +01:00
|
|
|
if( vips_image_map( image, vips__xml_properties_meta, &dbuf ) ) {
|
|
|
|
vips_dbuf_destroy( &dbuf );
|
2017-02-25 19:10:42 +01:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:44:12 +01:00
|
|
|
vips_dbuf_writef( &dbuf, " </properties>\n" );
|
|
|
|
vips_dbuf_writef( &dbuf, "</image>\n" );
|
2017-02-25 19:10:42 +01:00
|
|
|
|
2017-02-27 23:06:22 +01:00
|
|
|
return( (char *) vips_dbuf_steal( &dbuf, NULL ) );
|
2017-02-25 19:10:42 +01:00
|
|
|
}
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
/* Append XML to output fd.
|
|
|
|
*/
|
|
|
|
int
|
2017-02-25 18:28:48 +01:00
|
|
|
vips__writehist( VipsImage *image )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
2017-02-25 18:28:48 +01:00
|
|
|
char *xml;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
assert( image->dtype == VIPS_IMAGE_OPENOUT );
|
|
|
|
assert( image->fd != -1 );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2017-02-25 19:10:42 +01:00
|
|
|
if( !(xml = build_xml( image )) )
|
2009-08-16 17:00:08 +02:00
|
|
|
return( -1 );
|
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
if( vips__write_extension_block( image, xml, strlen( xml ) ) ) {
|
|
|
|
g_free( xml );
|
2009-08-16 17:00:08 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2017-08-16 19:36:25 +02:00
|
|
|
printf( "vips__writehist: saved XML is: \"%s\"\n", xml );
|
2009-08-16 17:00:08 +02:00
|
|
|
#endif /*DEBUG*/
|
|
|
|
|
2017-02-25 18:28:48 +01:00
|
|
|
g_free( xml );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the filename, read the header, some sanity checking.
|
|
|
|
*/
|
2009-12-10 11:58:10 +01:00
|
|
|
int
|
2011-03-01 15:49:16 +01:00
|
|
|
vips_image_open_input( VipsImage *image )
|
2009-08-16 17:00:08 +02:00
|
|
|
{
|
|
|
|
/* We don't use im->sizeof_header here, but we know we're reading a
|
|
|
|
* VIPS image anyway.
|
|
|
|
*/
|
2011-03-28 18:18:06 +02:00
|
|
|
unsigned char header[VIPS_SIZEOF_HEADER];
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
gint64 psize;
|
|
|
|
gint64 rsize;
|
|
|
|
|
2011-02-26 15:46:38 +01:00
|
|
|
image->dtype = VIPS_IMAGE_OPENIN;
|
2012-07-05 15:40:18 +02:00
|
|
|
|
|
|
|
/* We may have an fd already, see vips_image_rewind_output().
|
|
|
|
*/
|
|
|
|
if( image->fd == -1 ) {
|
|
|
|
image->fd = vips__open_image_read( image->filename );
|
|
|
|
if( image->fd == -1 )
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
vips__seek( image->fd, 0 );
|
2011-03-28 18:18:06 +02:00
|
|
|
if( read( image->fd, header, VIPS_SIZEOF_HEADER ) !=
|
|
|
|
VIPS_SIZEOF_HEADER ||
|
2011-03-28 15:57:43 +02:00
|
|
|
vips__read_header_bytes( image, header ) ) {
|
2011-03-24 12:21:24 +01:00
|
|
|
vips_error_system( errno, "VipsImage",
|
2011-03-23 19:07:51 +01:00
|
|
|
_( "unable to read header for \"%s\"" ),
|
|
|
|
image->filename );
|
2009-08-16 17:00:08 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2016-05-17 20:43:47 +02:00
|
|
|
/* Predict and check the file size. Only issue a warning, we want to be
|
|
|
|
* able to read all the header fields we can, even if the actual data
|
|
|
|
* isn't there.
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
2011-03-28 15:57:43 +02:00
|
|
|
psize = image_pixel_length( image );
|
2011-03-28 17:03:31 +02:00
|
|
|
if( (rsize = vips_file_length( image->fd )) == -1 )
|
2009-08-16 17:00:08 +02:00
|
|
|
return( -1 );
|
|
|
|
image->file_length = rsize;
|
2016-05-17 20:43:47 +02:00
|
|
|
if( psize > rsize )
|
2017-01-03 16:52:27 +01:00
|
|
|
g_warning( _( "unable to read data for \"%s\", %s" ),
|
2009-08-16 17:00:08 +02:00
|
|
|
image->filename, _( "file has been truncated" ) );
|
|
|
|
|
2011-02-14 19:23:01 +01:00
|
|
|
/* Set demand style. This suits a disc file we read sequentially.
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
2011-02-26 15:46:38 +01:00
|
|
|
image->dhint = VIPS_DEMAND_STYLE_THINSTRIP;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
/* Set the history part of im descriptor. Don't return an error if this
|
|
|
|
* fails (due to eg. corrupted XML) because it's probably mostly
|
|
|
|
* harmless.
|
|
|
|
*/
|
2011-03-28 15:57:43 +02:00
|
|
|
if( readhist( image ) ) {
|
2017-03-06 11:07:31 +01:00
|
|
|
g_warning( _( "error reading vips image metadata: %s" ),
|
|
|
|
vips_error_buffer() );
|
2011-02-26 15:46:38 +01:00
|
|
|
vips_error_clear();
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2011-03-23 19:07:51 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
vips_image_open_output( VipsImage *image )
|
|
|
|
{
|
|
|
|
if( image->fd == -1 ) {
|
|
|
|
/* Don't use im->sizeof_header here, but we know we're
|
|
|
|
* writing a VIPS image anyway.
|
|
|
|
*/
|
2011-03-28 18:18:06 +02:00
|
|
|
unsigned char header[VIPS_SIZEOF_HEADER];
|
2011-11-10 18:48:09 +01:00
|
|
|
|
2011-12-15 11:59:05 +01:00
|
|
|
if( (image->fd = vips__open_image_write( image->filename,
|
|
|
|
image->delete_on_close )) < 0 )
|
2011-03-23 19:07:51 +01:00
|
|
|
return( -1 );
|
|
|
|
|
2011-12-01 14:17:40 +01:00
|
|
|
/* We always write in native mode, so we must overwrite the
|
|
|
|
* magic we read from the file originally.
|
|
|
|
*/
|
|
|
|
image->magic = vips_amiMSBfirst() ?
|
|
|
|
VIPS_MAGIC_SPARC : VIPS_MAGIC_INTEL;
|
|
|
|
|
2011-03-28 15:57:43 +02:00
|
|
|
if( vips__write_header_bytes( image, header ) ||
|
2011-03-28 18:18:06 +02:00
|
|
|
vips__write( image->fd, header, VIPS_SIZEOF_HEADER ) )
|
2011-03-23 19:07:51 +01:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|