| 1 |
greg |
2.24 |
/* Copyright (c) 1993 Regents of the University of California */
|
| 2 |
greg |
1.1 |
|
| 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 |
greg |
2.29 |
#include <signal.h>
|
| 25 |
greg |
1.1 |
#include <X11/Xlib.h>
|
| 26 |
|
|
#include <X11/cursorfont.h>
|
| 27 |
|
|
#include <X11/Xutil.h>
|
| 28 |
greg |
2.24 |
#include <X11/Xatom.h>
|
| 29 |
greg |
1.1 |
|
| 30 |
|
|
#include "color.h"
|
| 31 |
|
|
#include "view.h"
|
| 32 |
|
|
#include "x11raster.h"
|
| 33 |
|
|
#include "random.h"
|
| 34 |
greg |
1.26 |
#include "resolu.h"
|
| 35 |
greg |
1.1 |
|
| 36 |
greg |
2.28 |
#ifdef __alpha
|
| 37 |
|
|
#define int4 int
|
| 38 |
|
|
#endif
|
| 39 |
|
|
#ifndef int4
|
| 40 |
|
|
#define int4 long
|
| 41 |
|
|
#endif
|
| 42 |
|
|
|
| 43 |
greg |
1.1 |
#define FONTNAME "8x13" /* text font we'll use */
|
| 44 |
|
|
|
| 45 |
greg |
2.3 |
#define CTRL(c) ((c)-'@')
|
| 46 |
greg |
1.1 |
|
| 47 |
|
|
#define BORWIDTH 5 /* border width */
|
| 48 |
|
|
|
| 49 |
greg |
1.18 |
#define ICONSIZ (8*10) /* maximum icon dimension (even 8) */
|
| 50 |
greg |
1.17 |
|
| 51 |
greg |
1.1 |
#define ourscreen DefaultScreen(thedisplay)
|
| 52 |
|
|
#define ourroot RootWindow(thedisplay,ourscreen)
|
| 53 |
|
|
|
| 54 |
greg |
1.5 |
#define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
|
| 55 |
|
|
|
| 56 |
greg |
1.2 |
#define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
|
| 57 |
|
|
|
| 58 |
greg |
1.1 |
double gamcor = 2.2; /* gamma correction */
|
| 59 |
greg |
2.35 |
char *gamstr = NULL; /* gamma value override */
|
| 60 |
greg |
1.1 |
|
| 61 |
|
|
int dither = 1; /* dither colors? */
|
| 62 |
|
|
int fast = 0; /* keep picture in Pixmap? */
|
| 63 |
|
|
|
| 64 |
greg |
2.6 |
char *dispname = NULL; /* our display name */
|
| 65 |
|
|
|
| 66 |
greg |
1.1 |
Window wind = 0; /* our output window */
|
| 67 |
greg |
2.9 |
unsigned long ourblack=0, ourwhite=1; /* black and white for this visual */
|
| 68 |
greg |
1.2 |
int maxcolors = 0; /* maximum colors */
|
| 69 |
greg |
1.1 |
int greyscale = 0; /* in grey */
|
| 70 |
|
|
|
| 71 |
|
|
int scale = 0; /* scalefactor; power of two */
|
| 72 |
|
|
|
| 73 |
|
|
int xoff = 0; /* x image offset */
|
| 74 |
|
|
int yoff = 0; /* y image offset */
|
| 75 |
|
|
|
| 76 |
greg |
2.29 |
int parent = 0; /* number of children, -1 if child */
|
| 77 |
|
|
|
| 78 |
greg |
1.1 |
VIEW ourview = STDVIEW; /* image view parameters */
|
| 79 |
|
|
int gotview = 0; /* got parameters from file */
|
| 80 |
|
|
|
| 81 |
|
|
COLR *scanline; /* scan line buffer */
|
| 82 |
|
|
|
| 83 |
greg |
1.26 |
RESOLU inpres; /* input resolution and ordering */
|
| 84 |
|
|
int xmax, ymax; /* picture dimensions */
|
| 85 |
greg |
1.1 |
int width, height; /* window size */
|
| 86 |
|
|
char *fname = NULL; /* input file name */
|
| 87 |
|
|
FILE *fin = stdin; /* input file */
|
| 88 |
|
|
long *scanpos = NULL; /* scan line positions in file */
|
| 89 |
|
|
int cury = 0; /* current scan location */
|
| 90 |
|
|
|
| 91 |
|
|
double exposure = 1.0; /* exposure compensation used */
|
| 92 |
|
|
|
| 93 |
greg |
1.14 |
int wrongformat = 0; /* input in another format? */
|
| 94 |
|
|
|
| 95 |
greg |
2.6 |
GC ourgc; /* standard graphics context */
|
| 96 |
greg |
1.5 |
GC revgc; /* graphics context with GXinvert */
|
| 97 |
|
|
|
| 98 |
greg |
2.6 |
int *ourrank; /* our visual class ranking */
|
| 99 |
|
|
XVisualInfo ourvis; /* our visual */
|
| 100 |
|
|
XRASTER *ourras; /* our stored image */
|
| 101 |
greg |
1.1 |
unsigned char *ourdata; /* our image data */
|
| 102 |
|
|
|
| 103 |
|
|
struct {
|
| 104 |
|
|
int xmin, ymin, xsiz, ysiz;
|
| 105 |
|
|
} box = {0, 0, 0, 0}; /* current box */
|
| 106 |
|
|
|
| 107 |
|
|
char *geometry = NULL; /* geometry specification */
|
| 108 |
|
|
|
| 109 |
greg |
1.18 |
char icondata[ICONSIZ*ICONSIZ/8]; /* icon bitmap data */
|
| 110 |
greg |
1.17 |
int iconwidth = 0, iconheight = 0;
|
| 111 |
|
|
|
| 112 |
greg |
1.1 |
char *progname;
|
| 113 |
|
|
|
| 114 |
|
|
char errmsg[128];
|
| 115 |
|
|
|
| 116 |
greg |
2.13 |
extern BYTE clrtab[256][3]; /* global color map */
|
| 117 |
|
|
|
| 118 |
greg |
1.1 |
extern long ftell();
|
| 119 |
|
|
|
| 120 |
greg |
2.35 |
extern char *getenv();
|
| 121 |
|
|
|
| 122 |
greg |
1.1 |
Display *thedisplay;
|
| 123 |
greg |
2.24 |
Atom closedownAtom, wmProtocolsAtom;
|
| 124 |
greg |
1.1 |
|
| 125 |
greg |
2.32 |
int sigrecv;
|
| 126 |
greg |
2.14 |
|
| 127 |
greg |
2.32 |
int onsig() { sigrecv++; }
|
| 128 |
greg |
2.29 |
|
| 129 |
greg |
2.32 |
|
| 130 |
greg |
1.1 |
main(argc, argv)
|
| 131 |
|
|
int argc;
|
| 132 |
|
|
char *argv[];
|
| 133 |
|
|
{
|
| 134 |
|
|
int headline();
|
| 135 |
|
|
int i;
|
| 136 |
greg |
2.29 |
int pid;
|
| 137 |
greg |
1.1 |
|
| 138 |
|
|
progname = argv[0];
|
| 139 |
|
|
|
| 140 |
|
|
for (i = 1; i < argc; i++)
|
| 141 |
|
|
if (argv[i][0] == '-')
|
| 142 |
|
|
switch (argv[i][1]) {
|
| 143 |
|
|
case 'c':
|
| 144 |
|
|
maxcolors = atoi(argv[++i]);
|
| 145 |
|
|
break;
|
| 146 |
|
|
case 'b':
|
| 147 |
|
|
greyscale = !greyscale;
|
| 148 |
|
|
break;
|
| 149 |
|
|
case 'm':
|
| 150 |
|
|
maxcolors = 2;
|
| 151 |
|
|
break;
|
| 152 |
|
|
case 'd':
|
| 153 |
greg |
2.7 |
if (argv[i][2] == 'i')
|
| 154 |
greg |
2.6 |
dispname = argv[++i];
|
| 155 |
greg |
2.7 |
else
|
| 156 |
|
|
dither = !dither;
|
| 157 |
greg |
1.1 |
break;
|
| 158 |
|
|
case 'f':
|
| 159 |
|
|
fast = !fast;
|
| 160 |
|
|
break;
|
| 161 |
|
|
case 'e':
|
| 162 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
|
| 163 |
|
|
goto userr;
|
| 164 |
|
|
scale = atoi(argv[++i]);
|
| 165 |
|
|
break;
|
| 166 |
|
|
case 'g':
|
| 167 |
greg |
2.7 |
if (argv[i][2] == 'e')
|
| 168 |
greg |
1.1 |
geometry = argv[++i];
|
| 169 |
|
|
else
|
| 170 |
greg |
2.35 |
gamstr = argv[++i];
|
| 171 |
greg |
1.1 |
break;
|
| 172 |
|
|
default:
|
| 173 |
|
|
goto userr;
|
| 174 |
|
|
}
|
| 175 |
greg |
1.3 |
else if (argv[i][0] == '=')
|
| 176 |
|
|
geometry = argv[i];
|
| 177 |
greg |
1.1 |
else
|
| 178 |
|
|
break;
|
| 179 |
|
|
|
| 180 |
greg |
2.29 |
if (i > argc)
|
| 181 |
|
|
goto userr;
|
| 182 |
|
|
while (i < argc-1) {
|
| 183 |
greg |
2.32 |
sigrecv = 0;
|
| 184 |
|
|
signal(SIGCONT, onsig);
|
| 185 |
greg |
2.29 |
if ((pid=fork()) == 0) { /* a child for each picture */
|
| 186 |
|
|
parent = -1;
|
| 187 |
|
|
break;
|
| 188 |
|
|
}
|
| 189 |
|
|
if (pid < 0)
|
| 190 |
|
|
quiterr("fork failed");
|
| 191 |
|
|
parent++;
|
| 192 |
greg |
2.32 |
while (!sigrecv)
|
| 193 |
|
|
pause(); /* wait for wake-up call */
|
| 194 |
greg |
2.29 |
i++;
|
| 195 |
|
|
}
|
| 196 |
|
|
if (i < argc) { /* open picture file */
|
| 197 |
greg |
1.1 |
fname = argv[i];
|
| 198 |
|
|
fin = fopen(fname, "r");
|
| 199 |
greg |
2.33 |
if (fin == NULL)
|
| 200 |
|
|
quiterr("cannot open picture file");
|
| 201 |
greg |
2.29 |
}
|
| 202 |
greg |
1.1 |
/* get header */
|
| 203 |
greg |
1.14 |
getheader(fin, headline, NULL);
|
| 204 |
greg |
1.1 |
/* get picture dimensions */
|
| 205 |
greg |
1.26 |
if (wrongformat || !fgetsresolu(&inpres, fin))
|
| 206 |
greg |
1.14 |
quiterr("bad picture format");
|
| 207 |
greg |
1.26 |
xmax = scanlen(&inpres);
|
| 208 |
|
|
ymax = numscans(&inpres);
|
| 209 |
greg |
1.1 |
/* set view parameters */
|
| 210 |
|
|
if (gotview && setview(&ourview) != NULL)
|
| 211 |
|
|
gotview = 0;
|
| 212 |
|
|
if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
|
| 213 |
|
|
quiterr("out of memory");
|
| 214 |
|
|
|
| 215 |
greg |
2.24 |
init(argc, argv); /* get file and open window */
|
| 216 |
greg |
1.1 |
|
| 217 |
greg |
2.32 |
if (parent < 0) {
|
| 218 |
greg |
2.29 |
kill(getppid(), SIGCONT); /* signal parent if child */
|
| 219 |
greg |
2.32 |
sigrecv--;
|
| 220 |
|
|
}
|
| 221 |
greg |
1.1 |
for ( ; ; )
|
| 222 |
|
|
getevent(); /* main loop */
|
| 223 |
|
|
userr:
|
| 224 |
|
|
fprintf(stderr,
|
| 225 |
greg |
2.35 |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor] pic ..\n",
|
| 226 |
greg |
1.1 |
progname);
|
| 227 |
greg |
2.21 |
exit(1);
|
| 228 |
greg |
1.1 |
}
|
| 229 |
|
|
|
| 230 |
|
|
|
| 231 |
|
|
headline(s) /* get relevant info from header */
|
| 232 |
|
|
char *s;
|
| 233 |
|
|
{
|
| 234 |
greg |
1.14 |
char fmt[32];
|
| 235 |
greg |
1.1 |
|
| 236 |
greg |
1.2 |
if (isexpos(s))
|
| 237 |
|
|
exposure *= exposval(s);
|
| 238 |
greg |
1.14 |
else if (isformat(s)) {
|
| 239 |
|
|
formatval(fmt, s);
|
| 240 |
|
|
wrongformat = strcmp(fmt, COLRFMT);
|
| 241 |
greg |
2.4 |
} else if (isview(s) && sscanview(&ourview, s) > 0)
|
| 242 |
|
|
gotview++;
|
| 243 |
greg |
1.1 |
}
|
| 244 |
|
|
|
| 245 |
|
|
|
| 246 |
greg |
2.24 |
init(argc, argv) /* get data and open window */
|
| 247 |
|
|
int argc;
|
| 248 |
|
|
char **argv;
|
| 249 |
greg |
1.1 |
{
|
| 250 |
greg |
1.4 |
XSetWindowAttributes ourwinattr;
|
| 251 |
greg |
2.24 |
XClassHint xclshints;
|
| 252 |
|
|
XWMHints xwmhints;
|
| 253 |
|
|
XSizeHints xszhints;
|
| 254 |
|
|
XTextProperty windowName, iconName;
|
| 255 |
|
|
XGCValues xgcv;
|
| 256 |
|
|
char *name;
|
| 257 |
greg |
2.6 |
register int i;
|
| 258 |
greg |
1.1 |
|
| 259 |
|
|
if (fname != NULL) {
|
| 260 |
|
|
scanpos = (long *)malloc(ymax*sizeof(long));
|
| 261 |
|
|
if (scanpos == NULL)
|
| 262 |
greg |
2.24 |
quiterr("out of memory");
|
| 263 |
greg |
1.1 |
for (i = 0; i < ymax; i++)
|
| 264 |
|
|
scanpos[i] = -1;
|
| 265 |
greg |
2.24 |
name = fname;
|
| 266 |
|
|
} else
|
| 267 |
|
|
name = progname;
|
| 268 |
|
|
/* remove directory prefix from name */
|
| 269 |
|
|
for (i = strlen(name); i-- > 0; )
|
| 270 |
|
|
if (name[i] == '/')
|
| 271 |
|
|
break;
|
| 272 |
|
|
name += i+1;
|
| 273 |
greg |
2.6 |
if ((thedisplay = XOpenDisplay(dispname)) == NULL)
|
| 274 |
|
|
quiterr("cannot open display");
|
| 275 |
greg |
2.35 |
/* set gamma value */
|
| 276 |
|
|
if (gamstr == NULL) /* get it from the X server */
|
| 277 |
|
|
gamstr = XGetDefault(thedisplay, "radiance", "gamma");
|
| 278 |
|
|
if (gamstr == NULL) /* get it from the environment */
|
| 279 |
|
|
gamstr = getenv("GAMMA");
|
| 280 |
|
|
if (gamstr != NULL)
|
| 281 |
|
|
gamcor = atof(gamstr);
|
| 282 |
greg |
2.6 |
/* get best visual for default screen */
|
| 283 |
|
|
getbestvis();
|
| 284 |
greg |
1.4 |
/* store image */
|
| 285 |
|
|
getras();
|
| 286 |
greg |
2.20 |
/* get size and position */
|
| 287 |
greg |
2.24 |
xszhints.flags = 0;
|
| 288 |
|
|
xszhints.width = xmax; xszhints.height = ymax;
|
| 289 |
greg |
2.20 |
if (geometry != NULL) {
|
| 290 |
greg |
2.24 |
i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
|
| 291 |
|
|
(unsigned *)&xszhints.width,
|
| 292 |
|
|
(unsigned *)&xszhints.height);
|
| 293 |
greg |
2.20 |
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
|
| 294 |
greg |
2.24 |
xszhints.flags |= USSize;
|
| 295 |
greg |
2.20 |
else
|
| 296 |
greg |
2.24 |
xszhints.flags |= PSize;
|
| 297 |
greg |
2.20 |
if ((i&(XValue|YValue)) == (XValue|YValue)) {
|
| 298 |
greg |
2.24 |
xszhints.flags |= USPosition;
|
| 299 |
greg |
2.20 |
if (i & XNegative)
|
| 300 |
greg |
2.24 |
xszhints.x += DisplayWidth(thedisplay,
|
| 301 |
|
|
ourscreen)-1-xszhints.width-2*BORWIDTH;
|
| 302 |
greg |
2.20 |
if (i & YNegative)
|
| 303 |
greg |
2.24 |
xszhints.y += DisplayHeight(thedisplay,
|
| 304 |
|
|
ourscreen)-1-xszhints.height-2*BORWIDTH;
|
| 305 |
greg |
2.20 |
}
|
| 306 |
|
|
}
|
| 307 |
greg |
2.24 |
/* open window */
|
| 308 |
greg |
2.23 |
ourwinattr.border_pixel = ourwhite;
|
| 309 |
|
|
ourwinattr.background_pixel = ourblack;
|
| 310 |
greg |
2.6 |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
|
| 311 |
|
|
ourvis.visual, AllocNone);
|
| 312 |
greg |
2.24 |
ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
|
| 313 |
|
|
ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
|
| 314 |
|
|
ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
|
| 315 |
|
|
wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
|
| 316 |
|
|
xszhints.width, xszhints.height, BORWIDTH,
|
| 317 |
|
|
ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
|
| 318 |
|
|
CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
|
| 319 |
greg |
1.4 |
if (wind == 0)
|
| 320 |
greg |
2.6 |
quiterr("cannot create window");
|
| 321 |
greg |
1.5 |
width = xmax;
|
| 322 |
|
|
height = ymax;
|
| 323 |
greg |
2.32 |
/* prepare graphics drawing context */
|
| 324 |
|
|
if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
|
| 325 |
|
|
quiterr("cannot get font");
|
| 326 |
greg |
2.24 |
xgcv.foreground = ourblack;
|
| 327 |
|
|
xgcv.background = ourwhite;
|
| 328 |
|
|
ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
|
| 329 |
|
|
GCFont, &xgcv);
|
| 330 |
|
|
xgcv.function = GXinvert;
|
| 331 |
|
|
revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
|
| 332 |
|
|
GCFunction, &xgcv);
|
| 333 |
|
|
|
| 334 |
|
|
/* set up the window manager */
|
| 335 |
|
|
xwmhints.flags = InputHint|IconPixmapHint;
|
| 336 |
|
|
xwmhints.input = True;
|
| 337 |
|
|
xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
|
| 338 |
greg |
1.17 |
wind, icondata, iconwidth, iconheight);
|
| 339 |
greg |
2.24 |
|
| 340 |
|
|
windowName.encoding = iconName.encoding = XA_STRING;
|
| 341 |
|
|
windowName.format = iconName.format = 8;
|
| 342 |
|
|
windowName.value = (u_char *)name;
|
| 343 |
|
|
windowName.nitems = strlen(windowName.value);
|
| 344 |
|
|
iconName.value = (u_char *)name;
|
| 345 |
|
|
iconName.nitems = strlen(windowName.value);
|
| 346 |
|
|
|
| 347 |
|
|
xclshints.res_name = NULL;
|
| 348 |
|
|
xclshints.res_class = "Ximage";
|
| 349 |
|
|
XSetWMProperties(thedisplay, wind, &windowName, &iconName,
|
| 350 |
|
|
argv, argc, &xszhints, &xwmhints, &xclshints);
|
| 351 |
|
|
closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
|
| 352 |
|
|
wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
|
| 353 |
|
|
XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
|
| 354 |
|
|
|
| 355 |
greg |
1.1 |
XMapWindow(thedisplay, wind);
|
| 356 |
|
|
return;
|
| 357 |
|
|
} /* end of init */
|
| 358 |
|
|
|
| 359 |
|
|
|
| 360 |
|
|
quiterr(err) /* print message and exit */
|
| 361 |
|
|
char *err;
|
| 362 |
|
|
{
|
| 363 |
greg |
2.31 |
register int es;
|
| 364 |
|
|
int cs;
|
| 365 |
|
|
|
| 366 |
|
|
if (es = err != NULL)
|
| 367 |
greg |
2.29 |
fprintf(stderr, "%s: %s: %s\n", progname,
|
| 368 |
|
|
fname==NULL?"<stdin>":fname, err);
|
| 369 |
greg |
2.31 |
if (parent > 0 & wind != 0) {
|
| 370 |
greg |
2.29 |
XDestroyWindow(thedisplay, wind);
|
| 371 |
|
|
XFlush(thedisplay);
|
| 372 |
greg |
2.32 |
} else if (parent < 0 & sigrecv == 0)
|
| 373 |
|
|
kill(getppid(), SIGCONT);
|
| 374 |
greg |
2.31 |
while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
|
| 375 |
|
|
if (es == 0)
|
| 376 |
|
|
es = cs>>8 & 0xff;
|
| 377 |
greg |
2.29 |
parent--;
|
| 378 |
greg |
2.31 |
}
|
| 379 |
|
|
exit(es);
|
| 380 |
greg |
1.1 |
}
|
| 381 |
|
|
|
| 382 |
|
|
|
| 383 |
greg |
2.6 |
static int
|
| 384 |
|
|
viscmp(v1,v2) /* compare visual to see which is better, descending */
|
| 385 |
|
|
register XVisualInfo *v1, *v2;
|
| 386 |
|
|
{
|
| 387 |
|
|
int bad1 = 0, bad2 = 0;
|
| 388 |
|
|
register int *rp;
|
| 389 |
|
|
|
| 390 |
|
|
if (v1->class == v2->class) {
|
| 391 |
|
|
if (v1->class == TrueColor || v1->class == DirectColor) {
|
| 392 |
|
|
/* prefer 24-bit to 32-bit */
|
| 393 |
|
|
if (v1->depth == 24 && v2->depth == 32)
|
| 394 |
|
|
return(-1);
|
| 395 |
|
|
if (v1->depth == 32 && v2->depth == 24)
|
| 396 |
|
|
return(1);
|
| 397 |
|
|
return(0);
|
| 398 |
|
|
}
|
| 399 |
|
|
/* don't be too greedy */
|
| 400 |
|
|
if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
|
| 401 |
|
|
return(v1->depth - v2->depth);
|
| 402 |
|
|
return(v2->depth - v1->depth);
|
| 403 |
|
|
}
|
| 404 |
|
|
/* prefer Pseudo when < 24-bit */
|
| 405 |
|
|
if ((v1->class == TrueColor || v1->class == DirectColor) &&
|
| 406 |
|
|
v1->depth < 24)
|
| 407 |
|
|
bad1 = 1;
|
| 408 |
|
|
if ((v2->class == TrueColor || v2->class == DirectColor) &&
|
| 409 |
|
|
v2->depth < 24)
|
| 410 |
|
|
bad2 = -1;
|
| 411 |
|
|
if (bad1 | bad2)
|
| 412 |
|
|
return(bad1+bad2);
|
| 413 |
|
|
/* otherwise, use class ranking */
|
| 414 |
|
|
for (rp = ourrank; *rp != -1; rp++) {
|
| 415 |
|
|
if (v1->class == *rp)
|
| 416 |
|
|
return(-1);
|
| 417 |
|
|
if (v2->class == *rp)
|
| 418 |
|
|
return(1);
|
| 419 |
|
|
}
|
| 420 |
|
|
return(0);
|
| 421 |
|
|
}
|
| 422 |
|
|
|
| 423 |
|
|
|
| 424 |
|
|
getbestvis() /* get the best visual for this screen */
|
| 425 |
|
|
{
|
| 426 |
greg |
2.7 |
#ifdef DEBUG
|
| 427 |
greg |
2.6 |
static char vistype[][12] = {
|
| 428 |
|
|
"StaticGray",
|
| 429 |
|
|
"GrayScale",
|
| 430 |
|
|
"StaticColor",
|
| 431 |
|
|
"PseudoColor",
|
| 432 |
|
|
"TrueColor",
|
| 433 |
|
|
"DirectColor"
|
| 434 |
|
|
};
|
| 435 |
greg |
2.7 |
#endif
|
| 436 |
greg |
2.6 |
static int rankings[3][6] = {
|
| 437 |
|
|
{TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
|
| 438 |
greg |
2.7 |
{PseudoColor,GrayScale,StaticGray,-1},
|
| 439 |
|
|
{PseudoColor,GrayScale,StaticGray,-1}
|
| 440 |
greg |
2.6 |
};
|
| 441 |
|
|
XVisualInfo *xvi;
|
| 442 |
|
|
int vismatched;
|
| 443 |
|
|
register int i, j;
|
| 444 |
|
|
|
| 445 |
|
|
if (greyscale) {
|
| 446 |
|
|
ourrank = rankings[2];
|
| 447 |
|
|
if (maxcolors < 2) maxcolors = 256;
|
| 448 |
|
|
} else if (maxcolors >= 2 && maxcolors <= 256)
|
| 449 |
|
|
ourrank = rankings[1];
|
| 450 |
|
|
else {
|
| 451 |
|
|
ourrank = rankings[0];
|
| 452 |
|
|
maxcolors = 256;
|
| 453 |
|
|
}
|
| 454 |
|
|
/* find best visual */
|
| 455 |
|
|
ourvis.screen = ourscreen;
|
| 456 |
|
|
xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
|
| 457 |
|
|
if (xvi == NULL)
|
| 458 |
|
|
quiterr("no visuals for this screen!");
|
| 459 |
greg |
2.7 |
#ifdef DEBUG
|
| 460 |
|
|
fprintf(stderr, "Supported visuals:\n");
|
| 461 |
|
|
for (i = 0; i < vismatched; i++)
|
| 462 |
|
|
fprintf(stderr, "\ttype %s, depth %d\n",
|
| 463 |
|
|
vistype[xvi[i].class], xvi[i].depth);
|
| 464 |
|
|
#endif
|
| 465 |
greg |
2.6 |
for (i = 0, j = 1; j < vismatched; j++)
|
| 466 |
|
|
if (viscmp(&xvi[i],&xvi[j]) > 0)
|
| 467 |
|
|
i = j;
|
| 468 |
|
|
/* compare to least acceptable */
|
| 469 |
|
|
for (j = 0; ourrank[j++] != -1; )
|
| 470 |
|
|
;
|
| 471 |
|
|
ourvis.class = ourrank[--j];
|
| 472 |
|
|
ourvis.depth = 1;
|
| 473 |
|
|
if (viscmp(&xvi[i],&ourvis) > 0)
|
| 474 |
|
|
quiterr("inadequate visuals on this screen");
|
| 475 |
|
|
/* OK, we'll use it */
|
| 476 |
|
|
copystruct(&ourvis, &xvi[i]);
|
| 477 |
greg |
2.7 |
#ifdef DEBUG
|
| 478 |
|
|
fprintf(stderr, "Selected visual type %s, depth %d\n",
|
| 479 |
|
|
vistype[ourvis.class], ourvis.depth);
|
| 480 |
|
|
#endif
|
| 481 |
greg |
2.8 |
/* make appropriate adjustments */
|
| 482 |
greg |
2.6 |
if (ourvis.class == GrayScale || ourvis.class == StaticGray)
|
| 483 |
|
|
greyscale = 1;
|
| 484 |
greg |
2.7 |
if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
|
| 485 |
|
|
maxcolors = ourvis.colormap_size;
|
| 486 |
greg |
2.8 |
if (ourvis.class == StaticGray) {
|
| 487 |
|
|
ourblack = 0;
|
| 488 |
|
|
ourwhite = 255;
|
| 489 |
|
|
} else if (ourvis.class == PseudoColor) {
|
| 490 |
|
|
ourblack = BlackPixel(thedisplay,ourscreen);
|
| 491 |
|
|
ourwhite = WhitePixel(thedisplay,ourscreen);
|
| 492 |
greg |
2.9 |
if ((ourblack|ourwhite) & ~255L) {
|
| 493 |
|
|
ourblack = 0;
|
| 494 |
|
|
ourwhite = 1;
|
| 495 |
|
|
}
|
| 496 |
greg |
2.13 |
if (maxcolors > 4)
|
| 497 |
|
|
maxcolors -= 2;
|
| 498 |
greg |
2.8 |
} else {
|
| 499 |
|
|
ourblack = 0;
|
| 500 |
greg |
2.9 |
ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
|
| 501 |
greg |
2.8 |
}
|
| 502 |
greg |
2.6 |
XFree((char *)xvi);
|
| 503 |
|
|
}
|
| 504 |
|
|
|
| 505 |
|
|
|
| 506 |
greg |
1.1 |
getras() /* get raster file */
|
| 507 |
|
|
{
|
| 508 |
|
|
XVisualInfo vinfo;
|
| 509 |
|
|
|
| 510 |
|
|
if (maxcolors <= 2) { /* monochrome */
|
| 511 |
|
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
|
| 512 |
|
|
if (ourdata == NULL)
|
| 513 |
|
|
goto fail;
|
| 514 |
greg |
2.6 |
ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
|
| 515 |
greg |
1.1 |
xmax, ymax, 8);
|
| 516 |
|
|
if (ourras == NULL)
|
| 517 |
|
|
goto fail;
|
| 518 |
|
|
getmono();
|
| 519 |
greg |
2.13 |
} else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
|
| 520 |
greg |
2.28 |
ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
|
| 521 |
greg |
1.1 |
if (ourdata == NULL)
|
| 522 |
|
|
goto fail;
|
| 523 |
greg |
2.28 |
ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
|
| 524 |
greg |
2.6 |
ourdata, xmax, ymax, 32);
|
| 525 |
greg |
1.1 |
if (ourras == NULL)
|
| 526 |
|
|
goto fail;
|
| 527 |
|
|
getfull();
|
| 528 |
|
|
} else {
|
| 529 |
|
|
ourdata = (unsigned char *)malloc(xmax*ymax);
|
| 530 |
|
|
if (ourdata == NULL)
|
| 531 |
|
|
goto fail;
|
| 532 |
greg |
2.6 |
ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
|
| 533 |
greg |
1.1 |
xmax, ymax, 8);
|
| 534 |
|
|
if (ourras == NULL)
|
| 535 |
|
|
goto fail;
|
| 536 |
greg |
2.13 |
if (greyscale | ourvis.class == StaticGray)
|
| 537 |
greg |
2.7 |
getgrey();
|
| 538 |
greg |
2.13 |
else
|
| 539 |
|
|
getmapped();
|
| 540 |
|
|
if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
|
| 541 |
|
|
goto fail;
|
| 542 |
greg |
1.1 |
}
|
| 543 |
greg |
2.13 |
return;
|
| 544 |
greg |
1.1 |
fail:
|
| 545 |
greg |
1.9 |
quiterr("could not create raster image");
|
| 546 |
greg |
1.1 |
}
|
| 547 |
|
|
|
| 548 |
|
|
|
| 549 |
|
|
getevent() /* process the next event */
|
| 550 |
|
|
{
|
| 551 |
greg |
2.24 |
XEvent xev;
|
| 552 |
greg |
1.1 |
|
| 553 |
greg |
2.24 |
XNextEvent(thedisplay, &xev);
|
| 554 |
|
|
switch ((int)xev.type) {
|
| 555 |
greg |
1.1 |
case KeyPress:
|
| 556 |
greg |
2.24 |
docom(&xev.xkey);
|
| 557 |
greg |
1.1 |
break;
|
| 558 |
|
|
case ConfigureNotify:
|
| 559 |
greg |
2.24 |
width = xev.xconfigure.width;
|
| 560 |
|
|
height = xev.xconfigure.height;
|
| 561 |
greg |
1.1 |
break;
|
| 562 |
|
|
case MapNotify:
|
| 563 |
|
|
map_rcolors(ourras, wind);
|
| 564 |
|
|
if (fast)
|
| 565 |
greg |
2.6 |
make_rpixmap(ourras, wind);
|
| 566 |
greg |
1.1 |
break;
|
| 567 |
|
|
case UnmapNotify:
|
| 568 |
greg |
2.7 |
if (!fast)
|
| 569 |
|
|
unmap_rcolors(ourras);
|
| 570 |
greg |
1.1 |
break;
|
| 571 |
|
|
case Expose:
|
| 572 |
greg |
2.24 |
redraw(xev.xexpose.x, xev.xexpose.y,
|
| 573 |
|
|
xev.xexpose.width, xev.xexpose.height);
|
| 574 |
greg |
1.1 |
break;
|
| 575 |
|
|
case ButtonPress:
|
| 576 |
greg |
2.24 |
if (xev.xbutton.state & (ShiftMask|ControlMask))
|
| 577 |
|
|
moveimage(&xev.xbutton);
|
| 578 |
|
|
else if (xev.xbutton.button == Button2)
|
| 579 |
|
|
traceray(xev.xbutton.x, xev.xbutton.y);
|
| 580 |
greg |
1.1 |
else
|
| 581 |
greg |
2.24 |
getbox(&xev.xbutton);
|
| 582 |
greg |
1.1 |
break;
|
| 583 |
greg |
2.29 |
case ClientMessage:
|
| 584 |
greg |
2.24 |
if ((xev.xclient.message_type == wmProtocolsAtom) &&
|
| 585 |
|
|
(xev.xclient.data.l[0] == closedownAtom))
|
| 586 |
|
|
quiterr(NULL);
|
| 587 |
|
|
break;
|
| 588 |
greg |
1.1 |
}
|
| 589 |
|
|
}
|
| 590 |
|
|
|
| 591 |
|
|
|
| 592 |
greg |
2.22 |
traceray(xpos, ypos) /* print ray corresponding to pixel */
|
| 593 |
|
|
int xpos, ypos;
|
| 594 |
|
|
{
|
| 595 |
|
|
FLOAT hv[2];
|
| 596 |
|
|
FVECT rorg, rdir;
|
| 597 |
|
|
|
| 598 |
|
|
if (!gotview) { /* no view, no can do */
|
| 599 |
|
|
XBell(thedisplay, 0);
|
| 600 |
|
|
return(-1);
|
| 601 |
|
|
}
|
| 602 |
|
|
pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
|
| 603 |
|
|
if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
|
| 604 |
|
|
return(-1);
|
| 605 |
|
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
|
| 606 |
|
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
|
| 607 |
|
|
fflush(stdout);
|
| 608 |
|
|
return(0);
|
| 609 |
|
|
}
|
| 610 |
|
|
|
| 611 |
|
|
|
| 612 |
|
|
docom(ekey) /* execute command */
|
| 613 |
greg |
1.1 |
XKeyPressedEvent *ekey;
|
| 614 |
|
|
{
|
| 615 |
|
|
char buf[80];
|
| 616 |
|
|
COLOR cval;
|
| 617 |
|
|
XColor cvx;
|
| 618 |
|
|
int com, n;
|
| 619 |
|
|
double comp;
|
| 620 |
greg |
1.26 |
FLOAT hv[2];
|
| 621 |
greg |
1.1 |
|
| 622 |
|
|
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
|
| 623 |
|
|
if (n == 0)
|
| 624 |
|
|
return(0);
|
| 625 |
|
|
com = buf[0];
|
| 626 |
|
|
switch (com) { /* interpret command */
|
| 627 |
|
|
case 'q':
|
| 628 |
greg |
2.22 |
case 'Q':
|
| 629 |
greg |
2.3 |
case CTRL('D'): /* quit */
|
| 630 |
greg |
2.21 |
quiterr(NULL);
|
| 631 |
greg |
1.1 |
case '\n':
|
| 632 |
|
|
case '\r':
|
| 633 |
|
|
case 'l':
|
| 634 |
|
|
case 'c': /* value */
|
| 635 |
|
|
if (avgbox(cval) == -1)
|
| 636 |
|
|
return(-1);
|
| 637 |
|
|
switch (com) {
|
| 638 |
|
|
case '\n':
|
| 639 |
|
|
case '\r': /* radiance */
|
| 640 |
|
|
sprintf(buf, "%.3f", intens(cval)/exposure);
|
| 641 |
|
|
break;
|
| 642 |
|
|
case 'l': /* luminance */
|
| 643 |
greg |
1.16 |
sprintf(buf, "%.0fL", luminance(cval)/exposure);
|
| 644 |
greg |
1.1 |
break;
|
| 645 |
|
|
case 'c': /* color */
|
| 646 |
|
|
comp = pow(2.0, (double)scale);
|
| 647 |
|
|
sprintf(buf, "(%.2f,%.2f,%.2f)",
|
| 648 |
|
|
colval(cval,RED)*comp,
|
| 649 |
|
|
colval(cval,GRN)*comp,
|
| 650 |
|
|
colval(cval,BLU)*comp);
|
| 651 |
|
|
break;
|
| 652 |
|
|
}
|
| 653 |
greg |
1.3 |
XDrawImageString(thedisplay, wind, ourgc,
|
| 654 |
|
|
box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
|
| 655 |
greg |
1.1 |
return(0);
|
| 656 |
|
|
case 'i': /* identify (contour) */
|
| 657 |
|
|
if (ourras->pixels == NULL)
|
| 658 |
|
|
return(-1);
|
| 659 |
|
|
n = ourdata[ekey->x-xoff+xmax*(ekey->y-yoff)];
|
| 660 |
|
|
n = ourras->pmap[n];
|
| 661 |
|
|
cvx.pixel = ourras->cdefs[n].pixel;
|
| 662 |
|
|
cvx.red = random() & 65535;
|
| 663 |
|
|
cvx.green = random() & 65535;
|
| 664 |
|
|
cvx.blue = random() & 65535;
|
| 665 |
greg |
1.2 |
cvx.flags = DoRed|DoGreen|DoBlue;
|
| 666 |
|
|
XStoreColor(thedisplay, ourras->cmap, &cvx);
|
| 667 |
greg |
1.1 |
return(0);
|
| 668 |
|
|
case 'p': /* position */
|
| 669 |
greg |
1.26 |
pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
|
| 670 |
|
|
sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
|
| 671 |
|
|
(int)(hv[1]*inpres.yr));
|
| 672 |
greg |
1.1 |
XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
|
| 673 |
|
|
buf, strlen(buf));
|
| 674 |
|
|
return(0);
|
| 675 |
|
|
case 't': /* trace */
|
| 676 |
greg |
2.22 |
return(traceray(ekey->x, ekey->y));
|
| 677 |
greg |
1.1 |
case '=': /* adjust exposure */
|
| 678 |
greg |
2.25 |
case '@': /* adaptation level */
|
| 679 |
greg |
1.1 |
if (avgbox(cval) == -1)
|
| 680 |
|
|
return(-1);
|
| 681 |
greg |
2.34 |
comp = bright(cval);
|
| 682 |
|
|
if (comp < 1e-20) {
|
| 683 |
|
|
XBell(thedisplay, 0);
|
| 684 |
|
|
return(-1);
|
| 685 |
|
|
}
|
| 686 |
|
|
if (com == '@')
|
| 687 |
|
|
comp = 106./exposure/
|
| 688 |
|
|
pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
|
| 689 |
|
|
else
|
| 690 |
|
|
comp = .5/comp;
|
| 691 |
greg |
2.25 |
comp = log(comp)/.69315 - scale;
|
| 692 |
|
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */
|
| 693 |
greg |
1.1 |
if (n == 0)
|
| 694 |
|
|
return(0);
|
| 695 |
|
|
scale_rcolors(ourras, pow(2.0, (double)n));
|
| 696 |
|
|
scale += n;
|
| 697 |
|
|
sprintf(buf, "%+d", scale);
|
| 698 |
greg |
1.3 |
XDrawImageString(thedisplay, wind, ourgc,
|
| 699 |
|
|
box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
|
| 700 |
greg |
1.1 |
XFlush(thedisplay);
|
| 701 |
|
|
free(ourdata);
|
| 702 |
|
|
free_raster(ourras);
|
| 703 |
|
|
getras();
|
| 704 |
|
|
/* fall through */
|
| 705 |
greg |
2.3 |
case CTRL('R'): /* redraw */
|
| 706 |
|
|
case CTRL('L'):
|
| 707 |
greg |
1.1 |
unmap_rcolors(ourras);
|
| 708 |
|
|
XClearWindow(thedisplay, wind);
|
| 709 |
|
|
map_rcolors(ourras, wind);
|
| 710 |
|
|
if (fast)
|
| 711 |
greg |
2.12 |
make_rpixmap(ourras, wind);
|
| 712 |
greg |
1.1 |
redraw(0, 0, width, height);
|
| 713 |
|
|
return(0);
|
| 714 |
greg |
2.27 |
case 'f': /* turn on fast redraw */
|
| 715 |
|
|
fast = 1;
|
| 716 |
|
|
make_rpixmap(ourras, wind);
|
| 717 |
|
|
return(0);
|
| 718 |
|
|
case 'F': /* turn off fast redraw */
|
| 719 |
|
|
fast = 0;
|
| 720 |
|
|
free_rpixmap(ourras);
|
| 721 |
|
|
return(0);
|
| 722 |
greg |
2.19 |
case '0': /* recenter origin */
|
| 723 |
|
|
if (xoff == 0 & yoff == 0)
|
| 724 |
|
|
return(0);
|
| 725 |
|
|
xoff = yoff = 0;
|
| 726 |
|
|
XClearWindow(thedisplay, wind);
|
| 727 |
|
|
redraw(0, 0, width, height);
|
| 728 |
|
|
return(0);
|
| 729 |
greg |
1.1 |
case ' ': /* clear */
|
| 730 |
|
|
redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
|
| 731 |
|
|
return(0);
|
| 732 |
|
|
default:
|
| 733 |
greg |
1.2 |
XBell(thedisplay, 0);
|
| 734 |
greg |
1.1 |
return(-1);
|
| 735 |
|
|
}
|
| 736 |
|
|
}
|
| 737 |
|
|
|
| 738 |
|
|
|
| 739 |
greg |
1.2 |
moveimage(ebut) /* shift the image */
|
| 740 |
|
|
XButtonPressedEvent *ebut;
|
| 741 |
greg |
1.1 |
{
|
| 742 |
greg |
2.24 |
XEvent e;
|
| 743 |
greg |
1.5 |
int mxo, myo;
|
| 744 |
greg |
1.1 |
|
| 745 |
greg |
2.24 |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
|
| 746 |
|
|
while (e.type == MotionNotify) {
|
| 747 |
|
|
mxo = e.xmotion.x;
|
| 748 |
|
|
myo = e.xmotion.y;
|
| 749 |
greg |
1.5 |
revline(ebut->x, ebut->y, mxo, myo);
|
| 750 |
|
|
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
|
| 751 |
|
|
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
|
| 752 |
greg |
2.24 |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
|
| 753 |
greg |
1.5 |
revline(ebut->x, ebut->y, mxo, myo);
|
| 754 |
|
|
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
|
| 755 |
|
|
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
|
| 756 |
greg |
1.3 |
}
|
| 757 |
greg |
2.24 |
xoff += e.xbutton.x - ebut->x;
|
| 758 |
|
|
yoff += e.xbutton.y - ebut->y;
|
| 759 |
greg |
1.1 |
XClearWindow(thedisplay, wind);
|
| 760 |
|
|
redraw(0, 0, width, height);
|
| 761 |
|
|
}
|
| 762 |
|
|
|
| 763 |
|
|
|
| 764 |
|
|
getbox(ebut) /* get new box */
|
| 765 |
|
|
XButtonPressedEvent *ebut;
|
| 766 |
|
|
{
|
| 767 |
greg |
2.24 |
XEvent e;
|
| 768 |
greg |
1.1 |
|
| 769 |
greg |
2.24 |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
|
| 770 |
|
|
while (e.type == MotionNotify) {
|
| 771 |
|
|
revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
|
| 772 |
|
|
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
|
| 773 |
greg |
1.1 |
revbox(ebut->x, ebut->y, box.xmin, box.ymin);
|
| 774 |
|
|
}
|
| 775 |
greg |
2.24 |
box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
|
| 776 |
|
|
box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
|
| 777 |
greg |
1.1 |
if (box.xmin > ebut->x) {
|
| 778 |
|
|
box.xsiz = box.xmin - ebut->x + 1;
|
| 779 |
|
|
box.xmin = ebut->x;
|
| 780 |
|
|
} else {
|
| 781 |
|
|
box.xsiz = ebut->x - box.xmin + 1;
|
| 782 |
|
|
}
|
| 783 |
|
|
if (box.ymin > ebut->y) {
|
| 784 |
|
|
box.ysiz = box.ymin - ebut->y + 1;
|
| 785 |
|
|
box.ymin = ebut->y;
|
| 786 |
|
|
} else {
|
| 787 |
|
|
box.ysiz = ebut->y - box.ymin + 1;
|
| 788 |
|
|
}
|
| 789 |
|
|
}
|
| 790 |
|
|
|
| 791 |
|
|
|
| 792 |
|
|
revbox(x0, y0, x1, y1) /* draw box with reversed lines */
|
| 793 |
|
|
int x0, y0, x1, y1;
|
| 794 |
|
|
{
|
| 795 |
greg |
1.5 |
revline(x0, y0, x1, y0);
|
| 796 |
|
|
revline(x0, y1, x1, y1);
|
| 797 |
|
|
revline(x0, y0, x0, y1);
|
| 798 |
|
|
revline(x1, y0, x1, y1);
|
| 799 |
|
|
}
|
| 800 |
greg |
1.1 |
|
| 801 |
|
|
|
| 802 |
|
|
avgbox(clr) /* average color over current box */
|
| 803 |
|
|
COLOR clr;
|
| 804 |
|
|
{
|
| 805 |
greg |
1.25 |
static COLOR lc;
|
| 806 |
|
|
static int ll, lr, lt, lb;
|
| 807 |
greg |
1.1 |
int left, right, top, bottom;
|
| 808 |
|
|
int y;
|
| 809 |
|
|
double d;
|
| 810 |
|
|
COLOR ctmp;
|
| 811 |
|
|
register int x;
|
| 812 |
|
|
|
| 813 |
|
|
setcolor(clr, 0.0, 0.0, 0.0);
|
| 814 |
|
|
left = box.xmin - xoff;
|
| 815 |
|
|
right = left + box.xsiz;
|
| 816 |
|
|
if (left < 0)
|
| 817 |
|
|
left = 0;
|
| 818 |
|
|
if (right > xmax)
|
| 819 |
|
|
right = xmax;
|
| 820 |
|
|
if (left >= right)
|
| 821 |
|
|
return(-1);
|
| 822 |
|
|
top = box.ymin - yoff;
|
| 823 |
|
|
bottom = top + box.ysiz;
|
| 824 |
|
|
if (top < 0)
|
| 825 |
|
|
top = 0;
|
| 826 |
|
|
if (bottom > ymax)
|
| 827 |
|
|
bottom = ymax;
|
| 828 |
|
|
if (top >= bottom)
|
| 829 |
|
|
return(-1);
|
| 830 |
greg |
1.25 |
if (left == ll && right == lr && top == lt && bottom == lb) {
|
| 831 |
|
|
copycolor(clr, lc);
|
| 832 |
greg |
2.15 |
return(0);
|
| 833 |
greg |
1.25 |
}
|
| 834 |
greg |
1.1 |
for (y = top; y < bottom; y++) {
|
| 835 |
|
|
if (getscan(y) == -1)
|
| 836 |
|
|
return(-1);
|
| 837 |
|
|
for (x = left; x < right; x++) {
|
| 838 |
|
|
colr_color(ctmp, scanline[x]);
|
| 839 |
|
|
addcolor(clr, ctmp);
|
| 840 |
|
|
}
|
| 841 |
|
|
}
|
| 842 |
|
|
d = 1.0/((right-left)*(bottom-top));
|
| 843 |
|
|
scalecolor(clr, d);
|
| 844 |
greg |
1.25 |
ll = left; lr = right; lt = top; lb = bottom;
|
| 845 |
|
|
copycolor(lc, clr);
|
| 846 |
greg |
1.1 |
return(0);
|
| 847 |
|
|
}
|
| 848 |
|
|
|
| 849 |
|
|
|
| 850 |
|
|
getmono() /* get monochrome data */
|
| 851 |
|
|
{
|
| 852 |
|
|
register unsigned char *dp;
|
| 853 |
|
|
register int x, err;
|
| 854 |
greg |
1.23 |
int y, errp;
|
| 855 |
greg |
1.1 |
short *cerr;
|
| 856 |
|
|
|
| 857 |
greg |
1.10 |
if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
|
| 858 |
greg |
1.9 |
quiterr("out of memory in getmono");
|
| 859 |
greg |
1.1 |
dp = ourdata - 1;
|
| 860 |
|
|
for (y = 0; y < ymax; y++) {
|
| 861 |
greg |
2.15 |
getscan(y);
|
| 862 |
greg |
2.16 |
add2icon(y, scanline);
|
| 863 |
greg |
1.10 |
normcolrs(scanline, xmax, scale);
|
| 864 |
greg |
1.1 |
err = 0;
|
| 865 |
|
|
for (x = 0; x < xmax; x++) {
|
| 866 |
|
|
if (!(x&7))
|
| 867 |
|
|
*++dp = 0;
|
| 868 |
greg |
1.23 |
errp = err;
|
| 869 |
greg |
1.10 |
err += normbright(scanline[x]) + cerr[x];
|
| 870 |
greg |
1.1 |
if (err > 127)
|
| 871 |
|
|
err -= 255;
|
| 872 |
|
|
else
|
| 873 |
greg |
1.3 |
*dp |= 1<<(7-(x&07));
|
| 874 |
greg |
1.23 |
err /= 3;
|
| 875 |
|
|
cerr[x] = err + errp;
|
| 876 |
greg |
1.1 |
}
|
| 877 |
|
|
}
|
| 878 |
|
|
free((char *)cerr);
|
| 879 |
|
|
}
|
| 880 |
|
|
|
| 881 |
|
|
|
| 882 |
greg |
1.17 |
add2icon(y, scan) /* add a scanline to our icon data */
|
| 883 |
|
|
int y;
|
| 884 |
|
|
COLR *scan;
|
| 885 |
|
|
{
|
| 886 |
|
|
static short cerr[ICONSIZ];
|
| 887 |
greg |
1.20 |
static int ynext;
|
| 888 |
|
|
static char *dp;
|
| 889 |
greg |
2.17 |
COLR clr;
|
| 890 |
greg |
1.17 |
register int err;
|
| 891 |
greg |
1.20 |
register int x, ti;
|
| 892 |
greg |
1.23 |
int errp;
|
| 893 |
greg |
1.17 |
|
| 894 |
|
|
if (iconheight == 0) { /* initialize */
|
| 895 |
greg |
1.18 |
if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
|
| 896 |
greg |
1.17 |
iconwidth = xmax;
|
| 897 |
|
|
iconheight = ymax;
|
| 898 |
|
|
} else if (xmax > ymax) {
|
| 899 |
|
|
iconwidth = ICONSIZ;
|
| 900 |
|
|
iconheight = ICONSIZ*ymax/xmax;
|
| 901 |
greg |
1.24 |
if (iconheight < 1)
|
| 902 |
|
|
iconheight = 1;
|
| 903 |
greg |
1.17 |
} else {
|
| 904 |
|
|
iconwidth = ICONSIZ*xmax/ymax;
|
| 905 |
greg |
1.24 |
if (iconwidth < 1)
|
| 906 |
|
|
iconwidth = 1;
|
| 907 |
greg |
1.17 |
iconheight = ICONSIZ;
|
| 908 |
|
|
}
|
| 909 |
greg |
1.20 |
ynext = 0;
|
| 910 |
greg |
1.17 |
dp = icondata - 1;
|
| 911 |
|
|
}
|
| 912 |
greg |
1.20 |
if (y < ynext*ymax/iconheight) /* skip this one */
|
| 913 |
greg |
1.17 |
return;
|
| 914 |
|
|
err = 0;
|
| 915 |
|
|
for (x = 0; x < iconwidth; x++) {
|
| 916 |
|
|
if (!(x&7))
|
| 917 |
|
|
*++dp = 0;
|
| 918 |
greg |
1.23 |
errp = err;
|
| 919 |
greg |
1.20 |
ti = x*xmax/iconwidth;
|
| 920 |
greg |
2.17 |
copycolr(clr, scan[ti]);
|
| 921 |
|
|
normcolrs(clr, 1, scale);
|
| 922 |
|
|
err += normbright(clr) + cerr[x];
|
| 923 |
greg |
1.17 |
if (err > 127)
|
| 924 |
|
|
err -= 255;
|
| 925 |
|
|
else
|
| 926 |
|
|
*dp |= 1<<(x&07);
|
| 927 |
greg |
1.23 |
err /= 3;
|
| 928 |
|
|
cerr[x] = err + errp;
|
| 929 |
greg |
1.17 |
}
|
| 930 |
greg |
1.20 |
ynext++;
|
| 931 |
greg |
1.17 |
}
|
| 932 |
|
|
|
| 933 |
|
|
|
| 934 |
greg |
1.1 |
getfull() /* get full (24-bit) data */
|
| 935 |
|
|
{
|
| 936 |
|
|
int y;
|
| 937 |
greg |
2.28 |
register unsigned int4 *dp;
|
| 938 |
greg |
1.10 |
register int x;
|
| 939 |
|
|
/* set gamma correction */
|
| 940 |
|
|
setcolrgam(gamcor);
|
| 941 |
|
|
/* read and convert file */
|
| 942 |
greg |
2.28 |
dp = (unsigned int4 *)ourdata;
|
| 943 |
greg |
1.10 |
for (y = 0; y < ymax; y++) {
|
| 944 |
greg |
2.15 |
getscan(y);
|
| 945 |
greg |
2.16 |
add2icon(y, scanline);
|
| 946 |
greg |
1.10 |
if (scale)
|
| 947 |
|
|
shiftcolrs(scanline, xmax, scale);
|
| 948 |
|
|
colrs_gambs(scanline, xmax);
|
| 949 |
greg |
2.6 |
if (ourras->image->blue_mask & 1)
|
| 950 |
|
|
for (x = 0; x < xmax; x++)
|
| 951 |
greg |
2.35 |
*dp++ = (unsigned int4)scanline[x][RED] << 16 |
|
| 952 |
|
|
(unsigned int4)scanline[x][GRN] << 8 |
|
| 953 |
|
|
(unsigned int4)scanline[x][BLU] ;
|
| 954 |
greg |
2.6 |
else
|
| 955 |
|
|
for (x = 0; x < xmax; x++)
|
| 956 |
greg |
2.35 |
*dp++ = (unsigned int4)scanline[x][RED] |
|
| 957 |
|
|
(unsigned int4)scanline[x][GRN] << 8 |
|
| 958 |
|
|
(unsigned int4)scanline[x][BLU] << 16 ;
|
| 959 |
greg |
1.10 |
}
|
| 960 |
greg |
1.1 |
}
|
| 961 |
|
|
|
| 962 |
|
|
|
| 963 |
greg |
2.7 |
getgrey() /* get greyscale data */
|
| 964 |
|
|
{
|
| 965 |
|
|
int y;
|
| 966 |
|
|
register unsigned char *dp;
|
| 967 |
|
|
register int x;
|
| 968 |
|
|
/* set gamma correction */
|
| 969 |
|
|
setcolrgam(gamcor);
|
| 970 |
|
|
/* read and convert file */
|
| 971 |
|
|
dp = ourdata;
|
| 972 |
|
|
for (y = 0; y < ymax; y++) {
|
| 973 |
greg |
2.15 |
getscan(y);
|
| 974 |
greg |
2.16 |
add2icon(y, scanline);
|
| 975 |
greg |
2.7 |
if (scale)
|
| 976 |
|
|
shiftcolrs(scanline, xmax, scale);
|
| 977 |
greg |
2.18 |
for (x = 0; x < xmax; x++)
|
| 978 |
|
|
scanline[x][GRN] = normbright(scanline[x]);
|
| 979 |
greg |
2.7 |
colrs_gambs(scanline, xmax);
|
| 980 |
greg |
2.13 |
if (maxcolors < 256)
|
| 981 |
greg |
2.7 |
for (x = 0; x < xmax; x++)
|
| 982 |
greg |
2.18 |
*dp++ = ((long)scanline[x][GRN] *
|
| 983 |
|
|
maxcolors + maxcolors/2) >> 8;
|
| 984 |
greg |
2.7 |
else
|
| 985 |
|
|
for (x = 0; x < xmax; x++)
|
| 986 |
greg |
2.18 |
*dp++ = scanline[x][GRN];
|
| 987 |
greg |
2.7 |
}
|
| 988 |
greg |
2.13 |
for (x = 0; x < maxcolors; x++)
|
| 989 |
|
|
clrtab[x][RED] = clrtab[x][GRN] =
|
| 990 |
greg |
2.18 |
clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
|
| 991 |
greg |
2.7 |
}
|
| 992 |
|
|
|
| 993 |
|
|
|
| 994 |
greg |
2.13 |
getmapped() /* get color-mapped data */
|
| 995 |
|
|
{
|
| 996 |
|
|
int y;
|
| 997 |
greg |
2.30 |
/* make sure we can do it first */
|
| 998 |
|
|
if (fname == NULL)
|
| 999 |
|
|
quiterr("cannot map colors from standard input");
|
| 1000 |
greg |
2.13 |
/* set gamma correction */
|
| 1001 |
|
|
setcolrgam(gamcor);
|
| 1002 |
|
|
/* make histogram */
|
| 1003 |
|
|
new_histo();
|
| 1004 |
|
|
for (y = 0; y < ymax; y++) {
|
| 1005 |
|
|
if (getscan(y) < 0)
|
| 1006 |
greg |
2.30 |
break;
|
| 1007 |
greg |
2.16 |
add2icon(y, scanline);
|
| 1008 |
greg |
2.13 |
if (scale)
|
| 1009 |
|
|
shiftcolrs(scanline, xmax, scale);
|
| 1010 |
|
|
colrs_gambs(scanline, xmax);
|
| 1011 |
|
|
cnt_colrs(scanline, xmax);
|
| 1012 |
|
|
}
|
| 1013 |
|
|
/* map pixels */
|
| 1014 |
|
|
if (!new_clrtab(maxcolors))
|
| 1015 |
|
|
quiterr("cannot create color map");
|
| 1016 |
|
|
for (y = 0; y < ymax; y++) {
|
| 1017 |
greg |
2.30 |
getscan(y);
|
| 1018 |
greg |
2.13 |
if (scale)
|
| 1019 |
|
|
shiftcolrs(scanline, xmax, scale);
|
| 1020 |
|
|
colrs_gambs(scanline, xmax);
|
| 1021 |
|
|
if (dither)
|
| 1022 |
|
|
dith_colrs(ourdata+y*xmax, scanline, xmax);
|
| 1023 |
|
|
else
|
| 1024 |
|
|
map_colrs(ourdata+y*xmax, scanline, xmax);
|
| 1025 |
|
|
}
|
| 1026 |
|
|
}
|
| 1027 |
|
|
|
| 1028 |
|
|
|
| 1029 |
greg |
1.1 |
scale_rcolors(xr, sf) /* scale color map */
|
| 1030 |
|
|
register XRASTER *xr;
|
| 1031 |
|
|
double sf;
|
| 1032 |
|
|
{
|
| 1033 |
|
|
register int i;
|
| 1034 |
|
|
long maxv;
|
| 1035 |
|
|
|
| 1036 |
|
|
if (xr->pixels == NULL)
|
| 1037 |
|
|
return;
|
| 1038 |
|
|
|
| 1039 |
|
|
sf = pow(sf, 1.0/gamcor);
|
| 1040 |
|
|
maxv = 65535/sf;
|
| 1041 |
|
|
|
| 1042 |
|
|
for (i = xr->ncolors; i--; ) {
|
| 1043 |
|
|
xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
|
| 1044 |
|
|
65535 :
|
| 1045 |
|
|
xr->cdefs[i].red * sf;
|
| 1046 |
|
|
xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
|
| 1047 |
|
|
65535 :
|
| 1048 |
|
|
xr->cdefs[i].green * sf;
|
| 1049 |
|
|
xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
|
| 1050 |
|
|
65535 :
|
| 1051 |
|
|
xr->cdefs[i].blue * sf;
|
| 1052 |
|
|
}
|
| 1053 |
|
|
XStoreColors(thedisplay, xr->cmap, xr->cdefs, xr->ncolors);
|
| 1054 |
|
|
}
|
| 1055 |
|
|
|
| 1056 |
|
|
|
| 1057 |
|
|
getscan(y)
|
| 1058 |
|
|
int y;
|
| 1059 |
|
|
{
|
| 1060 |
greg |
2.30 |
static int trunced = -1; /* truncated file? */
|
| 1061 |
|
|
skipit:
|
| 1062 |
|
|
if (trunced >= 0 && y >= trunced) {
|
| 1063 |
|
|
bzero(scanline, xmax*sizeof(COLR));
|
| 1064 |
|
|
return(-1);
|
| 1065 |
|
|
}
|
| 1066 |
greg |
1.1 |
if (y != cury) {
|
| 1067 |
|
|
if (scanpos == NULL || scanpos[y] == -1)
|
| 1068 |
|
|
return(-1);
|
| 1069 |
|
|
if (fseek(fin, scanpos[y], 0) == -1)
|
| 1070 |
greg |
1.9 |
quiterr("fseek error");
|
| 1071 |
greg |
1.1 |
cury = y;
|
| 1072 |
greg |
2.11 |
} else if (scanpos != NULL && scanpos[y] == -1)
|
| 1073 |
greg |
1.1 |
scanpos[y] = ftell(fin);
|
| 1074 |
|
|
|
| 1075 |
greg |
2.30 |
if (freadcolrs(scanline, xmax, fin) < 0) {
|
| 1076 |
|
|
fprintf(stderr, "%s: %s: unfinished picture\n",
|
| 1077 |
|
|
progname, fname==NULL?"<stdin>":fname);
|
| 1078 |
|
|
trunced = y;
|
| 1079 |
|
|
goto skipit;
|
| 1080 |
|
|
}
|
| 1081 |
greg |
1.1 |
cury++;
|
| 1082 |
|
|
return(0);
|
| 1083 |
|
|
}
|