| 27 |
|
|
| 28 |
|
#include "color.h" |
| 29 |
|
#include "view.h" |
| 30 |
– |
#include "pic.h" |
| 30 |
|
#include "x11raster.h" |
| 31 |
|
#include "random.h" |
| 32 |
|
#include "resolu.h" |
| 103 |
|
|
| 104 |
|
char errmsg[128]; |
| 105 |
|
|
| 106 |
+ |
extern BYTE clrtab[256][3]; /* global color map */ |
| 107 |
+ |
|
| 108 |
|
extern long ftell(); |
| 109 |
|
|
| 110 |
|
extern char *malloc(), *calloc(); |
| 111 |
|
|
| 111 |
– |
extern double pow(), log(); |
| 112 |
– |
|
| 112 |
|
Display *thedisplay; |
| 113 |
|
|
| 114 |
+ |
|
| 115 |
|
main(argc, argv) |
| 116 |
|
int argc; |
| 117 |
|
char *argv[]; |
| 118 |
|
{ |
| 119 |
+ |
extern char *getenv(); |
| 120 |
+ |
char *gv; |
| 121 |
|
int headline(); |
| 122 |
|
int i; |
| 123 |
|
|
| 124 |
|
progname = argv[0]; |
| 125 |
+ |
if ((gv = getenv("GAMMA")) != NULL) |
| 126 |
+ |
gamcor = atof(gv); |
| 127 |
|
|
| 128 |
|
for (i = 1; i < argc; i++) |
| 129 |
|
if (argv[i][0] == '-') |
| 307 |
|
} |
| 308 |
|
|
| 309 |
|
|
| 306 |
– |
eputs(s) |
| 307 |
– |
char *s; |
| 308 |
– |
{ |
| 309 |
– |
fputs(s, stderr); |
| 310 |
– |
} |
| 311 |
– |
|
| 312 |
– |
|
| 313 |
– |
quit(code) |
| 314 |
– |
int code; |
| 315 |
– |
{ |
| 316 |
– |
exit(code); |
| 317 |
– |
} |
| 318 |
– |
|
| 319 |
– |
|
| 310 |
|
static int |
| 311 |
|
viscmp(v1,v2) /* compare visual to see which is better, descending */ |
| 312 |
|
register XVisualInfo *v1, *v2; |
| 410 |
|
greyscale = 1; |
| 411 |
|
if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) |
| 412 |
|
maxcolors = ourvis.colormap_size; |
| 423 |
– |
if (maxcolors > 4) |
| 424 |
– |
maxcolors -= 2; |
| 413 |
|
if (ourvis.class == StaticGray) { |
| 414 |
|
ourblack = 0; |
| 415 |
|
ourwhite = 255; |
| 420 |
|
ourblack = 0; |
| 421 |
|
ourwhite = 1; |
| 422 |
|
} |
| 423 |
+ |
if (maxcolors > 4) |
| 424 |
+ |
maxcolors -= 2; |
| 425 |
|
} else { |
| 426 |
|
ourblack = 0; |
| 427 |
|
ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask; |
| 432 |
|
|
| 433 |
|
getras() /* get raster file */ |
| 434 |
|
{ |
| 445 |
– |
colormap ourmap; |
| 435 |
|
XVisualInfo vinfo; |
| 436 |
|
|
| 437 |
|
if (maxcolors <= 2) { /* monochrome */ |
| 443 |
|
if (ourras == NULL) |
| 444 |
|
goto fail; |
| 445 |
|
getmono(); |
| 446 |
< |
} else if (ourvis.class == TrueColor || ourvis.class == DirectColor) { |
| 446 |
> |
} else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { |
| 447 |
|
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
| 448 |
|
if (ourdata == NULL) |
| 449 |
|
goto fail; |
| 460 |
|
xmax, ymax, 8); |
| 461 |
|
if (ourras == NULL) |
| 462 |
|
goto fail; |
| 463 |
< |
if (ourvis.class == StaticGray) |
| 463 |
> |
if (greyscale | ourvis.class == StaticGray) |
| 464 |
|
getgrey(); |
| 465 |
< |
else { |
| 466 |
< |
if (greyscale) |
| 467 |
< |
biq(dither,maxcolors,1,ourmap); |
| 468 |
< |
else |
| 480 |
< |
ciq(dither,maxcolors,1,ourmap); |
| 481 |
< |
if (init_rcolors(ourras, ourmap[0], |
| 482 |
< |
ourmap[1], ourmap[2]) == 0) |
| 483 |
< |
goto fail; |
| 484 |
< |
} |
| 465 |
> |
else |
| 466 |
> |
getmapped(); |
| 467 |
> |
if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab)) |
| 468 |
> |
goto fail; |
| 469 |
|
} |
| 470 |
< |
return; |
| 470 |
> |
return; |
| 471 |
|
fail: |
| 472 |
|
quiterr("could not create raster image"); |
| 473 |
|
} |
| 611 |
|
XClearWindow(thedisplay, wind); |
| 612 |
|
map_rcolors(ourras, wind); |
| 613 |
|
if (fast) |
| 614 |
< |
make_rpixmap(ourras); |
| 614 |
> |
make_rpixmap(ourras, wind); |
| 615 |
|
redraw(0, 0, width, height); |
| 616 |
|
return(0); |
| 617 |
|
case ' ': /* clear */ |
| 725 |
|
return(-1); |
| 726 |
|
if (left == ll && right == lr && top == lt && bottom == lb) { |
| 727 |
|
copycolor(clr, lc); |
| 728 |
< |
return; |
| 728 |
> |
return(0); |
| 729 |
|
} |
| 730 |
|
for (y = top; y < bottom; y++) { |
| 731 |
|
if (getscan(y) == -1) |
| 754 |
|
quiterr("out of memory in getmono"); |
| 755 |
|
dp = ourdata - 1; |
| 756 |
|
for (y = 0; y < ymax; y++) { |
| 757 |
< |
if (getscan(y) < 0) |
| 774 |
< |
quiterr("seek error in getmono"); |
| 775 |
< |
normcolrs(scanline, xmax, scale); |
| 757 |
> |
getscan(y); |
| 758 |
|
add2icon(y, scanline); |
| 759 |
+ |
normcolrs(scanline, xmax, scale); |
| 760 |
|
err = 0; |
| 761 |
|
for (x = 0; x < xmax; x++) { |
| 762 |
|
if (!(x&7)) |
| 782 |
|
static short cerr[ICONSIZ]; |
| 783 |
|
static int ynext; |
| 784 |
|
static char *dp; |
| 785 |
+ |
COLR clr; |
| 786 |
|
register int err; |
| 787 |
|
register int x, ti; |
| 788 |
|
int errp; |
| 813 |
|
*++dp = 0; |
| 814 |
|
errp = err; |
| 815 |
|
ti = x*xmax/iconwidth; |
| 816 |
< |
err += normbright(scan[ti]) + cerr[x]; |
| 816 |
> |
copycolr(clr, scan[ti]); |
| 817 |
> |
normcolrs(clr, 1, scale); |
| 818 |
> |
err += normbright(clr) + cerr[x]; |
| 819 |
|
if (err > 127) |
| 820 |
|
err -= 255; |
| 821 |
|
else |
| 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"); |
| 840 |
> |
getscan(y); |
| 841 |
> |
add2icon(y, scanline); |
| 842 |
|
if (scale) |
| 843 |
|
shiftcolrs(scanline, xmax, scale); |
| 844 |
|
colrs_gambs(scanline, xmax); |
| 859 |
– |
add2icon(y, scanline); |
| 845 |
|
if (ourras->image->blue_mask & 1) |
| 846 |
|
for (x = 0; x < xmax; x++) |
| 847 |
|
*dp++ = scanline[x][RED] << 16 | |
| 866 |
|
/* read and convert file */ |
| 867 |
|
dp = ourdata; |
| 868 |
|
for (y = 0; y < ymax; y++) { |
| 869 |
< |
if (getscan(y) < 0) |
| 870 |
< |
quiterr("seek error in getfull"); |
| 869 |
> |
getscan(y); |
| 870 |
> |
add2icon(y, scanline); |
| 871 |
|
if (scale) |
| 872 |
|
shiftcolrs(scanline, xmax, scale); |
| 873 |
+ |
for (x = 0; x < xmax; x++) |
| 874 |
+ |
scanline[x][GRN] = normbright(scanline[x]); |
| 875 |
|
colrs_gambs(scanline, xmax); |
| 876 |
< |
add2icon(y, scanline); |
| 890 |
< |
if (ourvis.colormap_size < 256) |
| 876 |
> |
if (maxcolors < 256) |
| 877 |
|
for (x = 0; x < xmax; x++) |
| 878 |
< |
*dp++ = ((long)normbright(scanline[x]) * |
| 879 |
< |
ourvis.colormap_size + 128) >> 8; |
| 878 |
> |
*dp++ = ((long)scanline[x][GRN] * |
| 879 |
> |
maxcolors + maxcolors/2) >> 8; |
| 880 |
|
else |
| 881 |
|
for (x = 0; x < xmax; x++) |
| 882 |
< |
*dp++ = normbright(scanline[x]); |
| 882 |
> |
*dp++ = scanline[x][GRN]; |
| 883 |
|
} |
| 884 |
+ |
for (x = 0; x < maxcolors; x++) |
| 885 |
+ |
clrtab[x][RED] = clrtab[x][GRN] = |
| 886 |
+ |
clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; |
| 887 |
|
} |
| 888 |
|
|
| 889 |
|
|
| 890 |
+ |
getmapped() /* get color-mapped data */ |
| 891 |
+ |
{ |
| 892 |
+ |
int y; |
| 893 |
+ |
/* set gamma correction */ |
| 894 |
+ |
setcolrgam(gamcor); |
| 895 |
+ |
/* make histogram */ |
| 896 |
+ |
new_histo(); |
| 897 |
+ |
for (y = 0; y < ymax; y++) { |
| 898 |
+ |
if (getscan(y) < 0) |
| 899 |
+ |
quiterr("seek error in getmapped"); |
| 900 |
+ |
add2icon(y, scanline); |
| 901 |
+ |
if (scale) |
| 902 |
+ |
shiftcolrs(scanline, xmax, scale); |
| 903 |
+ |
colrs_gambs(scanline, xmax); |
| 904 |
+ |
cnt_colrs(scanline, xmax); |
| 905 |
+ |
} |
| 906 |
+ |
/* map pixels */ |
| 907 |
+ |
if (!new_clrtab(maxcolors)) |
| 908 |
+ |
quiterr("cannot create color map"); |
| 909 |
+ |
for (y = 0; y < ymax; y++) { |
| 910 |
+ |
if (getscan(y) < 0) |
| 911 |
+ |
quiterr("seek error in getmapped"); |
| 912 |
+ |
if (scale) |
| 913 |
+ |
shiftcolrs(scanline, xmax, scale); |
| 914 |
+ |
colrs_gambs(scanline, xmax); |
| 915 |
+ |
if (dither) |
| 916 |
+ |
dith_colrs(ourdata+y*xmax, scanline, xmax); |
| 917 |
+ |
else |
| 918 |
+ |
map_colrs(ourdata+y*xmax, scanline, xmax); |
| 919 |
+ |
} |
| 920 |
+ |
} |
| 921 |
+ |
|
| 922 |
+ |
|
| 923 |
|
scale_rcolors(xr, sf) /* scale color map */ |
| 924 |
|
register XRASTER *xr; |
| 925 |
|
double sf; |
| 957 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
| 958 |
|
quiterr("fseek error"); |
| 959 |
|
cury = y; |
| 960 |
< |
} else if (scanpos != NULL) |
| 960 |
> |
} else if (scanpos != NULL && scanpos[y] == -1) |
| 961 |
|
scanpos[y] = ftell(fin); |
| 962 |
|
|
| 963 |
|
if (freadcolrs(scanline, xmax, fin) < 0) |
| 965 |
|
|
| 966 |
|
cury++; |
| 967 |
|
return(0); |
| 946 |
– |
} |
| 947 |
– |
|
| 948 |
– |
|
| 949 |
– |
picreadline3(y, l3) /* read in 3-byte scanline */ |
| 950 |
– |
int y; |
| 951 |
– |
register rgbpixel *l3; |
| 952 |
– |
{ |
| 953 |
– |
register int i; |
| 954 |
– |
/* read scanline */ |
| 955 |
– |
if (getscan(y) < 0) |
| 956 |
– |
quiterr("cannot seek for picreadline"); |
| 957 |
– |
/* convert scanline */ |
| 958 |
– |
normcolrs(scanline, xmax, scale); |
| 959 |
– |
add2icon(y, scanline); |
| 960 |
– |
for (i = 0; i < xmax; i++) { |
| 961 |
– |
l3[i].r = scanline[i][RED]; |
| 962 |
– |
l3[i].g = scanline[i][GRN]; |
| 963 |
– |
l3[i].b = scanline[i][BLU]; |
| 964 |
– |
} |
| 965 |
– |
} |
| 966 |
– |
|
| 967 |
– |
|
| 968 |
– |
picwriteline(y, l) /* add 8-bit scanline to image */ |
| 969 |
– |
int y; |
| 970 |
– |
pixel *l; |
| 971 |
– |
{ |
| 972 |
– |
bcopy((char *)l, (char *)ourdata+y*xmax, xmax); |
| 973 |
– |
} |
| 974 |
– |
|
| 975 |
– |
|
| 976 |
– |
picreadcm(map) /* do gamma correction */ |
| 977 |
– |
colormap map; |
| 978 |
– |
{ |
| 979 |
– |
extern double pow(); |
| 980 |
– |
register int i, val; |
| 981 |
– |
|
| 982 |
– |
for (i = 0; i < 256; i++) { |
| 983 |
– |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
| 984 |
– |
map[0][i] = map[1][i] = map[2][i] = val; |
| 985 |
– |
} |
| 986 |
– |
} |
| 987 |
– |
|
| 988 |
– |
|
| 989 |
– |
picwritecm(map) /* handled elsewhere */ |
| 990 |
– |
colormap map; |
| 991 |
– |
{ |
| 992 |
– |
#if 0 |
| 993 |
– |
register int i; |
| 994 |
– |
|
| 995 |
– |
for (i = 0; i < 256; i++) |
| 996 |
– |
printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); |
| 997 |
– |
#endif |
| 968 |
|
} |