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 |
+ |
int imready = 0; /* is image up? */ |
85 |
+ |
|
86 |
+ |
GC revgc; /* graphics context with GXinvert */ |
87 |
+ |
|
88 |
|
XRASTER *ourras; /* our stored image */ |
89 |
|
unsigned char *ourdata; /* our image data */ |
90 |
|
|
94 |
|
|
95 |
|
char *geometry = NULL; /* geometry specification */ |
96 |
|
|
97 |
+ |
char icondata[ICONSIZ*ICONSIZ/8]; /* icon bitmap data */ |
98 |
+ |
int iconwidth = 0, iconheight = 0; |
99 |
+ |
|
100 |
|
char *progname; |
101 |
|
|
102 |
|
char errmsg[128]; |
150 |
|
default: |
151 |
|
goto userr; |
152 |
|
} |
153 |
+ |
else if (argv[i][0] == '=') |
154 |
+ |
geometry = argv[i]; |
155 |
|
else |
156 |
|
break; |
157 |
|
|
158 |
< |
if (argc-i == 1) { |
158 |
> |
if (i == argc-1) { |
159 |
|
fname = argv[i]; |
160 |
|
fin = fopen(fname, "r"); |
161 |
|
if (fin == NULL) { |
162 |
|
sprintf(errmsg, "can't open file \"%s\"", fname); |
163 |
|
quiterr(errmsg); |
164 |
|
} |
165 |
< |
} |
165 |
> |
} else if (i != argc) |
166 |
> |
goto userr; |
167 |
|
/* get header */ |
168 |
< |
getheader(fin, headline); |
168 |
> |
getheader(fin, headline, NULL); |
169 |
|
/* get picture dimensions */ |
170 |
< |
if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
171 |
< |
quiterr("bad picture size"); |
170 |
> |
if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
171 |
> |
quiterr("bad picture format"); |
172 |
|
/* set view parameters */ |
173 |
|
if (gotview && setview(&ourview) != NULL) |
174 |
|
gotview = 0; |
192 |
|
{ |
193 |
|
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
194 |
|
register char **an; |
195 |
+ |
char fmt[32]; |
196 |
|
|
197 |
|
if (isexpos(s)) |
198 |
|
exposure *= exposval(s); |
199 |
< |
else |
199 |
> |
else if (isformat(s)) { |
200 |
> |
formatval(fmt, s); |
201 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
202 |
> |
} else |
203 |
|
for (an = altname; *an != NULL; an++) |
204 |
|
if (!strncmp(*an, s, strlen(*an))) { |
205 |
|
if (sscanview(&ourview, s+strlen(*an)) > 0) |
211 |
|
|
212 |
|
init() /* get data and open window */ |
213 |
|
{ |
214 |
+ |
XWMHints ourxwmhints; |
215 |
+ |
XSetWindowAttributes ourwinattr; |
216 |
|
XSizeHints oursizhints; |
217 |
|
register int i; |
218 |
|
|
225 |
|
} |
226 |
|
if ((thedisplay = XOpenDisplay(NULL)) == NULL) |
227 |
|
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"); |
228 |
|
if (maxcolors == 0) { /* get number of available colors */ |
229 |
|
i = DisplayPlanes(thedisplay,ourscreen); |
230 |
|
maxcolors = i > 8 ? 256 : 1<<i; |
231 |
|
if (maxcolors > 4) maxcolors -= 2; |
232 |
|
} |
233 |
+ |
/* store image */ |
234 |
+ |
getras(); |
235 |
+ |
/* open window */ |
236 |
+ |
ourwinattr.border_pixel = ourblack; |
237 |
+ |
ourwinattr.background_pixel = ourwhite; |
238 |
+ |
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
239 |
+ |
0, InputOutput, ourras->visual, |
240 |
+ |
CWBackPixel|CWBorderPixel, &ourwinattr); |
241 |
+ |
if (wind == 0) |
242 |
+ |
quiterr("can't create window"); |
243 |
+ |
width = xmax; |
244 |
+ |
height = ymax; |
245 |
|
fontid = XLoadFont(thedisplay, FONTNAME); |
246 |
|
if (fontid == 0) |
247 |
|
quiterr("can't get font"); |
248 |
|
XSetFont(thedisplay, ourgc, fontid); |
249 |
< |
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
249 |
> |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
250 |
> |
XSetFunction(thedisplay, revgc, GXinvert); |
251 |
|
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
252 |
|
XC_diamond_cross)); |
253 |
|
if (geometry != NULL) { |
254 |
|
bzero((char *)&oursizhints, sizeof(oursizhints)); |
255 |
|
i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, |
256 |
< |
&oursizhints.width, &oursizhints.height); |
257 |
< |
if (i & (XValue|YValue) == (XValue|YValue)) { |
256 |
> |
(unsigned *)&oursizhints.width, |
257 |
> |
(unsigned *)&oursizhints.height); |
258 |
> |
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
259 |
> |
oursizhints.flags |= USSize; |
260 |
> |
else { |
261 |
> |
oursizhints.width = xmax; |
262 |
> |
oursizhints.height = ymax; |
263 |
> |
oursizhints.flags |= PSize; |
264 |
> |
} |
265 |
> |
if ((i&(XValue|YValue)) == (XValue|YValue)) { |
266 |
|
oursizhints.flags |= USPosition; |
267 |
|
if (i & XNegative) |
268 |
< |
oursizhints.x += DisplayWidth(thedisplay,ourscreen)-1; |
268 |
> |
oursizhints.x += DisplayWidth(thedisplay, |
269 |
> |
ourscreen)-1-oursizhints.width-2*BORWIDTH; |
270 |
|
if (i & YNegative) |
271 |
< |
oursizhints.y += DisplayHeight(thedisplay,ourscreen)-1; |
271 |
> |
oursizhints.y += DisplayHeight(thedisplay, |
272 |
> |
ourscreen)-1-oursizhints.height-2*BORWIDTH; |
273 |
|
} |
233 |
– |
if (i & (WidthValue|HeightValue) == (WidthValue|HeightValue)) |
234 |
– |
oursizhints.flags |= USSize; |
274 |
|
XSetNormalHints(thedisplay, wind, &oursizhints); |
275 |
|
} |
276 |
< |
/* store image */ |
277 |
< |
getras(); |
276 |
> |
ourxwmhints.flags = InputHint|IconPixmapHint; |
277 |
> |
ourxwmhints.input = True; |
278 |
> |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
279 |
> |
wind, icondata, iconwidth, iconheight); |
280 |
> |
XSetWMHints(thedisplay, wind, &ourxwmhints); |
281 |
|
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
282 |
|
|ButtonMotionMask|StructureNotifyMask |
283 |
|
|KeyPressMask|ExposureMask); |
284 |
|
XMapWindow(thedisplay, wind); |
285 |
+ |
/* make sure the image is up */ |
286 |
+ |
do |
287 |
+ |
getevent(); |
288 |
+ |
while (!imready); |
289 |
+ |
/* store name as ready signal */ |
290 |
+ |
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
291 |
|
return; |
292 |
|
memerr: |
293 |
|
quiterr("out of memory"); |
322 |
|
getras() /* get raster file */ |
323 |
|
{ |
324 |
|
colormap ourmap; |
277 |
– |
unsigned char rmap[256], gmap[256], bmap[256]; |
325 |
|
XVisualInfo vinfo; |
279 |
– |
register int i; |
326 |
|
|
327 |
|
if (maxcolors <= 2) { /* monochrome */ |
328 |
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
333 |
|
if (ourras == NULL) |
334 |
|
goto fail; |
335 |
|
getmono(); |
336 |
< |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) { |
336 |
> |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
337 |
> |
/* kludge for DirectColor */ |
338 |
> |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
339 |
|
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
340 |
|
if (ourdata == NULL) |
341 |
|
goto fail; |
356 |
|
biq(dither,maxcolors,1,ourmap); |
357 |
|
else |
358 |
|
ciq(dither,maxcolors,1,ourmap); |
359 |
< |
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) |
359 |
> |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
360 |
|
goto fail; |
361 |
|
} |
362 |
|
return; |
363 |
|
fail: |
364 |
< |
quit("could not create raster image"); |
364 |
> |
quiterr("could not create raster image"); |
365 |
|
} |
366 |
|
|
367 |
|
|
394 |
|
break; |
395 |
|
case Expose: |
396 |
|
redraw(e.e.x, e.e.y, e.e.width, e.e.height); |
397 |
+ |
imready++; |
398 |
|
break; |
399 |
|
case ButtonPress: |
400 |
|
if (e.b.state & (ShiftMask|ControlMask)) |
436 |
|
sprintf(buf, "%.3f", intens(cval)/exposure); |
437 |
|
break; |
438 |
|
case 'l': /* luminance */ |
439 |
< |
sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); |
439 |
> |
sprintf(buf, "%.0fL", luminance(cval)/exposure); |
440 |
|
break; |
441 |
|
case 'c': /* color */ |
442 |
|
comp = pow(2.0, (double)scale); |
446 |
|
colval(cval,BLU)*comp); |
447 |
|
break; |
448 |
|
} |
449 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
450 |
< |
buf, strlen(buf)); |
449 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
450 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
451 |
|
return(0); |
452 |
|
case 'i': /* identify (contour) */ |
453 |
|
if (ourras->pixels == NULL) |
471 |
|
XBell(thedisplay, 0); |
472 |
|
return(-1); |
473 |
|
} |
474 |
< |
viewray(rorg, rdir, &ourview, |
474 |
> |
if (viewray(rorg, rdir, &ourview, |
475 |
|
(ekey->x-xoff+.5)/xmax, |
476 |
< |
(ymax-1-ekey->y+yoff+.5)/ymax); |
476 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
477 |
> |
return(-1); |
478 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
479 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
480 |
|
fflush(stdout); |
488 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
489 |
|
scale += n; |
490 |
|
sprintf(buf, "%+d", scale); |
491 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
492 |
< |
buf, strlen(buf)); |
491 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
492 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
493 |
|
XFlush(thedisplay); |
494 |
|
free(ourdata); |
495 |
|
free_raster(ourras); |
518 |
|
XButtonPressedEvent *ebut; |
519 |
|
{ |
520 |
|
union { |
521 |
< |
XEvent u; |
522 |
< |
XButtonReleasedEvent b; |
523 |
< |
} e; |
521 |
> |
XEvent u; |
522 |
> |
XButtonReleasedEvent b; |
523 |
> |
XPointerMovedEvent m; |
524 |
> |
} e; |
525 |
> |
int mxo, myo; |
526 |
|
|
527 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask, &e.u); |
527 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
528 |
> |
while (e.u.type == MotionNotify) { |
529 |
> |
mxo = e.m.x; |
530 |
> |
myo = e.m.y; |
531 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
532 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
533 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
534 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
535 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
536 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
537 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
538 |
> |
} |
539 |
|
xoff += e.b.x - ebut->x; |
540 |
|
yoff += e.b.y - ebut->y; |
541 |
|
XClearWindow(thedisplay, wind); |
578 |
|
revbox(x0, y0, x1, y1) /* draw box with reversed lines */ |
579 |
|
int x0, y0, x1, y1; |
580 |
|
{ |
581 |
< |
static GC mygc = 0; |
581 |
> |
revline(x0, y0, x1, y0); |
582 |
> |
revline(x0, y1, x1, y1); |
583 |
> |
revline(x0, y0, x0, y1); |
584 |
> |
revline(x1, y0, x1, y1); |
585 |
> |
} |
586 |
|
|
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 */ |
587 |
|
|
535 |
– |
|
588 |
|
avgbox(clr) /* average color over current box */ |
589 |
|
COLOR clr; |
590 |
|
{ |
630 |
|
register unsigned char *dp; |
631 |
|
register int x, err; |
632 |
|
int y; |
581 |
– |
rgbpixel *inline; |
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 |
> |
err += normbright(scanline[x]) + cerr[x]; |
648 |
|
if (err > 127) |
649 |
|
err -= 255; |
650 |
|
else |
651 |
< |
*dp |= 1<<(x&07); |
651 |
> |
*dp |= 1<<(7-(x&07)); |
652 |
|
cerr[x] = err >>= 1; |
653 |
|
} |
654 |
|
} |
602 |
– |
free((char *)inline); |
655 |
|
free((char *)cerr); |
656 |
|
} |
657 |
|
|
658 |
|
|
659 |
+ |
add2icon(y, scan) /* add a scanline to our icon data */ |
660 |
+ |
int y; |
661 |
+ |
COLR *scan; |
662 |
+ |
{ |
663 |
+ |
static short cerr[ICONSIZ]; |
664 |
+ |
static int ynext; |
665 |
+ |
static char *dp; |
666 |
+ |
register int err; |
667 |
+ |
register int x, ti; |
668 |
+ |
|
669 |
+ |
if (iconheight == 0) { /* initialize */ |
670 |
+ |
if (xmax <= ICONSIZ && ymax <= ICONSIZ) { |
671 |
+ |
iconwidth = xmax; |
672 |
+ |
iconheight = ymax; |
673 |
+ |
} else if (xmax > ymax) { |
674 |
+ |
iconwidth = ICONSIZ; |
675 |
+ |
iconheight = ICONSIZ*ymax/xmax; |
676 |
+ |
} else { |
677 |
+ |
iconwidth = ICONSIZ*xmax/ymax; |
678 |
+ |
iconheight = ICONSIZ; |
679 |
+ |
} |
680 |
+ |
ynext = 0; |
681 |
+ |
dp = icondata - 1; |
682 |
+ |
} |
683 |
+ |
if (y < ynext*ymax/iconheight) /* skip this one */ |
684 |
+ |
return; |
685 |
+ |
err = 0; |
686 |
+ |
for (x = 0; x < iconwidth; x++) { |
687 |
+ |
if (!(x&7)) |
688 |
+ |
*++dp = 0; |
689 |
+ |
ti = x*xmax/iconwidth; |
690 |
+ |
err += normbright(scan[ti]) + cerr[x]; |
691 |
+ |
if (err > 127) |
692 |
+ |
err -= 255; |
693 |
+ |
else |
694 |
+ |
*dp |= 1<<(x&07); |
695 |
+ |
cerr[x] = err >>= 1; |
696 |
+ |
} |
697 |
+ |
ynext++; |
698 |
+ |
} |
699 |
+ |
|
700 |
+ |
|
701 |
|
getfull() /* get full (24-bit) data */ |
702 |
|
{ |
703 |
|
int y; |
704 |
< |
|
705 |
< |
for (y = 0; y < ymax; y++) |
706 |
< |
picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3)); |
704 |
> |
register unsigned char *dp; |
705 |
> |
register int x; |
706 |
> |
/* set gamma correction */ |
707 |
> |
setcolrgam(gamcor); |
708 |
> |
/* read and convert file */ |
709 |
> |
dp = ourdata; |
710 |
> |
for (y = 0; y < ymax; y++) { |
711 |
> |
if (getscan(y) < 0) |
712 |
> |
quiterr("seek error in getfull"); |
713 |
> |
if (scale) |
714 |
> |
shiftcolrs(scanline, xmax, scale); |
715 |
> |
colrs_gambs(scanline, xmax); |
716 |
> |
add2icon(y, scanline); |
717 |
> |
for (x = 0; x < xmax; x++) { |
718 |
> |
*dp++ = scanline[x][RED]; |
719 |
> |
*dp++ = scanline[x][GRN]; |
720 |
> |
*dp++ = scanline[x][BLU]; |
721 |
> |
} |
722 |
> |
} |
723 |
|
} |
724 |
|
|
725 |
|
|
758 |
|
if (scanpos == NULL || scanpos[y] == -1) |
759 |
|
return(-1); |
760 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
761 |
< |
quit("fseek error"); |
761 |
> |
quiterr("fseek error"); |
762 |
|
cury = y; |
763 |
|
} else if (scanpos != NULL) |
764 |
|
scanpos[y] = ftell(fin); |
781 |
|
quiterr("cannot seek for picreadline"); |
782 |
|
/* convert scanline */ |
783 |
|
normcolrs(scanline, xmax, scale); |
784 |
+ |
add2icon(y, scanline); |
785 |
|
for (i = 0; i < xmax; i++) { |
786 |
|
l3[i].r = scanline[i][RED]; |
787 |
|
l3[i].g = scanline[i][GRN]; |
805 |
|
register int i, val; |
806 |
|
|
807 |
|
for (i = 0; i < 256; i++) { |
808 |
< |
val = pow(i/256.0, 1.0/gamcor) * 256.0; |
808 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
809 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
810 |
|
} |
811 |
|
} |