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

Comparing ray/src/px/x11image.c (file contents):
Revision 1.1 by greg, Thu Mar 1 13:18:22 1990 UTC vs.
Revision 2.16 by greg, Thu Oct 22 17:51:05 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 27 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27  
28   #include  "color.h"
29   #include  "view.h"
30 #include  "pic.h"
30   #include  "x11raster.h"
31   #include  "random.h"
32 + #include  "resolu.h"
33  
34   #define  FONTNAME       "8x13"          /* text font we'll use */
35  
36 < #define  CTRL(c)        ('c'-'@')
36 > #define  CTRL(c)        ((c)-'@')
37  
38   #define  BORWIDTH       5               /* border width */
39  
40 + #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
41 +
42   #define  ourscreen      DefaultScreen(thedisplay)
41 #define  ourblack       BlackPixel(thedisplay,ourscreen)
42 #define  ourwhite       WhitePixel(thedisplay,ourscreen)
43   #define  ourroot        RootWindow(thedisplay,ourscreen)
44 #define  ourgc          DefaultGC(thedisplay,ourscreen)
44  
45 + #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
46 +
47 + #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
48 +
49   double  gamcor = 2.2;                   /* gamma correction */
50  
51   int  dither = 1;                        /* dither colors? */
52   int  fast = 0;                          /* keep picture in Pixmap? */
53  
54 + char    *dispname = NULL;               /* our display name */
55 +
56   Window  wind = 0;                       /* our output window */
57 + unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
58   Font  fontid;                           /* our font */
59  
60 < long  maxcolors = 0;                    /* maximum colors */
60 > int  maxcolors = 0;                     /* maximum colors */
61   int  greyscale = 0;                     /* in grey */
62  
63   int  scale = 0;                         /* scalefactor; power of two */
# Line 64 | Line 70 | int  gotview = 0;                      /* got parameters from file */
70  
71   COLR  *scanline;                        /* scan line buffer */
72  
73 < int  xmax, ymax;                        /* picture resolution */
73 > RESOLU  inpres;                         /* input resolution and ordering */
74 > int  xmax, ymax;                        /* picture dimensions */
75   int  width, height;                     /* window size */
76   char  *fname = NULL;                    /* input file name */
77   FILE  *fin = stdin;                     /* input file */
# Line 73 | Line 80 | int  cury = 0;                         /* current scan location */
80  
81   double  exposure = 1.0;                 /* exposure compensation used */
82  
83 < XRASTER *ourras;                        /* our stored image */
83 > int  wrongformat = 0;                   /* input in another format? */
84 >
85 > GC      ourgc;                          /* standard graphics context */
86 > GC      revgc;                          /* graphics context with GXinvert */
87 >
88 > int             *ourrank;               /* our visual class ranking */
89 > XVisualInfo     ourvis;                 /* our visual */
90 > XRASTER         *ourras;                /* our stored image */
91   unsigned char   *ourdata;               /* our image data */
92  
93   struct {
# Line 82 | Line 96 | struct {
96  
97   char  *geometry = NULL;                 /* geometry specification */
98  
99 + char  icondata[ICONSIZ*ICONSIZ/8];      /* icon bitmap data */
100 + int  iconwidth = 0, iconheight = 0;
101 +
102   char  *progname;
103  
104   char  errmsg[128];
105  
106 + extern BYTE  clrtab[256][3];            /* global color map */
107 +
108   extern long  ftell();
109  
110   extern char  *malloc(), *calloc();
111  
93 extern double  atof(), pow(), log();
94
112   Display  *thedisplay;
113  
114 +
115   main(argc, argv)
116   int  argc;
117   char  *argv[];
118   {
119 +        extern char  *getenv();
120 +        char  *gv;
121          int  headline();
122          int  i;
123          
124          progname = argv[0];
125 +        if ((gv = getenv("GAMMA")) != NULL)
126 +                gamcor = atof(gv);
127  
128          for (i = 1; i < argc; i++)
129                  if (argv[i][0] == '-')
# Line 116 | Line 138 | char  *argv[];
138                                  maxcolors = 2;
139                                  break;
140                          case 'd':
141 <                                dither = !dither;
141 >                                if (argv[i][2] == 'i')
142 >                                        dispname = argv[++i];
143 >                                else
144 >                                        dither = !dither;
145                                  break;
146                          case 'f':
147                                  fast = !fast;
# Line 127 | Line 152 | char  *argv[];
152                                  scale = atoi(argv[++i]);
153                                  break;
154                          case 'g':
155 <                                if (!strcmp(argv[i], "-geometry"))
155 >                                if (argv[i][2] == 'e')
156                                          geometry = argv[++i];
157                                  else
158                                          gamcor = atof(argv[++i]);
# Line 135 | Line 160 | char  *argv[];
160                          default:
161                                  goto userr;
162                          }
163 +                else if (argv[i][0] == '=')
164 +                        geometry = argv[i];
165                  else
166                          break;
167  
168 <        if (argc-i == 1) {
168 >        if (i == argc-1) {
169                  fname = argv[i];
170                  fin = fopen(fname, "r");
171                  if (fin == NULL) {
172 <                        sprintf(errmsg, "can't open file \"%s\"", fname);
172 >                        sprintf(errmsg, "cannot open file \"%s\"", fname);
173                          quiterr(errmsg);
174                  }
175 <        }
175 >        } else if (i != argc)
176 >                goto userr;
177                                  /* get header */
178 <        getheader(fin, headline);
178 >        getheader(fin, headline, NULL);
179                                  /* get picture dimensions */
180 <        if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
181 <                quiterr("bad picture size");
180 >        if (wrongformat || !fgetsresolu(&inpres, fin))
181 >                quiterr("bad picture format");
182 >        xmax = scanlen(&inpres);
183 >        ymax = numscans(&inpres);
184                                  /* set view parameters */
185          if (gotview && setview(&ourview) != NULL)
186                  gotview = 0;
# Line 163 | Line 193 | char  *argv[];
193                  getevent();             /* main loop */
194   userr:
195          fprintf(stderr,
196 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
196 >        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
197                          progname);
198          quit(1);
199   }
# Line 172 | Line 202 | userr:
202   headline(s)             /* get relevant info from header */
203   char  *s;
204   {
205 <        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
176 <        register char  **an;
205 >        char  fmt[32];
206  
207 <        if (!strncmp(s, "EXPOSURE=", 9))
208 <                exposure *= atof(s+9);
209 <        else
210 <                for (an = altname; *an != NULL; an++)
211 <                        if (!strncmp(*an, s, strlen(*an))) {
212 <                                if (sscanview(&ourview, s+strlen(*an)) == 0)
213 <                                        gotview++;
185 <                                return;
186 <                        }
207 >        if (isexpos(s))
208 >                exposure *= exposval(s);
209 >        else if (isformat(s)) {
210 >                formatval(fmt, s);
211 >                wrongformat = strcmp(fmt, COLRFMT);
212 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
213 >                gotview++;
214   }
215  
216  
217   init()                  /* get data and open window */
218   {
219 <        XSizeHints  oursizhints;
220 <        register int  i;
219 >        XWMHints        ourxwmhints;
220 >        XSetWindowAttributes    ourwinattr;
221 >        XSizeHints      oursizhints;
222 >        register int    i;
223          
224          if (fname != NULL) {
225                  scanpos = (long *)malloc(ymax*sizeof(long));
# Line 199 | Line 228 | init()                 /* get data and open window */
228                  for (i = 0; i < ymax; i++)
229                          scanpos[i] = -1;
230          }
231 <        if ((thedisplay = XOpenDisplay(NULL)) == NULL)
232 <                quiterr("can't open display; DISPLAY variable set?");
233 <        wind = XCreateSimpleWindow(thedisplay,
234 <                ourroot, 0, 0, xmax, ymax, BORWIDTH, ourblack, ourwhite);
231 >        if ((thedisplay = XOpenDisplay(dispname)) == NULL)
232 >                quiterr("cannot open display");
233 >                                /* get best visual for default screen */
234 >        getbestvis();
235 >                                /* store image */
236 >        getras();
237 >                                /* open window */
238 >        ourwinattr.border_pixel = ourblack;
239 >        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("can't create window");
247 <        if (maxcolors == 0) {           /* get number of available colors */
248 <                maxcolors = 1<<DisplayPlanes(thedisplay,ourscreen);
249 <                if (maxcolors > 4) maxcolors -= 2;
250 <        }
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("can't get font");
256 >                quiterr("cannot get font");
257          XSetFont(thedisplay, ourgc, fontid);
216        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
258          XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
259                          XC_diamond_cross));
260 +        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
261          if (geometry != NULL) {
262                  bzero((char *)&oursizhints, sizeof(oursizhints));
263                  i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
264 <                                &oursizhints.width, &oursizhints.height);
265 <                if (i & (XValue|YValue) == (XValue|YValue)) {
264 >                                (unsigned *)&oursizhints.width,
265 >                                (unsigned *)&oursizhints.height);
266 >                if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
267 >                        oursizhints.flags |= USSize;
268 >                else {
269 >                        oursizhints.width = xmax;
270 >                        oursizhints.height = ymax;
271 >                        oursizhints.flags |= PSize;
272 >                }
273 >                if ((i&(XValue|YValue)) == (XValue|YValue)) {
274                          oursizhints.flags |= USPosition;
275                          if (i & XNegative)
276 <                                oursizhints.x += DisplayWidth(thedisplay,ourscreen)-1;
276 >                                oursizhints.x += DisplayWidth(thedisplay,
277 >                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
278                          if (i & YNegative)
279 <                                oursizhints.y += DisplayHeight(thedisplay,ourscreen)-1;
279 >                                oursizhints.y += DisplayHeight(thedisplay,
280 >                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
281                  }
230                if (i & (WidthValue|HeightValue) == (WidthValue|HeightValue))
231                        oursizhints.flags |= USSize;
282                  XSetNormalHints(thedisplay, wind, &oursizhints);
283          }
284 <                                /* store image */
285 <        getras();
286 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask|
287 <                        StructureNotifyMask|ButtonMotionMask|
288 <                        KeyPressMask|ExposureMask);
284 >        ourxwmhints.flags = InputHint|IconPixmapHint;
285 >        ourxwmhints.input = True;
286 >        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
287 >                        wind, icondata, iconwidth, iconheight);
288 >        XSetWMHints(thedisplay, wind, &ourxwmhints);
289 >        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
290 >                        |ButtonMotionMask|StructureNotifyMask
291 >                        |KeyPressMask|ExposureMask);
292          XMapWindow(thedisplay, wind);
293          return;
294   memerr:
# Line 254 | Line 307 | char  *err;
307   }
308  
309  
310 < eputs(s)
311 < char    *s;
310 > static int
311 > viscmp(v1,v2)           /* compare visual to see which is better, descending */
312 > register XVisualInfo    *v1, *v2;
313   {
314 <        fputs(s, stderr);
314 >        int     bad1 = 0, bad2 = 0;
315 >        register int  *rp;
316 >
317 >        if (v1->class == v2->class) {
318 >                if (v1->class == TrueColor || v1->class == DirectColor) {
319 >                                        /* prefer 24-bit to 32-bit */
320 >                        if (v1->depth == 24 && v2->depth == 32)
321 >                                return(-1);
322 >                        if (v1->depth == 32 && v2->depth == 24)
323 >                                return(1);
324 >                        return(0);
325 >                }
326 >                                        /* don't be too greedy */
327 >                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
328 >                        return(v1->depth - v2->depth);
329 >                return(v2->depth - v1->depth);
330 >        }
331 >                                        /* prefer Pseudo when < 24-bit */
332 >        if ((v1->class == TrueColor || v1->class == DirectColor) &&
333 >                        v1->depth < 24)
334 >                bad1 = 1;
335 >        if ((v2->class == TrueColor || v2->class == DirectColor) &&
336 >                        v2->depth < 24)
337 >                bad2 = -1;
338 >        if (bad1 | bad2)
339 >                return(bad1+bad2);
340 >                                        /* otherwise, use class ranking */
341 >        for (rp = ourrank; *rp != -1; rp++) {
342 >                if (v1->class == *rp)
343 >                        return(-1);
344 >                if (v2->class == *rp)
345 >                        return(1);
346 >        }
347 >        return(0);
348   }
349  
350  
351 < quit(code)
265 < int  code;
351 > getbestvis()                    /* get the best visual for this screen */
352   {
353 <        exit(code);
353 > #ifdef DEBUG
354 > static char  vistype[][12] = {
355 >                "StaticGray",
356 >                "GrayScale",
357 >                "StaticColor",
358 >                "PseudoColor",
359 >                "TrueColor",
360 >                "DirectColor"
361 > };
362 > #endif
363 >        static int      rankings[3][6] = {
364 >                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
365 >                {PseudoColor,GrayScale,StaticGray,-1},
366 >                {PseudoColor,GrayScale,StaticGray,-1}
367 >        };
368 >        XVisualInfo     *xvi;
369 >        int     vismatched;
370 >        register int    i, j;
371 >
372 >        if (greyscale) {
373 >                ourrank = rankings[2];
374 >                if (maxcolors < 2) maxcolors = 256;
375 >        } else if (maxcolors >= 2 && maxcolors <= 256)
376 >                ourrank = rankings[1];
377 >        else {
378 >                ourrank = rankings[0];
379 >                maxcolors = 256;
380 >        }
381 >                                        /* find best visual */
382 >        ourvis.screen = ourscreen;
383 >        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
384 >        if (xvi == NULL)
385 >                quiterr("no visuals for this screen!");
386 > #ifdef DEBUG
387 >        fprintf(stderr, "Supported visuals:\n");
388 >        for (i = 0; i < vismatched; i++)
389 >                fprintf(stderr, "\ttype %s, depth %d\n",
390 >                                vistype[xvi[i].class], xvi[i].depth);
391 > #endif
392 >        for (i = 0, j = 1; j < vismatched; j++)
393 >                if (viscmp(&xvi[i],&xvi[j]) > 0)
394 >                        i = j;
395 >                                        /* compare to least acceptable */
396 >        for (j = 0; ourrank[j++] != -1; )
397 >                ;
398 >        ourvis.class = ourrank[--j];
399 >        ourvis.depth = 1;
400 >        if (viscmp(&xvi[i],&ourvis) > 0)
401 >                quiterr("inadequate visuals on this screen");
402 >                                        /* OK, we'll use it */
403 >        copystruct(&ourvis, &xvi[i]);
404 > #ifdef DEBUG
405 >        fprintf(stderr, "Selected visual type %s, depth %d\n",
406 >                        vistype[ourvis.class], ourvis.depth);
407 > #endif
408 >                                        /* make appropriate adjustments */
409 >        if (ourvis.class == GrayScale || ourvis.class == StaticGray)
410 >                greyscale = 1;
411 >        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
412 >                maxcolors = ourvis.colormap_size;
413 >        if (ourvis.class == StaticGray) {
414 >                ourblack = 0;
415 >                ourwhite = 255;
416 >        } else if (ourvis.class == PseudoColor) {
417 >                ourblack = BlackPixel(thedisplay,ourscreen);
418 >                ourwhite = WhitePixel(thedisplay,ourscreen);
419 >                if ((ourblack|ourwhite) & ~255L) {
420 >                        ourblack = 0;
421 >                        ourwhite = 1;
422 >                }
423 >                if (maxcolors > 4)
424 >                        maxcolors -= 2;
425 >        } else {
426 >                ourblack = 0;
427 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
428 >        }
429 >        XFree((char *)xvi);
430   }
431  
432  
433   getras()                                /* get raster file */
434   {
273        colormap        ourmap;
274        unsigned char   rmap[256], gmap[256], bmap[256];
435          XVisualInfo     vinfo;
276        register int  i;
436  
437          if (maxcolors <= 2) {           /* monochrome */
438                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
439                  if (ourdata == NULL)
440                          goto fail;
441 <                ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
441 >                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
442                                  xmax, ymax, 8);
443                  if (ourras == NULL)
444                          goto fail;
445                  getmono();
446 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) {
447 <                ourdata = (unsigned char *)malloc(xmax*ymax*3);
446 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
447 >                ourdata = (unsigned char *)malloc(4*xmax*ymax);
448                  if (ourdata == NULL)
449                          goto fail;
450 <                ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
451 <                                xmax, ymax, 8);
450 >                ourras = make_raster(thedisplay, &ourvis, 32,
451 >                                ourdata, xmax, ymax, 32);
452                  if (ourras == NULL)
453                          goto fail;
454                  getfull();
# Line 297 | Line 456 | getras()                               /* get raster file */
456                  ourdata = (unsigned char *)malloc(xmax*ymax);
457                  if (ourdata == NULL)
458                          goto fail;
459 <                ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
459 >                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
460                                  xmax, ymax, 8);
461                  if (ourras == NULL)
462                          goto fail;
463 <                if (greyscale)
464 <                        biq(dither,maxcolors,1,ourmap);
463 >                if (greyscale | ourvis.class == StaticGray)
464 >                        getgrey();
465                  else
466 <                        ciq(dither,maxcolors,1,ourmap);
467 <                for (i = 0; i < 256; i++) {
309 <                        rmap[i] = ourmap[0][i];
310 <                        gmap[i] = ourmap[1][i];
311 <                        bmap[i] = ourmap[2][i];
312 <                }
313 <                if (init_rcolors(ourras, rmap, gmap, bmap) == 0)
466 >                        getmapped();
467 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
468                          goto fail;
469          }
470          return;
471   fail:
472 <        quit("could not create raster image");
472 >        quiterr("could not create raster image");
473   }
474  
475  
# Line 341 | Line 495 | getevent()                             /* process the next event */
495          case MapNotify:
496                  map_rcolors(ourras, wind);
497                  if (fast)
498 <                        make_rpixmap(ourras);
498 >                        make_rpixmap(ourras, wind);
499                  break;
500          case UnmapNotify:
501 <                unmap_rcolors(ourras);
501 >                if (!fast)
502 >                        unmap_rcolors(ourras);
503                  break;
504          case Expose:
505                  redraw(e.e.x, e.e.y, e.e.width, e.e.height);
# Line 359 | Line 514 | getevent()                             /* process the next event */
514   }
515  
516  
362 redraw(x, y, w, h)                      /* redraw section of window */
363 int  x, y;
364 int  w, h;
365 {
366        patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras);
367 }
368
369
517   docom(ekey)                                     /* execute command */
518   XKeyPressedEvent  *ekey;
519   {
# Line 375 | Line 522 | XKeyPressedEvent  *ekey;
522          XColor  cvx;
523          int  com, n;
524          double  comp;
525 +        FLOAT  hv[2];
526          FVECT  rorg, rdir;
527  
528          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
# Line 383 | Line 531 | XKeyPressedEvent  *ekey;
531          com = buf[0];
532          switch (com) {                  /* interpret command */
533          case 'q':
534 <        case CTRL(D):                           /* quit */
534 >        case CTRL('D'):                         /* quit */
535                  quit(0);
536          case '\n':
537          case '\r':
# Line 397 | Line 545 | XKeyPressedEvent  *ekey;
545                          sprintf(buf, "%.3f", intens(cval)/exposure);
546                          break;
547                  case 'l':                               /* luminance */
548 <                        sprintf(buf, "%.0fn", bright(cval)*683.0/exposure);
548 >                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
549                          break;
550                  case 'c':                               /* color */
551                          comp = pow(2.0, (double)scale);
# Line 407 | Line 555 | XKeyPressedEvent  *ekey;
555                                          colval(cval,BLU)*comp);
556                          break;
557                  }
558 <                XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin,
559 <                                buf, strlen(buf));
558 >                XDrawImageString(thedisplay, wind, ourgc,
559 >                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
560                  return(0);
561          case 'i':                               /* identify (contour) */
562                  if (ourras->pixels == NULL)
# Line 419 | Line 567 | XKeyPressedEvent  *ekey;
567                  cvx.red = random() & 65535;
568                  cvx.green = random() & 65535;
569                  cvx.blue = random() & 65535;
570 <                XStoreColors(thedisplay, ourras->cmap, &cvx, 1);
570 >                cvx.flags = DoRed|DoGreen|DoBlue;
571 >                XStoreColor(thedisplay, ourras->cmap, &cvx);
572                  return(0);
573          case 'p':                               /* position */
574 <                sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
574 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
575 >                sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
576 >                                (int)(hv[1]*inpres.yr));
577                  XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
578                                          buf, strlen(buf));
579                  return(0);
# Line 431 | Line 582 | XKeyPressedEvent  *ekey;
582                          XBell(thedisplay, 0);
583                          return(-1);
584                  }
585 <                viewray(rorg, rdir, &ourview,
586 <                                (ekey->x-xoff+.5)/xmax,
587 <                                (ymax-1-ekey->y+yoff+.5)/ymax);
585 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
586 >                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
587 >                        return(-1);
588                  printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
589                  printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
590                  fflush(stdout);
# Line 447 | Line 598 | XKeyPressedEvent  *ekey;
598                  scale_rcolors(ourras, pow(2.0, (double)n));
599                  scale += n;
600                  sprintf(buf, "%+d", scale);
601 <                XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin,
602 <                                buf, strlen(buf));
601 >                XDrawImageString(thedisplay, wind, ourgc,
602 >                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
603                  XFlush(thedisplay);
604                  free(ourdata);
605                  free_raster(ourras);
606                  getras();
607          /* fall through */
608 <        case CTRL(R):                           /* redraw */
609 <        case CTRL(L):
608 >        case CTRL('R'):                         /* redraw */
609 >        case CTRL('L'):
610                  unmap_rcolors(ourras);
611                  XClearWindow(thedisplay, wind);
612                  map_rcolors(ourras, wind);
613                  if (fast)
614 <                        make_rpixmap(ourras);
614 >                        make_rpixmap(ourras, wind);
615                  redraw(0, 0, width, height);
616                  return(0);
617          case ' ':                               /* clear */
618                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
619                  return(0);
620          default:
621 <                XBell(0);
621 >                XBell(thedisplay, 0);
622                  return(-1);
623          }
624   }
625  
626  
627 < moveimage(ep)                           /* shift the image */
628 < XButtonPressedEvent  *ep;
627 > moveimage(ebut)                         /* shift the image */
628 > XButtonPressedEvent  *ebut;
629   {
630 <        XButtonPressedEvent  eb;
630 >        union {
631 >                XEvent  u;
632 >                XButtonReleasedEvent  b;
633 >                XPointerMovedEvent  m;
634 >        }  e;
635 >        int     mxo, myo;
636  
637 <        XMaskEvent(thedisplay, ButtonReleaseMask, &eb);
638 <        xoff += eb.x - ep->x;
639 <        yoff += eb.y - ep->y;
637 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
638 >        while (e.u.type == MotionNotify) {
639 >                mxo = e.m.x;
640 >                myo = e.m.y;
641 >                revline(ebut->x, ebut->y, mxo, myo);
642 >                revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
643 >                                xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
644 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
645 >                revline(ebut->x, ebut->y, mxo, myo);
646 >                revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
647 >                                xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
648 >        }
649 >        xoff += e.b.x - ebut->x;
650 >        yoff += e.b.y - ebut->y;
651          XClearWindow(thedisplay, wind);
652          redraw(0, 0, width, height);
486        return(0);
653   }
654  
655  
# Line 491 | Line 657 | getbox(ebut)                           /* get new box */
657   XButtonPressedEvent  *ebut;
658   {
659          union {
660 <                XEvent  e;
660 >                XEvent  u;
661                  XButtonReleasedEvent  b;
662                  XPointerMovedEvent  m;
663          }  e;
664  
665 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.e);
666 <        while (e.e.type == ButtonMotionMask) {
665 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
666 >        while (e.u.type == MotionNotify) {
667                  revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
668 <                XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask,
503 <                                &e.e);
668 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
669                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
670          }
671          box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
# Line 523 | Line 688 | XButtonPressedEvent  *ebut;
688   revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
689   int  x0, y0, x1, y1;
690   {
691 <        static GC       mygc = 0;
691 >        revline(x0, y0, x1, y0);
692 >        revline(x0, y1, x1, y1);
693 >        revline(x0, y0, x0, y1);
694 >        revline(x1, y0, x1, y1);
695 > }
696  
528        if (mygc == 0) {
529                mygc = XCreateGC(thedisplay, wind, 0, 0);
530                XSetPlaneMask(thedisplay, mygc, ~0L);
531                XSetFunction(thedisplay, mygc, GXinvert);
532        }
533        XDrawLine(thedisplay, wind, mygc, x0, y0, x1, y0);
534        XDrawLine(thedisplay, wind, mygc, x0, y1, x1, y1);
535        XDrawLine(thedisplay, wind, mygc, x0, y0, x0, y1);
536        XDrawLine(thedisplay, wind, mygc, x1, y0, x1, y1);
537 } /* end of revbox */
697  
539
698   avgbox(clr)                             /* average color over current box */
699   COLOR  clr;
700   {
701 +        static COLOR  lc;
702 +        static int  ll, lr, lt, lb;
703          int  left, right, top, bottom;
704          int  y;
705          double  d;
# Line 563 | Line 723 | COLOR  clr;
723                  bottom = ymax;
724          if (top >= bottom)
725                  return(-1);
726 +        if (left == ll && right == lr && top == lt && bottom == lb) {
727 +                copycolor(clr, lc);
728 +                return(0);
729 +        }
730          for (y = top; y < bottom; y++) {
731                  if (getscan(y) == -1)
732                          return(-1);
# Line 573 | Line 737 | COLOR  clr;
737          }
738          d = 1.0/((right-left)*(bottom-top));
739          scalecolor(clr, d);
740 +        ll = left; lr = right; lt = top; lb = bottom;
741 +        copycolor(lc, clr);
742          return(0);
743   }
744  
# Line 581 | Line 747 | getmono()                      /* get monochrome data */
747   {
748          register unsigned char  *dp;
749          register int    x, err;
750 <        int     y;
585 <        rgbpixel        *inline;
750 >        int     y, errp;
751          short   *cerr;
752  
753 <        if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
754 <                        || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
590 <                quit("out of memory in getmono");
753 >        if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
754 >                quiterr("out of memory in getmono");
755          dp = ourdata - 1;
756          for (y = 0; y < ymax; y++) {
757 <                picreadline3(y, inline);
757 >                getscan(y);
758 >                add2icon(y, scanline);
759 >                normcolrs(scanline, xmax, scale);
760                  err = 0;
761                  for (x = 0; x < xmax; x++) {
762                          if (!(x&7))
763                                  *++dp = 0;
764 <                        err += rgb_bright(&inline[x]) + cerr[x];
764 >                        errp = err;
765 >                        err += normbright(scanline[x]) + cerr[x];
766                          if (err > 127)
767                                  err -= 255;
768                          else
769 <                                *dp |= 1<<(x&07);
770 <                        cerr[x] = err >>= 1;
769 >                                *dp |= 1<<(7-(x&07));
770 >                        err /= 3;
771 >                        cerr[x] = err + errp;
772                  }
773          }
606        free((char *)inline);
774          free((char *)cerr);
775   }
776  
777  
778 + add2icon(y, scan)               /* add a scanline to our icon data */
779 + int  y;
780 + COLR  *scan;
781 + {
782 +        static short  cerr[ICONSIZ];
783 +        static int  ynext;
784 +        static char  *dp;
785 +        double  sf;
786 +        COLOR  col;
787 +        register int  err;
788 +        register int    x, ti;
789 +        int  errp;
790 +
791 +        if (iconheight == 0) {          /* initialize */
792 +                if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
793 +                        iconwidth = xmax;
794 +                        iconheight = ymax;
795 +                } else if (xmax > ymax) {
796 +                        iconwidth = ICONSIZ;
797 +                        iconheight = ICONSIZ*ymax/xmax;
798 +                        if (iconheight < 1)
799 +                                iconheight = 1;
800 +                } else {
801 +                        iconwidth = ICONSIZ*xmax/ymax;
802 +                        if (iconwidth < 1)
803 +                                iconwidth = 1;
804 +                        iconheight = ICONSIZ;
805 +                }
806 +                ynext = 0;
807 +                dp = icondata - 1;
808 +        }
809 +        if (y < ynext*ymax/iconheight)  /* skip this one */
810 +                return;
811 +        sf = pow(2.0, (double)(scale+8));
812 +        err = 0;
813 +        for (x = 0; x < iconwidth; x++) {
814 +                if (!(x&7))
815 +                        *++dp = 0;
816 +                errp = err;
817 +                ti = x*xmax/iconwidth;
818 +                colr_color(col, scan[ti]);
819 +                ti = sf*bright(col);
820 +                if (ti > 255) ti = 255;
821 +                err += ti + cerr[x];
822 +                if (err > 127)
823 +                        err -= 255;
824 +                else
825 +                        *dp |= 1<<(x&07);
826 +                err /= 3;
827 +                cerr[x] = err + errp;
828 +        }
829 +        ynext++;
830 + }
831 +
832 +
833   getfull()                       /* get full (24-bit) data */
834   {
835          int     y;
836 +        register unsigned long  *dp;
837 +        register int    x;
838 +                                        /* set gamma correction */
839 +        setcolrgam(gamcor);
840 +                                        /* read and convert file */
841 +        dp = (unsigned long *)ourdata;
842 +        for (y = 0; y < ymax; y++) {
843 +                getscan(y);
844 +                add2icon(y, scanline);
845 +                if (scale)
846 +                        shiftcolrs(scanline, xmax, scale);
847 +                colrs_gambs(scanline, xmax);
848 +                if (ourras->image->blue_mask & 1)
849 +                        for (x = 0; x < xmax; x++)
850 +                                *dp++ = scanline[x][RED] << 16 |
851 +                                        scanline[x][GRN] << 8 |
852 +                                        scanline[x][BLU] ;
853 +                else
854 +                        for (x = 0; x < xmax; x++)
855 +                                *dp++ = scanline[x][RED] |
856 +                                        scanline[x][GRN] << 8 |
857 +                                        scanline[x][BLU] << 16 ;
858 +        }
859 + }
860  
861 <        for (y = 0; y < ymax; y++)
862 <                picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3));
861 >
862 > getgrey()                       /* get greyscale data */
863 > {
864 >        int     y;
865 >        register unsigned char  *dp;
866 >        register int    x;
867 >                                        /* set gamma correction */
868 >        setcolrgam(gamcor);
869 >                                        /* read and convert file */
870 >        dp = ourdata;
871 >        for (y = 0; y < ymax; y++) {
872 >                getscan(y);
873 >                add2icon(y, scanline);
874 >                if (scale)
875 >                        shiftcolrs(scanline, xmax, scale);
876 >                colrs_gambs(scanline, xmax);
877 >                if (maxcolors < 256)
878 >                        for (x = 0; x < xmax; x++)
879 >                                *dp++ = ((long)normbright(scanline[x]) *
880 >                                        maxcolors + 128) >> 8;
881 >                else
882 >                        for (x = 0; x < xmax; x++)
883 >                                *dp++ = normbright(scanline[x]);
884 >        }
885 >        for (x = 0; x < maxcolors; x++)
886 >                clrtab[x][RED] = clrtab[x][GRN] =
887 >                        clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors;
888   }
889  
890  
891 + getmapped()                     /* get color-mapped data */
892 + {
893 +        int     y;
894 +                                        /* set gamma correction */
895 +        setcolrgam(gamcor);
896 +                                        /* make histogram */
897 +        new_histo();
898 +        for (y = 0; y < ymax; y++) {
899 +                if (getscan(y) < 0)
900 +                        quiterr("seek error in getmapped");
901 +                add2icon(y, scanline);
902 +                if (scale)
903 +                        shiftcolrs(scanline, xmax, scale);
904 +                colrs_gambs(scanline, xmax);
905 +                cnt_colrs(scanline, xmax);
906 +        }
907 +                                        /* map pixels */
908 +        if (!new_clrtab(maxcolors))
909 +                quiterr("cannot create color map");
910 +        for (y = 0; y < ymax; y++) {
911 +                if (getscan(y) < 0)
912 +                        quiterr("seek error in getmapped");
913 +                if (scale)
914 +                        shiftcolrs(scanline, xmax, scale);
915 +                colrs_gambs(scanline, xmax);
916 +                if (dither)
917 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
918 +                else
919 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
920 +        }
921 + }
922 +
923 +
924   scale_rcolors(xr, sf)                   /* scale color map */
925   register XRASTER        *xr;
926   double  sf;
# Line 652 | Line 956 | int  y;
956                  if (scanpos == NULL || scanpos[y] == -1)
957                          return(-1);
958                  if (fseek(fin, scanpos[y], 0) == -1)
959 <                        quit("fseek error");
959 >                        quiterr("fseek error");
960                  cury = y;
961 <        } else if (scanpos != NULL)
961 >        } else if (scanpos != NULL && scanpos[y] == -1)
962                  scanpos[y] = ftell(fin);
963  
964          if (freadcolrs(scanline, xmax, fin) < 0)
# Line 662 | Line 966 | int  y;
966  
967          cury++;
968          return(0);
665 }
666
667
668 picreadline3(y, l3)                     /* read in 3-byte scanline */
669 int  y;
670 register rgbpixel  *l3;
671 {
672        register int    i;
673                                                        /* read scanline */
674        if (getscan(y) < 0)
675                quiterr("cannot seek for picreadline");
676                                                        /* convert scanline */
677        normcolrs(scanline, xmax, scale);
678        for (i = 0; i < xmax; i++) {
679                l3[i].r = scanline[i][RED];
680                l3[i].g = scanline[i][GRN];
681                l3[i].b = scanline[i][BLU];
682        }
683 }
684
685
686 picwriteline(y, l)              /* add 8-bit scanline to image */
687 int  y;
688 pixel  *l;
689 {
690        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
691 }
692
693
694 picreadcm(map)                  /* do gamma correction */
695 colormap  map;
696 {
697        extern double  pow();
698        register int  i, val;
699
700        for (i = 0; i < 256; i++) {
701                val = pow(i/256.0, 1.0/gamcor) * 256.0;
702                map[0][i] = map[1][i] = map[2][i] = val;
703        }
704 }
705
706
707 picwritecm(map)                 /* handled elsewhere */
708 colormap  map;
709 {
710 #ifdef DEBUG
711        register int i;
712
713        for (i = 0; i < 256; i++)
714                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
715 #endif
969   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines