1 |
/* Copyright (c) 1990 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 "x11icon.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 ourscreen DefaultScreen(thedisplay) |
42 |
#define ourblack BlackPixel(thedisplay,ourscreen) |
43 |
#define ourwhite WhitePixel(thedisplay,ourscreen) |
44 |
#define ourroot RootWindow(thedisplay,ourscreen) |
45 |
#define ourgc DefaultGC(thedisplay,ourscreen) |
46 |
|
47 |
#define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) |
48 |
|
49 |
#define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) |
50 |
|
51 |
double gamcor = 2.2; /* gamma correction */ |
52 |
|
53 |
int dither = 1; /* dither colors? */ |
54 |
int fast = 0; /* keep picture in Pixmap? */ |
55 |
|
56 |
Window wind = 0; /* our output window */ |
57 |
Font fontid; /* our font */ |
58 |
|
59 |
int maxcolors = 0; /* maximum colors */ |
60 |
int greyscale = 0; /* in grey */ |
61 |
|
62 |
int scale = 0; /* scalefactor; power of two */ |
63 |
|
64 |
int xoff = 0; /* x image offset */ |
65 |
int yoff = 0; /* y image offset */ |
66 |
|
67 |
VIEW ourview = STDVIEW; /* image view parameters */ |
68 |
int gotview = 0; /* got parameters from file */ |
69 |
|
70 |
COLR *scanline; /* scan line buffer */ |
71 |
|
72 |
int xmax, ymax; /* picture resolution */ |
73 |
int width, height; /* window size */ |
74 |
char *fname = NULL; /* input file name */ |
75 |
FILE *fin = stdin; /* input file */ |
76 |
long *scanpos = NULL; /* scan line positions in file */ |
77 |
int cury = 0; /* current scan location */ |
78 |
|
79 |
double exposure = 1.0; /* exposure compensation used */ |
80 |
|
81 |
int wrongformat = 0; /* input in another format? */ |
82 |
|
83 |
GC revgc; /* graphics context with GXinvert */ |
84 |
|
85 |
XRASTER *ourras; /* our stored image */ |
86 |
unsigned char *ourdata; /* our image data */ |
87 |
|
88 |
struct { |
89 |
int xmin, ymin, xsiz, ysiz; |
90 |
} box = {0, 0, 0, 0}; /* current box */ |
91 |
|
92 |
char *geometry = NULL; /* geometry specification */ |
93 |
|
94 |
char *progname; |
95 |
|
96 |
char errmsg[128]; |
97 |
|
98 |
extern long ftell(); |
99 |
|
100 |
extern char *malloc(), *calloc(); |
101 |
|
102 |
extern double atof(), pow(), log(); |
103 |
|
104 |
Display *thedisplay; |
105 |
|
106 |
main(argc, argv) |
107 |
int argc; |
108 |
char *argv[]; |
109 |
{ |
110 |
int headline(); |
111 |
int i; |
112 |
|
113 |
progname = argv[0]; |
114 |
|
115 |
for (i = 1; i < argc; i++) |
116 |
if (argv[i][0] == '-') |
117 |
switch (argv[i][1]) { |
118 |
case 'c': |
119 |
maxcolors = atoi(argv[++i]); |
120 |
break; |
121 |
case 'b': |
122 |
greyscale = !greyscale; |
123 |
break; |
124 |
case 'm': |
125 |
maxcolors = 2; |
126 |
break; |
127 |
case 'd': |
128 |
dither = !dither; |
129 |
break; |
130 |
case 'f': |
131 |
fast = !fast; |
132 |
break; |
133 |
case 'e': |
134 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
135 |
goto userr; |
136 |
scale = atoi(argv[++i]); |
137 |
break; |
138 |
case 'g': |
139 |
if (!strcmp(argv[i], "-geometry")) |
140 |
geometry = argv[++i]; |
141 |
else |
142 |
gamcor = atof(argv[++i]); |
143 |
break; |
144 |
default: |
145 |
goto userr; |
146 |
} |
147 |
else if (argv[i][0] == '=') |
148 |
geometry = argv[i]; |
149 |
else |
150 |
break; |
151 |
|
152 |
if (i == argc-1) { |
153 |
fname = argv[i]; |
154 |
fin = fopen(fname, "r"); |
155 |
if (fin == NULL) { |
156 |
sprintf(errmsg, "can't open file \"%s\"", fname); |
157 |
quiterr(errmsg); |
158 |
} |
159 |
} else if (i != argc) |
160 |
goto userr; |
161 |
/* get header */ |
162 |
getheader(fin, headline, NULL); |
163 |
/* get picture dimensions */ |
164 |
if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
165 |
quiterr("bad picture format"); |
166 |
/* set view parameters */ |
167 |
if (gotview && setview(&ourview) != NULL) |
168 |
gotview = 0; |
169 |
if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
170 |
quiterr("out of memory"); |
171 |
|
172 |
init(); /* get file and open window */ |
173 |
|
174 |
for ( ; ; ) |
175 |
getevent(); /* main loop */ |
176 |
userr: |
177 |
fprintf(stderr, |
178 |
"Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
179 |
progname); |
180 |
quit(1); |
181 |
} |
182 |
|
183 |
|
184 |
headline(s) /* get relevant info from header */ |
185 |
char *s; |
186 |
{ |
187 |
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
188 |
register char **an; |
189 |
char fmt[32]; |
190 |
|
191 |
if (isexpos(s)) |
192 |
exposure *= exposval(s); |
193 |
else if (isformat(s)) { |
194 |
formatval(fmt, s); |
195 |
wrongformat = strcmp(fmt, COLRFMT); |
196 |
} else |
197 |
for (an = altname; *an != NULL; an++) |
198 |
if (!strncmp(*an, s, strlen(*an))) { |
199 |
if (sscanview(&ourview, s+strlen(*an)) > 0) |
200 |
gotview++; |
201 |
return; |
202 |
} |
203 |
} |
204 |
|
205 |
|
206 |
init() /* get data and open window */ |
207 |
{ |
208 |
XWMHints ourxwmhints; |
209 |
XSetWindowAttributes ourwinattr; |
210 |
XSizeHints oursizhints; |
211 |
register int i; |
212 |
|
213 |
if (fname != NULL) { |
214 |
scanpos = (long *)malloc(ymax*sizeof(long)); |
215 |
if (scanpos == NULL) |
216 |
goto memerr; |
217 |
for (i = 0; i < ymax; i++) |
218 |
scanpos[i] = -1; |
219 |
} |
220 |
if ((thedisplay = XOpenDisplay(NULL)) == NULL) |
221 |
quiterr("can't open display; DISPLAY variable set?"); |
222 |
if (maxcolors == 0) { /* get number of available colors */ |
223 |
i = DisplayPlanes(thedisplay,ourscreen); |
224 |
maxcolors = i > 8 ? 256 : 1<<i; |
225 |
if (maxcolors > 4) maxcolors -= 2; |
226 |
} |
227 |
/* store image */ |
228 |
getras(); |
229 |
/* open window */ |
230 |
ourwinattr.border_pixel = ourblack; |
231 |
ourwinattr.background_pixel = ourwhite; |
232 |
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
233 |
0, InputOutput, ourras->visual, |
234 |
CWBackPixel|CWBorderPixel, &ourwinattr); |
235 |
if (wind == 0) |
236 |
quiterr("can't create window"); |
237 |
width = xmax; |
238 |
height = ymax; |
239 |
fontid = XLoadFont(thedisplay, FONTNAME); |
240 |
if (fontid == 0) |
241 |
quiterr("can't get font"); |
242 |
XSetFont(thedisplay, ourgc, fontid); |
243 |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
244 |
XSetFunction(thedisplay, revgc, GXinvert); |
245 |
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
246 |
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
247 |
XC_diamond_cross)); |
248 |
if (geometry != NULL) { |
249 |
bzero((char *)&oursizhints, sizeof(oursizhints)); |
250 |
i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, |
251 |
(unsigned *)&oursizhints.width, |
252 |
(unsigned *)&oursizhints.height); |
253 |
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
254 |
oursizhints.flags |= USSize; |
255 |
else { |
256 |
oursizhints.width = xmax; |
257 |
oursizhints.height = ymax; |
258 |
oursizhints.flags |= PSize; |
259 |
} |
260 |
if ((i&(XValue|YValue)) == (XValue|YValue)) { |
261 |
oursizhints.flags |= USPosition; |
262 |
if (i & XNegative) |
263 |
oursizhints.x += DisplayWidth(thedisplay, |
264 |
ourscreen)-1-oursizhints.width-2*BORWIDTH; |
265 |
if (i & YNegative) |
266 |
oursizhints.y += DisplayHeight(thedisplay, |
267 |
ourscreen)-1-oursizhints.height-2*BORWIDTH; |
268 |
} |
269 |
XSetNormalHints(thedisplay, wind, &oursizhints); |
270 |
} |
271 |
ourxwmhints.flags = InputHint|IconPixmapHint; |
272 |
ourxwmhints.input = True; |
273 |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
274 |
wind, x11icon_bits, x11icon_width, x11icon_height); |
275 |
XSetWMHints(thedisplay, wind, &ourxwmhints); |
276 |
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
277 |
|ButtonMotionMask|StructureNotifyMask |
278 |
|KeyPressMask|ExposureMask); |
279 |
XMapWindow(thedisplay, wind); |
280 |
return; |
281 |
memerr: |
282 |
quiterr("out of memory"); |
283 |
} /* end of init */ |
284 |
|
285 |
|
286 |
quiterr(err) /* print message and exit */ |
287 |
char *err; |
288 |
{ |
289 |
if (err != NULL) { |
290 |
fprintf(stderr, "%s: %s\n", progname, err); |
291 |
exit(1); |
292 |
} |
293 |
exit(0); |
294 |
} |
295 |
|
296 |
|
297 |
eputs(s) |
298 |
char *s; |
299 |
{ |
300 |
fputs(s, stderr); |
301 |
} |
302 |
|
303 |
|
304 |
quit(code) |
305 |
int code; |
306 |
{ |
307 |
exit(code); |
308 |
} |
309 |
|
310 |
|
311 |
getras() /* get raster file */ |
312 |
{ |
313 |
colormap ourmap; |
314 |
XVisualInfo vinfo; |
315 |
|
316 |
if (maxcolors <= 2) { /* monochrome */ |
317 |
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
318 |
if (ourdata == NULL) |
319 |
goto fail; |
320 |
ourras = make_raster(thedisplay, ourscreen, 1, ourdata, |
321 |
xmax, ymax, 8); |
322 |
if (ourras == NULL) |
323 |
goto fail; |
324 |
getmono(); |
325 |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
326 |
/* kludge for DirectColor */ |
327 |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
328 |
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
329 |
if (ourdata == NULL) |
330 |
goto fail; |
331 |
ourras = make_raster(thedisplay, ourscreen, 24, ourdata, |
332 |
xmax, ymax, 8); |
333 |
if (ourras == NULL) |
334 |
goto fail; |
335 |
getfull(); |
336 |
} else { |
337 |
ourdata = (unsigned char *)malloc(xmax*ymax); |
338 |
if (ourdata == NULL) |
339 |
goto fail; |
340 |
ourras = make_raster(thedisplay, ourscreen, 8, ourdata, |
341 |
xmax, ymax, 8); |
342 |
if (ourras == NULL) |
343 |
goto fail; |
344 |
if (greyscale) |
345 |
biq(dither,maxcolors,1,ourmap); |
346 |
else |
347 |
ciq(dither,maxcolors,1,ourmap); |
348 |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
349 |
goto fail; |
350 |
} |
351 |
return; |
352 |
fail: |
353 |
quiterr("could not create raster image"); |
354 |
} |
355 |
|
356 |
|
357 |
getevent() /* process the next event */ |
358 |
{ |
359 |
union { |
360 |
XEvent u; |
361 |
XConfigureEvent c; |
362 |
XExposeEvent e; |
363 |
XButtonPressedEvent b; |
364 |
XKeyPressedEvent k; |
365 |
} e; |
366 |
|
367 |
XNextEvent(thedisplay, &e.u); |
368 |
switch (e.u.type) { |
369 |
case KeyPress: |
370 |
docom(&e.k); |
371 |
break; |
372 |
case ConfigureNotify: |
373 |
width = e.c.width; |
374 |
height = e.c.height; |
375 |
break; |
376 |
case MapNotify: |
377 |
map_rcolors(ourras, wind); |
378 |
if (fast) |
379 |
make_rpixmap(ourras); |
380 |
break; |
381 |
case UnmapNotify: |
382 |
unmap_rcolors(ourras); |
383 |
break; |
384 |
case Expose: |
385 |
redraw(e.e.x, e.e.y, e.e.width, e.e.height); |
386 |
break; |
387 |
case ButtonPress: |
388 |
if (e.b.state & (ShiftMask|ControlMask)) |
389 |
moveimage(&e.b); |
390 |
else |
391 |
getbox(&e.b); |
392 |
break; |
393 |
} |
394 |
} |
395 |
|
396 |
|
397 |
docom(ekey) /* execute command */ |
398 |
XKeyPressedEvent *ekey; |
399 |
{ |
400 |
char buf[80]; |
401 |
COLOR cval; |
402 |
XColor cvx; |
403 |
int com, n; |
404 |
double comp; |
405 |
FVECT rorg, rdir; |
406 |
|
407 |
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); |
408 |
if (n == 0) |
409 |
return(0); |
410 |
com = buf[0]; |
411 |
switch (com) { /* interpret command */ |
412 |
case 'q': |
413 |
case CTRL(D): /* quit */ |
414 |
quit(0); |
415 |
case '\n': |
416 |
case '\r': |
417 |
case 'l': |
418 |
case 'c': /* value */ |
419 |
if (avgbox(cval) == -1) |
420 |
return(-1); |
421 |
switch (com) { |
422 |
case '\n': |
423 |
case '\r': /* radiance */ |
424 |
sprintf(buf, "%.3f", intens(cval)/exposure); |
425 |
break; |
426 |
case 'l': /* luminance */ |
427 |
sprintf(buf, "%.0fn", luminance(cval)/exposure); |
428 |
break; |
429 |
case 'c': /* color */ |
430 |
comp = pow(2.0, (double)scale); |
431 |
sprintf(buf, "(%.2f,%.2f,%.2f)", |
432 |
colval(cval,RED)*comp, |
433 |
colval(cval,GRN)*comp, |
434 |
colval(cval,BLU)*comp); |
435 |
break; |
436 |
} |
437 |
XDrawImageString(thedisplay, wind, ourgc, |
438 |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
439 |
return(0); |
440 |
case 'i': /* identify (contour) */ |
441 |
if (ourras->pixels == NULL) |
442 |
return(-1); |
443 |
n = ourdata[ekey->x-xoff+xmax*(ekey->y-yoff)]; |
444 |
n = ourras->pmap[n]; |
445 |
cvx.pixel = ourras->cdefs[n].pixel; |
446 |
cvx.red = random() & 65535; |
447 |
cvx.green = random() & 65535; |
448 |
cvx.blue = random() & 65535; |
449 |
cvx.flags = DoRed|DoGreen|DoBlue; |
450 |
XStoreColor(thedisplay, ourras->cmap, &cvx); |
451 |
return(0); |
452 |
case 'p': /* position */ |
453 |
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
454 |
XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y, |
455 |
buf, strlen(buf)); |
456 |
return(0); |
457 |
case 't': /* trace */ |
458 |
if (!gotview) { |
459 |
XBell(thedisplay, 0); |
460 |
return(-1); |
461 |
} |
462 |
if (viewray(rorg, rdir, &ourview, |
463 |
(ekey->x-xoff+.5)/xmax, |
464 |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
465 |
return(-1); |
466 |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
467 |
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
468 |
fflush(stdout); |
469 |
return(0); |
470 |
case '=': /* adjust exposure */ |
471 |
if (avgbox(cval) == -1) |
472 |
return(-1); |
473 |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
474 |
if (n == 0) |
475 |
return(0); |
476 |
scale_rcolors(ourras, pow(2.0, (double)n)); |
477 |
scale += n; |
478 |
sprintf(buf, "%+d", scale); |
479 |
XDrawImageString(thedisplay, wind, ourgc, |
480 |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
481 |
XFlush(thedisplay); |
482 |
free(ourdata); |
483 |
free_raster(ourras); |
484 |
getras(); |
485 |
/* fall through */ |
486 |
case CTRL(R): /* redraw */ |
487 |
case CTRL(L): |
488 |
unmap_rcolors(ourras); |
489 |
XClearWindow(thedisplay, wind); |
490 |
map_rcolors(ourras, wind); |
491 |
if (fast) |
492 |
make_rpixmap(ourras); |
493 |
redraw(0, 0, width, height); |
494 |
return(0); |
495 |
case ' ': /* clear */ |
496 |
redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); |
497 |
return(0); |
498 |
default: |
499 |
XBell(thedisplay, 0); |
500 |
return(-1); |
501 |
} |
502 |
} |
503 |
|
504 |
|
505 |
moveimage(ebut) /* shift the image */ |
506 |
XButtonPressedEvent *ebut; |
507 |
{ |
508 |
union { |
509 |
XEvent u; |
510 |
XButtonReleasedEvent b; |
511 |
XPointerMovedEvent m; |
512 |
} e; |
513 |
int mxo, myo; |
514 |
|
515 |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
516 |
while (e.u.type == MotionNotify) { |
517 |
mxo = e.m.x; |
518 |
myo = e.m.y; |
519 |
revline(ebut->x, ebut->y, mxo, myo); |
520 |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
521 |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
522 |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
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 |
} |
527 |
xoff += e.b.x - ebut->x; |
528 |
yoff += e.b.y - ebut->y; |
529 |
XClearWindow(thedisplay, wind); |
530 |
redraw(0, 0, width, height); |
531 |
} |
532 |
|
533 |
|
534 |
getbox(ebut) /* get new box */ |
535 |
XButtonPressedEvent *ebut; |
536 |
{ |
537 |
union { |
538 |
XEvent u; |
539 |
XButtonReleasedEvent b; |
540 |
XPointerMovedEvent m; |
541 |
} e; |
542 |
|
543 |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
544 |
while (e.u.type == MotionNotify) { |
545 |
revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); |
546 |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
547 |
revbox(ebut->x, ebut->y, box.xmin, box.ymin); |
548 |
} |
549 |
box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); |
550 |
box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y); |
551 |
if (box.xmin > ebut->x) { |
552 |
box.xsiz = box.xmin - ebut->x + 1; |
553 |
box.xmin = ebut->x; |
554 |
} else { |
555 |
box.xsiz = ebut->x - box.xmin + 1; |
556 |
} |
557 |
if (box.ymin > ebut->y) { |
558 |
box.ysiz = box.ymin - ebut->y + 1; |
559 |
box.ymin = ebut->y; |
560 |
} else { |
561 |
box.ysiz = ebut->y - box.ymin + 1; |
562 |
} |
563 |
} |
564 |
|
565 |
|
566 |
revbox(x0, y0, x1, y1) /* draw box with reversed lines */ |
567 |
int x0, y0, x1, y1; |
568 |
{ |
569 |
revline(x0, y0, x1, y0); |
570 |
revline(x0, y1, x1, y1); |
571 |
revline(x0, y0, x0, y1); |
572 |
revline(x1, y0, x1, y1); |
573 |
} |
574 |
|
575 |
|
576 |
avgbox(clr) /* average color over current box */ |
577 |
COLOR clr; |
578 |
{ |
579 |
int left, right, top, bottom; |
580 |
int y; |
581 |
double d; |
582 |
COLOR ctmp; |
583 |
register int x; |
584 |
|
585 |
setcolor(clr, 0.0, 0.0, 0.0); |
586 |
left = box.xmin - xoff; |
587 |
right = left + box.xsiz; |
588 |
if (left < 0) |
589 |
left = 0; |
590 |
if (right > xmax) |
591 |
right = xmax; |
592 |
if (left >= right) |
593 |
return(-1); |
594 |
top = box.ymin - yoff; |
595 |
bottom = top + box.ysiz; |
596 |
if (top < 0) |
597 |
top = 0; |
598 |
if (bottom > ymax) |
599 |
bottom = ymax; |
600 |
if (top >= bottom) |
601 |
return(-1); |
602 |
for (y = top; y < bottom; y++) { |
603 |
if (getscan(y) == -1) |
604 |
return(-1); |
605 |
for (x = left; x < right; x++) { |
606 |
colr_color(ctmp, scanline[x]); |
607 |
addcolor(clr, ctmp); |
608 |
} |
609 |
} |
610 |
d = 1.0/((right-left)*(bottom-top)); |
611 |
scalecolor(clr, d); |
612 |
return(0); |
613 |
} |
614 |
|
615 |
|
616 |
getmono() /* get monochrome data */ |
617 |
{ |
618 |
register unsigned char *dp; |
619 |
register int x, err; |
620 |
int y; |
621 |
short *cerr; |
622 |
|
623 |
if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
624 |
quiterr("out of memory in getmono"); |
625 |
dp = ourdata - 1; |
626 |
for (y = 0; y < ymax; y++) { |
627 |
if (getscan(y) < 0) |
628 |
quiterr("seek error in getmono"); |
629 |
normcolrs(scanline, xmax, scale); |
630 |
err = 0; |
631 |
for (x = 0; x < xmax; x++) { |
632 |
if (!(x&7)) |
633 |
*++dp = 0; |
634 |
err += normbright(scanline[x]) + cerr[x]; |
635 |
if (err > 127) |
636 |
err -= 255; |
637 |
else |
638 |
*dp |= 1<<(7-(x&07)); |
639 |
cerr[x] = err >>= 1; |
640 |
} |
641 |
} |
642 |
free((char *)cerr); |
643 |
} |
644 |
|
645 |
|
646 |
getfull() /* get full (24-bit) data */ |
647 |
{ |
648 |
int y; |
649 |
register unsigned char *dp; |
650 |
register int x; |
651 |
/* set gamma correction */ |
652 |
setcolrgam(gamcor); |
653 |
/* read and convert file */ |
654 |
dp = ourdata; |
655 |
for (y = 0; y < ymax; y++) { |
656 |
if (getscan(y) < 0) |
657 |
quiterr("seek error in getfull"); |
658 |
if (scale) |
659 |
shiftcolrs(scanline, xmax, scale); |
660 |
colrs_gambs(scanline, xmax); |
661 |
for (x = 0; x < xmax; x++) { |
662 |
*dp++ = scanline[x][RED]; |
663 |
*dp++ = scanline[x][GRN]; |
664 |
*dp++ = scanline[x][BLU]; |
665 |
} |
666 |
} |
667 |
} |
668 |
|
669 |
|
670 |
scale_rcolors(xr, sf) /* scale color map */ |
671 |
register XRASTER *xr; |
672 |
double sf; |
673 |
{ |
674 |
register int i; |
675 |
long maxv; |
676 |
|
677 |
if (xr->pixels == NULL) |
678 |
return; |
679 |
|
680 |
sf = pow(sf, 1.0/gamcor); |
681 |
maxv = 65535/sf; |
682 |
|
683 |
for (i = xr->ncolors; i--; ) { |
684 |
xr->cdefs[i].red = xr->cdefs[i].red > maxv ? |
685 |
65535 : |
686 |
xr->cdefs[i].red * sf; |
687 |
xr->cdefs[i].green = xr->cdefs[i].green > maxv ? |
688 |
65535 : |
689 |
xr->cdefs[i].green * sf; |
690 |
xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? |
691 |
65535 : |
692 |
xr->cdefs[i].blue * sf; |
693 |
} |
694 |
XStoreColors(thedisplay, xr->cmap, xr->cdefs, xr->ncolors); |
695 |
} |
696 |
|
697 |
|
698 |
getscan(y) |
699 |
int y; |
700 |
{ |
701 |
if (y != cury) { |
702 |
if (scanpos == NULL || scanpos[y] == -1) |
703 |
return(-1); |
704 |
if (fseek(fin, scanpos[y], 0) == -1) |
705 |
quiterr("fseek error"); |
706 |
cury = y; |
707 |
} else if (scanpos != NULL) |
708 |
scanpos[y] = ftell(fin); |
709 |
|
710 |
if (freadcolrs(scanline, xmax, fin) < 0) |
711 |
quiterr("read error"); |
712 |
|
713 |
cury++; |
714 |
return(0); |
715 |
} |
716 |
|
717 |
|
718 |
picreadline3(y, l3) /* read in 3-byte scanline */ |
719 |
int y; |
720 |
register rgbpixel *l3; |
721 |
{ |
722 |
register int i; |
723 |
/* read scanline */ |
724 |
if (getscan(y) < 0) |
725 |
quiterr("cannot seek for picreadline"); |
726 |
/* convert scanline */ |
727 |
normcolrs(scanline, xmax, scale); |
728 |
for (i = 0; i < xmax; i++) { |
729 |
l3[i].r = scanline[i][RED]; |
730 |
l3[i].g = scanline[i][GRN]; |
731 |
l3[i].b = scanline[i][BLU]; |
732 |
} |
733 |
} |
734 |
|
735 |
|
736 |
picwriteline(y, l) /* add 8-bit scanline to image */ |
737 |
int y; |
738 |
pixel *l; |
739 |
{ |
740 |
bcopy((char *)l, (char *)ourdata+y*xmax, xmax); |
741 |
} |
742 |
|
743 |
|
744 |
picreadcm(map) /* do gamma correction */ |
745 |
colormap map; |
746 |
{ |
747 |
extern double pow(); |
748 |
register int i, val; |
749 |
|
750 |
for (i = 0; i < 256; i++) { |
751 |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
752 |
map[0][i] = map[1][i] = map[2][i] = val; |
753 |
} |
754 |
} |
755 |
|
756 |
|
757 |
picwritecm(map) /* handled elsewhere */ |
758 |
colormap map; |
759 |
{ |
760 |
#ifdef DEBUG |
761 |
register int i; |
762 |
|
763 |
for (i = 0; i < 256; i++) |
764 |
printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); |
765 |
#endif |
766 |
} |