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"; |
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'-'@') |
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) |
44 |
– |
#define ourgc DefaultGC(thedisplay,ourscreen) |
47 |
|
|
48 |
|
#define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) |
49 |
|
|
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 |
|
|
72 |
|
|
73 |
|
COLR *scanline; /* scan line buffer */ |
74 |
|
|
75 |
< |
int xmax, ymax; /* picture resolution */ |
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 */ |
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 |
< |
XRASTER *ourras; /* our stored image */ |
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 { |
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]; |
109 |
|
|
110 |
|
extern char *malloc(), *calloc(); |
111 |
|
|
112 |
< |
extern double atof(), pow(), log(); |
112 |
> |
extern double pow(), log(); |
113 |
|
|
114 |
|
Display *thedisplay; |
115 |
|
|
135 |
|
maxcolors = 2; |
136 |
|
break; |
137 |
|
case 'd': |
138 |
< |
dither = !dither; |
138 |
> |
if (argv[i][2] == 'i') |
139 |
> |
dispname = argv[++i]; |
140 |
> |
else |
141 |
> |
dither = !dither; |
142 |
|
break; |
143 |
|
case 'f': |
144 |
|
fast = !fast; |
149 |
|
scale = atoi(argv[++i]); |
150 |
|
break; |
151 |
|
case 'g': |
152 |
< |
if (!strcmp(argv[i], "-geometry")) |
152 |
> |
if (argv[i][2] == 'e') |
153 |
|
geometry = argv[++i]; |
154 |
|
else |
155 |
|
gamcor = atof(argv[++i]); |
166 |
|
fname = argv[i]; |
167 |
|
fin = fopen(fname, "r"); |
168 |
|
if (fin == NULL) { |
169 |
< |
sprintf(errmsg, "can't open file \"%s\"", fname); |
169 |
> |
sprintf(errmsg, "cannot open file \"%s\"", fname); |
170 |
|
quiterr(errmsg); |
171 |
|
} |
172 |
|
} else if (i != argc) |
173 |
|
goto userr; |
174 |
|
/* get header */ |
175 |
< |
getheader(fin, headline); |
175 |
> |
getheader(fin, headline, NULL); |
176 |
|
/* get picture dimensions */ |
177 |
< |
if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
178 |
< |
quiterr("bad picture size"); |
177 |
> |
if (wrongformat || !fgetsresolu(&inpres, fin)) |
178 |
> |
quiterr("bad picture format"); |
179 |
> |
xmax = scanlen(&inpres); |
180 |
> |
ymax = numscans(&inpres); |
181 |
|
/* set view parameters */ |
182 |
|
if (gotview && setview(&ourview) != NULL) |
183 |
|
gotview = 0; |
190 |
|
getevent(); /* main loop */ |
191 |
|
userr: |
192 |
|
fprintf(stderr, |
193 |
< |
"Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
193 |
> |
"Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
194 |
|
progname); |
195 |
|
quit(1); |
196 |
|
} |
199 |
|
headline(s) /* get relevant info from header */ |
200 |
|
char *s; |
201 |
|
{ |
202 |
< |
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
185 |
< |
register char **an; |
202 |
> |
char fmt[32]; |
203 |
|
|
204 |
|
if (isexpos(s)) |
205 |
|
exposure *= exposval(s); |
206 |
< |
else |
207 |
< |
for (an = altname; *an != NULL; an++) |
208 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
209 |
< |
if (sscanview(&ourview, s+strlen(*an)) > 0) |
210 |
< |
gotview++; |
194 |
< |
return; |
195 |
< |
} |
206 |
> |
else if (isformat(s)) { |
207 |
> |
formatval(fmt, s); |
208 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
209 |
> |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
210 |
> |
gotview++; |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
init() /* get data and open window */ |
215 |
|
{ |
216 |
+ |
XWMHints ourxwmhints; |
217 |
|
XSetWindowAttributes ourwinattr; |
218 |
< |
XSizeHints oursizhints; |
219 |
< |
register int i; |
218 |
> |
XSizeHints oursizhints; |
219 |
> |
register int i; |
220 |
|
|
221 |
|
if (fname != NULL) { |
222 |
|
scanpos = (long *)malloc(ymax*sizeof(long)); |
225 |
|
for (i = 0; i < ymax; i++) |
226 |
|
scanpos[i] = -1; |
227 |
|
} |
228 |
< |
if ((thedisplay = XOpenDisplay(NULL)) == NULL) |
229 |
< |
quiterr("can't open display; DISPLAY variable set?"); |
230 |
< |
if (maxcolors == 0) { /* get number of available colors */ |
231 |
< |
i = DisplayPlanes(thedisplay,ourscreen); |
216 |
< |
maxcolors = i > 8 ? 256 : 1<<i; |
217 |
< |
if (maxcolors > 4) maxcolors -= 2; |
218 |
< |
} |
228 |
> |
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
229 |
> |
quiterr("cannot open display"); |
230 |
> |
/* get best visual for default screen */ |
231 |
> |
getbestvis(); |
232 |
|
/* store image */ |
233 |
|
getras(); |
234 |
|
/* open window */ |
235 |
|
ourwinattr.border_pixel = ourblack; |
236 |
|
ourwinattr.background_pixel = ourwhite; |
237 |
+ |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, |
238 |
+ |
ourvis.visual, AllocNone); |
239 |
|
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
240 |
< |
0, InputOutput, ourras->visual, |
241 |
< |
CWBackPixel|CWBorderPixel, &ourwinattr); |
240 |
> |
ourvis.depth, InputOutput, ourvis.visual, |
241 |
> |
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
242 |
|
if (wind == 0) |
243 |
< |
quiterr("can't create window"); |
243 |
> |
quiterr("cannot create window"); |
244 |
|
width = xmax; |
245 |
|
height = ymax; |
246 |
+ |
ourgc = XCreateGC(thedisplay, wind, 0, 0); |
247 |
+ |
XSetState(thedisplay, ourgc, BlackPixel(thedisplay,ourscreen), |
248 |
+ |
WhitePixel(thedisplay,ourscreen), GXcopy, AllPlanes); |
249 |
+ |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
250 |
+ |
XSetFunction(thedisplay, revgc, GXinvert); |
251 |
|
fontid = XLoadFont(thedisplay, FONTNAME); |
252 |
|
if (fontid == 0) |
253 |
< |
quiterr("can't get font"); |
253 |
> |
quiterr("cannot get font"); |
254 |
|
XSetFont(thedisplay, ourgc, fontid); |
235 |
– |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
236 |
– |
XSetFunction(thedisplay, revgc, GXinvert); |
237 |
– |
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
255 |
|
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
256 |
|
XC_diamond_cross)); |
257 |
+ |
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
258 |
|
if (geometry != NULL) { |
259 |
|
bzero((char *)&oursizhints, sizeof(oursizhints)); |
260 |
|
i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, |
261 |
< |
&oursizhints.width, &oursizhints.height); |
261 |
> |
(unsigned *)&oursizhints.width, |
262 |
> |
(unsigned *)&oursizhints.height); |
263 |
|
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
264 |
|
oursizhints.flags |= USSize; |
265 |
|
else { |
278 |
|
} |
279 |
|
XSetNormalHints(thedisplay, wind, &oursizhints); |
280 |
|
} |
281 |
+ |
ourxwmhints.flags = InputHint|IconPixmapHint; |
282 |
+ |
ourxwmhints.input = True; |
283 |
+ |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
284 |
+ |
wind, icondata, iconwidth, iconheight); |
285 |
+ |
XSetWMHints(thedisplay, wind, &ourxwmhints); |
286 |
|
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
287 |
|
|ButtonMotionMask|StructureNotifyMask |
288 |
|
|KeyPressMask|ExposureMask); |
318 |
|
} |
319 |
|
|
320 |
|
|
321 |
+ |
static int |
322 |
+ |
viscmp(v1,v2) /* compare visual to see which is better, descending */ |
323 |
+ |
register XVisualInfo *v1, *v2; |
324 |
+ |
{ |
325 |
+ |
int bad1 = 0, bad2 = 0; |
326 |
+ |
register int *rp; |
327 |
+ |
|
328 |
+ |
if (v1->class == v2->class) { |
329 |
+ |
if (v1->class == TrueColor || v1->class == DirectColor) { |
330 |
+ |
/* prefer 24-bit to 32-bit */ |
331 |
+ |
if (v1->depth == 24 && v2->depth == 32) |
332 |
+ |
return(-1); |
333 |
+ |
if (v1->depth == 32 && v2->depth == 24) |
334 |
+ |
return(1); |
335 |
+ |
return(0); |
336 |
+ |
} |
337 |
+ |
/* don't be too greedy */ |
338 |
+ |
if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth) |
339 |
+ |
return(v1->depth - v2->depth); |
340 |
+ |
return(v2->depth - v1->depth); |
341 |
+ |
} |
342 |
+ |
/* prefer Pseudo when < 24-bit */ |
343 |
+ |
if ((v1->class == TrueColor || v1->class == DirectColor) && |
344 |
+ |
v1->depth < 24) |
345 |
+ |
bad1 = 1; |
346 |
+ |
if ((v2->class == TrueColor || v2->class == DirectColor) && |
347 |
+ |
v2->depth < 24) |
348 |
+ |
bad2 = -1; |
349 |
+ |
if (bad1 | bad2) |
350 |
+ |
return(bad1+bad2); |
351 |
+ |
/* otherwise, use class ranking */ |
352 |
+ |
for (rp = ourrank; *rp != -1; rp++) { |
353 |
+ |
if (v1->class == *rp) |
354 |
+ |
return(-1); |
355 |
+ |
if (v2->class == *rp) |
356 |
+ |
return(1); |
357 |
+ |
} |
358 |
+ |
return(0); |
359 |
+ |
} |
360 |
+ |
|
361 |
+ |
|
362 |
+ |
getbestvis() /* get the best visual for this screen */ |
363 |
+ |
{ |
364 |
+ |
#ifdef DEBUG |
365 |
+ |
static char vistype[][12] = { |
366 |
+ |
"StaticGray", |
367 |
+ |
"GrayScale", |
368 |
+ |
"StaticColor", |
369 |
+ |
"PseudoColor", |
370 |
+ |
"TrueColor", |
371 |
+ |
"DirectColor" |
372 |
+ |
}; |
373 |
+ |
#endif |
374 |
+ |
static int rankings[3][6] = { |
375 |
+ |
{TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1}, |
376 |
+ |
{PseudoColor,GrayScale,StaticGray,-1}, |
377 |
+ |
{PseudoColor,GrayScale,StaticGray,-1} |
378 |
+ |
}; |
379 |
+ |
XVisualInfo *xvi; |
380 |
+ |
int vismatched; |
381 |
+ |
register int i, j; |
382 |
+ |
|
383 |
+ |
if (greyscale) { |
384 |
+ |
ourrank = rankings[2]; |
385 |
+ |
if (maxcolors < 2) maxcolors = 256; |
386 |
+ |
} else if (maxcolors >= 2 && maxcolors <= 256) |
387 |
+ |
ourrank = rankings[1]; |
388 |
+ |
else { |
389 |
+ |
ourrank = rankings[0]; |
390 |
+ |
maxcolors = 256; |
391 |
+ |
} |
392 |
+ |
/* find best visual */ |
393 |
+ |
ourvis.screen = ourscreen; |
394 |
+ |
xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched); |
395 |
+ |
if (xvi == NULL) |
396 |
+ |
quiterr("no visuals for this screen!"); |
397 |
+ |
#ifdef DEBUG |
398 |
+ |
fprintf(stderr, "Supported visuals:\n"); |
399 |
+ |
for (i = 0; i < vismatched; i++) |
400 |
+ |
fprintf(stderr, "\ttype %s, depth %d\n", |
401 |
+ |
vistype[xvi[i].class], xvi[i].depth); |
402 |
+ |
#endif |
403 |
+ |
for (i = 0, j = 1; j < vismatched; j++) |
404 |
+ |
if (viscmp(&xvi[i],&xvi[j]) > 0) |
405 |
+ |
i = j; |
406 |
+ |
/* compare to least acceptable */ |
407 |
+ |
for (j = 0; ourrank[j++] != -1; ) |
408 |
+ |
; |
409 |
+ |
ourvis.class = ourrank[--j]; |
410 |
+ |
ourvis.depth = 1; |
411 |
+ |
if (viscmp(&xvi[i],&ourvis) > 0) |
412 |
+ |
quiterr("inadequate visuals on this screen"); |
413 |
+ |
/* OK, we'll use it */ |
414 |
+ |
copystruct(&ourvis, &xvi[i]); |
415 |
+ |
#ifdef DEBUG |
416 |
+ |
fprintf(stderr, "Selected visual type %s, depth %d\n", |
417 |
+ |
vistype[ourvis.class], ourvis.depth); |
418 |
+ |
#endif |
419 |
+ |
if (ourvis.class == GrayScale || ourvis.class == StaticGray) |
420 |
+ |
greyscale = 1; |
421 |
+ |
if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) |
422 |
+ |
maxcolors = ourvis.colormap_size; |
423 |
+ |
if (maxcolors > 4) |
424 |
+ |
maxcolors -= 2; |
425 |
+ |
XFree((char *)xvi); |
426 |
+ |
} |
427 |
+ |
|
428 |
+ |
|
429 |
|
getras() /* get raster file */ |
430 |
|
{ |
431 |
|
colormap ourmap; |
435 |
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
436 |
|
if (ourdata == NULL) |
437 |
|
goto fail; |
438 |
< |
ourras = make_raster(thedisplay, ourscreen, 1, ourdata, |
438 |
> |
ourras = make_raster(thedisplay, &ourvis, 1, ourdata, |
439 |
|
xmax, ymax, 8); |
440 |
|
if (ourras == NULL) |
441 |
|
goto fail; |
442 |
|
getmono(); |
443 |
< |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
444 |
< |
/* kludge for DirectColor */ |
313 |
< |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
314 |
< |
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
443 |
> |
} else if (ourvis.class == TrueColor || ourvis.class == DirectColor) { |
444 |
> |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
445 |
|
if (ourdata == NULL) |
446 |
|
goto fail; |
447 |
< |
ourras = make_raster(thedisplay, ourscreen, 24, ourdata, |
448 |
< |
xmax, ymax, 8); |
447 |
> |
ourras = make_raster(thedisplay, &ourvis, 32, |
448 |
> |
ourdata, xmax, ymax, 32); |
449 |
|
if (ourras == NULL) |
450 |
|
goto fail; |
451 |
|
getfull(); |
453 |
|
ourdata = (unsigned char *)malloc(xmax*ymax); |
454 |
|
if (ourdata == NULL) |
455 |
|
goto fail; |
456 |
< |
ourras = make_raster(thedisplay, ourscreen, 8, ourdata, |
456 |
> |
ourras = make_raster(thedisplay, &ourvis, 8, ourdata, |
457 |
|
xmax, ymax, 8); |
458 |
|
if (ourras == NULL) |
459 |
|
goto fail; |
460 |
< |
if (greyscale) |
461 |
< |
biq(dither,maxcolors,1,ourmap); |
462 |
< |
else |
463 |
< |
ciq(dither,maxcolors,1,ourmap); |
464 |
< |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
465 |
< |
goto fail; |
460 |
> |
if (ourvis.class == StaticGray) |
461 |
> |
getgrey(); |
462 |
> |
else { |
463 |
> |
if (greyscale) |
464 |
> |
biq(dither,maxcolors,1,ourmap); |
465 |
> |
else |
466 |
> |
ciq(dither,maxcolors,1,ourmap); |
467 |
> |
if (init_rcolors(ourras, ourmap[0], |
468 |
> |
ourmap[1], ourmap[2]) == 0) |
469 |
> |
goto fail; |
470 |
> |
} |
471 |
|
} |
472 |
< |
return; |
472 |
> |
return; |
473 |
|
fail: |
474 |
|
quiterr("could not create raster image"); |
475 |
|
} |
497 |
|
case MapNotify: |
498 |
|
map_rcolors(ourras, wind); |
499 |
|
if (fast) |
500 |
< |
make_rpixmap(ourras); |
500 |
> |
make_rpixmap(ourras, wind); |
501 |
|
break; |
502 |
|
case UnmapNotify: |
503 |
< |
unmap_rcolors(ourras); |
503 |
> |
if (!fast) |
504 |
> |
unmap_rcolors(ourras); |
505 |
|
break; |
506 |
|
case Expose: |
507 |
|
redraw(e.e.x, e.e.y, e.e.width, e.e.height); |
524 |
|
XColor cvx; |
525 |
|
int com, n; |
526 |
|
double comp; |
527 |
+ |
FLOAT hv[2]; |
528 |
|
FVECT rorg, rdir; |
529 |
|
|
530 |
|
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); |
533 |
|
com = buf[0]; |
534 |
|
switch (com) { /* interpret command */ |
535 |
|
case 'q': |
536 |
< |
case CTRL(D): /* quit */ |
536 |
> |
case CTRL('D'): /* quit */ |
537 |
|
quit(0); |
538 |
|
case '\n': |
539 |
|
case '\r': |
547 |
|
sprintf(buf, "%.3f", intens(cval)/exposure); |
548 |
|
break; |
549 |
|
case 'l': /* luminance */ |
550 |
< |
sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); |
550 |
> |
sprintf(buf, "%.0fL", luminance(cval)/exposure); |
551 |
|
break; |
552 |
|
case 'c': /* color */ |
553 |
|
comp = pow(2.0, (double)scale); |
573 |
|
XStoreColor(thedisplay, ourras->cmap, &cvx); |
574 |
|
return(0); |
575 |
|
case 'p': /* position */ |
576 |
< |
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
576 |
> |
pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); |
577 |
> |
sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr), |
578 |
> |
(int)(hv[1]*inpres.yr)); |
579 |
|
XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y, |
580 |
|
buf, strlen(buf)); |
581 |
|
return(0); |
584 |
|
XBell(thedisplay, 0); |
585 |
|
return(-1); |
586 |
|
} |
587 |
< |
if (viewray(rorg, rdir, &ourview, |
588 |
< |
(ekey->x-xoff+.5)/xmax, |
450 |
< |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
587 |
> |
pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); |
588 |
> |
if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
589 |
|
return(-1); |
590 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
591 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
607 |
|
free_raster(ourras); |
608 |
|
getras(); |
609 |
|
/* fall through */ |
610 |
< |
case CTRL(R): /* redraw */ |
611 |
< |
case CTRL(L): |
610 |
> |
case CTRL('R'): /* redraw */ |
611 |
> |
case CTRL('L'): |
612 |
|
unmap_rcolors(ourras); |
613 |
|
XClearWindow(thedisplay, wind); |
614 |
|
map_rcolors(ourras, wind); |
700 |
|
avgbox(clr) /* average color over current box */ |
701 |
|
COLOR clr; |
702 |
|
{ |
703 |
+ |
static COLOR lc; |
704 |
+ |
static int ll, lr, lt, lb; |
705 |
|
int left, right, top, bottom; |
706 |
|
int y; |
707 |
|
double d; |
725 |
|
bottom = ymax; |
726 |
|
if (top >= bottom) |
727 |
|
return(-1); |
728 |
+ |
if (left == ll && right == lr && top == lt && bottom == lb) { |
729 |
+ |
copycolor(clr, lc); |
730 |
+ |
return; |
731 |
+ |
} |
732 |
|
for (y = top; y < bottom; y++) { |
733 |
|
if (getscan(y) == -1) |
734 |
|
return(-1); |
739 |
|
} |
740 |
|
d = 1.0/((right-left)*(bottom-top)); |
741 |
|
scalecolor(clr, d); |
742 |
+ |
ll = left; lr = right; lt = top; lb = bottom; |
743 |
+ |
copycolor(lc, clr); |
744 |
|
return(0); |
745 |
|
} |
746 |
|
|
749 |
|
{ |
750 |
|
register unsigned char *dp; |
751 |
|
register int x, err; |
752 |
< |
int y; |
752 |
> |
int y, errp; |
753 |
|
short *cerr; |
754 |
|
|
755 |
|
if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
759 |
|
if (getscan(y) < 0) |
760 |
|
quiterr("seek error in getmono"); |
761 |
|
normcolrs(scanline, xmax, scale); |
762 |
+ |
add2icon(y, scanline); |
763 |
|
err = 0; |
764 |
|
for (x = 0; x < xmax; x++) { |
765 |
|
if (!(x&7)) |
766 |
|
*++dp = 0; |
767 |
+ |
errp = err; |
768 |
|
err += normbright(scanline[x]) + cerr[x]; |
769 |
|
if (err > 127) |
770 |
|
err -= 255; |
771 |
|
else |
772 |
|
*dp |= 1<<(7-(x&07)); |
773 |
< |
cerr[x] = err >>= 1; |
773 |
> |
err /= 3; |
774 |
> |
cerr[x] = err + errp; |
775 |
|
} |
776 |
|
} |
777 |
|
free((char *)cerr); |
778 |
|
} |
779 |
|
|
780 |
|
|
781 |
+ |
add2icon(y, scan) /* add a scanline to our icon data */ |
782 |
+ |
int y; |
783 |
+ |
COLR *scan; |
784 |
+ |
{ |
785 |
+ |
static short cerr[ICONSIZ]; |
786 |
+ |
static int ynext; |
787 |
+ |
static char *dp; |
788 |
+ |
register int err; |
789 |
+ |
register int x, ti; |
790 |
+ |
int errp; |
791 |
+ |
|
792 |
+ |
if (iconheight == 0) { /* initialize */ |
793 |
+ |
if (xmax <= ICONSIZ && ymax <= ICONSIZ) { |
794 |
+ |
iconwidth = xmax; |
795 |
+ |
iconheight = ymax; |
796 |
+ |
} else if (xmax > ymax) { |
797 |
+ |
iconwidth = ICONSIZ; |
798 |
+ |
iconheight = ICONSIZ*ymax/xmax; |
799 |
+ |
if (iconheight < 1) |
800 |
+ |
iconheight = 1; |
801 |
+ |
} else { |
802 |
+ |
iconwidth = ICONSIZ*xmax/ymax; |
803 |
+ |
if (iconwidth < 1) |
804 |
+ |
iconwidth = 1; |
805 |
+ |
iconheight = ICONSIZ; |
806 |
+ |
} |
807 |
+ |
ynext = 0; |
808 |
+ |
dp = icondata - 1; |
809 |
+ |
} |
810 |
+ |
if (y < ynext*ymax/iconheight) /* skip this one */ |
811 |
+ |
return; |
812 |
+ |
err = 0; |
813 |
+ |
for (x = 0; x < iconwidth; x++) { |
814 |
+ |
if (!(x&7)) |
815 |
+ |
*++dp = 0; |
816 |
+ |
errp = err; |
817 |
+ |
ti = x*xmax/iconwidth; |
818 |
+ |
err += normbright(scan[ti]) + cerr[x]; |
819 |
+ |
if (err > 127) |
820 |
+ |
err -= 255; |
821 |
+ |
else |
822 |
+ |
*dp |= 1<<(x&07); |
823 |
+ |
err /= 3; |
824 |
+ |
cerr[x] = err + errp; |
825 |
+ |
} |
826 |
+ |
ynext++; |
827 |
+ |
} |
828 |
+ |
|
829 |
+ |
|
830 |
|
getfull() /* get full (24-bit) data */ |
831 |
|
{ |
832 |
|
int y; |
833 |
+ |
register unsigned long *dp; |
834 |
+ |
register int x; |
835 |
+ |
/* set gamma correction */ |
836 |
+ |
setcolrgam(gamcor); |
837 |
+ |
/* read and convert file */ |
838 |
+ |
dp = (unsigned long *)ourdata; |
839 |
+ |
for (y = 0; y < ymax; y++) { |
840 |
+ |
if (getscan(y) < 0) |
841 |
+ |
quiterr("seek error in getfull"); |
842 |
+ |
if (scale) |
843 |
+ |
shiftcolrs(scanline, xmax, scale); |
844 |
+ |
colrs_gambs(scanline, xmax); |
845 |
+ |
add2icon(y, scanline); |
846 |
+ |
if (ourras->image->blue_mask & 1) |
847 |
+ |
for (x = 0; x < xmax; x++) |
848 |
+ |
*dp++ = scanline[x][RED] << 16 | |
849 |
+ |
scanline[x][GRN] << 8 | |
850 |
+ |
scanline[x][BLU] ; |
851 |
+ |
else |
852 |
+ |
for (x = 0; x < xmax; x++) |
853 |
+ |
*dp++ = scanline[x][RED] | |
854 |
+ |
scanline[x][GRN] << 8 | |
855 |
+ |
scanline[x][BLU] << 16 ; |
856 |
+ |
} |
857 |
+ |
} |
858 |
+ |
|
859 |
+ |
|
860 |
+ |
getgrey() /* get greyscale data */ |
861 |
+ |
{ |
862 |
+ |
int y; |
863 |
|
register unsigned char *dp; |
864 |
|
register int x; |
865 |
|
/* set gamma correction */ |
872 |
|
if (scale) |
873 |
|
shiftcolrs(scanline, xmax, scale); |
874 |
|
colrs_gambs(scanline, xmax); |
875 |
< |
for (x = 0; x < xmax; x++) { |
876 |
< |
*dp++ = scanline[x][RED]; |
877 |
< |
*dp++ = scanline[x][GRN]; |
878 |
< |
*dp++ = scanline[x][BLU]; |
879 |
< |
} |
875 |
> |
add2icon(y, scanline); |
876 |
> |
if (ourvis.colormap_size < 256) |
877 |
> |
for (x = 0; x < xmax; x++) |
878 |
> |
*dp++ = ((long)normbright(scanline[x]) * |
879 |
> |
ourvis.colormap_size + 128) >> 8; |
880 |
> |
else |
881 |
> |
for (x = 0; x < xmax; x++) |
882 |
> |
*dp++ = normbright(scanline[x]); |
883 |
|
} |
884 |
|
} |
885 |
|
|
942 |
|
quiterr("cannot seek for picreadline"); |
943 |
|
/* convert scanline */ |
944 |
|
normcolrs(scanline, xmax, scale); |
945 |
+ |
add2icon(y, scanline); |
946 |
|
for (i = 0; i < xmax; i++) { |
947 |
|
l3[i].r = scanline[i][RED]; |
948 |
|
l3[i].g = scanline[i][GRN]; |
966 |
|
register int i, val; |
967 |
|
|
968 |
|
for (i = 0; i < 256; i++) { |
969 |
< |
val = pow(i/256.0, 1.0/gamcor) * 256.0; |
969 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
970 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
971 |
|
} |
972 |
|
} |
975 |
|
picwritecm(map) /* handled elsewhere */ |
976 |
|
colormap map; |
977 |
|
{ |
978 |
< |
#ifdef DEBUG |
978 |
> |
#if 0 |
979 |
|
register int i; |
980 |
|
|
981 |
|
for (i = 0; i < 256; i++) |