VipsImage * vips__matrix_read_file( FILE *fp ) { char whitemap[256]; int i; char *p; int width; int height; double scale; double offset; VipsImage *out; for( i = 0; i < 256; i++ ) whitemap[i] = 0; for( p = WHITESPACE; *p; p++ ) whitemap[(int) *p] = 1; if( vips__matrix_header( whitemap, fp, &width, &height, &scale, &offset ) ) return( NULL ); if( !(out = vips_image_new_matrix( width, height )) ) return( NULL ); vips_image_set_double( out, "scale", scale ); vips_image_set_double( out, "offset", offset ); if( vips__matrix_body( whitemap, out, fp ) ) { g_object_unref( out ); return( NULL ); } return( out ); }
/* Get the header from an matrix file. * * Also read the first line and make sure there are the right number of * entries. */ int vips__matrix_read_header( const char *filename, int *width, int *height, double *scale, double *offset ) { char whitemap[256]; int i; char *p; FILE *fp; int ch; double d; for( i = 0; i < 256; i++ ) whitemap[i] = 0; for( p = WHITESPACE; *p; p++ ) whitemap[(int) *p] = 1; if( !(fp = vips__file_open_read( filename, NULL, TRUE )) ) return( -1 ); if( vips__matrix_header( whitemap, fp, width, height, scale, offset ) ) { fclose( fp ); return( -1 ); } for( i = 0; i < *width; i++ ) { ch = read_ascii_double( fp, whitemap, &d ); if( ch ) { fclose( fp ); vips_error( "mask2vips", "%s", _( "line too short" ) ); return( -1 ); } } /* Deliberately don't check for line too long. */ fclose( fp ); return( 0 ); }