Example #1
0
VImage 
VImage::composite( VImage other, VipsBlendMode mode, VOption *options )
{
	VImage v[2] = { *this, other }; 
	std::vector<VImage> ivec( v, v + VIPS_NUMBER( v ) );
	int m[1] = { static_cast<int>( mode ) }; 
	std::vector<int> mvec( m, m + VIPS_NUMBER( m ) );

	return( composite( ivec, mvec, options ) ); 
}
Example #2
0
static int
mat2vips_get_header( matvar_t *var, VipsImage *im )
{
	int width, height, bands;
	VipsBandFormat format;
	VipsInterpretation interpretation; 
	int i;

	width = 1;
	height = 1;
	bands = 1;
	switch( var->rank ) {
	case 3:
		bands = var->dims[2];

	case 2:
		height = var->dims[1];

	case 1:
		width = var->dims[0];
		break;

	default:
		vips_error( "mat2vips", 
			_( "unsupported rank %d\n" ), var->rank );
		return( -1 );
	}

	if( bands > 1 )
		interpretation = VIPS_INTERPRETATION_MULTIBAND;
	else
		interpretation = VIPS_INTERPRETATION_B_W;

	for( i = 0; i < VIPS_NUMBER( mat2vips_formats ); i++ )
		if( mat2vips_formats[i][0] == var->class_type )
			break;
	if( i == VIPS_NUMBER( mat2vips_formats ) ) {
		vips_error( "mat2vips", _( "unsupported class type %d\n" ),
			var->class_type );
		return( -1 );
	}
	format = mat2vips_formats[i][1];

	vips_image_init_fields( im,
		 width, height, bands,
		 format,
		 VIPS_CODING_NONE, interpretation, 1.0, 1.0 );

	return( 0 );
}
Example #3
0
static void
vips_magick7_print_traits( Image *image ) 
{
	static const int trait_bits[] = {
		CopyPixelTrait,
		UpdatePixelTrait,
		BlendPixelTrait
	};
	static const char *trait_names[] = {
		"CopyPixelTrait",
		"UpdatePixelTrait",
		"BlendPixelTrait"
	};

	int b; 
	int i; 

	printf( "vips_magick7_print_traits: channel traits:\n" ); 
	for( b = 0; b < GetPixelChannels( image ); b++ ) { 
		PixelChannel channel = 
			GetPixelChannelChannel( image, b ); 
		PixelTrait traits = 
			GetPixelChannelTraits( image, channel );
		
		printf( "\t%d) ", b ); 
		for( i = 0; i < VIPS_NUMBER( trait_bits ); i++ )
			if( traits & trait_bits[i] )
				printf( "%s ", trait_names[i] ); 
		if( traits == 0 )
			printf( "undefined" ); 
		printf( "\n" ); 
	} 
}
Example #4
0
VImage 
VImage::bandjoin( VImage other, VOption *options )
{
	VImage v[2] = { *this, other }; 
	std::vector<VImage> vec( v, v + VIPS_NUMBER( v ) );

	return( bandjoin( vec, options ) ); 
}
Example #5
0
static int
vips_fits_set_header( VipsFits *fits, VipsImage *in )
{
	int status;
	int bitpix;
	int i;

	status = 0;

	fits->naxis = 3;
	fits->naxes[0] = in->Xsize;
	fits->naxes[1] = in->Ysize;
	fits->naxes[2] = in->Bands;

	for( i = 0; i < VIPS_NUMBER( fits2vips_formats ); i++ )
		if( fits2vips_formats[i][1] == in->BandFmt )
			break;
	if( i == VIPS_NUMBER( fits2vips_formats ) ) {
		vips_error( "fits", 
			_( "unsupported BandFmt %d\n" ), in->BandFmt );
		return( -1 );
	}
	bitpix = fits2vips_formats[i][0];
	fits->datatype = fits2vips_formats[i][2];

#ifdef VIPS_DEBUG
	VIPS_DEBUG_MSG( "naxis = %d\n", fits->naxis );
	for( i = 0; i < fits->naxis; i++ )
		VIPS_DEBUG_MSG( "%d) %lld\n", i, fits->naxes[i] );
	VIPS_DEBUG_MSG( "bitpix = %d\n", bitpix );
#endif /*VIPS_DEBUG*/

	if( fits_create_imgll( fits->fptr, bitpix, fits->naxis, 
		fits->naxes, &status ) ) {
		vips_fits_error( status );
		return( -1 );
	}

	if( vips_image_map( in,
		(VipsImageMapFn) vips_fits_write_meta, fits ) )
		return( -1 );

	return( 0 );
}
Example #6
0
static void
vips_magick7_print_image_type( Image *image )
{
	static const int image_types[] = {
		UndefinedType,
		BilevelType,
		GrayscaleType,
		GrayscaleAlphaType, 
		PaletteType,
		PaletteAlphaType,
		TrueColorType,
		TrueColorAlphaType,
		ColorSeparationType,
		ColorSeparationAlphaType,
		OptimizeType,
		PaletteBilevelAlphaType
	};

	static const char *image_type_names[] = {
		"UndefinedType",
		"BilevelType",
		"GrayscaleType",
		"GrayscaleAlphaType", 
		"PaletteType",
		"PaletteAlphaType",
		"TrueColorType",
		"TrueColorAlphaType",
		"ColorSeparationType",
		"ColorSeparationAlphaType",
		"OptimizeType",
		"PaletteBilevelAlphaType"
	};

	int i;

	for( i = 0; i < VIPS_NUMBER( image_types ); i++ ) 
		if( GetImageType( image ) == image_types[i] ) {
			printf( "\t%s\n", image_type_names[i] );
			break;
		}
	if( i == VIPS_NUMBER( image_types ) )
		printf( "\tunknown GetImageType()\n" ); 
}
Example #7
0
int
vips__foreign_nifti_BandFmt2datatype( VipsBandFormat fmt )
{
	int i;

	for( i = 0; i < VIPS_NUMBER( vips_foreign_nifti_DT2Vips ); i++ )
		if( vips_foreign_nifti_DT2Vips[i].fmt == fmt )
			return( vips_foreign_nifti_DT2Vips[i].datatype );

	return( -1 );
}
Example #8
0
VipsBandFormat
vips__foreign_nifti_datatype2BandFmt( int datatype )
{
	int i;

	for( i = 0; i < VIPS_NUMBER( vips_foreign_nifti_DT2Vips ); i++ )
		if( vips_foreign_nifti_DT2Vips[i].datatype == datatype )
			return( vips_foreign_nifti_DT2Vips[i].fmt );

	return( VIPS_FORMAT_NOTSET );
}
Example #9
0
/* Turn a vips7 type name to an enum.
 */
static VipsWrap7Type
vips_wrap7_lookup_type( im_arg_type type )
{
	int i;

	for( i = 0; i < VIPS_NUMBER( vips_wrap7_supported ); i++ )
		if( strcmp( type, vips_wrap7_supported[i] ) == 0 )
			return( (VipsWrap7Type) i );

	return( VIPS_WRAP7_NONE );
}
Example #10
0
static struct dsr *
read_header( const char *header )
{
	struct dsr *d;
	unsigned int len;

	if( !(d = (struct dsr *) vips__file_read_name( header, NULL, &len )) )
		return( NULL );

	if( len != sizeof( struct dsr ) ) {
		vips_error( "analyze2vips", 
			"%s", _( "header file size incorrect" ) );
		vips_free( d );
		return( NULL );
	}

	/* Ouch! Should check at configure time I guess.
	 */
	g_assert( sizeof( struct dsr ) == 348 );

	/* dsr headers are always SPARC byte order (MSB first). Do we need to 
	 * swap?
	 */
	if( !vips_amiMSBfirst() ) {
		int i;

		for( i = 0; i < VIPS_NUMBER( dsr_header ); i++ ) {
			unsigned char *p;


			switch( dsr_header[i].type ) {
			case SHORT:
				p = &G_STRUCT_MEMBER( unsigned char, d, 
					dsr_header[i].offset );
				vips__copy_2byte( TRUE, p, p );
				break;

			case INT:
			case FLOAT: 
				p = &G_STRUCT_MEMBER( unsigned char, d, 
					dsr_header[i].offset );
				vips__copy_4byte( TRUE, p, p );
				break;

			case BYTE:
			case STRING:
				break;

			default:
				g_assert( 0 );
			}
		}
	}
Example #11
0
int
vips__ppm_isppm( const char *filename )
{
	VipsPel buf[3];

	if( vips__get_bytes( filename, buf, 2 ) ) {
		int i;

		buf[2] = '\0';
		for( i = 0; i < VIPS_NUMBER( magic_names ); i++ )
			if( strcmp( (char *) buf, magic_names[i] ) == 0 )
				return( TRUE );
	}

	return( 0 );
}
Example #12
0
/**
 * vips_image_get:
 * @image: image to get the field from from
 * @field: the name to give the metadata
 * @value_copy: (transfer full) (out caller-allocates): the %GValue is copied into this
 *
 * Fill @value_copy with a copy of the header field. @value_copy must be zeroed 
 * but uninitialised.
 *
 * This will return -1 and add a message to the error buffer if the field
 * does not exist. Use vips_image_get_typeof() to test for the 
 * existence of a field first if you are not certain it will be there.
 *
 * For example, to read a double from an image (though of course you would use
 * vips_image_get_double() in practice):
 *
 * |[
 * GValue value = { 0 };
 * double d;
 *
 * if (vips_image_get (image, field, &amp;value))
 *   return -1;
 *
 * if (G_VALUE_TYPE (&amp;value) != G_TYPE_DOUBLE) {
 *   vips_error( "mydomain", 
 *     _("field \"%s\" is of type %s, not double"),
 *     field, 
 *     g_type_name (G_VALUE_TYPE (&amp;value)));
 *   g_value_unset (&amp;value);
 *   return -1;
 * }
 *
 * d = g_value_get_double (&amp;value);
 * g_value_unset (&amp;value);
 * ]|
 *
 * See also: vips_image_get_typeof(), vips_image_get_double().
 *
 * Returns: (skip): 0 on success, -1 otherwise.
 */
int
vips_image_get( const VipsImage *image, const char *field, GValue *value_copy )
{
	int i;
	VipsMeta *meta;

	g_assert( field );
	g_assert( value_copy );

	for( i = 0; i < VIPS_NUMBER( int_field ); i++ ) 
		if( strcmp( field, int_field[i].field ) == 0 ) {
			g_value_init( value_copy, G_TYPE_INT );
			g_value_set_int( value_copy, 
				G_STRUCT_MEMBER( int, image, 
					int_field[i].offset ) );
			return( 0 );
		}
Example #13
0
/**
 * vips_colourspace_issupported:
 * @image: input image
 *
 * Test if @image is in a colourspace that vips_colourspace() can process. For
 * example, #VIPS_INTERPRETATION_RGB images are not in a well-defined 
 * colourspace, but #VIPS_INTERPRETATION_sRGB ones are.
 *
 * Returns: %TRUE if @image is in a supported colourspace.
 */
gboolean
vips_colourspace_issupported( const VipsImage *image )
{
	VipsInterpretation interpretation;
	int i;

	/* Treat RGB as sRGB. If you want some other treatment,
	 * you'll need to use the icc funcs.
	 */
	interpretation = vips_image_guess_interpretation( image );
	if( interpretation == VIPS_INTERPRETATION_RGB )
		interpretation = VIPS_INTERPRETATION_sRGB;

	for( i = 0; i < VIPS_NUMBER( vips_colour_routes ); i++ )
		if( vips_colour_routes[i].from == interpretation )
			return( TRUE );

	return( FALSE );
}
Example #14
0
void *
vips__foreign_nifti_map( VipsNiftiMapFn fn, void *a, void *b )
{
	int i;
	void *result;

	for( i = 0; i < VIPS_NUMBER( vips_foreign_nifti_fields ); i++ ) {
		GValue value = { 0 };

		g_value_init( &value, vips_foreign_nifti_fields[i].type );
		result = fn( vips_foreign_nifti_fields[i].name, &value,
			vips_foreign_nifti_fields[i].offset, a, b );
		g_value_unset( &value );

		if( result )
			return( result );
	}

	return( NULL );
}
Example #15
0
static void
print_dsr( struct dsr *d )
{
	int i;

	for( i = 0; i < VIPS_NUMBER( dsr_header ); i++ ) {
		printf( "%s = ", dsr_header[i].name );

		switch( dsr_header[i].type ) {
		case BYTE:
			printf( "%d\n", G_STRUCT_MEMBER( char, d, 
				dsr_header[i].offset ) );
			break;

		case SHORT:
			printf( "%d\n", G_STRUCT_MEMBER( short, d, 
				dsr_header[i].offset ) );
			break;

		case INT:
			printf( "%d\n", G_STRUCT_MEMBER( int, d, 
				dsr_header[i].offset ) );
			break;

		case FLOAT: 
			printf( "%g\n", G_STRUCT_MEMBER( float, d, 
				dsr_header[i].offset ) );
			break;

		case STRING:
			printf( "\"%s\"\n", getstr( dsr_header[i].len, 
				&G_STRUCT_MEMBER( char, d, 
					dsr_header[i].offset ) ) );
			break;

		default:
			g_assert( 0 );
		}
	}
}
Example #16
0
static int
vips_colourspace_build( VipsObject *object )
{
	VipsColourspace *colourspace = (VipsColourspace *) object; 

	int i, j;
	VipsImage *x;
	VipsImage **t = (VipsImage **) 
		vips_object_local_array( object, 1 );
	VipsImage **pipe = (VipsImage **) 
		vips_object_local_array( object, MAX_STEPS );

	VipsInterpretation interpretation;

	/* Verify that all input args have been set.
	 */
	if( VIPS_OBJECT_CLASS( vips_colourspace_parent_class )->
		build( object ) )
		return( -1 );

	x = colourspace->in;

	/* Unpack radiance-coded images. We can't use interpretation for this,
	 * since rad images can be scRGB or XYZ.
	 */
	if( x->Coding == VIPS_CODING_RAD ) {
		if( vips_rad2float( x, &t[0], NULL ) )
			return( -1 );
		x = t[0]; 
	}

	if( vips_object_argument_isset( object, "source_space" ) )
		interpretation = colourspace->source_space;
	else
		interpretation = vips_image_guess_interpretation( x );

	/* Treat RGB as sRGB. If you want some other treatment,
	 * you'll need to use the icc funcs.
	 */
	if( interpretation == VIPS_INTERPRETATION_RGB )
		interpretation = VIPS_INTERPRETATION_sRGB;

	/* No conversion necessary.
	 */
	if( interpretation == colourspace->space ) {
		g_object_set( colourspace, "out", vips_image_new(), NULL ); 

		return( vips_image_write( colourspace->in, colourspace->out ) );
	}

	for( i = 0; i < VIPS_NUMBER( vips_colour_routes ); i++ )
		if( vips_colour_routes[i].from == interpretation &&
			vips_colour_routes[i].to == colourspace->space )
			break;
	if( i == VIPS_NUMBER( vips_colour_routes ) ) {
		vips_error( "vips_colourspace", 
			_( "no known route between '%s' and '%s'" ),
			vips_enum_nick( VIPS_TYPE_INTERPRETATION, 
				interpretation ),
			vips_enum_nick( VIPS_TYPE_INTERPRETATION, 
				colourspace->space ) );
		return( -1 );
	}

	for( j = 0; vips_colour_routes[i].route[j]; j++ ) {
		if( vips_colour_routes[i].route[j]( x, &pipe[j], NULL ) ) 
			return( -1 );
		x = pipe[j];
	}

	g_object_set( colourspace, "out", vips_image_new(), NULL ); 
	if( vips_image_write( x, colourspace->out ) )
		return( -1 );

	return( 0 );
}
Example #17
0
		read->config.options.use_scaling = 1;
		read->config.options.scaled_width = read->width;
		read->config.options.scaled_height = read->height; 
	}

	return( read );
}

/* Map vips metadata names to webp names.
 */
const VipsWebPNames vips__webp_names[] = {
	{ VIPS_META_ICC_NAME, "ICCP", 0x20 },
	{ VIPS_META_XMP_NAME, "XMP ", 0x04 },
	{ VIPS_META_EXIF_NAME, "EXIF", 0x08 }
};
const int vips__n_webp_names = VIPS_NUMBER( vips__webp_names ); 

static int
read_header( Read *read, VipsImage *out )
{
	vips_image_init_fields( out,
		read->width, read->height,
		read->config.input.has_alpha ? 4 : 3,
		VIPS_FORMAT_UCHAR, VIPS_CODING_NONE,
		VIPS_INTERPRETATION_sRGB,
		1.0, 1.0 );

	vips_image_pipelinev( out, VIPS_DEMAND_STYLE_THINSTRIP, NULL );

#ifdef HAVE_LIBWEBPMUX
{
Example #18
0
static int
vips_similarity_build( VipsObject *object )
{
	VipsResample *resample = VIPS_RESAMPLE( object );
	VipsSimilarity *similarity = (VipsSimilarity *) object;
	VipsImage **t = (VipsImage **) 
		vips_object_local_array( object, 4 );

	gboolean handled;

	if( VIPS_OBJECT_CLASS( vips_similarity_parent_class )->build( object ) )
		return( -1 );

	handled = FALSE;

	/* Use vips_reduce(), if we can.
	 */
	if( similarity->interpolate &&
		similarity->angle == 0.0 &&
		similarity->idx == 0.0 &&
		similarity->idy == 0.0 &&
		similarity->odx == 0.0 &&
		similarity->ody == 0.0 ) {
		const char *nickname = VIPS_OBJECT_GET_CLASS( 
			similarity->interpolate )->nickname;

		int i; 

		for( i = 0; i < VIPS_NUMBER( vips_similarity_kernel ); i++ ) {
			VipsInterpolateKernel *ik = &vips_similarity_kernel[i];

			if( strcmp( nickname, ik->nickname ) == 0 ) {
				if( vips_reduce( resample->in, &t[0], 
					1.0 / similarity->scale, 
					1.0 / similarity->scale, 
					"kernel", ik->kernel,
					NULL ) )
					return( -1 );

				handled = TRUE;
				break;
			}
		}
	}

	if( !handled ) { 
		double a = similarity->scale * 
			cos( VIPS_RAD( similarity->angle ) ); 
		double b = similarity->scale * 
			-sin( VIPS_RAD( similarity->angle ) );
		double c = -b;
		double d = a;

		if( vips_affine( resample->in, &t[0], a, b, c, d, 
			"interpolate", similarity->interpolate,
			"odx", similarity->odx,
			"ody", similarity->ody,
			"idx", similarity->idx,
			"idy", similarity->idy,
			NULL ) )
			return( -1 );
	}

	if( vips_image_write( t[0], resample->out ) )
		return( -1 ); 

	return( 0 );
}
Example #19
0
File: copy.c Project: kjell/libvips
static int
vips_copy_build( VipsObject *object )
{
    VipsConversion *conversion = VIPS_CONVERSION( object );
    VipsCopy *copy = (VipsCopy *) object;

    int i;

    if( VIPS_OBJECT_CLASS( vips_copy_parent_class )->build( object ) )
        return( -1 );

    if( vips_image_pio_input( copy->in ) )
        return( -1 );

    if( vips_image_pipelinev( conversion->out,
                              VIPS_DEMAND_STYLE_THINSTRIP, copy->in, NULL ) )
        return( -1 );

    /* Use props to adjust header fields.
     */
    for( i = 0; i < VIPS_NUMBER( vips_copy_names ); i++ ) {
        const char *name = vips_copy_names[i];

        GParamSpec *pspec;
        VipsArgumentClass *argument_class;
        VipsArgumentInstance *argument_instance;

        if( vips_object_get_argument( object, name,
                                      &pspec, &argument_class, &argument_instance ) )
            return( -1 );

        if( argument_instance->assigned ) {
            GType type = G_PARAM_SPEC_VALUE_TYPE( pspec );
            GValue value = { 0, };

            g_value_init( &value, type );
            g_object_get_property( G_OBJECT( object ),
                                   name, &value );

#ifdef VIPS_DEBUG
            {
                char *str;

                str = g_strdup_value_contents( &value );
                printf( "vips_copy_build: %s = %s\n", name, str );
                g_free( str );
            }
#endif /* VIPS_DEBUG */

            g_object_set_property( G_OBJECT( conversion->out ),
                                   name, &value );
            g_value_unset( &value );
        }
    }

    if( vips_image_generate( conversion->out,
                             vips_start_one, vips_copy_gen, vips_stop_one,
                             copy->in, copy ) )
        return( -1 );

    return( 0 );
}
Example #20
0
static void
vips_magick7_print_channel_names( Image *image )
{
	static const int pixel_channels[] = {
		UndefinedPixelChannel, 
		RedPixelChannel, 
		CyanPixelChannel, 
		GrayPixelChannel, 
		LPixelChannel, 
		LabelPixelChannel, 
		YPixelChannel, 
		aPixelChannel, 
		GreenPixelChannel, 
		MagentaPixelChannel, 
		CbPixelChannel, 
		bPixelChannel, 
		BluePixelChannel, 
		YellowPixelChannel, 
		CrPixelChannel, 
		BlackPixelChannel, 
		AlphaPixelChannel, 
		IndexPixelChannel, 
		ReadMaskPixelChannel, 
		WriteMaskPixelChannel, 
		MetaPixelChannel, 
		IntensityPixelChannel, 
		CompositePixelChannel, 
		SyncPixelChannel
	};
	static const char *pixel_channel_names[] = {
		"UndefinedPixelChannel", 
		"RedPixelChannel", 
		"CyanPixelChannel", 
		"GrayPixelChannel", 
		"LPixelChannel", 
		"LabelPixelChannel", 
		"YPixelChannel", 
		"aPixelChannel", 
		"GreenPixelChannel", 
		"MagentaPixelChannel", 
		"CbPixelChannel", 
		"bPixelChannel", 
		"BluePixelChannel", 
		"YellowPixelChannel", 
		"CrPixelChannel", 
		"BlackPixelChannel", 
		"AlphaPixelChannel", 
		"IndexPixelChannel", 
		"ReadMaskPixelChannel", 
		"WriteMaskPixelChannel", 
		"MetaPixelChannel", 
		"IntensityPixelChannel", 
		"CompositePixelChannel", 
		"SyncPixelChannel", 
	};

	int b; 
	int i; 

	printf( "vips_magick7_print_channel_names: channel names:\n" ); 
	for( b = 0; b < GetPixelChannels( image ); b++ ) { 
		PixelChannel channel = 
			GetPixelChannelChannel( image, b ); 
		
		printf( "\t%d) ", b ); 
		for( i = 0; i < VIPS_NUMBER( pixel_channels ); i++ )
			/* Don't break on found, many channel names repeat.
			 */
			if( channel == pixel_channels[i] ) 
				printf( "%s ", pixel_channel_names[i] );
		printf( "\n" ); 
	} 
}
Example #21
0
static int
read_header( FILE *fp, VipsImage *out, int *bits, int *ascii, int *msb_first )
{
	int width, height, bands; 
	VipsBandFormat format;
	VipsInterpretation interpretation; 
	int index;
	char buf[MAX_THING];

	/* Characteristics, indexed by ppm type.
	 */
	static int lookup_bits[] = {
		1, 8, 8, 1, 8, 8, 32, 32
	};
	static int lookup_bands[] = {
		1, 1, 3, 1, 1, 3, 3, 1
	};
	static int lookup_ascii[] = {
		1, 1, 1, 0, 0, 0, 0, 0
	};

	/* Read in the magic number.
	 */
	buf[0] = fgetc( fp );
	buf[1] = fgetc( fp );
	buf[2] = '\0';

	for( index = 0; index < VIPS_NUMBER( magic_names ); index++ )
		if( strcmp( magic_names[index], buf ) == 0 ) 
			break;
	if( index == VIPS_NUMBER( magic_names ) ) {
		vips_error( "ppm2vips", "%s", _( "bad magic number" ) );
		return( -1 );
	}
	*bits = lookup_bits[index];
	bands = lookup_bands[index];
	*ascii = lookup_ascii[index];

	/* Default ... can be changed below for PFM images.
	 */
	*msb_first = 0;

	/* Read in size.
	 */
	if( read_int( fp, &width ) ||
		read_int( fp, &height ) )
		return( -1 );

	/* Read in max value / scale for >1 bit images.
	 */
	if( *bits > 1 ) {
		if( index == 6 || index == 7 ) {
			float scale;

			if( read_float( fp, &scale ) )
				return( -1 );

			/* Scale > 0 means big-endian.
			 */
			*msb_first = scale > 0;
			vips_image_set_double( out, 
				"pfm-scale", fabs( scale ) );
		}
		else {
			int max_value;

			if( read_int( fp, &max_value ) )
				return( -1 );

			if( max_value > 255 )
				*bits = 16;
			if( max_value > 65535 )
				*bits = 32;
		}
	}

	/* For binary images, there is always exactly 1 more whitespace
	 * character before the data starts.
	 */
	if( !*ascii && !isspace( fgetc( fp ) ) ) {
		vips_error( "ppm2vips", "%s", 
			_( "not whitespace before start of binary data" ) );
		return( -1 );
	}

	/* Choose a VIPS bandfmt.
	 */
	switch( *bits ) {
	case 1:
	case 8:
		format = VIPS_FORMAT_UCHAR;
		break;

	case 16:
		format = VIPS_FORMAT_USHORT;
		break;

	case 32:
		if( index == 6 || index == 7 )
			format = VIPS_FORMAT_FLOAT;
		else
			format = VIPS_FORMAT_UINT;
		break;

	default:
		g_assert( 0 );

		/* Keep -Wall happy.
		 */
		return( 0 );
	}

	if( bands == 1 ) {
		if( format == VIPS_FORMAT_USHORT )
			interpretation = VIPS_INTERPRETATION_GREY16;
		else
			interpretation = VIPS_INTERPRETATION_B_W;
	}
	else {
		if( format == VIPS_FORMAT_USHORT )
			interpretation = VIPS_INTERPRETATION_RGB16;
		else if( format == VIPS_FORMAT_UINT )
			interpretation = VIPS_INTERPRETATION_RGB;
		else 
			interpretation = VIPS_INTERPRETATION_sRGB;
	}

	vips_image_init_fields( out,
		width, height, bands, format, 
		VIPS_CODING_NONE, interpretation, 1.0, 1.0 );

	return( 0 );
}
Example #22
0
static int
vips_fits_get_header( VipsFits *fits, VipsImage *out )
{
	int status;
	int bitpix;

	int width, height, bands, format, type;
	int keysexist;
	int i;

	status = 0;

	if( fits_get_img_paramll( fits->fptr, 
		10, &bitpix, &fits->naxis, fits->naxes, &status ) ) {
		vips_fits_error( status );
		return( -1 );
	}

#ifdef VIPS_DEBUG
	VIPS_DEBUG_MSG( "naxis = %d\n", fits->naxis );
	for( i = 0; i < fits->naxis; i++ )
		VIPS_DEBUG_MSG( "%d) %lld\n", i, fits->naxes[i] );
#endif /*VIPS_DEBUG*/

	height = 1;
	bands = 1;
	switch( fits->naxis ) {
	/* If you add more dimensions here, adjust data read below. See also
	 * the definition of MAX_DIMENSIONS above.
	 */
	case 10:
	case 9:
	case 8:
	case 7:
	case 6:
	case 5:
	case 4:
		for( i = fits->naxis; i > 3; i-- )
			if( fits->naxes[i - 1] != 1 ) {
				vips_error( "fits", 
					"%s", _( "dimensions above 3 "
					"must be size 1" ) );
				return( -1 );
			}

	case 3:
		bands = fits->naxes[2];

	case 2:
		height = fits->naxes[1];

	case 1:
		width = fits->naxes[0];
		break;

	default:
		vips_error( "fits", _( "bad number of axis %d" ), fits->naxis );
		return( -1 );
	}

	/* Are we in one-band mode?
	 */
	if( fits->band_select != -1 )
		bands = 1;

	/* Get image format. We want the 'raw' format of the image, our caller
	 * can convert using the meta info if they want.
	 */
	for( i = 0; i < VIPS_NUMBER( fits2vips_formats ); i++ )
		if( fits2vips_formats[i][0] == bitpix )
			break;
	if( i == VIPS_NUMBER( fits2vips_formats ) ) {
		vips_error( "fits", _( "unsupported bitpix %d\n" ),
			bitpix );
		return( -1 );
	}
	format = fits2vips_formats[i][1];
	fits->datatype = fits2vips_formats[i][2];

	if( bands == 1 ) {
		if( format == VIPS_FORMAT_USHORT )
			type = VIPS_INTERPRETATION_GREY16;
		else
			type = VIPS_INTERPRETATION_B_W;
	}
	else if( bands == 3 ) {
		if( format == VIPS_FORMAT_USHORT )
			type = VIPS_INTERPRETATION_RGB16;
		else
			type = VIPS_INTERPRETATION_RGB;
	}
	else
		type = VIPS_INTERPRETATION_MULTIBAND;

	vips_image_pipelinev( out, VIPS_DEMAND_STYLE_SMALLTILE, NULL );
	vips_image_init_fields( out,
		 width, height, bands,
		 format,
		 VIPS_CODING_NONE, type, 1.0, 1.0 );

	/* Read all keys into meta.
	 */
	if( fits_get_hdrspace( fits->fptr, &keysexist, NULL, &status ) ) {
		vips_fits_error( status );
		return( -1 );
	}

	for( i = 0; i < keysexist; i++ ) {
		char record[81];
		char vipsname[100];

		if( fits_read_record( fits->fptr, i + 1, record, &status ) ) {
			vips_fits_error( status );
			return( -1 );
		}

		VIPS_DEBUG_MSG( "fits2vips: setting meta on vips image:\n" );
		VIPS_DEBUG_MSG( " record == \"%s\"\n", record );

		/* FITS lets keys repeat. For example, HISTORY appears many
		 * times, each time with a fresh line of history attached. We
		 * have to include the key index in the vips name we assign.
		 */

		vips_snprintf( vipsname, 100, "fits-%d", i );
		vips_image_set_string( out, vipsname, record );
	}

	return( 0 );
}