/* Read a ppm/pgm file using mmap(). */ static int read_mmap( FILE *fp, const char *filename, int msb_first, VipsImage *out ) { const guint64 header_offset = ftell( fp ); VipsImage *x = vips_image_new(); VipsImage **t = (VipsImage **) vips_object_local_array( VIPS_OBJECT( x ), 3 ); if( vips_rawload( filename, &t[0], out->Xsize, out->Ysize, VIPS_IMAGE_SIZEOF_PEL( out ), "offset", header_offset, NULL ) || vips_copy( t[0], &t[1], "bands", out->Bands, "format", out->BandFmt, "coding", out->Coding, NULL ) || vips_copy( t[1], &t[2], "swap", !vips_amiMSBfirst(), NULL ) || vips_image_write( t[2], out ) ) { g_object_unref( x ); return( -1 ); } g_object_unref( x ); return( 0 ); }
/* The inverse: take ink to a vec of double. Used in the vips7 compat * wrappers. Result valid while im is valid. */ double * vips__ink_to_vector( const char *domain, VipsImage *im, VipsPel *ink, int *n ) { VipsImage **t = (VipsImage **) vips_object_local_array( VIPS_OBJECT( im ), 6 ); double *result; #ifdef VIPS_DEBUG printf( "vips__ink_to_vector: starting\n" ); #endif /*VIPS_DEBUG*/ /* Wrap a VipsImage around ink. */ t[0] = vips_image_new_from_memory( ink, 1, 1, VIPS_IMAGE_SIZEOF_PEL( im ), VIPS_FORMAT_UCHAR ); if( vips_copy( t[0], &t[1], "bands", im->Bands, "format", im->BandFmt, "coding", im->Coding, "interpretation", im->Type, NULL ) ) return( NULL ); /* The image may be coded .. unpack to double. */ if( vips_image_decode( t[1], &t[2] ) || vips_cast( t[2], &t[3], VIPS_FORMAT_DOUBLE, NULL ) ) return( NULL ); /* To a mem buffer, then copy to out. */ if( !(t[4] = vips_image_new_memory()) || vips_image_write( t[3], t[4] ) ) return( NULL ); if( !(result = VIPS_ARRAY( im, t[4]->Bands, double )) ) return( NULL ); memcpy( result, t[4]->data, VIPS_IMAGE_SIZEOF_PEL( t[4] ) ); *n = t[4]->Bands; #ifdef VIPS_DEBUG { int i; printf( "vips__ink_to_vector:\n" ); printf( "\tink = " ); for( i = 0; i < n; i++ ) printf( "%d ", ink[i] ); printf( "\n" ); printf( "\tvec = " ); for( i = 0; i < *n; i++ ) printf( "%d ", result[i] ); printf( "\n" ); } #endif /*VIPS_DEBUG*/ return( result ); }
/* Make an n-band image. Input 1 or n bands. */ int vips__bandup( const char *domain, VipsImage *in, VipsImage **out, int n ) { VipsImage *bands[256]; int i; if( in->Bands == n ) return( vips_copy( in, out, NULL ) ); if( in->Bands != 1 ) { vips_error( domain, _( "not one band or %d bands" ), n ); return( -1 ); } if( n > 256 || n < 1 ) { vips_error( domain, "%s", _( "bad bands" ) ); return( -1 ); } for( i = 0; i < n; i++ ) bands[i] = in; return( vips_bandjoin( bands, out, n, NULL ) ); }