ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.20
Committed: Fri Feb 12 18:03:08 1993 UTC (31 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.19: +24 -23 lines
Log Message:
fixed -geometry option handling for twm and like window managers

File Contents

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