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.39 by greg, Mon Nov 21 09:18:48 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 151 | Line 167 | char  *argv[];
167                                  if (argv[i][2] == 'e')
168                                          geometry = argv[++i];
169                                  else
170 <                                        gamcor = atof(argv[++i]);
170 >                                        gamstr = argv[++i];
171                                  break;
172                          default:
173                                  goto userr;
# Line 161 | Line 177 | char  *argv[];
177                  else
178                          break;
179  
180 <        if (i == argc-1) {
180 >        if (i > argc)
181 >                goto userr;
182 >        while (i < argc-1) {
183 >                sigrecv = 0;
184 >                signal(SIGCONT, onsig);
185 >                if ((pid=fork()) == 0) {        /* a child for each picture */
186 >                        parent = -1;
187 >                        break;
188 >                }
189 >                if (pid < 0)
190 >                        quiterr("fork failed");
191 >                parent++;
192 >                while (!sigrecv)
193 >                        pause();        /* wait for wake-up call */
194 >                i++;
195 >        }
196 >        if (i < argc) {                 /* open picture file */
197                  fname = argv[i];
198                  fin = fopen(fname, "r");
199 <                if (fin == NULL) {
200 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
201 <                        quiterr(errmsg);
170 <                }
171 <        } else if (i != argc)
172 <                goto userr;
199 >                if (fin == NULL)
200 >                        quiterr("cannot open picture file");
201 >        }
202                                  /* get header */
203          getheader(fin, headline, NULL);
204                                  /* get picture dimensions */
# Line 183 | Line 212 | char  *argv[];
212          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
213                  quiterr("out of memory");
214  
215 <        init();                 /* get file and open window */
215 >        init(argc, argv);                       /* get file and open window */
216  
217          for ( ; ; )
218                  getevent();             /* main loop */
219   userr:
220          fprintf(stderr,
221 <        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
221 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor] pic ..\n",
222                          progname);
223 <        quit(1);
223 >        exit(1);
224   }
225  
226  
# Line 210 | Line 239 | char  *s;
239   }
240  
241  
242 < init()                  /* get data and open window */
242 > init(argc, argv)                        /* get data and open window */
243 > int argc;
244 > char **argv;
245   {
215        XWMHints        ourxwmhints;
246          XSetWindowAttributes    ourwinattr;
247 <        XSizeHints      oursizhints;
247 >        XClassHint      xclshints;
248 >        XWMHints        xwmhints;
249 >        XSizeHints      xszhints;
250 >        XTextProperty   windowName, iconName;
251 >        XGCValues       xgcv;
252 >        char    *name;
253          register int    i;
254          
255          if (fname != NULL) {
256                  scanpos = (long *)malloc(ymax*sizeof(long));
257                  if (scanpos == NULL)
258 <                        goto memerr;
258 >                        quiterr("out of memory");
259                  for (i = 0; i < ymax; i++)
260                          scanpos[i] = -1;
261 <        }
261 >                name = fname;
262 >        } else
263 >                name = progname;
264 >                                /* remove directory prefix from name */
265 >        for (i = strlen(name); i-- > 0; )
266 >                if (name[i] == '/')
267 >                        break;
268 >        name += i+1;
269          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
270                  quiterr("cannot open display");
271 +                                /* set gamma value */
272 +        if (gamstr == NULL)             /* get it from the X server */
273 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
274 +        if (gamstr == NULL)             /* get it from the environment */
275 +                gamstr = getenv("DISPLAY_GAMMA");
276 +        if (gamstr != NULL)
277 +                gamcor = atof(gamstr);
278                                  /* get best visual for default screen */
279          getbestvis();
280                                  /* store image */
281          getras();
282 <                                /* open window */
283 <        ourwinattr.border_pixel = ourblack;
284 <        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);
282 >                                /* get size and position */
283 >        xszhints.flags = 0;
284 >        xszhints.width = xmax; xszhints.height = ymax;
285          if (geometry != NULL) {
286 <                bzero((char *)&oursizhints, sizeof(oursizhints));
287 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
288 <                                (unsigned *)&oursizhints.width,
260 <                                (unsigned *)&oursizhints.height);
286 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
287 >                                (unsigned *)&xszhints.width,
288 >                                (unsigned *)&xszhints.height);
289                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
290 <                        oursizhints.flags |= USSize;
291 <                else {
292 <                        oursizhints.width = xmax;
265 <                        oursizhints.height = ymax;
266 <                        oursizhints.flags |= PSize;
267 <                }
290 >                        xszhints.flags |= USSize;
291 >                else
292 >                        xszhints.flags |= PSize;
293                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
294 <                        oursizhints.flags |= USPosition;
294 >                        xszhints.flags |= USPosition;
295                          if (i & XNegative)
296 <                                oursizhints.x += DisplayWidth(thedisplay,
297 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
296 >                                xszhints.x += DisplayWidth(thedisplay,
297 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
298                          if (i & YNegative)
299 <                                oursizhints.y += DisplayHeight(thedisplay,
300 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
299 >                                xszhints.y += DisplayHeight(thedisplay,
300 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
301                  }
277                XSetNormalHints(thedisplay, wind, &oursizhints);
302          }
303 <        ourxwmhints.flags = InputHint|IconPixmapHint;
304 <        ourxwmhints.input = True;
305 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
303 >        /* open window */
304 >        ourwinattr.border_pixel = ourwhite;
305 >        ourwinattr.background_pixel = ourblack;
306 >        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
307 >                        ourvis.visual, AllocNone);
308 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
309 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
310 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
311 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
312 >                        xszhints.width, xszhints.height, BORWIDTH,
313 >                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
314 >                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
315 >        if (wind == 0)
316 >                quiterr("cannot create window");
317 >        width = xmax;
318 >        height = ymax;
319 >        /* prepare graphics drawing context */
320 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
321 >                quiterr("cannot get font");
322 >        xgcv.foreground = ourblack;
323 >        xgcv.background = ourwhite;
324 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
325 >                        GCFont, &xgcv);
326 >        xgcv.function = GXinvert;
327 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
328 >                        GCFunction, &xgcv);
329 >
330 >        /* set up the window manager */
331 >        xwmhints.flags = InputHint|IconPixmapHint;
332 >        xwmhints.input = True;
333 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
334                          wind, icondata, iconwidth, iconheight);
335 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
336 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
337 <                        |ButtonMotionMask|StructureNotifyMask
338 <                        |KeyPressMask|ExposureMask);
335 >
336 >        windowName.encoding = iconName.encoding = XA_STRING;
337 >        windowName.format = iconName.format = 8;
338 >        windowName.value = (u_char *)name;
339 >        windowName.nitems = strlen(windowName.value);
340 >        iconName.value = (u_char *)name;
341 >        iconName.nitems = strlen(windowName.value);
342 >
343 >        xclshints.res_name = NULL;
344 >        xclshints.res_class = "Ximage";
345 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
346 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
347 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
348 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
349 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
350 >
351          XMapWindow(thedisplay, wind);
288        return;
289 memerr:
290        quiterr("out of memory");
352   } /* end of init */
353  
354  
355   quiterr(err)            /* print message and exit */
356   char  *err;
357   {
358 <        if (err != NULL) {
359 <                fprintf(stderr, "%s: %s\n", progname, err);
360 <                exit(1);
358 >        register int  es;
359 >        int  cs;
360 >
361 >        if (es = err != NULL)
362 >                fprintf(stderr, "%s: %s: %s\n", progname,
363 >                                fname==NULL?"<stdin>":fname, err);
364 >        if (thedisplay != NULL)
365 >                XCloseDisplay(thedisplay);
366 >        if (parent < 0 & sigrecv == 0)
367 >                kill(getppid(), SIGCONT);
368 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
369 >                if (es == 0)
370 >                        es = cs>>8 & 0xff;
371 >                parent--;
372          }
373 <        exit(0);
373 >        exit(es);
374   }
375  
376  
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
377   static int
378   viscmp(v1,v2)           /* compare visual to see which is better, descending */
379   register XVisualInfo    *v1, *v2;
# Line 419 | Line 477 | static char  vistype[][12] = {
477                  greyscale = 1;
478          if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
479                  maxcolors = ourvis.colormap_size;
422        if (maxcolors > 4)
423                maxcolors -= 2;
480          if (ourvis.class == StaticGray) {
481                  ourblack = 0;
482                  ourwhite = 255;
483          } else if (ourvis.class == PseudoColor) {
484                  ourblack = BlackPixel(thedisplay,ourscreen);
485                  ourwhite = WhitePixel(thedisplay,ourscreen);
486 +                if ((ourblack|ourwhite) & ~255L) {
487 +                        ourblack = 0;
488 +                        ourwhite = 1;
489 +                }
490 +                if (maxcolors > 4)
491 +                        maxcolors -= 2;
492          } else {
493                  ourblack = 0;
494 <                ourwhite = ~0;
494 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
495          }
496          XFree((char *)xvi);
497   }
# Line 437 | Line 499 | static char  vistype[][12] = {
499  
500   getras()                                /* get raster file */
501   {
440        colormap        ourmap;
502          XVisualInfo     vinfo;
503  
504          if (maxcolors <= 2) {           /* monochrome */
# Line 449 | Line 510 | getras()                               /* get raster file */
510                  if (ourras == NULL)
511                          goto fail;
512                  getmono();
513 <        } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
514 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
513 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
514 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
515                  if (ourdata == NULL)
516                          goto fail;
517 <                ourras = make_raster(thedisplay, &ourvis, 32,
517 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
518                                  ourdata, xmax, ymax, 32);
519                  if (ourras == NULL)
520                          goto fail;
# Line 466 | Line 527 | getras()                               /* get raster file */
527                                  xmax, ymax, 8);
528                  if (ourras == NULL)
529                          goto fail;
530 <                if (ourvis.class == StaticGray)
530 >                if (greyscale | ourvis.class == StaticGray)
531                          getgrey();
532 <                else {
533 <                        if (greyscale)
534 <                                biq(dither,maxcolors,1,ourmap);
535 <                        else
475 <                                ciq(dither,maxcolors,1,ourmap);
476 <                        if (init_rcolors(ourras, ourmap[0],
477 <                                        ourmap[1], ourmap[2]) == 0)
478 <                                goto fail;
479 <                }
532 >                else
533 >                        getmapped();
534 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
535 >                        goto fail;
536          }
537 <                return;
537 >        return;
538   fail:
539          quiterr("could not create raster image");
540   }
# Line 486 | Line 542 | fail:
542  
543   getevent()                              /* process the next event */
544   {
545 <        union {
490 <                XEvent  u;
491 <                XConfigureEvent  c;
492 <                XExposeEvent  e;
493 <                XButtonPressedEvent  b;
494 <                XKeyPressedEvent  k;
495 <        } e;
545 >        XEvent xev;
546  
547 <        XNextEvent(thedisplay, &e.u);
548 <        switch (e.u.type) {
547 >        XNextEvent(thedisplay, &xev);
548 >        switch ((int)xev.type) {
549          case KeyPress:
550 <                docom(&e.k);
550 >                docom(&xev.xkey);
551                  break;
552          case ConfigureNotify:
553 <                width = e.c.width;
554 <                height = e.c.height;
553 >                width = xev.xconfigure.width;
554 >                height = xev.xconfigure.height;
555                  break;
556          case MapNotify:
557                  map_rcolors(ourras, wind);
558                  if (fast)
559                          make_rpixmap(ourras, wind);
560 +                if (parent < 0 & sigrecv == 0) {        /* notify parent */
561 +                        kill(getppid(), SIGCONT);
562 +                        sigrecv--;
563 +                }
564                  break;
565          case UnmapNotify:
566                  if (!fast)
567                          unmap_rcolors(ourras);
568                  break;
569          case Expose:
570 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
570 >                redraw(xev.xexpose.x, xev.xexpose.y,
571 >                                xev.xexpose.width, xev.xexpose.height);
572                  break;
573          case ButtonPress:
574 <                if (e.b.state & (ShiftMask|ControlMask))
575 <                        moveimage(&e.b);
574 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
575 >                        moveimage(&xev.xbutton);
576 >                else if (xev.xbutton.button == Button2)
577 >                        traceray(xev.xbutton.x, xev.xbutton.y);
578                  else
579 <                        getbox(&e.b);
579 >                        getbox(&xev.xbutton);
580                  break;
581 +        case ClientMessage:
582 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
583 +                                (xev.xclient.data.l[0] == closedownAtom))
584 +                        quiterr(NULL);
585 +                break;
586          }
587   }
588  
589  
590 < docom(ekey)                                     /* execute command */
590 > traceray(xpos, ypos)                    /* print ray corresponding to pixel */
591 > int  xpos, ypos;
592 > {
593 >        FLOAT  hv[2];
594 >        FVECT  rorg, rdir;
595 >
596 >        if (!gotview) {         /* no view, no can do */
597 >                XBell(thedisplay, 0);
598 >                return(-1);
599 >        }
600 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
601 >        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
602 >                return(-1);
603 >        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
604 >        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
605 >        fflush(stdout);
606 >        return(0);
607 > }
608 >
609 >
610 > docom(ekey)                             /* execute command */
611   XKeyPressedEvent  *ekey;
612   {
613          char  buf[80];
# Line 534 | Line 616 | XKeyPressedEvent  *ekey;
616          int  com, n;
617          double  comp;
618          FLOAT  hv[2];
537        FVECT  rorg, rdir;
619  
620          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
621          if (n == 0)
# Line 542 | Line 623 | XKeyPressedEvent  *ekey;
623          com = buf[0];
624          switch (com) {                  /* interpret command */
625          case 'q':
626 +        case 'Q':
627          case CTRL('D'):                         /* quit */
628 <                quit(0);
628 >                quiterr(NULL);
629          case '\n':
630          case '\r':
631          case 'l':
# Line 589 | Line 671 | XKeyPressedEvent  *ekey;
671                                          buf, strlen(buf));
672                  return(0);
673          case 't':                               /* trace */
674 <                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);
674 >                return(traceray(ekey->x, ekey->y));
675          case '=':                               /* adjust exposure */
676 +        case '@':                               /* adaptation level */
677                  if (avgbox(cval) == -1)
678                          return(-1);
679 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
679 >                comp = bright(cval);
680 >                if (comp < 1e-20) {
681 >                        XBell(thedisplay, 0);
682 >                        return(-1);
683 >                }
684 >                if (com == '@')
685 >                        comp = 106./exposure/
686 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
687 >                else
688 >                        comp = .5/comp;
689 >                comp = log(comp)/.69315 - scale;
690 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
691                  if (n == 0)
692                          return(0);
693                  scale_rcolors(ourras, pow(2.0, (double)n));
# Line 622 | Line 706 | XKeyPressedEvent  *ekey;
706                  XClearWindow(thedisplay, wind);
707                  map_rcolors(ourras, wind);
708                  if (fast)
709 <                        make_rpixmap(ourras);
709 >                        make_rpixmap(ourras, wind);
710                  redraw(0, 0, width, height);
711                  return(0);
712 +        case 'f':                               /* turn on fast redraw */
713 +                fast = 1;
714 +                make_rpixmap(ourras, wind);
715 +                return(0);
716 +        case 'F':                               /* turn off fast redraw */
717 +                fast = 0;
718 +                free_rpixmap(ourras);
719 +                return(0);
720 +        case '0':                               /* recenter origin */
721 +                if (xoff == 0 & yoff == 0)
722 +                        return(0);
723 +                xoff = yoff = 0;
724 +                XClearWindow(thedisplay, wind);
725 +                redraw(0, 0, width, height);
726 +                return(0);
727          case ' ':                               /* clear */
728                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
729                  return(0);
# Line 638 | Line 737 | XKeyPressedEvent  *ekey;
737   moveimage(ebut)                         /* shift the image */
738   XButtonPressedEvent  *ebut;
739   {
740 <        union {
642 <                XEvent  u;
643 <                XButtonReleasedEvent  b;
644 <                XPointerMovedEvent  m;
645 <        }  e;
740 >        XEvent  e;
741          int     mxo, myo;
742  
743 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
744 <        while (e.u.type == MotionNotify) {
745 <                mxo = e.m.x;
746 <                myo = e.m.y;
743 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
744 >        while (e.type == MotionNotify) {
745 >                mxo = e.xmotion.x;
746 >                myo = e.xmotion.y;
747                  revline(ebut->x, ebut->y, mxo, myo);
748                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
749                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
750 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
750 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
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          }
755 <        xoff += e.b.x - ebut->x;
756 <        yoff += e.b.y - ebut->y;
755 >        xoff += e.xbutton.x - ebut->x;
756 >        yoff += e.xbutton.y - ebut->y;
757          XClearWindow(thedisplay, wind);
758          redraw(0, 0, width, height);
759   }
# Line 667 | Line 762 | XButtonPressedEvent  *ebut;
762   getbox(ebut)                            /* get new box */
763   XButtonPressedEvent  *ebut;
764   {
765 <        union {
671 <                XEvent  u;
672 <                XButtonReleasedEvent  b;
673 <                XPointerMovedEvent  m;
674 <        }  e;
765 >        XEvent  e;
766  
767 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
768 <        while (e.u.type == MotionNotify) {
769 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
770 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
767 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
768 >        while (e.type == MotionNotify) {
769 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
770 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
771                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
772          }
773 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
774 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
773 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
774 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
775          if (box.xmin > ebut->x) {
776                  box.xsiz = box.xmin - ebut->x + 1;
777                  box.xmin = ebut->x;
# Line 736 | Line 827 | COLOR  clr;
827                  return(-1);
828          if (left == ll && right == lr && top == lt && bottom == lb) {
829                  copycolor(clr, lc);
830 <                return;
830 >                return(0);
831          }
832          for (y = top; y < bottom; y++) {
833                  if (getscan(y) == -1)
# Line 765 | Line 856 | getmono()                      /* get monochrome data */
856                  quiterr("out of memory in getmono");
857          dp = ourdata - 1;
858          for (y = 0; y < ymax; y++) {
859 <                if (getscan(y) < 0)
769 <                        quiterr("seek error in getmono");
770 <                normcolrs(scanline, xmax, scale);
859 >                getscan(y);
860                  add2icon(y, scanline);
861 +                normcolrs(scanline, xmax, scale);
862                  err = 0;
863                  for (x = 0; x < xmax; x++) {
864                          if (!(x&7))
# Line 794 | Line 884 | COLR  *scan;
884          static short  cerr[ICONSIZ];
885          static int  ynext;
886          static char  *dp;
887 +        COLR  clr;
888          register int  err;
889          register int    x, ti;
890          int  errp;
# Line 824 | Line 915 | COLR  *scan;
915                          *++dp = 0;
916                  errp = err;
917                  ti = x*xmax/iconwidth;
918 <                err += normbright(scan[ti]) + cerr[x];
918 >                copycolr(clr, scan[ti]);
919 >                normcolrs(clr, 1, scale);
920 >                err += normbright(clr) + cerr[x];
921                  if (err > 127)
922                          err -= 255;
923                  else
# Line 839 | Line 932 | COLR  *scan;
932   getfull()                       /* get full (24-bit) data */
933   {
934          int     y;
935 <        register unsigned long  *dp;
935 >        register unsigned int4  *dp;
936          register int    x;
937                                          /* set gamma correction */
938          setcolrgam(gamcor);
939                                          /* read and convert file */
940 <        dp = (unsigned long *)ourdata;
940 >        dp = (unsigned int4 *)ourdata;
941          for (y = 0; y < ymax; y++) {
942 <                if (getscan(y) < 0)
943 <                        quiterr("seek error in getfull");
942 >                getscan(y);
943 >                add2icon(y, scanline);
944                  if (scale)
945                          shiftcolrs(scanline, xmax, scale);
946                  colrs_gambs(scanline, xmax);
854                add2icon(y, scanline);
947                  if (ourras->image->blue_mask & 1)
948                          for (x = 0; x < xmax; x++)
949 <                                *dp++ = scanline[x][RED] << 16 |
950 <                                        scanline[x][GRN] << 8 |
951 <                                        scanline[x][BLU] ;
949 >                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
950 >                                        (unsigned int4)scanline[x][GRN] << 8 |
951 >                                        (unsigned int4)scanline[x][BLU] ;
952                  else
953                          for (x = 0; x < xmax; x++)
954 <                                *dp++ = scanline[x][RED] |
955 <                                        scanline[x][GRN] << 8 |
956 <                                        scanline[x][BLU] << 16 ;
954 >                                *dp++ = (unsigned int4)scanline[x][RED] |
955 >                                        (unsigned int4)scanline[x][GRN] << 8 |
956 >                                        (unsigned int4)scanline[x][BLU] << 16 ;
957          }
958   }
959  
# Line 876 | Line 968 | getgrey()                      /* get greyscale data */
968                                          /* read and convert file */
969          dp = ourdata;
970          for (y = 0; y < ymax; y++) {
971 <                if (getscan(y) < 0)
972 <                        quiterr("seek error in getfull");
971 >                getscan(y);
972 >                add2icon(y, scanline);
973                  if (scale)
974                          shiftcolrs(scanline, xmax, scale);
975 +                for (x = 0; x < xmax; x++)
976 +                        scanline[x][GRN] = normbright(scanline[x]);
977                  colrs_gambs(scanline, xmax);
978 <                add2icon(y, scanline);
885 <                if (ourvis.colormap_size < 256)
978 >                if (maxcolors < 256)
979                          for (x = 0; x < xmax; x++)
980 <                                *dp++ = ((long)normbright(scanline[x]) *
981 <                                        ourvis.colormap_size + 128) >> 8;
980 >                                *dp++ = ((long)scanline[x][GRN] *
981 >                                        maxcolors + maxcolors/2) >> 8;
982                  else
983                          for (x = 0; x < xmax; x++)
984 <                                *dp++ = normbright(scanline[x]);
984 >                                *dp++ = scanline[x][GRN];
985          }
986 +        for (x = 0; x < maxcolors; x++)
987 +                clrtab[x][RED] = clrtab[x][GRN] =
988 +                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
989   }
990  
991  
992 + getmapped()                     /* get color-mapped data */
993 + {
994 +        int     y;
995 +                                        /* make sure we can do it first */
996 +        if (fname == NULL)
997 +                quiterr("cannot map colors from standard input");
998 +                                        /* set gamma correction */
999 +        setcolrgam(gamcor);
1000 +                                        /* make histogram */
1001 +        if (new_histo((long)xmax*ymax) == -1)
1002 +                quiterr("cannot initialize histogram");
1003 +        for (y = 0; y < ymax; y++) {
1004 +                if (getscan(y) < 0)
1005 +                        break;
1006 +                add2icon(y, scanline);
1007 +                if (scale)
1008 +                        shiftcolrs(scanline, xmax, scale);
1009 +                colrs_gambs(scanline, xmax);
1010 +                cnt_colrs(scanline, xmax);
1011 +        }
1012 +                                        /* map pixels */
1013 +        if (!new_clrtab(maxcolors))
1014 +                quiterr("cannot create color map");
1015 +        for (y = 0; y < ymax; y++) {
1016 +                getscan(y);
1017 +                if (scale)
1018 +                        shiftcolrs(scanline, xmax, scale);
1019 +                colrs_gambs(scanline, xmax);
1020 +                if (dither)
1021 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
1022 +                else
1023 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
1024 +        }
1025 + }
1026 +
1027 +
1028   scale_rcolors(xr, sf)                   /* scale color map */
1029   register XRASTER        *xr;
1030   double  sf;
# Line 924 | Line 1056 | double sf;
1056   getscan(y)
1057   int  y;
1058   {
1059 +        static int  trunced = -1;               /* truncated file? */
1060 + skipit:
1061 +        if (trunced >= 0 && y >= trunced) {
1062 +                bzero(scanline, xmax*sizeof(COLR));
1063 +                return(-1);
1064 +        }
1065          if (y != cury) {
1066                  if (scanpos == NULL || scanpos[y] == -1)
1067                          return(-1);
1068                  if (fseek(fin, scanpos[y], 0) == -1)
1069                          quiterr("fseek error");
1070                  cury = y;
1071 <        } else if (scanpos != NULL)
1071 >        } else if (scanpos != NULL && scanpos[y] == -1)
1072                  scanpos[y] = ftell(fin);
1073  
1074 <        if (freadcolrs(scanline, xmax, fin) < 0)
1075 <                quiterr("read error");
1076 <
1074 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1075 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1076 >                                progname, fname==NULL?"<stdin>":fname);
1077 >                trunced = y;
1078 >                goto skipit;
1079 >        }
1080          cury++;
1081          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
1082   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines