ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.7
Committed: Thu May 28 09:43:09 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +63 -23 lines
Log Message:
further refinements for grayscale and odd devices

File Contents

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