1 |
< |
/* Copyright (c) 1990 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
37 |
|
|
38 |
|
#define BORWIDTH 5 /* border width */ |
39 |
|
|
40 |
+ |
#define ICONSIZ (8*10) /* maximum icon dimension (even 8) */ |
41 |
+ |
|
42 |
|
#define ourscreen DefaultScreen(thedisplay) |
43 |
|
#define ourblack BlackPixel(thedisplay,ourscreen) |
44 |
|
#define ourwhite WhitePixel(thedisplay,ourscreen) |
45 |
|
#define ourroot RootWindow(thedisplay,ourscreen) |
46 |
|
#define ourgc DefaultGC(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 */ |
79 |
|
|
80 |
|
double exposure = 1.0; /* exposure compensation used */ |
81 |
|
|
82 |
+ |
int wrongformat = 0; /* input in another format? */ |
83 |
+ |
|
84 |
+ |
GC revgc; /* graphics context with GXinvert */ |
85 |
+ |
|
86 |
|
XRASTER *ourras; /* our stored image */ |
87 |
|
unsigned char *ourdata; /* our image data */ |
88 |
|
|
92 |
|
|
93 |
|
char *geometry = NULL; /* geometry specification */ |
94 |
|
|
95 |
+ |
char icondata[ICONSIZ*ICONSIZ/8]; /* icon bitmap data */ |
96 |
+ |
int iconwidth = 0, iconheight = 0; |
97 |
+ |
|
98 |
|
char *progname; |
99 |
|
|
100 |
|
char errmsg[128]; |
148 |
|
default: |
149 |
|
goto userr; |
150 |
|
} |
151 |
+ |
else if (argv[i][0] == '=') |
152 |
+ |
geometry = argv[i]; |
153 |
|
else |
154 |
|
break; |
155 |
|
|
156 |
< |
if (argc-i == 1) { |
156 |
> |
if (i == argc-1) { |
157 |
|
fname = argv[i]; |
158 |
|
fin = fopen(fname, "r"); |
159 |
|
if (fin == NULL) { |
160 |
|
sprintf(errmsg, "can't open file \"%s\"", fname); |
161 |
|
quiterr(errmsg); |
162 |
|
} |
163 |
< |
} |
163 |
> |
} else if (i != argc) |
164 |
> |
goto userr; |
165 |
|
/* get header */ |
166 |
< |
getheader(fin, headline); |
166 |
> |
getheader(fin, headline, NULL); |
167 |
|
/* get picture dimensions */ |
168 |
< |
if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
169 |
< |
quiterr("bad picture size"); |
168 |
> |
if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
169 |
> |
quiterr("bad picture format"); |
170 |
|
/* set view parameters */ |
171 |
|
if (gotview && setview(&ourview) != NULL) |
172 |
|
gotview = 0; |
190 |
|
{ |
191 |
|
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
192 |
|
register char **an; |
193 |
+ |
char fmt[32]; |
194 |
|
|
195 |
|
if (isexpos(s)) |
196 |
|
exposure *= exposval(s); |
197 |
< |
else |
197 |
> |
else if (isformat(s)) { |
198 |
> |
formatval(fmt, s); |
199 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
200 |
> |
} else |
201 |
|
for (an = altname; *an != NULL; an++) |
202 |
|
if (!strncmp(*an, s, strlen(*an))) { |
203 |
|
if (sscanview(&ourview, s+strlen(*an)) > 0) |
209 |
|
|
210 |
|
init() /* get data and open window */ |
211 |
|
{ |
212 |
+ |
XWMHints ourxwmhints; |
213 |
+ |
XSetWindowAttributes ourwinattr; |
214 |
|
XSizeHints oursizhints; |
215 |
|
register int i; |
216 |
|
|
223 |
|
} |
224 |
|
if ((thedisplay = XOpenDisplay(NULL)) == NULL) |
225 |
|
quiterr("can't open display; DISPLAY variable set?"); |
206 |
– |
wind = XCreateSimpleWindow(thedisplay, |
207 |
– |
ourroot, 0, 0, xmax, ymax, BORWIDTH, ourblack, ourwhite); |
208 |
– |
if (wind == 0) |
209 |
– |
quiterr("can't create window"); |
226 |
|
if (maxcolors == 0) { /* get number of available colors */ |
227 |
|
i = DisplayPlanes(thedisplay,ourscreen); |
228 |
|
maxcolors = i > 8 ? 256 : 1<<i; |
229 |
|
if (maxcolors > 4) maxcolors -= 2; |
230 |
|
} |
231 |
+ |
/* store image */ |
232 |
+ |
getras(); |
233 |
+ |
/* open window */ |
234 |
+ |
ourwinattr.border_pixel = ourblack; |
235 |
+ |
ourwinattr.background_pixel = ourwhite; |
236 |
+ |
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
237 |
+ |
0, InputOutput, ourras->visual, |
238 |
+ |
CWBackPixel|CWBorderPixel, &ourwinattr); |
239 |
+ |
if (wind == 0) |
240 |
+ |
quiterr("can't create window"); |
241 |
+ |
width = xmax; |
242 |
+ |
height = ymax; |
243 |
|
fontid = XLoadFont(thedisplay, FONTNAME); |
244 |
|
if (fontid == 0) |
245 |
|
quiterr("can't get font"); |
246 |
|
XSetFont(thedisplay, ourgc, fontid); |
247 |
< |
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
247 |
> |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
248 |
> |
XSetFunction(thedisplay, revgc, GXinvert); |
249 |
|
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
250 |
|
XC_diamond_cross)); |
251 |
+ |
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
252 |
|
if (geometry != NULL) { |
253 |
|
bzero((char *)&oursizhints, sizeof(oursizhints)); |
254 |
|
i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, |
255 |
< |
&oursizhints.width, &oursizhints.height); |
256 |
< |
if (i & (XValue|YValue) == (XValue|YValue)) { |
255 |
> |
(unsigned *)&oursizhints.width, |
256 |
> |
(unsigned *)&oursizhints.height); |
257 |
> |
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
258 |
> |
oursizhints.flags |= USSize; |
259 |
> |
else { |
260 |
> |
oursizhints.width = xmax; |
261 |
> |
oursizhints.height = ymax; |
262 |
> |
oursizhints.flags |= PSize; |
263 |
> |
} |
264 |
> |
if ((i&(XValue|YValue)) == (XValue|YValue)) { |
265 |
|
oursizhints.flags |= USPosition; |
266 |
|
if (i & XNegative) |
267 |
< |
oursizhints.x += DisplayWidth(thedisplay,ourscreen)-1; |
267 |
> |
oursizhints.x += DisplayWidth(thedisplay, |
268 |
> |
ourscreen)-1-oursizhints.width-2*BORWIDTH; |
269 |
|
if (i & YNegative) |
270 |
< |
oursizhints.y += DisplayHeight(thedisplay,ourscreen)-1; |
270 |
> |
oursizhints.y += DisplayHeight(thedisplay, |
271 |
> |
ourscreen)-1-oursizhints.height-2*BORWIDTH; |
272 |
|
} |
233 |
– |
if (i & (WidthValue|HeightValue) == (WidthValue|HeightValue)) |
234 |
– |
oursizhints.flags |= USSize; |
273 |
|
XSetNormalHints(thedisplay, wind, &oursizhints); |
274 |
|
} |
275 |
< |
/* store image */ |
276 |
< |
getras(); |
275 |
> |
ourxwmhints.flags = InputHint|IconPixmapHint; |
276 |
> |
ourxwmhints.input = True; |
277 |
> |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
278 |
> |
wind, icondata, iconwidth, iconheight); |
279 |
> |
XSetWMHints(thedisplay, wind, &ourxwmhints); |
280 |
|
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
281 |
|
|ButtonMotionMask|StructureNotifyMask |
282 |
|
|KeyPressMask|ExposureMask); |
315 |
|
getras() /* get raster file */ |
316 |
|
{ |
317 |
|
colormap ourmap; |
277 |
– |
unsigned char rmap[256], gmap[256], bmap[256]; |
318 |
|
XVisualInfo vinfo; |
279 |
– |
register int i; |
319 |
|
|
320 |
|
if (maxcolors <= 2) { /* monochrome */ |
321 |
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
326 |
|
if (ourras == NULL) |
327 |
|
goto fail; |
328 |
|
getmono(); |
329 |
< |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) { |
329 |
> |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
330 |
> |
/* kludge for DirectColor */ |
331 |
> |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
332 |
|
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
333 |
|
if (ourdata == NULL) |
334 |
|
goto fail; |
349 |
|
biq(dither,maxcolors,1,ourmap); |
350 |
|
else |
351 |
|
ciq(dither,maxcolors,1,ourmap); |
352 |
< |
for (i = 0; i < 256; i++) { |
312 |
< |
rmap[i] = ourmap[0][i]; |
313 |
< |
gmap[i] = ourmap[1][i]; |
314 |
< |
bmap[i] = ourmap[2][i]; |
315 |
< |
} |
316 |
< |
if (init_rcolors(ourras, rmap, gmap, bmap) == 0) |
352 |
> |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
353 |
|
goto fail; |
354 |
|
} |
355 |
|
return; |
356 |
|
fail: |
357 |
< |
quit("could not create raster image"); |
357 |
> |
quiterr("could not create raster image"); |
358 |
|
} |
359 |
|
|
360 |
|
|
428 |
|
sprintf(buf, "%.3f", intens(cval)/exposure); |
429 |
|
break; |
430 |
|
case 'l': /* luminance */ |
431 |
< |
sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); |
431 |
> |
sprintf(buf, "%.0fL", luminance(cval)/exposure); |
432 |
|
break; |
433 |
|
case 'c': /* color */ |
434 |
|
comp = pow(2.0, (double)scale); |
438 |
|
colval(cval,BLU)*comp); |
439 |
|
break; |
440 |
|
} |
441 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
442 |
< |
buf, strlen(buf)); |
441 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
442 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
443 |
|
return(0); |
444 |
|
case 'i': /* identify (contour) */ |
445 |
|
if (ourras->pixels == NULL) |
463 |
|
XBell(thedisplay, 0); |
464 |
|
return(-1); |
465 |
|
} |
466 |
< |
viewray(rorg, rdir, &ourview, |
466 |
> |
if (viewray(rorg, rdir, &ourview, |
467 |
|
(ekey->x-xoff+.5)/xmax, |
468 |
< |
(ymax-1-ekey->y+yoff+.5)/ymax); |
468 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
469 |
> |
return(-1); |
470 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
471 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
472 |
|
fflush(stdout); |
480 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
481 |
|
scale += n; |
482 |
|
sprintf(buf, "%+d", scale); |
483 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
484 |
< |
buf, strlen(buf)); |
483 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
484 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
485 |
|
XFlush(thedisplay); |
486 |
|
free(ourdata); |
487 |
|
free_raster(ourras); |
510 |
|
XButtonPressedEvent *ebut; |
511 |
|
{ |
512 |
|
union { |
513 |
< |
XEvent u; |
514 |
< |
XButtonReleasedEvent b; |
515 |
< |
} e; |
513 |
> |
XEvent u; |
514 |
> |
XButtonReleasedEvent b; |
515 |
> |
XPointerMovedEvent m; |
516 |
> |
} e; |
517 |
> |
int mxo, myo; |
518 |
|
|
519 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask, &e.u); |
519 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
520 |
> |
while (e.u.type == MotionNotify) { |
521 |
> |
mxo = e.m.x; |
522 |
> |
myo = e.m.y; |
523 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
524 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
525 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
526 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
527 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
528 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
529 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
530 |
> |
} |
531 |
|
xoff += e.b.x - ebut->x; |
532 |
|
yoff += e.b.y - ebut->y; |
533 |
|
XClearWindow(thedisplay, wind); |
570 |
|
revbox(x0, y0, x1, y1) /* draw box with reversed lines */ |
571 |
|
int x0, y0, x1, y1; |
572 |
|
{ |
573 |
< |
static GC mygc = 0; |
573 |
> |
revline(x0, y0, x1, y0); |
574 |
> |
revline(x0, y1, x1, y1); |
575 |
> |
revline(x0, y0, x0, y1); |
576 |
> |
revline(x1, y0, x1, y1); |
577 |
> |
} |
578 |
|
|
525 |
– |
if (mygc == 0) { |
526 |
– |
mygc = XCreateGC(thedisplay, wind, 0, 0); |
527 |
– |
XSetFunction(thedisplay, mygc, GXinvert); |
528 |
– |
} |
529 |
– |
XDrawLine(thedisplay, wind, mygc, x0, y0, x1, y0); |
530 |
– |
XDrawLine(thedisplay, wind, mygc, x0, y1, x1, y1); |
531 |
– |
XDrawLine(thedisplay, wind, mygc, x0, y0, x0, y1); |
532 |
– |
XDrawLine(thedisplay, wind, mygc, x1, y0, x1, y1); |
533 |
– |
} /* end of revbox */ |
579 |
|
|
535 |
– |
|
580 |
|
avgbox(clr) /* average color over current box */ |
581 |
|
COLOR clr; |
582 |
|
{ |
583 |
+ |
static COLOR lc; |
584 |
+ |
static int ll, lr, lt, lb; |
585 |
|
int left, right, top, bottom; |
586 |
|
int y; |
587 |
|
double d; |
605 |
|
bottom = ymax; |
606 |
|
if (top >= bottom) |
607 |
|
return(-1); |
608 |
+ |
if (left == ll && right == lr && top == lt && bottom == lb) { |
609 |
+ |
copycolor(clr, lc); |
610 |
+ |
return; |
611 |
+ |
} |
612 |
|
for (y = top; y < bottom; y++) { |
613 |
|
if (getscan(y) == -1) |
614 |
|
return(-1); |
619 |
|
} |
620 |
|
d = 1.0/((right-left)*(bottom-top)); |
621 |
|
scalecolor(clr, d); |
622 |
+ |
ll = left; lr = right; lt = top; lb = bottom; |
623 |
+ |
copycolor(lc, clr); |
624 |
|
return(0); |
625 |
|
} |
626 |
|
|
629 |
|
{ |
630 |
|
register unsigned char *dp; |
631 |
|
register int x, err; |
632 |
< |
int y; |
581 |
< |
rgbpixel *inline; |
632 |
> |
int y, errp; |
633 |
|
short *cerr; |
634 |
|
|
635 |
< |
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
636 |
< |
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
586 |
< |
quit("out of memory in getmono"); |
635 |
> |
if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
636 |
> |
quiterr("out of memory in getmono"); |
637 |
|
dp = ourdata - 1; |
638 |
|
for (y = 0; y < ymax; y++) { |
639 |
< |
picreadline3(y, inline); |
639 |
> |
if (getscan(y) < 0) |
640 |
> |
quiterr("seek error in getmono"); |
641 |
> |
normcolrs(scanline, xmax, scale); |
642 |
> |
add2icon(y, scanline); |
643 |
|
err = 0; |
644 |
|
for (x = 0; x < xmax; x++) { |
645 |
|
if (!(x&7)) |
646 |
|
*++dp = 0; |
647 |
< |
err += rgb_bright(&inline[x]) + cerr[x]; |
647 |
> |
errp = err; |
648 |
> |
err += normbright(scanline[x]) + cerr[x]; |
649 |
|
if (err > 127) |
650 |
|
err -= 255; |
651 |
|
else |
652 |
< |
*dp |= 1<<(x&07); |
653 |
< |
cerr[x] = err >>= 1; |
652 |
> |
*dp |= 1<<(7-(x&07)); |
653 |
> |
err /= 3; |
654 |
> |
cerr[x] = err + errp; |
655 |
|
} |
656 |
|
} |
602 |
– |
free((char *)inline); |
657 |
|
free((char *)cerr); |
658 |
|
} |
659 |
|
|
660 |
|
|
661 |
+ |
add2icon(y, scan) /* add a scanline to our icon data */ |
662 |
+ |
int y; |
663 |
+ |
COLR *scan; |
664 |
+ |
{ |
665 |
+ |
static short cerr[ICONSIZ]; |
666 |
+ |
static int ynext; |
667 |
+ |
static char *dp; |
668 |
+ |
register int err; |
669 |
+ |
register int x, ti; |
670 |
+ |
int errp; |
671 |
+ |
|
672 |
+ |
if (iconheight == 0) { /* initialize */ |
673 |
+ |
if (xmax <= ICONSIZ && ymax <= ICONSIZ) { |
674 |
+ |
iconwidth = xmax; |
675 |
+ |
iconheight = ymax; |
676 |
+ |
} else if (xmax > ymax) { |
677 |
+ |
iconwidth = ICONSIZ; |
678 |
+ |
iconheight = ICONSIZ*ymax/xmax; |
679 |
+ |
if (iconheight < 1) |
680 |
+ |
iconheight = 1; |
681 |
+ |
} else { |
682 |
+ |
iconwidth = ICONSIZ*xmax/ymax; |
683 |
+ |
if (iconwidth < 1) |
684 |
+ |
iconwidth = 1; |
685 |
+ |
iconheight = ICONSIZ; |
686 |
+ |
} |
687 |
+ |
ynext = 0; |
688 |
+ |
dp = icondata - 1; |
689 |
+ |
} |
690 |
+ |
if (y < ynext*ymax/iconheight) /* skip this one */ |
691 |
+ |
return; |
692 |
+ |
err = 0; |
693 |
+ |
for (x = 0; x < iconwidth; x++) { |
694 |
+ |
if (!(x&7)) |
695 |
+ |
*++dp = 0; |
696 |
+ |
errp = err; |
697 |
+ |
ti = x*xmax/iconwidth; |
698 |
+ |
err += normbright(scan[ti]) + cerr[x]; |
699 |
+ |
if (err > 127) |
700 |
+ |
err -= 255; |
701 |
+ |
else |
702 |
+ |
*dp |= 1<<(x&07); |
703 |
+ |
err /= 3; |
704 |
+ |
cerr[x] = err + errp; |
705 |
+ |
} |
706 |
+ |
ynext++; |
707 |
+ |
} |
708 |
+ |
|
709 |
+ |
|
710 |
|
getfull() /* get full (24-bit) data */ |
711 |
|
{ |
712 |
|
int y; |
713 |
< |
|
714 |
< |
for (y = 0; y < ymax; y++) |
715 |
< |
picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3)); |
713 |
> |
register unsigned char *dp; |
714 |
> |
register int x; |
715 |
> |
/* set gamma correction */ |
716 |
> |
setcolrgam(gamcor); |
717 |
> |
/* read and convert file */ |
718 |
> |
dp = ourdata; |
719 |
> |
for (y = 0; y < ymax; y++) { |
720 |
> |
if (getscan(y) < 0) |
721 |
> |
quiterr("seek error in getfull"); |
722 |
> |
if (scale) |
723 |
> |
shiftcolrs(scanline, xmax, scale); |
724 |
> |
colrs_gambs(scanline, xmax); |
725 |
> |
add2icon(y, scanline); |
726 |
> |
for (x = 0; x < xmax; x++) { |
727 |
> |
*dp++ = scanline[x][RED]; |
728 |
> |
*dp++ = scanline[x][GRN]; |
729 |
> |
*dp++ = scanline[x][BLU]; |
730 |
> |
} |
731 |
> |
} |
732 |
|
} |
733 |
|
|
734 |
|
|
767 |
|
if (scanpos == NULL || scanpos[y] == -1) |
768 |
|
return(-1); |
769 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
770 |
< |
quit("fseek error"); |
770 |
> |
quiterr("fseek error"); |
771 |
|
cury = y; |
772 |
|
} else if (scanpos != NULL) |
773 |
|
scanpos[y] = ftell(fin); |
790 |
|
quiterr("cannot seek for picreadline"); |
791 |
|
/* convert scanline */ |
792 |
|
normcolrs(scanline, xmax, scale); |
793 |
+ |
add2icon(y, scanline); |
794 |
|
for (i = 0; i < xmax; i++) { |
795 |
|
l3[i].r = scanline[i][RED]; |
796 |
|
l3[i].g = scanline[i][GRN]; |
814 |
|
register int i, val; |
815 |
|
|
816 |
|
for (i = 0; i < 256; i++) { |
817 |
< |
val = pow(i/256.0, 1.0/gamcor) * 256.0; |
817 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
818 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
819 |
|
} |
820 |
|
} |