more hackery

getting closer, stuck on vips image byte-swap logic now, probably need a
im_file2vips() type function
This commit is contained in:
John Cupitt 2011-02-23 17:38:24 +00:00
parent 1d364cd9f1
commit 16b032f8c1
6 changed files with 74 additions and 64 deletions

View File

@ -31,6 +31,7 @@
- set MAP_NOCACHE on OS X, otherwise performance dives off a cliff with - set MAP_NOCACHE on OS X, otherwise performance dives off a cliff with
files larger than memory files larger than memory
- removed man pages, we are all gtk-doc now - removed man pages, we are all gtk-doc now
- added VIPS_FORMAT_BIGENDIAN format flag
30/11/10 started 7.24.0 30/11/10 started 7.24.0
- bump for new stable - bump for new stable

View File

@ -68,11 +68,16 @@
* VipsFormatFlags: * VipsFormatFlags:
* @VIPS_FORMAT_NONE: no flags set * @VIPS_FORMAT_NONE: no flags set
* @VIPS_FORMAT_PARTIAL: the image may be read lazilly * @VIPS_FORMAT_PARTIAL: the image may be read lazilly
* @VIPS_FORMAT_BIGENDIAN: image pixels are most-significant byte first
* *
* Some hints about the image loader. * Some hints about the image loader.
* *
* @VIPS_FORMAT_PARTIAL means that the image can be read directly from the * @VIPS_FORMAT_PARTIAL means that the image can be read directly from the
* file without needing to be unpacked to a temporary image first. * file without needing to be unpacked to a temporary image first.
*
* @VIPS_FORMAT_BIGENDIAN means that image pixels are most-significant byte
* first. Depending on the native byte order of the host machine, you may
* need to swap bytes. See im_copy_swap().
*/ */
/** /**
@ -339,6 +344,27 @@ vips_format_get_flags( VipsFormatClass *format, const char *filename )
static const char *vips_suffs[] = { ".v", NULL }; static const char *vips_suffs[] = { ".v", NULL };
int
im_isvips( const char *filename )
{
unsigned char buf[4];
if( im__get_bytes( filename, buf, 4 ) ) {
if( buf[0] == 0x08 && buf[1] == 0xf2 &&
buf[2] == 0xa6 && buf[3] == 0xb6 )
/* SPARC-order VIPS image.
*/
return( 1 );
else if( buf[3] == 0x08 && buf[2] == 0xf2 &&
buf[1] == 0xa6 && buf[0] == 0xb6 )
/* INTEL-order VIPS image.
*/
return( 1 );
}
return( 0 );
}
static int static int
file2vips( const char *filename, IMAGE *out ) file2vips( const char *filename, IMAGE *out )
{ {
@ -366,7 +392,19 @@ vips2file( IMAGE *im, const char *filename )
static VipsFormatFlags static VipsFormatFlags
vips_flags( const char *filename ) vips_flags( const char *filename )
{ {
return( VIPS_FORMAT_PARTIAL ); VipsFormatFlags flags;
unsigned char buf[4];
flags = VIPS_FORMAT_PARTIAL;
if( im__get_bytes( filename, buf, 4 ) &&
buf[0] == 0x08 &&
buf[1] == 0xf2 &&
buf[2] == 0xa6 &&
buf[3] == 0xb6 )
flags |= VIPS_FORMAT_BIGENDIAN;
return( flags );
} }
/* Vips format adds no new members. /* Vips format adds no new members.

View File

@ -54,7 +54,8 @@ extern "C" {
*/ */
typedef enum { typedef enum {
VIPS_FORMAT_NONE = 0, /* No flags set */ VIPS_FORMAT_NONE = 0, /* No flags set */
VIPS_FORMAT_PARTIAL = 1 /* Lazy read OK (eg. tiled tiff) */ VIPS_FORMAT_PARTIAL = 1, /* Lazy read OK (eg. tiled tiff) */
VIPS_FORMAT_BIGENDIAN = 2 /* Most-significant byte first */
} VipsFormatFlags; } VipsFormatFlags;
/* Don't instantiate these things, just use the class stuff. /* Don't instantiate these things, just use the class stuff.

View File

@ -382,7 +382,7 @@ vips_image_finalize( GObject *gobject )
if( image->dtype == VIPS_IMAGE_OPENOUT ) if( image->dtype == VIPS_IMAGE_OPENOUT )
(void) im__writehist( image ); (void) im__writehist( image );
if( close( image->fd ) == -1 ) if( close( image->fd ) == -1 )
im_error( "vips_image_finalize", im_error( "VipsImage",
_( "unable to close fd for %s" ), _( "unable to close fd for %s" ),
image->filename ); image->filename );
image->fd = -1; image->fd = -1;
@ -818,6 +818,21 @@ vips_image_build( VipsObject *object )
return( -1 ); return( -1 );
if( vips_format_is_vips( format ) ) { if( vips_format_is_vips( format ) ) {
/* We may need to byteswap.
*/
VipsFormatFlags flags =
vips_format_get_flags( format,
image->filename );
gboolean bigendian = flags & VIPS_FORMAT_BIGENDIAN;
gboolean swap = bigendian != im_amiMSBfirst();
if( swap ) {
VipsImage *real;
if( !(real = im_open_local( image, "p" );
image->dtype = VIPS_IMAGE_PARTIAL;
if( vips_open_input( image ) ) if( vips_open_input( image ) )
return( -1 ); return( -1 );
@ -835,59 +850,48 @@ vips_image_build( VipsObject *object )
im_amiMSBfirst() && im_amiMSBfirst() &&
vips_format_sizeof( image->BandFmt ) != vips_format_sizeof( image->BandFmt ) !=
1 ) { 1 ) {
im_error( "vips_image_build", im_error( "VipsImage", "%s",
_( "open for read-" _( "open read-"
"write for native format " "write for native format "
"images only" ) ); "images only" ) );
return( -1 ); return( -1 );
} }
image->dtype = VIPS_IMAGE_TYPE_OPENIN; image->dtype = VIPS_IMAGE_OPENINRW;
} }
} }
else { else {
if( vips_open_lazy( image, if( vips_open_lazy( image, format,
format, filename, mode[1] == 'd' ) ) image->filename, image->mode[1] == 'd' ) )
return( -1 ); return( -1 );
} }
break; break;
case 'w': case 'w':
if( (format = vips_format_for_name( filename )) ) { if( !(format = vips_format_for_name( image->filename )) )
if( vips_format_is_vips( format ) ) {
if( vips_open_output( image ) )
return( -1 );
}
else {
image->dtype = VIPS_IMAGE_TYPE_PARTIAL;
vips_attach_save( image,
format->save, filename );
}
}
else {
char suffix[FILENAME_MAX];
im_filename_suffix( filename, suffix );
im_error( "vips_image_build",
_( "unsupported filetype \"%s\"" ),
suffix );
return( -1 ); return( -1 );
if( vips_format_is_vips( format ) )
image->dtype = VIPS_IMAGE_OPENOUT;
else {
image->dtype = VIPS_IMAGE_PARTIAL;
vips_attach_save( image,
format->save, image->filename );
} }
break; break;
case 't': case 't':
image->dtype = VIPS_IMAGE_TYPE_SETBUF; image->dtype = VIPS_IMAGE_SETBUF;
image->dhint = VIPS_DEMAND_ANY; image->dhint = VIPS_DEMAND_ANY;
break; break;
case 'p': case 'p':
image->dtype = VIPS_IMAGE_TYPE_PARTIAL; image->dtype = VIPS_IMAGE_PARTIAL;
break; break;
default: default:
im_error( "vips_image_build", _( "bad mode \"%s\"" ), mode ); im_error( "VipsImage", _( "bad mode \"%s\"" ), mode );
return( -1 ); return( -1 );
} }

View File

@ -1498,27 +1498,6 @@ im_ispoweroftwo( int p )
return( 0 ); return( 0 );
} }
int
im_isvips( const char *filename )
{
unsigned char buf[4];
if( im__get_bytes( filename, buf, 4 ) ) {
if( buf[0] == 0x08 && buf[1] == 0xf2 &&
buf[2] == 0xa6 && buf[3] == 0xb6 )
/* SPARC-order VIPS image.
*/
return( 1 );
else if( buf[3] == 0x08 && buf[2] == 0xf2 &&
buf[1] == 0xa6 && buf[0] == 0xb6 )
/* INTEL-order VIPS image.
*/
return( 1 );
}
return( 0 );
}
/* Test this processor for endianness. True for SPARC order. /* Test this processor for endianness. True for SPARC order.
*/ */
int int

View File

@ -1031,16 +1031,3 @@ im_open_vips( const char *filename )
return( im ); return( im );
} }
IMAGE *
im_openout( const char *filename )
{
IMAGE *image;
if( !(image = im_init( filename )) )
return( NULL );
image->dtype = IM_OPENOUT;
return( image );
}