File: vipsedit.c

package info (click to toggle)
vips 8.17.3-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 52,228 kB
  • sloc: ansic: 169,684; cpp: 12,156; python: 4,887; sh: 733; perl: 40; makefile: 25; javascript: 6
file content (302 lines) | stat: -rw-r--r-- 8,018 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* modify vips file header! - useful for setting resolution, coding...
 * 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
 */

/*

	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
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
	02110-1301  USA

 */

/*

	These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /*HAVE_UNISTD_H*/
#include <locale.h>

#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>

/* We have to represent all header fields as char * so we can spot unset args
 * safely.
 */
static char *xsize = NULL;
static char *ysize = NULL;
static char *bands = NULL;
static char *format = NULL;
static char *interpretation = NULL;
static char *coding = NULL;
static char *xres = NULL;
static char *yres = NULL;
static char *xoffset = NULL;
static char *yoffset = NULL;
static char *endian = NULL;
static gboolean setext = FALSE;
static gboolean version = FALSE;

static GOptionEntry entries[] = {
	{ "endian", 'n', 0, G_OPTION_ARG_STRING, &endian,
		N_("tag file as big or little-endian"), NULL },
	{ "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" },
	{ "bands", 'b', 0, G_OPTION_ARG_STRING, &bands,
		N_("set Bands to N"), "N" },
	{ "format", 'f', 0, G_OPTION_ARG_STRING, &format,
		N_("set BandFmt to F (eg. uchar, float)"), "F" },
	{ "interpretation", 'i', 0, G_OPTION_ARG_STRING, &interpretation,
		N_("set interpretation to I (eg. xyz)"), "I" },
	{ "coding", 'c', 0, G_OPTION_ARG_STRING, &coding,
		N_("set Coding to C (eg. labq)"), "C" },
	{ "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,
		N_("set Xoffset to N pixels"), "N" },
	{ "yoffset", 'v', 0, G_OPTION_ARG_STRING, &yoffset,
		N_("set Yoffset to N pixels"), "N" },
	{ "setext", 'e', 0, G_OPTION_ARG_NONE, &setext,
		N_("replace extension block with stdin"), NULL },
	{ "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" },
	{ "type", 't', 0, G_OPTION_ARG_STRING, &interpretation,
		N_("set Type to T (deprecated, use interpretation)"), "T" },
	{ "version", 'v', 0, G_OPTION_ARG_NONE, &version,
		N_("print version"), NULL },
	{ NULL }
};

static void
parse_pint(char *arg, int *out)
{
	/* Might as well set an upper limit.
	 */
	*out = atoi(arg);
	if (*out <= 0 || *out > 1000000)
		vips_error_exit(_("'%s' is not a positive integer"), arg);
}

int
main(int argc, char **argv)
{
	GOptionContext *context;
	GOptionGroup *main_group;
	GError *error = NULL;
	VipsImage *im;
	unsigned char header[VIPS_SIZEOF_HEADER];

	if (VIPS_INIT(argv[0]))
		vips_error_exit("%s", _("unable to start VIPS"));

#ifdef ENABLE_NLS
	textdomain(GETTEXT_PACKAGE);
#endif /* ENABLE_NLS */
	setlocale(LC_ALL, "");

	{
		char *basename;

		basename = g_path_get_basename(argv[0]);
		g_set_prgname(basename);
		g_free(basename);
	}

	/* On Windows, argv is ascii-only .. use this to get a utf-8 version of
	 * the args.
	 */
#ifdef G_OS_WIN32
	argv = g_win32_get_command_line();
#endif /*G_OS_WIN32*/

	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);

#ifdef G_OS_WIN32
	if (!g_option_context_parse_strv(context, &argv, &error))
#else  /*!G_OS_WIN32*/
	if (!g_option_context_parse(context, &argc, &argv, &error))
#endif /*G_OS_WIN32*/
	{
		vips_g_error(&error);

		exit(-1);
	}

	/* On Windows, argc will not have been updated by
	 * g_option_context_parse_strv().
	 */
	for (argc = 0; argv[argc]; argc++)
		;

	if (version)
		printf("vips-%s\n", vips_version_string());

	if (argc != 2) {
		fprintf(stderr, _("usage: %s [OPTION...] vips-file\n"),
			g_get_prgname());

		exit(-1);
	}

	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"),
			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
			vips_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) {
		int f;

		if ((f = vips_enum_from_nick(argv[0],
				 VIPS_TYPE_BAND_FORMAT, format)) < 0)
			vips_error_exit(_("bad format %s"), format);

		im->BandFmt = f;

		/* 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;
	}
	if (interpretation) {
		int i;

		if ((i = vips_enum_from_nick(argv[0],
				 VIPS_TYPE_INTERPRETATION, interpretation)) < 0)
			vips_error_exit(_("bad interpretation %s"),
				interpretation);

		im->Type = i;
	}
	if (coding) {
		int c;

		if ((c = vips_enum_from_nick(argv[0],
				 VIPS_TYPE_CODING, coding)) < 0)
			vips_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 (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);

	if (setext) {
		char *xml;
		size_t size;

		if (!(xml = vips__file_read(stdin, "stdin", &size)))
			vips_error_exit("%s", _("could not get ext data"));

		/* Strip trailing whitespace ... we can get stray \n at the
		 * end, eg. "echo | vipsedit --setext fred.v".
		 */
		while (size > 0 && g_ascii_isspace(xml[size - 1]))
			size -= 1;

		if (vips__write_extension_block(im, xml, size))
			vips_error_exit("%s", _("could not set extension"));
		g_free(xml);
	}

	g_object_unref(im);

	g_option_context_free(context);

#ifdef G_OS_WIN32
	g_strfreev(argv);
#endif /*G_OS_WIN32*/

	vips_shutdown();

	return 0;
}