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.8 by greg, Fri May 29 14:21:12 1992 UTC vs.
Revision 2.40 by greg, Wed Nov 23 14:24:34 1994 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21  
22   #include  "standard.h"
23  
24 + #include  <signal.h>
25   #include  <X11/Xlib.h>
26   #include  <X11/cursorfont.h>
27   #include  <X11/Xutil.h>
28 + #include  <X11/Xatom.h>
29  
30   #include  "color.h"
31   #include  "view.h"
30 #include  "pic.h"
32   #include  "x11raster.h"
33   #include  "random.h"
34   #include  "resolu.h"
35  
36 + #ifdef  __alpha
37 + #define  int4           int
38 + #endif
39 + #ifndef  int4
40 + #define  int4           long
41 + #endif
42 +
43   #define  FONTNAME       "8x13"          /* text font we'll use */
44  
45   #define  CTRL(c)        ((c)-'@')
# Line 48 | Line 56 | static char SCCSid[] = "$SunId$ LBL";
56   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
57  
58   double  gamcor = 2.2;                   /* gamma correction */
59 + char  *gamstr = NULL;                   /* gamma value override */
60  
61   int  dither = 1;                        /* dither colors? */
62   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 55 | Line 64 | int  fast = 0;                         /* keep picture in Pixmap? */
64   char    *dispname = NULL;               /* our display name */
65  
66   Window  wind = 0;                       /* our output window */
67 < unsigned long  ourblack=1, ourwhite=0;  /* black and white for this visual */
59 < Font  fontid;                           /* our font */
60 <
67 > unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
68   int  maxcolors = 0;                     /* maximum colors */
69   int  greyscale = 0;                     /* in grey */
70  
# Line 66 | Line 73 | int  scale = 0;                                /* scalefactor; power of two */
73   int  xoff = 0;                          /* x image offset */
74   int  yoff = 0;                          /* y image offset */
75  
76 + int  parent = 0;                        /* number of children, -1 if child */
77 +
78   VIEW  ourview = STDVIEW;                /* image view parameters */
79   int  gotview = 0;                       /* got parameters from file */
80  
# Line 104 | Line 113 | char  *progname;
113  
114   char  errmsg[128];
115  
116 + BYTE  clrtab[256][3];                   /* global color map */
117 +
118   extern long  ftell();
119  
120 < extern char  *malloc(), *calloc();
120 > extern char  *getenv();
121  
111 extern double  pow(), log();
112
122   Display  *thedisplay;
123 + Atom  closedownAtom, wmProtocolsAtom;
124  
125 + int  sigrecv;
126 +
127 + int  onsig() { sigrecv++; }
128 +
129 +
130   main(argc, argv)
131   int  argc;
132   char  *argv[];
133   {
134          int  headline();
135          int  i;
136 +        int  pid;
137          
138          progname = argv[0];
139  
# Line 131 | Line 147 | char  *argv[];
147                                  greyscale = !greyscale;
148                                  break;
149                          case 'm':
150 +                                greyscale = 1;
151                                  maxcolors = 2;
152                                  break;
153                          case 'd':
# Line 151 | Line 168 | char  *argv[];
168                                  if (argv[i][2] == 'e')
169                                          geometry = argv[++i];
170                                  else
171 <                                        gamcor = atof(argv[++i]);
171 >                                        gamstr = argv[++i];
172                                  break;
173                          default:
174                                  goto userr;
# Line 161 | Line 178 | char  *argv[];
178                  else
179                          break;
180  
181 <        if (i == argc-1) {
181 >        if (i > argc)
182 >                goto userr;
183 >        while (i < argc-1) {
184 >                sigrecv = 0;
185 >                signal(SIGCONT, onsig);
186 >                if ((pid=fork()) == 0) {        /* a child for each picture */
187 >                        parent = -1;
188 >                        break;
189 >                }
190 >                if (pid < 0)
191 >                        quiterr("fork failed");
192 >                parent++;
193 >                while (!sigrecv)
194 >                        pause();        /* wait for wake-up call */
195 >                i++;
196 >        }
197 >        if (i < argc) {                 /* open picture file */
198                  fname = argv[i];
199                  fin = fopen(fname, "r");
200 <                if (fin == NULL) {
201 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
202 <                        quiterr(errmsg);
170 <                }
171 <        } else if (i != argc)
172 <                goto userr;
200 >                if (fin == NULL)
201 >                        quiterr("cannot open picture file");
202 >        }
203                                  /* get header */
204          getheader(fin, headline, NULL);
205                                  /* get picture dimensions */
# Line 183 | Line 213 | char  *argv[];
213          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
214                  quiterr("out of memory");
215  
216 <        init();                 /* get file and open window */
216 >        init(argc, argv);                       /* get file and open window */
217  
218          for ( ; ; )
219                  getevent();             /* main loop */
220   userr:
221          fprintf(stderr,
222 <        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
222 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor] pic ..\n",
223                          progname);
224 <        quit(1);
224 >        exit(1);
225   }
226  
227  
# Line 210 | Line 240 | char  *s;
240   }
241  
242  
243 < init()                  /* get data and open window */
243 > init(argc, argv)                        /* get data and open window */
244 > int argc;
245 > char **argv;
246   {
215        XWMHints        ourxwmhints;
247          XSetWindowAttributes    ourwinattr;
248 <        XSizeHints      oursizhints;
248 >        XClassHint      xclshints;
249 >        XWMHints        xwmhints;
250 >        XSizeHints      xszhints;
251 >        XTextProperty   windowName, iconName;
252 >        XGCValues       xgcv;
253 >        char    *name;
254          register int    i;
255          
256          if (fname != NULL) {
257                  scanpos = (long *)malloc(ymax*sizeof(long));
258                  if (scanpos == NULL)
259 <                        goto memerr;
259 >                        quiterr("out of memory");
260                  for (i = 0; i < ymax; i++)
261                          scanpos[i] = -1;
262 <        }
262 >                name = fname;
263 >        } else
264 >                name = progname;
265 >                                /* remove directory prefix from name */
266 >        for (i = strlen(name); i-- > 0; )
267 >                if (name[i] == '/')
268 >                        break;
269 >        name += i+1;
270          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
271                  quiterr("cannot open display");
272 +                                /* set gamma value */
273 +        if (gamstr == NULL)             /* get it from the X server */
274 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
275 +        if (gamstr == NULL)             /* get it from the environment */
276 +                gamstr = getenv("DISPLAY_GAMMA");
277 +        if (gamstr != NULL)
278 +                gamcor = atof(gamstr);
279                                  /* get best visual for default screen */
280          getbestvis();
281                                  /* store image */
282          getras();
283 <                                /* open window */
284 <        ourwinattr.border_pixel = ourblack;
285 <        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 <                        ourvis.depth, InputOutput, ourvis.visual,
240 <                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
241 <        if (wind == 0)
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("cannot get font");
252 <        XSetFont(thedisplay, ourgc, fontid);
253 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
254 <                        XC_diamond_cross));
255 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
283 >                                /* get size and position */
284 >        xszhints.flags = 0;
285 >        xszhints.width = xmax; xszhints.height = ymax;
286          if (geometry != NULL) {
287 <                bzero((char *)&oursizhints, sizeof(oursizhints));
288 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
289 <                                (unsigned *)&oursizhints.width,
260 <                                (unsigned *)&oursizhints.height);
287 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
288 >                                (unsigned *)&xszhints.width,
289 >                                (unsigned *)&xszhints.height);
290                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
291 <                        oursizhints.flags |= USSize;
292 <                else {
293 <                        oursizhints.width = xmax;
265 <                        oursizhints.height = ymax;
266 <                        oursizhints.flags |= PSize;
267 <                }
291 >                        xszhints.flags |= USSize;
292 >                else
293 >                        xszhints.flags |= PSize;
294                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
295 <                        oursizhints.flags |= USPosition;
295 >                        xszhints.flags |= USPosition;
296                          if (i & XNegative)
297 <                                oursizhints.x += DisplayWidth(thedisplay,
298 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
297 >                                xszhints.x += DisplayWidth(thedisplay,
298 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
299                          if (i & YNegative)
300 <                                oursizhints.y += DisplayHeight(thedisplay,
301 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
300 >                                xszhints.y += DisplayHeight(thedisplay,
301 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
302                  }
277                XSetNormalHints(thedisplay, wind, &oursizhints);
303          }
304 <        ourxwmhints.flags = InputHint|IconPixmapHint;
305 <        ourxwmhints.input = True;
306 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
304 >        /* open window */
305 >        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
306 >        ourwinattr.border_pixel = ourwhite;
307 >        ourwinattr.background_pixel = ourblack;
308 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
309 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
310 >                i |= CWColormap;
311 >        }
312 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
313 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
314 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
315 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
316 >                        xszhints.width, xszhints.height, BORWIDTH,
317 >                        ourvis.depth, InputOutput, ourvis.visual,
318 >                        i, &ourwinattr);
319 >        if (wind == 0)
320 >                quiterr("cannot create window");
321 >        width = xmax;
322 >        height = ymax;
323 >        /* prepare graphics drawing context */
324 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
325 >                quiterr("cannot get font");
326 >        xgcv.foreground = ourblack;
327 >        xgcv.background = ourwhite;
328 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
329 >                        GCFont, &xgcv);
330 >        xgcv.function = GXinvert;
331 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
332 >                        GCFunction, &xgcv);
333 >
334 >        /* set up the window manager */
335 >        xwmhints.flags = InputHint|IconPixmapHint;
336 >        xwmhints.input = True;
337 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
338                          wind, icondata, iconwidth, iconheight);
339 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
340 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
341 <                        |ButtonMotionMask|StructureNotifyMask
342 <                        |KeyPressMask|ExposureMask);
339 >
340 >        windowName.encoding = iconName.encoding = XA_STRING;
341 >        windowName.format = iconName.format = 8;
342 >        windowName.value = (u_char *)name;
343 >        windowName.nitems = strlen(windowName.value);
344 >        iconName.value = (u_char *)name;
345 >        iconName.nitems = strlen(windowName.value);
346 >
347 >        xclshints.res_name = NULL;
348 >        xclshints.res_class = "Ximage";
349 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
350 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
351 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
352 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
353 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
354 >
355          XMapWindow(thedisplay, wind);
288        return;
289 memerr:
290        quiterr("out of memory");
356   } /* end of init */
357  
358  
359   quiterr(err)            /* print message and exit */
360   char  *err;
361   {
362 <        if (err != NULL) {
363 <                fprintf(stderr, "%s: %s\n", progname, err);
364 <                exit(1);
362 >        register int  es;
363 >        int  cs;
364 >
365 >        if (es = err != NULL)
366 >                fprintf(stderr, "%s: %s: %s\n", progname,
367 >                                fname==NULL?"<stdin>":fname, err);
368 >        if (thedisplay != NULL)
369 >                XCloseDisplay(thedisplay);
370 >        if (parent < 0 & sigrecv == 0)
371 >                kill(getppid(), SIGCONT);
372 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
373 >                if (es == 0)
374 >                        es = cs>>8 & 0xff;
375 >                parent--;
376          }
377 <        exit(0);
377 >        exit(es);
378   }
379  
380  
305 eputs(s)
306 char    *s;
307 {
308        fputs(s, stderr);
309 }
310
311
312 quit(code)
313 int  code;
314 {
315        exit(code);
316 }
317
318
381   static int
382   viscmp(v1,v2)           /* compare visual to see which is better, descending */
383   register XVisualInfo    *v1, *v2;
# Line 419 | Line 481 | static char  vistype[][12] = {
481                  greyscale = 1;
482          if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
483                  maxcolors = ourvis.colormap_size;
422        if (maxcolors > 4)
423                maxcolors -= 2;
484          if (ourvis.class == StaticGray) {
485                  ourblack = 0;
486                  ourwhite = 255;
487          } else if (ourvis.class == PseudoColor) {
488                  ourblack = BlackPixel(thedisplay,ourscreen);
489                  ourwhite = WhitePixel(thedisplay,ourscreen);
490 +                if ((ourblack|ourwhite) & ~255L) {
491 +                        ourblack = 0;
492 +                        ourwhite = 1;
493 +                }
494 +                if (maxcolors > 4)
495 +                        maxcolors -= 2;
496          } else {
497                  ourblack = 0;
498 <                ourwhite = ~0;
498 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
499          }
500          XFree((char *)xvi);
501   }
# Line 437 | Line 503 | static char  vistype[][12] = {
503  
504   getras()                                /* get raster file */
505   {
440        colormap        ourmap;
506          XVisualInfo     vinfo;
507  
508          if (maxcolors <= 2) {           /* monochrome */
# Line 449 | Line 514 | getras()                               /* get raster file */
514                  if (ourras == NULL)
515                          goto fail;
516                  getmono();
517 <        } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
518 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
517 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
518 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
519                  if (ourdata == NULL)
520                          goto fail;
521 <                ourras = make_raster(thedisplay, &ourvis, 32,
521 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
522                                  ourdata, xmax, ymax, 32);
523                  if (ourras == NULL)
524                          goto fail;
# Line 466 | Line 531 | getras()                               /* get raster file */
531                                  xmax, ymax, 8);
532                  if (ourras == NULL)
533                          goto fail;
534 <                if (ourvis.class == StaticGray)
534 >                if (greyscale | ourvis.class == StaticGray)
535                          getgrey();
536 <                else {
537 <                        if (greyscale)
538 <                                biq(dither,maxcolors,1,ourmap);
539 <                        else
475 <                                ciq(dither,maxcolors,1,ourmap);
476 <                        if (init_rcolors(ourras, ourmap[0],
477 <                                        ourmap[1], ourmap[2]) == 0)
478 <                                goto fail;
479 <                }
536 >                else
537 >                        getmapped();
538 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
539 >                        goto fail;
540          }
541 <                return;
541 >        return;
542   fail:
543          quiterr("could not create raster image");
544   }
# Line 486 | Line 546 | fail:
546  
547   getevent()                              /* process the next event */
548   {
549 <        union {
490 <                XEvent  u;
491 <                XConfigureEvent  c;
492 <                XExposeEvent  e;
493 <                XButtonPressedEvent  b;
494 <                XKeyPressedEvent  k;
495 <        } e;
549 >        XEvent xev;
550  
551 <        XNextEvent(thedisplay, &e.u);
552 <        switch (e.u.type) {
551 >        XNextEvent(thedisplay, &xev);
552 >        switch ((int)xev.type) {
553          case KeyPress:
554 <                docom(&e.k);
554 >                docom(&xev.xkey);
555                  break;
556          case ConfigureNotify:
557 <                width = e.c.width;
558 <                height = e.c.height;
557 >                width = xev.xconfigure.width;
558 >                height = xev.xconfigure.height;
559                  break;
560          case MapNotify:
561                  map_rcolors(ourras, wind);
562                  if (fast)
563                          make_rpixmap(ourras, wind);
564 +                if (parent < 0 & sigrecv == 0) {        /* notify parent */
565 +                        kill(getppid(), SIGCONT);
566 +                        sigrecv--;
567 +                }
568                  break;
569          case UnmapNotify:
570                  if (!fast)
571                          unmap_rcolors(ourras);
572                  break;
573          case Expose:
574 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
574 >                redraw(xev.xexpose.x, xev.xexpose.y,
575 >                                xev.xexpose.width, xev.xexpose.height);
576                  break;
577          case ButtonPress:
578 <                if (e.b.state & (ShiftMask|ControlMask))
579 <                        moveimage(&e.b);
578 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
579 >                        moveimage(&xev.xbutton);
580 >                else if (xev.xbutton.button == Button2)
581 >                        traceray(xev.xbutton.x, xev.xbutton.y);
582                  else
583 <                        getbox(&e.b);
583 >                        getbox(&xev.xbutton);
584                  break;
585 +        case ClientMessage:
586 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
587 +                                (xev.xclient.data.l[0] == closedownAtom))
588 +                        quiterr(NULL);
589 +                break;
590          }
591   }
592  
593  
594 < docom(ekey)                                     /* execute command */
594 > traceray(xpos, ypos)                    /* print ray corresponding to pixel */
595 > int  xpos, ypos;
596 > {
597 >        FLOAT  hv[2];
598 >        FVECT  rorg, rdir;
599 >
600 >        if (!gotview) {         /* no view, no can do */
601 >                XBell(thedisplay, 0);
602 >                return(-1);
603 >        }
604 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
605 >        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
606 >                return(-1);
607 >        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
608 >        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
609 >        fflush(stdout);
610 >        return(0);
611 > }
612 >
613 >
614 > docom(ekey)                             /* execute command */
615   XKeyPressedEvent  *ekey;
616   {
617          char  buf[80];
# Line 534 | Line 620 | XKeyPressedEvent  *ekey;
620          int  com, n;
621          double  comp;
622          FLOAT  hv[2];
537        FVECT  rorg, rdir;
623  
624          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
625          if (n == 0)
# Line 542 | Line 627 | XKeyPressedEvent  *ekey;
627          com = buf[0];
628          switch (com) {                  /* interpret command */
629          case 'q':
630 +        case 'Q':
631          case CTRL('D'):                         /* quit */
632 <                quit(0);
632 >                quiterr(NULL);
633          case '\n':
634          case '\r':
635          case 'l':
# Line 589 | Line 675 | XKeyPressedEvent  *ekey;
675                                          buf, strlen(buf));
676                  return(0);
677          case 't':                               /* trace */
678 <                if (!gotview) {
593 <                        XBell(thedisplay, 0);
594 <                        return(-1);
595 <                }
596 <                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
597 <                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
598 <                        return(-1);
599 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
600 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
601 <                fflush(stdout);
602 <                return(0);
678 >                return(traceray(ekey->x, ekey->y));
679          case '=':                               /* adjust exposure */
680 +        case '@':                               /* adaptation level */
681                  if (avgbox(cval) == -1)
682                          return(-1);
683 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
683 >                comp = bright(cval);
684 >                if (comp < 1e-20) {
685 >                        XBell(thedisplay, 0);
686 >                        return(-1);
687 >                }
688 >                if (com == '@')
689 >                        comp = 106./exposure/
690 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
691 >                else
692 >                        comp = .5/comp;
693 >                comp = log(comp)/.69315 - scale;
694 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
695                  if (n == 0)
696                          return(0);
697                  scale_rcolors(ourras, pow(2.0, (double)n));
# Line 622 | Line 710 | XKeyPressedEvent  *ekey;
710                  XClearWindow(thedisplay, wind);
711                  map_rcolors(ourras, wind);
712                  if (fast)
713 <                        make_rpixmap(ourras);
713 >                        make_rpixmap(ourras, wind);
714                  redraw(0, 0, width, height);
715                  return(0);
716 +        case 'f':                               /* turn on fast redraw */
717 +                fast = 1;
718 +                make_rpixmap(ourras, wind);
719 +                return(0);
720 +        case 'F':                               /* turn off fast redraw */
721 +                fast = 0;
722 +                free_rpixmap(ourras);
723 +                return(0);
724 +        case '0':                               /* recenter origin */
725 +                if (xoff == 0 & yoff == 0)
726 +                        return(0);
727 +                xoff = yoff = 0;
728 +                XClearWindow(thedisplay, wind);
729 +                redraw(0, 0, width, height);
730 +                return(0);
731          case ' ':                               /* clear */
732                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
733                  return(0);
# Line 638 | Line 741 | XKeyPressedEvent  *ekey;
741   moveimage(ebut)                         /* shift the image */
742   XButtonPressedEvent  *ebut;
743   {
744 <        union {
642 <                XEvent  u;
643 <                XButtonReleasedEvent  b;
644 <                XPointerMovedEvent  m;
645 <        }  e;
744 >        XEvent  e;
745          int     mxo, myo;
746  
747 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
748 <        while (e.u.type == MotionNotify) {
749 <                mxo = e.m.x;
750 <                myo = e.m.y;
747 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
748 >        while (e.type == MotionNotify) {
749 >                mxo = e.xmotion.x;
750 >                myo = e.xmotion.y;
751                  revline(ebut->x, ebut->y, mxo, myo);
752                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
753                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
754 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
754 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
755                  revline(ebut->x, ebut->y, mxo, myo);
756                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
757                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
758          }
759 <        xoff += e.b.x - ebut->x;
760 <        yoff += e.b.y - ebut->y;
759 >        xoff += e.xbutton.x - ebut->x;
760 >        yoff += e.xbutton.y - ebut->y;
761          XClearWindow(thedisplay, wind);
762          redraw(0, 0, width, height);
763   }
# Line 667 | Line 766 | XButtonPressedEvent  *ebut;
766   getbox(ebut)                            /* get new box */
767   XButtonPressedEvent  *ebut;
768   {
769 <        union {
671 <                XEvent  u;
672 <                XButtonReleasedEvent  b;
673 <                XPointerMovedEvent  m;
674 <        }  e;
769 >        XEvent  e;
770  
771 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
772 <        while (e.u.type == MotionNotify) {
773 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
774 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
771 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
772 >        while (e.type == MotionNotify) {
773 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
774 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
775                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
776          }
777 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
778 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
777 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
778 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
779          if (box.xmin > ebut->x) {
780                  box.xsiz = box.xmin - ebut->x + 1;
781                  box.xmin = ebut->x;
# Line 736 | Line 831 | COLOR  clr;
831                  return(-1);
832          if (left == ll && right == lr && top == lt && bottom == lb) {
833                  copycolor(clr, lc);
834 <                return;
834 >                return(0);
835          }
836          for (y = top; y < bottom; y++) {
837                  if (getscan(y) == -1)
# Line 765 | Line 860 | getmono()                      /* get monochrome data */
860                  quiterr("out of memory in getmono");
861          dp = ourdata - 1;
862          for (y = 0; y < ymax; y++) {
863 <                if (getscan(y) < 0)
769 <                        quiterr("seek error in getmono");
770 <                normcolrs(scanline, xmax, scale);
863 >                getscan(y);
864                  add2icon(y, scanline);
865 +                normcolrs(scanline, xmax, scale);
866                  err = 0;
867                  for (x = 0; x < xmax; x++) {
868                          if (!(x&7))
# Line 794 | Line 888 | COLR  *scan;
888          static short  cerr[ICONSIZ];
889          static int  ynext;
890          static char  *dp;
891 +        COLR  clr;
892          register int  err;
893          register int    x, ti;
894          int  errp;
# Line 824 | Line 919 | COLR  *scan;
919                          *++dp = 0;
920                  errp = err;
921                  ti = x*xmax/iconwidth;
922 <                err += normbright(scan[ti]) + cerr[x];
922 >                copycolr(clr, scan[ti]);
923 >                normcolrs(clr, 1, scale);
924 >                err += normbright(clr) + cerr[x];
925                  if (err > 127)
926                          err -= 255;
927                  else
# Line 839 | Line 936 | COLR  *scan;
936   getfull()                       /* get full (24-bit) data */
937   {
938          int     y;
939 <        register unsigned long  *dp;
939 >        register unsigned int4  *dp;
940          register int    x;
941                                          /* set gamma correction */
942          setcolrgam(gamcor);
943                                          /* read and convert file */
944 <        dp = (unsigned long *)ourdata;
944 >        dp = (unsigned int4 *)ourdata;
945          for (y = 0; y < ymax; y++) {
946 <                if (getscan(y) < 0)
947 <                        quiterr("seek error in getfull");
946 >                getscan(y);
947 >                add2icon(y, scanline);
948                  if (scale)
949                          shiftcolrs(scanline, xmax, scale);
950                  colrs_gambs(scanline, xmax);
854                add2icon(y, scanline);
951                  if (ourras->image->blue_mask & 1)
952                          for (x = 0; x < xmax; x++)
953 <                                *dp++ = scanline[x][RED] << 16 |
954 <                                        scanline[x][GRN] << 8 |
955 <                                        scanline[x][BLU] ;
953 >                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
954 >                                        (unsigned int4)scanline[x][GRN] << 8 |
955 >                                        (unsigned int4)scanline[x][BLU] ;
956                  else
957                          for (x = 0; x < xmax; x++)
958 <                                *dp++ = scanline[x][RED] |
959 <                                        scanline[x][GRN] << 8 |
960 <                                        scanline[x][BLU] << 16 ;
958 >                                *dp++ = (unsigned int4)scanline[x][RED] |
959 >                                        (unsigned int4)scanline[x][GRN] << 8 |
960 >                                        (unsigned int4)scanline[x][BLU] << 16 ;
961          }
962   }
963  
# Line 876 | Line 972 | getgrey()                      /* get greyscale data */
972                                          /* read and convert file */
973          dp = ourdata;
974          for (y = 0; y < ymax; y++) {
975 <                if (getscan(y) < 0)
976 <                        quiterr("seek error in getfull");
975 >                getscan(y);
976 >                add2icon(y, scanline);
977                  if (scale)
978                          shiftcolrs(scanline, xmax, scale);
979 +                for (x = 0; x < xmax; x++)
980 +                        scanline[x][GRN] = normbright(scanline[x]);
981                  colrs_gambs(scanline, xmax);
982 <                add2icon(y, scanline);
885 <                if (ourvis.colormap_size < 256)
982 >                if (maxcolors < 256)
983                          for (x = 0; x < xmax; x++)
984 <                                *dp++ = ((long)normbright(scanline[x]) *
985 <                                        ourvis.colormap_size + 128) >> 8;
984 >                                *dp++ = ((long)scanline[x][GRN] *
985 >                                        maxcolors + maxcolors/2) >> 8;
986                  else
987                          for (x = 0; x < xmax; x++)
988 <                                *dp++ = normbright(scanline[x]);
988 >                                *dp++ = scanline[x][GRN];
989          }
990 +        for (x = 0; x < maxcolors; x++)
991 +                clrtab[x][RED] = clrtab[x][GRN] =
992 +                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
993   }
994  
995  
996 + getmapped()                     /* get color-mapped data */
997 + {
998 +        int     y;
999 +                                        /* make sure we can do it first */
1000 +        if (fname == NULL)
1001 +                quiterr("cannot map colors from standard input");
1002 +                                        /* set gamma correction */
1003 +        setcolrgam(gamcor);
1004 +                                        /* make histogram */
1005 +        if (new_histo((long)xmax*ymax) == -1)
1006 +                quiterr("cannot initialize histogram");
1007 +        for (y = 0; y < ymax; y++) {
1008 +                if (getscan(y) < 0)
1009 +                        break;
1010 +                add2icon(y, scanline);
1011 +                if (scale)
1012 +                        shiftcolrs(scanline, xmax, scale);
1013 +                colrs_gambs(scanline, xmax);
1014 +                cnt_colrs(scanline, xmax);
1015 +        }
1016 +                                        /* map pixels */
1017 +        if (!new_clrtab(maxcolors))
1018 +                quiterr("cannot create color map");
1019 +        for (y = 0; y < ymax; y++) {
1020 +                getscan(y);
1021 +                if (scale)
1022 +                        shiftcolrs(scanline, xmax, scale);
1023 +                colrs_gambs(scanline, xmax);
1024 +                if (dither)
1025 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
1026 +                else
1027 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
1028 +        }
1029 + }
1030 +
1031 +
1032   scale_rcolors(xr, sf)                   /* scale color map */
1033   register XRASTER        *xr;
1034   double  sf;
# Line 924 | Line 1060 | double sf;
1060   getscan(y)
1061   int  y;
1062   {
1063 +        static int  trunced = -1;               /* truncated file? */
1064 + skipit:
1065 +        if (trunced >= 0 && y >= trunced) {
1066 +                bzero(scanline, xmax*sizeof(COLR));
1067 +                return(-1);
1068 +        }
1069          if (y != cury) {
1070                  if (scanpos == NULL || scanpos[y] == -1)
1071                          return(-1);
1072                  if (fseek(fin, scanpos[y], 0) == -1)
1073                          quiterr("fseek error");
1074                  cury = y;
1075 <        } else if (scanpos != NULL)
1075 >        } else if (scanpos != NULL && scanpos[y] == -1)
1076                  scanpos[y] = ftell(fin);
1077  
1078 <        if (freadcolrs(scanline, xmax, fin) < 0)
1079 <                quiterr("read error");
1080 <
1078 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1079 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1080 >                                progname, fname==NULL?"<stdin>":fname);
1081 >                trunced = y;
1082 >                goto skipit;
1083 >        }
1084          cury++;
1085          return(0);
941 }
942
943
944 picreadline3(y, l3)                     /* read in 3-byte scanline */
945 int  y;
946 register rgbpixel  *l3;
947 {
948        register int    i;
949                                                        /* read scanline */
950        if (getscan(y) < 0)
951                quiterr("cannot seek for picreadline");
952                                                        /* convert scanline */
953        normcolrs(scanline, xmax, scale);
954        add2icon(y, scanline);
955        for (i = 0; i < xmax; i++) {
956                l3[i].r = scanline[i][RED];
957                l3[i].g = scanline[i][GRN];
958                l3[i].b = scanline[i][BLU];
959        }
960 }
961
962
963 picwriteline(y, l)              /* add 8-bit scanline to image */
964 int  y;
965 pixel  *l;
966 {
967        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
968 }
969
970
971 picreadcm(map)                  /* do gamma correction */
972 colormap  map;
973 {
974        extern double  pow();
975        register int  i, val;
976
977        for (i = 0; i < 256; i++) {
978                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
979                map[0][i] = map[1][i] = map[2][i] = val;
980        }
981 }
982
983
984 picwritecm(map)                 /* handled elsewhere */
985 colormap  map;
986 {
987 #if 0
988        register int i;
989
990        for (i = 0; i < 256; i++)
991                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
992 #endif
1086   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines