ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
(Generate patch)

Comparing ray/src/px/x11image.c (file contents):
Revision 2.4 by greg, Tue Apr 28 09:40:32 1992 UTC vs.
Revision 2.8 by greg, Fri May 29 14:21:12 1992 UTC

# Line 41 | Line 41 | static char SCCSid[] = "$SunId$ LBL";
41   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
42  
43   #define  ourscreen      DefaultScreen(thedisplay)
44 #define  ourblack       BlackPixel(thedisplay,ourscreen)
45 #define  ourwhite       WhitePixel(thedisplay,ourscreen)
44   #define  ourroot        RootWindow(thedisplay,ourscreen)
47 #define  ourgc          DefaultGC(thedisplay,ourscreen)
45  
46   #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
47  
# Line 55 | Line 52 | double  gamcor = 2.2;                  /* gamma correction */
52   int  dither = 1;                        /* dither colors? */
53   int  fast = 0;                          /* keep picture in Pixmap? */
54  
55 + char    *dispname = NULL;               /* our display name */
56 +
57   Window  wind = 0;                       /* our output window */
58 + unsigned long  ourblack=1, ourwhite=0;  /* black and white for this visual */
59   Font  fontid;                           /* our font */
60  
61   int  maxcolors = 0;                     /* maximum colors */
# Line 83 | Line 83 | double  exposure = 1.0;                        /* exposure compensation use
83  
84   int  wrongformat = 0;                   /* input in another format? */
85  
86 + GC      ourgc;                          /* standard graphics context */
87   GC      revgc;                          /* graphics context with GXinvert */
88  
89 < XRASTER *ourras;                        /* our stored image */
89 > int             *ourrank;               /* our visual class ranking */
90 > XVisualInfo     ourvis;                 /* our visual */
91 > XRASTER         *ourras;                /* our stored image */
92   unsigned char   *ourdata;               /* our image data */
93  
94   struct {
# Line 131 | Line 134 | char  *argv[];
134                                  maxcolors = 2;
135                                  break;
136                          case 'd':
137 <                                dither = !dither;
137 >                                if (argv[i][2] == 'i')
138 >                                        dispname = argv[++i];
139 >                                else
140 >                                        dither = !dither;
141                                  break;
142                          case 'f':
143                                  fast = !fast;
# Line 142 | Line 148 | char  *argv[];
148                                  scale = atoi(argv[++i]);
149                                  break;
150                          case 'g':
151 <                                if (!strcmp(argv[i], "-geometry"))
151 >                                if (argv[i][2] == 'e')
152                                          geometry = argv[++i];
153                                  else
154                                          gamcor = atof(argv[++i]);
# Line 159 | Line 165 | char  *argv[];
165                  fname = argv[i];
166                  fin = fopen(fname, "r");
167                  if (fin == NULL) {
168 <                        sprintf(errmsg, "can't open file \"%s\"", fname);
168 >                        sprintf(errmsg, "cannot open file \"%s\"", fname);
169                          quiterr(errmsg);
170                  }
171          } else if (i != argc)
# Line 183 | Line 189 | char  *argv[];
189                  getevent();             /* main loop */
190   userr:
191          fprintf(stderr,
192 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
192 >        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
193                          progname);
194          quit(1);
195   }
# Line 208 | Line 214 | init()                 /* get data and open window */
214   {
215          XWMHints        ourxwmhints;
216          XSetWindowAttributes    ourwinattr;
217 <        XSizeHints  oursizhints;
218 <        register int  i;
217 >        XSizeHints      oursizhints;
218 >        register int    i;
219          
220          if (fname != NULL) {
221                  scanpos = (long *)malloc(ymax*sizeof(long));
# Line 218 | Line 224 | init()                 /* get data and open window */
224                  for (i = 0; i < ymax; i++)
225                          scanpos[i] = -1;
226          }
227 <        if ((thedisplay = XOpenDisplay(NULL)) == NULL)
228 <                quiterr("can't open display; DISPLAY variable set?");
229 <        if (maxcolors == 0) {           /* get number of available colors */
230 <                i = DisplayPlanes(thedisplay,ourscreen);
225 <                maxcolors = i > 8 ? 256 : 1<<i;
226 <                if (maxcolors > 4) maxcolors -= 2;
227 <        }
227 >        if ((thedisplay = XOpenDisplay(dispname)) == NULL)
228 >                quiterr("cannot open display");
229 >                                /* get best visual for default screen */
230 >        getbestvis();
231                                  /* store image */
232          getras();
233                                  /* open window */
234          ourwinattr.border_pixel = ourblack;
235          ourwinattr.background_pixel = ourwhite;
236 +        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
237 +                        ourvis.visual, AllocNone);
238          wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
239 <                        0, InputOutput, ourras->visual,
240 <                        CWBackPixel|CWBorderPixel, &ourwinattr);
239 >                        ourvis.depth, InputOutput, ourvis.visual,
240 >                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
241          if (wind == 0)
242 <                quiterr("can't create window");
242 >                quiterr("cannot create window");
243          width = xmax;
244          height = ymax;
245 +        ourgc = XCreateGC(thedisplay, wind, 0, 0);
246 +        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
247 +        revgc = XCreateGC(thedisplay, wind, 0, 0);
248 +        XSetFunction(thedisplay, revgc, GXinvert);
249          fontid = XLoadFont(thedisplay, FONTNAME);
250          if (fontid == 0)
251 <                quiterr("can't get font");
251 >                quiterr("cannot get font");
252          XSetFont(thedisplay, ourgc, fontid);
244        revgc = XCreateGC(thedisplay, wind, 0, 0);
245        XSetFunction(thedisplay, revgc, GXinvert);
253          XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
254                          XC_diamond_cross));
255          XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
# Line 309 | Line 316 | int  code;
316   }
317  
318  
319 + static int
320 + viscmp(v1,v2)           /* compare visual to see which is better, descending */
321 + register XVisualInfo    *v1, *v2;
322 + {
323 +        int     bad1 = 0, bad2 = 0;
324 +        register int  *rp;
325 +
326 +        if (v1->class == v2->class) {
327 +                if (v1->class == TrueColor || v1->class == DirectColor) {
328 +                                        /* prefer 24-bit to 32-bit */
329 +                        if (v1->depth == 24 && v2->depth == 32)
330 +                                return(-1);
331 +                        if (v1->depth == 32 && v2->depth == 24)
332 +                                return(1);
333 +                        return(0);
334 +                }
335 +                                        /* don't be too greedy */
336 +                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
337 +                        return(v1->depth - v2->depth);
338 +                return(v2->depth - v1->depth);
339 +        }
340 +                                        /* prefer Pseudo when < 24-bit */
341 +        if ((v1->class == TrueColor || v1->class == DirectColor) &&
342 +                        v1->depth < 24)
343 +                bad1 = 1;
344 +        if ((v2->class == TrueColor || v2->class == DirectColor) &&
345 +                        v2->depth < 24)
346 +                bad2 = -1;
347 +        if (bad1 | bad2)
348 +                return(bad1+bad2);
349 +                                        /* otherwise, use class ranking */
350 +        for (rp = ourrank; *rp != -1; rp++) {
351 +                if (v1->class == *rp)
352 +                        return(-1);
353 +                if (v2->class == *rp)
354 +                        return(1);
355 +        }
356 +        return(0);
357 + }
358 +
359 +
360 + getbestvis()                    /* get the best visual for this screen */
361 + {
362 + #ifdef DEBUG
363 + static char  vistype[][12] = {
364 +                "StaticGray",
365 +                "GrayScale",
366 +                "StaticColor",
367 +                "PseudoColor",
368 +                "TrueColor",
369 +                "DirectColor"
370 + };
371 + #endif
372 +        static int      rankings[3][6] = {
373 +                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
374 +                {PseudoColor,GrayScale,StaticGray,-1},
375 +                {PseudoColor,GrayScale,StaticGray,-1}
376 +        };
377 +        XVisualInfo     *xvi;
378 +        int     vismatched;
379 +        register int    i, j;
380 +
381 +        if (greyscale) {
382 +                ourrank = rankings[2];
383 +                if (maxcolors < 2) maxcolors = 256;
384 +        } else if (maxcolors >= 2 && maxcolors <= 256)
385 +                ourrank = rankings[1];
386 +        else {
387 +                ourrank = rankings[0];
388 +                maxcolors = 256;
389 +        }
390 +                                        /* find best visual */
391 +        ourvis.screen = ourscreen;
392 +        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
393 +        if (xvi == NULL)
394 +                quiterr("no visuals for this screen!");
395 + #ifdef DEBUG
396 +        fprintf(stderr, "Supported visuals:\n");
397 +        for (i = 0; i < vismatched; i++)
398 +                fprintf(stderr, "\ttype %s, depth %d\n",
399 +                                vistype[xvi[i].class], xvi[i].depth);
400 + #endif
401 +        for (i = 0, j = 1; j < vismatched; j++)
402 +                if (viscmp(&xvi[i],&xvi[j]) > 0)
403 +                        i = j;
404 +                                        /* compare to least acceptable */
405 +        for (j = 0; ourrank[j++] != -1; )
406 +                ;
407 +        ourvis.class = ourrank[--j];
408 +        ourvis.depth = 1;
409 +        if (viscmp(&xvi[i],&ourvis) > 0)
410 +                quiterr("inadequate visuals on this screen");
411 +                                        /* OK, we'll use it */
412 +        copystruct(&ourvis, &xvi[i]);
413 + #ifdef DEBUG
414 +        fprintf(stderr, "Selected visual type %s, depth %d\n",
415 +                        vistype[ourvis.class], ourvis.depth);
416 + #endif
417 +                                        /* make appropriate adjustments */
418 +        if (ourvis.class == GrayScale || ourvis.class == StaticGray)
419 +                greyscale = 1;
420 +        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
421 +                maxcolors = ourvis.colormap_size;
422 +        if (maxcolors > 4)
423 +                maxcolors -= 2;
424 +        if (ourvis.class == StaticGray) {
425 +                ourblack = 0;
426 +                ourwhite = 255;
427 +        } else if (ourvis.class == PseudoColor) {
428 +                ourblack = BlackPixel(thedisplay,ourscreen);
429 +                ourwhite = WhitePixel(thedisplay,ourscreen);
430 +        } else {
431 +                ourblack = 0;
432 +                ourwhite = ~0;
433 +        }
434 +        XFree((char *)xvi);
435 + }
436 +
437 +
438   getras()                                /* get raster file */
439   {
440          colormap        ourmap;
# Line 318 | Line 444 | getras()                               /* get raster file */
444                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
445                  if (ourdata == NULL)
446                          goto fail;
447 <                ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
447 >                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
448                                  xmax, ymax, 8);
449                  if (ourras == NULL)
450                          goto fail;
451                  getmono();
452 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)
453 <                                                /* kludge for DirectColor */
328 <        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) {
329 <                ourdata = (unsigned char *)malloc(xmax*ymax*3);
452 >        } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
453 >                ourdata = (unsigned char *)malloc(4*xmax*ymax);
454                  if (ourdata == NULL)
455                          goto fail;
456 <                ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
457 <                                xmax, ymax, 8);
456 >                ourras = make_raster(thedisplay, &ourvis, 32,
457 >                                ourdata, xmax, ymax, 32);
458                  if (ourras == NULL)
459                          goto fail;
460                  getfull();
# Line 338 | Line 462 | getras()                               /* get raster file */
462                  ourdata = (unsigned char *)malloc(xmax*ymax);
463                  if (ourdata == NULL)
464                          goto fail;
465 <                ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
465 >                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
466                                  xmax, ymax, 8);
467                  if (ourras == NULL)
468                          goto fail;
469 <                if (greyscale)
470 <                        biq(dither,maxcolors,1,ourmap);
471 <                else
472 <                        ciq(dither,maxcolors,1,ourmap);
473 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
474 <                        goto fail;
469 >                if (ourvis.class == StaticGray)
470 >                        getgrey();
471 >                else {
472 >                        if (greyscale)
473 >                                biq(dither,maxcolors,1,ourmap);
474 >                        else
475 >                                ciq(dither,maxcolors,1,ourmap);
476 >                        if (init_rcolors(ourras, ourmap[0],
477 >                                        ourmap[1], ourmap[2]) == 0)
478 >                                goto fail;
479 >                }
480          }
481 <        return;
481 >                return;
482   fail:
483          quiterr("could not create raster image");
484   }
# Line 377 | Line 506 | getevent()                             /* process the next event */
506          case MapNotify:
507                  map_rcolors(ourras, wind);
508                  if (fast)
509 <                        make_rpixmap(ourras);
509 >                        make_rpixmap(ourras, wind);
510                  break;
511          case UnmapNotify:
512 <                unmap_rcolors(ourras);
512 >                if (!fast)
513 >                        unmap_rcolors(ourras);
514                  break;
515          case Expose:
516                  redraw(e.e.x, e.e.y, e.e.width, e.e.height);
# Line 709 | Line 839 | COLR  *scan;
839   getfull()                       /* get full (24-bit) data */
840   {
841          int     y;
842 +        register unsigned long  *dp;
843 +        register int    x;
844 +                                        /* set gamma correction */
845 +        setcolrgam(gamcor);
846 +                                        /* read and convert file */
847 +        dp = (unsigned long *)ourdata;
848 +        for (y = 0; y < ymax; y++) {
849 +                if (getscan(y) < 0)
850 +                        quiterr("seek error in getfull");
851 +                if (scale)
852 +                        shiftcolrs(scanline, xmax, scale);
853 +                colrs_gambs(scanline, xmax);
854 +                add2icon(y, scanline);
855 +                if (ourras->image->blue_mask & 1)
856 +                        for (x = 0; x < xmax; x++)
857 +                                *dp++ = scanline[x][RED] << 16 |
858 +                                        scanline[x][GRN] << 8 |
859 +                                        scanline[x][BLU] ;
860 +                else
861 +                        for (x = 0; x < xmax; x++)
862 +                                *dp++ = scanline[x][RED] |
863 +                                        scanline[x][GRN] << 8 |
864 +                                        scanline[x][BLU] << 16 ;
865 +        }
866 + }
867 +
868 +
869 + getgrey()                       /* get greyscale data */
870 + {
871 +        int     y;
872          register unsigned char  *dp;
873          register int    x;
874                                          /* set gamma correction */
# Line 722 | Line 882 | getfull()                      /* get full (24-bit) data */
882                          shiftcolrs(scanline, xmax, scale);
883                  colrs_gambs(scanline, xmax);
884                  add2icon(y, scanline);
885 <                for (x = 0; x < xmax; x++) {
886 <                        *dp++ = scanline[x][RED];
887 <                        *dp++ = scanline[x][GRN];
888 <                        *dp++ = scanline[x][BLU];
889 <                }
885 >                if (ourvis.colormap_size < 256)
886 >                        for (x = 0; x < xmax; x++)
887 >                                *dp++ = ((long)normbright(scanline[x]) *
888 >                                        ourvis.colormap_size + 128) >> 8;
889 >                else
890 >                        for (x = 0; x < xmax; x++)
891 >                                *dp++ = normbright(scanline[x]);
892          }
893   }
894  
# Line 822 | Line 984 | colormap  map;
984   picwritecm(map)                 /* handled elsewhere */
985   colormap  map;
986   {
987 < #ifdef DEBUG
987 > #if 0
988          register int i;
989  
990          for (i = 0; i < 256; i++)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines