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

# 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 break;
141 }
142 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 else if (argv[i][0] == '=')
162 geometry = argv[i];
163 else
164 break;
165
166 if (i == argc-1) {
167 fname = argv[i];
168 fin = fopen(fname, "r");
169 if (fin == NULL) {
170 sprintf(errmsg, "cannot open file \"%s\"", fname);
171 quiterr(errmsg);
172 }
173 } else if (i != argc)
174 goto userr;
175 /* get header */
176 getheader(fin, headline, NULL);
177 /* get picture dimensions */
178 if (wrongformat || !fgetsresolu(&inpres, fin))
179 quiterr("bad picture format");
180 xmax = scanlen(&inpres);
181 ymax = numscans(&inpres);
182 /* 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 char fmt[32];
204
205 if (isexpos(s))
206 exposure *= exposval(s);
207 else if (isformat(s)) {
208 formatval(fmt, s);
209 wrongformat = strcmp(fmt, COLRFMT);
210 } else if (isview(s) && sscanview(&ourview, s) > 0)
211 gotview++;
212 }
213
214
215 init() /* get data and open window */
216 {
217 XWMHints ourxwmhints;
218 XSetWindowAttributes ourwinattr;
219 XSizeHints oursizhints;
220 register int i;
221
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 if ((thedisplay = XOpenDisplay(dispname)) == NULL)
230 quiterr("cannot open display");
231 /* get best visual for default screen */
232 getbestvis();
233 /* store image */
234 getras();
235 /* open window */
236 ourwinattr.border_pixel = ourblack;
237 ourwinattr.background_pixel = ourwhite;
238 ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
239 ourvis.visual, AllocNone);
240 wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
241 ourvis.depth, InputOutput, ourvis.visual,
242 CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
243 if (wind == 0)
244 quiterr("cannot create window");
245 width = xmax;
246 height = ymax;
247 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 fontid = XLoadFont(thedisplay, FONTNAME);
253 if (fontid == 0)
254 quiterr("cannot get font");
255 XSetFont(thedisplay, ourgc, fontid);
256 XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
257 XC_diamond_cross));
258 XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
259 if (geometry != NULL) {
260 bzero((char *)&oursizhints, sizeof(oursizhints));
261 i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
262 (unsigned *)&oursizhints.width,
263 (unsigned *)&oursizhints.height);
264 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 oursizhints.flags |= USPosition;
273 if (i & XNegative)
274 oursizhints.x += DisplayWidth(thedisplay,
275 ourscreen)-1-oursizhints.width-2*BORWIDTH;
276 if (i & YNegative)
277 oursizhints.y += DisplayHeight(thedisplay,
278 ourscreen)-1-oursizhints.height-2*BORWIDTH;
279 }
280 XSetNormalHints(thedisplay, wind, &oursizhints);
281 }
282 ourxwmhints.flags = InputHint|IconPixmapHint;
283 ourxwmhints.input = True;
284 ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
285 wind, icondata, iconwidth, iconheight);
286 XSetWMHints(thedisplay, wind, &ourxwmhints);
287 XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
288 |ButtonMotionMask|StructureNotifyMask
289 |KeyPressMask|ExposureMask);
290 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 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 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 ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
432 xmax, ymax, 8);
433 if (ourras == NULL)
434 goto fail;
435 getmono();
436 } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
437 ourdata = (unsigned char *)malloc(4*xmax*ymax);
438 if (ourdata == NULL)
439 goto fail;
440 ourras = make_raster(thedisplay, &ourvis, 32,
441 ourdata, xmax, ymax, 32);
442 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 ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
450 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 if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
458 goto fail;
459 }
460 return;
461 fail:
462 quiterr("could not create raster image");
463 }
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 make_rpixmap(ourras, wind);
489 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 FLOAT hv[2];
515 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 case CTRL('D'): /* quit */
524 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 sprintf(buf, "%.0fL", luminance(cval)/exposure);
538 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 XDrawImageString(thedisplay, wind, ourgc,
548 box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
549 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 cvx.flags = DoRed|DoGreen|DoBlue;
560 XStoreColor(thedisplay, ourras->cmap, &cvx);
561 return(0);
562 case 'p': /* position */
563 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 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 pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
575 if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
576 return(-1);
577 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 XDrawImageString(thedisplay, wind, ourgc,
591 box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
592 XFlush(thedisplay);
593 free(ourdata);
594 free_raster(ourras);
595 getras();
596 /* fall through */
597 case CTRL('R'): /* redraw */
598 case CTRL('L'):
599 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 XBell(thedisplay, 0);
611 return(-1);
612 }
613 }
614
615
616 moveimage(ebut) /* shift the image */
617 XButtonPressedEvent *ebut;
618 {
619 union {
620 XEvent u;
621 XButtonReleasedEvent b;
622 XPointerMovedEvent m;
623 } e;
624 int mxo, myo;
625
626 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
627 while (e.u.type == MotionNotify) {
628 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 XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
634 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 }
638 xoff += e.b.x - ebut->x;
639 yoff += e.b.y - ebut->y;
640 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 XEvent u;
650 XButtonReleasedEvent b;
651 XPointerMovedEvent m;
652 } e;
653
654 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
655 while (e.u.type == MotionNotify) {
656 revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
657 XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
658 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 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
686
687 avgbox(clr) /* average color over current box */
688 COLOR clr;
689 {
690 static COLOR lc;
691 static int ll, lr, lt, lb;
692 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 if (left == ll && right == lr && top == lt && bottom == lb) {
716 copycolor(clr, lc);
717 return;
718 }
719 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 ll = left; lr = right; lt = top; lb = bottom;
730 copycolor(lc, clr);
731 return(0);
732 }
733
734
735 getmono() /* get monochrome data */
736 {
737 register unsigned char *dp;
738 register int x, err;
739 int y, errp;
740 short *cerr;
741
742 if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
743 quiterr("out of memory in getmono");
744 dp = ourdata - 1;
745 for (y = 0; y < ymax; y++) {
746 if (getscan(y) < 0)
747 quiterr("seek error in getmono");
748 normcolrs(scanline, xmax, scale);
749 add2icon(y, scanline);
750 err = 0;
751 for (x = 0; x < xmax; x++) {
752 if (!(x&7))
753 *++dp = 0;
754 errp = err;
755 err += normbright(scanline[x]) + cerr[x];
756 if (err > 127)
757 err -= 255;
758 else
759 *dp |= 1<<(7-(x&07));
760 err /= 3;
761 cerr[x] = err + errp;
762 }
763 }
764 free((char *)cerr);
765 }
766
767
768 add2icon(y, scan) /* add a scanline to our icon data */
769 int y;
770 COLR *scan;
771 {
772 static short cerr[ICONSIZ];
773 static int ynext;
774 static char *dp;
775 register int err;
776 register int x, ti;
777 int errp;
778
779 if (iconheight == 0) { /* initialize */
780 if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
781 iconwidth = xmax;
782 iconheight = ymax;
783 } else if (xmax > ymax) {
784 iconwidth = ICONSIZ;
785 iconheight = ICONSIZ*ymax/xmax;
786 if (iconheight < 1)
787 iconheight = 1;
788 } else {
789 iconwidth = ICONSIZ*xmax/ymax;
790 if (iconwidth < 1)
791 iconwidth = 1;
792 iconheight = ICONSIZ;
793 }
794 ynext = 0;
795 dp = icondata - 1;
796 }
797 if (y < ynext*ymax/iconheight) /* skip this one */
798 return;
799 err = 0;
800 for (x = 0; x < iconwidth; x++) {
801 if (!(x&7))
802 *++dp = 0;
803 errp = err;
804 ti = x*xmax/iconwidth;
805 err += normbright(scan[ti]) + cerr[x];
806 if (err > 127)
807 err -= 255;
808 else
809 *dp |= 1<<(x&07);
810 err /= 3;
811 cerr[x] = err + errp;
812 }
813 ynext++;
814 }
815
816
817 getfull() /* get full (24-bit) data */
818 {
819 int y;
820 register unsigned long *dp;
821 register int x;
822 /* set gamma correction */
823 setcolrgam(gamcor);
824 /* read and convert file */
825 dp = (unsigned long *)ourdata;
826 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 add2icon(y, scanline);
833 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 }
844 }
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 quiterr("fseek error");
883 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 add2icon(y, scanline);
906 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 val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
930 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 }