ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.79
Committed: Mon Apr 28 01:37:07 2025 UTC (11 days, 6 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.78: +37 -35 lines
Log Message:
fix(ximage): Fixed bug in area averaging introduced with colr_color() inlining

File Contents

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