ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
(Generate patch)

Comparing ray/src/px/x11image.c (file contents):
Revision 2.10 by greg, Wed Jun 24 09:17:39 1992 UTC vs.
Revision 2.32 by greg, Thu Oct 28 11:39:59 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 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 56 | Line 64 | char   *dispname = NULL;               /* our display name */
64  
65   Window  wind = 0;                       /* our output window */
66   unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
59 Font  fontid;                           /* our font */
60
67   int  maxcolors = 0;                     /* maximum colors */
68   int  greyscale = 0;                     /* in grey */
69  
# Line 66 | Line 72 | int  scale = 0;                                /* scalefactor; power of two */
72   int  xoff = 0;                          /* x image offset */
73   int  yoff = 0;                          /* y image offset */
74  
75 + int  parent = 0;                        /* number of children, -1 if child */
76 +
77   VIEW  ourview = STDVIEW;                /* image view parameters */
78   int  gotview = 0;                       /* got parameters from file */
79  
# Line 104 | Line 112 | char  *progname;
112  
113   char  errmsg[128];
114  
115 + extern BYTE  clrtab[256][3];            /* global color map */
116 +
117   extern long  ftell();
118  
119 < extern char  *malloc(), *calloc();
119 > Display  *thedisplay;
120 > Atom  closedownAtom, wmProtocolsAtom;
121  
122 < extern double  pow(), log();
122 > int  sigrecv;
123  
124 < Display  *thedisplay;
124 > int  onsig() { sigrecv++; }
125  
126 +
127   main(argc, argv)
128   int  argc;
129   char  *argv[];
# Line 120 | Line 132 | char  *argv[];
132          char  *gv;
133          int  headline();
134          int  i;
135 +        int  pid;
136          
137          progname = argv[0];
138          if ((gv = getenv("GAMMA")) != NULL)
# Line 165 | 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);
203                  }
204 <        } else if (i != argc)
176 <                goto userr;
204 >        }
205                                  /* get header */
206          getheader(fin, headline, NULL);
207                                  /* get picture dimensions */
# Line 187 | Line 215 | char  *argv[];
215          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
216                  quiterr("out of memory");
217  
218 <        init();                 /* get file and open window */
218 >        init(argc, argv);                       /* get file and open window */
219  
220 +        if (parent < 0) {
221 +                kill(getppid(), SIGCONT);       /* signal parent if child */
222 +                sigrecv--;
223 +        }
224          for ( ; ; )
225                  getevent();             /* main loop */
226   userr:
227          fprintf(stderr,
228 <        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
228 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n",
229                          progname);
230 <        quit(1);
230 >        exit(1);
231   }
232  
233  
# Line 214 | Line 246 | char  *s;
246   }
247  
248  
249 < init()                  /* get data and open window */
249 > init(argc, argv)                        /* get data and open window */
250 > int argc;
251 > char **argv;
252   {
219        XWMHints        ourxwmhints;
253          XSetWindowAttributes    ourwinattr;
254 <        XSizeHints      oursizhints;
254 >        XClassHint      xclshints;
255 >        XWMHints        xwmhints;
256 >        XSizeHints      xszhints;
257 >        XTextProperty   windowName, iconName;
258 >        XGCValues       xgcv;
259 >        char    *name;
260          register int    i;
261          
262          if (fname != NULL) {
263                  scanpos = (long *)malloc(ymax*sizeof(long));
264                  if (scanpos == NULL)
265 <                        goto memerr;
265 >                        quiterr("out of memory");
266                  for (i = 0; i < ymax; i++)
267                          scanpos[i] = -1;
268 <        }
268 >                name = fname;
269 >        } else
270 >                name = progname;
271 >                                /* remove directory prefix from name */
272 >        for (i = strlen(name); i-- > 0; )
273 >                if (name[i] == '/')
274 >                        break;
275 >        name += i+1;
276          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
277                  quiterr("cannot open display");
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;
240 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
241 <                        ourvis.visual, AllocNone);
242 <        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
243 <                        ourvis.depth, InputOutput, ourvis.visual,
244 <                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
245 <        if (wind == 0)
246 <                quiterr("cannot create window");
247 <        XFreeColormap(thedisplay, ourwinattr.colormap);
248 <        width = xmax;
249 <        height = ymax;
250 <        ourgc = XCreateGC(thedisplay, wind, 0, 0);
251 <        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
252 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
253 <        XSetFunction(thedisplay, revgc, GXinvert);
254 <        fontid = XLoadFont(thedisplay, FONTNAME);
255 <        if (fontid == 0)
256 <                quiterr("cannot get font");
257 <        XSetFont(thedisplay, ourgc, fontid);
258 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
259 <                        XC_diamond_cross));
260 <        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,
265 <                                (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;
270 <                        oursizhints.height = ymax;
271 <                        oursizhints.flags |= PSize;
272 <                }
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                  }
282                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);
352          return;
294 memerr:
295        quiterr("out of memory");
353   } /* end of init */
354  
355  
356   quiterr(err)            /* print message and exit */
357   char  *err;
358   {
359 <        if (err != NULL) {
360 <                fprintf(stderr, "%s: %s\n", progname, err);
361 <                exit(1);
359 >        register int  es;
360 >        int  cs;
361 >
362 >        if (es = err != NULL)
363 >                fprintf(stderr, "%s: %s: %s\n", progname,
364 >                                fname==NULL?"<stdin>":fname, err);
365 >        if (parent > 0 & wind != 0) {
366 >                XDestroyWindow(thedisplay, wind);
367 >                XFlush(thedisplay);
368 >        } else if (parent < 0 & sigrecv == 0)
369 >                kill(getppid(), SIGCONT);
370 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
371 >                if (es == 0)
372 >                        es = cs>>8 & 0xff;
373 >                parent--;
374          }
375 <        exit(0);
375 >        exit(es);
376   }
377  
378  
310 eputs(s)
311 char    *s;
312 {
313        fputs(s, stderr);
314 }
315
316
317 quit(code)
318 int  code;
319 {
320        exit(code);
321 }
322
323
379   static int
380   viscmp(v1,v2)           /* compare visual to see which is better, descending */
381   register XVisualInfo    *v1, *v2;
# Line 424 | Line 479 | static char  vistype[][12] = {
479                  greyscale = 1;
480          if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
481                  maxcolors = ourvis.colormap_size;
427        if (maxcolors > 4)
428                maxcolors -= 2;
482          if (ourvis.class == StaticGray) {
483                  ourblack = 0;
484                  ourwhite = 255;
# Line 436 | Line 489 | static char  vistype[][12] = {
489                          ourblack = 0;
490                          ourwhite = 1;
491                  }
492 +                if (maxcolors > 4)
493 +                        maxcolors -= 2;
494          } else {
495                  ourblack = 0;
496                  ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
# Line 446 | Line 501 | static char  vistype[][12] = {
501  
502   getras()                                /* get raster file */
503   {
449        colormap        ourmap;
504          XVisualInfo     vinfo;
505  
506          if (maxcolors <= 2) {           /* monochrome */
# Line 458 | Line 512 | getras()                               /* get raster file */
512                  if (ourras == NULL)
513                          goto fail;
514                  getmono();
515 <        } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
516 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
515 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
516 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
517                  if (ourdata == NULL)
518                          goto fail;
519 <                ourras = make_raster(thedisplay, &ourvis, 32,
519 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
520                                  ourdata, xmax, ymax, 32);
521                  if (ourras == NULL)
522                          goto fail;
# Line 475 | Line 529 | getras()                               /* get raster file */
529                                  xmax, ymax, 8);
530                  if (ourras == NULL)
531                          goto fail;
532 <                if (ourvis.class == StaticGray)
532 >                if (greyscale | ourvis.class == StaticGray)
533                          getgrey();
534 <                else {
535 <                        if (greyscale)
536 <                                biq(dither,maxcolors,1,ourmap);
537 <                        else
484 <                                ciq(dither,maxcolors,1,ourmap);
485 <                        if (init_rcolors(ourras, ourmap[0],
486 <                                        ourmap[1], ourmap[2]) == 0)
487 <                                goto fail;
488 <                }
534 >                else
535 >                        getmapped();
536 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
537 >                        goto fail;
538          }
539 <                return;
539 >        return;
540   fail:
541          quiterr("could not create raster image");
542   }
# Line 495 | Line 544 | fail:
544  
545   getevent()                              /* process the next event */
546   {
547 <        union {
499 <                XEvent  u;
500 <                XConfigureEvent  c;
501 <                XExposeEvent  e;
502 <                XButtonPressedEvent  b;
503 <                XKeyPressedEvent  k;
504 <        } e;
547 >        XEvent xev;
548  
549 <        XNextEvent(thedisplay, &e.u);
550 <        switch (e.u.type) {
549 >        XNextEvent(thedisplay, &xev);
550 >        switch ((int)xev.type) {
551          case KeyPress:
552 <                docom(&e.k);
552 >                docom(&xev.xkey);
553                  break;
554          case ConfigureNotify:
555 <                width = e.c.width;
556 <                height = e.c.height;
555 >                width = xev.xconfigure.width;
556 >                height = xev.xconfigure.height;
557                  break;
558          case MapNotify:
559                  map_rcolors(ourras, wind);
# Line 522 | Line 565 | getevent()                             /* process the next event */
565                          unmap_rcolors(ourras);
566                  break;
567          case Expose:
568 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
568 >                redraw(xev.xexpose.x, xev.xexpose.y,
569 >                                xev.xexpose.width, xev.xexpose.height);
570                  break;
571          case ButtonPress:
572 <                if (e.b.state & (ShiftMask|ControlMask))
573 <                        moveimage(&e.b);
572 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
573 >                        moveimage(&xev.xbutton);
574 >                else if (xev.xbutton.button == Button2)
575 >                        traceray(xev.xbutton.x, xev.xbutton.y);
576                  else
577 <                        getbox(&e.b);
577 >                        getbox(&xev.xbutton);
578                  break;
579 +        case ClientMessage:
580 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
581 +                                (xev.xclient.data.l[0] == closedownAtom))
582 +                        quiterr(NULL);
583 +                break;
584          }
585   }
586  
587  
588 < docom(ekey)                                     /* execute command */
588 > traceray(xpos, ypos)                    /* print ray corresponding to pixel */
589 > int  xpos, ypos;
590 > {
591 >        FLOAT  hv[2];
592 >        FVECT  rorg, rdir;
593 >
594 >        if (!gotview) {         /* no view, no can do */
595 >                XBell(thedisplay, 0);
596 >                return(-1);
597 >        }
598 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
599 >        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
600 >                return(-1);
601 >        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
602 >        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
603 >        fflush(stdout);
604 >        return(0);
605 > }
606 >
607 >
608 > docom(ekey)                             /* execute command */
609   XKeyPressedEvent  *ekey;
610   {
611          char  buf[80];
# Line 543 | Line 614 | XKeyPressedEvent  *ekey;
614          int  com, n;
615          double  comp;
616          FLOAT  hv[2];
546        FVECT  rorg, rdir;
617  
618          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
619          if (n == 0)
# Line 551 | Line 621 | XKeyPressedEvent  *ekey;
621          com = buf[0];
622          switch (com) {                  /* interpret command */
623          case 'q':
624 +        case 'Q':
625          case CTRL('D'):                         /* quit */
626 <                quit(0);
626 >                quiterr(NULL);
627          case '\n':
628          case '\r':
629          case 'l':
# Line 598 | Line 669 | XKeyPressedEvent  *ekey;
669                                          buf, strlen(buf));
670                  return(0);
671          case 't':                               /* trace */
672 <                if (!gotview) {
602 <                        XBell(thedisplay, 0);
603 <                        return(-1);
604 <                }
605 <                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
606 <                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
607 <                        return(-1);
608 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
609 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
610 <                fflush(stdout);
611 <                return(0);
672 >                return(traceray(ekey->x, ekey->y));
673          case '=':                               /* adjust exposure */
674 +        case '@':                               /* adaptation level */
675                  if (avgbox(cval) == -1)
676                          return(-1);
677 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
677 >                comp = com=='@'
678 >                ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure
679 >                : .5/bright(cval) ;
680 >                comp = log(comp)/.69315 - scale;
681 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
682                  if (n == 0)
683                          return(0);
684                  scale_rcolors(ourras, pow(2.0, (double)n));
# Line 631 | Line 697 | XKeyPressedEvent  *ekey;
697                  XClearWindow(thedisplay, wind);
698                  map_rcolors(ourras, wind);
699                  if (fast)
700 <                        make_rpixmap(ourras);
700 >                        make_rpixmap(ourras, wind);
701                  redraw(0, 0, width, height);
702                  return(0);
703 +        case 'f':                               /* turn on fast redraw */
704 +                fast = 1;
705 +                make_rpixmap(ourras, wind);
706 +                return(0);
707 +        case 'F':                               /* turn off fast redraw */
708 +                fast = 0;
709 +                free_rpixmap(ourras);
710 +                return(0);
711 +        case '0':                               /* recenter origin */
712 +                if (xoff == 0 & yoff == 0)
713 +                        return(0);
714 +                xoff = yoff = 0;
715 +                XClearWindow(thedisplay, wind);
716 +                redraw(0, 0, width, height);
717 +                return(0);
718          case ' ':                               /* clear */
719                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
720                  return(0);
# Line 647 | Line 728 | XKeyPressedEvent  *ekey;
728   moveimage(ebut)                         /* shift the image */
729   XButtonPressedEvent  *ebut;
730   {
731 <        union {
651 <                XEvent  u;
652 <                XButtonReleasedEvent  b;
653 <                XPointerMovedEvent  m;
654 <        }  e;
731 >        XEvent  e;
732          int     mxo, myo;
733  
734 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
735 <        while (e.u.type == MotionNotify) {
736 <                mxo = e.m.x;
737 <                myo = e.m.y;
734 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
735 >        while (e.type == MotionNotify) {
736 >                mxo = e.xmotion.x;
737 >                myo = e.xmotion.y;
738                  revline(ebut->x, ebut->y, mxo, myo);
739                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
740                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
741 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
741 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
742                  revline(ebut->x, ebut->y, mxo, myo);
743                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
744                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
745          }
746 <        xoff += e.b.x - ebut->x;
747 <        yoff += e.b.y - ebut->y;
746 >        xoff += e.xbutton.x - ebut->x;
747 >        yoff += e.xbutton.y - ebut->y;
748          XClearWindow(thedisplay, wind);
749          redraw(0, 0, width, height);
750   }
# Line 676 | Line 753 | XButtonPressedEvent  *ebut;
753   getbox(ebut)                            /* get new box */
754   XButtonPressedEvent  *ebut;
755   {
756 <        union {
680 <                XEvent  u;
681 <                XButtonReleasedEvent  b;
682 <                XPointerMovedEvent  m;
683 <        }  e;
756 >        XEvent  e;
757  
758 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
759 <        while (e.u.type == MotionNotify) {
760 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
761 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
758 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
759 >        while (e.type == MotionNotify) {
760 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
761 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
762                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
763          }
764 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
765 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
764 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
765 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
766          if (box.xmin > ebut->x) {
767                  box.xsiz = box.xmin - ebut->x + 1;
768                  box.xmin = ebut->x;
# Line 745 | Line 818 | COLOR  clr;
818                  return(-1);
819          if (left == ll && right == lr && top == lt && bottom == lb) {
820                  copycolor(clr, lc);
821 <                return;
821 >                return(0);
822          }
823          for (y = top; y < bottom; y++) {
824                  if (getscan(y) == -1)
# Line 774 | Line 847 | getmono()                      /* get monochrome data */
847                  quiterr("out of memory in getmono");
848          dp = ourdata - 1;
849          for (y = 0; y < ymax; y++) {
850 <                if (getscan(y) < 0)
778 <                        quiterr("seek error in getmono");
779 <                normcolrs(scanline, xmax, scale);
850 >                getscan(y);
851                  add2icon(y, scanline);
852 +                normcolrs(scanline, xmax, scale);
853                  err = 0;
854                  for (x = 0; x < xmax; x++) {
855                          if (!(x&7))
# Line 803 | Line 875 | COLR  *scan;
875          static short  cerr[ICONSIZ];
876          static int  ynext;
877          static char  *dp;
878 +        COLR  clr;
879          register int  err;
880          register int    x, ti;
881          int  errp;
# Line 833 | Line 906 | COLR  *scan;
906                          *++dp = 0;
907                  errp = err;
908                  ti = x*xmax/iconwidth;
909 <                err += normbright(scan[ti]) + cerr[x];
909 >                copycolr(clr, scan[ti]);
910 >                normcolrs(clr, 1, scale);
911 >                err += normbright(clr) + cerr[x];
912                  if (err > 127)
913                          err -= 255;
914                  else
# Line 848 | Line 923 | COLR  *scan;
923   getfull()                       /* get full (24-bit) data */
924   {
925          int     y;
926 <        register unsigned long  *dp;
926 >        register unsigned int4  *dp;
927          register int    x;
928                                          /* set gamma correction */
929          setcolrgam(gamcor);
930                                          /* read and convert file */
931 <        dp = (unsigned long *)ourdata;
931 >        dp = (unsigned int4 *)ourdata;
932          for (y = 0; y < ymax; y++) {
933 <                if (getscan(y) < 0)
934 <                        quiterr("seek error in getfull");
933 >                getscan(y);
934 >                add2icon(y, scanline);
935                  if (scale)
936                          shiftcolrs(scanline, xmax, scale);
937                  colrs_gambs(scanline, xmax);
863                add2icon(y, scanline);
938                  if (ourras->image->blue_mask & 1)
939                          for (x = 0; x < xmax; x++)
940                                  *dp++ = scanline[x][RED] << 16 |
# Line 885 | Line 959 | getgrey()                      /* get greyscale data */
959                                          /* read and convert file */
960          dp = ourdata;
961          for (y = 0; y < ymax; y++) {
962 <                if (getscan(y) < 0)
963 <                        quiterr("seek error in getfull");
962 >                getscan(y);
963 >                add2icon(y, scanline);
964                  if (scale)
965                          shiftcolrs(scanline, xmax, scale);
966 +                for (x = 0; x < xmax; x++)
967 +                        scanline[x][GRN] = normbright(scanline[x]);
968                  colrs_gambs(scanline, xmax);
969 <                add2icon(y, scanline);
894 <                if (ourvis.colormap_size < 256)
969 >                if (maxcolors < 256)
970                          for (x = 0; x < xmax; x++)
971 <                                *dp++ = ((long)normbright(scanline[x]) *
972 <                                        ourvis.colormap_size + 128) >> 8;
971 >                                *dp++ = ((long)scanline[x][GRN] *
972 >                                        maxcolors + maxcolors/2) >> 8;
973                  else
974                          for (x = 0; x < xmax; x++)
975 <                                *dp++ = normbright(scanline[x]);
975 >                                *dp++ = scanline[x][GRN];
976          }
977 +        for (x = 0; x < maxcolors; x++)
978 +                clrtab[x][RED] = clrtab[x][GRN] =
979 +                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
980   }
981  
982  
983 + getmapped()                     /* get color-mapped data */
984 + {
985 +        int     y;
986 +                                        /* make sure we can do it first */
987 +        if (fname == NULL)
988 +                quiterr("cannot map colors from standard input");
989 +                                        /* set gamma correction */
990 +        setcolrgam(gamcor);
991 +                                        /* make histogram */
992 +        new_histo();
993 +        for (y = 0; y < ymax; y++) {
994 +                if (getscan(y) < 0)
995 +                        break;
996 +                add2icon(y, scanline);
997 +                if (scale)
998 +                        shiftcolrs(scanline, xmax, scale);
999 +                colrs_gambs(scanline, xmax);
1000 +                cnt_colrs(scanline, xmax);
1001 +        }
1002 +                                        /* map pixels */
1003 +        if (!new_clrtab(maxcolors))
1004 +                quiterr("cannot create color map");
1005 +        for (y = 0; y < ymax; y++) {
1006 +                getscan(y);
1007 +                if (scale)
1008 +                        shiftcolrs(scanline, xmax, scale);
1009 +                colrs_gambs(scanline, xmax);
1010 +                if (dither)
1011 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
1012 +                else
1013 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
1014 +        }
1015 + }
1016 +
1017 +
1018   scale_rcolors(xr, sf)                   /* scale color map */
1019   register XRASTER        *xr;
1020   double  sf;
# Line 933 | Line 1046 | double sf;
1046   getscan(y)
1047   int  y;
1048   {
1049 +        static int  trunced = -1;               /* truncated file? */
1050 + skipit:
1051 +        if (trunced >= 0 && y >= trunced) {
1052 +                bzero(scanline, xmax*sizeof(COLR));
1053 +                return(-1);
1054 +        }
1055          if (y != cury) {
1056                  if (scanpos == NULL || scanpos[y] == -1)
1057                          return(-1);
1058                  if (fseek(fin, scanpos[y], 0) == -1)
1059                          quiterr("fseek error");
1060                  cury = y;
1061 <        } else if (scanpos != NULL)
1061 >        } else if (scanpos != NULL && scanpos[y] == -1)
1062                  scanpos[y] = ftell(fin);
1063  
1064 <        if (freadcolrs(scanline, xmax, fin) < 0)
1065 <                quiterr("read error");
1066 <
1064 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1065 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1066 >                                progname, fname==NULL?"<stdin>":fname);
1067 >                trunced = y;
1068 >                goto skipit;
1069 >        }
1070          cury++;
1071          return(0);
950 }
951
952
953 picreadline3(y, l3)                     /* read in 3-byte scanline */
954 int  y;
955 register rgbpixel  *l3;
956 {
957        register int    i;
958                                                        /* read scanline */
959        if (getscan(y) < 0)
960                quiterr("cannot seek for picreadline");
961                                                        /* convert scanline */
962        normcolrs(scanline, xmax, scale);
963        add2icon(y, scanline);
964        for (i = 0; i < xmax; i++) {
965                l3[i].r = scanline[i][RED];
966                l3[i].g = scanline[i][GRN];
967                l3[i].b = scanline[i][BLU];
968        }
969 }
970
971
972 picwriteline(y, l)              /* add 8-bit scanline to image */
973 int  y;
974 pixel  *l;
975 {
976        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
977 }
978
979
980 picreadcm(map)                  /* do gamma correction */
981 colormap  map;
982 {
983        extern double  pow();
984        register int  i, val;
985
986        for (i = 0; i < 256; i++) {
987                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
988                map[0][i] = map[1][i] = map[2][i] = val;
989        }
990 }
991
992
993 picwritecm(map)                 /* handled elsewhere */
994 colormap  map;
995 {
996 #if 0
997        register int i;
998
999        for (i = 0; i < 256; i++)
1000                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
1001 #endif
1072   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines