| 1 | 
< | 
/* Copyright (c) 1990 Regents of the University of California */ | 
| 1 | 
> | 
/* Copyright (c) 1992 Regents of the University of California */ | 
| 2 | 
  | 
 | 
| 3 | 
  | 
#ifndef lint | 
| 4 | 
  | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 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" | 
| 33 | 
  | 
 | 
| 34 | 
  | 
#define  FONTNAME       "8x13"          /* text font we'll use */ | 
| 35 | 
  | 
 | 
| 36 | 
< | 
#define  CTRL(c)        ('c'-'@') | 
| 36 | 
> | 
#define  CTRL(c)        ((c)-'@') | 
| 37 | 
  | 
 | 
| 38 | 
  | 
#define  BORWIDTH       5               /* border width */ | 
| 39 | 
  | 
 | 
| 40 | 
+ | 
#define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */ | 
| 41 | 
+ | 
 | 
| 42 | 
  | 
#define  ourscreen      DefaultScreen(thedisplay) | 
| 41 | 
– | 
#define  ourblack       BlackPixel(thedisplay,ourscreen) | 
| 42 | 
– | 
#define  ourwhite       WhitePixel(thedisplay,ourscreen) | 
| 43 | 
  | 
#define  ourroot        RootWindow(thedisplay,ourscreen) | 
| 44 | 
– | 
#define  ourgc          DefaultGC(thedisplay,ourscreen) | 
| 44 | 
  | 
 | 
| 45 | 
  | 
#define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) | 
| 46 | 
  | 
 | 
| 51 | 
  | 
int  dither = 1;                        /* dither colors? */ | 
| 52 | 
  | 
int  fast = 0;                          /* keep picture in Pixmap? */ | 
| 53 | 
  | 
 | 
| 54 | 
+ | 
char    *dispname = NULL;               /* our display name */ | 
| 55 | 
+ | 
 | 
| 56 | 
  | 
Window  wind = 0;                       /* our output window */ | 
| 57 | 
+ | 
unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */ | 
| 58 | 
  | 
Font  fontid;                           /* our font */ | 
| 59 | 
  | 
 | 
| 60 | 
  | 
int  maxcolors = 0;                     /* maximum colors */ | 
| 70 | 
  | 
 | 
| 71 | 
  | 
COLR  *scanline;                        /* scan line buffer */ | 
| 72 | 
  | 
 | 
| 73 | 
< | 
int  xmax, ymax;                        /* picture resolution */ | 
| 73 | 
> | 
RESOLU  inpres;                         /* input resolution and ordering */ | 
| 74 | 
> | 
int  xmax, ymax;                        /* picture dimensions */ | 
| 75 | 
  | 
int  width, height;                     /* window size */ | 
| 76 | 
  | 
char  *fname = NULL;                    /* input file name */ | 
| 77 | 
  | 
FILE  *fin = stdin;                     /* input file */ | 
| 80 | 
  | 
 | 
| 81 | 
  | 
double  exposure = 1.0;                 /* exposure compensation used */ | 
| 82 | 
  | 
 | 
| 83 | 
+ | 
int  wrongformat = 0;                   /* input in another format? */ | 
| 84 | 
+ | 
 | 
| 85 | 
+ | 
GC      ourgc;                          /* standard graphics context */ | 
| 86 | 
  | 
GC      revgc;                          /* graphics context with GXinvert */ | 
| 87 | 
  | 
 | 
| 88 | 
< | 
XRASTER *ourras;                        /* our stored image */ | 
| 88 | 
> | 
int             *ourrank;               /* our visual class ranking */ | 
| 89 | 
> | 
XVisualInfo     ourvis;                 /* our visual */ | 
| 90 | 
> | 
XRASTER         *ourras;                /* our stored image */ | 
| 91 | 
  | 
unsigned char   *ourdata;               /* our image data */ | 
| 92 | 
  | 
 | 
| 93 | 
  | 
struct { | 
| 96 | 
  | 
 | 
| 97 | 
  | 
char  *geometry = NULL;                 /* geometry specification */ | 
| 98 | 
  | 
 | 
| 99 | 
+ | 
char  icondata[ICONSIZ*ICONSIZ/8];      /* icon bitmap data */ | 
| 100 | 
+ | 
int  iconwidth = 0, iconheight = 0; | 
| 101 | 
+ | 
 | 
| 102 | 
  | 
char  *progname; | 
| 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 | 
  | 
 | 
| 99 | 
– | 
extern double  atof(), pow(), log(); | 
| 100 | 
– | 
 | 
| 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] == '-') | 
| 138 | 
  | 
                                maxcolors = 2; | 
| 139 | 
  | 
                                break; | 
| 140 | 
  | 
                        case 'd': | 
| 141 | 
< | 
                                dither = !dither; | 
| 141 | 
> | 
                                if (argv[i][2] == 'i') | 
| 142 | 
> | 
                                        dispname = argv[++i]; | 
| 143 | 
> | 
                                else | 
| 144 | 
> | 
                                        dither = !dither; | 
| 145 | 
  | 
                                break; | 
| 146 | 
  | 
                        case 'f': | 
| 147 | 
  | 
                                fast = !fast; | 
| 152 | 
  | 
                                scale = atoi(argv[++i]); | 
| 153 | 
  | 
                                break; | 
| 154 | 
  | 
                        case 'g': | 
| 155 | 
< | 
                                if (!strcmp(argv[i], "-geometry")) | 
| 155 | 
> | 
                                if (argv[i][2] == 'e') | 
| 156 | 
  | 
                                        geometry = argv[++i]; | 
| 157 | 
  | 
                                else | 
| 158 | 
  | 
                                        gamcor = atof(argv[++i]); | 
| 169 | 
  | 
                fname = argv[i]; | 
| 170 | 
  | 
                fin = fopen(fname, "r"); | 
| 171 | 
  | 
                if (fin == NULL) { | 
| 172 | 
< | 
                        sprintf(errmsg, "can't open file \"%s\"", fname); | 
| 172 | 
> | 
                        sprintf(errmsg, "cannot open file \"%s\"", fname); | 
| 173 | 
  | 
                        quiterr(errmsg); | 
| 174 | 
  | 
                } | 
| 175 | 
  | 
        } else if (i != argc) | 
| 176 | 
  | 
                goto userr; | 
| 177 | 
  | 
                                /* get header */ | 
| 178 | 
< | 
        getheader(fin, headline); | 
| 178 | 
> | 
        getheader(fin, headline, NULL); | 
| 179 | 
  | 
                                /* get picture dimensions */ | 
| 180 | 
< | 
        if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) | 
| 181 | 
< | 
                quiterr("bad picture size"); | 
| 180 | 
> | 
        if (wrongformat || !fgetsresolu(&inpres, fin)) | 
| 181 | 
> | 
                quiterr("bad picture format"); | 
| 182 | 
> | 
        xmax = scanlen(&inpres); | 
| 183 | 
> | 
        ymax = numscans(&inpres); | 
| 184 | 
  | 
                                /* set view parameters */ | 
| 185 | 
  | 
        if (gotview && setview(&ourview) != NULL) | 
| 186 | 
  | 
                gotview = 0; | 
| 193 | 
  | 
                getevent();             /* main loop */ | 
| 194 | 
  | 
userr: | 
| 195 | 
  | 
        fprintf(stderr, | 
| 196 | 
< | 
        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", | 
| 196 | 
> | 
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n", | 
| 197 | 
  | 
                        progname); | 
| 198 | 
< | 
        quit(1); | 
| 198 | 
> | 
        exit(1); | 
| 199 | 
  | 
} | 
| 200 | 
  | 
 | 
| 201 | 
  | 
 | 
| 202 | 
  | 
headline(s)             /* get relevant info from header */ | 
| 203 | 
  | 
char  *s; | 
| 204 | 
  | 
{ | 
| 205 | 
< | 
        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; | 
| 185 | 
< | 
        register char  **an; | 
| 205 | 
> | 
        char  fmt[32]; | 
| 206 | 
  | 
 | 
| 207 | 
  | 
        if (isexpos(s)) | 
| 208 | 
  | 
                exposure *= exposval(s); | 
| 209 | 
< | 
        else | 
| 210 | 
< | 
                for (an = altname; *an != NULL; an++) | 
| 211 | 
< | 
                        if (!strncmp(*an, s, strlen(*an))) { | 
| 212 | 
< | 
                                if (sscanview(&ourview, s+strlen(*an)) > 0) | 
| 213 | 
< | 
                                        gotview++; | 
| 194 | 
< | 
                                return; | 
| 195 | 
< | 
                        } | 
| 209 | 
> | 
        else if (isformat(s)) { | 
| 210 | 
> | 
                formatval(fmt, s); | 
| 211 | 
> | 
                wrongformat = strcmp(fmt, COLRFMT); | 
| 212 | 
> | 
        } else if (isview(s) && sscanview(&ourview, s) > 0) | 
| 213 | 
> | 
                gotview++; | 
| 214 | 
  | 
} | 
| 215 | 
  | 
 | 
| 216 | 
  | 
 | 
| 217 | 
  | 
init()                  /* get data and open window */ | 
| 218 | 
  | 
{ | 
| 219 | 
+ | 
        XWMHints        ourxwmhints; | 
| 220 | 
  | 
        XSetWindowAttributes    ourwinattr; | 
| 221 | 
< | 
        XSizeHints  oursizhints; | 
| 222 | 
< | 
        register int  i; | 
| 221 | 
> | 
        XSizeHints      oursizhints; | 
| 222 | 
> | 
        register int    i; | 
| 223 | 
  | 
         | 
| 224 | 
  | 
        if (fname != NULL) { | 
| 225 | 
  | 
                scanpos = (long *)malloc(ymax*sizeof(long)); | 
| 228 | 
  | 
                for (i = 0; i < ymax; i++) | 
| 229 | 
  | 
                        scanpos[i] = -1; | 
| 230 | 
  | 
        } | 
| 231 | 
< | 
        if ((thedisplay = XOpenDisplay(NULL)) == NULL) | 
| 232 | 
< | 
                quiterr("can't open display; DISPLAY variable set?"); | 
| 233 | 
< | 
        if (maxcolors == 0) {           /* get number of available colors */ | 
| 234 | 
< | 
                i = DisplayPlanes(thedisplay,ourscreen); | 
| 216 | 
< | 
                maxcolors = i > 8 ? 256 : 1<<i; | 
| 217 | 
< | 
                if (maxcolors > 4) maxcolors -= 2; | 
| 218 | 
< | 
        } | 
| 231 | 
> | 
        if ((thedisplay = XOpenDisplay(dispname)) == NULL) | 
| 232 | 
> | 
                quiterr("cannot open display"); | 
| 233 | 
> | 
                                /* get best visual for default screen */ | 
| 234 | 
> | 
        getbestvis(); | 
| 235 | 
  | 
                                /* store image */ | 
| 236 | 
  | 
        getras(); | 
| 237 | 
< | 
                                /* open window */ | 
| 238 | 
< | 
        ourwinattr.border_pixel = ourblack; | 
| 239 | 
< | 
        ourwinattr.background_pixel = ourwhite; | 
| 224 | 
< | 
        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, | 
| 225 | 
< | 
                        0, InputOutput, ourras->visual,  | 
| 226 | 
< | 
                        CWBackPixel|CWBorderPixel, &ourwinattr); | 
| 227 | 
< | 
        if (wind == 0) | 
| 228 | 
< | 
                quiterr("can't create window"); | 
| 229 | 
< | 
        width = xmax; | 
| 230 | 
< | 
        height = ymax; | 
| 231 | 
< | 
        fontid = XLoadFont(thedisplay, FONTNAME); | 
| 232 | 
< | 
        if (fontid == 0) | 
| 233 | 
< | 
                quiterr("can't get font"); | 
| 234 | 
< | 
        XSetFont(thedisplay, ourgc, fontid); | 
| 235 | 
< | 
        revgc = XCreateGC(thedisplay, wind, 0, 0); | 
| 236 | 
< | 
        XSetFunction(thedisplay, revgc, GXinvert); | 
| 237 | 
< | 
        XStoreName(thedisplay, wind, fname == NULL ? progname : fname); | 
| 238 | 
< | 
        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,  | 
| 239 | 
< | 
                        XC_diamond_cross)); | 
| 237 | 
> | 
                                /* get size and position */ | 
| 238 | 
> | 
        bzero((char *)&oursizhints, sizeof(oursizhints)); | 
| 239 | 
> | 
        oursizhints.width = xmax; oursizhints.height = ymax; | 
| 240 | 
  | 
        if (geometry != NULL) { | 
| 241 | 
– | 
                bzero((char *)&oursizhints, sizeof(oursizhints)); | 
| 241 | 
  | 
                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, | 
| 242 | 
< | 
                                &oursizhints.width, &oursizhints.height); | 
| 242 | 
> | 
                                (unsigned *)&oursizhints.width, | 
| 243 | 
> | 
                                (unsigned *)&oursizhints.height); | 
| 244 | 
  | 
                if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) | 
| 245 | 
  | 
                        oursizhints.flags |= USSize; | 
| 246 | 
< | 
                else { | 
| 247 | 
< | 
                        oursizhints.width = xmax; | 
| 248 | 
< | 
                        oursizhints.height = ymax; | 
| 246 | 
> | 
                else | 
| 247 | 
  | 
                        oursizhints.flags |= PSize; | 
| 250 | 
– | 
                } | 
| 248 | 
  | 
                if ((i&(XValue|YValue)) == (XValue|YValue)) { | 
| 249 | 
  | 
                        oursizhints.flags |= USPosition; | 
| 250 | 
  | 
                        if (i & XNegative) | 
| 254 | 
  | 
                                oursizhints.y += DisplayHeight(thedisplay, | 
| 255 | 
  | 
                                ourscreen)-1-oursizhints.height-2*BORWIDTH; | 
| 256 | 
  | 
                } | 
| 260 | 
– | 
                XSetNormalHints(thedisplay, wind, &oursizhints); | 
| 257 | 
  | 
        } | 
| 258 | 
+ | 
                                /* open window */ | 
| 259 | 
+ | 
        ourwinattr.border_pixel = ourwhite; | 
| 260 | 
+ | 
        ourwinattr.background_pixel = ourblack; | 
| 261 | 
+ | 
        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, | 
| 262 | 
+ | 
                        ourvis.visual, AllocNone); | 
| 263 | 
+ | 
        wind = XCreateWindow(thedisplay, ourroot, oursizhints.x, oursizhints.y, | 
| 264 | 
+ | 
                        oursizhints.width, oursizhints.height, BORWIDTH, | 
| 265 | 
+ | 
                        ourvis.depth, InputOutput, ourvis.visual,  | 
| 266 | 
+ | 
                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); | 
| 267 | 
+ | 
        if (wind == 0) | 
| 268 | 
+ | 
                quiterr("cannot create window"); | 
| 269 | 
+ | 
        XFreeColormap(thedisplay, ourwinattr.colormap); | 
| 270 | 
+ | 
        width = xmax; | 
| 271 | 
+ | 
        height = ymax; | 
| 272 | 
+ | 
        ourgc = XCreateGC(thedisplay, wind, 0, 0); | 
| 273 | 
+ | 
        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); | 
| 274 | 
+ | 
        revgc = XCreateGC(thedisplay, wind, 0, 0); | 
| 275 | 
+ | 
        XSetFunction(thedisplay, revgc, GXinvert); | 
| 276 | 
+ | 
        fontid = XLoadFont(thedisplay, FONTNAME); | 
| 277 | 
+ | 
        if (fontid == 0) | 
| 278 | 
+ | 
                quiterr("cannot get font"); | 
| 279 | 
+ | 
        XSetFont(thedisplay, ourgc, fontid); | 
| 280 | 
+ | 
        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,  | 
| 281 | 
+ | 
                        XC_diamond_cross)); | 
| 282 | 
+ | 
        XStoreName(thedisplay, wind, fname == NULL ? progname : fname); | 
| 283 | 
+ | 
        if (oursizhints.flags) | 
| 284 | 
+ | 
                XSetNormalHints(thedisplay, wind, &oursizhints); | 
| 285 | 
+ | 
        ourxwmhints.flags = InputHint|IconPixmapHint; | 
| 286 | 
+ | 
        ourxwmhints.input = True; | 
| 287 | 
+ | 
        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, | 
| 288 | 
+ | 
                        wind, icondata, iconwidth, iconheight); | 
| 289 | 
+ | 
        XSetWMHints(thedisplay, wind, &ourxwmhints); | 
| 290 | 
  | 
        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask | 
| 291 | 
  | 
                        |ButtonMotionMask|StructureNotifyMask | 
| 292 | 
  | 
                        |KeyPressMask|ExposureMask); | 
| 308 | 
  | 
} | 
| 309 | 
  | 
 | 
| 310 | 
  | 
 | 
| 311 | 
< | 
eputs(s) | 
| 312 | 
< | 
char    *s; | 
| 311 | 
> | 
static int | 
| 312 | 
> | 
viscmp(v1,v2)           /* compare visual to see which is better, descending */ | 
| 313 | 
> | 
register XVisualInfo    *v1, *v2; | 
| 314 | 
  | 
{ | 
| 315 | 
< | 
        fputs(s, stderr); | 
| 315 | 
> | 
        int     bad1 = 0, bad2 = 0; | 
| 316 | 
> | 
        register int  *rp; | 
| 317 | 
> | 
 | 
| 318 | 
> | 
        if (v1->class == v2->class) { | 
| 319 | 
> | 
                if (v1->class == TrueColor || v1->class == DirectColor) { | 
| 320 | 
> | 
                                        /* prefer 24-bit to 32-bit */ | 
| 321 | 
> | 
                        if (v1->depth == 24 && v2->depth == 32) | 
| 322 | 
> | 
                                return(-1); | 
| 323 | 
> | 
                        if (v1->depth == 32 && v2->depth == 24) | 
| 324 | 
> | 
                                return(1); | 
| 325 | 
> | 
                        return(0); | 
| 326 | 
> | 
                } | 
| 327 | 
> | 
                                        /* don't be too greedy */ | 
| 328 | 
> | 
                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth) | 
| 329 | 
> | 
                        return(v1->depth - v2->depth); | 
| 330 | 
> | 
                return(v2->depth - v1->depth); | 
| 331 | 
> | 
        } | 
| 332 | 
> | 
                                        /* prefer Pseudo when < 24-bit */ | 
| 333 | 
> | 
        if ((v1->class == TrueColor || v1->class == DirectColor) && | 
| 334 | 
> | 
                        v1->depth < 24) | 
| 335 | 
> | 
                bad1 = 1; | 
| 336 | 
> | 
        if ((v2->class == TrueColor || v2->class == DirectColor) && | 
| 337 | 
> | 
                        v2->depth < 24) | 
| 338 | 
> | 
                bad2 = -1; | 
| 339 | 
> | 
        if (bad1 | bad2) | 
| 340 | 
> | 
                return(bad1+bad2); | 
| 341 | 
> | 
                                        /* otherwise, use class ranking */ | 
| 342 | 
> | 
        for (rp = ourrank; *rp != -1; rp++) { | 
| 343 | 
> | 
                if (v1->class == *rp) | 
| 344 | 
> | 
                        return(-1); | 
| 345 | 
> | 
                if (v2->class == *rp) | 
| 346 | 
> | 
                        return(1); | 
| 347 | 
> | 
        } | 
| 348 | 
> | 
        return(0); | 
| 349 | 
  | 
} | 
| 350 | 
  | 
 | 
| 351 | 
  | 
 | 
| 352 | 
< | 
quit(code) | 
| 291 | 
< | 
int  code; | 
| 352 | 
> | 
getbestvis()                    /* get the best visual for this screen */ | 
| 353 | 
  | 
{ | 
| 354 | 
< | 
        exit(code); | 
| 354 | 
> | 
#ifdef DEBUG | 
| 355 | 
> | 
static char  vistype[][12] = { | 
| 356 | 
> | 
                "StaticGray", | 
| 357 | 
> | 
                "GrayScale", | 
| 358 | 
> | 
                "StaticColor", | 
| 359 | 
> | 
                "PseudoColor", | 
| 360 | 
> | 
                "TrueColor", | 
| 361 | 
> | 
                "DirectColor" | 
| 362 | 
> | 
}; | 
| 363 | 
> | 
#endif | 
| 364 | 
> | 
        static int      rankings[3][6] = { | 
| 365 | 
> | 
                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1}, | 
| 366 | 
> | 
                {PseudoColor,GrayScale,StaticGray,-1}, | 
| 367 | 
> | 
                {PseudoColor,GrayScale,StaticGray,-1} | 
| 368 | 
> | 
        }; | 
| 369 | 
> | 
        XVisualInfo     *xvi; | 
| 370 | 
> | 
        int     vismatched; | 
| 371 | 
> | 
        register int    i, j; | 
| 372 | 
> | 
 | 
| 373 | 
> | 
        if (greyscale) { | 
| 374 | 
> | 
                ourrank = rankings[2]; | 
| 375 | 
> | 
                if (maxcolors < 2) maxcolors = 256; | 
| 376 | 
> | 
        } else if (maxcolors >= 2 && maxcolors <= 256) | 
| 377 | 
> | 
                ourrank = rankings[1]; | 
| 378 | 
> | 
        else { | 
| 379 | 
> | 
                ourrank = rankings[0]; | 
| 380 | 
> | 
                maxcolors = 256; | 
| 381 | 
> | 
        } | 
| 382 | 
> | 
                                        /* find best visual */ | 
| 383 | 
> | 
        ourvis.screen = ourscreen; | 
| 384 | 
> | 
        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched); | 
| 385 | 
> | 
        if (xvi == NULL) | 
| 386 | 
> | 
                quiterr("no visuals for this screen!"); | 
| 387 | 
> | 
#ifdef DEBUG | 
| 388 | 
> | 
        fprintf(stderr, "Supported visuals:\n"); | 
| 389 | 
> | 
        for (i = 0; i < vismatched; i++) | 
| 390 | 
> | 
                fprintf(stderr, "\ttype %s, depth %d\n", | 
| 391 | 
> | 
                                vistype[xvi[i].class], xvi[i].depth); | 
| 392 | 
> | 
#endif | 
| 393 | 
> | 
        for (i = 0, j = 1; j < vismatched; j++) | 
| 394 | 
> | 
                if (viscmp(&xvi[i],&xvi[j]) > 0) | 
| 395 | 
> | 
                        i = j; | 
| 396 | 
> | 
                                        /* compare to least acceptable */ | 
| 397 | 
> | 
        for (j = 0; ourrank[j++] != -1; ) | 
| 398 | 
> | 
                ; | 
| 399 | 
> | 
        ourvis.class = ourrank[--j]; | 
| 400 | 
> | 
        ourvis.depth = 1; | 
| 401 | 
> | 
        if (viscmp(&xvi[i],&ourvis) > 0) | 
| 402 | 
> | 
                quiterr("inadequate visuals on this screen"); | 
| 403 | 
> | 
                                        /* OK, we'll use it */ | 
| 404 | 
> | 
        copystruct(&ourvis, &xvi[i]); | 
| 405 | 
> | 
#ifdef DEBUG | 
| 406 | 
> | 
        fprintf(stderr, "Selected visual type %s, depth %d\n", | 
| 407 | 
> | 
                        vistype[ourvis.class], ourvis.depth); | 
| 408 | 
> | 
#endif | 
| 409 | 
> | 
                                        /* make appropriate adjustments */ | 
| 410 | 
> | 
        if (ourvis.class == GrayScale || ourvis.class == StaticGray) | 
| 411 | 
> | 
                greyscale = 1; | 
| 412 | 
> | 
        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) | 
| 413 | 
> | 
                maxcolors = ourvis.colormap_size; | 
| 414 | 
> | 
        if (ourvis.class == StaticGray) { | 
| 415 | 
> | 
                ourblack = 0; | 
| 416 | 
> | 
                ourwhite = 255; | 
| 417 | 
> | 
        } else if (ourvis.class == PseudoColor) { | 
| 418 | 
> | 
                ourblack = BlackPixel(thedisplay,ourscreen); | 
| 419 | 
> | 
                ourwhite = WhitePixel(thedisplay,ourscreen); | 
| 420 | 
> | 
                if ((ourblack|ourwhite) & ~255L) { | 
| 421 | 
> | 
                        ourblack = 0; | 
| 422 | 
> | 
                        ourwhite = 1; | 
| 423 | 
> | 
                } | 
| 424 | 
> | 
                if (maxcolors > 4) | 
| 425 | 
> | 
                        maxcolors -= 2; | 
| 426 | 
> | 
        } else { | 
| 427 | 
> | 
                ourblack = 0; | 
| 428 | 
> | 
                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask; | 
| 429 | 
> | 
        } | 
| 430 | 
> | 
        XFree((char *)xvi); | 
| 431 | 
  | 
} | 
| 432 | 
  | 
 | 
| 433 | 
  | 
 | 
| 434 | 
  | 
getras()                                /* get raster file */ | 
| 435 | 
  | 
{ | 
| 299 | 
– | 
        colormap        ourmap; | 
| 436 | 
  | 
        XVisualInfo     vinfo; | 
| 437 | 
  | 
 | 
| 438 | 
  | 
        if (maxcolors <= 2) {           /* monochrome */ | 
| 439 | 
  | 
                ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); | 
| 440 | 
  | 
                if (ourdata == NULL) | 
| 441 | 
  | 
                        goto fail; | 
| 442 | 
< | 
                ourras = make_raster(thedisplay, ourscreen, 1, ourdata, | 
| 442 | 
> | 
                ourras = make_raster(thedisplay, &ourvis, 1, ourdata, | 
| 443 | 
  | 
                                xmax, ymax, 8); | 
| 444 | 
  | 
                if (ourras == NULL) | 
| 445 | 
  | 
                        goto fail; | 
| 446 | 
  | 
                getmono(); | 
| 447 | 
< | 
        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) | 
| 448 | 
< | 
                                                /* kludge for DirectColor */ | 
| 313 | 
< | 
        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { | 
| 314 | 
< | 
                ourdata = (unsigned char *)malloc(xmax*ymax*3); | 
| 447 | 
> | 
        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { | 
| 448 | 
> | 
                ourdata = (unsigned char *)malloc(4*xmax*ymax); | 
| 449 | 
  | 
                if (ourdata == NULL) | 
| 450 | 
  | 
                        goto fail; | 
| 451 | 
< | 
                ourras = make_raster(thedisplay, ourscreen, 24, ourdata, | 
| 452 | 
< | 
                                xmax, ymax, 8); | 
| 451 | 
> | 
                ourras = make_raster(thedisplay, &ourvis, 32, | 
| 452 | 
> | 
                                ourdata, xmax, ymax, 32); | 
| 453 | 
  | 
                if (ourras == NULL) | 
| 454 | 
  | 
                        goto fail; | 
| 455 | 
  | 
                getfull(); | 
| 457 | 
  | 
                ourdata = (unsigned char *)malloc(xmax*ymax); | 
| 458 | 
  | 
                if (ourdata == NULL) | 
| 459 | 
  | 
                        goto fail; | 
| 460 | 
< | 
                ourras = make_raster(thedisplay, ourscreen, 8, ourdata, | 
| 460 | 
> | 
                ourras = make_raster(thedisplay, &ourvis, 8, ourdata, | 
| 461 | 
  | 
                                xmax, ymax, 8); | 
| 462 | 
  | 
                if (ourras == NULL) | 
| 463 | 
  | 
                        goto fail; | 
| 464 | 
< | 
                if (greyscale) | 
| 465 | 
< | 
                        biq(dither,maxcolors,1,ourmap); | 
| 464 | 
> | 
                if (greyscale | ourvis.class == StaticGray) | 
| 465 | 
> | 
                        getgrey(); | 
| 466 | 
  | 
                else | 
| 467 | 
< | 
                        ciq(dither,maxcolors,1,ourmap); | 
| 468 | 
< | 
                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) | 
| 467 | 
> | 
                        getmapped(); | 
| 468 | 
> | 
                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab)) | 
| 469 | 
  | 
                        goto fail; | 
| 470 | 
  | 
        } | 
| 471 | 
  | 
        return; | 
| 496 | 
  | 
        case MapNotify: | 
| 497 | 
  | 
                map_rcolors(ourras, wind); | 
| 498 | 
  | 
                if (fast) | 
| 499 | 
< | 
                        make_rpixmap(ourras); | 
| 499 | 
> | 
                        make_rpixmap(ourras, wind); | 
| 500 | 
  | 
                break; | 
| 501 | 
  | 
        case UnmapNotify: | 
| 502 | 
< | 
                unmap_rcolors(ourras); | 
| 502 | 
> | 
                if (!fast) | 
| 503 | 
> | 
                        unmap_rcolors(ourras); | 
| 504 | 
  | 
                break; | 
| 505 | 
  | 
        case Expose: | 
| 506 | 
  | 
                redraw(e.e.x, e.e.y, e.e.width, e.e.height); | 
| 508 | 
  | 
        case ButtonPress: | 
| 509 | 
  | 
                if (e.b.state & (ShiftMask|ControlMask)) | 
| 510 | 
  | 
                        moveimage(&e.b); | 
| 511 | 
+ | 
                else if (e.b.button == Button2) | 
| 512 | 
+ | 
                        traceray(e.b.x, e.b.y); | 
| 513 | 
  | 
                else | 
| 514 | 
  | 
                        getbox(&e.b); | 
| 515 | 
  | 
                break; | 
| 517 | 
  | 
} | 
| 518 | 
  | 
 | 
| 519 | 
  | 
 | 
| 520 | 
< | 
docom(ekey)                                     /* execute command */ | 
| 520 | 
> | 
traceray(xpos, ypos)                    /* print ray corresponding to pixel */ | 
| 521 | 
> | 
int  xpos, ypos; | 
| 522 | 
> | 
{ | 
| 523 | 
> | 
        FLOAT  hv[2]; | 
| 524 | 
> | 
        FVECT  rorg, rdir; | 
| 525 | 
> | 
 | 
| 526 | 
> | 
        if (!gotview) {         /* no view, no can do */ | 
| 527 | 
> | 
                XBell(thedisplay, 0); | 
| 528 | 
> | 
                return(-1); | 
| 529 | 
> | 
        } | 
| 530 | 
> | 
        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); | 
| 531 | 
> | 
        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) | 
| 532 | 
> | 
                return(-1); | 
| 533 | 
> | 
        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 534 | 
> | 
        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); | 
| 535 | 
> | 
        fflush(stdout); | 
| 536 | 
> | 
        return(0); | 
| 537 | 
> | 
} | 
| 538 | 
> | 
 | 
| 539 | 
> | 
 | 
| 540 | 
> | 
docom(ekey)                             /* execute command */ | 
| 541 | 
  | 
XKeyPressedEvent  *ekey; | 
| 542 | 
  | 
{ | 
| 543 | 
  | 
        char  buf[80]; | 
| 545 | 
  | 
        XColor  cvx; | 
| 546 | 
  | 
        int  com, n; | 
| 547 | 
  | 
        double  comp; | 
| 548 | 
< | 
        FVECT  rorg, rdir; | 
| 548 | 
> | 
        FLOAT  hv[2]; | 
| 549 | 
  | 
 | 
| 550 | 
  | 
        n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);  | 
| 551 | 
  | 
        if (n == 0) | 
| 553 | 
  | 
        com = buf[0]; | 
| 554 | 
  | 
        switch (com) {                  /* interpret command */ | 
| 555 | 
  | 
        case 'q': | 
| 556 | 
< | 
        case CTRL(D):                           /* quit */ | 
| 557 | 
< | 
                quit(0); | 
| 556 | 
> | 
        case 'Q': | 
| 557 | 
> | 
        case CTRL('D'):                         /* quit */ | 
| 558 | 
> | 
                quiterr(NULL); | 
| 559 | 
  | 
        case '\n': | 
| 560 | 
  | 
        case '\r': | 
| 561 | 
  | 
        case 'l': | 
| 568 | 
  | 
                        sprintf(buf, "%.3f", intens(cval)/exposure); | 
| 569 | 
  | 
                        break; | 
| 570 | 
  | 
                case 'l':                               /* luminance */ | 
| 571 | 
< | 
                        sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); | 
| 571 | 
> | 
                        sprintf(buf, "%.0fL", luminance(cval)/exposure); | 
| 572 | 
  | 
                        break; | 
| 573 | 
  | 
                case 'c':                               /* color */ | 
| 574 | 
  | 
                        comp = pow(2.0, (double)scale); | 
| 594 | 
  | 
                XStoreColor(thedisplay, ourras->cmap, &cvx); | 
| 595 | 
  | 
                return(0); | 
| 596 | 
  | 
        case 'p':                               /* position */ | 
| 597 | 
< | 
                sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); | 
| 597 | 
> | 
                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); | 
| 598 | 
> | 
                sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr), | 
| 599 | 
> | 
                                (int)(hv[1]*inpres.yr)); | 
| 600 | 
  | 
                XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y, | 
| 601 | 
  | 
                                        buf, strlen(buf)); | 
| 602 | 
  | 
                return(0); | 
| 603 | 
  | 
        case 't':                               /* trace */ | 
| 604 | 
< | 
                if (!gotview) { | 
| 445 | 
< | 
                        XBell(thedisplay, 0); | 
| 446 | 
< | 
                        return(-1); | 
| 447 | 
< | 
                } | 
| 448 | 
< | 
                if (viewray(rorg, rdir, &ourview, | 
| 449 | 
< | 
                                (ekey->x-xoff+.5)/xmax, | 
| 450 | 
< | 
                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0) | 
| 451 | 
< | 
                        return(-1); | 
| 452 | 
< | 
                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 453 | 
< | 
                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); | 
| 454 | 
< | 
                fflush(stdout); | 
| 455 | 
< | 
                return(0); | 
| 604 | 
> | 
                return(traceray(ekey->x, ekey->y)); | 
| 605 | 
  | 
        case '=':                               /* adjust exposure */ | 
| 606 | 
  | 
                if (avgbox(cval) == -1) | 
| 607 | 
  | 
                        return(-1); | 
| 618 | 
  | 
                free_raster(ourras); | 
| 619 | 
  | 
                getras(); | 
| 620 | 
  | 
        /* fall through */ | 
| 621 | 
< | 
        case CTRL(R):                           /* redraw */ | 
| 622 | 
< | 
        case CTRL(L): | 
| 621 | 
> | 
        case CTRL('R'):                         /* redraw */ | 
| 622 | 
> | 
        case CTRL('L'): | 
| 623 | 
  | 
                unmap_rcolors(ourras); | 
| 624 | 
  | 
                XClearWindow(thedisplay, wind); | 
| 625 | 
  | 
                map_rcolors(ourras, wind); | 
| 626 | 
  | 
                if (fast) | 
| 627 | 
< | 
                        make_rpixmap(ourras); | 
| 627 | 
> | 
                        make_rpixmap(ourras, wind); | 
| 628 | 
  | 
                redraw(0, 0, width, height); | 
| 629 | 
  | 
                return(0); | 
| 630 | 
+ | 
        case '0':                               /* recenter origin */ | 
| 631 | 
+ | 
                if (xoff == 0 & yoff == 0) | 
| 632 | 
+ | 
                        return(0); | 
| 633 | 
+ | 
                xoff = yoff = 0; | 
| 634 | 
+ | 
                XClearWindow(thedisplay, wind); | 
| 635 | 
+ | 
                redraw(0, 0, width, height); | 
| 636 | 
+ | 
                return(0); | 
| 637 | 
  | 
        case ' ':                               /* clear */ | 
| 638 | 
  | 
                redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); | 
| 639 | 
  | 
                return(0); | 
| 718 | 
  | 
avgbox(clr)                             /* average color over current box */ | 
| 719 | 
  | 
COLOR  clr; | 
| 720 | 
  | 
{ | 
| 721 | 
+ | 
        static COLOR  lc; | 
| 722 | 
+ | 
        static int  ll, lr, lt, lb; | 
| 723 | 
  | 
        int  left, right, top, bottom; | 
| 724 | 
  | 
        int  y; | 
| 725 | 
  | 
        double  d; | 
| 743 | 
  | 
                bottom = ymax; | 
| 744 | 
  | 
        if (top >= bottom) | 
| 745 | 
  | 
                return(-1); | 
| 746 | 
+ | 
        if (left == ll && right == lr && top == lt && bottom == lb) { | 
| 747 | 
+ | 
                copycolor(clr, lc); | 
| 748 | 
+ | 
                return(0); | 
| 749 | 
+ | 
        } | 
| 750 | 
  | 
        for (y = top; y < bottom; y++) { | 
| 751 | 
  | 
                if (getscan(y) == -1) | 
| 752 | 
  | 
                        return(-1); | 
| 757 | 
  | 
        } | 
| 758 | 
  | 
        d = 1.0/((right-left)*(bottom-top)); | 
| 759 | 
  | 
        scalecolor(clr, d); | 
| 760 | 
+ | 
        ll = left; lr = right; lt = top; lb = bottom; | 
| 761 | 
+ | 
        copycolor(lc, clr); | 
| 762 | 
  | 
        return(0); | 
| 763 | 
  | 
} | 
| 764 | 
  | 
 | 
| 767 | 
  | 
{ | 
| 768 | 
  | 
        register unsigned char  *dp; | 
| 769 | 
  | 
        register int    x, err; | 
| 770 | 
< | 
        int     y; | 
| 770 | 
> | 
        int     y, errp; | 
| 771 | 
  | 
        short   *cerr; | 
| 772 | 
  | 
 | 
| 773 | 
  | 
        if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) | 
| 774 | 
  | 
                quiterr("out of memory in getmono"); | 
| 775 | 
  | 
        dp = ourdata - 1; | 
| 776 | 
  | 
        for (y = 0; y < ymax; y++) { | 
| 777 | 
< | 
                if (getscan(y) < 0) | 
| 778 | 
< | 
                        quiterr("seek error in getmono"); | 
| 777 | 
> | 
                getscan(y); | 
| 778 | 
> | 
                add2icon(y, scanline); | 
| 779 | 
  | 
                normcolrs(scanline, xmax, scale); | 
| 780 | 
  | 
                err = 0; | 
| 781 | 
  | 
                for (x = 0; x < xmax; x++) { | 
| 782 | 
  | 
                        if (!(x&7)) | 
| 783 | 
  | 
                                *++dp = 0; | 
| 784 | 
+ | 
                        errp = err; | 
| 785 | 
  | 
                        err += normbright(scanline[x]) + cerr[x]; | 
| 786 | 
  | 
                        if (err > 127) | 
| 787 | 
  | 
                                err -= 255; | 
| 788 | 
  | 
                        else | 
| 789 | 
  | 
                                *dp |= 1<<(7-(x&07)); | 
| 790 | 
< | 
                        cerr[x] = err >>= 1; | 
| 790 | 
> | 
                        err /= 3; | 
| 791 | 
> | 
                        cerr[x] = err + errp; | 
| 792 | 
  | 
                } | 
| 793 | 
  | 
        } | 
| 794 | 
  | 
        free((char *)cerr); | 
| 795 | 
  | 
} | 
| 796 | 
  | 
 | 
| 797 | 
  | 
 | 
| 798 | 
+ | 
add2icon(y, scan)               /* add a scanline to our icon data */ | 
| 799 | 
+ | 
int  y; | 
| 800 | 
+ | 
COLR  *scan; | 
| 801 | 
+ | 
{ | 
| 802 | 
+ | 
        static short  cerr[ICONSIZ]; | 
| 803 | 
+ | 
        static int  ynext; | 
| 804 | 
+ | 
        static char  *dp; | 
| 805 | 
+ | 
        COLR  clr; | 
| 806 | 
+ | 
        register int  err; | 
| 807 | 
+ | 
        register int    x, ti; | 
| 808 | 
+ | 
        int  errp; | 
| 809 | 
+ | 
 | 
| 810 | 
+ | 
        if (iconheight == 0) {          /* initialize */ | 
| 811 | 
+ | 
                if (xmax <= ICONSIZ && ymax <= ICONSIZ) { | 
| 812 | 
+ | 
                        iconwidth = xmax; | 
| 813 | 
+ | 
                        iconheight = ymax; | 
| 814 | 
+ | 
                } else if (xmax > ymax) { | 
| 815 | 
+ | 
                        iconwidth = ICONSIZ; | 
| 816 | 
+ | 
                        iconheight = ICONSIZ*ymax/xmax; | 
| 817 | 
+ | 
                        if (iconheight < 1) | 
| 818 | 
+ | 
                                iconheight = 1; | 
| 819 | 
+ | 
                } else { | 
| 820 | 
+ | 
                        iconwidth = ICONSIZ*xmax/ymax; | 
| 821 | 
+ | 
                        if (iconwidth < 1) | 
| 822 | 
+ | 
                                iconwidth = 1; | 
| 823 | 
+ | 
                        iconheight = ICONSIZ; | 
| 824 | 
+ | 
                } | 
| 825 | 
+ | 
                ynext = 0; | 
| 826 | 
+ | 
                dp = icondata - 1; | 
| 827 | 
+ | 
        } | 
| 828 | 
+ | 
        if (y < ynext*ymax/iconheight)  /* skip this one */ | 
| 829 | 
+ | 
                return; | 
| 830 | 
+ | 
        err = 0; | 
| 831 | 
+ | 
        for (x = 0; x < iconwidth; x++) { | 
| 832 | 
+ | 
                if (!(x&7)) | 
| 833 | 
+ | 
                        *++dp = 0; | 
| 834 | 
+ | 
                errp = err; | 
| 835 | 
+ | 
                ti = x*xmax/iconwidth; | 
| 836 | 
+ | 
                copycolr(clr, scan[ti]); | 
| 837 | 
+ | 
                normcolrs(clr, 1, scale); | 
| 838 | 
+ | 
                err += normbright(clr) + cerr[x]; | 
| 839 | 
+ | 
                if (err > 127) | 
| 840 | 
+ | 
                        err -= 255; | 
| 841 | 
+ | 
                else | 
| 842 | 
+ | 
                        *dp |= 1<<(x&07); | 
| 843 | 
+ | 
                err /= 3; | 
| 844 | 
+ | 
                cerr[x] = err + errp; | 
| 845 | 
+ | 
        } | 
| 846 | 
+ | 
        ynext++; | 
| 847 | 
+ | 
} | 
| 848 | 
+ | 
 | 
| 849 | 
+ | 
 | 
| 850 | 
  | 
getfull()                       /* get full (24-bit) data */ | 
| 851 | 
  | 
{ | 
| 852 | 
  | 
        int     y; | 
| 853 | 
+ | 
        register unsigned long  *dp; | 
| 854 | 
+ | 
        register int    x; | 
| 855 | 
+ | 
                                        /* set gamma correction */ | 
| 856 | 
+ | 
        setcolrgam(gamcor); | 
| 857 | 
+ | 
                                        /* read and convert file */ | 
| 858 | 
+ | 
        dp = (unsigned long *)ourdata; | 
| 859 | 
+ | 
        for (y = 0; y < ymax; y++) { | 
| 860 | 
+ | 
                getscan(y); | 
| 861 | 
+ | 
                add2icon(y, scanline); | 
| 862 | 
+ | 
                if (scale) | 
| 863 | 
+ | 
                        shiftcolrs(scanline, xmax, scale); | 
| 864 | 
+ | 
                colrs_gambs(scanline, xmax); | 
| 865 | 
+ | 
                if (ourras->image->blue_mask & 1) | 
| 866 | 
+ | 
                        for (x = 0; x < xmax; x++) | 
| 867 | 
+ | 
                                *dp++ = scanline[x][RED] << 16 | | 
| 868 | 
+ | 
                                        scanline[x][GRN] << 8 | | 
| 869 | 
+ | 
                                        scanline[x][BLU] ; | 
| 870 | 
+ | 
                else | 
| 871 | 
+ | 
                        for (x = 0; x < xmax; x++) | 
| 872 | 
+ | 
                                *dp++ = scanline[x][RED] | | 
| 873 | 
+ | 
                                        scanline[x][GRN] << 8 | | 
| 874 | 
+ | 
                                        scanline[x][BLU] << 16 ; | 
| 875 | 
+ | 
        } | 
| 876 | 
+ | 
} | 
| 877 | 
+ | 
 | 
| 878 | 
+ | 
 | 
| 879 | 
+ | 
getgrey()                       /* get greyscale data */ | 
| 880 | 
+ | 
{ | 
| 881 | 
+ | 
        int     y; | 
| 882 | 
  | 
        register unsigned char  *dp; | 
| 883 | 
  | 
        register int    x; | 
| 884 | 
  | 
                                        /* set gamma correction */ | 
| 886 | 
  | 
                                        /* read and convert file */ | 
| 887 | 
  | 
        dp = ourdata; | 
| 888 | 
  | 
        for (y = 0; y < ymax; y++) { | 
| 889 | 
+ | 
                getscan(y); | 
| 890 | 
+ | 
                add2icon(y, scanline); | 
| 891 | 
+ | 
                if (scale) | 
| 892 | 
+ | 
                        shiftcolrs(scanline, xmax, scale); | 
| 893 | 
+ | 
                for (x = 0; x < xmax; x++) | 
| 894 | 
+ | 
                        scanline[x][GRN] = normbright(scanline[x]); | 
| 895 | 
+ | 
                colrs_gambs(scanline, xmax); | 
| 896 | 
+ | 
                if (maxcolors < 256) | 
| 897 | 
+ | 
                        for (x = 0; x < xmax; x++) | 
| 898 | 
+ | 
                                *dp++ = ((long)scanline[x][GRN] * | 
| 899 | 
+ | 
                                        maxcolors + maxcolors/2) >> 8; | 
| 900 | 
+ | 
                else | 
| 901 | 
+ | 
                        for (x = 0; x < xmax; x++) | 
| 902 | 
+ | 
                                *dp++ = scanline[x][GRN]; | 
| 903 | 
+ | 
        } | 
| 904 | 
+ | 
        for (x = 0; x < maxcolors; x++) | 
| 905 | 
+ | 
                clrtab[x][RED] = clrtab[x][GRN] = | 
| 906 | 
+ | 
                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; | 
| 907 | 
+ | 
} | 
| 908 | 
+ | 
 | 
| 909 | 
+ | 
 | 
| 910 | 
+ | 
getmapped()                     /* get color-mapped data */ | 
| 911 | 
+ | 
{ | 
| 912 | 
+ | 
        int     y; | 
| 913 | 
+ | 
                                        /* set gamma correction */ | 
| 914 | 
+ | 
        setcolrgam(gamcor); | 
| 915 | 
+ | 
                                        /* make histogram */ | 
| 916 | 
+ | 
        new_histo(); | 
| 917 | 
+ | 
        for (y = 0; y < ymax; y++) { | 
| 918 | 
  | 
                if (getscan(y) < 0) | 
| 919 | 
< | 
                        quiterr("seek error in getfull"); | 
| 919 | 
> | 
                        quiterr("seek error in getmapped"); | 
| 920 | 
> | 
                add2icon(y, scanline); | 
| 921 | 
  | 
                if (scale) | 
| 922 | 
  | 
                        shiftcolrs(scanline, xmax, scale); | 
| 923 | 
  | 
                colrs_gambs(scanline, xmax); | 
| 924 | 
< | 
                for (x = 0; x < xmax; x++) { | 
| 648 | 
< | 
                        *dp++ = scanline[x][RED]; | 
| 649 | 
< | 
                        *dp++ = scanline[x][GRN]; | 
| 650 | 
< | 
                        *dp++ = scanline[x][BLU]; | 
| 651 | 
< | 
                } | 
| 924 | 
> | 
                cnt_colrs(scanline, xmax); | 
| 925 | 
  | 
        } | 
| 926 | 
+ | 
                                        /* map pixels */ | 
| 927 | 
+ | 
        if (!new_clrtab(maxcolors)) | 
| 928 | 
+ | 
                quiterr("cannot create color map"); | 
| 929 | 
+ | 
        for (y = 0; y < ymax; y++) { | 
| 930 | 
+ | 
                if (getscan(y) < 0) | 
| 931 | 
+ | 
                        quiterr("seek error in getmapped"); | 
| 932 | 
+ | 
                if (scale) | 
| 933 | 
+ | 
                        shiftcolrs(scanline, xmax, scale); | 
| 934 | 
+ | 
                colrs_gambs(scanline, xmax); | 
| 935 | 
+ | 
                if (dither) | 
| 936 | 
+ | 
                        dith_colrs(ourdata+y*xmax, scanline, xmax); | 
| 937 | 
+ | 
                else | 
| 938 | 
+ | 
                        map_colrs(ourdata+y*xmax, scanline, xmax); | 
| 939 | 
+ | 
        } | 
| 940 | 
  | 
} | 
| 941 | 
  | 
 | 
| 942 | 
  | 
 | 
| 977 | 
  | 
                if (fseek(fin, scanpos[y], 0) == -1) | 
| 978 | 
  | 
                        quiterr("fseek error"); | 
| 979 | 
  | 
                cury = y; | 
| 980 | 
< | 
        } else if (scanpos != NULL) | 
| 980 | 
> | 
        } else if (scanpos != NULL && scanpos[y] == -1) | 
| 981 | 
  | 
                scanpos[y] = ftell(fin); | 
| 982 | 
  | 
 | 
| 983 | 
  | 
        if (freadcolrs(scanline, xmax, fin) < 0) | 
| 985 | 
  | 
 | 
| 986 | 
  | 
        cury++; | 
| 987 | 
  | 
        return(0); | 
| 701 | 
– | 
} | 
| 702 | 
– | 
 | 
| 703 | 
– | 
 | 
| 704 | 
– | 
picreadline3(y, l3)                     /* read in 3-byte scanline */ | 
| 705 | 
– | 
int  y; | 
| 706 | 
– | 
register rgbpixel  *l3; | 
| 707 | 
– | 
{ | 
| 708 | 
– | 
        register int    i; | 
| 709 | 
– | 
                                                        /* read scanline */ | 
| 710 | 
– | 
        if (getscan(y) < 0) | 
| 711 | 
– | 
                quiterr("cannot seek for picreadline"); | 
| 712 | 
– | 
                                                        /* convert scanline */ | 
| 713 | 
– | 
        normcolrs(scanline, xmax, scale); | 
| 714 | 
– | 
        for (i = 0; i < xmax; i++) { | 
| 715 | 
– | 
                l3[i].r = scanline[i][RED]; | 
| 716 | 
– | 
                l3[i].g = scanline[i][GRN]; | 
| 717 | 
– | 
                l3[i].b = scanline[i][BLU]; | 
| 718 | 
– | 
        } | 
| 719 | 
– | 
} | 
| 720 | 
– | 
 | 
| 721 | 
– | 
 | 
| 722 | 
– | 
picwriteline(y, l)              /* add 8-bit scanline to image */ | 
| 723 | 
– | 
int  y; | 
| 724 | 
– | 
pixel  *l; | 
| 725 | 
– | 
{ | 
| 726 | 
– | 
        bcopy((char *)l, (char *)ourdata+y*xmax, xmax); | 
| 727 | 
– | 
} | 
| 728 | 
– | 
 | 
| 729 | 
– | 
 | 
| 730 | 
– | 
picreadcm(map)                  /* do gamma correction */ | 
| 731 | 
– | 
colormap  map; | 
| 732 | 
– | 
{ | 
| 733 | 
– | 
        extern double  pow(); | 
| 734 | 
– | 
        register int  i, val; | 
| 735 | 
– | 
 | 
| 736 | 
– | 
        for (i = 0; i < 256; i++) { | 
| 737 | 
– | 
                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; | 
| 738 | 
– | 
                map[0][i] = map[1][i] = map[2][i] = val; | 
| 739 | 
– | 
        } | 
| 740 | 
– | 
} | 
| 741 | 
– | 
 | 
| 742 | 
– | 
 | 
| 743 | 
– | 
picwritecm(map)                 /* handled elsewhere */ | 
| 744 | 
– | 
colormap  map; | 
| 745 | 
– | 
{ | 
| 746 | 
– | 
#ifdef DEBUG | 
| 747 | 
– | 
        register int i; | 
| 748 | 
– | 
 | 
| 749 | 
– | 
        for (i = 0; i < 256; i++) | 
| 750 | 
– | 
                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); | 
| 751 | 
– | 
#endif | 
| 988 | 
  | 
} |