File: xloadimage.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (571 lines) | stat: -rw-r--r-- 15,554 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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* xloadimage.c:
 *
 * generic image loader for X11
 *
 * jim frost 09.27.89
 *
 * Copyright 1989, 1990, 1991 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include "xloadimage.h"
#include "options.h"
#include "misc.h"
#ifdef VMS
#include "patchlevel."
#define NO_FORK
#else
#include "patchlevel"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <signal.h>

char *ProgramName= "xloadimage";

/* if an image loader needs to have our display and screen, it will get
 * them from here.  this is done to keep most of the image routines
 * clean
 */

Display      *Disp= NULL;
int           Scrn= 0;

/* used for the -default option.  this is the root weave bitmap with
 * the bits in the order that xloadimage likes.
 */

#define root_weave_width 4
#define root_weave_height 4
static byte root_weave_bits[] = {
  0xe0, 0xb0, 0xd0, 0x70
};

static Image *doProcessOnImage(image, option, verbose)
     Image *image;
     Option *option;
{ Image  *retimage= image;
  XColor  xcolor; /* color for foreground/background */

  switch (option->type) {
  case BACKGROUND:
    if (image->depth > 1)
      break;
    XParseColor(Disp, DefaultColormap(Disp, Scrn), option->info.background,
		&xcolor);
    image->rgb.red[0]= xcolor.red;
    image->rgb.green[0]= xcolor.green;
    image->rgb.blue[0]= xcolor.blue;
    break;

  case BRIGHT:
    brighten(image, option->info.bright, verbose);
    break;

  case CLIP:
    retimage= clip(image, option->info.clip.x, option->info.clip.y,
		   option->info.clip.w, option->info.clip.h, verbose);
    break;

  case COLORS:
    retimage= reduce(image, option->info.colors, verbose);
    break;

  case DITHER:
    retimage= dither(image, verbose);
    break;

  case FOREGROUND:
    if (image->depth > 1)
      break;
    XParseColor(Disp, DefaultColormap(Disp, Scrn), option->info.foreground,
		&xcolor);
    image->rgb.red[1]= xcolor.red;
    image->rgb.green[1]= xcolor.green;
    image->rgb.blue[1]= xcolor.blue;
    break;

  case GAMMA:
    gammacorrect(image, option->info.gamma, verbose);
    break;

  case GRAY:
    if (BITMAPP(image))
      retimage= undither(image, verbose);
    else
      gray(image, verbose);
    break;

  case HALFTONE:
    retimage= halftone(image, verbose);
    break;

  case NORMALIZE:
    retimage= normalize(image, verbose);
    break;

  case ROTATE:
    retimage= rotate(image, option->info.rotate, verbose);
    break;

  case SMOOTH:
    retimage= smooth(image, 1, verbose);
    break;

  case TITLE:
    if (image->title)
      lfree((byte *)image->title);
    image->title= dupString(option->info.title);
    break;

  case ZOOM:
    retimage= zoom(image, option->info.zoom.x, option->info.zoom.y, verbose);
    break;

  default:
    /* Nothing to do */
    break;
  }
  return(retimage);
}

/* process a list of options on an image
 */
static Image *processImage(image, global_options, image_options)
     Image *image;
     OptionSet *global_options;
     OptionSet *image_options;
{ Option       *opt;
  Image        *tmpimage;
  unsigned int  verbose;

  verbose= (getOption(global_options, VERBOSE) != NULL);

  /* go through the global options and process them
   */
  for (opt= global_options->options; opt; opt= opt->next) {

    /* if option already exists locally for this image, ignore it
     */
    if (getOption(image_options, opt->type))
      continue;
    tmpimage= doProcessOnImage(image, opt, verbose);
    if (tmpimage != image) {
      freeImage(image);
      image= tmpimage;
    }
  }

  /* go through local options
   */
  for (opt= image_options->options; opt; opt= opt->next) {
    tmpimage= doProcessOnImage(image, opt, verbose);
    if (tmpimage != image) {
      freeImage(image);
      image= tmpimage;
    }
  }
  return(image);
}

/* the real thing
 */

int main(argc, argv)
     int argc;
     char *argv[];
{ Option *opt;
  char         *dname;
  Image        *dispimage;      /* image that will be sent to the display */
  Image        *newimage;       /* new image we're loading */
  Image        *tmpimage;
  Display      *disp;           /* display we're sending to */
  int           scrn;           /* screen we're sending to */
  char         *border;         /* name of border color */
  XColor        xcolor;         /* color for border option */
  OptionSet    *global_options; /* set of global options */
  OptionSet    *image_options;  /* set of image options */
  OptionSet    *optset, *tmpset;
  Option       *dump;
  unsigned int  fullscreen;
  unsigned int  onroot;
  unsigned int  verbose;
  unsigned int  winwidth, winheight; /* geometry of image */
  unsigned int  shrinktofit;

  /* set up internal error handlers
   */

  signal(SIGSEGV, internalError);
#ifdef	SIGBUS
  signal(SIGBUS, internalError);
#endif
  signal(SIGFPE, internalError);
  signal(SIGILL, internalError);
#if defined(_AIX) && defined(_IBMR2)
  /* the RS/6000 (AIX 3.1) has a new signal, SIGDANGER, which you get
   * when memory is exhausted.  since malloc() can overcommit, it's a good
   * idea to trap this one.
   */
  signal(SIGDANGER, memoryExhausted);
#endif

  ProgramName= argv[0];
  if (argc < 2)
    usage();

  /* defaults and other initial settings.  some of these depend on what
   * our name was when invoked.
   */

  loadPathsAndExts();

  processOptions(argc, argv, &global_options, &image_options);

  verbose= (getOption(global_options, VERBOSE) != NULL);

  /* if no images are specified and we're not setting the default root,
   * this invocation is a no-op
   */
  if ((image_options->next == NULL) &&
      (getOption(image_options, NAME) == NULL) &&
      (getOption(global_options, DEFAULT) == NULL)) {
    fprintf(stderr, "%s: No images were specified.\n", argv[0]);
    usageHelp();
    /* NOTREACHED */
  }

  if (getOption(global_options, IDENTIFY)) {
    for (optset= image_options; optset; optset= optset->next) {
      if ((opt= getOption(optset, NAME)))
	identifyImage(opt->info.name);
    }
    exit(0);
  }

  /* start talking to the display
   */

  opt= getOption(global_options, DISPLAY);
  dname= (opt ? opt->info.display : NULL);
  if (! (Disp= disp= XOpenDisplay(dname))) {
    printf("%s: Cannot open display\n", XDisplayName(dname));
    exit(1);
  }
  Scrn= scrn= DefaultScreen(disp);
  XSetErrorHandler(errorHandler);

  /* background ourselves if the user asked us to
   */

#ifndef NO_FORK
  if (getOption(global_options, FORK))
    switch(fork()) {
    case -1:
      perror("fork");
      /* FALLTHRU */
    case 0:
      break;
    default:
      exit(0);
    }
#endif /* !NO_FORK */

  dispimage= NULL;

  onroot= (getOption(global_options, ONROOT) != NULL);
  fullscreen= (getOption(global_options, FULLSCREEN) != NULL);
  shrinktofit= (getOption(global_options, SHRINKTOFIT) != NULL);
  if ((opt= getOption(global_options, GEOMETRY))) {
    winwidth= opt->info.geometry.w;
    winheight= opt->info.geometry.h;
  }
  else {
    winwidth= 0;
    winheight= 0;
  }

  /* find out if we're supposed to dump this silly thing
   */
  dump= getOption(global_options, DUMP);

  if (!getOption(global_options, DEFAULT) &&
      (dump || onroot) && (winwidth || winheight ||
			   getOption(image_options, CENTER) ||
			   getOption(image_options, AT) ||
			   fullscreen)) {
    if (!winwidth)
      winwidth= DisplayWidth(disp, scrn);
    if (!winheight)
      winheight= DisplayHeight(disp, scrn);
    opt= getOption(global_options, BORDER);
    border= (opt ? opt->info.border : NULL);
    if (border)
      XParseColor(disp, DefaultColormap(disp, scrn), border, &xcolor);
    else
      xcolor.red= xcolor.green= xcolor.blue = 65535;
    if (DefaultDepth(disp, scrn) == 1) {
      dispimage= newBitImage(winwidth, winheight);
      *(dispimage->rgb.red)= xcolor.red;
      *(dispimage->rgb.green)= xcolor.green;
      *(dispimage->rgb.blue)= xcolor.blue;
      if (xcolor.red || xcolor.blue || xcolor.green) {
	*(dispimage->rgb.red + 1)= 0;
	*(dispimage->rgb.green + 1)= 0;
	*(dispimage->rgb.blue + 1)= 0;
      }
      else {
	*(dispimage->rgb.red + 1)= 65535;
	*(dispimage->rgb.green + 1)= 65535;
	*(dispimage->rgb.blue + 1)= 65535;
      }
      fill(dispimage, 0, 0, winwidth, winheight, 0);
    }
    else {
      dispimage= newTrueImage(winwidth, winheight);
      dispimage->rgb.used= 1;
      fill(dispimage, 0, 0, winwidth, winheight,
	   RGB_TO_TRUE(xcolor.red, xcolor.green, xcolor.blue));
    }
    dispimage->title= dupString("Root Image");
  }

  /* load in each named image
   */

  for (optset= image_options; optset; optset= optset->next) {
  get_another_image:

    /* handle -default option.  this creates a base image using the
     * default tile weave.
     */
    if (getOption(image_options, DEFAULT)) {
      newimage= newBitImage(root_weave_width, root_weave_height);
      bcopy(root_weave_bits, newimage->data,
	    ((root_weave_width / 8) + (root_weave_width % 8 ? 1 : 0)) *
	    root_weave_height);
    }
    else if (! (opt= getOption(optset, NAME))) {

      /* this gets post-processing accomplished for -dump and -onroot.
       */
      if (dispimage)
	dispimage= processImage(dispimage, global_options, optset);
      continue;
    }
    else if (! (newimage= loadImage(global_options, optset, opt->info.name, verbose)))
      continue;

    /* retitle the image if we were asked to
     */
    if ((opt= getOption(optset, TITLE))) {
      if (newimage->title)
	lfree((byte *)newimage->title);
      newimage->title= dupString(opt->info.title);
    }

    /* if this is the first image and we're putting it on the root window
     * in fullscreen mode, set the zoom factors and
     * location to something reasonable.
     */

    if ((optset == image_options) && onroot && fullscreen &&
	!getOption(optset, ZOOM) && !getOption(optset, AT) &&
	!getOption(optset, CENTER)) {

      opt= newOption(ZOOM);
      if ((newimage->width > DisplayWidth(disp, scrn)) ||
	  (newimage->height > DisplayHeight(disp, scrn))) {
	opt->info.zoom.x= opt->info.zoom.y= 
	  ((int)newimage->width - DisplayWidth(disp, scrn) >
	   (int)newimage->height - DisplayHeight(disp, scrn) ?
	   (float)DisplayWidth(disp, scrn) / (float)newimage->width * 100.0 :
	   (float)DisplayHeight(disp, scrn) / (float)newimage->height * 100.0);
      }
      else {
	opt->info.zoom.x= opt->info.zoom.y=
	  (DisplayWidth(disp, scrn) - newimage->width <
	   DisplayHeight(disp, scrn) - newimage->height ?
	   (float)DisplayWidth(disp, scrn) / (float)newimage->width * 100.0 :
	   (float)DisplayHeight(disp, scrn) / (float)newimage->height * 100.0);
      }
      addOption(optset, opt);
      opt= newOption(CENTER);
      addOption(optset, opt);
    }

    if ((optset == image_options) && shrinktofit && !onroot &&
	!getOption(optset, ZOOM)) {

      opt= newOption(ZOOM);

      opt->info.zoom.x= opt->info.zoom.y= 
	(newimage->width - (DisplayWidth(disp, scrn) * 0.9) >
	 newimage->height - (DisplayHeight(disp, scrn) * 0.9) ?
	 ((float)DisplayWidth(disp, scrn) * 0.9)
	 / (float)newimage->width * 100.0 :
	 ((float)DisplayHeight(disp, scrn) * 0.9)
	 / (float)newimage->height * 100.0);
      if ((opt->info.zoom.x > 100) || (opt->info.zoom.y > 100))
        opt->info.zoom.x=opt->info.zoom.y=100;

      addOption(optset, opt);
    }

    newimage= processImage(newimage, global_options, optset);

    /* handle -center
     */
    if (dispimage && getOption(optset, CENTER)) {
      tmpimage= merge(dispimage, newimage,
		      (int)(dispimage->width - newimage->width) / 2,
		      (int)(dispimage->height - newimage->height) / 2,
		      verbose);
      if (dispimage != tmpimage) {
	freeImage(dispimage);
	dispimage= tmpimage;
      }
    }

    /* merge onto previous image
     */
    else if (dispimage) {
      if (! dispimage->title)
	dispimage->title= dupString(newimage->title);

      /* handle -at
       */
      if ((opt= getOption(optset, AT)))
	tmpimage= merge(dispimage, newimage,
			opt->info.at.x, opt->info.at.y, verbose);
      else
	tmpimage= merge(dispimage, newimage, 0, 0, verbose);
      if (dispimage != tmpimage) {
	freeImage(dispimage);
	dispimage= tmpimage;
      }
      freeImage(newimage);
    }
    else
      dispimage= newimage;

    /* if the user asked for tiling we tile the image now
     */
    if (getOption(optset, TILE)) {
      if (!winwidth)
	winwidth= DisplayWidth(disp, scrn);
      if (!winheight)
	winheight= DisplayHeight(disp, scrn);
      dispimage= tile(newimage, 0, 0, winwidth, winheight, verbose);
    }

    /* if next image is to be merged onto this one, read it
     */
    if (dump || onroot || (getOption(optset->next, MERGE)))
      continue;

  redisplay_in_window:
    switch(imageInWindow(disp, scrn, dispimage, global_options,
			 optset, argc, argv, verbose)) {
    case '\0': /* window got nuked by someone */
      XCloseDisplay(disp);
      exit(1);
    case '\003':
    case 'q':  /* user quit */
      cleanUpWindow(disp);
      XCloseDisplay(disp);
      exit(0);
    case ' ':
    case 'n':  /* next image */
      if ((opt= getOption(optset->next, GOTO))) {
	char *tag= opt->info.go_to;

	for (tmpset= image_options; tmpset; tmpset= tmpset->next) {
	  if ((opt= getOption(tmpset, NAME)) &&
	      !strcmp(tag, opt->info.name)) {
	    optset= tmpset;
	    freeImage(dispimage);
	    dispimage= NULL;
	    goto get_another_image; /* ick */
	  }
	}
	fprintf(stderr, "Target for -goto %s was not found\n", tag);
      }
      break;
    case 'p':  /* previous image */
      for (tmpset= image_options; tmpset && (tmpset->next != optset);
	   tmpset= tmpset->next)
	/* EMPTY */
	;
      if (!tmpset)
	goto redisplay_in_window; /* ick */
      optset= tmpset;
      freeImage(dispimage);
      dispimage= NULL;
      goto get_another_image; /* ick */
    case '<':
      if ((opt = getOption(optset,ZOOM)) == NULL) {
	opt= newOption(ZOOM);
	opt->info.zoom.x= opt->info.zoom.y= 50.0;
	addOption(optset, opt);
      } else {
	opt->info.zoom.x= opt->info.zoom.x ? opt->info.zoom.x * 0.5 : 50;
	opt->info.zoom.y= opt->info.zoom.y ? opt->info.zoom.y * 0.5 : 50;
      }
      tmpimage= dispimage;
      dispimage=
	zoom(dispimage, 50, 50,
	     (getOption(global_options, VERBOSE) != NULL));
      if (tmpimage != dispimage)
	free(tmpimage);
      goto redisplay_in_window; /* ick */
    case '>':
      if ((opt = getOption(optset,ZOOM)) == NULL) {
	opt= newOption(ZOOM);
	opt->info.zoom.x= opt->info.zoom.y= 200.0;
	addOption(optset, opt);
      } else {
	opt->info.zoom.x= opt->info.zoom.x ? opt->info.zoom.x * 2.0 : 200;
	opt->info.zoom.y= opt->info.zoom.y ? opt->info.zoom.y * 2.0 : 200;
      }
      tmpimage= dispimage;
      dispimage=
	zoom(dispimage, 200, 200,
	     (getOption(global_options, VERBOSE) != NULL));
      if (tmpimage != dispimage)
	free(tmpimage);
      goto redisplay_in_window; /* ick */
    }
    freeImage(dispimage);
    dispimage= NULL;
  }

  /* dump image into a NIFF file rather than displaying
   */
  if (dump && dispimage) {
    for (optset= image_options; optset && optset->next; optset= optset->next)
      /* EMPTY */
      ;
    if ((opt= getOption(optset, NAME))) {
      if (dispimage->title)
	lfree((byte *)dispimage->title);
      dispimage->title= dupString(opt->info.title);
    }
    dumpImage(dispimage, dump->info.dump.type, dump->info.dump.file, verbose);
    freeImage(dispimage);
    dispimage= NULL;
    exit(0);
  }

  /* display image on root
   */
  if (onroot && dispimage)
    imageOnRoot(disp, scrn, dispimage, global_options, verbose);

  /* shut down
   */
  XCloseDisplay(disp);
  exit(0);
}