ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.68
Committed: Tue Sep 28 17:54:18 2004 UTC (19 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 2.67: +1 -1 lines
Log Message:
Fixed messed-up RCCS Id at top of file

File Contents

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