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.21 by greg, Fri Feb 19 10:46:16 1993 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();
111  
112 extern double  pow(), log();
113
112   Display  *thedisplay;
113  
114 +
115   main(argc, argv)
116   int  argc;
117   char  *argv[];
118   {
119 +        extern char  *getenv();
120 +        char  *gv;
121          int  headline();
122          int  i;
123          
124          progname = argv[0];
125 +        if ((gv = getenv("GAMMA")) != NULL)
126 +                gamcor = atof(gv);
127  
128          for (i = 1; i < argc; i++)
129                  if (argv[i][0] == '-')
# Line 135 | Line 138 | char  *argv[];
138                                  maxcolors = 2;
139                                  break;
140                          case 'd':
141 <                                if (argv[i][2] == 'i') {
141 >                                if (argv[i][2] == 'i')
142                                          dispname = argv[++i];
143 <                                        break;
144 <                                }
142 <                                dither = !dither;
143 >                                else
144 >                                        dither = !dither;
145                                  break;
146                          case 'f':
147                                  fast = !fast;
# Line 150 | Line 152 | char  *argv[];
152                                  scale = atoi(argv[++i]);
153                                  break;
154                          case 'g':
155 <                                if (!strcmp(argv[i], "-geometry"))
155 >                                if (argv[i][2] == 'e')
156                                          geometry = argv[++i];
157                                  else
158                                          gamcor = atof(argv[++i]);
# Line 191 | Line 193 | char  *argv[];
193                  getevent();             /* main loop */
194   userr:
195          fprintf(stderr,
196 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
196 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n",
197                          progname);
198 <        quit(1);
198 >        exit(1);
199   }
200  
201  
# Line 232 | Line 234 | init()                 /* get data and open window */
234          getbestvis();
235                                  /* store image */
236          getras();
237 +                                /* get size and position */
238 +        bzero((char *)&oursizhints, sizeof(oursizhints));
239 +        oursizhints.width = xmax; oursizhints.height = ymax;
240 +        if (geometry != NULL) {
241 +                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
242 +                                (unsigned *)&oursizhints.width,
243 +                                (unsigned *)&oursizhints.height);
244 +                if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
245 +                        oursizhints.flags |= USSize;
246 +                else
247 +                        oursizhints.flags |= PSize;
248 +                if ((i&(XValue|YValue)) == (XValue|YValue)) {
249 +                        oursizhints.flags |= USPosition;
250 +                        if (i & XNegative)
251 +                                oursizhints.x += DisplayWidth(thedisplay,
252 +                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
253 +                        if (i & YNegative)
254 +                                oursizhints.y += DisplayHeight(thedisplay,
255 +                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
256 +                }
257 +        }
258                                  /* open window */
259          ourwinattr.border_pixel = ourblack;
260          ourwinattr.background_pixel = ourwhite;
261          ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
262                          ourvis.visual, AllocNone);
263 <        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
263 >        wind = XCreateWindow(thedisplay, ourroot, oursizhints.x, oursizhints.y,
264 >                        oursizhints.width, oursizhints.height, BORWIDTH,
265                          ourvis.depth, InputOutput, ourvis.visual,
266                          CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
267          if (wind == 0)
268                  quiterr("cannot create window");
269 +        XFreeColormap(thedisplay, ourwinattr.colormap);
270          width = xmax;
271          height = ymax;
272          ourgc = XCreateGC(thedisplay, wind, 0, 0);
273 <        XSetState(thedisplay, ourgc, BlackPixel(thedisplay,ourscreen),
249 <                        WhitePixel(thedisplay,ourscreen), GXcopy, AllPlanes);
273 >        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
274          revgc = XCreateGC(thedisplay, wind, 0, 0);
275          XSetFunction(thedisplay, revgc, GXinvert);
276          fontid = XLoadFont(thedisplay, FONTNAME);
# Line 256 | Line 280 | init()                 /* get data and open window */
280          XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
281                          XC_diamond_cross));
282          XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
283 <        if (geometry != NULL) {
260 <                bzero((char *)&oursizhints, sizeof(oursizhints));
261 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
262 <                                (unsigned *)&oursizhints.width,
263 <                                (unsigned *)&oursizhints.height);
264 <                if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
265 <                        oursizhints.flags |= USSize;
266 <                else {
267 <                        oursizhints.width = xmax;
268 <                        oursizhints.height = ymax;
269 <                        oursizhints.flags |= PSize;
270 <                }
271 <                if ((i&(XValue|YValue)) == (XValue|YValue)) {
272 <                        oursizhints.flags |= USPosition;
273 <                        if (i & XNegative)
274 <                                oursizhints.x += DisplayWidth(thedisplay,
275 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
276 <                        if (i & YNegative)
277 <                                oursizhints.y += DisplayHeight(thedisplay,
278 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
279 <                }
283 >        if (oursizhints.flags)
284                  XSetNormalHints(thedisplay, wind, &oursizhints);
281        }
285          ourxwmhints.flags = InputHint|IconPixmapHint;
286          ourxwmhints.input = True;
287          ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
# Line 305 | Line 308 | char  *err;
308   }
309  
310  
308 eputs(s)
309 char    *s;
310 {
311        fputs(s, stderr);
312 }
313
314
315 quit(code)
316 int  code;
317 {
318        exit(code);
319 }
320
321
311   static int
312   viscmp(v1,v2)           /* compare visual to see which is better, descending */
313   register XVisualInfo    *v1, *v2;
# Line 362 | Line 351 | register XVisualInfo   *v1, *v2;
351  
352   getbestvis()                    /* get the best visual for this screen */
353   {
354 + #ifdef DEBUG
355   static char  vistype[][12] = {
356                  "StaticGray",
357                  "GrayScale",
# Line 370 | Line 360 | static char  vistype[][12] = {
360                  "TrueColor",
361                  "DirectColor"
362   };
363 + #endif
364          static int      rankings[3][6] = {
365                  {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
366 <                {PseudoColor,GrayScale,-1},
367 <                {PseudoColor,GrayScale,-1}
366 >                {PseudoColor,GrayScale,StaticGray,-1},
367 >                {PseudoColor,GrayScale,StaticGray,-1}
368          };
369          XVisualInfo     *xvi;
370          int     vismatched;
# Line 393 | Line 384 | static char  vistype[][12] = {
384          xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
385          if (xvi == NULL)
386                  quiterr("no visuals for this screen!");
387 < for (i = 0; i < vismatched; i++)
388 < fprintf(stderr, "Type %s, depth %d\n", vistype[xvi[i].class], xvi[i].depth);
387 > #ifdef DEBUG
388 >        fprintf(stderr, "Supported visuals:\n");
389 >        for (i = 0; i < vismatched; i++)
390 >                fprintf(stderr, "\ttype %s, depth %d\n",
391 >                                vistype[xvi[i].class], xvi[i].depth);
392 > #endif
393          for (i = 0, j = 1; j < vismatched; j++)
394                  if (viscmp(&xvi[i],&xvi[j]) > 0)
395                          i = j;
# Line 407 | Line 402 | fprintf(stderr, "Type %s, depth %d\n", vistype[xvi[i].
402                  quiterr("inadequate visuals on this screen");
403                                          /* OK, we'll use it */
404          copystruct(&ourvis, &xvi[i]);
405 < fprintf(stderr, "Selected visual type %s, depth %d\n", vistype[ourvis.class],
406 < ourvis.depth);
405 > #ifdef DEBUG
406 >        fprintf(stderr, "Selected visual type %s, depth %d\n",
407 >                        vistype[ourvis.class], ourvis.depth);
408 > #endif
409 >                                        /* make appropriate adjustments */
410          if (ourvis.class == GrayScale || ourvis.class == StaticGray)
411                  greyscale = 1;
412 <        if (1<<ourvis.depth < maxcolors)
413 <                maxcolors = 1<<ourvis.depth;
414 <        if (maxcolors > 4)
415 <                maxcolors -= 2;
412 >        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
413 >                maxcolors = ourvis.colormap_size;
414 >        if (ourvis.class == StaticGray) {
415 >                ourblack = 0;
416 >                ourwhite = 255;
417 >        } else if (ourvis.class == PseudoColor) {
418 >                ourblack = BlackPixel(thedisplay,ourscreen);
419 >                ourwhite = WhitePixel(thedisplay,ourscreen);
420 >                if ((ourblack|ourwhite) & ~255L) {
421 >                        ourblack = 0;
422 >                        ourwhite = 1;
423 >                }
424 >                if (maxcolors > 4)
425 >                        maxcolors -= 2;
426 >        } else {
427 >                ourblack = 0;
428 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
429 >        }
430          XFree((char *)xvi);
431   }
432  
433  
434   getras()                                /* get raster file */
435   {
424        colormap        ourmap;
436          XVisualInfo     vinfo;
437  
438          if (maxcolors <= 2) {           /* monochrome */
# Line 433 | Line 444 | getras()                               /* get raster file */
444                  if (ourras == NULL)
445                          goto fail;
446                  getmono();
447 <        } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
447 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
448                  ourdata = (unsigned char *)malloc(4*xmax*ymax);
449                  if (ourdata == NULL)
450                          goto fail;
# Line 450 | Line 461 | getras()                               /* get raster file */
461                                  xmax, ymax, 8);
462                  if (ourras == NULL)
463                          goto fail;
464 <                if (greyscale)
465 <                        biq(dither,maxcolors,1,ourmap);
464 >                if (greyscale | ourvis.class == StaticGray)
465 >                        getgrey();
466                  else
467 <                        ciq(dither,maxcolors,1,ourmap);
468 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
467 >                        getmapped();
468 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
469                          goto fail;
470          }
471          return;
# Line 488 | Line 499 | getevent()                             /* process the next event */
499                          make_rpixmap(ourras, wind);
500                  break;
501          case UnmapNotify:
502 <                unmap_rcolors(ourras);
502 >                if (!fast)
503 >                        unmap_rcolors(ourras);
504                  break;
505          case Expose:
506                  redraw(e.e.x, e.e.y, e.e.width, e.e.height);
# Line 521 | Line 533 | XKeyPressedEvent  *ekey;
533          switch (com) {                  /* interpret command */
534          case 'q':
535          case CTRL('D'):                         /* quit */
536 <                quit(0);
536 >                quiterr(NULL);
537          case '\n':
538          case '\r':
539          case 'l':
# Line 600 | Line 612 | XKeyPressedEvent  *ekey;
612                  XClearWindow(thedisplay, wind);
613                  map_rcolors(ourras, wind);
614                  if (fast)
615 <                        make_rpixmap(ourras);
615 >                        make_rpixmap(ourras, wind);
616                  redraw(0, 0, width, height);
617                  return(0);
618 +        case '0':                               /* recenter origin */
619 +                if (xoff == 0 & yoff == 0)
620 +                        return(0);
621 +                xoff = yoff = 0;
622 +                XClearWindow(thedisplay, wind);
623 +                redraw(0, 0, width, height);
624 +                return(0);
625          case ' ':                               /* clear */
626                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
627                  return(0);
# Line 714 | Line 733 | COLOR  clr;
733                  return(-1);
734          if (left == ll && right == lr && top == lt && bottom == lb) {
735                  copycolor(clr, lc);
736 <                return;
736 >                return(0);
737          }
738          for (y = top; y < bottom; y++) {
739                  if (getscan(y) == -1)
# Line 743 | Line 762 | getmono()                      /* get monochrome data */
762                  quiterr("out of memory in getmono");
763          dp = ourdata - 1;
764          for (y = 0; y < ymax; y++) {
765 <                if (getscan(y) < 0)
747 <                        quiterr("seek error in getmono");
748 <                normcolrs(scanline, xmax, scale);
765 >                getscan(y);
766                  add2icon(y, scanline);
767 +                normcolrs(scanline, xmax, scale);
768                  err = 0;
769                  for (x = 0; x < xmax; x++) {
770                          if (!(x&7))
# Line 772 | Line 790 | COLR  *scan;
790          static short  cerr[ICONSIZ];
791          static int  ynext;
792          static char  *dp;
793 +        COLR  clr;
794          register int  err;
795          register int    x, ti;
796          int  errp;
# Line 802 | Line 821 | COLR  *scan;
821                          *++dp = 0;
822                  errp = err;
823                  ti = x*xmax/iconwidth;
824 <                err += normbright(scan[ti]) + cerr[x];
824 >                copycolr(clr, scan[ti]);
825 >                normcolrs(clr, 1, scale);
826 >                err += normbright(clr) + cerr[x];
827                  if (err > 127)
828                          err -= 255;
829                  else
# Line 824 | Line 845 | getfull()                      /* get full (24-bit) data */
845                                          /* read and convert file */
846          dp = (unsigned long *)ourdata;
847          for (y = 0; y < ymax; y++) {
848 <                if (getscan(y) < 0)
849 <                        quiterr("seek error in getfull");
848 >                getscan(y);
849 >                add2icon(y, scanline);
850                  if (scale)
851                          shiftcolrs(scanline, xmax, scale);
852                  colrs_gambs(scanline, xmax);
832                add2icon(y, scanline);
853                  if (ourras->image->blue_mask & 1)
854                          for (x = 0; x < xmax; x++)
855                                  *dp++ = scanline[x][RED] << 16 |
# Line 844 | Line 864 | getfull()                      /* get full (24-bit) data */
864   }
865  
866  
867 + getgrey()                       /* get greyscale data */
868 + {
869 +        int     y;
870 +        register unsigned char  *dp;
871 +        register int    x;
872 +                                        /* set gamma correction */
873 +        setcolrgam(gamcor);
874 +                                        /* read and convert file */
875 +        dp = ourdata;
876 +        for (y = 0; y < ymax; y++) {
877 +                getscan(y);
878 +                add2icon(y, scanline);
879 +                if (scale)
880 +                        shiftcolrs(scanline, xmax, scale);
881 +                for (x = 0; x < xmax; x++)
882 +                        scanline[x][GRN] = normbright(scanline[x]);
883 +                colrs_gambs(scanline, xmax);
884 +                if (maxcolors < 256)
885 +                        for (x = 0; x < xmax; x++)
886 +                                *dp++ = ((long)scanline[x][GRN] *
887 +                                        maxcolors + maxcolors/2) >> 8;
888 +                else
889 +                        for (x = 0; x < xmax; x++)
890 +                                *dp++ = scanline[x][GRN];
891 +        }
892 +        for (x = 0; x < maxcolors; x++)
893 +                clrtab[x][RED] = clrtab[x][GRN] =
894 +                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
895 + }
896 +
897 +
898 + getmapped()                     /* get color-mapped data */
899 + {
900 +        int     y;
901 +                                        /* set gamma correction */
902 +        setcolrgam(gamcor);
903 +                                        /* make histogram */
904 +        new_histo();
905 +        for (y = 0; y < ymax; y++) {
906 +                if (getscan(y) < 0)
907 +                        quiterr("seek error in getmapped");
908 +                add2icon(y, scanline);
909 +                if (scale)
910 +                        shiftcolrs(scanline, xmax, scale);
911 +                colrs_gambs(scanline, xmax);
912 +                cnt_colrs(scanline, xmax);
913 +        }
914 +                                        /* map pixels */
915 +        if (!new_clrtab(maxcolors))
916 +                quiterr("cannot create color map");
917 +        for (y = 0; y < ymax; y++) {
918 +                if (getscan(y) < 0)
919 +                        quiterr("seek error in getmapped");
920 +                if (scale)
921 +                        shiftcolrs(scanline, xmax, scale);
922 +                colrs_gambs(scanline, xmax);
923 +                if (dither)
924 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
925 +                else
926 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
927 +        }
928 + }
929 +
930 +
931   scale_rcolors(xr, sf)                   /* scale color map */
932   register XRASTER        *xr;
933   double  sf;
# Line 881 | Line 965 | int  y;
965                  if (fseek(fin, scanpos[y], 0) == -1)
966                          quiterr("fseek error");
967                  cury = y;
968 <        } else if (scanpos != NULL)
968 >        } else if (scanpos != NULL && scanpos[y] == -1)
969                  scanpos[y] = ftell(fin);
970  
971          if (freadcolrs(scanline, xmax, fin) < 0)
# Line 889 | Line 973 | int  y;
973  
974          cury++;
975          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
976   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines