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

Comparing ray/src/px/x11image.c (file contents):
Revision 1.16 by greg, Wed May 1 12:40:00 1991 UTC vs.
Revision 2.28 by greg, Mon Jul 19 15:18:05 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 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 24 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24   #include  <X11/Xlib.h>
25   #include  <X11/cursorfont.h>
26   #include  <X11/Xutil.h>
27 + #include  <X11/Xatom.h>
28  
29   #include  "color.h"
30   #include  "view.h"
30 #include  "pic.h"
31   #include  "x11raster.h"
32   #include  "random.h"
33 < #include  "x11icon.h"
33 > #include  "resolu.h"
34  
35 + #ifdef  __alpha
36 + #define  int4           int
37 + #endif
38 + #ifndef  int4
39 + #define  int4           long
40 + #endif
41 +
42   #define  FONTNAME       "8x13"          /* text font we'll use */
43  
44 < #define  CTRL(c)        ('c'-'@')
44 > #define  CTRL(c)        ((c)-'@')
45  
46   #define  BORWIDTH       5               /* border width */
47  
48 + #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
49 +
50   #define  ourscreen      DefaultScreen(thedisplay)
42 #define  ourblack       BlackPixel(thedisplay,ourscreen)
43 #define  ourwhite       WhitePixel(thedisplay,ourscreen)
51   #define  ourroot        RootWindow(thedisplay,ourscreen)
45 #define  ourgc          DefaultGC(thedisplay,ourscreen)
52  
53   #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
54  
# Line 53 | Line 59 | double  gamcor = 2.2;                  /* gamma correction */
59   int  dither = 1;                        /* dither colors? */
60   int  fast = 0;                          /* keep picture in Pixmap? */
61  
62 < Window  wind = 0;                       /* our output window */
57 < Font  fontid;                           /* our font */
62 > char    *dispname = NULL;               /* our display name */
63  
64 + Window  wind = 0;                       /* our output window */
65 + unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
66   int  maxcolors = 0;                     /* maximum colors */
67   int  greyscale = 0;                     /* in grey */
68  
# Line 69 | Line 76 | int  gotview = 0;                      /* got parameters from file */
76  
77   COLR  *scanline;                        /* scan line buffer */
78  
79 < int  xmax, ymax;                        /* picture resolution */
79 > RESOLU  inpres;                         /* input resolution and ordering */
80 > int  xmax, ymax;                        /* picture dimensions */
81   int  width, height;                     /* window size */
82   char  *fname = NULL;                    /* input file name */
83   FILE  *fin = stdin;                     /* input file */
# Line 80 | Line 88 | double  exposure = 1.0;                        /* exposure compensation use
88  
89   int  wrongformat = 0;                   /* input in another format? */
90  
91 + GC      ourgc;                          /* standard graphics context */
92   GC      revgc;                          /* graphics context with GXinvert */
93  
94 < XRASTER *ourras;                        /* our stored image */
94 > int             *ourrank;               /* our visual class ranking */
95 > XVisualInfo     ourvis;                 /* our visual */
96 > XRASTER         *ourras;                /* our stored image */
97   unsigned char   *ourdata;               /* our image data */
98  
99   struct {
# Line 91 | Line 102 | struct {
102  
103   char  *geometry = NULL;                 /* geometry specification */
104  
105 + char  icondata[ICONSIZ*ICONSIZ/8];      /* icon bitmap data */
106 + int  iconwidth = 0, iconheight = 0;
107 +
108   char  *progname;
109  
110   char  errmsg[128];
111  
112 + extern BYTE  clrtab[256][3];            /* global color map */
113 +
114   extern long  ftell();
115  
100 extern char  *malloc(), *calloc();
101
102 extern double  atof(), pow(), log();
103
116   Display  *thedisplay;
117 + Atom  closedownAtom, wmProtocolsAtom;
118  
119 +
120   main(argc, argv)
121   int  argc;
122   char  *argv[];
123   {
124 +        extern char  *getenv();
125 +        char  *gv;
126          int  headline();
127          int  i;
128          
129          progname = argv[0];
130 +        if ((gv = getenv("GAMMA")) != NULL)
131 +                gamcor = atof(gv);
132  
133          for (i = 1; i < argc; i++)
134                  if (argv[i][0] == '-')
# Line 125 | Line 143 | char  *argv[];
143                                  maxcolors = 2;
144                                  break;
145                          case 'd':
146 <                                dither = !dither;
146 >                                if (argv[i][2] == 'i')
147 >                                        dispname = argv[++i];
148 >                                else
149 >                                        dither = !dither;
150                                  break;
151                          case 'f':
152                                  fast = !fast;
# Line 136 | Line 157 | char  *argv[];
157                                  scale = atoi(argv[++i]);
158                                  break;
159                          case 'g':
160 <                                if (!strcmp(argv[i], "-geometry"))
160 >                                if (argv[i][2] == 'e')
161                                          geometry = argv[++i];
162                                  else
163                                          gamcor = atof(argv[++i]);
# Line 153 | Line 174 | char  *argv[];
174                  fname = argv[i];
175                  fin = fopen(fname, "r");
176                  if (fin == NULL) {
177 <                        sprintf(errmsg, "can't open file \"%s\"", fname);
177 >                        sprintf(errmsg, "cannot open file \"%s\"", fname);
178                          quiterr(errmsg);
179                  }
180          } else if (i != argc)
# Line 161 | Line 182 | char  *argv[];
182                                  /* get header */
183          getheader(fin, headline, NULL);
184                                  /* get picture dimensions */
185 <        if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
185 >        if (wrongformat || !fgetsresolu(&inpres, fin))
186                  quiterr("bad picture format");
187 +        xmax = scanlen(&inpres);
188 +        ymax = numscans(&inpres);
189                                  /* set view parameters */
190          if (gotview && setview(&ourview) != NULL)
191                  gotview = 0;
192          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
193                  quiterr("out of memory");
194  
195 <        init();                 /* get file and open window */
195 >        init(argc, argv);                       /* get file and open window */
196  
197          for ( ; ; )
198                  getevent();             /* main loop */
199   userr:
200          fprintf(stderr,
201 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
201 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n",
202                          progname);
203 <        quit(1);
203 >        exit(1);
204   }
205  
206  
207   headline(s)             /* get relevant info from header */
208   char  *s;
209   {
187        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
188        register char  **an;
210          char  fmt[32];
211  
212          if (isexpos(s))
# Line 193 | Line 214 | char  *s;
214          else if (isformat(s)) {
215                  formatval(fmt, s);
216                  wrongformat = strcmp(fmt, COLRFMT);
217 <        } else
218 <                for (an = altname; *an != NULL; an++)
198 <                        if (!strncmp(*an, s, strlen(*an))) {
199 <                                if (sscanview(&ourview, s+strlen(*an)) > 0)
200 <                                        gotview++;
201 <                                return;
202 <                        }
217 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
218 >                gotview++;
219   }
220  
221  
222 < init()                  /* get data and open window */
222 > init(argc, argv)                        /* get data and open window */
223 > int argc;
224 > char **argv;
225   {
208        XWMHints        ourxwmhints;
226          XSetWindowAttributes    ourwinattr;
227 <        XSizeHints  oursizhints;
228 <        register int  i;
227 >        XClassHint      xclshints;
228 >        XWMHints        xwmhints;
229 >        XSizeHints      xszhints;
230 >        XTextProperty   windowName, iconName;
231 >        XGCValues       xgcv;
232 >        char    *name;
233 >        register int    i;
234          
235          if (fname != NULL) {
236                  scanpos = (long *)malloc(ymax*sizeof(long));
237                  if (scanpos == NULL)
238 <                        goto memerr;
238 >                        quiterr("out of memory");
239                  for (i = 0; i < ymax; i++)
240                          scanpos[i] = -1;
241 <        }
242 <        if ((thedisplay = XOpenDisplay(NULL)) == NULL)
243 <                quiterr("can't open display; DISPLAY variable set?");
244 <        if (maxcolors == 0) {           /* get number of available colors */
245 <                i = DisplayPlanes(thedisplay,ourscreen);
246 <                maxcolors = i > 8 ? 256 : 1<<i;
247 <                if (maxcolors > 4) maxcolors -= 2;
248 <        }
241 >                name = fname;
242 >        } else
243 >                name = progname;
244 >                                /* remove directory prefix from name */
245 >        for (i = strlen(name); i-- > 0; )
246 >                if (name[i] == '/')
247 >                        break;
248 >        name += i+1;
249 >        if ((thedisplay = XOpenDisplay(dispname)) == NULL)
250 >                quiterr("cannot open display");
251 >                                /* get best visual for default screen */
252 >        getbestvis();
253                                  /* store image */
254          getras();
255 <                                /* open window */
256 <        ourwinattr.border_pixel = ourblack;
257 <        ourwinattr.background_pixel = ourwhite;
232 <        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
233 <                        0, InputOutput, ourras->visual,
234 <                        CWBackPixel|CWBorderPixel, &ourwinattr);
235 <        if (wind == 0)
236 <                quiterr("can't create window");
237 <        width = xmax;
238 <        height = ymax;
239 <        fontid = XLoadFont(thedisplay, FONTNAME);
240 <        if (fontid == 0)
241 <                quiterr("can't get font");
242 <        XSetFont(thedisplay, ourgc, fontid);
243 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
244 <        XSetFunction(thedisplay, revgc, GXinvert);
245 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
246 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
247 <                        XC_diamond_cross));
255 >                                /* get size and position */
256 >        xszhints.flags = 0;
257 >        xszhints.width = xmax; xszhints.height = ymax;
258          if (geometry != NULL) {
259 <                bzero((char *)&oursizhints, sizeof(oursizhints));
260 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
261 <                                (unsigned *)&oursizhints.width,
252 <                                (unsigned *)&oursizhints.height);
259 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
260 >                                (unsigned *)&xszhints.width,
261 >                                (unsigned *)&xszhints.height);
262                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
263 <                        oursizhints.flags |= USSize;
264 <                else {
265 <                        oursizhints.width = xmax;
257 <                        oursizhints.height = ymax;
258 <                        oursizhints.flags |= PSize;
259 <                }
263 >                        xszhints.flags |= USSize;
264 >                else
265 >                        xszhints.flags |= PSize;
266                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
267 <                        oursizhints.flags |= USPosition;
267 >                        xszhints.flags |= USPosition;
268                          if (i & XNegative)
269 <                                oursizhints.x += DisplayWidth(thedisplay,
270 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
269 >                                xszhints.x += DisplayWidth(thedisplay,
270 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
271                          if (i & YNegative)
272 <                                oursizhints.y += DisplayHeight(thedisplay,
273 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
272 >                                xszhints.y += DisplayHeight(thedisplay,
273 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
274                  }
269                XSetNormalHints(thedisplay, wind, &oursizhints);
275          }
276 <        ourxwmhints.flags = InputHint|IconPixmapHint;
277 <        ourxwmhints.input = True;
278 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
279 <                        wind, x11icon_bits, x11icon_width, x11icon_height);
280 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
281 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
282 <                        |ButtonMotionMask|StructureNotifyMask
283 <                        |KeyPressMask|ExposureMask);
276 >        /* open window */
277 >        ourwinattr.border_pixel = ourwhite;
278 >        ourwinattr.background_pixel = ourblack;
279 >        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
280 >                        ourvis.visual, AllocNone);
281 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
282 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
283 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
284 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
285 >                        xszhints.width, xszhints.height, BORWIDTH,
286 >                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
287 >                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
288 >        if (wind == 0)
289 >                quiterr("cannot create window");
290 >        width = xmax;
291 >        height = ymax;
292 >        xgcv.foreground = ourblack;
293 >        xgcv.background = ourwhite;
294 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
295 >                quiterr("cannot get font");
296 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
297 >                        GCFont, &xgcv);
298 >        xgcv.function = GXinvert;
299 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
300 >                        GCFunction, &xgcv);
301 >
302 >        /* set up the window manager */
303 >        xwmhints.flags = InputHint|IconPixmapHint;
304 >        xwmhints.input = True;
305 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
306 >                        wind, icondata, iconwidth, iconheight);
307 >
308 >        windowName.encoding = iconName.encoding = XA_STRING;
309 >        windowName.format = iconName.format = 8;
310 >        windowName.value = (u_char *)name;
311 >        windowName.nitems = strlen(windowName.value);
312 >        iconName.value = (u_char *)name;
313 >        iconName.nitems = strlen(windowName.value);
314 >
315 >        xclshints.res_name = NULL;
316 >        xclshints.res_class = "Ximage";
317 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
318 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
319 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
320 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
321 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
322 >
323          XMapWindow(thedisplay, wind);
324          return;
281 memerr:
282        quiterr("out of memory");
325   } /* end of init */
326  
327  
# Line 294 | Line 336 | char  *err;
336   }
337  
338  
339 < eputs(s)
340 < char    *s;
339 > static int
340 > viscmp(v1,v2)           /* compare visual to see which is better, descending */
341 > register XVisualInfo    *v1, *v2;
342   {
343 <        fputs(s, stderr);
343 >        int     bad1 = 0, bad2 = 0;
344 >        register int  *rp;
345 >
346 >        if (v1->class == v2->class) {
347 >                if (v1->class == TrueColor || v1->class == DirectColor) {
348 >                                        /* prefer 24-bit to 32-bit */
349 >                        if (v1->depth == 24 && v2->depth == 32)
350 >                                return(-1);
351 >                        if (v1->depth == 32 && v2->depth == 24)
352 >                                return(1);
353 >                        return(0);
354 >                }
355 >                                        /* don't be too greedy */
356 >                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
357 >                        return(v1->depth - v2->depth);
358 >                return(v2->depth - v1->depth);
359 >        }
360 >                                        /* prefer Pseudo when < 24-bit */
361 >        if ((v1->class == TrueColor || v1->class == DirectColor) &&
362 >                        v1->depth < 24)
363 >                bad1 = 1;
364 >        if ((v2->class == TrueColor || v2->class == DirectColor) &&
365 >                        v2->depth < 24)
366 >                bad2 = -1;
367 >        if (bad1 | bad2)
368 >                return(bad1+bad2);
369 >                                        /* otherwise, use class ranking */
370 >        for (rp = ourrank; *rp != -1; rp++) {
371 >                if (v1->class == *rp)
372 >                        return(-1);
373 >                if (v2->class == *rp)
374 >                        return(1);
375 >        }
376 >        return(0);
377   }
378  
379  
380 < quit(code)
305 < int  code;
380 > getbestvis()                    /* get the best visual for this screen */
381   {
382 <        exit(code);
382 > #ifdef DEBUG
383 > static char  vistype[][12] = {
384 >                "StaticGray",
385 >                "GrayScale",
386 >                "StaticColor",
387 >                "PseudoColor",
388 >                "TrueColor",
389 >                "DirectColor"
390 > };
391 > #endif
392 >        static int      rankings[3][6] = {
393 >                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
394 >                {PseudoColor,GrayScale,StaticGray,-1},
395 >                {PseudoColor,GrayScale,StaticGray,-1}
396 >        };
397 >        XVisualInfo     *xvi;
398 >        int     vismatched;
399 >        register int    i, j;
400 >
401 >        if (greyscale) {
402 >                ourrank = rankings[2];
403 >                if (maxcolors < 2) maxcolors = 256;
404 >        } else if (maxcolors >= 2 && maxcolors <= 256)
405 >                ourrank = rankings[1];
406 >        else {
407 >                ourrank = rankings[0];
408 >                maxcolors = 256;
409 >        }
410 >                                        /* find best visual */
411 >        ourvis.screen = ourscreen;
412 >        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
413 >        if (xvi == NULL)
414 >                quiterr("no visuals for this screen!");
415 > #ifdef DEBUG
416 >        fprintf(stderr, "Supported visuals:\n");
417 >        for (i = 0; i < vismatched; i++)
418 >                fprintf(stderr, "\ttype %s, depth %d\n",
419 >                                vistype[xvi[i].class], xvi[i].depth);
420 > #endif
421 >        for (i = 0, j = 1; j < vismatched; j++)
422 >                if (viscmp(&xvi[i],&xvi[j]) > 0)
423 >                        i = j;
424 >                                        /* compare to least acceptable */
425 >        for (j = 0; ourrank[j++] != -1; )
426 >                ;
427 >        ourvis.class = ourrank[--j];
428 >        ourvis.depth = 1;
429 >        if (viscmp(&xvi[i],&ourvis) > 0)
430 >                quiterr("inadequate visuals on this screen");
431 >                                        /* OK, we'll use it */
432 >        copystruct(&ourvis, &xvi[i]);
433 > #ifdef DEBUG
434 >        fprintf(stderr, "Selected visual type %s, depth %d\n",
435 >                        vistype[ourvis.class], ourvis.depth);
436 > #endif
437 >                                        /* make appropriate adjustments */
438 >        if (ourvis.class == GrayScale || ourvis.class == StaticGray)
439 >                greyscale = 1;
440 >        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
441 >                maxcolors = ourvis.colormap_size;
442 >        if (ourvis.class == StaticGray) {
443 >                ourblack = 0;
444 >                ourwhite = 255;
445 >        } else if (ourvis.class == PseudoColor) {
446 >                ourblack = BlackPixel(thedisplay,ourscreen);
447 >                ourwhite = WhitePixel(thedisplay,ourscreen);
448 >                if ((ourblack|ourwhite) & ~255L) {
449 >                        ourblack = 0;
450 >                        ourwhite = 1;
451 >                }
452 >                if (maxcolors > 4)
453 >                        maxcolors -= 2;
454 >        } else {
455 >                ourblack = 0;
456 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
457 >        }
458 >        XFree((char *)xvi);
459   }
460  
461  
462   getras()                                /* get raster file */
463   {
313        colormap        ourmap;
464          XVisualInfo     vinfo;
465  
466          if (maxcolors <= 2) {           /* monochrome */
467                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
468                  if (ourdata == NULL)
469                          goto fail;
470 <                ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
470 >                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
471                                  xmax, ymax, 8);
472                  if (ourras == NULL)
473                          goto fail;
474                  getmono();
475 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)
476 <                                                /* kludge for DirectColor */
327 <        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) {
328 <                ourdata = (unsigned char *)malloc(xmax*ymax*3);
475 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
476 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
477                  if (ourdata == NULL)
478                          goto fail;
479 <                ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
480 <                                xmax, ymax, 8);
479 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
480 >                                ourdata, xmax, ymax, 32);
481                  if (ourras == NULL)
482                          goto fail;
483                  getfull();
# Line 337 | Line 485 | getras()                               /* get raster file */
485                  ourdata = (unsigned char *)malloc(xmax*ymax);
486                  if (ourdata == NULL)
487                          goto fail;
488 <                ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
488 >                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
489                                  xmax, ymax, 8);
490                  if (ourras == NULL)
491                          goto fail;
492 <                if (greyscale)
493 <                        biq(dither,maxcolors,1,ourmap);
492 >                if (greyscale | ourvis.class == StaticGray)
493 >                        getgrey();
494                  else
495 <                        ciq(dither,maxcolors,1,ourmap);
496 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
495 >                        getmapped();
496 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
497                          goto fail;
498          }
499          return;
# Line 356 | Line 504 | fail:
504  
505   getevent()                              /* process the next event */
506   {
507 <        union {
360 <                XEvent  u;
361 <                XConfigureEvent  c;
362 <                XExposeEvent  e;
363 <                XButtonPressedEvent  b;
364 <                XKeyPressedEvent  k;
365 <        } e;
507 >        XEvent xev;
508  
509 <        XNextEvent(thedisplay, &e.u);
510 <        switch (e.u.type) {
509 >        XNextEvent(thedisplay, &xev);
510 >        switch ((int)xev.type) {
511          case KeyPress:
512 <                docom(&e.k);
512 >                docom(&xev.xkey);
513                  break;
514          case ConfigureNotify:
515 <                width = e.c.width;
516 <                height = e.c.height;
515 >                width = xev.xconfigure.width;
516 >                height = xev.xconfigure.height;
517                  break;
518          case MapNotify:
519                  map_rcolors(ourras, wind);
520                  if (fast)
521 <                        make_rpixmap(ourras);
521 >                        make_rpixmap(ourras, wind);
522                  break;
523          case UnmapNotify:
524 <                unmap_rcolors(ourras);
524 >                if (!fast)
525 >                        unmap_rcolors(ourras);
526                  break;
527          case Expose:
528 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
528 >                redraw(xev.xexpose.x, xev.xexpose.y,
529 >                                xev.xexpose.width, xev.xexpose.height);
530                  break;
531          case ButtonPress:
532 <                if (e.b.state & (ShiftMask|ControlMask))
533 <                        moveimage(&e.b);
532 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
533 >                        moveimage(&xev.xbutton);
534 >                else if (xev.xbutton.button == Button2)
535 >                        traceray(xev.xbutton.x, xev.xbutton.y);
536                  else
537 <                        getbox(&e.b);
537 >                        getbox(&xev.xbutton);
538                  break;
539 +   case ClientMessage:
540 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
541 +                                (xev.xclient.data.l[0] == closedownAtom))
542 +                        quiterr(NULL);
543 +                break;
544          }
545   }
546  
547  
548 < docom(ekey)                                     /* execute command */
548 > traceray(xpos, ypos)                    /* print ray corresponding to pixel */
549 > int  xpos, ypos;
550 > {
551 >        FLOAT  hv[2];
552 >        FVECT  rorg, rdir;
553 >
554 >        if (!gotview) {         /* no view, no can do */
555 >                XBell(thedisplay, 0);
556 >                return(-1);
557 >        }
558 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
559 >        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
560 >                return(-1);
561 >        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
562 >        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
563 >        fflush(stdout);
564 >        return(0);
565 > }
566 >
567 >
568 > docom(ekey)                             /* execute command */
569   XKeyPressedEvent  *ekey;
570   {
571          char  buf[80];
# Line 402 | Line 573 | XKeyPressedEvent  *ekey;
573          XColor  cvx;
574          int  com, n;
575          double  comp;
576 <        FVECT  rorg, rdir;
576 >        FLOAT  hv[2];
577  
578          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
579          if (n == 0)
# Line 410 | Line 581 | XKeyPressedEvent  *ekey;
581          com = buf[0];
582          switch (com) {                  /* interpret command */
583          case 'q':
584 <        case CTRL(D):                           /* quit */
585 <                quit(0);
584 >        case 'Q':
585 >        case CTRL('D'):                         /* quit */
586 >                quiterr(NULL);
587          case '\n':
588          case '\r':
589          case 'l':
# Line 450 | Line 622 | XKeyPressedEvent  *ekey;
622                  XStoreColor(thedisplay, ourras->cmap, &cvx);
623                  return(0);
624          case 'p':                               /* position */
625 <                sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
625 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
626 >                sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
627 >                                (int)(hv[1]*inpres.yr));
628                  XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
629                                          buf, strlen(buf));
630                  return(0);
631          case 't':                               /* trace */
632 <                if (!gotview) {
459 <                        XBell(thedisplay, 0);
460 <                        return(-1);
461 <                }
462 <                if (viewray(rorg, rdir, &ourview,
463 <                                (ekey->x-xoff+.5)/xmax,
464 <                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
465 <                        return(-1);
466 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
467 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
468 <                fflush(stdout);
469 <                return(0);
632 >                return(traceray(ekey->x, ekey->y));
633          case '=':                               /* adjust exposure */
634 +        case '@':                               /* adaptation level */
635                  if (avgbox(cval) == -1)
636                          return(-1);
637 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
637 >                comp = com=='@'
638 >                ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure
639 >                : .5/bright(cval) ;
640 >                comp = log(comp)/.69315 - scale;
641 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
642                  if (n == 0)
643                          return(0);
644                  scale_rcolors(ourras, pow(2.0, (double)n));
# Line 483 | Line 651 | XKeyPressedEvent  *ekey;
651                  free_raster(ourras);
652                  getras();
653          /* fall through */
654 <        case CTRL(R):                           /* redraw */
655 <        case CTRL(L):
654 >        case CTRL('R'):                         /* redraw */
655 >        case CTRL('L'):
656                  unmap_rcolors(ourras);
657                  XClearWindow(thedisplay, wind);
658                  map_rcolors(ourras, wind);
659                  if (fast)
660 <                        make_rpixmap(ourras);
660 >                        make_rpixmap(ourras, wind);
661                  redraw(0, 0, width, height);
662                  return(0);
663 +        case 'f':                               /* turn on fast redraw */
664 +                fast = 1;
665 +                make_rpixmap(ourras, wind);
666 +                return(0);
667 +        case 'F':                               /* turn off fast redraw */
668 +                fast = 0;
669 +                free_rpixmap(ourras);
670 +                return(0);
671 +        case '0':                               /* recenter origin */
672 +                if (xoff == 0 & yoff == 0)
673 +                        return(0);
674 +                xoff = yoff = 0;
675 +                XClearWindow(thedisplay, wind);
676 +                redraw(0, 0, width, height);
677 +                return(0);
678          case ' ':                               /* clear */
679                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
680                  return(0);
# Line 505 | Line 688 | XKeyPressedEvent  *ekey;
688   moveimage(ebut)                         /* shift the image */
689   XButtonPressedEvent  *ebut;
690   {
691 <        union {
509 <                XEvent  u;
510 <                XButtonReleasedEvent  b;
511 <                XPointerMovedEvent  m;
512 <        }  e;
691 >        XEvent  e;
692          int     mxo, myo;
693  
694 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
695 <        while (e.u.type == MotionNotify) {
696 <                mxo = e.m.x;
697 <                myo = e.m.y;
694 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
695 >        while (e.type == MotionNotify) {
696 >                mxo = e.xmotion.x;
697 >                myo = e.xmotion.y;
698                  revline(ebut->x, ebut->y, mxo, myo);
699                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
700                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
701 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
701 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
702                  revline(ebut->x, ebut->y, mxo, myo);
703                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
704                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
705          }
706 <        xoff += e.b.x - ebut->x;
707 <        yoff += e.b.y - ebut->y;
706 >        xoff += e.xbutton.x - ebut->x;
707 >        yoff += e.xbutton.y - ebut->y;
708          XClearWindow(thedisplay, wind);
709          redraw(0, 0, width, height);
710   }
# Line 534 | Line 713 | XButtonPressedEvent  *ebut;
713   getbox(ebut)                            /* get new box */
714   XButtonPressedEvent  *ebut;
715   {
716 <        union {
538 <                XEvent  u;
539 <                XButtonReleasedEvent  b;
540 <                XPointerMovedEvent  m;
541 <        }  e;
716 >        XEvent  e;
717  
718 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
719 <        while (e.u.type == MotionNotify) {
720 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
721 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
718 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
719 >        while (e.type == MotionNotify) {
720 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
721 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
722                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
723          }
724 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
725 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
724 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
725 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
726          if (box.xmin > ebut->x) {
727                  box.xsiz = box.xmin - ebut->x + 1;
728                  box.xmin = ebut->x;
# Line 576 | Line 751 | int  x0, y0, x1, y1;
751   avgbox(clr)                             /* average color over current box */
752   COLOR  clr;
753   {
754 +        static COLOR  lc;
755 +        static int  ll, lr, lt, lb;
756          int  left, right, top, bottom;
757          int  y;
758          double  d;
# Line 599 | Line 776 | COLOR  clr;
776                  bottom = ymax;
777          if (top >= bottom)
778                  return(-1);
779 +        if (left == ll && right == lr && top == lt && bottom == lb) {
780 +                copycolor(clr, lc);
781 +                return(0);
782 +        }
783          for (y = top; y < bottom; y++) {
784                  if (getscan(y) == -1)
785                          return(-1);
# Line 609 | Line 790 | COLOR  clr;
790          }
791          d = 1.0/((right-left)*(bottom-top));
792          scalecolor(clr, d);
793 +        ll = left; lr = right; lt = top; lb = bottom;
794 +        copycolor(lc, clr);
795          return(0);
796   }
797  
# Line 617 | Line 800 | getmono()                      /* get monochrome data */
800   {
801          register unsigned char  *dp;
802          register int    x, err;
803 <        int     y;
803 >        int     y, errp;
804          short   *cerr;
805  
806          if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
807                  quiterr("out of memory in getmono");
808          dp = ourdata - 1;
809          for (y = 0; y < ymax; y++) {
810 <                if (getscan(y) < 0)
811 <                        quiterr("seek error in getmono");
810 >                getscan(y);
811 >                add2icon(y, scanline);
812                  normcolrs(scanline, xmax, scale);
813                  err = 0;
814                  for (x = 0; x < xmax; x++) {
815                          if (!(x&7))
816                                  *++dp = 0;
817 +                        errp = err;
818                          err += normbright(scanline[x]) + cerr[x];
819                          if (err > 127)
820                                  err -= 255;
821                          else
822                                  *dp |= 1<<(7-(x&07));
823 <                        cerr[x] = err >>= 1;
823 >                        err /= 3;
824 >                        cerr[x] = err + errp;
825                  }
826          }
827          free((char *)cerr);
828   }
829  
830  
831 + add2icon(y, scan)               /* add a scanline to our icon data */
832 + int  y;
833 + COLR  *scan;
834 + {
835 +        static short  cerr[ICONSIZ];
836 +        static int  ynext;
837 +        static char  *dp;
838 +        COLR  clr;
839 +        register int  err;
840 +        register int    x, ti;
841 +        int  errp;
842 +
843 +        if (iconheight == 0) {          /* initialize */
844 +                if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
845 +                        iconwidth = xmax;
846 +                        iconheight = ymax;
847 +                } else if (xmax > ymax) {
848 +                        iconwidth = ICONSIZ;
849 +                        iconheight = ICONSIZ*ymax/xmax;
850 +                        if (iconheight < 1)
851 +                                iconheight = 1;
852 +                } else {
853 +                        iconwidth = ICONSIZ*xmax/ymax;
854 +                        if (iconwidth < 1)
855 +                                iconwidth = 1;
856 +                        iconheight = ICONSIZ;
857 +                }
858 +                ynext = 0;
859 +                dp = icondata - 1;
860 +        }
861 +        if (y < ynext*ymax/iconheight)  /* skip this one */
862 +                return;
863 +        err = 0;
864 +        for (x = 0; x < iconwidth; x++) {
865 +                if (!(x&7))
866 +                        *++dp = 0;
867 +                errp = err;
868 +                ti = x*xmax/iconwidth;
869 +                copycolr(clr, scan[ti]);
870 +                normcolrs(clr, 1, scale);
871 +                err += normbright(clr) + cerr[x];
872 +                if (err > 127)
873 +                        err -= 255;
874 +                else
875 +                        *dp |= 1<<(x&07);
876 +                err /= 3;
877 +                cerr[x] = err + errp;
878 +        }
879 +        ynext++;
880 + }
881 +
882 +
883   getfull()                       /* get full (24-bit) data */
884   {
885          int     y;
886 +        register unsigned int4  *dp;
887 +        register int    x;
888 +                                        /* set gamma correction */
889 +        setcolrgam(gamcor);
890 +                                        /* read and convert file */
891 +        dp = (unsigned int4 *)ourdata;
892 +        for (y = 0; y < ymax; y++) {
893 +                getscan(y);
894 +                add2icon(y, scanline);
895 +                if (scale)
896 +                        shiftcolrs(scanline, xmax, scale);
897 +                colrs_gambs(scanline, xmax);
898 +                if (ourras->image->blue_mask & 1)
899 +                        for (x = 0; x < xmax; x++)
900 +                                *dp++ = scanline[x][RED] << 16 |
901 +                                        scanline[x][GRN] << 8 |
902 +                                        scanline[x][BLU] ;
903 +                else
904 +                        for (x = 0; x < xmax; x++)
905 +                                *dp++ = scanline[x][RED] |
906 +                                        scanline[x][GRN] << 8 |
907 +                                        scanline[x][BLU] << 16 ;
908 +        }
909 + }
910 +
911 +
912 + getgrey()                       /* get greyscale data */
913 + {
914 +        int     y;
915          register unsigned char  *dp;
916          register int    x;
917                                          /* set gamma correction */
# Line 653 | Line 919 | getfull()                      /* get full (24-bit) data */
919                                          /* read and convert file */
920          dp = ourdata;
921          for (y = 0; y < ymax; y++) {
922 +                getscan(y);
923 +                add2icon(y, scanline);
924 +                if (scale)
925 +                        shiftcolrs(scanline, xmax, scale);
926 +                for (x = 0; x < xmax; x++)
927 +                        scanline[x][GRN] = normbright(scanline[x]);
928 +                colrs_gambs(scanline, xmax);
929 +                if (maxcolors < 256)
930 +                        for (x = 0; x < xmax; x++)
931 +                                *dp++ = ((long)scanline[x][GRN] *
932 +                                        maxcolors + maxcolors/2) >> 8;
933 +                else
934 +                        for (x = 0; x < xmax; x++)
935 +                                *dp++ = scanline[x][GRN];
936 +        }
937 +        for (x = 0; x < maxcolors; x++)
938 +                clrtab[x][RED] = clrtab[x][GRN] =
939 +                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
940 + }
941 +
942 +
943 + getmapped()                     /* get color-mapped data */
944 + {
945 +        int     y;
946 +                                        /* set gamma correction */
947 +        setcolrgam(gamcor);
948 +                                        /* make histogram */
949 +        new_histo();
950 +        for (y = 0; y < ymax; y++) {
951                  if (getscan(y) < 0)
952 <                        quiterr("seek error in getfull");
952 >                        quiterr("seek error in getmapped");
953 >                add2icon(y, scanline);
954                  if (scale)
955                          shiftcolrs(scanline, xmax, scale);
956                  colrs_gambs(scanline, xmax);
957 <                for (x = 0; x < xmax; x++) {
662 <                        *dp++ = scanline[x][RED];
663 <                        *dp++ = scanline[x][GRN];
664 <                        *dp++ = scanline[x][BLU];
665 <                }
957 >                cnt_colrs(scanline, xmax);
958          }
959 +                                        /* map pixels */
960 +        if (!new_clrtab(maxcolors))
961 +                quiterr("cannot create color map");
962 +        for (y = 0; y < ymax; y++) {
963 +                if (getscan(y) < 0)
964 +                        quiterr("seek error in getmapped");
965 +                if (scale)
966 +                        shiftcolrs(scanline, xmax, scale);
967 +                colrs_gambs(scanline, xmax);
968 +                if (dither)
969 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
970 +                else
971 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
972 +        }
973   }
974  
975  
# Line 704 | Line 1010 | int  y;
1010                  if (fseek(fin, scanpos[y], 0) == -1)
1011                          quiterr("fseek error");
1012                  cury = y;
1013 <        } else if (scanpos != NULL)
1013 >        } else if (scanpos != NULL && scanpos[y] == -1)
1014                  scanpos[y] = ftell(fin);
1015  
1016          if (freadcolrs(scanline, xmax, fin) < 0)
# Line 712 | Line 1018 | int  y;
1018  
1019          cury++;
1020          return(0);
715 }
716
717
718 picreadline3(y, l3)                     /* read in 3-byte scanline */
719 int  y;
720 register rgbpixel  *l3;
721 {
722        register int    i;
723                                                        /* read scanline */
724        if (getscan(y) < 0)
725                quiterr("cannot seek for picreadline");
726                                                        /* convert scanline */
727        normcolrs(scanline, xmax, scale);
728        for (i = 0; i < xmax; i++) {
729                l3[i].r = scanline[i][RED];
730                l3[i].g = scanline[i][GRN];
731                l3[i].b = scanline[i][BLU];
732        }
733 }
734
735
736 picwriteline(y, l)              /* add 8-bit scanline to image */
737 int  y;
738 pixel  *l;
739 {
740        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
741 }
742
743
744 picreadcm(map)                  /* do gamma correction */
745 colormap  map;
746 {
747        extern double  pow();
748        register int  i, val;
749
750        for (i = 0; i < 256; i++) {
751                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
752                map[0][i] = map[1][i] = map[2][i] = val;
753        }
754 }
755
756
757 picwritecm(map)                 /* handled elsewhere */
758 colormap  map;
759 {
760 #ifdef DEBUG
761        register int i;
762
763        for (i = 0; i < 256; i++)
764                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
765 #endif
1021   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines