ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
(Generate patch)

Comparing ray/src/px/x11image.c (file contents):
Revision 1.20 by greg, Fri May 3 10:54:47 1991 UTC vs.
Revision 2.20 by greg, Fri Feb 12 18:03:08 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 27 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27  
28   #include  "color.h"
29   #include  "view.h"
30 #include  "pic.h"
30   #include  "x11raster.h"
31   #include  "random.h"
32 + #include  "resolu.h"
33  
34   #define  FONTNAME       "8x13"          /* text font we'll use */
35  
36 < #define  CTRL(c)        ('c'-'@')
36 > #define  CTRL(c)        ((c)-'@')
37  
38   #define  BORWIDTH       5               /* border width */
39  
40   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
41  
42   #define  ourscreen      DefaultScreen(thedisplay)
43 #define  ourblack       BlackPixel(thedisplay,ourscreen)
44 #define  ourwhite       WhitePixel(thedisplay,ourscreen)
43   #define  ourroot        RootWindow(thedisplay,ourscreen)
46 #define  ourgc          DefaultGC(thedisplay,ourscreen)
44  
45   #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
46  
# Line 54 | Line 51 | double  gamcor = 2.2;                  /* gamma correction */
51   int  dither = 1;                        /* dither colors? */
52   int  fast = 0;                          /* keep picture in Pixmap? */
53  
54 + char    *dispname = NULL;               /* our display name */
55 +
56   Window  wind = 0;                       /* our output window */
57 + unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
58   Font  fontid;                           /* our font */
59  
60   int  maxcolors = 0;                     /* maximum colors */
# Line 70 | Line 70 | int  gotview = 0;                      /* got parameters from file */
70  
71   COLR  *scanline;                        /* scan line buffer */
72  
73 < int  xmax, ymax;                        /* picture resolution */
73 > RESOLU  inpres;                         /* input resolution and ordering */
74 > int  xmax, ymax;                        /* picture dimensions */
75   int  width, height;                     /* window size */
76   char  *fname = NULL;                    /* input file name */
77   FILE  *fin = stdin;                     /* input file */
# Line 81 | Line 82 | double  exposure = 1.0;                        /* exposure compensation use
82  
83   int  wrongformat = 0;                   /* input in another format? */
84  
85 + GC      ourgc;                          /* standard graphics context */
86   GC      revgc;                          /* graphics context with GXinvert */
87  
88 < XRASTER *ourras;                        /* our stored image */
88 > int             *ourrank;               /* our visual class ranking */
89 > XVisualInfo     ourvis;                 /* our visual */
90 > XRASTER         *ourras;                /* our stored image */
91   unsigned char   *ourdata;               /* our image data */
92  
93   struct {
# Line 99 | Line 103 | char  *progname;
103  
104   char  errmsg[128];
105  
106 + extern BYTE  clrtab[256][3];            /* global color map */
107 +
108   extern long  ftell();
109  
110   extern char  *malloc(), *calloc();
111  
106 extern double  atof(), pow(), log();
107
112   Display  *thedisplay;
113  
114 +
115   main(argc, argv)
116   int  argc;
117   char  *argv[];
118   {
119 +        extern char  *getenv();
120 +        char  *gv;
121          int  headline();
122          int  i;
123          
124          progname = argv[0];
125 +        if ((gv = getenv("GAMMA")) != NULL)
126 +                gamcor = atof(gv);
127  
128          for (i = 1; i < argc; i++)
129                  if (argv[i][0] == '-')
# Line 129 | Line 138 | char  *argv[];
138                                  maxcolors = 2;
139                                  break;
140                          case 'd':
141 <                                dither = !dither;
141 >                                if (argv[i][2] == 'i')
142 >                                        dispname = argv[++i];
143 >                                else
144 >                                        dither = !dither;
145                                  break;
146                          case 'f':
147                                  fast = !fast;
# Line 140 | Line 152 | char  *argv[];
152                                  scale = atoi(argv[++i]);
153                                  break;
154                          case 'g':
155 <                                if (!strcmp(argv[i], "-geometry"))
155 >                                if (argv[i][2] == 'e')
156                                          geometry = argv[++i];
157                                  else
158                                          gamcor = atof(argv[++i]);
# Line 157 | Line 169 | char  *argv[];
169                  fname = argv[i];
170                  fin = fopen(fname, "r");
171                  if (fin == NULL) {
172 <                        sprintf(errmsg, "can't open file \"%s\"", fname);
172 >                        sprintf(errmsg, "cannot open file \"%s\"", fname);
173                          quiterr(errmsg);
174                  }
175          } else if (i != argc)
# Line 165 | Line 177 | char  *argv[];
177                                  /* get header */
178          getheader(fin, headline, NULL);
179                                  /* get picture dimensions */
180 <        if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
180 >        if (wrongformat || !fgetsresolu(&inpres, fin))
181                  quiterr("bad picture format");
182 +        xmax = scanlen(&inpres);
183 +        ymax = numscans(&inpres);
184                                  /* set view parameters */
185          if (gotview && setview(&ourview) != NULL)
186                  gotview = 0;
# Line 179 | Line 193 | char  *argv[];
193                  getevent();             /* main loop */
194   userr:
195          fprintf(stderr,
196 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
196 >        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
197                          progname);
198          quit(1);
199   }
# Line 188 | Line 202 | userr:
202   headline(s)             /* get relevant info from header */
203   char  *s;
204   {
191        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
192        register char  **an;
205          char  fmt[32];
206  
207          if (isexpos(s))
# Line 197 | Line 209 | char  *s;
209          else if (isformat(s)) {
210                  formatval(fmt, s);
211                  wrongformat = strcmp(fmt, COLRFMT);
212 <        } else
213 <                for (an = altname; *an != NULL; an++)
202 <                        if (!strncmp(*an, s, strlen(*an))) {
203 <                                if (sscanview(&ourview, s+strlen(*an)) > 0)
204 <                                        gotview++;
205 <                                return;
206 <                        }
212 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
213 >                gotview++;
214   }
215  
216  
# Line 211 | Line 218 | init()                 /* get data and open window */
218   {
219          XWMHints        ourxwmhints;
220          XSetWindowAttributes    ourwinattr;
221 <        XSizeHints  oursizhints;
222 <        register int  i;
221 >        XSizeHints      oursizhints;
222 >        register int    i;
223          
224          if (fname != NULL) {
225                  scanpos = (long *)malloc(ymax*sizeof(long));
# Line 221 | Line 228 | init()                 /* get data and open window */
228                  for (i = 0; i < ymax; i++)
229                          scanpos[i] = -1;
230          }
231 <        if ((thedisplay = XOpenDisplay(NULL)) == NULL)
232 <                quiterr("can't open display; DISPLAY variable set?");
233 <        if (maxcolors == 0) {           /* get number of available colors */
234 <                i = DisplayPlanes(thedisplay,ourscreen);
228 <                maxcolors = i > 8 ? 256 : 1<<i;
229 <                if (maxcolors > 4) maxcolors -= 2;
230 <        }
231 >        if ((thedisplay = XOpenDisplay(dispname)) == NULL)
232 >                quiterr("cannot open display");
233 >                                /* get best visual for default screen */
234 >        getbestvis();
235                                  /* store image */
236          getras();
237 <                                /* open window */
238 <        ourwinattr.border_pixel = ourblack;
239 <        ourwinattr.background_pixel = ourwhite;
236 <        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
237 <                        0, InputOutput, ourras->visual,
238 <                        CWBackPixel|CWBorderPixel, &ourwinattr);
239 <        if (wind == 0)
240 <                quiterr("can't create window");
241 <        width = xmax;
242 <        height = ymax;
243 <        fontid = XLoadFont(thedisplay, FONTNAME);
244 <        if (fontid == 0)
245 <                quiterr("can't get font");
246 <        XSetFont(thedisplay, ourgc, fontid);
247 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
248 <        XSetFunction(thedisplay, revgc, GXinvert);
249 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
250 <                        XC_diamond_cross));
237 >                                /* get size and position */
238 >        bzero((char *)&oursizhints, sizeof(oursizhints));
239 >        oursizhints.width = xmax; oursizhints.height = ymax;
240          if (geometry != NULL) {
252                bzero((char *)&oursizhints, sizeof(oursizhints));
241                  i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
242                                  (unsigned *)&oursizhints.width,
243                                  (unsigned *)&oursizhints.height);
244                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
245                          oursizhints.flags |= USSize;
246 <                else {
259 <                        oursizhints.width = xmax;
260 <                        oursizhints.height = ymax;
246 >                else
247                          oursizhints.flags |= PSize;
262                }
248                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
249                          oursizhints.flags |= USPosition;
250                          if (i & XNegative)
# Line 269 | Line 254 | init()                 /* get data and open window */
254                                  oursizhints.y += DisplayHeight(thedisplay,
255                                  ourscreen)-1-oursizhints.height-2*BORWIDTH;
256                  }
272                XSetNormalHints(thedisplay, wind, &oursizhints);
257          }
258 +                                /* open window */
259 +        ourwinattr.border_pixel = ourblack;
260 +        ourwinattr.background_pixel = ourwhite;
261 +        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
262 +                        ourvis.visual, AllocNone);
263 +        wind = XCreateWindow(thedisplay, ourroot, oursizhints.x, oursizhints.y,
264 +                        oursizhints.width, oursizhints.height, BORWIDTH,
265 +                        ourvis.depth, InputOutput, ourvis.visual,
266 +                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
267 +        if (wind == 0)
268 +                quiterr("cannot create window");
269 +        XFreeColormap(thedisplay, ourwinattr.colormap);
270 +        width = xmax;
271 +        height = ymax;
272 +        ourgc = XCreateGC(thedisplay, wind, 0, 0);
273 +        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
274 +        revgc = XCreateGC(thedisplay, wind, 0, 0);
275 +        XSetFunction(thedisplay, revgc, GXinvert);
276 +        fontid = XLoadFont(thedisplay, FONTNAME);
277 +        if (fontid == 0)
278 +                quiterr("cannot get font");
279 +        XSetFont(thedisplay, ourgc, fontid);
280 +        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
281 +                        XC_diamond_cross));
282 +        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
283 +        if (oursizhints.flags)
284 +                XSetNormalHints(thedisplay, wind, &oursizhints);
285          ourxwmhints.flags = InputHint|IconPixmapHint;
286          ourxwmhints.input = True;
287          ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
# Line 280 | Line 291 | init()                 /* get data and open window */
291                          |ButtonMotionMask|StructureNotifyMask
292                          |KeyPressMask|ExposureMask);
293          XMapWindow(thedisplay, wind);
283                                /* store name last as ready signal */
284        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
294          return;
295   memerr:
296          quiterr("out of memory");
# Line 299 | Line 308 | char  *err;
308   }
309  
310  
311 < eputs(s)
312 < char    *s;
311 > static int
312 > viscmp(v1,v2)           /* compare visual to see which is better, descending */
313 > register XVisualInfo    *v1, *v2;
314   {
315 <        fputs(s, stderr);
315 >        int     bad1 = 0, bad2 = 0;
316 >        register int  *rp;
317 >
318 >        if (v1->class == v2->class) {
319 >                if (v1->class == TrueColor || v1->class == DirectColor) {
320 >                                        /* prefer 24-bit to 32-bit */
321 >                        if (v1->depth == 24 && v2->depth == 32)
322 >                                return(-1);
323 >                        if (v1->depth == 32 && v2->depth == 24)
324 >                                return(1);
325 >                        return(0);
326 >                }
327 >                                        /* don't be too greedy */
328 >                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
329 >                        return(v1->depth - v2->depth);
330 >                return(v2->depth - v1->depth);
331 >        }
332 >                                        /* prefer Pseudo when < 24-bit */
333 >        if ((v1->class == TrueColor || v1->class == DirectColor) &&
334 >                        v1->depth < 24)
335 >                bad1 = 1;
336 >        if ((v2->class == TrueColor || v2->class == DirectColor) &&
337 >                        v2->depth < 24)
338 >                bad2 = -1;
339 >        if (bad1 | bad2)
340 >                return(bad1+bad2);
341 >                                        /* otherwise, use class ranking */
342 >        for (rp = ourrank; *rp != -1; rp++) {
343 >                if (v1->class == *rp)
344 >                        return(-1);
345 >                if (v2->class == *rp)
346 >                        return(1);
347 >        }
348 >        return(0);
349   }
350  
351  
352 < quit(code)
310 < int  code;
352 > getbestvis()                    /* get the best visual for this screen */
353   {
354 <        exit(code);
354 > #ifdef DEBUG
355 > static char  vistype[][12] = {
356 >                "StaticGray",
357 >                "GrayScale",
358 >                "StaticColor",
359 >                "PseudoColor",
360 >                "TrueColor",
361 >                "DirectColor"
362 > };
363 > #endif
364 >        static int      rankings[3][6] = {
365 >                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
366 >                {PseudoColor,GrayScale,StaticGray,-1},
367 >                {PseudoColor,GrayScale,StaticGray,-1}
368 >        };
369 >        XVisualInfo     *xvi;
370 >        int     vismatched;
371 >        register int    i, j;
372 >
373 >        if (greyscale) {
374 >                ourrank = rankings[2];
375 >                if (maxcolors < 2) maxcolors = 256;
376 >        } else if (maxcolors >= 2 && maxcolors <= 256)
377 >                ourrank = rankings[1];
378 >        else {
379 >                ourrank = rankings[0];
380 >                maxcolors = 256;
381 >        }
382 >                                        /* find best visual */
383 >        ourvis.screen = ourscreen;
384 >        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
385 >        if (xvi == NULL)
386 >                quiterr("no visuals for this screen!");
387 > #ifdef DEBUG
388 >        fprintf(stderr, "Supported visuals:\n");
389 >        for (i = 0; i < vismatched; i++)
390 >                fprintf(stderr, "\ttype %s, depth %d\n",
391 >                                vistype[xvi[i].class], xvi[i].depth);
392 > #endif
393 >        for (i = 0, j = 1; j < vismatched; j++)
394 >                if (viscmp(&xvi[i],&xvi[j]) > 0)
395 >                        i = j;
396 >                                        /* compare to least acceptable */
397 >        for (j = 0; ourrank[j++] != -1; )
398 >                ;
399 >        ourvis.class = ourrank[--j];
400 >        ourvis.depth = 1;
401 >        if (viscmp(&xvi[i],&ourvis) > 0)
402 >                quiterr("inadequate visuals on this screen");
403 >                                        /* OK, we'll use it */
404 >        copystruct(&ourvis, &xvi[i]);
405 > #ifdef DEBUG
406 >        fprintf(stderr, "Selected visual type %s, depth %d\n",
407 >                        vistype[ourvis.class], ourvis.depth);
408 > #endif
409 >                                        /* make appropriate adjustments */
410 >        if (ourvis.class == GrayScale || ourvis.class == StaticGray)
411 >                greyscale = 1;
412 >        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
413 >                maxcolors = ourvis.colormap_size;
414 >        if (ourvis.class == StaticGray) {
415 >                ourblack = 0;
416 >                ourwhite = 255;
417 >        } else if (ourvis.class == PseudoColor) {
418 >                ourblack = BlackPixel(thedisplay,ourscreen);
419 >                ourwhite = WhitePixel(thedisplay,ourscreen);
420 >                if ((ourblack|ourwhite) & ~255L) {
421 >                        ourblack = 0;
422 >                        ourwhite = 1;
423 >                }
424 >                if (maxcolors > 4)
425 >                        maxcolors -= 2;
426 >        } else {
427 >                ourblack = 0;
428 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
429 >        }
430 >        XFree((char *)xvi);
431   }
432  
433  
434   getras()                                /* get raster file */
435   {
318        colormap        ourmap;
436          XVisualInfo     vinfo;
437  
438          if (maxcolors <= 2) {           /* monochrome */
439                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
440                  if (ourdata == NULL)
441                          goto fail;
442 <                ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
442 >                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
443                                  xmax, ymax, 8);
444                  if (ourras == NULL)
445                          goto fail;
446                  getmono();
447 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)
448 <                                                /* kludge for DirectColor */
332 <        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) {
333 <                ourdata = (unsigned char *)malloc(xmax*ymax*3);
447 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
448 >                ourdata = (unsigned char *)malloc(4*xmax*ymax);
449                  if (ourdata == NULL)
450                          goto fail;
451 <                ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
452 <                                xmax, ymax, 8);
451 >                ourras = make_raster(thedisplay, &ourvis, 32,
452 >                                ourdata, xmax, ymax, 32);
453                  if (ourras == NULL)
454                          goto fail;
455                  getfull();
# Line 342 | Line 457 | getras()                               /* get raster file */
457                  ourdata = (unsigned char *)malloc(xmax*ymax);
458                  if (ourdata == NULL)
459                          goto fail;
460 <                ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
460 >                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
461                                  xmax, ymax, 8);
462                  if (ourras == NULL)
463                          goto fail;
464 <                if (greyscale)
465 <                        biq(dither,maxcolors,1,ourmap);
464 >                if (greyscale | ourvis.class == StaticGray)
465 >                        getgrey();
466                  else
467 <                        ciq(dither,maxcolors,1,ourmap);
468 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
467 >                        getmapped();
468 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
469                          goto fail;
470          }
471          return;
# Line 381 | Line 496 | getevent()                             /* process the next event */
496          case MapNotify:
497                  map_rcolors(ourras, wind);
498                  if (fast)
499 <                        make_rpixmap(ourras);
499 >                        make_rpixmap(ourras, wind);
500                  break;
501          case UnmapNotify:
502 <                unmap_rcolors(ourras);
502 >                if (!fast)
503 >                        unmap_rcolors(ourras);
504                  break;
505          case Expose:
506                  redraw(e.e.x, e.e.y, e.e.width, e.e.height);
# Line 407 | Line 523 | XKeyPressedEvent  *ekey;
523          XColor  cvx;
524          int  com, n;
525          double  comp;
526 +        FLOAT  hv[2];
527          FVECT  rorg, rdir;
528  
529          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
# Line 415 | Line 532 | XKeyPressedEvent  *ekey;
532          com = buf[0];
533          switch (com) {                  /* interpret command */
534          case 'q':
535 <        case CTRL(D):                           /* quit */
535 >        case CTRL('D'):                         /* quit */
536                  quit(0);
537          case '\n':
538          case '\r':
# Line 455 | Line 572 | XKeyPressedEvent  *ekey;
572                  XStoreColor(thedisplay, ourras->cmap, &cvx);
573                  return(0);
574          case 'p':                               /* position */
575 <                sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
575 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
576 >                sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
577 >                                (int)(hv[1]*inpres.yr));
578                  XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
579                                          buf, strlen(buf));
580                  return(0);
# Line 464 | Line 583 | XKeyPressedEvent  *ekey;
583                          XBell(thedisplay, 0);
584                          return(-1);
585                  }
586 <                if (viewray(rorg, rdir, &ourview,
587 <                                (ekey->x-xoff+.5)/xmax,
469 <                                (ymax-1-ekey->y+yoff+.5)/ymax) < 0)
586 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
587 >                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
588                          return(-1);
589                  printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
590                  printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
# Line 488 | Line 606 | XKeyPressedEvent  *ekey;
606                  free_raster(ourras);
607                  getras();
608          /* fall through */
609 <        case CTRL(R):                           /* redraw */
610 <        case CTRL(L):
609 >        case CTRL('R'):                         /* redraw */
610 >        case CTRL('L'):
611                  unmap_rcolors(ourras);
612                  XClearWindow(thedisplay, wind);
613                  map_rcolors(ourras, wind);
614                  if (fast)
615 <                        make_rpixmap(ourras);
615 >                        make_rpixmap(ourras, wind);
616                  redraw(0, 0, width, height);
617                  return(0);
618 +        case '0':                               /* recenter origin */
619 +                if (xoff == 0 & yoff == 0)
620 +                        return(0);
621 +                xoff = yoff = 0;
622 +                XClearWindow(thedisplay, wind);
623 +                redraw(0, 0, width, height);
624 +                return(0);
625          case ' ':                               /* clear */
626                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
627                  return(0);
# Line 581 | Line 706 | int  x0, y0, x1, y1;
706   avgbox(clr)                             /* average color over current box */
707   COLOR  clr;
708   {
709 +        static COLOR  lc;
710 +        static int  ll, lr, lt, lb;
711          int  left, right, top, bottom;
712          int  y;
713          double  d;
# Line 604 | Line 731 | COLOR  clr;
731                  bottom = ymax;
732          if (top >= bottom)
733                  return(-1);
734 +        if (left == ll && right == lr && top == lt && bottom == lb) {
735 +                copycolor(clr, lc);
736 +                return(0);
737 +        }
738          for (y = top; y < bottom; y++) {
739                  if (getscan(y) == -1)
740                          return(-1);
# Line 614 | Line 745 | COLOR  clr;
745          }
746          d = 1.0/((right-left)*(bottom-top));
747          scalecolor(clr, d);
748 +        ll = left; lr = right; lt = top; lb = bottom;
749 +        copycolor(lc, clr);
750          return(0);
751   }
752  
# Line 622 | Line 755 | getmono()                      /* get monochrome data */
755   {
756          register unsigned char  *dp;
757          register int    x, err;
758 <        int     y;
758 >        int     y, errp;
759          short   *cerr;
760  
761          if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
762                  quiterr("out of memory in getmono");
763          dp = ourdata - 1;
764          for (y = 0; y < ymax; y++) {
765 <                if (getscan(y) < 0)
633 <                        quiterr("seek error in getmono");
634 <                normcolrs(scanline, xmax, scale);
765 >                getscan(y);
766                  add2icon(y, scanline);
767 +                normcolrs(scanline, xmax, scale);
768                  err = 0;
769                  for (x = 0; x < xmax; x++) {
770                          if (!(x&7))
771                                  *++dp = 0;
772 +                        errp = err;
773                          err += normbright(scanline[x]) + cerr[x];
774                          if (err > 127)
775                                  err -= 255;
776                          else
777                                  *dp |= 1<<(7-(x&07));
778 <                        cerr[x] = err >>= 1;
778 >                        err /= 3;
779 >                        cerr[x] = err + errp;
780                  }
781          }
782          free((char *)cerr);
# Line 656 | Line 790 | COLR  *scan;
790          static short  cerr[ICONSIZ];
791          static int  ynext;
792          static char  *dp;
793 +        COLR  clr;
794          register int  err;
795          register int    x, ti;
796 +        int  errp;
797  
798          if (iconheight == 0) {          /* initialize */
799                  if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
# Line 666 | Line 802 | COLR  *scan;
802                  } else if (xmax > ymax) {
803                          iconwidth = ICONSIZ;
804                          iconheight = ICONSIZ*ymax/xmax;
805 +                        if (iconheight < 1)
806 +                                iconheight = 1;
807                  } else {
808                          iconwidth = ICONSIZ*xmax/ymax;
809 +                        if (iconwidth < 1)
810 +                                iconwidth = 1;
811                          iconheight = ICONSIZ;
812                  }
813                  ynext = 0;
# Line 679 | Line 819 | COLR  *scan;
819          for (x = 0; x < iconwidth; x++) {
820                  if (!(x&7))
821                          *++dp = 0;
822 +                errp = err;
823                  ti = x*xmax/iconwidth;
824 <                err += normbright(scan[ti]) + cerr[x];
824 >                copycolr(clr, scan[ti]);
825 >                normcolrs(clr, 1, scale);
826 >                err += normbright(clr) + cerr[x];
827                  if (err > 127)
828                          err -= 255;
829                  else
830                          *dp |= 1<<(x&07);
831 <                cerr[x] = err >>= 1;
831 >                err /= 3;
832 >                cerr[x] = err + errp;
833          }
834          ynext++;
835   }
# Line 694 | Line 838 | COLR  *scan;
838   getfull()                       /* get full (24-bit) data */
839   {
840          int     y;
841 +        register unsigned long  *dp;
842 +        register int    x;
843 +                                        /* set gamma correction */
844 +        setcolrgam(gamcor);
845 +                                        /* read and convert file */
846 +        dp = (unsigned long *)ourdata;
847 +        for (y = 0; y < ymax; y++) {
848 +                getscan(y);
849 +                add2icon(y, scanline);
850 +                if (scale)
851 +                        shiftcolrs(scanline, xmax, scale);
852 +                colrs_gambs(scanline, xmax);
853 +                if (ourras->image->blue_mask & 1)
854 +                        for (x = 0; x < xmax; x++)
855 +                                *dp++ = scanline[x][RED] << 16 |
856 +                                        scanline[x][GRN] << 8 |
857 +                                        scanline[x][BLU] ;
858 +                else
859 +                        for (x = 0; x < xmax; x++)
860 +                                *dp++ = scanline[x][RED] |
861 +                                        scanline[x][GRN] << 8 |
862 +                                        scanline[x][BLU] << 16 ;
863 +        }
864 + }
865 +
866 +
867 + getgrey()                       /* get greyscale data */
868 + {
869 +        int     y;
870          register unsigned char  *dp;
871          register int    x;
872                                          /* set gamma correction */
# Line 701 | Line 874 | getfull()                      /* get full (24-bit) data */
874                                          /* read and convert file */
875          dp = ourdata;
876          for (y = 0; y < ymax; y++) {
877 <                if (getscan(y) < 0)
878 <                        quiterr("seek error in getfull");
877 >                getscan(y);
878 >                add2icon(y, scanline);
879                  if (scale)
880                          shiftcolrs(scanline, xmax, scale);
881 +                for (x = 0; x < xmax; x++)
882 +                        scanline[x][GRN] = normbright(scanline[x]);
883                  colrs_gambs(scanline, xmax);
884 +                if (maxcolors < 256)
885 +                        for (x = 0; x < xmax; x++)
886 +                                *dp++ = ((long)scanline[x][GRN] *
887 +                                        maxcolors + maxcolors/2) >> 8;
888 +                else
889 +                        for (x = 0; x < xmax; x++)
890 +                                *dp++ = scanline[x][GRN];
891 +        }
892 +        for (x = 0; x < maxcolors; x++)
893 +                clrtab[x][RED] = clrtab[x][GRN] =
894 +                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
895 + }
896 +
897 +
898 + getmapped()                     /* get color-mapped data */
899 + {
900 +        int     y;
901 +                                        /* set gamma correction */
902 +        setcolrgam(gamcor);
903 +                                        /* make histogram */
904 +        new_histo();
905 +        for (y = 0; y < ymax; y++) {
906 +                if (getscan(y) < 0)
907 +                        quiterr("seek error in getmapped");
908                  add2icon(y, scanline);
909 <                for (x = 0; x < xmax; x++) {
910 <                        *dp++ = scanline[x][RED];
911 <                        *dp++ = scanline[x][GRN];
912 <                        *dp++ = scanline[x][BLU];
714 <                }
909 >                if (scale)
910 >                        shiftcolrs(scanline, xmax, scale);
911 >                colrs_gambs(scanline, xmax);
912 >                cnt_colrs(scanline, xmax);
913          }
914 +                                        /* map pixels */
915 +        if (!new_clrtab(maxcolors))
916 +                quiterr("cannot create color map");
917 +        for (y = 0; y < ymax; y++) {
918 +                if (getscan(y) < 0)
919 +                        quiterr("seek error in getmapped");
920 +                if (scale)
921 +                        shiftcolrs(scanline, xmax, scale);
922 +                colrs_gambs(scanline, xmax);
923 +                if (dither)
924 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
925 +                else
926 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
927 +        }
928   }
929  
930  
# Line 753 | Line 965 | int  y;
965                  if (fseek(fin, scanpos[y], 0) == -1)
966                          quiterr("fseek error");
967                  cury = y;
968 <        } else if (scanpos != NULL)
968 >        } else if (scanpos != NULL && scanpos[y] == -1)
969                  scanpos[y] = ftell(fin);
970  
971          if (freadcolrs(scanline, xmax, fin) < 0)
# Line 761 | Line 973 | int  y;
973  
974          cury++;
975          return(0);
764 }
765
766
767 picreadline3(y, l3)                     /* read in 3-byte scanline */
768 int  y;
769 register rgbpixel  *l3;
770 {
771        register int    i;
772                                                        /* read scanline */
773        if (getscan(y) < 0)
774                quiterr("cannot seek for picreadline");
775                                                        /* convert scanline */
776        normcolrs(scanline, xmax, scale);
777        add2icon(y, scanline);
778        for (i = 0; i < xmax; i++) {
779                l3[i].r = scanline[i][RED];
780                l3[i].g = scanline[i][GRN];
781                l3[i].b = scanline[i][BLU];
782        }
783 }
784
785
786 picwriteline(y, l)              /* add 8-bit scanline to image */
787 int  y;
788 pixel  *l;
789 {
790        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
791 }
792
793
794 picreadcm(map)                  /* do gamma correction */
795 colormap  map;
796 {
797        extern double  pow();
798        register int  i, val;
799
800        for (i = 0; i < 256; i++) {
801                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
802                map[0][i] = map[1][i] = map[2][i] = val;
803        }
804 }
805
806
807 picwritecm(map)                 /* handled elsewhere */
808 colormap  map;
809 {
810 #ifdef DEBUG
811        register int i;
812
813        for (i = 0; i < 256; i++)
814                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
815 #endif
976   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines