home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume5 / xldimage / part02 / xloadimage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-13  |  7.9 KB  |  361 lines

  1. /* loadimage.c:
  2.  *
  3.  * generic image loader for X11
  4.  *
  5.  * jim frost 09.27.89
  6.  *
  7.  * Copyright 1989 Jim Frost.  See included file "copyright.h" for complete
  8.  * copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "xloadimage.h"
  13.  
  14. /* options array and definitions
  15.  */
  16.  
  17. char *Options[] = {
  18.   "onroot", /* global options */
  19.   "border",
  20.   "display",
  21.   "geometry",
  22.   "help",
  23.   "identify",
  24.   "list",
  25.   "install",
  26.   "path",
  27.   "quiet",
  28.   "supported",
  29.   "verbose",
  30.   "view",
  31.  
  32.   "at", /* image options */
  33.   "background",
  34.   "brighten",
  35.   "center",
  36.   "clip",
  37.   "colors",
  38.   "dither",
  39.   "foreground",
  40.   "name",
  41.   "xzoom",
  42.   "yzoom",
  43.   "zoom",
  44.   NULL
  45. };
  46.  
  47. #define ONROOT    0
  48. #define BORDER    1
  49. #define DISPLAY   2
  50. #define GEOMETRY  3
  51. #define HELP      4
  52. #define IDENTIFY  5
  53. #define LIST      6
  54. #define INSTALL   7
  55. #define PATH      8
  56. #define QUIET     9
  57. #define SUPPORTED 10
  58. #define VERBOSE   11
  59. #define VIEW      12
  60.  
  61. #define AT         13
  62. #define BACKGROUND 14
  63. #define BRIGHT     15
  64. #define CENTER     16
  65. #define CLIP       17
  66. #define COLORS     18
  67. #define DITHER     19
  68. #define FOREGROUND 20
  69. #define NAME       21
  70. #define XZOOM      22
  71. #define YZOOM      23
  72. #define ZOOM       24
  73.  
  74. /* the real thing
  75.  */
  76.  
  77. main(argc, argv)
  78.      int argc;
  79.      char *argv[];
  80. { unsigned int  onroot= 1;
  81.   char         *border;
  82.   char         *dname;
  83.   unsigned int  identify;
  84.   unsigned int  install;
  85.   unsigned int  verbose;
  86.   Image        *dispimage;      /* image that will be sent to the display */
  87.   Image        *newimage;       /* new image we're loading */
  88.   Display      *disp;           /* display we're sending to */
  89.   int           scrn;           /* screen we're sending to */
  90.   XColor        xcolor;         /* color for border option */
  91.   ImageOptions  images[MAXIMAGES]; /* list of image w/ options to load */
  92.   unsigned int  imagecount;     /* number of images in ImageName array */
  93.   unsigned int  a;
  94.   unsigned int  winx, winy;     /* location of window */
  95.   unsigned int  winwidth, winheight; /* geometry of window */
  96.  
  97.   if (argc < 2)
  98.     usage(argv[0]);
  99.  
  100.   /* defaults and other initial settings.  some of these depend on what
  101.    * our name was when invoked.
  102.    */
  103.  
  104.   loadPathsAndExts();
  105.   onroot= 0;
  106.   verbose= 1;
  107.   if (!strcmp(tail(argv[0]), "xview")) {
  108.     onroot= 0;
  109.     verbose= 1;
  110.   }
  111.   else if (!strcmp(tail(argv[0]), "xsetbg")) {
  112.     onroot= 1;
  113.     verbose= 0;
  114.   }
  115.   border= NULL;
  116.   dname= NULL;
  117.   identify= 0;
  118.   install= 0;
  119.   winx= winy= winwidth= winheight= 0;
  120.  
  121.   imagecount= 0;
  122.   for (a= 1; a < argc; a++) {
  123.     switch (optionNumber(argv[a], Options)) {
  124.     case OPT_BADOPT:
  125.       printf("%s: Bad option\n", argv[a]);
  126.       usage(argv[0]);
  127.       /* NOTREACHED */
  128.  
  129.     case OPT_NOTOPT:
  130.       if (imagecount == MAXIMAGES)
  131.     printf("%s: Too many images (ignoring)\n", argv[++a]);
  132.       else
  133.     images[imagecount++].name= argv[a];
  134.       break;
  135.  
  136.     case OPT_SHORTOPT:
  137.       printf("%s: Not enough characters to identify option\n", argv[a]);
  138.       usage(argv[0]);
  139.       /* NOTREACHED */
  140.  
  141.     /* process options global to everything
  142.      */
  143.  
  144.     case ONROOT:
  145.       onroot= 1;
  146.       break;
  147.  
  148.     case BORDER:
  149.       border= argv[++a];
  150.       break;
  151.  
  152.     case DISPLAY:
  153.       dname= argv[++a];
  154.       break;
  155.  
  156.     case GEOMETRY:
  157.       XParseGeometry(argv[++a], &winx, &winy, &winwidth, &winheight);
  158.       break;
  159.  
  160.     case HELP:
  161.       usage(argv[0]);
  162.       exit(0);
  163.  
  164.     case IDENTIFY:
  165.       identify= 1;
  166.       break;
  167.  
  168.     case LIST:
  169.       listImages();
  170.       exit(0);
  171.  
  172.     case INSTALL:
  173.       install= 1;
  174.       break;
  175.  
  176.     case PATH:
  177.       showPath();
  178.       break;
  179.  
  180.     case QUIET:
  181.       verbose= 0;
  182.       break;
  183.  
  184.     case SUPPORTED:
  185.       supportedImageTypes();
  186.       break;
  187.  
  188.     case VERBOSE:
  189.       verbose= 1;
  190.       break;
  191.  
  192.     case VIEW:
  193.       onroot= 0;
  194.  
  195.     /* process options local to an image
  196.      */
  197.  
  198.     case AT:
  199.       if (sscanf(argv[++a], "%d,%d",
  200.          &images[imagecount].atx, &images[imagecount].aty) != 2) {
  201.     printf("Bad argument to -at\n");
  202.     usage(argv[0]);
  203.     /* NOTREACHED */
  204.       }
  205.       break;
  206.  
  207.     case BACKGROUND:
  208.       images[imagecount].bg= argv[++a];
  209.       break;
  210.  
  211.     case BRIGHT:
  212.       images[imagecount].bright= atoi(argv[++a]);
  213.       break;
  214.  
  215.     case CENTER:
  216.       images[imagecount].center= 1;
  217.       break;
  218.  
  219.     case CLIP:
  220.       if (sscanf(argv[++a], "%d,%d,%d,%d",
  221.          &images[imagecount].clipx, &images[imagecount].clipy,
  222.          &images[imagecount].clipw, &images[imagecount].cliph) != 4) {
  223.     printf("Bad argument to -clip\n");
  224.     usage(argv[0]);
  225.     /* NOTREACHED */
  226.       }
  227.       break;
  228.  
  229.     case COLORS:
  230.       images[imagecount].colors= atoi(argv[++a]);
  231.       if (images[imagecount].colors < 2) {
  232.     printf("Argument to -colors is too low (ignored)\n");
  233.     images[imagecount].colors= 0;
  234.       }
  235.       else if (images[imagecount].colors > 65536) {
  236.     printf("Argument to -colors is too high (ignored)\n");
  237.     images[imagecount].colors= 0;
  238.       }
  239.       break;
  240.  
  241.     case DITHER:
  242.       images[imagecount].dither= 1;
  243.       break;
  244.  
  245.     case FOREGROUND:
  246.       images[imagecount].fg= argv[++a];
  247.       break;
  248.  
  249.     case NAME:
  250.       if (imagecount == MAXIMAGES)
  251.     printf("%s: Too many images (ignoring)\n", argv[++a]);
  252.       else
  253.     images[imagecount++].name= argv[++a];
  254.       break;
  255.  
  256.     case XZOOM:
  257.       images[imagecount].xzoom= atoi(argv[++a]);
  258.       break;
  259.  
  260.     case YZOOM:
  261.       images[imagecount].yzoom= atoi(argv[++a]);
  262.       break;
  263.  
  264.     case ZOOM:
  265.       images[imagecount].xzoom= images[imagecount].yzoom= atoi(argv[++a]);
  266.       break;
  267.  
  268.     default:
  269.  
  270.       /* this should not happen!
  271.        */
  272.  
  273.       printf("%s: Internal error parsing arguments\n", argv[0]);
  274.       exit(1);
  275.     }
  276.   }
  277.  
  278.   if (!imagecount) /* NO-OP from here on */
  279.     exit(0);
  280.  
  281.   if (identify) {                    /* identify the named image(s) */
  282.     for (a= 0; a < imagecount; a++)
  283.       identifyImage(images[a].name);
  284.     exit(0);
  285.   }
  286.  
  287.   /* start talking to the display
  288.    */
  289.  
  290.   if (! (disp= XOpenDisplay(dname))) {
  291.     printf("%s: Cannot open display\n", XDisplayName(dname));
  292.     exit(1);
  293.   }
  294.   scrn= DefaultScreen(disp);
  295.   XSetIOErrorHandler(ioErrorHandler);
  296.  
  297.   dispimage= NULL;
  298.   if (onroot && (winwidth || winheight || images[0].center ||
  299.       images[a].atx || images[a].aty)) {
  300.     if (!winwidth)
  301.     winwidth= DisplayWidth(disp, scrn);
  302.     if (!winheight)
  303.       winheight= DisplayHeight(disp, scrn);
  304.     if (DefaultDepth(disp, scrn) == 1)
  305.       dispimage= newBitImage(winwidth, winheight);
  306.     else {
  307.       dispimage= newRGBImage(winwidth, winheight, DefaultDepth(disp, scrn));
  308.       dispimage->rgb.used= 1;
  309.     }
  310.     *(dispimage->rgb.red)= 65535;   /* default border value is white */
  311.     *(dispimage->rgb.green)= 65535;
  312.     *(dispimage->rgb.blue)= 65535;
  313.     if (border) {
  314.       XParseColor(disp, DefaultColormap(disp, scrn), border, &xcolor);
  315.       *dispimage->rgb.red= xcolor.red;
  316.       *dispimage->rgb.green= xcolor.green;
  317.       *dispimage->rgb.blue= xcolor.blue;
  318.     }
  319.     fill(dispimage, 0, 0, winwidth, winheight, 0, verbose);
  320.   }
  321.  
  322.   /* load in each named image
  323.    */
  324.  
  325.   for (a= 0; a < imagecount; a++) {
  326.     if (! (newimage= loadImage(images[a].name, verbose)))
  327.       continue;
  328.     if ((dispimage && BITMAPP(dispimage)) || (DefaultDepth(disp, scrn) == 1))
  329.       images[a].dither= 1;
  330.     newimage= processImage(disp, scrn, newimage, &images[a], verbose);
  331.     if (!images[a].clipw && !images[a].cliph) {
  332.       images[a].clipw= newimage->width;
  333.       images[a].cliph= newimage->height;
  334.     }
  335.     if (images[a].center) {
  336.       images[a].atx= (dispimage->width - images[a].clipw) / 2;
  337.       images[a].aty= (dispimage->height - images[a].cliph) / 2;
  338.     }
  339.     if (dispimage) {
  340.       if (! dispimage->title)
  341.     dispimage->title= dupString(newimage->title);
  342.       merge(dispimage, newimage, images[a].atx, images[a].aty, verbose);
  343.       freeImage(newimage);
  344.     }
  345.     else
  346.       dispimage= newimage;
  347.   }
  348.  
  349.   if (! dispimage) {
  350.     printf("No images to display\n");
  351.     exit(1);
  352.   }
  353.  
  354.   if (onroot)
  355.     imageOnRoot(disp, scrn, dispimage, install, verbose);
  356.   else
  357.     imageInWindow(disp, scrn, dispimage, winx, winy, winwidth, winheight,
  358.           install, verbose);
  359.   XCloseDisplay(disp);
  360. }
  361.