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.10 by greg, Wed Jun 24 09:17:39 1992 UTC vs.
Revision 2.23 by greg, Tue Mar 2 11:10:15 1993 UTC

# 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 104 | 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  
111 extern double  pow(), log();
112
112   Display  *thedisplay;
113  
114 +
115   main(argc, argv)
116   int  argc;
117   char  *argv[];
# Line 193 | Line 193 | char  *argv[];
193                  getevent();             /* main loop */
194   userr:
195          fprintf(stderr,
196 <        "Usage: %s [-display disp][-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 234 | 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;
259 >        ourwinattr.border_pixel = ourwhite;
260 >        ourwinattr.background_pixel = ourblack;
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)
# Line 258 | 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) {
262 <                bzero((char *)&oursizhints, sizeof(oursizhints));
263 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
264 <                                (unsigned *)&oursizhints.width,
265 <                                (unsigned *)&oursizhints.height);
266 <                if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
267 <                        oursizhints.flags |= USSize;
268 <                else {
269 <                        oursizhints.width = xmax;
270 <                        oursizhints.height = ymax;
271 <                        oursizhints.flags |= PSize;
272 <                }
273 <                if ((i&(XValue|YValue)) == (XValue|YValue)) {
274 <                        oursizhints.flags |= USPosition;
275 <                        if (i & XNegative)
276 <                                oursizhints.x += DisplayWidth(thedisplay,
277 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
278 <                        if (i & YNegative)
279 <                                oursizhints.y += DisplayHeight(thedisplay,
280 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
281 <                }
283 >        if (oursizhints.flags)
284                  XSetNormalHints(thedisplay, wind, &oursizhints);
283        }
285          ourxwmhints.flags = InputHint|IconPixmapHint;
286          ourxwmhints.input = True;
287          ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
# Line 307 | Line 308 | char  *err;
308   }
309  
310  
310 eputs(s)
311 char    *s;
312 {
313        fputs(s, stderr);
314 }
315
316
317 quit(code)
318 int  code;
319 {
320        exit(code);
321 }
322
323
311   static int
312   viscmp(v1,v2)           /* compare visual to see which is better, descending */
313   register XVisualInfo    *v1, *v2;
# Line 424 | Line 411 | static char  vistype[][12] = {
411                  greyscale = 1;
412          if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
413                  maxcolors = ourvis.colormap_size;
427        if (maxcolors > 4)
428                maxcolors -= 2;
414          if (ourvis.class == StaticGray) {
415                  ourblack = 0;
416                  ourwhite = 255;
# Line 436 | Line 421 | static char  vistype[][12] = {
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;
# Line 446 | Line 433 | static char  vistype[][12] = {
433  
434   getras()                                /* get raster file */
435   {
449        colormap        ourmap;
436          XVisualInfo     vinfo;
437  
438          if (maxcolors <= 2) {           /* monochrome */
# Line 458 | 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 475 | Line 461 | getras()                               /* get raster file */
461                                  xmax, ymax, 8);
462                  if (ourras == NULL)
463                          goto fail;
464 <                if (ourvis.class == StaticGray)
464 >                if (greyscale | ourvis.class == StaticGray)
465                          getgrey();
466 <                else {
467 <                        if (greyscale)
468 <                                biq(dither,maxcolors,1,ourmap);
469 <                        else
484 <                                ciq(dither,maxcolors,1,ourmap);
485 <                        if (init_rcolors(ourras, ourmap[0],
486 <                                        ourmap[1], ourmap[2]) == 0)
487 <                                goto fail;
488 <                }
466 >                else
467 >                        getmapped();
468 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
469 >                        goto fail;
470          }
471 <                return;
471 >        return;
472   fail:
473          quiterr("could not create raster image");
474   }
# Line 527 | Line 508 | getevent()                             /* process the next event */
508          case ButtonPress:
509                  if (e.b.state & (ShiftMask|ControlMask))
510                          moveimage(&e.b);
511 +                else if (e.b.button == Button2)
512 +                        traceray(e.b.x, e.b.y);
513                  else
514                          getbox(&e.b);
515                  break;
# Line 534 | Line 517 | getevent()                             /* process the next event */
517   }
518  
519  
520 < docom(ekey)                                     /* execute command */
520 > traceray(xpos, ypos)                    /* print ray corresponding to pixel */
521 > int  xpos, ypos;
522 > {
523 >        FLOAT  hv[2];
524 >        FVECT  rorg, rdir;
525 >
526 >        if (!gotview) {         /* no view, no can do */
527 >                XBell(thedisplay, 0);
528 >                return(-1);
529 >        }
530 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
531 >        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
532 >                return(-1);
533 >        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
534 >        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
535 >        fflush(stdout);
536 >        return(0);
537 > }
538 >
539 >
540 > docom(ekey)                             /* execute command */
541   XKeyPressedEvent  *ekey;
542   {
543          char  buf[80];
# Line 543 | Line 546 | XKeyPressedEvent  *ekey;
546          int  com, n;
547          double  comp;
548          FLOAT  hv[2];
546        FVECT  rorg, rdir;
549  
550          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
551          if (n == 0)
# Line 551 | Line 553 | XKeyPressedEvent  *ekey;
553          com = buf[0];
554          switch (com) {                  /* interpret command */
555          case 'q':
556 +        case 'Q':
557          case CTRL('D'):                         /* quit */
558 <                quit(0);
558 >                quiterr(NULL);
559          case '\n':
560          case '\r':
561          case 'l':
# Line 598 | Line 601 | XKeyPressedEvent  *ekey;
601                                          buf, strlen(buf));
602                  return(0);
603          case 't':                               /* trace */
604 <                if (!gotview) {
602 <                        XBell(thedisplay, 0);
603 <                        return(-1);
604 <                }
605 <                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
606 <                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
607 <                        return(-1);
608 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
609 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
610 <                fflush(stdout);
611 <                return(0);
604 >                return(traceray(ekey->x, ekey->y));
605          case '=':                               /* adjust exposure */
606                  if (avgbox(cval) == -1)
607                          return(-1);
# Line 631 | Line 624 | XKeyPressedEvent  *ekey;
624                  XClearWindow(thedisplay, wind);
625                  map_rcolors(ourras, wind);
626                  if (fast)
627 <                        make_rpixmap(ourras);
627 >                        make_rpixmap(ourras, wind);
628                  redraw(0, 0, width, height);
629                  return(0);
630 +        case '0':                               /* recenter origin */
631 +                if (xoff == 0 & yoff == 0)
632 +                        return(0);
633 +                xoff = yoff = 0;
634 +                XClearWindow(thedisplay, wind);
635 +                redraw(0, 0, width, height);
636 +                return(0);
637          case ' ':                               /* clear */
638                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
639                  return(0);
# Line 745 | Line 745 | COLOR  clr;
745                  return(-1);
746          if (left == ll && right == lr && top == lt && bottom == lb) {
747                  copycolor(clr, lc);
748 <                return;
748 >                return(0);
749          }
750          for (y = top; y < bottom; y++) {
751                  if (getscan(y) == -1)
# Line 774 | Line 774 | getmono()                      /* get monochrome data */
774                  quiterr("out of memory in getmono");
775          dp = ourdata - 1;
776          for (y = 0; y < ymax; y++) {
777 <                if (getscan(y) < 0)
778 <                        quiterr("seek error in getmono");
779 <                normcolrs(scanline, xmax, scale);
777 >                getscan(y);
778                  add2icon(y, scanline);
779 +                normcolrs(scanline, xmax, scale);
780                  err = 0;
781                  for (x = 0; x < xmax; x++) {
782                          if (!(x&7))
# Line 803 | Line 802 | COLR  *scan;
802          static short  cerr[ICONSIZ];
803          static int  ynext;
804          static char  *dp;
805 +        COLR  clr;
806          register int  err;
807          register int    x, ti;
808          int  errp;
# Line 833 | Line 833 | COLR  *scan;
833                          *++dp = 0;
834                  errp = err;
835                  ti = x*xmax/iconwidth;
836 <                err += normbright(scan[ti]) + cerr[x];
836 >                copycolr(clr, scan[ti]);
837 >                normcolrs(clr, 1, scale);
838 >                err += normbright(clr) + cerr[x];
839                  if (err > 127)
840                          err -= 255;
841                  else
# Line 855 | Line 857 | getfull()                      /* get full (24-bit) data */
857                                          /* read and convert file */
858          dp = (unsigned long *)ourdata;
859          for (y = 0; y < ymax; y++) {
860 <                if (getscan(y) < 0)
861 <                        quiterr("seek error in getfull");
860 >                getscan(y);
861 >                add2icon(y, scanline);
862                  if (scale)
863                          shiftcolrs(scanline, xmax, scale);
864                  colrs_gambs(scanline, xmax);
863                add2icon(y, scanline);
865                  if (ourras->image->blue_mask & 1)
866                          for (x = 0; x < xmax; x++)
867                                  *dp++ = scanline[x][RED] << 16 |
# Line 885 | Line 886 | getgrey()                      /* get greyscale data */
886                                          /* read and convert file */
887          dp = ourdata;
888          for (y = 0; y < ymax; y++) {
889 <                if (getscan(y) < 0)
890 <                        quiterr("seek error in getfull");
889 >                getscan(y);
890 >                add2icon(y, scanline);
891                  if (scale)
892                          shiftcolrs(scanline, xmax, scale);
893 +                for (x = 0; x < xmax; x++)
894 +                        scanline[x][GRN] = normbright(scanline[x]);
895                  colrs_gambs(scanline, xmax);
896 <                add2icon(y, scanline);
894 <                if (ourvis.colormap_size < 256)
896 >                if (maxcolors < 256)
897                          for (x = 0; x < xmax; x++)
898 <                                *dp++ = ((long)normbright(scanline[x]) *
899 <                                        ourvis.colormap_size + 128) >> 8;
898 >                                *dp++ = ((long)scanline[x][GRN] *
899 >                                        maxcolors + maxcolors/2) >> 8;
900                  else
901                          for (x = 0; x < xmax; x++)
902 <                                *dp++ = normbright(scanline[x]);
902 >                                *dp++ = scanline[x][GRN];
903          }
904 +        for (x = 0; x < maxcolors; x++)
905 +                clrtab[x][RED] = clrtab[x][GRN] =
906 +                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
907   }
908  
909  
910 + getmapped()                     /* get color-mapped data */
911 + {
912 +        int     y;
913 +                                        /* set gamma correction */
914 +        setcolrgam(gamcor);
915 +                                        /* make histogram */
916 +        new_histo();
917 +        for (y = 0; y < ymax; y++) {
918 +                if (getscan(y) < 0)
919 +                        quiterr("seek error in getmapped");
920 +                add2icon(y, scanline);
921 +                if (scale)
922 +                        shiftcolrs(scanline, xmax, scale);
923 +                colrs_gambs(scanline, xmax);
924 +                cnt_colrs(scanline, xmax);
925 +        }
926 +                                        /* map pixels */
927 +        if (!new_clrtab(maxcolors))
928 +                quiterr("cannot create color map");
929 +        for (y = 0; y < ymax; y++) {
930 +                if (getscan(y) < 0)
931 +                        quiterr("seek error in getmapped");
932 +                if (scale)
933 +                        shiftcolrs(scanline, xmax, scale);
934 +                colrs_gambs(scanline, xmax);
935 +                if (dither)
936 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
937 +                else
938 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
939 +        }
940 + }
941 +
942 +
943   scale_rcolors(xr, sf)                   /* scale color map */
944   register XRASTER        *xr;
945   double  sf;
# Line 939 | Line 977 | int  y;
977                  if (fseek(fin, scanpos[y], 0) == -1)
978                          quiterr("fseek error");
979                  cury = y;
980 <        } else if (scanpos != NULL)
980 >        } else if (scanpos != NULL && scanpos[y] == -1)
981                  scanpos[y] = ftell(fin);
982  
983          if (freadcolrs(scanline, xmax, fin) < 0)
# Line 947 | Line 985 | int  y;
985  
986          cury++;
987          return(0);
950 }
951
952
953 picreadline3(y, l3)                     /* read in 3-byte scanline */
954 int  y;
955 register rgbpixel  *l3;
956 {
957        register int    i;
958                                                        /* read scanline */
959        if (getscan(y) < 0)
960                quiterr("cannot seek for picreadline");
961                                                        /* convert scanline */
962        normcolrs(scanline, xmax, scale);
963        add2icon(y, scanline);
964        for (i = 0; i < xmax; i++) {
965                l3[i].r = scanline[i][RED];
966                l3[i].g = scanline[i][GRN];
967                l3[i].b = scanline[i][BLU];
968        }
969 }
970
971
972 picwriteline(y, l)              /* add 8-bit scanline to image */
973 int  y;
974 pixel  *l;
975 {
976        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
977 }
978
979
980 picreadcm(map)                  /* do gamma correction */
981 colormap  map;
982 {
983        extern double  pow();
984        register int  i, val;
985
986        for (i = 0; i < 256; i++) {
987                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
988                map[0][i] = map[1][i] = map[2][i] = val;
989        }
990 }
991
992
993 picwritecm(map)                 /* handled elsewhere */
994 colormap  map;
995 {
996 #if 0
997        register int i;
998
999        for (i = 0; i < 256; i++)
1000                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
1001 #endif
988   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines