ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.6
Committed: Wed May 27 14:28:50 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +147 -34 lines
Log Message:
fixes for different display types

File Contents

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