Merge pull request #1899 from kleisauke/byteswap-vipsload

Ensure vipsload only byte swaps if necessary
This commit is contained in:
John Cupitt 2020-11-23 14:07:23 +00:00 committed by GitHub
commit eddc99e6d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 16 deletions

View File

@ -859,10 +859,7 @@ vips_image_build( VipsObject *object )
if( (magic = vips__file_magic( filename )) ) {
/* We may need to byteswap.
*/
guint32 us = vips_amiMSBfirst() ?
VIPS_MAGIC_INTEL : VIPS_MAGIC_SPARC;
if( magic == us ) {
if( GUINT_FROM_BE( magic ) == image->magic ) {
/* Native open.
*/
if( vips_image_open_input( image ) )

View File

@ -1602,16 +1602,13 @@ vips_ispoweroftwo( int p )
int
vips_amiMSBfirst( void )
{
int test;
unsigned char *p = (unsigned char *) &test;
test = 0;
p[0] = 255;
if( test == 255 )
return( 0 );
else
return( 1 );
#if G_BYTE_ORDER == G_BIG_ENDIAN
return( 1 );
#elif G_BYTE_ORDER == G_LITTLE_ENDIAN
return( 0 );
#else
#error "Byte order not recognised"
#endif
}
/* Return the tmp dir. On Windows, GetTempPath() will also check the values of

View File

@ -354,7 +354,7 @@ vips__read_header_bytes( VipsImage *im, unsigned char *from )
/* We need to swap for other fields if the file byte order is
* different from ours.
*/
swap = vips_amiMSBfirst() != (im->magic == VIPS_MAGIC_SPARC);
swap = vips_amiMSBfirst() != vips_image_isMSBfirst( im );
for( i = 0; i < VIPS_NUMBER( fields ); i++ ) {
fields[i].copy( swap,
@ -435,7 +435,7 @@ vips__write_header_bytes( VipsImage *im, unsigned char *to )
/* 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);
gboolean swap = vips_amiMSBfirst() != vips_image_isMSBfirst( im );
int i;
unsigned char *q;

View File

@ -128,6 +128,17 @@ class TestForeign:
for i in range(len(before_exif)):
assert before_exif[i] == after_exif[i]
# https://github.com/libvips/libvips/issues/1847
filename = temp_filename(self.tempdir, ".v")
x = pyvips.Image.black(16, 16) + 128
x.write_to_file(filename)
x = pyvips.Image.new_from_file(filename)
assert x.width == 16
assert x.height == 16
assert x.bands == 1
assert x.avg() == 128
x = None
@skip_if_no("jpegload")