int main( int argc, char **argv ) { GOptionContext *context; GError *error = NULL; IMAGE *im; unsigned char header[IM_SIZEOF_HEADER]; if( im_init_world( argv[0] ) ) error_exit( "%s", _( "unable to start VIPS" ) ); textdomain( GETTEXT_PACKAGE ); setlocale( LC_ALL, "" ); context = g_option_context_new( _( "vipsfile - edit vipsfile header" ) ); g_option_context_add_main_entries( context, entries, GETTEXT_PACKAGE ); g_option_context_add_group( context, im_get_option_group() ); if( !g_option_context_parse( context, &argc, &argv, &error ) ) { if( error ) { fprintf( stderr, "%s\n", error->message ); g_error_free( error ); } exit( -1 ); } if( argc != 2 ) { fprintf( stderr, _( "usage: %s [OPTION...] vipsfile\n" ), g_get_prgname() ); exit( -1 ); } if( !(im = im_init( argv[1] )) || (im->fd = im__open_image_file( im->filename )) == -1 ) error_exit( _( "could not open image %s" ), argv[1] ); if( read( im->fd, header, IM_SIZEOF_HEADER ) != IM_SIZEOF_HEADER || im__read_header_bytes( im, header ) ) error_exit( _( "could not read VIPS header for %s" ), im->filename ); if( xsize ) parse_pint( xsize, &im->Xsize ); if( ysize ) parse_pint( ysize, &im->Ysize ); if( bands ) parse_pint( bands, &im->Bands ); if( format ) { if( (im->BandFmt = im_char2BandFmt( format )) < 0 ) error_exit( _( "bad format %s" ), format ); im->Bbits = im_bits_of_fmt( im->BandFmt ); } if( type ) { if( (im->Type = im_char2Type( type )) < 0 ) error_exit( _( "bad type %s" ), type ); } if( coding ) { if( (im->Coding = im_char2Coding( coding )) < 0 ) error_exit( _( "bad coding %s" ), coding ); } if( xres ) im->Xres = atof( xres ); if( yres ) im->Yres = atof( yres ); if( xoffset ) im->Xoffset = atoi( xoffset ); if( yoffset ) im->Yoffset = atoi( yoffset ); if( lseek( im->fd, 0, SEEK_SET ) == (off_t) -1 ) error_exit( _( "could not seek on %s" ), im->filename ); if( im__write_header_bytes( im, header ) || im__write( im->fd, header, IM_SIZEOF_HEADER ) ) error_exit( _( "could not write to %s" ), im->filename ); if( setext ) { char *xml; unsigned int size; if( !(xml = im__file_read( stdin, "stdin", &size )) ) error_exit( "%s", _( "could not get ext data" ) ); /* Strip trailing whitespace ... we can get stray \n at the * end, eg. "echo | edvips --setext fred.v". */ while( size > 0 && isspace( xml[size - 1] ) ) size -= 1; if( im__write_extension_block( im, xml, size ) ) error_exit( "%s", _( "could not set extension" ) ); im_free( xml ); } im_close( im ); vips_shutdown(); return( 0 ); }
/** * im_binfile: * @name: filename to open * @xsize: image width * @ysize: image height * @bands: image bands (or bytes per pixel) * @offset: bytes to skip at start of file * * This function maps the named file and returns an #IMAGE you can use to * read it. * * It returns an 8-bit image with @bands bands. If the image is not 8-bit, use * im_copy_set() to transform the descriptor after loading it. * * See also: im_copy_set(), im_raw2vips(), im_open(). * * Returns: the new #IMAGE, or NULL on error. */ IMAGE * im_binfile( const char *name, int xsize, int ysize, int bands, int offset ) { IMAGE *im; gint64 psize; gint64 rsize; /* Check parameters. */ if( xsize <= 0 || ysize <= 0 || bands <= 0 ) { im_error( "im_binfile", "%s", _( "bad parameters" ) ); return( NULL ); } /* Make new output image for us. */ if( !(im = im_init( name )) ) return( NULL ); if( (im->fd = im__open_image_file( name )) == -1 ) { im_close( im ); return( NULL ); } im->dtype = IM_OPENIN; im->sizeof_header = offset; /* Predict file size. */ psize = (gint64) xsize * ysize * bands + offset; /* Read the real file length and check against what we think * the size should be. */ if( (rsize = im_file_length( im->fd )) == -1 ) { im_close( im ); return( NULL ); } im->file_length = rsize; /* Very common, so special message. */ if( psize > rsize ) { im_error( "im_binfile", _( "unable to open %s: " "file has been truncated" ), im->filename ); im_close( im ); return( NULL ); } /* Just wierd. Only print a warning for this, since we should * still be able to process it without coredumps. */ if( psize < rsize ) im_warn( "im_binfile", _( "%s is longer than expected" ), im->filename ); /* Set header fields. */ im->Xsize = xsize; im->Ysize = ysize; im->Bands = bands; /* Set others to standard values. */ im->BandFmt = IM_BANDFMT_UCHAR; im->Bbits = im_bits_of_fmt( im->BandFmt ); im->Coding = IM_CODING_NONE; if( bands == 1 ) im->Type = IM_TYPE_B_W; else if( bands == 3 ) im->Type = IM_TYPE_RGB; else im->Type = IM_TYPE_MULTIBAND; im->Xres = 1.0; im->Yres = 1.0; im->Length = 0; im->Compression = 0; im->Level = 0; im->Xoffset = 0; im->Yoffset = 0; /* Init others too. */ im->dhint = IM_THINSTRIP; return( im ); }
int main( int argc, char **argv ) { GOptionContext *context; GOptionGroup *main_group; GError *error = NULL; IMAGE *im; unsigned char header[IM_SIZEOF_HEADER]; if( VIPS_INIT( argv[0] ) ) vips_error_exit( "%s", _( "unable to start VIPS" ) ); textdomain( GETTEXT_PACKAGE ); setlocale( LC_ALL, "" ); context = g_option_context_new( _( "vipsedit - edit vips file header" ) ); main_group = g_option_group_new( NULL, NULL, NULL, NULL, NULL ); g_option_group_add_entries( main_group, entries ); vips_add_option_entries( main_group ); g_option_group_set_translation_domain( main_group, GETTEXT_PACKAGE ); g_option_context_set_main_group( context, main_group ); if( !g_option_context_parse( context, &argc, &argv, &error ) ) { if( error ) { fprintf( stderr, "%s\n", error->message ); g_error_free( error ); } exit( -1 ); } if( argc != 2 ) { fprintf( stderr, _( "usage: %s [OPTION...] vips-file\n" ), g_get_prgname() ); exit( -1 ); } if( !(im = im_init( argv[1] )) || (im->fd = im__open_image_file( im->filename )) == -1 ) error_exit( _( "could not open image %s" ), argv[1] ); if( read( im->fd, header, IM_SIZEOF_HEADER ) != IM_SIZEOF_HEADER || im__read_header_bytes( im, header ) ) error_exit( _( "could not read VIPS header for %s" ), im->filename ); if( endian ) { if( strcmp( endian, "little" ) == 0 ) im->magic = VIPS_MAGIC_INTEL; else if( strcmp( endian, "big" ) == 0 ) im->magic = VIPS_MAGIC_SPARC; else error_exit( _( "bad endian-ness %s, " "should be 'big' or 'little'" ), endian ); } if( xsize ) parse_pint( xsize, &im->Xsize ); if( ysize ) parse_pint( ysize, &im->Ysize ); if( bands ) parse_pint( bands, &im->Bands ); if( format ) { VipsBandFormat f; if( (f = im_char2BandFmt( format )) < 0 ) error_exit( _( "bad format %s" ), format ); im->BandFmt = f; im->Bbits = im_bits_of_fmt( f ); } if( interpretation ) { VipsInterpretation i; if( (i = im_char2Type( interpretation )) < 0 ) error_exit( _( "bad interpretation %s" ), interpretation ); im->Type = i; } if( coding ) { VipsCoding c; if( (c = im_char2Coding( coding )) < 0 ) error_exit( _( "bad coding %s" ), coding ); im->Coding = c; } if( xres ) im->Xres = atof( xres ); if( yres ) im->Yres = atof( yres ); if( xoffset ) im->Xoffset = atoi( xoffset ); if( yoffset ) im->Yoffset = atoi( yoffset ); if( lseek( im->fd, 0, SEEK_SET ) == (off_t) -1 ) error_exit( _( "could not seek on %s" ), im->filename ); if( im__write_header_bytes( im, header ) || im__write( im->fd, header, IM_SIZEOF_HEADER ) ) error_exit( _( "could not write to %s" ), im->filename ); if( setext ) { char *xml; size_t size; if( !(xml = im__file_read( stdin, "stdin", &size )) ) error_exit( "%s", _( "could not get ext data" ) ); /* Strip trailing whitespace ... we can get stray \n at the * end, eg. "echo | editvips --setext fred.v". */ while( size > 0 && isspace( xml[size - 1] ) ) size -= 1; if( im__write_extension_block( im, xml, size ) ) error_exit( "%s", _( "could not set extension" ) ); im_free( xml ); } im_close( im ); vips_shutdown(); return( 0 ); }