File: misc.c

package info (click to toggle)
xli 1.17.0+20061110-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,540 kB
  • sloc: ansic: 25,840; makefile: 11
file content (482 lines) | stat: -rw-r--r-- 12,247 bytes parent folder | download
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* misc.c:
 *
 * miscellaneous funcs
 *
 * jim frost 10.05.89
 *
 * Copyright 1989, 1990, 1991 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include "xli.h"
#include "patchlevel"
#include <signal.h>
#include <string.h>

static char *signalName(int sig)
{
	static char buf[32];

	switch (sig) {
	case SIGSEGV:
		return ("SEGV");
	case SIGBUS:
		return ("BUS");
	case SIGFPE:
		return ("FPE");
	case SIGILL:
		return ("ILL");
	default:
		sprintf(buf, "Signal %d", sig);
		return (buf);
	}
}

void memoryExhausted(void)
{
	fprintf(stderr,
		"Memory has been exhausted; Last function was %s, operation cannot continue (sorry).\n", globals.lastfunc);
	if (globals._Xdebug)
		abort();
	else
		exit(1);
}

void internalError(int sig)
{
	static int handling_error = 0;

	switch (handling_error++) {
	case 0:
		printf("\
An internal error (%s) has occurred. Last function was %s.\n\
If you would like to file a bug report, please send email to %s\n\
with a description of how you triggered the bug, the output of xli\n\
before the failure, and the following information:\n\n",
		       signalName(sig), globals.lastfunc, AUTHOR_EMAIL);

		printf("********************************************************************************\n");
		printf("xli Version %s.%s\n", VERSION, PATCHLEVEL);
		tellAboutDisplay(&globals.dinfo);
		printf("********************************************************************************\n\n");
		break;
	case 1:
		fprintf(stderr, "\
An internal error has occurred within the internal error handler.  No more\n\
information about the error is available, sorry.\n");
		break;
	}
	if (globals._Xdebug)	/* dump core if -debug is on */
		abort();
	printf("If you run xli again with the -dumpcore flag, then it will core dump\n");
	printf("so you can investigate the problem yourself.\n");
	exit(1);
}

void version(void)
{
	printf("xli version %s patchlevel %s.\n", VERSION, PATCHLEVEL);
	printf("based on xli version 1 patchlevel 16 by Graeme Gill.\n");
	printf("based on xloadimage version 3 patchlevel 01 by Jim Frost.\n");
	printf("Please send email to %s for bug reports.\n", AUTHOR_EMAIL);
}

void usage(char *name)
{
	version();
	printf("\nUsage: %s [global options] {[image options] image_name ...}\n\n",
	       tail(name));
	printf("\
Type `%s -help [option ...]' for information on a particular option, or\n\
`%s -help' to enter the interactive help facility.\n", tail(name), tail(name));
	exit(1);
}

char *tail(char *path)
{
	int s;
	char *t;

	t = path;
	for (s = 0; *(path + s) != '\0'; s++)
		if (*(path + s) == '/')
			t = path + s + 1;
	return (t);
}

#define MIN(a,b) ( (a)<(b) ? (a) : (b))

Image *processImage(DisplayInfo *dinfo, Image *iimage, ImageOptions *options)
{
	Image *image = iimage, *tmpimage;
	XColor xcolor;

	CURRFUNC("processImage");

	/* Pre-processing */

	/* clip the image if requested */

	if ((options->clipx != 0) || (options->clipy != 0) ||
	    (options->clipw != 0) || (options->cliph != 0)) {
		tmpimage = clip(image, options->clipx, options->clipy,
			(options->clipw ? options->clipw : image->width),
		       (options->cliph ? options->cliph : image->height),
				options);
		if (tmpimage != image && iimage != image)
			freeImage(image);
		image = tmpimage;
	}
	if (options->rotate) {
		tmpimage = rotate(image, options->rotate, globals.verbose);
		if (tmpimage != image && iimage != image)
			freeImage(image);
		image = tmpimage;
	}
	/* zoom image */
	if (options->zoom_auto) {
		if (image->width > globals.dinfo.width * .9)
			options->xzoom = globals.dinfo.width * 90 / image->width;
		else
			options->xzoom = 100;
		if (image->height > globals.dinfo.height * .9)
			options->yzoom = globals.dinfo.height * 90 / image->height;
		else
			options->yzoom = 100;
		/* both dimensions should be shrunk by the same factor */
		options->xzoom = options->yzoom =
			MIN(options->xzoom, options->yzoom);
	}
	if (options->xzoom || options->yzoom) {
		/* if the image is to be blown up, compress before doing it */
		if (!options->colors && RGBP(image) &&	
		    ((!options->xzoom && (options->yzoom > 100)) ||
		     (!options->yzoom && (options->xzoom > 100)) ||
		     (options->xzoom + options->yzoom > 200))) {
			compress_cmap(image, globals.verbose);
		}
		tmpimage = zoom(image, options->xzoom, options->yzoom,
			globals.verbose, TRUE);
		if (tmpimage != image && iimage != image)
			freeImage(image);
		image = tmpimage;
	}

	/* set foreground and background colors of mono image */
	xcolor.flags = DoRed | DoGreen | DoBlue;
	if (image->depth == 1 && (options->fg || options->bg)) {
		int backgmap = 0;
		/* figure out background map number - xwd can make it what it
		 * likes
		 */
		if (*image->rgb.red == 0 && *image->rgb.green == 0 &&
				*image->rgb.blue == 0)
			backgmap = 1;
		if (options->fg) {
			xliParseXColor(dinfo, options->fg, &xcolor);
			xliGammaCorrectXColor(&xcolor, DEFAULT_DISPLAY_GAMMA);
			*(image->rgb.red + (1 - backgmap)) = xcolor.red;
			*(image->rgb.green + (1 - backgmap)) = xcolor.green;
			*(image->rgb.blue + (1 - backgmap)) = xcolor.blue;
		}
		if (options->bg) {
			xliParseXColor(dinfo, options->bg, &xcolor);
			xliGammaCorrectXColor(&xcolor, DEFAULT_DISPLAY_GAMMA);
			*(image->rgb.red + backgmap) = xcolor.red;
			*(image->rgb.green + backgmap) = xcolor.green;
			*(image->rgb.blue + backgmap) = xcolor.blue;
		}
	}

	/* General image processing */
	if (options->smooth > 0) {	/* image is to be smoothed */
		tmpimage = smooth(image, options->smooth, globals.verbose);
		if (tmpimage != image && iimage != image)
			freeImage(image);
		image = tmpimage;
	}

	/* Post-processing */
	if (options->gray)	/* convert image to grayscale */
		gray(image, globals.verbose);

	if (options->normalize) {	/* normalize image */
		tmpimage = normalize(image, globals.verbose);
		if (tmpimage != image && iimage != image)
			freeImage(image);
		image = tmpimage;
	}
	if (options->bright)	/* alter image brightness */
		brighten(image, options->bright, globals.verbose);

	/* forcibly reduce colormap */
	if (options->colors && (TRUEP(image) || (RGBP(image) && (options->colors < image->rgb.used)))) {
		tmpimage = reduce(image, options->colors, options->colordither,
			UNSET_GAMMA, globals.verbose);
		if (tmpimage != image && iimage != image)
			freeImage(image);
		image = tmpimage;
	}

	if (options->dither && (image->depth > 1)) {
		/* image is to be dithered */
		if (options->dither == 1)
			tmpimage = dither(image, globals.verbose);
		else
			tmpimage = halftone(image, globals.verbose);
		if (tmpimage != image && iimage != image)
			freeImage(image);
		image = tmpimage;
		/* Hmmm - if foreground or -background is used, */
		/* make sure it applies here as well */
		if (image->depth == 1 && (options->fg || options->bg)) {
			int backgmap = 0;

			/* figure out background map number -
			 * xwd can make it what it likes
			 */
			if (*image->rgb.red == 0 && *image->rgb.green == 0 &&
					*image->rgb.blue == 0)
				backgmap = 1;
			if (options->fg) {
				xliParseXColor(dinfo, options->fg, &xcolor);
				xliGammaCorrectXColor(&xcolor,
					DEFAULT_DISPLAY_GAMMA);
				image->rgb.red[1 - backgmap] = xcolor.red;
				image->rgb.green[1 - backgmap] = xcolor.green;
				image->rgb.blue[1 - backgmap] = xcolor.blue;
			}

			if (options->bg) {
				xliParseXColor(dinfo, options->bg, &xcolor);
				xliGammaCorrectXColor(&xcolor,
					DEFAULT_DISPLAY_GAMMA);
				image->rgb.red[backgmap] = xcolor.red;
				image->rgb.green[backgmap] = xcolor.green;
				image->rgb.blue[backgmap] = xcolor.blue;
			}
		}
	}

	if (options->expand && !TRUEP(image)) {
		/* expand image to truecolor */
		if (globals.verbose)
			fprintf(stderr, "  Expanding image to TRUE color\n");
		tmpimage = expandtotrue(image);
		if (tmpimage != image && iimage != image)
			freeImage(image);
		image = tmpimage;
	}

	if (RGBP(image) && !image->rgb.compressed) {
		/* make sure colormap is minimized */
		compress_cmap(image, globals.verbose);
	}

	return (image);
}

/* A dumb version that should work reliably
 * search for "s2" in "s1"
 */
char *xlistrstr(char *s1, char *s2)
{
	int n;
	char *p;

	for (n = strlen(s2), p = s1;; p++) {
		if (!(p = index(p, *s2)))
			return (char *) 0;
		if (!strncmp(p, s2, n))
			return p;
	}
}

#ifndef HAS_MEMCPY
/* A block fill routine for BSD style systems */
void bfill(char *s, int n, int c)
{
#if defined(_CRAY)		/* can't do arithmetic on cray pointers */
	while (n >= 8) {
		*s++ = c;
		*s++ = c;
		*s++ = c;
		*s++ = c;
		*s++ = c;
		*s++ = c;
		*s++ = c;
		*s++ = c;
		n -= 8;
	}
	while (n-- > 0)
		*s++ = c;
	return;
#else
	int b;

	/* bytes to next word */
	b = (0 - (int) s) & (sizeof(unsigned long) - 1);
	if (n < b)
		b = n;
	while (n != 0) {
		n -= b;
		while (b-- > 0)
			*s++ = c;
		if (n == 0)
			return;
		/* words to fill */
		b = n & ~(sizeof(unsigned long) - 1);
		if (b != 0) {
			unsigned long f;
			int i;
			f = c & (((unsigned long) -1) >> ((sizeof(unsigned long) - sizeof(char)) * 8));
			for (i = sizeof(char); i < sizeof(unsigned long); i *= 2)
				f |= (f << (8 * i));
			n -= b;	/* remaining count */
			while (b > 0) {
				*((unsigned long *) s) = f;
				s += sizeof(unsigned long);
				b -= sizeof(unsigned long);
			}
		}
		b = n;
	}
#endif
}

#endif

/* An ascii to hex table */

short BEHexTable[256];		/* Big endian conversion value */
static boolean BEHexTableInitialized = FALSE;
short LEHexTable[256];		/* Little endian conversion value */
static boolean LEHexTableInitialized = FALSE;

#define b0000 0			/* things make more sense if you see them by bit */
#define b0001 1
#define b0010 2
#define b0011 3
#define b0100 4
#define b0101 5
#define b0110 6
#define b0111 7
#define b1000 8
#define b1001 9
#define b1010 10
#define b1011 11
#define b1100 12
#define b1101 13
#define b1110 14
#define b1111 15

/* build a hex digit value table.
 */

void initBEHexTable(void)
{
	int a;

	if (BEHexTableInitialized)
		return;

	for (a = 0; a < 256; a++)
		BEHexTable[a] = HEXBAD;

	BEHexTable['0'] = b0000;
	BEHexTable['1'] = b1000;
	BEHexTable['2'] = b0100;
	BEHexTable['3'] = b1100;
	BEHexTable['4'] = b0010;
	BEHexTable['5'] = b1010;
	BEHexTable['6'] = b0110;
	BEHexTable['7'] = b1110;
	BEHexTable['8'] = b0001;
	BEHexTable['9'] = b1001;
	BEHexTable['A'] = b0101;
	BEHexTable['a'] = BEHexTable['A'];
	BEHexTable['B'] = b1101;
	BEHexTable['b'] = BEHexTable['B'];
	BEHexTable['C'] = b0011;
	BEHexTable['c'] = BEHexTable['C'];
	BEHexTable['D'] = b1011;
	BEHexTable['d'] = BEHexTable['D'];
	BEHexTable['E'] = b0111;
	BEHexTable['e'] = BEHexTable['E'];
	BEHexTable['F'] = b1111;
	BEHexTable['f'] = BEHexTable['F'];
	BEHexTable['x'] = HEXSTART_BAD;
	BEHexTable['\r'] = HEXDELIM_IGNORE;
	BEHexTable['\n'] = HEXDELIM_IGNORE;
	BEHexTable['\t'] = HEXDELIM_IGNORE;
	BEHexTable[' '] = HEXDELIM_IGNORE;
	BEHexTable[','] = HEXDELIM_BAD;
	BEHexTable['}'] = HEXDELIM_BAD;


	BEHexTableInitialized = TRUE;
}

void initLEHexTable(void)
{
	int a;

	if (LEHexTableInitialized)
		return;

	for (a = 0; a < 256; a++)
		LEHexTable[a] = HEXBAD;

	LEHexTable['0'] = 0x0;
	LEHexTable['1'] = 0x1;
	LEHexTable['2'] = 0x2;
	LEHexTable['3'] = 0x3;
	LEHexTable['4'] = 0x4;
	LEHexTable['5'] = 0x5;
	LEHexTable['6'] = 0x6;
	LEHexTable['7'] = 0x7;
	LEHexTable['8'] = 0x8;
	LEHexTable['9'] = 0x9;
	LEHexTable['A'] = 0xa;
	LEHexTable['a'] = LEHexTable['A'];
	LEHexTable['B'] = 0xb;
	LEHexTable['b'] = LEHexTable['B'];
	LEHexTable['C'] = 0xc;
	LEHexTable['c'] = LEHexTable['C'];
	LEHexTable['D'] = 0xd;
	LEHexTable['d'] = LEHexTable['D'];
	LEHexTable['E'] = 0xe;
	LEHexTable['e'] = LEHexTable['E'];
	LEHexTable['F'] = 0xf;
	LEHexTable['f'] = LEHexTable['F'];
	LEHexTable['x'] = HEXSTART_BAD;
	LEHexTable['\r'] = HEXDELIM_IGNORE;
	LEHexTable['\n'] = HEXDELIM_IGNORE;
	LEHexTable['\t'] = HEXDELIM_IGNORE;
	LEHexTable[' '] = HEXDELIM_IGNORE;
	LEHexTable[','] = HEXDELIM_BAD;
	LEHexTable['}'] = HEXDELIM_BAD;


	LEHexTableInitialized = TRUE;
}

/* Hext number to integer */
/* return -1 if not legal hex */
int hstoi(unsigned char *s, int n)
{
	int i, c;
	int value = 0;

	if (!LEHexTableInitialized)
		initLEHexTable();
	for (i = 0; i < n; i++) {
		c = s[i];
		c = LEHexTable[c & 0xff];
		if (c < 0)	/* bad hex */
			return (-1);
		value = (value << 4) | c;
	}
	return value;
}