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 1.1 by greg, Thu Mar 1 13:18:22 1990 UTC vs.
Revision 1.16 by greg, Wed May 1 12:40:00 1991 UTC

# Line 30 | Line 30 | static char SCCSid[] = "$SunId$ LBL";
30   #include  "pic.h"
31   #include  "x11raster.h"
32   #include  "random.h"
33 + #include  "x11icon.h"
34  
35   #define  FONTNAME       "8x13"          /* text font we'll use */
36  
# Line 43 | Line 44 | static char SCCSid[] = "$SunId$ LBL";
44   #define  ourroot        RootWindow(thedisplay,ourscreen)
45   #define  ourgc          DefaultGC(thedisplay,ourscreen)
46  
47 + #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
48 +
49 + #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
50 +
51   double  gamcor = 2.2;                   /* gamma correction */
52  
53   int  dither = 1;                        /* dither colors? */
# Line 51 | Line 56 | int  fast = 0;                         /* keep picture in Pixmap? */
56   Window  wind = 0;                       /* our output window */
57   Font  fontid;                           /* our font */
58  
59 < long  maxcolors = 0;                    /* maximum colors */
59 > int  maxcolors = 0;                     /* maximum colors */
60   int  greyscale = 0;                     /* in grey */
61  
62   int  scale = 0;                         /* scalefactor; power of two */
# Line 73 | Line 78 | int  cury = 0;                         /* current scan location */
78  
79   double  exposure = 1.0;                 /* exposure compensation used */
80  
81 + int  wrongformat = 0;                   /* input in another format? */
82 +
83 + GC      revgc;                          /* graphics context with GXinvert */
84 +
85   XRASTER *ourras;                        /* our stored image */
86   unsigned char   *ourdata;               /* our image data */
87  
# Line 135 | Line 144 | char  *argv[];
144                          default:
145                                  goto userr;
146                          }
147 +                else if (argv[i][0] == '=')
148 +                        geometry = argv[i];
149                  else
150                          break;
151  
152 <        if (argc-i == 1) {
152 >        if (i == argc-1) {
153                  fname = argv[i];
154                  fin = fopen(fname, "r");
155                  if (fin == NULL) {
156                          sprintf(errmsg, "can't open file \"%s\"", fname);
157                          quiterr(errmsg);
158                  }
159 <        }
159 >        } else if (i != argc)
160 >                goto userr;
161                                  /* get header */
162 <        getheader(fin, headline);
162 >        getheader(fin, headline, NULL);
163                                  /* get picture dimensions */
164 <        if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
165 <                quiterr("bad picture size");
164 >        if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
165 >                quiterr("bad picture format");
166                                  /* set view parameters */
167          if (gotview && setview(&ourview) != NULL)
168                  gotview = 0;
# Line 174 | Line 186 | char  *s;
186   {
187          static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
188          register char  **an;
189 +        char  fmt[32];
190  
191 <        if (!strncmp(s, "EXPOSURE=", 9))
192 <                exposure *= atof(s+9);
193 <        else
191 >        if (isexpos(s))
192 >                exposure *= exposval(s);
193 >        else if (isformat(s)) {
194 >                formatval(fmt, s);
195 >                wrongformat = strcmp(fmt, COLRFMT);
196 >        } else
197                  for (an = altname; *an != NULL; an++)
198                          if (!strncmp(*an, s, strlen(*an))) {
199 <                                if (sscanview(&ourview, s+strlen(*an)) == 0)
199 >                                if (sscanview(&ourview, s+strlen(*an)) > 0)
200                                          gotview++;
201                                  return;
202                          }
# Line 189 | Line 205 | char  *s;
205  
206   init()                  /* get data and open window */
207   {
208 +        XWMHints        ourxwmhints;
209 +        XSetWindowAttributes    ourwinattr;
210          XSizeHints  oursizhints;
211          register int  i;
212          
# Line 201 | Line 219 | init()                 /* get data and open window */
219          }
220          if ((thedisplay = XOpenDisplay(NULL)) == NULL)
221                  quiterr("can't open display; DISPLAY variable set?");
204        wind = XCreateSimpleWindow(thedisplay,
205                ourroot, 0, 0, xmax, ymax, BORWIDTH, ourblack, ourwhite);
206        if (wind == 0)
207                quiterr("can't create window");
222          if (maxcolors == 0) {           /* get number of available colors */
223 <                maxcolors = 1<<DisplayPlanes(thedisplay,ourscreen);
223 >                i = DisplayPlanes(thedisplay,ourscreen);
224 >                maxcolors = i > 8 ? 256 : 1<<i;
225                  if (maxcolors > 4) maxcolors -= 2;
226          }
227 +                                /* store image */
228 +        getras();
229 +                                /* open window */
230 +        ourwinattr.border_pixel = ourblack;
231 +        ourwinattr.background_pixel = ourwhite;
232 +        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
233 +                        0, InputOutput, ourras->visual,
234 +                        CWBackPixel|CWBorderPixel, &ourwinattr);
235 +        if (wind == 0)
236 +                quiterr("can't create window");
237 +        width = xmax;
238 +        height = ymax;
239          fontid = XLoadFont(thedisplay, FONTNAME);
240          if (fontid == 0)
241                  quiterr("can't get font");
242          XSetFont(thedisplay, ourgc, fontid);
243 +        revgc = XCreateGC(thedisplay, wind, 0, 0);
244 +        XSetFunction(thedisplay, revgc, GXinvert);
245          XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
246          XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
247                          XC_diamond_cross));
248          if (geometry != NULL) {
249                  bzero((char *)&oursizhints, sizeof(oursizhints));
250                  i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
251 <                                &oursizhints.width, &oursizhints.height);
252 <                if (i & (XValue|YValue) == (XValue|YValue)) {
251 >                                (unsigned *)&oursizhints.width,
252 >                                (unsigned *)&oursizhints.height);
253 >                if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
254 >                        oursizhints.flags |= USSize;
255 >                else {
256 >                        oursizhints.width = xmax;
257 >                        oursizhints.height = ymax;
258 >                        oursizhints.flags |= PSize;
259 >                }
260 >                if ((i&(XValue|YValue)) == (XValue|YValue)) {
261                          oursizhints.flags |= USPosition;
262                          if (i & XNegative)
263 <                                oursizhints.x += DisplayWidth(thedisplay,ourscreen)-1;
263 >                                oursizhints.x += DisplayWidth(thedisplay,
264 >                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
265                          if (i & YNegative)
266 <                                oursizhints.y += DisplayHeight(thedisplay,ourscreen)-1;
266 >                                oursizhints.y += DisplayHeight(thedisplay,
267 >                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
268                  }
230                if (i & (WidthValue|HeightValue) == (WidthValue|HeightValue))
231                        oursizhints.flags |= USSize;
269                  XSetNormalHints(thedisplay, wind, &oursizhints);
270          }
271 <                                /* store image */
272 <        getras();
273 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask|
274 <                        StructureNotifyMask|ButtonMotionMask|
275 <                        KeyPressMask|ExposureMask);
271 >        ourxwmhints.flags = InputHint|IconPixmapHint;
272 >        ourxwmhints.input = True;
273 >        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
274 >                        wind, x11icon_bits, x11icon_width, x11icon_height);
275 >        XSetWMHints(thedisplay, wind, &ourxwmhints);
276 >        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
277 >                        |ButtonMotionMask|StructureNotifyMask
278 >                        |KeyPressMask|ExposureMask);
279          XMapWindow(thedisplay, wind);
280          return;
281   memerr:
# Line 271 | Line 311 | int  code;
311   getras()                                /* get raster file */
312   {
313          colormap        ourmap;
274        unsigned char   rmap[256], gmap[256], bmap[256];
314          XVisualInfo     vinfo;
276        register int  i;
315  
316          if (maxcolors <= 2) {           /* monochrome */
317                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
# Line 284 | Line 322 | getras()                               /* get raster file */
322                  if (ourras == NULL)
323                          goto fail;
324                  getmono();
325 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) {
325 >        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)
326 >                                                /* kludge for DirectColor */
327 >        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) {
328                  ourdata = (unsigned char *)malloc(xmax*ymax*3);
329                  if (ourdata == NULL)
330                          goto fail;
# Line 305 | Line 345 | getras()                               /* get raster file */
345                          biq(dither,maxcolors,1,ourmap);
346                  else
347                          ciq(dither,maxcolors,1,ourmap);
348 <                for (i = 0; i < 256; i++) {
309 <                        rmap[i] = ourmap[0][i];
310 <                        gmap[i] = ourmap[1][i];
311 <                        bmap[i] = ourmap[2][i];
312 <                }
313 <                if (init_rcolors(ourras, rmap, gmap, bmap) == 0)
348 >                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
349                          goto fail;
350          }
351          return;
352   fail:
353 <        quit("could not create raster image");
353 >        quiterr("could not create raster image");
354   }
355  
356  
# Line 359 | Line 394 | getevent()                             /* process the next event */
394   }
395  
396  
362 redraw(x, y, w, h)                      /* redraw section of window */
363 int  x, y;
364 int  w, h;
365 {
366        patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras);
367 }
368
369
397   docom(ekey)                                     /* execute command */
398   XKeyPressedEvent  *ekey;
399   {
# Line 397 | Line 424 | XKeyPressedEvent  *ekey;
424                          sprintf(buf, "%.3f", intens(cval)/exposure);
425                          break;
426                  case 'l':                               /* luminance */
427 <                        sprintf(buf, "%.0fn", bright(cval)*683.0/exposure);
427 >                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
428                          break;
429                  case 'c':                               /* color */
430                          comp = pow(2.0, (double)scale);
# Line 407 | Line 434 | XKeyPressedEvent  *ekey;
434                                          colval(cval,BLU)*comp);
435                          break;
436                  }
437 <                XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin,
438 <                                buf, strlen(buf));
437 >                XDrawImageString(thedisplay, wind, ourgc,
438 >                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
439                  return(0);
440          case 'i':                               /* identify (contour) */
441                  if (ourras->pixels == NULL)
# Line 419 | Line 446 | XKeyPressedEvent  *ekey;
446                  cvx.red = random() & 65535;
447                  cvx.green = random() & 65535;
448                  cvx.blue = random() & 65535;
449 <                XStoreColors(thedisplay, ourras->cmap, &cvx, 1);
449 >                cvx.flags = DoRed|DoGreen|DoBlue;
450 >                XStoreColor(thedisplay, ourras->cmap, &cvx);
451                  return(0);
452          case 'p':                               /* position */
453                  sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
# Line 431 | Line 459 | XKeyPressedEvent  *ekey;
459                          XBell(thedisplay, 0);
460                          return(-1);
461                  }
462 <                viewray(rorg, rdir, &ourview,
462 >                if (viewray(rorg, rdir, &ourview,
463                                  (ekey->x-xoff+.5)/xmax,
464 <                                (ymax-1-ekey->y+yoff+.5)/ymax);
464 >                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
465 >                        return(-1);
466                  printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
467                  printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
468                  fflush(stdout);
# Line 447 | Line 476 | XKeyPressedEvent  *ekey;
476                  scale_rcolors(ourras, pow(2.0, (double)n));
477                  scale += n;
478                  sprintf(buf, "%+d", scale);
479 <                XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin,
480 <                                buf, strlen(buf));
479 >                XDrawImageString(thedisplay, wind, ourgc,
480 >                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
481                  XFlush(thedisplay);
482                  free(ourdata);
483                  free_raster(ourras);
# Line 467 | Line 496 | XKeyPressedEvent  *ekey;
496                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
497                  return(0);
498          default:
499 <                XBell(0);
499 >                XBell(thedisplay, 0);
500                  return(-1);
501          }
502   }
503  
504  
505 < moveimage(ep)                           /* shift the image */
506 < XButtonPressedEvent  *ep;
505 > moveimage(ebut)                         /* shift the image */
506 > XButtonPressedEvent  *ebut;
507   {
508 <        XButtonPressedEvent  eb;
508 >        union {
509 >                XEvent  u;
510 >                XButtonReleasedEvent  b;
511 >                XPointerMovedEvent  m;
512 >        }  e;
513 >        int     mxo, myo;
514  
515 <        XMaskEvent(thedisplay, ButtonReleaseMask, &eb);
516 <        xoff += eb.x - ep->x;
517 <        yoff += eb.y - ep->y;
515 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
516 >        while (e.u.type == MotionNotify) {
517 >                mxo = e.m.x;
518 >                myo = e.m.y;
519 >                revline(ebut->x, ebut->y, mxo, myo);
520 >                revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
521 >                                xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
522 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
523 >                revline(ebut->x, ebut->y, mxo, myo);
524 >                revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
525 >                                xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
526 >        }
527 >        xoff += e.b.x - ebut->x;
528 >        yoff += e.b.y - ebut->y;
529          XClearWindow(thedisplay, wind);
530          redraw(0, 0, width, height);
486        return(0);
531   }
532  
533  
# Line 491 | Line 535 | getbox(ebut)                           /* get new box */
535   XButtonPressedEvent  *ebut;
536   {
537          union {
538 <                XEvent  e;
538 >                XEvent  u;
539                  XButtonReleasedEvent  b;
540                  XPointerMovedEvent  m;
541          }  e;
542  
543 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.e);
544 <        while (e.e.type == ButtonMotionMask) {
543 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
544 >        while (e.u.type == MotionNotify) {
545                  revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
546 <                XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask,
503 <                                &e.e);
546 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
547                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
548          }
549          box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
# Line 523 | Line 566 | XButtonPressedEvent  *ebut;
566   revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
567   int  x0, y0, x1, y1;
568   {
569 <        static GC       mygc = 0;
569 >        revline(x0, y0, x1, y0);
570 >        revline(x0, y1, x1, y1);
571 >        revline(x0, y0, x0, y1);
572 >        revline(x1, y0, x1, y1);
573 > }
574  
528        if (mygc == 0) {
529                mygc = XCreateGC(thedisplay, wind, 0, 0);
530                XSetPlaneMask(thedisplay, mygc, ~0L);
531                XSetFunction(thedisplay, mygc, GXinvert);
532        }
533        XDrawLine(thedisplay, wind, mygc, x0, y0, x1, y0);
534        XDrawLine(thedisplay, wind, mygc, x0, y1, x1, y1);
535        XDrawLine(thedisplay, wind, mygc, x0, y0, x0, y1);
536        XDrawLine(thedisplay, wind, mygc, x1, y0, x1, y1);
537 } /* end of revbox */
575  
539
576   avgbox(clr)                             /* average color over current box */
577   COLOR  clr;
578   {
# Line 582 | Line 618 | getmono()                      /* get monochrome data */
618          register unsigned char  *dp;
619          register int    x, err;
620          int     y;
585        rgbpixel        *inline;
621          short   *cerr;
622  
623 <        if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
624 <                        || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
590 <                quit("out of memory in getmono");
623 >        if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
624 >                quiterr("out of memory in getmono");
625          dp = ourdata - 1;
626          for (y = 0; y < ymax; y++) {
627 <                picreadline3(y, inline);
627 >                if (getscan(y) < 0)
628 >                        quiterr("seek error in getmono");
629 >                normcolrs(scanline, xmax, scale);
630                  err = 0;
631                  for (x = 0; x < xmax; x++) {
632                          if (!(x&7))
633                                  *++dp = 0;
634 <                        err += rgb_bright(&inline[x]) + cerr[x];
634 >                        err += normbright(scanline[x]) + cerr[x];
635                          if (err > 127)
636                                  err -= 255;
637                          else
638 <                                *dp |= 1<<(x&07);
638 >                                *dp |= 1<<(7-(x&07));
639                          cerr[x] = err >>= 1;
640                  }
641          }
606        free((char *)inline);
642          free((char *)cerr);
643   }
644  
# Line 611 | Line 646 | getmono()                      /* get monochrome data */
646   getfull()                       /* get full (24-bit) data */
647   {
648          int     y;
649 <
650 <        for (y = 0; y < ymax; y++)
651 <                picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3));
649 >        register unsigned char  *dp;
650 >        register int    x;
651 >                                        /* set gamma correction */
652 >        setcolrgam(gamcor);
653 >                                        /* read and convert file */
654 >        dp = ourdata;
655 >        for (y = 0; y < ymax; y++) {
656 >                if (getscan(y) < 0)
657 >                        quiterr("seek error in getfull");
658 >                if (scale)
659 >                        shiftcolrs(scanline, xmax, scale);
660 >                colrs_gambs(scanline, xmax);
661 >                for (x = 0; x < xmax; x++) {
662 >                        *dp++ = scanline[x][RED];
663 >                        *dp++ = scanline[x][GRN];
664 >                        *dp++ = scanline[x][BLU];
665 >                }
666 >        }
667   }
668  
669  
# Line 652 | Line 702 | int  y;
702                  if (scanpos == NULL || scanpos[y] == -1)
703                          return(-1);
704                  if (fseek(fin, scanpos[y], 0) == -1)
705 <                        quit("fseek error");
705 >                        quiterr("fseek error");
706                  cury = y;
707          } else if (scanpos != NULL)
708                  scanpos[y] = ftell(fin);
# Line 698 | Line 748 | colormap  map;
748          register int  i, val;
749  
750          for (i = 0; i < 256; i++) {
751 <                val = pow(i/256.0, 1.0/gamcor) * 256.0;
751 >                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
752                  map[0][i] = map[1][i] = map[2][i] = val;
753          }
754   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines