1 |
– |
/* Copyright (c) 1987 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* ximage.c - driver for X-windows |
6 |
|
* |
18 |
|
|
19 |
|
#include <ctype.h> |
20 |
|
|
21 |
+ |
#include <time.h> |
22 |
+ |
|
23 |
|
#include "color.h" |
24 |
|
|
25 |
+ |
#include "resolu.h" |
26 |
+ |
|
27 |
|
#include "xraster.h" |
28 |
|
|
29 |
|
#include "view.h" |
30 |
|
|
31 |
|
#include "pic.h" |
32 |
|
|
33 |
+ |
#include "random.h" |
34 |
+ |
|
35 |
|
#define controlshift(e) (((XButtonEvent *)(e))->detail & (ShiftMask|ControlMask)) |
36 |
|
|
37 |
|
#define FONTNAME "9x15" /* text font we'll use */ |
38 |
|
|
39 |
< |
#define CTRL(c) ('c'-'@') |
39 |
> |
#define CTRL(c) ((c)-'@') |
40 |
|
|
41 |
|
#define BORWIDTH 5 /* border width */ |
42 |
|
#define BARHEIGHT 25 /* menu bar size */ |
43 |
|
|
44 |
< |
double gamcor = 2.0; /* gamma correction */ |
44 |
> |
double gamcor = 2.2; /* gamma correction */ |
45 |
|
|
46 |
|
XRASTER *ourras = NULL; /* our stored raster image */ |
47 |
|
|
59 |
|
int xoff = 0; /* x image offset */ |
60 |
|
int yoff = 0; /* y image offset */ |
61 |
|
|
62 |
< |
VIEW ourview = STDVIEW(0); /* image view parameters */ |
62 |
> |
VIEW ourview = STDVIEW; /* image view parameters */ |
63 |
|
int gotview = 0; /* got parameters from file */ |
64 |
|
|
65 |
|
COLR *scanline; /* scan line buffer */ |
73 |
|
|
74 |
|
double exposure = 1.0; /* exposure compensation used */ |
75 |
|
|
76 |
+ |
int wrongformat = 0; /* input in another format */ |
77 |
+ |
|
78 |
|
struct { |
79 |
|
int xmin, ymin, xsiz, ysiz; |
80 |
|
} box = {0, 0, 0, 0}; /* current box */ |
85 |
|
|
86 |
|
char errmsg[128]; |
87 |
|
|
83 |
– |
extern long ftell(); |
88 |
|
|
85 |
– |
extern char *malloc(), *calloc(); |
86 |
– |
|
87 |
– |
extern double atof(), pow(), log(); |
88 |
– |
|
89 |
– |
|
89 |
|
main(argc, argv) |
90 |
|
int argc; |
91 |
|
char *argv[]; |
92 |
|
{ |
93 |
+ |
extern char *getenv(); |
94 |
+ |
char *gv; |
95 |
|
int headline(); |
96 |
|
int i; |
97 |
|
|
98 |
|
progname = argv[0]; |
99 |
+ |
if ((gv = getenv("DISPLAY_GAMMA")) != NULL) |
100 |
+ |
gamcor = atof(gv); |
101 |
|
|
102 |
|
for (i = 1; i < argc; i++) |
103 |
|
if (argv[i][0] == '-') |
140 |
|
sprintf(errmsg, "can't open file \"%s\"", fname); |
141 |
|
quiterr(errmsg); |
142 |
|
} |
143 |
< |
} else |
141 |
< |
goto userr; |
142 |
< |
|
143 |
> |
} |
144 |
|
/* get header */ |
145 |
< |
getheader(fin, headline); |
145 |
> |
getheader(fin, headline, NULL); |
146 |
|
/* get picture dimensions */ |
147 |
< |
if (fscanf(fin, "-Y %d +X %d\n", &ymax, &xmax) != 2) |
147 |
> |
if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
148 |
|
quiterr("bad picture size"); |
149 |
|
/* set view parameters */ |
150 |
< |
if (gotview) { |
151 |
< |
ourview.hresolu = xmax; |
151 |
< |
ourview.vresolu = ymax; |
152 |
< |
if (setview(&ourview) != NULL) |
153 |
< |
gotview = 0; |
154 |
< |
} |
150 |
> |
if (gotview && setview(&ourview) != NULL) |
151 |
> |
gotview = 0; |
152 |
|
if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
153 |
|
quiterr("out of memory"); |
154 |
|
|
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
+ |
int |
168 |
|
headline(s) /* get relevant info from header */ |
169 |
|
char *s; |
170 |
|
{ |
171 |
< |
static char *altname[] = {"rview","rpict","VIEW=",NULL}; |
174 |
< |
register char **an; |
171 |
> |
char fmt[32]; |
172 |
|
|
173 |
< |
if (!strncmp(s, "EXPOSURE=", 9)) |
174 |
< |
exposure *= atof(s+9); |
175 |
< |
else |
176 |
< |
for (an = altname; *an != NULL; an++) |
177 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
178 |
< |
if (sscanview(&ourview, s+strlen(*an)) == 0) |
179 |
< |
gotview++; |
180 |
< |
return; |
184 |
< |
} |
173 |
> |
if (isexpos(s)) |
174 |
> |
exposure *= exposval(s); |
175 |
> |
else if (isformat(s)) { |
176 |
> |
formatval(fmt, s); |
177 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
178 |
> |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
179 |
> |
gotview++; |
180 |
> |
return(0); |
181 |
|
} |
182 |
|
|
183 |
|
|
188 |
– |
char * |
189 |
– |
sskip(s) /* skip a word */ |
190 |
– |
register char *s; |
191 |
– |
{ |
192 |
– |
while (isspace(*s)) s++; |
193 |
– |
while (*s && !isspace(*s)) s++; |
194 |
– |
return(s); |
195 |
– |
} |
196 |
– |
|
197 |
– |
|
184 |
|
init() /* get data and open window */ |
185 |
|
{ |
186 |
|
register int i; |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
+ |
void |
250 |
|
eputs(s) |
251 |
|
char *s; |
252 |
|
{ |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
+ |
void |
258 |
|
quit(code) |
259 |
|
int code; |
260 |
|
{ |
289 |
|
} |
290 |
|
return; |
291 |
|
memerr: |
292 |
< |
quit("out of memory"); |
292 |
> |
quiterr("out of memory"); |
293 |
|
} |
294 |
|
|
295 |
|
|
335 |
|
int x, y; |
336 |
|
int w, h; |
337 |
|
{ |
338 |
< |
if (map_rcolors(ourras) == NULL) { |
338 |
> |
if (ourras->ncolors && map_rcolors(ourras) == NULL) { |
339 |
|
fprintf(stderr, "%s: cannot allocate colors\n", progname); |
340 |
|
return(-1); |
341 |
|
} |
350 |
|
{ |
351 |
|
char buf[80]; |
352 |
|
COLOR cval; |
353 |
+ |
Color cvx; |
354 |
|
char *cp; |
355 |
|
int n; |
356 |
|
double comp; |
361 |
|
return(0); |
362 |
|
switch (*cp) { /* interpret command */ |
363 |
|
case 'q': |
364 |
< |
case CTRL(D): /* quiterr */ |
364 |
> |
case CTRL('D'): /* quit */ |
365 |
|
quit(0); |
366 |
|
case '\n': |
367 |
|
case '\r': |
372 |
|
switch (*cp) { |
373 |
|
case '\n': |
374 |
|
case '\r': /* radiance */ |
375 |
< |
sprintf(buf, "%-3g", intens(cval)/exposure); |
375 |
> |
sprintf(buf, "%.3f", intens(cval)/exposure); |
376 |
|
break; |
377 |
|
case 'l': /* luminance */ |
378 |
< |
sprintf(buf, "%-3gL", bright(cval)*683.0/exposure); |
378 |
> |
sprintf(buf, "%.0fL", luminance(cval)/exposure); |
379 |
|
break; |
380 |
|
case 'c': /* color */ |
381 |
|
comp = pow(2.0, (double)scale); |
388 |
|
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
389 |
|
fontid, BlackPixel, WhitePixel); |
390 |
|
return(0); |
391 |
+ |
case 'i': /* identify (contour) */ |
392 |
+ |
if (ourras->pixels == NULL) |
393 |
+ |
return(-1); |
394 |
+ |
n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)]; |
395 |
+ |
n = ourras->pmap[n]; |
396 |
+ |
cvx.pixel = ourras->cdefs[n].pixel; |
397 |
+ |
cvx.red = random() & 65535; |
398 |
+ |
cvx.green = random() & 65535; |
399 |
+ |
cvx.blue = random() & 65535; |
400 |
+ |
XStoreColor(&cvx); |
401 |
+ |
return(0); |
402 |
|
case 'p': /* position */ |
403 |
|
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
404 |
|
XText(wind, ekey->x, ekey->y, buf, strlen(buf), |
409 |
|
XFeep(0); |
410 |
|
return(-1); |
411 |
|
} |
412 |
< |
rayview(rorg, rdir, &ourview, |
413 |
< |
ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5); |
412 |
> |
if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax, |
413 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
414 |
> |
return(-1); |
415 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
416 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
417 |
|
fflush(stdout); |
420 |
|
if (avgbox(cval) == -1) |
421 |
|
return(-1); |
422 |
|
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
423 |
< |
if (n == 0) { |
423 |
< |
XFeep(0); |
423 |
> |
if (n == 0) |
424 |
|
return(0); |
425 |
– |
} |
425 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
426 |
|
scale += n; |
427 |
|
sprintf(buf, "%+d", scale); |
430 |
|
XFlush(); |
431 |
|
free_raster(ourras); |
432 |
|
getras(); |
433 |
< |
return(redraw(0, 0, width, height)); |
434 |
< |
case CTRL(R): /* redraw */ |
433 |
> |
/* fall through */ |
434 |
> |
case CTRL('R'): /* redraw */ |
435 |
> |
case CTRL('L'): |
436 |
> |
unmap_rcolors(ourras); |
437 |
|
XClear(wind); |
438 |
|
return(redraw(0, 0, width, height)); |
439 |
|
case ' ': /* clear */ |
544 |
|
{ |
545 |
|
register unsigned short *dp; |
546 |
|
register int x, err; |
547 |
< |
int y; |
548 |
< |
rgbpixel *inline; |
547 |
> |
int y, errp; |
548 |
> |
rgbpixel *inl; |
549 |
|
short *cerr; |
550 |
|
|
551 |
< |
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
551 |
> |
if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
552 |
|
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
553 |
< |
quit("out of memory in getmono"); |
553 |
> |
quiterr("out of memory in getmono"); |
554 |
|
dp = ourras->data.m - 1; |
555 |
|
for (y = 0; y < ymax; y++) { |
556 |
< |
picreadline3(y, inline); |
556 |
> |
picreadline3(y, inl); |
557 |
|
err = 0; |
558 |
|
for (x = 0; x < xmax; x++) { |
559 |
|
if (!(x&0xf)) |
560 |
|
*++dp = 0; |
561 |
< |
err += rgb_bright(&inline[x]) + cerr[x]; |
561 |
> |
errp = err; |
562 |
> |
err += rgb_bright(&inl[x]) + cerr[x]; |
563 |
|
if (err > 127) |
564 |
|
err -= 255; |
565 |
|
else |
566 |
|
*dp |= 1<<(x&0xf); |
567 |
< |
cerr[x] = err >>= 1; |
567 |
> |
err /= 3; |
568 |
> |
cerr[x] = err + errp; |
569 |
|
} |
570 |
|
} |
571 |
< |
free((char *)inline); |
572 |
< |
free((char *)cerr); |
571 |
> |
free((void *)inl); |
572 |
> |
free((void *)cerr); |
573 |
|
} |
574 |
|
|
575 |
|
|
597 |
|
xr->cdefs[xr->ncolors].pixel = *p; |
598 |
|
xr->pmap[*p] = xr->ncolors++; |
599 |
|
} |
600 |
< |
xr->cdefs = (Color *)realloc((char *)xr->cdefs, xr->ncolors*sizeof(Color)); |
600 |
> |
xr->cdefs = (Color *)realloc((void *)xr->cdefs, xr->ncolors*sizeof(Color)); |
601 |
|
if (xr->cdefs == NULL) |
602 |
|
return(0); |
603 |
|
return(1); |
609 |
|
double sf; |
610 |
|
{ |
611 |
|
register int i; |
612 |
< |
int maxv; |
612 |
> |
long maxv; |
613 |
|
|
614 |
+ |
if (xr->pixels == NULL) |
615 |
+ |
return; |
616 |
+ |
|
617 |
|
sf = pow(sf, 1.0/gamcor); |
618 |
< |
maxv = (1<<16) / sf; |
618 |
> |
maxv = 65535/sf; |
619 |
|
|
620 |
|
for (i = xr->ncolors; i--; ) { |
621 |
< |
xr->cdefs[i].red = xr->cdefs[i].red >= maxv ? |
622 |
< |
(1<<16)-1 : |
621 |
> |
xr->cdefs[i].red = xr->cdefs[i].red > maxv ? |
622 |
> |
65535 : |
623 |
|
xr->cdefs[i].red * sf; |
624 |
< |
xr->cdefs[i].green = xr->cdefs[i].green >= maxv ? |
625 |
< |
(1<<16)-1 : |
624 |
> |
xr->cdefs[i].green = xr->cdefs[i].green > maxv ? |
625 |
> |
65535 : |
626 |
|
xr->cdefs[i].green * sf; |
627 |
< |
xr->cdefs[i].blue = xr->cdefs[i].blue >= maxv ? |
628 |
< |
(1<<16)-1 : |
627 |
> |
xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? |
628 |
> |
65535 : |
629 |
|
xr->cdefs[i].blue * sf; |
630 |
|
} |
631 |
< |
if (xr->pixels != NULL) |
626 |
< |
XStoreColors(xr->ncolors, xr->cdefs); |
631 |
> |
XStoreColors(xr->ncolors, xr->cdefs); |
632 |
|
} |
633 |
|
|
634 |
|
|
639 |
|
if (scanpos == NULL || scanpos[y] == -1) |
640 |
|
return(-1); |
641 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
642 |
< |
quit("fseek error"); |
642 |
> |
quiterr("fseek error"); |
643 |
|
cury = y; |
644 |
< |
} else if (scanpos != NULL) |
644 |
> |
} else if (scanpos != NULL && scanpos[y] == -1) |
645 |
|
scanpos[y] = ftell(fin); |
646 |
|
|
647 |
|
if (freadcolrs(scanline, xmax, fin) < 0) |
656 |
|
int y; |
657 |
|
register rgbpixel *l3; |
658 |
|
{ |
659 |
< |
register BYTE *l4; |
660 |
< |
register int shift, c; |
656 |
< |
int i; |
657 |
< |
|
659 |
> |
register int i; |
660 |
> |
/* read scanline */ |
661 |
|
if (getscan(y) < 0) |
662 |
|
quiterr("cannot seek for picreadline"); |
663 |
|
/* convert scanline */ |
664 |
< |
for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) { |
665 |
< |
shift = l4[EXP] - COLXS + scale; |
666 |
< |
if (shift >= 8) { |
667 |
< |
l3->r = l3->g = l3->b = 255; |
668 |
< |
} else if (shift <= -8) { |
666 |
< |
l3->r = l3->g = l3->b = 0; |
667 |
< |
} else if (shift > 0) { |
668 |
< |
c = l4[RED] << shift; |
669 |
< |
l3->r = c > 255 ? 255 : c; |
670 |
< |
c = l4[GRN] << shift; |
671 |
< |
l3->g = c > 255 ? 255 : c; |
672 |
< |
c = l4[BLU] << shift; |
673 |
< |
l3->b = c > 255 ? 255 : c; |
674 |
< |
} else if (shift < 0) { |
675 |
< |
l3->r = l4[RED] >> -shift; |
676 |
< |
l3->g = l4[GRN] >> -shift; |
677 |
< |
l3->b = l4[BLU] >> -shift; |
678 |
< |
} else { |
679 |
< |
l3->r = l4[RED]; |
680 |
< |
l3->g = l4[GRN]; |
681 |
< |
l3->b = l4[BLU]; |
682 |
< |
} |
664 |
> |
normcolrs(scanline, xmax, scale); |
665 |
> |
for (i = 0; i < xmax; i++) { |
666 |
> |
l3[i].r = scanline[i][RED]; |
667 |
> |
l3[i].g = scanline[i][GRN]; |
668 |
> |
l3[i].b = scanline[i][BLU]; |
669 |
|
} |
670 |
|
} |
671 |
|
|
674 |
|
int y; |
675 |
|
pixel *l; |
676 |
|
{ |
677 |
< |
bcopy(l, ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
677 |
> |
bcopy((char *)l, (char *)ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
678 |
|
} |
679 |
|
|
680 |
|
|
681 |
< |
picreadcm(map) /* do gamcor correction */ |
681 |
> |
picreadcm(map) /* do gamma correction */ |
682 |
|
colormap map; |
683 |
|
{ |
684 |
|
extern double pow(); |
685 |
|
register int i, val; |
686 |
|
|
687 |
|
for (i = 0; i < 256; i++) { |
688 |
< |
val = pow(i/256.0, 1.0/gamcor) * 256.0; |
688 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
689 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
690 |
|
} |
691 |
|
} |