ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.73
Committed: Mon Nov 10 19:08:19 2008 UTC (15 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R0
Changes since 2.72: +2 -2 lines
Log Message:
Changed ".pic" extension to ".hdr" throughout

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.73 static const char RCSid[] = "$Id: x11image.c,v 2.72 2006/05/29 17:31:25 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * x11image.c - driver for X-windows
6     *
7     * 3/1/90
8     */
9    
10     /*
11     * Modified for X11
12     *
13     * January 1990
14     *
15     * Anat Grynberg and Greg Ward
16     */
17    
18    
19     #include "standard.h"
20    
21 schorsch 2.61 #include <string.h>
22 greg 2.29 #include <signal.h>
23 schorsch 2.66 #include <unistd.h>
24     #include <sys/types.h>
25 greg 2.67 #include <sys/wait.h>
26 greg 1.1 #include <X11/Xlib.h>
27     #include <X11/cursorfont.h>
28     #include <X11/Xutil.h>
29 greg 2.24 #include <X11/Xatom.h>
30 greg 1.1
31 gwlarson 2.52 #include "color.h"
32 greg 2.44 #include "tonemap.h"
33 schorsch 2.66 #include "clrtab.h"
34 greg 1.1 #include "view.h"
35     #include "x11raster.h"
36     #include "random.h"
37 greg 2.28
38 greg 1.1 #define FONTNAME "8x13" /* text font we'll use */
39    
40 greg 2.3 #define CTRL(c) ((c)-'@')
41 greg 1.1
42     #define BORWIDTH 5 /* border width */
43    
44 greg 1.18 #define ICONSIZ (8*10) /* maximum icon dimension (even 8) */
45 greg 1.17
46 gregl 2.50 #define FIXWEIGHT 20 /* weight to add for fixation points */
47    
48 greg 1.1 #define ourscreen DefaultScreen(thedisplay)
49     #define ourroot RootWindow(thedisplay,ourscreen)
50    
51 greg 1.5 #define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
52    
53 greg 1.2 #define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
54    
55 greg 1.1 double gamcor = 2.2; /* gamma correction */
56 greg 2.35 char *gamstr = NULL; /* gamma value override */
57 greg 1.1
58     int dither = 1; /* dither colors? */
59     int fast = 0; /* keep picture in Pixmap? */
60    
61 greg 2.6 char *dispname = NULL; /* our display name */
62    
63 greg 1.1 Window wind = 0; /* our output window */
64 greg 2.9 unsigned long ourblack=0, ourwhite=1; /* black and white for this visual */
65 greg 1.2 int maxcolors = 0; /* maximum colors */
66 greg 1.1 int greyscale = 0; /* in grey */
67    
68     int scale = 0; /* scalefactor; power of two */
69    
70     int xoff = 0; /* x image offset */
71     int yoff = 0; /* y image offset */
72    
73 greg 2.29 int parent = 0; /* number of children, -1 if child */
74 greg 2.41 int sequential = 0; /* display images in sequence */
75 greg 2.29
76 greg 2.42 char *tout = "od"; /* output of 't' command */
77 greg 2.43 int tinterv = 0; /* interval between mouse reports */
78 greg 2.42
79 gregl 2.50 int tmflags = TM_F_LINEAR; /* tone mapping flags */
80 greg 2.44
81 greg 1.1 VIEW ourview = STDVIEW; /* image view parameters */
82     int gotview = 0; /* got parameters from file */
83    
84     COLR *scanline; /* scan line buffer */
85 greg 2.44 TMbright *lscan; /* encoded luminance scanline */
86     BYTE *cscan; /* encoded chroma scanline */
87     BYTE *pscan; /* compute pixel scanline */
88 greg 1.1
89 greg 1.26 RESOLU inpres; /* input resolution and ordering */
90     int xmax, ymax; /* picture dimensions */
91 greg 1.1 int width, height; /* window size */
92     char *fname = NULL; /* input file name */
93 gwlarson 2.54 FILE *fin = NULL; /* input file */
94 greg 1.1 long *scanpos = NULL; /* scan line positions in file */
95     int cury = 0; /* current scan location */
96    
97     double exposure = 1.0; /* exposure compensation used */
98    
99 greg 1.14 int wrongformat = 0; /* input in another format? */
100    
101 greg 2.69 TMstruct *tmGlobal; /* base tone-mapping */
102     TMstruct *tmCurrent; /* curren tone-mapping */
103    
104 greg 2.6 GC ourgc; /* standard graphics context */
105 greg 1.5 GC revgc; /* graphics context with GXinvert */
106    
107 greg 2.6 int *ourrank; /* our visual class ranking */
108     XVisualInfo ourvis; /* our visual */
109     XRASTER *ourras; /* our stored image */
110 greg 1.1 unsigned char *ourdata; /* our image data */
111    
112     struct {
113     int xmin, ymin, xsiz, ysiz;
114 greg 2.56 } bbox = {0, 0, 0, 0}; /* current bbox */
115 greg 1.1
116     char *geometry = NULL; /* geometry specification */
117    
118 greg 1.18 char icondata[ICONSIZ*ICONSIZ/8]; /* icon bitmap data */
119 greg 1.17 int iconwidth = 0, iconheight = 0;
120    
121 greg 1.1 char *progname;
122    
123     char errmsg[128];
124    
125 greg 2.37 BYTE clrtab[256][3]; /* global color map */
126 greg 2.13
127 greg 2.35
128 greg 1.1 Display *thedisplay;
129 greg 2.24 Atom closedownAtom, wmProtocolsAtom;
130 greg 1.1
131 greg 2.32 int sigrecv;
132 greg 2.14
133 schorsch 2.58 void onsig(int i) { sigrecv++; }
134 greg 2.29
135 schorsch 2.66 typedef void doboxf_t(COLR *scn, int n, void *);
136    
137 schorsch 2.65 static gethfunc headline;
138 schorsch 2.66 static void init(int argc, char **argv);
139     static void quiterr(char *err);
140     static int viscmp(XVisualInfo *v1, XVisualInfo *v2);
141     static void getbestvis(void);
142     static void getras(void);
143     static void getevent(void);
144     static int traceray(int xpos, int ypos);
145     static int docom(XKeyPressedEvent *ekey);
146     static void moveimage(XButtonPressedEvent *ebut);
147     static void getbox(XButtonPressedEvent *ebut);
148     static void trackrays(XButtonPressedEvent *ebut);
149     static void revbox(int x0, int y0, int x1, int y1);
150     static doboxf_t colavg;
151     static doboxf_t addfix;
152     static int avgbox(COLOR cavg);
153     static int dobox(doboxf_t *f, void *p);
154     static void make_tonemap(void);
155     static void tmap_colrs(COLR *scn, int len);
156     static void getmono(void);
157     static void add2icon(int y, COLR *scan);
158     static void getfull(void);
159     static void getgrey(void);
160     static void getmapped(void);
161     static void scale_rcolors(XRASTER *xr, double sf);
162     static int getscan(int y);
163 schorsch 2.65
164 greg 2.32
165 schorsch 2.66 int
166     main(int argc, char *argv[])
167 greg 1.1 {
168     int i;
169 greg 2.29 int pid;
170 greg 1.1
171     progname = argv[0];
172 gwlarson 2.54 fin = stdin;
173 greg 1.1
174     for (i = 1; i < argc; i++)
175     if (argv[i][0] == '-')
176     switch (argv[i][1]) {
177 greg 2.42 case 'c': /* number of colors */
178 greg 1.1 maxcolors = atoi(argv[++i]);
179     break;
180 greg 2.42 case 'b': /* greyscale only */
181 greg 1.1 greyscale = !greyscale;
182     break;
183 greg 2.42 case 'm': /* monochrome */
184 greg 2.40 greyscale = 1;
185 greg 1.1 maxcolors = 2;
186     break;
187 greg 2.42 case 'd': /* display or dither */
188 greg 2.7 if (argv[i][2] == 'i')
189 greg 2.6 dispname = argv[++i];
190 greg 2.7 else
191     dither = !dither;
192 greg 1.1 break;
193 greg 2.42 case 'f': /* save pixmap */
194 greg 1.1 fast = !fast;
195     break;
196 greg 2.42 case 's': /* one at a time */
197 greg 2.41 sequential = !sequential;
198     break;
199 greg 2.42 case 'o': /* 't' output */
200     tout = argv[i]+2;
201     break;
202 greg 2.43 case 't': /* msec interval */
203     tinterv = atoi(argv[++i]);
204     break;
205 greg 2.42 case 'e': /* exposure comp. */
206 greg 2.44 i++;
207 greg 2.47 if (argv[i][0] == 'a') {
208 greg 2.44 tmflags = TM_F_CAMERA;
209     break;
210     }
211 greg 2.47 if (argv[i][0] == 'h') {
212 greg 2.44 tmflags = TM_F_HUMAN;
213     break;
214     }
215     if (argv[i][0] != '+' && argv[i][0] != '-')
216 greg 1.1 goto userr;
217 greg 2.44 scale = atoi(argv[i]);
218 greg 1.1 break;
219 greg 2.42 case 'g': /* gamma comp. */
220 greg 2.7 if (argv[i][2] == 'e')
221 greg 1.1 geometry = argv[++i];
222     else
223 greg 2.35 gamstr = argv[++i];
224 greg 1.1 break;
225     default:
226     goto userr;
227     }
228 greg 1.3 else if (argv[i][0] == '=')
229     geometry = argv[i];
230 greg 1.1 else
231     break;
232    
233 greg 2.29 if (i > argc)
234     goto userr;
235     while (i < argc-1) {
236 greg 2.32 sigrecv = 0;
237     signal(SIGCONT, onsig);
238 greg 2.29 if ((pid=fork()) == 0) { /* a child for each picture */
239     parent = -1;
240     break;
241     }
242     if (pid < 0)
243     quiterr("fork failed");
244     parent++;
245 greg 2.32 while (!sigrecv)
246     pause(); /* wait for wake-up call */
247 greg 2.29 i++;
248     }
249     if (i < argc) { /* open picture file */
250 greg 1.1 fname = argv[i];
251     fin = fopen(fname, "r");
252 greg 2.33 if (fin == NULL)
253     quiterr("cannot open picture file");
254 greg 2.29 }
255 greg 1.1 /* get header */
256 greg 1.14 getheader(fin, headline, NULL);
257 greg 1.1 /* get picture dimensions */
258 greg 1.26 if (wrongformat || !fgetsresolu(&inpres, fin))
259 greg 1.14 quiterr("bad picture format");
260 greg 1.26 xmax = scanlen(&inpres);
261     ymax = numscans(&inpres);
262 greg 1.1 /* set view parameters */
263     if (gotview && setview(&ourview) != NULL)
264     gotview = 0;
265     if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
266     quiterr("out of memory");
267    
268 greg 2.24 init(argc, argv); /* get file and open window */
269 greg 1.1
270     for ( ; ; )
271     getevent(); /* main loop */
272     userr:
273     fprintf(stderr,
274 greg 2.73 "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] hdr ..\n",
275 greg 1.1 progname);
276 greg 2.21 exit(1);
277 greg 1.1 }
278    
279    
280 schorsch 2.65 static int
281     headline( /* get relevant info from header */
282     char *s,
283     void *p
284     )
285 greg 1.1 {
286 greg 1.14 char fmt[32];
287 greg 1.1
288 greg 1.2 if (isexpos(s))
289     exposure *= exposval(s);
290 greg 2.44 else if (formatval(fmt, s))
291 greg 1.14 wrongformat = strcmp(fmt, COLRFMT);
292 greg 2.44 else if (isview(s) && sscanview(&ourview, s) > 0)
293 greg 2.4 gotview++;
294 gwlarson 2.53 return(0);
295 greg 1.1 }
296    
297    
298 schorsch 2.66 static void
299     init( /* get data and open window */
300     int argc,
301     char **argv
302     )
303 greg 1.1 {
304 greg 1.4 XSetWindowAttributes ourwinattr;
305 greg 2.24 XClassHint xclshints;
306     XWMHints xwmhints;
307     XSizeHints xszhints;
308     XTextProperty windowName, iconName;
309     XGCValues xgcv;
310     char *name;
311 greg 2.6 register int i;
312 greg 1.1
313     if (fname != NULL) {
314     scanpos = (long *)malloc(ymax*sizeof(long));
315     if (scanpos == NULL)
316 greg 2.24 quiterr("out of memory");
317 greg 1.1 for (i = 0; i < ymax; i++)
318     scanpos[i] = -1;
319 greg 2.24 name = fname;
320     } else
321     name = progname;
322     /* remove directory prefix from name */
323     for (i = strlen(name); i-- > 0; )
324     if (name[i] == '/')
325     break;
326     name += i+1;
327 greg 2.6 if ((thedisplay = XOpenDisplay(dispname)) == NULL)
328     quiterr("cannot open display");
329 greg 2.35 /* set gamma value */
330     if (gamstr == NULL) /* get it from the X server */
331     gamstr = XGetDefault(thedisplay, "radiance", "gamma");
332     if (gamstr == NULL) /* get it from the environment */
333 greg 2.38 gamstr = getenv("DISPLAY_GAMMA");
334 greg 2.35 if (gamstr != NULL)
335     gamcor = atof(gamstr);
336 greg 2.6 /* get best visual for default screen */
337     getbestvis();
338 greg 1.4 /* store image */
339     getras();
340 greg 2.20 /* get size and position */
341 greg 2.24 xszhints.flags = 0;
342     xszhints.width = xmax; xszhints.height = ymax;
343 greg 2.20 if (geometry != NULL) {
344 greg 2.24 i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
345     (unsigned *)&xszhints.width,
346     (unsigned *)&xszhints.height);
347 greg 2.20 if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
348 greg 2.24 xszhints.flags |= USSize;
349 greg 2.20 else
350 greg 2.24 xszhints.flags |= PSize;
351 greg 2.20 if ((i&(XValue|YValue)) == (XValue|YValue)) {
352 greg 2.24 xszhints.flags |= USPosition;
353 greg 2.20 if (i & XNegative)
354 greg 2.24 xszhints.x += DisplayWidth(thedisplay,
355     ourscreen)-1-xszhints.width-2*BORWIDTH;
356 greg 2.20 if (i & YNegative)
357 greg 2.24 xszhints.y += DisplayHeight(thedisplay,
358     ourscreen)-1-xszhints.height-2*BORWIDTH;
359 greg 2.20 }
360     }
361 greg 2.24 /* open window */
362 greg 2.40 i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
363 greg 2.23 ourwinattr.border_pixel = ourwhite;
364     ourwinattr.background_pixel = ourblack;
365 greg 2.40 if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
366     ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
367     i |= CWColormap;
368     }
369 greg 2.24 ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
370     ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
371     ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
372     wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
373     xszhints.width, xszhints.height, BORWIDTH,
374 greg 2.40 ourvis.depth, InputOutput, ourvis.visual,
375     i, &ourwinattr);
376 greg 1.4 if (wind == 0)
377 greg 2.6 quiterr("cannot create window");
378 greg 1.5 width = xmax;
379     height = ymax;
380 greg 2.32 /* prepare graphics drawing context */
381     if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
382     quiterr("cannot get font");
383 greg 2.24 xgcv.foreground = ourblack;
384     xgcv.background = ourwhite;
385     ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
386     GCFont, &xgcv);
387     xgcv.function = GXinvert;
388     revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
389     GCFunction, &xgcv);
390    
391     /* set up the window manager */
392     xwmhints.flags = InputHint|IconPixmapHint;
393     xwmhints.input = True;
394     xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
395 greg 1.17 wind, icondata, iconwidth, iconheight);
396 greg 2.24
397     windowName.encoding = iconName.encoding = XA_STRING;
398     windowName.format = iconName.format = 8;
399     windowName.value = (u_char *)name;
400 greg 2.71 windowName.nitems = strlen((char *)windowName.value);
401 greg 2.24 iconName.value = (u_char *)name;
402 greg 2.71 iconName.nitems = strlen((char *)windowName.value);
403 greg 2.24
404     xclshints.res_name = NULL;
405     xclshints.res_class = "Ximage";
406     XSetWMProperties(thedisplay, wind, &windowName, &iconName,
407     argv, argc, &xszhints, &xwmhints, &xclshints);
408     closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
409     wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
410     XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
411    
412 greg 1.1 XMapWindow(thedisplay, wind);
413     } /* end of init */
414    
415    
416 schorsch 2.66 static void
417     quiterr( /* print message and exit */
418     char *err
419     )
420 greg 1.1 {
421 greg 2.31 register int es;
422     int cs;
423    
424 schorsch 2.64 if ( (es = err != NULL) )
425 greg 2.29 fprintf(stderr, "%s: %s: %s\n", progname,
426     fname==NULL?"<stdin>":fname, err);
427 greg 2.36 if (thedisplay != NULL)
428     XCloseDisplay(thedisplay);
429 schorsch 2.64 if ((parent < 0) & (sigrecv == 0))
430 greg 2.32 kill(getppid(), SIGCONT);
431 greg 2.31 while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
432     if (es == 0)
433     es = cs>>8 & 0xff;
434 greg 2.29 parent--;
435 greg 2.31 }
436     exit(es);
437 greg 1.1 }
438    
439    
440 greg 2.6 static int
441 schorsch 2.66 viscmp( /* compare visual to see which is better, descending */
442     register XVisualInfo *v1,
443     register XVisualInfo *v2
444     )
445 greg 2.6 {
446     int bad1 = 0, bad2 = 0;
447     register int *rp;
448    
449     if (v1->class == v2->class) {
450 greg 2.72 if ((v1->class == TrueColor) | (v1->class == DirectColor)) {
451     /* prefer 24-bit */
452     if ((v1->depth == 24) & (v2->depth > 24))
453 greg 2.6 return(-1);
454 greg 2.72 if ((v1->depth > 24) & (v2->depth == 24))
455 greg 2.6 return(1);
456 gwlarson 2.55 /* go for maximum depth otherwise */
457     return(v2->depth - v1->depth);
458 greg 2.6 }
459     /* don't be too greedy */
460 greg 2.72 if ((maxcolors <= 1<<v1->depth) & (maxcolors <= 1<<v2->depth))
461 greg 2.6 return(v1->depth - v2->depth);
462     return(v2->depth - v1->depth);
463     }
464 gwlarson 2.55 /* prefer Pseudo when < 15-bit */
465 greg 2.72 if ((v1->class == TrueColor) | (v1->class == DirectColor) &&
466 gwlarson 2.55 v1->depth < 15)
467 greg 2.6 bad1 = 1;
468 greg 2.72 if ((v2->class == TrueColor) | (v2->class == DirectColor) &&
469 gwlarson 2.55 v2->depth < 15)
470 greg 2.6 bad2 = -1;
471     if (bad1 | bad2)
472     return(bad1+bad2);
473     /* otherwise, use class ranking */
474     for (rp = ourrank; *rp != -1; rp++) {
475     if (v1->class == *rp)
476     return(-1);
477     if (v2->class == *rp)
478     return(1);
479     }
480     return(0);
481     }
482    
483    
484 schorsch 2.66 static void
485     getbestvis(void) /* get the best visual for this screen */
486 greg 2.6 {
487 greg 2.7 #ifdef DEBUG
488 greg 2.6 static char vistype[][12] = {
489     "StaticGray",
490     "GrayScale",
491     "StaticColor",
492     "PseudoColor",
493     "TrueColor",
494     "DirectColor"
495     };
496 greg 2.7 #endif
497 greg 2.6 static int rankings[3][6] = {
498     {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
499 greg 2.7 {PseudoColor,GrayScale,StaticGray,-1},
500     {PseudoColor,GrayScale,StaticGray,-1}
501 greg 2.6 };
502     XVisualInfo *xvi;
503     int vismatched;
504     register int i, j;
505    
506     if (greyscale) {
507     ourrank = rankings[2];
508     if (maxcolors < 2) maxcolors = 256;
509     } else if (maxcolors >= 2 && maxcolors <= 256)
510     ourrank = rankings[1];
511     else {
512     ourrank = rankings[0];
513     maxcolors = 256;
514     }
515     /* find best visual */
516     ourvis.screen = ourscreen;
517     xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
518     if (xvi == NULL)
519     quiterr("no visuals for this screen!");
520 greg 2.7 #ifdef DEBUG
521     fprintf(stderr, "Supported visuals:\n");
522     for (i = 0; i < vismatched; i++)
523     fprintf(stderr, "\ttype %s, depth %d\n",
524     vistype[xvi[i].class], xvi[i].depth);
525     #endif
526 greg 2.6 for (i = 0, j = 1; j < vismatched; j++)
527     if (viscmp(&xvi[i],&xvi[j]) > 0)
528     i = j;
529     /* compare to least acceptable */
530     for (j = 0; ourrank[j++] != -1; )
531     ;
532     ourvis.class = ourrank[--j];
533     ourvis.depth = 1;
534     if (viscmp(&xvi[i],&ourvis) > 0)
535     quiterr("inadequate visuals on this screen");
536     /* OK, we'll use it */
537 schorsch 2.63 ourvis = xvi[i];
538 greg 2.7 #ifdef DEBUG
539     fprintf(stderr, "Selected visual type %s, depth %d\n",
540     vistype[ourvis.class], ourvis.depth);
541     #endif
542 greg 2.8 /* make appropriate adjustments */
543 greg 2.6 if (ourvis.class == GrayScale || ourvis.class == StaticGray)
544     greyscale = 1;
545 greg 2.7 if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
546     maxcolors = ourvis.colormap_size;
547 greg 2.8 if (ourvis.class == StaticGray) {
548     ourblack = 0;
549     ourwhite = 255;
550     } else if (ourvis.class == PseudoColor) {
551     ourblack = BlackPixel(thedisplay,ourscreen);
552     ourwhite = WhitePixel(thedisplay,ourscreen);
553 greg 2.9 if ((ourblack|ourwhite) & ~255L) {
554     ourblack = 0;
555     ourwhite = 1;
556     }
557 greg 2.13 if (maxcolors > 4)
558     maxcolors -= 2;
559 greg 2.8 } else {
560     ourblack = 0;
561 greg 2.9 ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
562 greg 2.8 }
563 greg 2.6 XFree((char *)xvi);
564     }
565    
566    
567 schorsch 2.66 static void
568     getras(void) /* get raster file */
569 greg 1.1 {
570     if (maxcolors <= 2) { /* monochrome */
571     ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
572     if (ourdata == NULL)
573     goto fail;
574 greg 2.71 ourras = make_raster(thedisplay, &ourvis, 1, (char *)ourdata,
575 greg 1.1 xmax, ymax, 8);
576     if (ourras == NULL)
577     goto fail;
578     getmono();
579 schorsch 2.64 } else if ((ourvis.class == TrueColor) | (ourvis.class == DirectColor)) {
580 greg 2.59 int datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16);
581 gwlarson 2.55 ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
582 greg 1.1 if (ourdata == NULL)
583     goto fail;
584 gwlarson 2.55 ourras = make_raster(thedisplay, &ourvis, datsiz*8,
585 greg 2.71 (char *)ourdata, xmax, ymax, datsiz*8);
586 greg 1.1 if (ourras == NULL)
587     goto fail;
588     getfull();
589     } else {
590     ourdata = (unsigned char *)malloc(xmax*ymax);
591     if (ourdata == NULL)
592     goto fail;
593 greg 2.71 ourras = make_raster(thedisplay, &ourvis, 8, (char *)ourdata,
594 greg 1.1 xmax, ymax, 8);
595     if (ourras == NULL)
596     goto fail;
597 greg 2.44 if (greyscale)
598 greg 2.7 getgrey();
599 greg 2.13 else
600     getmapped();
601     if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
602     goto fail;
603 greg 1.1 }
604 greg 2.13 return;
605 greg 1.1 fail:
606 greg 1.9 quiterr("could not create raster image");
607 greg 1.1 }
608    
609    
610 schorsch 2.66 static void
611     getevent(void) /* process the next event */
612 greg 1.1 {
613 greg 2.24 XEvent xev;
614 greg 1.1
615 greg 2.24 XNextEvent(thedisplay, &xev);
616     switch ((int)xev.type) {
617 greg 1.1 case KeyPress:
618 greg 2.24 docom(&xev.xkey);
619 greg 1.1 break;
620     case ConfigureNotify:
621 greg 2.24 width = xev.xconfigure.width;
622     height = xev.xconfigure.height;
623 greg 1.1 break;
624     case MapNotify:
625     map_rcolors(ourras, wind);
626     if (fast)
627 greg 2.6 make_rpixmap(ourras, wind);
628 schorsch 2.64 if ((!sequential) & (parent < 0) & (sigrecv == 0)) {
629 greg 2.39 kill(getppid(), SIGCONT);
630     sigrecv--;
631     }
632 greg 1.1 break;
633     case UnmapNotify:
634 greg 2.7 if (!fast)
635     unmap_rcolors(ourras);
636 greg 1.1 break;
637     case Expose:
638 greg 2.24 redraw(xev.xexpose.x, xev.xexpose.y,
639     xev.xexpose.width, xev.xexpose.height);
640 greg 1.1 break;
641     case ButtonPress:
642 greg 2.24 if (xev.xbutton.state & (ShiftMask|ControlMask))
643     moveimage(&xev.xbutton);
644 greg 1.1 else
645 greg 2.43 switch (xev.xbutton.button) {
646     case Button1:
647     getbox(&xev.xbutton);
648     break;
649     case Button2:
650     traceray(xev.xbutton.x, xev.xbutton.y);
651     break;
652     case Button3:
653     trackrays(&xev.xbutton);
654     break;
655     }
656 greg 1.1 break;
657 greg 2.29 case ClientMessage:
658 greg 2.24 if ((xev.xclient.message_type == wmProtocolsAtom) &&
659     (xev.xclient.data.l[0] == closedownAtom))
660     quiterr(NULL);
661     break;
662 greg 1.1 }
663     }
664    
665    
666 schorsch 2.66 static int
667     traceray( /* print requested pixel data */
668     int xpos,
669     int ypos
670     )
671 greg 2.22 {
672 schorsch 2.60 RREAL hv[2];
673 greg 2.22 FVECT rorg, rdir;
674 greg 2.42 COLOR cval;
675     register char *cp;
676 greg 2.22
677 greg 2.56 bbox.xmin = xpos; bbox.xsiz = 1;
678     bbox.ymin = ypos; bbox.ysiz = 1;
679 greg 2.42 avgbox(cval);
680     scalecolor(cval, 1./exposure);
681 greg 2.22 pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
682 greg 2.42 if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
683     rorg[0] = rorg[1] = rorg[2] =
684     rdir[0] = rdir[1] = rdir[2] = 0.;
685    
686     for (cp = tout; *cp; cp++) /* print what they asked for */
687     switch (*cp) {
688     case 'o': /* origin */
689     printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
690     break;
691     case 'd': /* direction */
692     printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
693     break;
694     case 'v': /* radiance value */
695     printf("%e %e %e ", colval(cval,RED),
696     colval(cval,GRN), colval(cval,BLU));
697     break;
698     case 'l': /* luminance */
699     printf("%e ", luminance(cval));
700     break;
701     case 'p': /* pixel position */
702     printf("%d %d ", (int)(hv[0]*inpres.xr),
703     (int)(hv[1]*inpres.yr));
704     break;
705     }
706     putchar('\n');
707 greg 2.22 fflush(stdout);
708     return(0);
709     }
710    
711    
712 schorsch 2.66 static int
713     docom( /* execute command */
714     XKeyPressedEvent *ekey
715     )
716 greg 1.1 {
717     char buf[80];
718     COLOR cval;
719     XColor cvx;
720     int com, n;
721     double comp;
722 schorsch 2.60 RREAL hv[2];
723 greg 1.1
724     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
725     if (n == 0)
726     return(0);
727     com = buf[0];
728     switch (com) { /* interpret command */
729     case 'q':
730 greg 2.22 case 'Q':
731 greg 2.3 case CTRL('D'): /* quit */
732 greg 2.21 quiterr(NULL);
733 greg 1.1 case '\n':
734     case '\r':
735     case 'l':
736     case 'c': /* value */
737 gregl 2.50 if (!avgbox(cval))
738 greg 1.1 return(-1);
739     switch (com) {
740     case '\n':
741     case '\r': /* radiance */
742     sprintf(buf, "%.3f", intens(cval)/exposure);
743     break;
744     case 'l': /* luminance */
745 greg 2.56 sprintf(buf, "%.1fL", luminance(cval)/exposure);
746 greg 1.1 break;
747     case 'c': /* color */
748     comp = pow(2.0, (double)scale);
749     sprintf(buf, "(%.2f,%.2f,%.2f)",
750     colval(cval,RED)*comp,
751     colval(cval,GRN)*comp,
752     colval(cval,BLU)*comp);
753     break;
754     }
755 greg 1.3 XDrawImageString(thedisplay, wind, ourgc,
756 greg 2.56 bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
757 greg 1.1 return(0);
758     case 'i': /* identify (contour) */
759     if (ourras->pixels == NULL)
760     return(-1);
761     n = ourdata[ekey->x-xoff+xmax*(ekey->y-yoff)];
762     n = ourras->pmap[n];
763     cvx.pixel = ourras->cdefs[n].pixel;
764     cvx.red = random() & 65535;
765     cvx.green = random() & 65535;
766     cvx.blue = random() & 65535;
767 greg 1.2 cvx.flags = DoRed|DoGreen|DoBlue;
768     XStoreColor(thedisplay, ourras->cmap, &cvx);
769 greg 1.1 return(0);
770     case 'p': /* position */
771 greg 1.26 pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
772     sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
773     (int)(hv[1]*inpres.yr));
774 greg 1.1 XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
775     buf, strlen(buf));
776     return(0);
777     case 't': /* trace */
778 greg 2.22 return(traceray(ekey->x, ekey->y));
779 greg 2.46 case 'a': /* auto exposure */
780 gwlarson 2.51 if (fname == NULL)
781     return(-1);
782 greg 2.46 tmflags = TM_F_CAMERA;
783     strcpy(buf, "auto exposure...");
784     goto remap;
785     case 'h': /* human response */
786 gwlarson 2.51 if (fname == NULL)
787     return(-1);
788 greg 2.46 tmflags = TM_F_HUMAN;
789     strcpy(buf, "human exposure...");
790     goto remap;
791 greg 1.1 case '=': /* adjust exposure */
792 greg 2.25 case '@': /* adaptation level */
793 gregl 2.50 if (!avgbox(cval))
794 greg 1.1 return(-1);
795 greg 2.34 comp = bright(cval);
796     if (comp < 1e-20) {
797     XBell(thedisplay, 0);
798     return(-1);
799     }
800     if (com == '@')
801     comp = 106./exposure/
802     pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
803     else
804     comp = .5/comp;
805 greg 2.25 comp = log(comp)/.69315 - scale;
806     n = comp < 0 ? comp-.5 : comp+.5 ; /* round */
807 gregl 2.50 if (tmflags != TM_F_LINEAR)
808     tmflags = TM_F_LINEAR; /* turn off tone mapping */
809 greg 2.44 else {
810     if (n == 0) /* else check if any change */
811     return(0);
812     scale_rcolors(ourras, pow(2.0, (double)n));
813     }
814 greg 1.1 scale += n;
815     sprintf(buf, "%+d", scale);
816 greg 2.46 remap:
817 greg 1.3 XDrawImageString(thedisplay, wind, ourgc,
818 greg 2.56 bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
819 greg 1.1 XFlush(thedisplay);
820 greg 2.56 /* free(ourdata); This is done in XDestroyImage()! */
821 greg 1.1 free_raster(ourras);
822     getras();
823     /* fall through */
824 greg 2.3 case CTRL('R'): /* redraw */
825     case CTRL('L'):
826 greg 1.1 unmap_rcolors(ourras);
827     XClearWindow(thedisplay, wind);
828     map_rcolors(ourras, wind);
829     if (fast)
830 greg 2.12 make_rpixmap(ourras, wind);
831 greg 1.1 redraw(0, 0, width, height);
832     return(0);
833 greg 2.27 case 'f': /* turn on fast redraw */
834     fast = 1;
835     make_rpixmap(ourras, wind);
836     return(0);
837     case 'F': /* turn off fast redraw */
838     fast = 0;
839     free_rpixmap(ourras);
840     return(0);
841 greg 2.19 case '0': /* recenter origin */
842 schorsch 2.64 if ((xoff == 0) & (yoff == 0))
843 greg 2.19 return(0);
844     xoff = yoff = 0;
845     XClearWindow(thedisplay, wind);
846     redraw(0, 0, width, height);
847     return(0);
848 greg 1.1 case ' ': /* clear */
849 greg 2.56 redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz);
850 greg 1.1 return(0);
851     default:
852 greg 1.2 XBell(thedisplay, 0);
853 greg 1.1 return(-1);
854     }
855     }
856    
857    
858 schorsch 2.66 static void
859     moveimage( /* shift the image */
860     XButtonPressedEvent *ebut
861     )
862 greg 1.1 {
863 greg 2.24 XEvent e;
864 greg 1.5 int mxo, myo;
865 greg 1.1
866 greg 2.24 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
867     while (e.type == MotionNotify) {
868     mxo = e.xmotion.x;
869     myo = e.xmotion.y;
870 greg 1.5 revline(ebut->x, ebut->y, mxo, myo);
871     revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
872     xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
873 greg 2.24 XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
874 greg 1.5 revline(ebut->x, ebut->y, mxo, myo);
875     revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
876     xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
877 greg 1.3 }
878 greg 2.24 xoff += e.xbutton.x - ebut->x;
879     yoff += e.xbutton.y - ebut->y;
880 greg 1.1 XClearWindow(thedisplay, wind);
881     redraw(0, 0, width, height);
882     }
883    
884    
885 schorsch 2.66 static void
886     getbox( /* get new bbox */
887     XButtonPressedEvent *ebut
888     )
889 greg 1.1 {
890 greg 2.24 XEvent e;
891 greg 1.1
892 greg 2.24 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
893     while (e.type == MotionNotify) {
894 greg 2.56 revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y);
895 greg 2.24 XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
896 greg 2.56 revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin);
897 greg 1.1 }
898 greg 2.56 bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
899     bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
900     if (bbox.xmin > ebut->x) {
901     bbox.xsiz = bbox.xmin - ebut->x + 1;
902     bbox.xmin = ebut->x;
903 greg 1.1 } else {
904 greg 2.56 bbox.xsiz = ebut->x - bbox.xmin + 1;
905 greg 1.1 }
906 greg 2.56 if (bbox.ymin > ebut->y) {
907     bbox.ysiz = bbox.ymin - ebut->y + 1;
908     bbox.ymin = ebut->y;
909 greg 1.1 } else {
910 greg 2.56 bbox.ysiz = ebut->y - bbox.ymin + 1;
911 greg 2.43 }
912     }
913    
914    
915 schorsch 2.66 static void
916     trackrays( /* trace rays as mouse moves */
917     XButtonPressedEvent *ebut
918     )
919 greg 2.43 {
920     XEvent e;
921     unsigned long lastrept;
922    
923     traceray(ebut->x, ebut->y);
924     lastrept = ebut->time;
925     XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
926     while (e.type == MotionNotify) {
927     if (e.xmotion.time >= lastrept + tinterv) {
928     traceray(e.xmotion.x, e.xmotion.y);
929     lastrept = e.xmotion.time;
930     }
931     XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
932 greg 1.1 }
933     }
934    
935    
936 schorsch 2.66 static void
937     revbox( /* draw bbox with reversed lines */
938     int x0,
939     int y0,
940     int x1,
941     int y1
942     )
943 greg 1.1 {
944 greg 1.5 revline(x0, y0, x1, y0);
945     revline(x0, y1, x1, y1);
946     revline(x0, y0, x0, y1);
947     revline(x1, y0, x1, y1);
948     }
949 greg 1.1
950    
951 schorsch 2.66 static void
952     colavg(
953     register COLR *scn,
954     register int n,
955     void *cavg
956     )
957 greg 1.1 {
958 gregl 2.50 COLOR col;
959    
960     while (n--) {
961 greg 2.56 colr_color(col, *scn++);
962 schorsch 2.66 addcolor((COLORV*)cavg, col);
963 gregl 2.50 }
964     }
965    
966    
967 schorsch 2.66 static int
968     avgbox( /* average color over current bbox */
969     COLOR cavg
970     )
971 gregl 2.50 {
972     double d;
973     register int rval;
974    
975     setcolor(cavg, 0., 0., 0.);
976 greg 2.69 rval = dobox(colavg, (void *)cavg);
977 gregl 2.50 if (rval > 0) {
978     d = 1./rval;
979     scalecolor(cavg, d);
980     }
981     return(rval);
982     }
983    
984    
985 schorsch 2.66 static int
986     dobox( /* run function over bbox */
987     //void (*f)(), /* function to call for each subscan */
988     doboxf_t *f, /* function to call for each subscan */
989     void *p /* pointer to private data */
990     )
991 gregl 2.50 {
992 greg 1.1 int left, right, top, bottom;
993     int y;
994    
995 greg 2.56 left = bbox.xmin - xoff;
996     right = left + bbox.xsiz;
997 greg 1.1 if (left < 0)
998     left = 0;
999     if (right > xmax)
1000     right = xmax;
1001     if (left >= right)
1002 gregl 2.50 return(0);
1003 greg 2.56 top = bbox.ymin - yoff;
1004     bottom = top + bbox.ysiz;
1005 greg 1.1 if (top < 0)
1006     top = 0;
1007     if (bottom > ymax)
1008     bottom = ymax;
1009     if (top >= bottom)
1010 greg 2.15 return(0);
1011 greg 1.1 for (y = top; y < bottom; y++) {
1012     if (getscan(y) == -1)
1013     return(-1);
1014 gregl 2.50 (*f)(scanline+left, right-left, p);
1015 greg 1.1 }
1016 gregl 2.50 return((right-left)*(bottom-top));
1017 greg 1.1 }
1018    
1019    
1020 schorsch 2.66 static void
1021     addfix( /* add fixation points to histogram */
1022     COLR *scn,
1023     int n,
1024 greg 2.69 void *p
1025 schorsch 2.66 )
1026 gregl 2.50 {
1027 greg 2.69 TMstruct * tms = (TMstruct *)p;
1028    
1029     if (tmCvColrs(tms, lscan, TM_NOCHROM, scn, n))
1030 gregl 2.50 goto tmerr;
1031 greg 2.69 if (tmAddHisto(tms, lscan, n, FIXWEIGHT))
1032 gregl 2.50 goto tmerr;
1033     return;
1034     tmerr:
1035     quiterr("tone mapping error");
1036     }
1037    
1038    
1039 schorsch 2.66 static void
1040     make_tonemap(void) /* initialize tone mapping */
1041 greg 2.44 {
1042 greg 2.48 int flags, y;
1043 greg 2.44
1044 gregl 2.50 if (tmflags != TM_F_LINEAR && fname == NULL) {
1045 greg 2.44 fprintf(stderr, "%s: cannot adjust tone of standard input\n",
1046     progname);
1047 gregl 2.50 tmflags = TM_F_LINEAR;
1048 greg 2.44 }
1049 gregl 2.50 if (tmflags == TM_F_LINEAR) { /* linear with clamping */
1050 greg 2.44 setcolrcor(pow, 1.0/gamcor);
1051     return;
1052     }
1053 greg 2.49 flags = tmflags; /* histogram adjustment */
1054 greg 2.48 if (greyscale) flags |= TM_F_BW;
1055 greg 2.69 if (tmGlobal != NULL) { /* reuse old histogram if one */
1056     tmDone(tmCurrent); tmCurrent = NULL;
1057     tmGlobal->flags = flags;
1058 greg 2.49 } else { /* else initialize */
1059     if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
1060 greg 2.44 goto memerr;
1061 greg 2.49 if (greyscale) {
1062     cscan = TM_NOCHROM;
1063     if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
1064     goto memerr;
1065     } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1066     == NULL)
1067     goto memerr;
1068     /* initialize tm library */
1069 greg 2.69 tmGlobal = tmInit(flags, stdprims, gamcor);
1070     if (tmGlobal == NULL)
1071 greg 2.49 goto memerr;
1072 greg 2.70 if (tmSetSpace(tmGlobal, stdprims, WHTEFFICACY/exposure, NULL))
1073 greg 2.44 goto tmerr;
1074 greg 2.49 /* compute picture histogram */
1075     for (y = 0; y < ymax; y++) {
1076     getscan(y);
1077 greg 2.69 if (tmCvColrs(tmGlobal, lscan, TM_NOCHROM,
1078     scanline, xmax))
1079 greg 2.49 goto tmerr;
1080 greg 2.69 if (tmAddHisto(tmGlobal, lscan, xmax, 1))
1081 greg 2.49 goto tmerr;
1082     }
1083 greg 2.44 }
1084 greg 2.69 tmCurrent = tmDup(tmGlobal); /* add fixations to duplicate map */
1085     dobox(addfix, (void *)tmCurrent);
1086 greg 2.49 /* (re)compute tone mapping */
1087 greg 2.69 if (tmComputeMapping(tmCurrent, gamcor, 0., 0.))
1088 greg 2.44 goto tmerr;
1089     return;
1090     memerr:
1091     quiterr("out of memory in make_tonemap");
1092     tmerr:
1093     quiterr("tone mapping error");
1094     }
1095    
1096    
1097 schorsch 2.66 static void
1098     tmap_colrs( /* apply tone mapping to scanline */
1099     register COLR *scn,
1100     int len
1101     )
1102 greg 2.44 {
1103     register BYTE *ps;
1104    
1105 gregl 2.50 if (tmflags == TM_F_LINEAR) {
1106 greg 2.44 if (scale)
1107     shiftcolrs(scn, len, scale);
1108     colrs_gambs(scn, len);
1109     return;
1110     }
1111     if (len > xmax)
1112     quiterr("code error 1 in tmap_colrs");
1113 greg 2.69 if (tmCvColrs(tmCurrent, lscan, cscan, scn, len))
1114 greg 2.44 goto tmerr;
1115 greg 2.69 if (tmMapPixels(tmCurrent, pscan, lscan, cscan, len))
1116 greg 2.44 goto tmerr;
1117     ps = pscan;
1118 greg 2.48 if (greyscale)
1119 greg 2.44 while (len--) {
1120     scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1121     scn[0][EXP] = COLXS;
1122     scn++;
1123     }
1124     else
1125     while (len--) {
1126     scn[0][RED] = *ps++;
1127     scn[0][GRN] = *ps++;
1128     scn[0][BLU] = *ps++;
1129     scn[0][EXP] = COLXS;
1130     scn++;
1131     }
1132     return;
1133     tmerr:
1134     quiterr("tone mapping error");
1135     }
1136    
1137    
1138 schorsch 2.66 static void
1139     getmono(void) /* get monochrome data */
1140 greg 1.1 {
1141     register unsigned char *dp;
1142     register int x, err;
1143 greg 1.23 int y, errp;
1144 greg 1.1 short *cerr;
1145    
1146 greg 1.10 if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
1147 greg 1.9 quiterr("out of memory in getmono");
1148 greg 1.1 dp = ourdata - 1;
1149     for (y = 0; y < ymax; y++) {
1150 greg 2.15 getscan(y);
1151 greg 2.16 add2icon(y, scanline);
1152 greg 1.10 normcolrs(scanline, xmax, scale);
1153 greg 1.1 err = 0;
1154     for (x = 0; x < xmax; x++) {
1155     if (!(x&7))
1156     *++dp = 0;
1157 greg 1.23 errp = err;
1158 greg 1.10 err += normbright(scanline[x]) + cerr[x];
1159 greg 1.1 if (err > 127)
1160     err -= 255;
1161     else
1162 greg 1.3 *dp |= 1<<(7-(x&07));
1163 greg 1.23 err /= 3;
1164     cerr[x] = err + errp;
1165 greg 1.1 }
1166     }
1167 greg 2.56 free((void *)cerr);
1168 greg 1.1 }
1169    
1170    
1171 schorsch 2.66 static void
1172     add2icon( /* add a scanline to our icon data */
1173     int y,
1174     COLR *scan
1175     )
1176 greg 1.17 {
1177     static short cerr[ICONSIZ];
1178 greg 1.20 static int ynext;
1179     static char *dp;
1180 greg 2.17 COLR clr;
1181 greg 1.17 register int err;
1182 greg 1.20 register int x, ti;
1183 greg 1.23 int errp;
1184 greg 1.17
1185     if (iconheight == 0) { /* initialize */
1186 greg 1.18 if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
1187 greg 1.17 iconwidth = xmax;
1188     iconheight = ymax;
1189     } else if (xmax > ymax) {
1190     iconwidth = ICONSIZ;
1191     iconheight = ICONSIZ*ymax/xmax;
1192 greg 1.24 if (iconheight < 1)
1193     iconheight = 1;
1194 greg 1.17 } else {
1195     iconwidth = ICONSIZ*xmax/ymax;
1196 greg 1.24 if (iconwidth < 1)
1197     iconwidth = 1;
1198 greg 1.17 iconheight = ICONSIZ;
1199     }
1200 greg 1.20 ynext = 0;
1201 greg 1.17 dp = icondata - 1;
1202     }
1203 greg 1.20 if (y < ynext*ymax/iconheight) /* skip this one */
1204 greg 1.17 return;
1205     err = 0;
1206     for (x = 0; x < iconwidth; x++) {
1207     if (!(x&7))
1208     *++dp = 0;
1209 greg 1.23 errp = err;
1210 greg 1.20 ti = x*xmax/iconwidth;
1211 greg 2.17 copycolr(clr, scan[ti]);
1212 greg 2.56 normcolrs(&clr, 1, scale);
1213 greg 2.17 err += normbright(clr) + cerr[x];
1214 greg 1.17 if (err > 127)
1215     err -= 255;
1216     else
1217     *dp |= 1<<(x&07);
1218 greg 1.23 err /= 3;
1219     cerr[x] = err + errp;
1220 greg 1.17 }
1221 greg 1.20 ynext++;
1222 greg 1.17 }
1223    
1224    
1225 schorsch 2.66 static void
1226     getfull(void) /* get full (24-bit) data */
1227 greg 1.1 {
1228     int y;
1229 greg 2.59 register uint32 *dp;
1230     register uint16 *dph;
1231 greg 1.10 register int x;
1232 greg 2.44 /* initialize tone mapping */
1233     make_tonemap();
1234 greg 1.10 /* read and convert file */
1235 greg 2.59 dp = (uint32 *)ourdata;
1236     dph = (uint16 *)ourdata;
1237 greg 1.10 for (y = 0; y < ymax; y++) {
1238 greg 2.15 getscan(y);
1239 greg 2.16 add2icon(y, scanline);
1240 greg 2.44 tmap_colrs(scanline, xmax);
1241 gwlarson 2.55 switch (ourras->image->blue_mask) {
1242     case 0xff: /* 24-bit RGB */
1243 greg 2.6 for (x = 0; x < xmax; x++)
1244 greg 2.59 *dp++ = (uint32)scanline[x][RED] << 16 |
1245     (uint32)scanline[x][GRN] << 8 |
1246     (uint32)scanline[x][BLU] ;
1247 gwlarson 2.55 break;
1248     case 0xff0000: /* 24-bit BGR */
1249 greg 2.6 for (x = 0; x < xmax; x++)
1250 greg 2.59 *dp++ = (uint32)scanline[x][RED] |
1251     (uint32)scanline[x][GRN] << 8 |
1252     (uint32)scanline[x][BLU] << 16 ;
1253 gwlarson 2.55 break;
1254     #if 0
1255     case 0x1f: /* 15-bit RGB */
1256     for (x = 0; x < xmax; x++)
1257     *dph++ = (scanline[x][RED] << 7 & 0x7c00) |
1258     (scanline[x][GRN] << 2 & 0x3e0) |
1259     (unsigned)scanline[x][BLU] >> 3 ;
1260     break;
1261     case 0x7c00: /* 15-bit BGR */
1262     for (x = 0; x < xmax; x++)
1263     *dph++ = (unsigned)scanline[x][RED] >> 3 |
1264     (scanline[x][GRN] << 2 & 0x3e0) |
1265     (scanline[x][BLU] << 7 & 0x7c00) ;
1266     break;
1267     #endif
1268     default: /* unknown */
1269     if (ourvis.depth > 16)
1270     for (x = 0; x < xmax; x++) {
1271     *dp = ourras->image->red_mask &
1272     ourras->image->red_mask*scanline[x][RED]/255;
1273     *dp |= ourras->image->green_mask &
1274     ourras->image->green_mask*scanline[x][GRN]/255;
1275     *dp++ |= ourras->image->blue_mask &
1276     ourras->image->blue_mask*scanline[x][BLU]/255;
1277     }
1278     else
1279     for (x = 0; x < xmax; x++) {
1280     *dph = ourras->image->red_mask &
1281     ourras->image->red_mask*scanline[x][RED]/255;
1282     *dph |= ourras->image->green_mask &
1283     ourras->image->green_mask*scanline[x][GRN]/255;
1284     *dph++ |= ourras->image->blue_mask &
1285     ourras->image->blue_mask*scanline[x][BLU]/255;
1286     }
1287     break;
1288     }
1289 greg 1.10 }
1290 greg 1.1 }
1291    
1292    
1293 schorsch 2.66 static void
1294     getgrey(void) /* get greyscale data */
1295 greg 2.7 {
1296     int y;
1297     register unsigned char *dp;
1298     register int x;
1299 greg 2.44 /* initialize tone mapping */
1300     make_tonemap();
1301 greg 2.7 /* read and convert file */
1302     dp = ourdata;
1303     for (y = 0; y < ymax; y++) {
1304 greg 2.15 getscan(y);
1305 greg 2.16 add2icon(y, scanline);
1306 greg 2.44 tmap_colrs(scanline, xmax);
1307 greg 2.13 if (maxcolors < 256)
1308 greg 2.7 for (x = 0; x < xmax; x++)
1309 greg 2.59 *dp++ = ((int32)scanline[x][GRN] *
1310 greg 2.18 maxcolors + maxcolors/2) >> 8;
1311 greg 2.7 else
1312     for (x = 0; x < xmax; x++)
1313 greg 2.18 *dp++ = scanline[x][GRN];
1314 greg 2.7 }
1315 greg 2.13 for (x = 0; x < maxcolors; x++)
1316     clrtab[x][RED] = clrtab[x][GRN] =
1317 greg 2.59 clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors;
1318 greg 2.7 }
1319    
1320    
1321 schorsch 2.66 static void
1322     getmapped(void) /* get color-mapped data */
1323 greg 2.13 {
1324     int y;
1325 greg 2.30 /* make sure we can do it first */
1326     if (fname == NULL)
1327     quiterr("cannot map colors from standard input");
1328 greg 2.44 /* initialize tone mapping */
1329     make_tonemap();
1330 greg 2.13 /* make histogram */
1331 greg 2.59 if (new_histo((int32)xmax*ymax) == -1)
1332 greg 2.37 quiterr("cannot initialize histogram");
1333 greg 2.13 for (y = 0; y < ymax; y++) {
1334     if (getscan(y) < 0)
1335 greg 2.30 break;
1336 greg 2.16 add2icon(y, scanline);
1337 greg 2.44 tmap_colrs(scanline, xmax);
1338 greg 2.13 cnt_colrs(scanline, xmax);
1339     }
1340     /* map pixels */
1341     if (!new_clrtab(maxcolors))
1342     quiterr("cannot create color map");
1343     for (y = 0; y < ymax; y++) {
1344 greg 2.30 getscan(y);
1345 greg 2.44 tmap_colrs(scanline, xmax);
1346 greg 2.13 if (dither)
1347     dith_colrs(ourdata+y*xmax, scanline, xmax);
1348     else
1349     map_colrs(ourdata+y*xmax, scanline, xmax);
1350     }
1351     }
1352    
1353    
1354 schorsch 2.66 static void
1355     scale_rcolors( /* scale color map */
1356     register XRASTER *xr,
1357     double sf
1358     )
1359 greg 1.1 {
1360     register int i;
1361     long maxv;
1362    
1363     if (xr->pixels == NULL)
1364     return;
1365    
1366     sf = pow(sf, 1.0/gamcor);
1367     maxv = 65535/sf;
1368    
1369     for (i = xr->ncolors; i--; ) {
1370     xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
1371     65535 :
1372     xr->cdefs[i].red * sf;
1373     xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
1374     65535 :
1375     xr->cdefs[i].green * sf;
1376     xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
1377     65535 :
1378     xr->cdefs[i].blue * sf;
1379     }
1380     XStoreColors(thedisplay, xr->cmap, xr->cdefs, xr->ncolors);
1381     }
1382    
1383    
1384 schorsch 2.66 static int
1385     getscan(
1386     int y
1387     )
1388 greg 1.1 {
1389 greg 2.30 static int trunced = -1; /* truncated file? */
1390     skipit:
1391     if (trunced >= 0 && y >= trunced) {
1392 schorsch 2.61 memset(scanline, '\0', xmax*sizeof(COLR));
1393 greg 2.30 return(-1);
1394     }
1395 greg 1.1 if (y != cury) {
1396     if (scanpos == NULL || scanpos[y] == -1)
1397     return(-1);
1398     if (fseek(fin, scanpos[y], 0) == -1)
1399 greg 1.9 quiterr("fseek error");
1400 greg 1.1 cury = y;
1401 greg 2.11 } else if (scanpos != NULL && scanpos[y] == -1)
1402 greg 1.1 scanpos[y] = ftell(fin);
1403    
1404 greg 2.30 if (freadcolrs(scanline, xmax, fin) < 0) {
1405     fprintf(stderr, "%s: %s: unfinished picture\n",
1406     progname, fname==NULL?"<stdin>":fname);
1407     trunced = y;
1408     goto skipit;
1409     }
1410 greg 1.1 cury++;
1411     return(0);
1412     }