2009-08-16 17:00:08 +02:00
|
|
|
/* modify vips file header! - useful for setting resolution, coding...
|
2020-06-18 14:21:43 +02:00
|
|
|
* very dangerous!
|
|
|
|
*
|
|
|
|
* no way of setting non-used codes in variables like newxres
|
|
|
|
* so need flags to show new parameter has been set.. boring
|
|
|
|
* Copyright K.Martinez 30/6/93
|
|
|
|
*
|
|
|
|
* 29/7/93 JC
|
|
|
|
* - format added
|
|
|
|
* - ==0 added to strcmp!
|
|
|
|
* 17/11/94 JC
|
|
|
|
* - new header fields added
|
|
|
|
* 21/10/04
|
|
|
|
* - more header updates
|
|
|
|
* 22/8/05
|
|
|
|
* - less-stupid-ified
|
|
|
|
* 20/9/05
|
|
|
|
* - rewritten with glib option parser, ready for xml options to go in
|
|
|
|
* 18/6/20 kleisauke
|
|
|
|
* - avoid using vips7 symbols
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This file is part of VIPS.
|
|
|
|
|
|
|
|
VIPS is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2013-03-07 06:40:19 +01:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
02110-1301 USA
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
2022-02-27 16:27:28 +01:00
|
|
|
#include <glib/gi18n.h>
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.h>
|
2022-06-12 13:22:36 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2009-08-16 17:00:08 +02:00
|
|
|
#include <unistd.h>
|
2022-06-12 13:22:36 +02:00
|
|
|
#endif /*HAVE_UNISTD_H*/
|
2010-05-25 18:46:03 +02:00
|
|
|
#include <locale.h>
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
#include <vips/vips.h>
|
|
|
|
#include <vips/internal.h>
|
2009-10-05 15:00:37 +02:00
|
|
|
#include <vips/debug.h>
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2018-06-01 17:02:09 +02:00
|
|
|
/* We have to represent all header fields as char * so we can spot unset args
|
2009-08-16 17:00:08 +02:00
|
|
|
* safely.
|
|
|
|
*/
|
|
|
|
static char *xsize = NULL;
|
|
|
|
static char *ysize = NULL;
|
|
|
|
static char *bands = NULL;
|
|
|
|
static char *format = NULL;
|
2012-01-27 18:14:59 +01:00
|
|
|
static char *interpretation = NULL;
|
2009-08-16 17:00:08 +02:00
|
|
|
static char *coding = NULL;
|
|
|
|
static char *xres = NULL;
|
|
|
|
static char *yres = NULL;
|
|
|
|
static char *xoffset = NULL;
|
|
|
|
static char *yoffset = NULL;
|
2011-12-01 14:17:40 +01:00
|
|
|
static char *endian = NULL;
|
2009-08-16 17:00:08 +02:00
|
|
|
static gboolean setext = FALSE;
|
2021-06-15 11:33:42 +02:00
|
|
|
static gboolean version = FALSE;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
static GOptionEntry entries[] = {
|
2011-12-01 14:17:40 +01:00
|
|
|
{ "endian", 'n', 0, G_OPTION_ARG_STRING, &endian,
|
|
|
|
N_( "tag file as big or little-endian" ), NULL },
|
2011-12-01 19:47:36 +01:00
|
|
|
{ "width", 'w', 0, G_OPTION_ARG_STRING, &xsize,
|
|
|
|
N_( "set width to N pixels" ), "N" },
|
|
|
|
{ "height", 'h', 0, G_OPTION_ARG_STRING, &ysize,
|
|
|
|
N_( "set height to N pixels" ), "N" },
|
2009-08-16 17:00:08 +02:00
|
|
|
{ "bands", 'b', 0, G_OPTION_ARG_STRING, &bands,
|
|
|
|
N_( "set Bands to N" ), "N" },
|
|
|
|
{ "format", 'f', 0, G_OPTION_ARG_STRING, &format,
|
2011-07-22 16:56:31 +02:00
|
|
|
N_( "set BandFmt to F (eg. uchar, float)" ), "F" },
|
2012-01-27 18:14:59 +01:00
|
|
|
{ "interpretation", 'i', 0, G_OPTION_ARG_STRING, &interpretation,
|
2011-07-22 16:56:31 +02:00
|
|
|
N_( "set interpretation to I (eg. xyz)" ), "I" },
|
2009-08-16 17:00:08 +02:00
|
|
|
{ "coding", 'c', 0, G_OPTION_ARG_STRING, &coding,
|
2011-07-22 16:56:31 +02:00
|
|
|
N_( "set Coding to C (eg. labq)" ), "C" },
|
2009-08-16 17:00:08 +02:00
|
|
|
{ "xres", 'X', 0, G_OPTION_ARG_STRING, &xres,
|
|
|
|
N_( "set Xres to R pixels/mm" ), "R" },
|
|
|
|
{ "yres", 'Y', 0, G_OPTION_ARG_STRING, &yres,
|
|
|
|
N_( "set Yres to R pixels/mm" ), "R" },
|
|
|
|
{ "xoffset", 'u', 0, G_OPTION_ARG_STRING, &xoffset,
|
2011-12-01 19:47:36 +01:00
|
|
|
N_( "set Xoffset to N pixels" ), "N" },
|
2009-08-16 17:00:08 +02:00
|
|
|
{ "yoffset", 'v', 0, G_OPTION_ARG_STRING, &yoffset,
|
2011-12-01 19:47:36 +01:00
|
|
|
N_( "set Yoffset to N pixels" ), "N" },
|
2009-08-16 17:00:08 +02:00
|
|
|
{ "setext", 'e', 0, G_OPTION_ARG_NONE, &setext,
|
|
|
|
N_( "replace extension block with stdin" ), NULL },
|
2011-12-01 19:47:36 +01:00
|
|
|
{ "xsize", 'x', 0, G_OPTION_ARG_STRING, &xsize,
|
|
|
|
N_( "set Xsize to N (deprecated, use width)" ), "N" },
|
|
|
|
{ "ysize", 'y', 0, G_OPTION_ARG_STRING, &ysize,
|
|
|
|
N_( "set Ysize to N (deprecated, use height)" ), "N" },
|
2012-01-27 18:14:59 +01:00
|
|
|
{ "type", 't', 0, G_OPTION_ARG_STRING, &interpretation,
|
2011-12-01 19:47:36 +01:00
|
|
|
N_( "set Type to T (deprecated, use interpretation)" ), "T" },
|
2021-06-15 11:33:42 +02:00
|
|
|
{ "version", 'v', 0, G_OPTION_ARG_NONE, &version,
|
|
|
|
N_( "print version" ), NULL },
|
2009-08-16 17:00:08 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_pint( char *arg, int *out )
|
|
|
|
{
|
|
|
|
/* Might as well set an upper limit.
|
|
|
|
*/
|
|
|
|
*out = atoi( arg );
|
2020-06-18 14:21:43 +02:00
|
|
|
if( *out <= 0 || *out > 1000000 )
|
|
|
|
vips_error_exit( _( "'%s' is not a positive integer" ), arg );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
GOptionContext *context;
|
2014-11-18 12:29:32 +01:00
|
|
|
GOptionGroup *main_group;
|
2009-08-16 17:00:08 +02:00
|
|
|
GError *error = NULL;
|
2020-06-18 14:21:43 +02:00
|
|
|
VipsImage *im;
|
|
|
|
unsigned char header[VIPS_SIZEOF_HEADER];
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2014-11-18 12:29:32 +01:00
|
|
|
if( VIPS_INIT( argv[0] ) )
|
|
|
|
vips_error_exit( "%s", _( "unable to start VIPS" ) );
|
2022-02-27 16:27:28 +01:00
|
|
|
|
|
|
|
#ifdef ENABLE_NLS
|
2010-05-25 18:46:03 +02:00
|
|
|
textdomain( GETTEXT_PACKAGE );
|
2022-02-27 16:27:28 +01:00
|
|
|
#endif /* ENABLE_NLS */
|
2010-05-25 18:46:03 +02:00
|
|
|
setlocale( LC_ALL, "" );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2021-06-22 16:28:21 +02:00
|
|
|
{
|
|
|
|
char *basename;
|
|
|
|
|
|
|
|
basename = g_path_get_basename( argv[0] );
|
|
|
|
g_set_prgname( basename );
|
|
|
|
g_free( basename );
|
|
|
|
}
|
|
|
|
|
2016-08-17 13:14:43 +02:00
|
|
|
/* On Windows, argv is ascii-only .. use this to get a utf-8 version of
|
|
|
|
* the args.
|
|
|
|
*/
|
2021-03-06 13:47:48 +01:00
|
|
|
#ifdef G_OS_WIN32
|
2016-08-17 13:14:43 +02:00
|
|
|
argv = g_win32_get_command_line();
|
2021-03-06 13:47:48 +01:00
|
|
|
#endif /*G_OS_WIN32*/
|
2016-08-17 13:14:43 +02:00
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
context = g_option_context_new(
|
2014-06-23 21:22:50 +02:00
|
|
|
_( "vipsedit - edit vips file header" ) );
|
2014-11-18 12:29:32 +01:00
|
|
|
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 );
|
|
|
|
|
2021-03-06 13:47:48 +01:00
|
|
|
#ifdef G_OS_WIN32
|
2016-08-17 13:14:43 +02:00
|
|
|
if( !g_option_context_parse_strv( context, &argv, &error ) )
|
2021-03-06 13:47:48 +01:00
|
|
|
#else /*!G_OS_WIN32*/
|
2016-08-17 13:14:43 +02:00
|
|
|
if( !g_option_context_parse( context, &argc, &argv, &error ) )
|
2021-03-06 13:47:48 +01:00
|
|
|
#endif /*G_OS_WIN32*/
|
2016-08-17 13:14:43 +02:00
|
|
|
{
|
2020-06-18 14:21:43 +02:00
|
|
|
vips_g_error( &error );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2016-08-17 13:14:43 +02:00
|
|
|
|
|
|
|
/* On Windows, argc will not have been updated by
|
|
|
|
* g_option_context_parse_strv().
|
|
|
|
*/
|
|
|
|
for( argc = 0; argv[argc]; argc++ )
|
|
|
|
;
|
|
|
|
|
2021-06-15 11:33:42 +02:00
|
|
|
if( version )
|
|
|
|
printf( "vips-%s\n", vips_version_string() );
|
|
|
|
|
2016-08-17 13:14:43 +02:00
|
|
|
if( argc != 2 ) {
|
2014-06-23 21:22:50 +02:00
|
|
|
fprintf( stderr, _( "usage: %s [OPTION...] vips-file\n" ),
|
2009-08-16 17:00:08 +02:00
|
|
|
g_get_prgname() );
|
2020-10-28 11:24:53 +01:00
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
2020-06-18 14:21:43 +02:00
|
|
|
if( !(im = vips_image_new_from_file( argv[1], NULL )) )
|
|
|
|
vips_error_exit( _( "could not open image %s" ), argv[1] );
|
|
|
|
|
|
|
|
vips__seek( im->fd, 0, SEEK_SET );
|
|
|
|
if( read( im->fd, header, VIPS_SIZEOF_HEADER ) !=
|
|
|
|
VIPS_SIZEOF_HEADER ||
|
|
|
|
vips__read_header_bytes( im, header ) )
|
|
|
|
vips_error_exit( _( "could not read VIPS header for %s" ),
|
2009-08-16 17:00:08 +02:00
|
|
|
im->filename );
|
|
|
|
|
2011-12-01 14:17:40 +01:00
|
|
|
if( endian ) {
|
|
|
|
if( strcmp( endian, "little" ) == 0 )
|
|
|
|
im->magic = VIPS_MAGIC_INTEL;
|
|
|
|
else if( strcmp( endian, "big" ) == 0 )
|
|
|
|
im->magic = VIPS_MAGIC_SPARC;
|
2020-06-18 14:21:43 +02:00
|
|
|
else
|
|
|
|
vips_error_exit( _( "bad endian-ness %s, "
|
2011-12-01 14:17:40 +01:00
|
|
|
"should be 'big' or 'little'" ), endian );
|
|
|
|
}
|
2009-08-16 17:00:08 +02:00
|
|
|
if( xsize )
|
|
|
|
parse_pint( xsize, &im->Xsize );
|
|
|
|
if( ysize )
|
|
|
|
parse_pint( ysize, &im->Ysize );
|
|
|
|
if( bands )
|
|
|
|
parse_pint( bands, &im->Bands );
|
|
|
|
if( format ) {
|
2020-06-18 14:21:43 +02:00
|
|
|
int f;
|
|
|
|
|
|
|
|
if( (f = vips_enum_from_nick( argv[0],
|
|
|
|
VIPS_TYPE_BAND_FORMAT, format )) < 0 )
|
|
|
|
vips_error_exit( _( "bad format %s" ), format );
|
2012-01-27 18:36:25 +01:00
|
|
|
|
|
|
|
im->BandFmt = f;
|
2020-06-18 14:21:43 +02:00
|
|
|
|
|
|
|
/* We don't use this, but make sure it's set in case any
|
|
|
|
* old binaries are expecting it.
|
|
|
|
*/
|
|
|
|
im->Bbits = vips_format_sizeof( f ) << 3;
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
2012-01-27 18:14:59 +01:00
|
|
|
if( interpretation ) {
|
2020-06-18 14:21:43 +02:00
|
|
|
int i;
|
2012-01-27 18:36:25 +01:00
|
|
|
|
2020-06-18 14:21:43 +02:00
|
|
|
if( (i = vips_enum_from_nick( argv[0],
|
|
|
|
VIPS_TYPE_INTERPRETATION, interpretation )) < 0 )
|
|
|
|
vips_error_exit( _( "bad interpretation %s" ),
|
2012-01-27 18:36:25 +01:00
|
|
|
interpretation );
|
2020-06-18 14:21:43 +02:00
|
|
|
|
2012-01-27 18:36:25 +01:00
|
|
|
im->Type = i;
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
if( coding ) {
|
2020-06-18 14:21:43 +02:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if( (c = vips_enum_from_nick( argv[0],
|
|
|
|
VIPS_TYPE_CODING, coding )) < 0 )
|
|
|
|
vips_error_exit( _( "bad coding %s" ), coding );
|
2012-01-27 18:36:25 +01:00
|
|
|
|
|
|
|
im->Coding = c;
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
if( xres )
|
|
|
|
im->Xres = atof( xres );
|
|
|
|
if( yres )
|
|
|
|
im->Yres = atof( yres );
|
|
|
|
if( xoffset )
|
|
|
|
im->Xoffset = atoi( xoffset );
|
|
|
|
if( yoffset )
|
|
|
|
im->Yoffset = atoi( yoffset );
|
|
|
|
|
2020-06-18 14:21:43 +02:00
|
|
|
if( vips__seek( im->fd, 0, SEEK_SET ) == (off_t) -1 )
|
|
|
|
vips_error_exit( _( "could not seek on %s" ), im->filename );
|
|
|
|
if( vips__write_header_bytes( im, header ) ||
|
|
|
|
vips__write( im->fd, header, VIPS_SIZEOF_HEADER ) )
|
|
|
|
vips_error_exit( _( "could not write to %s" ), im->filename );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
if( setext ) {
|
|
|
|
char *xml;
|
2014-09-18 11:19:24 +02:00
|
|
|
size_t size;
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2020-06-18 14:21:43 +02:00
|
|
|
if( !(xml = vips__file_read( stdin, "stdin", &size )) )
|
|
|
|
vips_error_exit( "%s", _( "could not get ext data" ) );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
|
|
|
/* Strip trailing whitespace ... we can get stray \n at the
|
2020-06-18 14:21:43 +02:00
|
|
|
* end, eg. "echo | vipsedit --setext fred.v".
|
2009-08-16 17:00:08 +02:00
|
|
|
*/
|
|
|
|
while( size > 0 && isspace( xml[size - 1] ) )
|
|
|
|
size -= 1;
|
|
|
|
|
2020-06-18 14:21:43 +02:00
|
|
|
if( vips__write_extension_block( im, xml, size ) )
|
|
|
|
vips_error_exit( "%s", _( "could not set extension" ) );
|
|
|
|
g_free( xml );
|
2009-08-16 17:00:08 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 14:21:43 +02:00
|
|
|
g_object_unref( im );
|
2009-08-16 17:00:08 +02:00
|
|
|
|
2020-10-28 11:24:53 +01:00
|
|
|
g_option_context_free( context );
|
|
|
|
|
2021-03-06 13:47:48 +01:00
|
|
|
#ifdef G_OS_WIN32
|
2016-08-17 13:14:43 +02:00
|
|
|
g_strfreev( argv );
|
2021-03-06 13:47:48 +01:00
|
|
|
#endif /*G_OS_WIN32*/
|
2016-08-17 13:14:43 +02:00
|
|
|
|
2011-09-24 11:17:32 +02:00
|
|
|
vips_shutdown();
|
|
|
|
|
2009-08-16 17:00:08 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|