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.6 by greg, Wed May 27 14:28:50 1992 UTC vs.
Revision 2.13 by greg, Mon Oct 12 12:59:22 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 27 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27  
28   #include  "color.h"
29   #include  "view.h"
30 #include  "pic.h"
30   #include  "x11raster.h"
31   #include  "random.h"
32   #include  "resolu.h"
# Line 41 | Line 40 | static char SCCSid[] = "$SunId$ LBL";
40   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
41  
42   #define  ourscreen      DefaultScreen(thedisplay)
44 #define  ourblack       BlackPixel(thedisplay,ourscreen)
45 #define  ourwhite       WhitePixel(thedisplay,ourscreen)
43   #define  ourroot        RootWindow(thedisplay,ourscreen)
44  
45   #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
# Line 57 | Line 54 | int  fast = 0;                         /* keep picture in Pixmap? */
54   char    *dispname = NULL;               /* our display name */
55  
56   Window  wind = 0;                       /* our output window */
57 + unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
58   Font  fontid;                           /* our font */
59  
60   int  maxcolors = 0;                     /* maximum colors */
# Line 105 | Line 103 | char  *progname;
103  
104   char  errmsg[128];
105  
106 + extern BYTE  clrtab[256][3];            /* global color map */
107 +
108   extern long  ftell();
109  
110   extern char  *malloc(), *calloc();
# Line 117 | Line 117 | main(argc, argv)
117   int  argc;
118   char  *argv[];
119   {
120 +        extern char  *getenv();
121 +        char  *gv;
122          int  headline();
123          int  i;
124          
125          progname = argv[0];
126 +        if ((gv = getenv("GAMMA")) != NULL)
127 +                gamcor = atof(gv);
128  
129          for (i = 1; i < argc; i++)
130                  if (argv[i][0] == '-')
# Line 135 | Line 139 | char  *argv[];
139                                  maxcolors = 2;
140                                  break;
141                          case 'd':
142 <                                if (argv[i][2] == 'i') {
142 >                                if (argv[i][2] == 'i')
143                                          dispname = argv[++i];
144 <                                        break;
145 <                                }
142 <                                dither = !dither;
144 >                                else
145 >                                        dither = !dither;
146                                  break;
147                          case 'f':
148                                  fast = !fast;
# Line 150 | Line 153 | char  *argv[];
153                                  scale = atoi(argv[++i]);
154                                  break;
155                          case 'g':
156 <                                if (!strcmp(argv[i], "-geometry"))
156 >                                if (argv[i][2] == 'e')
157                                          geometry = argv[++i];
158                                  else
159                                          gamcor = atof(argv[++i]);
# Line 191 | Line 194 | char  *argv[];
194                  getevent();             /* main loop */
195   userr:
196          fprintf(stderr,
197 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
197 >        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
198                          progname);
199          quit(1);
200   }
# Line 242 | Line 245 | init()                 /* get data and open window */
245                          CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
246          if (wind == 0)
247                  quiterr("cannot create window");
248 +        XFreeColormap(thedisplay, ourwinattr.colormap);
249          width = xmax;
250          height = ymax;
251          ourgc = XCreateGC(thedisplay, wind, 0, 0);
252 <        XSetState(thedisplay, ourgc, BlackPixel(thedisplay,ourscreen),
249 <                        WhitePixel(thedisplay,ourscreen), GXcopy, AllPlanes);
252 >        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
253          revgc = XCreateGC(thedisplay, wind, 0, 0);
254          XSetFunction(thedisplay, revgc, GXinvert);
255          fontid = XLoadFont(thedisplay, FONTNAME);
# Line 362 | Line 365 | register XVisualInfo   *v1, *v2;
365  
366   getbestvis()                    /* get the best visual for this screen */
367   {
368 + #ifdef DEBUG
369   static char  vistype[][12] = {
370                  "StaticGray",
371                  "GrayScale",
# Line 370 | Line 374 | static char  vistype[][12] = {
374                  "TrueColor",
375                  "DirectColor"
376   };
377 + #endif
378          static int      rankings[3][6] = {
379                  {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
380 <                {PseudoColor,GrayScale,-1},
381 <                {PseudoColor,GrayScale,-1}
380 >                {PseudoColor,GrayScale,StaticGray,-1},
381 >                {PseudoColor,GrayScale,StaticGray,-1}
382          };
383          XVisualInfo     *xvi;
384          int     vismatched;
# Line 393 | Line 398 | static char  vistype[][12] = {
398          xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
399          if (xvi == NULL)
400                  quiterr("no visuals for this screen!");
401 < for (i = 0; i < vismatched; i++)
402 < fprintf(stderr, "Type %s, depth %d\n", vistype[xvi[i].class], xvi[i].depth);
401 > #ifdef DEBUG
402 >        fprintf(stderr, "Supported visuals:\n");
403 >        for (i = 0; i < vismatched; i++)
404 >                fprintf(stderr, "\ttype %s, depth %d\n",
405 >                                vistype[xvi[i].class], xvi[i].depth);
406 > #endif
407          for (i = 0, j = 1; j < vismatched; j++)
408                  if (viscmp(&xvi[i],&xvi[j]) > 0)
409                          i = j;
# Line 407 | Line 416 | fprintf(stderr, "Type %s, depth %d\n", vistype[xvi[i].
416                  quiterr("inadequate visuals on this screen");
417                                          /* OK, we'll use it */
418          copystruct(&ourvis, &xvi[i]);
419 < fprintf(stderr, "Selected visual type %s, depth %d\n", vistype[ourvis.class],
420 < ourvis.depth);
419 > #ifdef DEBUG
420 >        fprintf(stderr, "Selected visual type %s, depth %d\n",
421 >                        vistype[ourvis.class], ourvis.depth);
422 > #endif
423 >                                        /* make appropriate adjustments */
424          if (ourvis.class == GrayScale || ourvis.class == StaticGray)
425                  greyscale = 1;
426 <        if (1<<ourvis.depth < maxcolors)
427 <                maxcolors = 1<<ourvis.depth;
428 <        if (maxcolors > 4)
429 <                maxcolors -= 2;
426 >        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
427 >                maxcolors = ourvis.colormap_size;
428 >        if (ourvis.class == StaticGray) {
429 >                ourblack = 0;
430 >                ourwhite = 255;
431 >        } else if (ourvis.class == PseudoColor) {
432 >                ourblack = BlackPixel(thedisplay,ourscreen);
433 >                ourwhite = WhitePixel(thedisplay,ourscreen);
434 >                if ((ourblack|ourwhite) & ~255L) {
435 >                        ourblack = 0;
436 >                        ourwhite = 1;
437 >                }
438 >                if (maxcolors > 4)
439 >                        maxcolors -= 2;
440 >        } else {
441 >                ourblack = 0;
442 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
443 >        }
444          XFree((char *)xvi);
445   }
446  
447  
448   getras()                                /* get raster file */
449   {
424        colormap        ourmap;
450          XVisualInfo     vinfo;
451  
452          if (maxcolors <= 2) {           /* monochrome */
# Line 433 | Line 458 | getras()                               /* get raster file */
458                  if (ourras == NULL)
459                          goto fail;
460                  getmono();
461 <        } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
461 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
462                  ourdata = (unsigned char *)malloc(4*xmax*ymax);
463                  if (ourdata == NULL)
464                          goto fail;
# Line 450 | Line 475 | getras()                               /* get raster file */
475                                  xmax, ymax, 8);
476                  if (ourras == NULL)
477                          goto fail;
478 <                if (greyscale)
479 <                        biq(dither,maxcolors,1,ourmap);
478 >                if (greyscale | ourvis.class == StaticGray)
479 >                        getgrey();
480                  else
481 <                        ciq(dither,maxcolors,1,ourmap);
482 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
481 >                        getmapped();
482 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
483                          goto fail;
484          }
485          return;
# Line 488 | Line 513 | getevent()                             /* process the next event */
513                          make_rpixmap(ourras, wind);
514                  break;
515          case UnmapNotify:
516 <                unmap_rcolors(ourras);
516 >                if (!fast)
517 >                        unmap_rcolors(ourras);
518                  break;
519          case Expose:
520                  redraw(e.e.x, e.e.y, e.e.width, e.e.height);
# Line 600 | Line 626 | XKeyPressedEvent  *ekey;
626                  XClearWindow(thedisplay, wind);
627                  map_rcolors(ourras, wind);
628                  if (fast)
629 <                        make_rpixmap(ourras);
629 >                        make_rpixmap(ourras, wind);
630                  redraw(0, 0, width, height);
631                  return(0);
632          case ' ':                               /* clear */
# Line 844 | Line 870 | getfull()                      /* get full (24-bit) data */
870   }
871  
872  
873 + getgrey()                       /* get greyscale data */
874 + {
875 +        int     y;
876 +        register unsigned char  *dp;
877 +        register int    x;
878 +                                        /* set gamma correction */
879 +        setcolrgam(gamcor);
880 +                                        /* read and convert file */
881 +        dp = ourdata;
882 +        for (y = 0; y < ymax; y++) {
883 +                if (getscan(y) < 0)
884 +                        quiterr("seek error in getgrey");
885 +                if (scale)
886 +                        shiftcolrs(scanline, xmax, scale);
887 +                colrs_gambs(scanline, xmax);
888 +                add2icon(y, scanline);
889 +                if (maxcolors < 256)
890 +                        for (x = 0; x < xmax; x++)
891 +                                *dp++ = ((long)normbright(scanline[x]) *
892 +                                        maxcolors + 128) >> 8;
893 +                else
894 +                        for (x = 0; x < xmax; x++)
895 +                                *dp++ = normbright(scanline[x]);
896 +        }
897 +        for (x = 0; x < maxcolors; x++)
898 +                clrtab[x][RED] = clrtab[x][GRN] =
899 +                        clrtab[x][BLU] = (x*256+maxcolors/2)/maxcolors;
900 + }
901 +
902 +
903 + getmapped()                     /* get color-mapped data */
904 + {
905 +        int     y;
906 +                                        /* set gamma correction */
907 +        setcolrgam(gamcor);
908 +                                        /* make histogram */
909 +        new_histo();
910 +        for (y = 0; y < ymax; y++) {
911 +                if (getscan(y) < 0)
912 +                        quiterr("seek error in getmapped");
913 +                if (scale)
914 +                        shiftcolrs(scanline, xmax, scale);
915 +                colrs_gambs(scanline, xmax);
916 +                add2icon(y, scanline);
917 +                cnt_colrs(scanline, xmax);
918 +        }
919 +                                        /* map pixels */
920 +        if (!new_clrtab(maxcolors))
921 +                quiterr("cannot create color map");
922 +        for (y = 0; y < ymax; y++) {
923 +                if (getscan(y) < 0)
924 +                        quiterr("seek error in getmapped");
925 +                if (scale)
926 +                        shiftcolrs(scanline, xmax, scale);
927 +                colrs_gambs(scanline, xmax);
928 +                if (dither)
929 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
930 +                else
931 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
932 +        }
933 + }
934 +
935 +
936   scale_rcolors(xr, sf)                   /* scale color map */
937   register XRASTER        *xr;
938   double  sf;
# Line 881 | Line 970 | int  y;
970                  if (fseek(fin, scanpos[y], 0) == -1)
971                          quiterr("fseek error");
972                  cury = y;
973 <        } else if (scanpos != NULL)
973 >        } else if (scanpos != NULL && scanpos[y] == -1)
974                  scanpos[y] = ftell(fin);
975  
976          if (freadcolrs(scanline, xmax, fin) < 0)
# Line 889 | Line 978 | int  y;
978  
979          cury++;
980          return(0);
892 }
893
894
895 picreadline3(y, l3)                     /* read in 3-byte scanline */
896 int  y;
897 register rgbpixel  *l3;
898 {
899        register int    i;
900                                                        /* read scanline */
901        if (getscan(y) < 0)
902                quiterr("cannot seek for picreadline");
903                                                        /* convert scanline */
904        normcolrs(scanline, xmax, scale);
905        add2icon(y, scanline);
906        for (i = 0; i < xmax; i++) {
907                l3[i].r = scanline[i][RED];
908                l3[i].g = scanline[i][GRN];
909                l3[i].b = scanline[i][BLU];
910        }
911 }
912
913
914 picwriteline(y, l)              /* add 8-bit scanline to image */
915 int  y;
916 pixel  *l;
917 {
918        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
919 }
920
921
922 picreadcm(map)                  /* do gamma correction */
923 colormap  map;
924 {
925        extern double  pow();
926        register int  i, val;
927
928        for (i = 0; i < 256; i++) {
929                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
930                map[0][i] = map[1][i] = map[2][i] = val;
931        }
932 }
933
934
935 picwritecm(map)                 /* handled elsewhere */
936 colormap  map;
937 {
938 #ifdef DEBUG
939        register int i;
940
941        for (i = 0; i < 256; i++)
942                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
943 #endif
981   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines