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.7 by greg, Thu Aug 30 11:21:34 1990 UTC vs.
Revision 2.9 by greg, Sat May 30 09:33:01 1992 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 30 | Line 30 | static char SCCSid[] = "$SunId$ LBL";
30   #include  "pic.h"
31   #include  "x11raster.h"
32   #include  "random.h"
33 + #include  "resolu.h"
34  
35   #define  FONTNAME       "8x13"          /* text font we'll use */
36  
37 < #define  CTRL(c)        ('c'-'@')
37 > #define  CTRL(c)        ((c)-'@')
38  
39   #define  BORWIDTH       5               /* border width */
40  
41 + #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
42 +
43   #define  ourscreen      DefaultScreen(thedisplay)
41 #define  ourblack       BlackPixel(thedisplay,ourscreen)
42 #define  ourwhite       WhitePixel(thedisplay,ourscreen)
44   #define  ourroot        RootWindow(thedisplay,ourscreen)
44 #define  ourgc          DefaultGC(thedisplay,ourscreen)
45  
46   #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1)
47  
# Line 52 | Line 52 | double  gamcor = 2.2;                  /* gamma correction */
52   int  dither = 1;                        /* dither colors? */
53   int  fast = 0;                          /* keep picture in Pixmap? */
54  
55 + char    *dispname = NULL;               /* our display name */
56 +
57   Window  wind = 0;                       /* our output window */
58 + unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
59   Font  fontid;                           /* our font */
60  
61   int  maxcolors = 0;                     /* maximum colors */
# Line 68 | Line 71 | int  gotview = 0;                      /* got parameters from file */
71  
72   COLR  *scanline;                        /* scan line buffer */
73  
74 < int  xmax, ymax;                        /* picture resolution */
74 > RESOLU  inpres;                         /* input resolution and ordering */
75 > int  xmax, ymax;                        /* picture dimensions */
76   int  width, height;                     /* window size */
77   char  *fname = NULL;                    /* input file name */
78   FILE  *fin = stdin;                     /* input file */
# Line 77 | Line 81 | int  cury = 0;                         /* current scan location */
81  
82   double  exposure = 1.0;                 /* exposure compensation used */
83  
84 + int  wrongformat = 0;                   /* input in another format? */
85 +
86 + GC      ourgc;                          /* standard graphics context */
87   GC      revgc;                          /* graphics context with GXinvert */
88  
89 < XRASTER *ourras;                        /* our stored image */
89 > int             *ourrank;               /* our visual class ranking */
90 > XVisualInfo     ourvis;                 /* our visual */
91 > XRASTER         *ourras;                /* our stored image */
92   unsigned char   *ourdata;               /* our image data */
93  
94   struct {
# Line 88 | Line 97 | struct {
97  
98   char  *geometry = NULL;                 /* geometry specification */
99  
100 + char  icondata[ICONSIZ*ICONSIZ/8];      /* icon bitmap data */
101 + int  iconwidth = 0, iconheight = 0;
102 +
103   char  *progname;
104  
105   char  errmsg[128];
# Line 96 | Line 108 | extern long  ftell();
108  
109   extern char  *malloc(), *calloc();
110  
111 < extern double  atof(), pow(), log();
111 > extern double  pow(), log();
112  
113   Display  *thedisplay;
114  
# Line 122 | Line 134 | char  *argv[];
134                                  maxcolors = 2;
135                                  break;
136                          case 'd':
137 <                                dither = !dither;
137 >                                if (argv[i][2] == 'i')
138 >                                        dispname = argv[++i];
139 >                                else
140 >                                        dither = !dither;
141                                  break;
142                          case 'f':
143                                  fast = !fast;
# Line 133 | Line 148 | char  *argv[];
148                                  scale = atoi(argv[++i]);
149                                  break;
150                          case 'g':
151 <                                if (!strcmp(argv[i], "-geometry"))
151 >                                if (argv[i][2] == 'e')
152                                          geometry = argv[++i];
153                                  else
154                                          gamcor = atof(argv[++i]);
# Line 150 | Line 165 | char  *argv[];
165                  fname = argv[i];
166                  fin = fopen(fname, "r");
167                  if (fin == NULL) {
168 <                        sprintf(errmsg, "can't open file \"%s\"", fname);
168 >                        sprintf(errmsg, "cannot open file \"%s\"", fname);
169                          quiterr(errmsg);
170                  }
171          } else if (i != argc)
172                  goto userr;
173                                  /* get header */
174 <        getheader(fin, headline);
174 >        getheader(fin, headline, NULL);
175                                  /* get picture dimensions */
176 <        if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
177 <                quiterr("bad picture size");
176 >        if (wrongformat || !fgetsresolu(&inpres, fin))
177 >                quiterr("bad picture format");
178 >        xmax = scanlen(&inpres);
179 >        ymax = numscans(&inpres);
180                                  /* set view parameters */
181          if (gotview && setview(&ourview) != NULL)
182                  gotview = 0;
# Line 172 | Line 189 | char  *argv[];
189                  getevent();             /* main loop */
190   userr:
191          fprintf(stderr,
192 <        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
192 >        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
193                          progname);
194          quit(1);
195   }
# Line 181 | Line 198 | userr:
198   headline(s)             /* get relevant info from header */
199   char  *s;
200   {
201 <        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
185 <        register char  **an;
201 >        char  fmt[32];
202  
203          if (isexpos(s))
204                  exposure *= exposval(s);
205 <        else
206 <                for (an = altname; *an != NULL; an++)
207 <                        if (!strncmp(*an, s, strlen(*an))) {
208 <                                if (sscanview(&ourview, s+strlen(*an)) > 0)
209 <                                        gotview++;
194 <                                return;
195 <                        }
205 >        else if (isformat(s)) {
206 >                formatval(fmt, s);
207 >                wrongformat = strcmp(fmt, COLRFMT);
208 >        } else if (isview(s) && sscanview(&ourview, s) > 0)
209 >                gotview++;
210   }
211  
212  
213   init()                  /* get data and open window */
214   {
215 +        XWMHints        ourxwmhints;
216          XSetWindowAttributes    ourwinattr;
217 <        XSizeHints  oursizhints;
218 <        register int  i;
217 >        XSizeHints      oursizhints;
218 >        register int    i;
219          
220          if (fname != NULL) {
221                  scanpos = (long *)malloc(ymax*sizeof(long));
# Line 209 | Line 224 | init()                 /* get data and open window */
224                  for (i = 0; i < ymax; i++)
225                          scanpos[i] = -1;
226          }
227 <        if ((thedisplay = XOpenDisplay(NULL)) == NULL)
228 <                quiterr("can't open display; DISPLAY variable set?");
229 <        if (maxcolors == 0) {           /* get number of available colors */
230 <                i = DisplayPlanes(thedisplay,ourscreen);
216 <                maxcolors = i > 8 ? 256 : 1<<i;
217 <                if (maxcolors > 4) maxcolors -= 2;
218 <        }
227 >        if ((thedisplay = XOpenDisplay(dispname)) == NULL)
228 >                quiterr("cannot open display");
229 >                                /* get best visual for default screen */
230 >        getbestvis();
231                                  /* store image */
232          getras();
233                                  /* open window */
234          ourwinattr.border_pixel = ourblack;
235          ourwinattr.background_pixel = ourwhite;
236 +        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
237 +                        ourvis.visual, AllocNone);
238          wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
239 <                        0, InputOutput, ourras->visual,
240 <                        CWBackPixel|CWBorderPixel, &ourwinattr);
239 >                        ourvis.depth, InputOutput, ourvis.visual,
240 >                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
241          if (wind == 0)
242 <                quiterr("can't create window");
242 >                quiterr("cannot create window");
243 >        XFreeColormap(thedisplay, ourwinattr.colormap);
244          width = xmax;
245          height = ymax;
246 +        ourgc = XCreateGC(thedisplay, wind, 0, 0);
247 +        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
248 +        revgc = XCreateGC(thedisplay, wind, 0, 0);
249 +        XSetFunction(thedisplay, revgc, GXinvert);
250          fontid = XLoadFont(thedisplay, FONTNAME);
251          if (fontid == 0)
252 <                quiterr("can't get font");
252 >                quiterr("cannot get font");
253          XSetFont(thedisplay, ourgc, fontid);
235        revgc = XCreateGC(thedisplay, wind, 0, 0);
236        XSetFunction(thedisplay, revgc, GXinvert);
237        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
254          XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
255                          XC_diamond_cross));
256 +        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
257          if (geometry != NULL) {
258                  bzero((char *)&oursizhints, sizeof(oursizhints));
259                  i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
260 <                                &oursizhints.width, &oursizhints.height);
260 >                                (unsigned *)&oursizhints.width,
261 >                                (unsigned *)&oursizhints.height);
262                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
263                          oursizhints.flags |= USSize;
264                  else {
# Line 259 | Line 277 | init()                 /* get data and open window */
277                  }
278                  XSetNormalHints(thedisplay, wind, &oursizhints);
279          }
280 +        ourxwmhints.flags = InputHint|IconPixmapHint;
281 +        ourxwmhints.input = True;
282 +        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
283 +                        wind, icondata, iconwidth, iconheight);
284 +        XSetWMHints(thedisplay, wind, &ourxwmhints);
285          XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
286                          |ButtonMotionMask|StructureNotifyMask
287                          |KeyPressMask|ExposureMask);
# Line 294 | Line 317 | int  code;
317   }
318  
319  
320 + static int
321 + viscmp(v1,v2)           /* compare visual to see which is better, descending */
322 + register XVisualInfo    *v1, *v2;
323 + {
324 +        int     bad1 = 0, bad2 = 0;
325 +        register int  *rp;
326 +
327 +        if (v1->class == v2->class) {
328 +                if (v1->class == TrueColor || v1->class == DirectColor) {
329 +                                        /* prefer 24-bit to 32-bit */
330 +                        if (v1->depth == 24 && v2->depth == 32)
331 +                                return(-1);
332 +                        if (v1->depth == 32 && v2->depth == 24)
333 +                                return(1);
334 +                        return(0);
335 +                }
336 +                                        /* don't be too greedy */
337 +                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
338 +                        return(v1->depth - v2->depth);
339 +                return(v2->depth - v1->depth);
340 +        }
341 +                                        /* prefer Pseudo when < 24-bit */
342 +        if ((v1->class == TrueColor || v1->class == DirectColor) &&
343 +                        v1->depth < 24)
344 +                bad1 = 1;
345 +        if ((v2->class == TrueColor || v2->class == DirectColor) &&
346 +                        v2->depth < 24)
347 +                bad2 = -1;
348 +        if (bad1 | bad2)
349 +                return(bad1+bad2);
350 +                                        /* otherwise, use class ranking */
351 +        for (rp = ourrank; *rp != -1; rp++) {
352 +                if (v1->class == *rp)
353 +                        return(-1);
354 +                if (v2->class == *rp)
355 +                        return(1);
356 +        }
357 +        return(0);
358 + }
359 +
360 +
361 + getbestvis()                    /* get the best visual for this screen */
362 + {
363 + #ifdef DEBUG
364 + static char  vistype[][12] = {
365 +                "StaticGray",
366 +                "GrayScale",
367 +                "StaticColor",
368 +                "PseudoColor",
369 +                "TrueColor",
370 +                "DirectColor"
371 + };
372 + #endif
373 +        static int      rankings[3][6] = {
374 +                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1},
375 +                {PseudoColor,GrayScale,StaticGray,-1},
376 +                {PseudoColor,GrayScale,StaticGray,-1}
377 +        };
378 +        XVisualInfo     *xvi;
379 +        int     vismatched;
380 +        register int    i, j;
381 +
382 +        if (greyscale) {
383 +                ourrank = rankings[2];
384 +                if (maxcolors < 2) maxcolors = 256;
385 +        } else if (maxcolors >= 2 && maxcolors <= 256)
386 +                ourrank = rankings[1];
387 +        else {
388 +                ourrank = rankings[0];
389 +                maxcolors = 256;
390 +        }
391 +                                        /* find best visual */
392 +        ourvis.screen = ourscreen;
393 +        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched);
394 +        if (xvi == NULL)
395 +                quiterr("no visuals for this screen!");
396 + #ifdef DEBUG
397 +        fprintf(stderr, "Supported visuals:\n");
398 +        for (i = 0; i < vismatched; i++)
399 +                fprintf(stderr, "\ttype %s, depth %d\n",
400 +                                vistype[xvi[i].class], xvi[i].depth);
401 + #endif
402 +        for (i = 0, j = 1; j < vismatched; j++)
403 +                if (viscmp(&xvi[i],&xvi[j]) > 0)
404 +                        i = j;
405 +                                        /* compare to least acceptable */
406 +        for (j = 0; ourrank[j++] != -1; )
407 +                ;
408 +        ourvis.class = ourrank[--j];
409 +        ourvis.depth = 1;
410 +        if (viscmp(&xvi[i],&ourvis) > 0)
411 +                quiterr("inadequate visuals on this screen");
412 +                                        /* OK, we'll use it */
413 +        copystruct(&ourvis, &xvi[i]);
414 + #ifdef DEBUG
415 +        fprintf(stderr, "Selected visual type %s, depth %d\n",
416 +                        vistype[ourvis.class], ourvis.depth);
417 + #endif
418 +                                        /* make appropriate adjustments */
419 +        if (ourvis.class == GrayScale || ourvis.class == StaticGray)
420 +                greyscale = 1;
421 +        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
422 +                maxcolors = ourvis.colormap_size;
423 +        if (maxcolors > 4)
424 +                maxcolors -= 2;
425 +        if (ourvis.class == StaticGray) {
426 +                ourblack = 0;
427 +                ourwhite = 255;
428 +        } else if (ourvis.class == PseudoColor) {
429 +                ourblack = BlackPixel(thedisplay,ourscreen);
430 +                ourwhite = WhitePixel(thedisplay,ourscreen);
431 +                if ((ourblack|ourwhite) & ~255L) {
432 +                        ourblack = 0;
433 +                        ourwhite = 1;
434 +                }
435 +        } else {
436 +                ourblack = 0;
437 +                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
438 +        }
439 +        XFree((char *)xvi);
440 + }
441 +
442 +
443   getras()                                /* get raster file */
444   {
445          colormap        ourmap;
# Line 303 | Line 449 | getras()                               /* get raster file */
449                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
450                  if (ourdata == NULL)
451                          goto fail;
452 <                ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
452 >                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
453                                  xmax, ymax, 8);
454                  if (ourras == NULL)
455                          goto fail;
456                  getmono();
457 <        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)
458 <                                                /* kludge for DirectColor */
313 <        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) {
314 <                ourdata = (unsigned char *)malloc(xmax*ymax*3);
457 >        } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
458 >                ourdata = (unsigned char *)malloc(4*xmax*ymax);
459                  if (ourdata == NULL)
460                          goto fail;
461 <                ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
462 <                                xmax, ymax, 8);
461 >                ourras = make_raster(thedisplay, &ourvis, 32,
462 >                                ourdata, xmax, ymax, 32);
463                  if (ourras == NULL)
464                          goto fail;
465                  getfull();
# Line 323 | Line 467 | getras()                               /* get raster file */
467                  ourdata = (unsigned char *)malloc(xmax*ymax);
468                  if (ourdata == NULL)
469                          goto fail;
470 <                ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
470 >                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
471                                  xmax, ymax, 8);
472                  if (ourras == NULL)
473                          goto fail;
474 <                if (greyscale)
475 <                        biq(dither,maxcolors,1,ourmap);
476 <                else
477 <                        ciq(dither,maxcolors,1,ourmap);
478 <                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
479 <                        goto fail;
474 >                if (ourvis.class == StaticGray)
475 >                        getgrey();
476 >                else {
477 >                        if (greyscale)
478 >                                biq(dither,maxcolors,1,ourmap);
479 >                        else
480 >                                ciq(dither,maxcolors,1,ourmap);
481 >                        if (init_rcolors(ourras, ourmap[0],
482 >                                        ourmap[1], ourmap[2]) == 0)
483 >                                goto fail;
484 >                }
485          }
486 <        return;
486 >                return;
487   fail:
488 <        quit("could not create raster image");
488 >        quiterr("could not create raster image");
489   }
490  
491  
# Line 362 | Line 511 | getevent()                             /* process the next event */
511          case MapNotify:
512                  map_rcolors(ourras, wind);
513                  if (fast)
514 <                        make_rpixmap(ourras);
514 >                        make_rpixmap(ourras, wind);
515                  break;
516          case UnmapNotify:
517 <                unmap_rcolors(ourras);
517 >                if (!fast)
518 >                        unmap_rcolors(ourras);
519                  break;
520          case Expose:
521                  redraw(e.e.x, e.e.y, e.e.width, e.e.height);
# Line 388 | Line 538 | XKeyPressedEvent  *ekey;
538          XColor  cvx;
539          int  com, n;
540          double  comp;
541 +        FLOAT  hv[2];
542          FVECT  rorg, rdir;
543  
544          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
# Line 396 | Line 547 | XKeyPressedEvent  *ekey;
547          com = buf[0];
548          switch (com) {                  /* interpret command */
549          case 'q':
550 <        case CTRL(D):                           /* quit */
550 >        case CTRL('D'):                         /* quit */
551                  quit(0);
552          case '\n':
553          case '\r':
# Line 410 | Line 561 | XKeyPressedEvent  *ekey;
561                          sprintf(buf, "%.3f", intens(cval)/exposure);
562                          break;
563                  case 'l':                               /* luminance */
564 <                        sprintf(buf, "%.0fn", bright(cval)*683.0/exposure);
564 >                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
565                          break;
566                  case 'c':                               /* color */
567                          comp = pow(2.0, (double)scale);
# Line 436 | Line 587 | XKeyPressedEvent  *ekey;
587                  XStoreColor(thedisplay, ourras->cmap, &cvx);
588                  return(0);
589          case 'p':                               /* position */
590 <                sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
590 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
591 >                sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr),
592 >                                (int)(hv[1]*inpres.yr));
593                  XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
594                                          buf, strlen(buf));
595                  return(0);
# Line 445 | Line 598 | XKeyPressedEvent  *ekey;
598                          XBell(thedisplay, 0);
599                          return(-1);
600                  }
601 <                viewray(rorg, rdir, &ourview,
602 <                                (ekey->x-xoff+.5)/xmax,
603 <                                (ymax-1-ekey->y+yoff+.5)/ymax);
601 >                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
602 >                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
603 >                        return(-1);
604                  printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
605                  printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
606                  fflush(stdout);
# Line 468 | Line 621 | XKeyPressedEvent  *ekey;
621                  free_raster(ourras);
622                  getras();
623          /* fall through */
624 <        case CTRL(R):                           /* redraw */
625 <        case CTRL(L):
624 >        case CTRL('R'):                         /* redraw */
625 >        case CTRL('L'):
626                  unmap_rcolors(ourras);
627                  XClearWindow(thedisplay, wind);
628                  map_rcolors(ourras, wind);
# Line 561 | Line 714 | int  x0, y0, x1, y1;
714   avgbox(clr)                             /* average color over current box */
715   COLOR  clr;
716   {
717 +        static COLOR  lc;
718 +        static int  ll, lr, lt, lb;
719          int  left, right, top, bottom;
720          int  y;
721          double  d;
# Line 584 | Line 739 | COLOR  clr;
739                  bottom = ymax;
740          if (top >= bottom)
741                  return(-1);
742 +        if (left == ll && right == lr && top == lt && bottom == lb) {
743 +                copycolor(clr, lc);
744 +                return;
745 +        }
746          for (y = top; y < bottom; y++) {
747                  if (getscan(y) == -1)
748                          return(-1);
# Line 594 | Line 753 | COLOR  clr;
753          }
754          d = 1.0/((right-left)*(bottom-top));
755          scalecolor(clr, d);
756 +        ll = left; lr = right; lt = top; lb = bottom;
757 +        copycolor(lc, clr);
758          return(0);
759   }
760  
# Line 602 | Line 763 | getmono()                      /* get monochrome data */
763   {
764          register unsigned char  *dp;
765          register int    x, err;
766 <        int     y;
606 <        rgbpixel        *inl;
766 >        int     y, errp;
767          short   *cerr;
768  
769 <        if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
770 <                        || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
611 <                quit("out of memory in getmono");
769 >        if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
770 >                quiterr("out of memory in getmono");
771          dp = ourdata - 1;
772          for (y = 0; y < ymax; y++) {
773 <                picreadline3(y, inl);
773 >                if (getscan(y) < 0)
774 >                        quiterr("seek error in getmono");
775 >                normcolrs(scanline, xmax, scale);
776 >                add2icon(y, scanline);
777                  err = 0;
778                  for (x = 0; x < xmax; x++) {
779                          if (!(x&7))
780                                  *++dp = 0;
781 <                        err += rgb_bright(&inl[x]) + cerr[x];
781 >                        errp = err;
782 >                        err += normbright(scanline[x]) + cerr[x];
783                          if (err > 127)
784                                  err -= 255;
785                          else
786                                  *dp |= 1<<(7-(x&07));
787 <                        cerr[x] = err >>= 1;
787 >                        err /= 3;
788 >                        cerr[x] = err + errp;
789                  }
790          }
627        free((char *)inl);
791          free((char *)cerr);
792   }
793  
794  
795 + add2icon(y, scan)               /* add a scanline to our icon data */
796 + int  y;
797 + COLR  *scan;
798 + {
799 +        static short  cerr[ICONSIZ];
800 +        static int  ynext;
801 +        static char  *dp;
802 +        register int  err;
803 +        register int    x, ti;
804 +        int  errp;
805 +
806 +        if (iconheight == 0) {          /* initialize */
807 +                if (xmax <= ICONSIZ && ymax <= ICONSIZ) {
808 +                        iconwidth = xmax;
809 +                        iconheight = ymax;
810 +                } else if (xmax > ymax) {
811 +                        iconwidth = ICONSIZ;
812 +                        iconheight = ICONSIZ*ymax/xmax;
813 +                        if (iconheight < 1)
814 +                                iconheight = 1;
815 +                } else {
816 +                        iconwidth = ICONSIZ*xmax/ymax;
817 +                        if (iconwidth < 1)
818 +                                iconwidth = 1;
819 +                        iconheight = ICONSIZ;
820 +                }
821 +                ynext = 0;
822 +                dp = icondata - 1;
823 +        }
824 +        if (y < ynext*ymax/iconheight)  /* skip this one */
825 +                return;
826 +        err = 0;
827 +        for (x = 0; x < iconwidth; x++) {
828 +                if (!(x&7))
829 +                        *++dp = 0;
830 +                errp = err;
831 +                ti = x*xmax/iconwidth;
832 +                err += normbright(scan[ti]) + cerr[x];
833 +                if (err > 127)
834 +                        err -= 255;
835 +                else
836 +                        *dp |= 1<<(x&07);
837 +                err /= 3;
838 +                cerr[x] = err + errp;
839 +        }
840 +        ynext++;
841 + }
842 +
843 +
844   getfull()                       /* get full (24-bit) data */
845   {
846          int     y;
847 +        register unsigned long  *dp;
848 +        register int    x;
849 +                                        /* set gamma correction */
850 +        setcolrgam(gamcor);
851 +                                        /* read and convert file */
852 +        dp = (unsigned long *)ourdata;
853 +        for (y = 0; y < ymax; y++) {
854 +                if (getscan(y) < 0)
855 +                        quiterr("seek error in getfull");
856 +                if (scale)
857 +                        shiftcolrs(scanline, xmax, scale);
858 +                colrs_gambs(scanline, xmax);
859 +                add2icon(y, scanline);
860 +                if (ourras->image->blue_mask & 1)
861 +                        for (x = 0; x < xmax; x++)
862 +                                *dp++ = scanline[x][RED] << 16 |
863 +                                        scanline[x][GRN] << 8 |
864 +                                        scanline[x][BLU] ;
865 +                else
866 +                        for (x = 0; x < xmax; x++)
867 +                                *dp++ = scanline[x][RED] |
868 +                                        scanline[x][GRN] << 8 |
869 +                                        scanline[x][BLU] << 16 ;
870 +        }
871 + }
872  
873 <        for (y = 0; y < ymax; y++)
874 <                picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3));
873 >
874 > getgrey()                       /* get greyscale data */
875 > {
876 >        int     y;
877 >        register unsigned char  *dp;
878 >        register int    x;
879 >                                        /* set gamma correction */
880 >        setcolrgam(gamcor);
881 >                                        /* read and convert file */
882 >        dp = ourdata;
883 >        for (y = 0; y < ymax; y++) {
884 >                if (getscan(y) < 0)
885 >                        quiterr("seek error in getfull");
886 >                if (scale)
887 >                        shiftcolrs(scanline, xmax, scale);
888 >                colrs_gambs(scanline, xmax);
889 >                add2icon(y, scanline);
890 >                if (ourvis.colormap_size < 256)
891 >                        for (x = 0; x < xmax; x++)
892 >                                *dp++ = ((long)normbright(scanline[x]) *
893 >                                        ourvis.colormap_size + 128) >> 8;
894 >                else
895 >                        for (x = 0; x < xmax; x++)
896 >                                *dp++ = normbright(scanline[x]);
897 >        }
898   }
899  
900  
# Line 673 | Line 933 | int  y;
933                  if (scanpos == NULL || scanpos[y] == -1)
934                          return(-1);
935                  if (fseek(fin, scanpos[y], 0) == -1)
936 <                        quit("fseek error");
936 >                        quiterr("fseek error");
937                  cury = y;
938          } else if (scanpos != NULL)
939                  scanpos[y] = ftell(fin);
# Line 696 | Line 956 | register rgbpixel  *l3;
956                  quiterr("cannot seek for picreadline");
957                                                          /* convert scanline */
958          normcolrs(scanline, xmax, scale);
959 +        add2icon(y, scanline);
960          for (i = 0; i < xmax; i++) {
961                  l3[i].r = scanline[i][RED];
962                  l3[i].g = scanline[i][GRN];
# Line 719 | Line 980 | colormap  map;
980          register int  i, val;
981  
982          for (i = 0; i < 256; i++) {
983 <                val = pow(i/256.0, 1.0/gamcor) * 256.0;
983 >                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
984                  map[0][i] = map[1][i] = map[2][i] = val;
985          }
986   }
# Line 728 | Line 989 | colormap  map;
989   picwritecm(map)                 /* handled elsewhere */
990   colormap  map;
991   {
992 < #ifdef DEBUG
992 > #if 0
993          register int i;
994  
995          for (i = 0; i < 256; i++)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines