ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 2.80
Committed: Tue Jun 3 21:31:51 2025 UTC (2 days, 17 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.79: +2 -4 lines
Log Message:
refactor: More consistent use of global char * progname and fixargv0()

File Contents

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