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

Comparing ray/src/px/vgaimage.c (file contents):
Revision 2.2 by greg, Wed Oct 14 18:56:58 1992 UTC vs.
Revision 2.10 by greg, Sat Feb 22 02:07:28 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  vgaimage.c - driver for VGA board under DOS
6   */
# Line 16 | Line 13 | static char SCCSid[] = "$SunId$ LBL";
13   #include <dos.h>
14   #include <i86.h>
15  
16 < #define  M_RDOWN        0x8
17 < #define  M_RUP          0x10
18 < #define  M_LDOWN        0x2
19 < #define  M_LUP          0x4
20 < #define  M_MOTION       0x1
16 > #define  M_RDOWN        0x8
17 > #define  M_RUP          0x10
18 > #define  M_LDOWN        0x2
19 > #define  M_LUP          0x4
20 > #define  M_MOTION       0x1
21  
22   int crad;
23   int mouse_event = 0;
24   int mouse_xpos = -1;
25   int mouse_ypos = -1;
26  
27 < #define  hide_cursor    move_cursor(-1,-1)
28 < #define  show_cursor    move_cursor(mouse_xpos,mouse_ypos)
27 > #define  hide_cursor()  move_cursor(-1,-1)
28 > #define  show_cursor()  move_cursor(mouse_xpos,mouse_ypos)
29  
30 < #define  CTRL(c)        ((c)-'@')
30 > #define  CTRL(c)        ((c)-'@')
31  
32 < #define  MAXWIDTH       1024
33 < #define  MAXHEIGHT      768
32 > #define  MAXWIDTH       1024
33 > #define  MAXHEIGHT      768
34  
35   short  ourblack = 0; ourwhite = 1;
36  
37 < double  gamcor = 2.2;                   /* gamma correction */
37 > double  gamcor = 2.2;                   /* gamma correction */
38  
39 < int  dither = 1;                        /* dither colors? */
39 > int  dither = 1;                        /* dither colors? */
40  
41 < int  maxcolors = 0;                     /* maximum colors */
42 < int  minpix = 0;                        /* minimum pixel value */
43 < int  greyscale = 0;                     /* in grey */
41 > int  maxcolors = 0;                     /* maximum colors */
42 > int  minpix = 0;                        /* minimum pixel value */
43 > int  greyscale = 0;                     /* in grey */
44  
45 < int  scale = 0;                         /* scalefactor; power of two */
45 > int  scale = 0;                         /* scalefactor; power of two */
46  
47 < COLR  scanline[MAXWIDTH];               /* scan line buffer */
47 > COLR  scanline[MAXWIDTH];               /* scan line buffer */
48  
49 < int  xmax, ymax;                        /* picture dimensions */
50 < FILE  *fin = stdin;                     /* input file */
51 < long  scanpos[MAXHEIGHT];               /* scan line positions in file */
52 < int  cury = 0;                          /* current scan location */
49 > int  xmax, ymax;                        /* picture dimensions */
50 > FILE  *fin = stdin;                     /* input file */
51 > long  scanpos[MAXHEIGHT];               /* scan line positions in file */
52 > int  cury = 0;                          /* current scan location */
53  
54 < double  exposure = 1.0;                 /* exposure compensation used */
54 > double  exposure = 1.0;                 /* exposure compensation used */
55  
56 < int  wrongformat = 0;                   /* input in another format? */
56 > int  wrongformat = 0;                   /* input in another format? */
57  
58   struct {
59          int  xmin, ymin, xsiz, ysiz;
60 < }  box = {0, 0, 0, 0};                  /* current box */
60 > }  box = {0, 0, 0, 0};                  /* current box */
61  
62   int  initialized = 0;
63   int  cheight, cwidth;
64  
65 < #define postext(x,y)    _settextposition(1+(y)/cheight,1+(x)/cwidth)
65 > #define postext(x,y)    _settextposition(1+(y)/cheight,1+(x)/cwidth)
66  
67   char  *progname;
68  
69   char  errmsg[128];
70  
71 < extern BYTE  clrtab[256][3];            /* global color map */
71 > extern BYTE  clrtab[256][3];            /* global color map */
72  
73   extern long  ftell();
74  
# Line 84 | Line 81 | char  *argv[];
81          char  *gv;
82          int  headline();
83          int  i;
84 <        
84 >
85          progname = argv[0] = fixargv0(argv[0]);
86 <        if ((gv = getenv("GAMMA")) != NULL)
86 >        if ((gv = getenv("DISPLAY_GAMMA")) != NULL)
87                  gamcor = atof(gv);
88  
89          for (i = 1; i < argc; i++)
# Line 118 | Line 115 | char  *argv[];
115                  else
116                          break;
117  
118 <        if (i == argc-1) {              /* open picture */
118 >        if (i == argc-1) {              /* open picture */
119                  fin = fopen(argv[i], "r");
120                  if (fin == NULL) {
121                          sprintf(errmsg, "cannot open file \"%s\"", argv[i]);
# Line 135 | Line 132 | char  *argv[];
132          if (xmax > MAXWIDTH | ymax > MAXHEIGHT)
133                  quiterr("input picture too large for VGA");
134  
135 <        init();                 /* initialize and display */
135 >        init();                 /* initialize and display */
136  
137 <        while (docommand())     /* loop on command */
137 >        while (docommand())     /* loop on command */
138                  ;
139          quiterr(NULL);
140   userr:
# Line 147 | Line 144 | userr:
144   }
145  
146  
147 < headline(s)             /* get relevant info from header */
147 > int
148 > headline(s)             /* get relevant info from header */
149   char  *s;
150   {
151          char  fmt[32];
# Line 158 | Line 156 | char  *s;
156                  formatval(fmt, s);
157                  wrongformat = strcmp(fmt, COLRFMT);
158          }
159 +        return(0);
160   }
161  
162  
163 < init()                  /* initialize and load display */
163 > init()                  /* initialize and load display */
164   {
165          static struct {
166 <                short   mode;
167 <                short   xsiz, ysiz;
168 <        } video[] = {    
166 >                short   mode;
167 >                short   xsiz, ysiz;
168 >        } video[] = {
169                  {_MRES256COLOR, 320, 200},
170 <                {_VRES256COLOR, 640, 400},
170 >                {_VRES256COLOR, 640, 480},
171                  {_SVRES256COLOR, 800, 600},
172                  {_XRES256COLOR, 1024, 768},
173 <                -1
173 >                {-1, 0, 0}
174          };
175 <        struct videoconfig      config;
176 <        register int    i;
175 >        struct videoconfig      config;
176 >        register int    i;
177                                  /* pick a card... */
178          for (i = 0; video[i].mode != -1; i++)
179                  if (video[i].xsiz >= xmax && video[i].ysiz >= ymax)
# Line 183 | Line 182 | init()                  /* initialize and load display
182                  quiterr("input picture too large");
183          if (_setvideomode(video[i].mode) == 0)
184                  quiterr("inadequate display card for picture");
185 +        ms_init();
186          initialized = 1;
187          _getvideoconfig(&config);
188 <        if (maxcolors == 0)
189 <                maxcolors = config.numcolors;
190 <        if (maxcolors > 4) {
191 <                maxcolors -= minpix = 2;
192 <                _settextcolor(ourwhite);
193 <        }
188 >        if (maxcolors == 0 | maxcolors > config.numcolors)
189 >                maxcolors = config.numcolors-2;
190 >        if (maxcolors <= config.numcolors-2)
191 >                minpix = 2;
192 >        else
193 >                ourwhite = maxcolors-1;
194 >        _settextcolor(ourwhite);
195          cheight = config.numypixels/config.numtextrows;
196          cwidth = config.numxpixels/config.numtextcols;
197                                  /* clear scan position array */
# Line 204 | Line 205 | init()                  /* initialize and load display
205   }
206  
207  
208 < quiterr(err)            /* print message and exit */
208 > quiterr(err)            /* print message and exit */
209   char  *err;
210   {
211 <        if (initialized)
211 >        if (initialized) {
212 >                ms_done();
213                  _setvideomode(_DEFAULTMODE);
214 +        }
215          if (err != NULL) {
216                  fprintf(stderr, "%s: %s\n", progname, err);
217                  exit(1);
# Line 218 | Line 221 | char  *err;
221  
222  
223   int
224 < docommand()                     /* execute command */
224 > docommand()                     /* execute command */
225   {
226          char  buf[64];
227          COLOR  cval;
228          int  com;
229 <        double  comp;
229 >        double  comp;
230  
231          while (!kbhit())
232                  watch_mouse();
233          com = getch();
234 <        switch (com) {                  /* interpret command */
234 >        switch (com) {                  /* interpret command */
235          case 'q':
236 <        case CTRL('Z'):                         /* quit */
236 >        case CTRL('Z'):                         /* quit */
237                  return(0);
238          case '\n':
239          case '\r':
240          case 'l':
241 <        case 'c':                               /* value */
241 >        case 'c':                               /* value */
242                  if (avgbox(cval) == -1)
243                          return(-1);
244                  switch (com) {
245                  case '\n':
246 <                case '\r':                              /* radiance */
246 >                case '\r':                              /* radiance */
247                          sprintf(buf, "%.3f", intens(cval)/exposure);
248                          break;
249 <                case 'l':                               /* luminance */
249 >                case 'l':                               /* luminance */
250                          sprintf(buf, "%.0fL", luminance(cval)/exposure);
251                          break;
252 <                case 'c':                               /* color */
252 >                case 'c':                               /* color */
253                          comp = pow(2.0, (double)scale);
254                          sprintf(buf, "(%.2f,%.2f,%.2f)",
255                                          colval(cval,RED)*comp,
# Line 265 | Line 268 | docommand()                     /* execute command */
268   }
269  
270  
271 < watch_mouse()                   /* look after mousie */
271 > watch_mouse()                   /* look after mousie */
272   {
273 <        static int      mouse_installed = 0;
271 <        int     a_x, a_y, l_x, l_y;
273 >        int     a_x, a_y, l_x, l_y;
274  
273        if (!mouse_installed) {
274                ms_init();
275                mouse_installed = 1;
276        }
275          if (mouse_event & M_MOTION)
276                  move_cursor(mouse_xpos, mouse_ypos);
277          if (!(mouse_event & M_LDOWN))
278                  return;
279          l_x = a_x = mouse_xpos; l_y = a_y = mouse_ypos;
280          hide_cursor();
281 <        revbox(a_x, a_y, l_x, l_y);             /* show box */
281 >        revbox(a_x, a_y, l_x, l_y);             /* show box */
282          do {
283                  mouse_event = 0;
284                  while (!mouse_event)
# Line 290 | Line 288 | watch_mouse()                   /* look after mousie *
288                          revbox(a_x, a_y, l_x=mouse_xpos, l_y=mouse_ypos);
289                  }
290          } while (!(mouse_event & M_LUP));
291 <        revbox(a_x, a_y, l_x, l_y);             /* hide box */
291 >        revbox(a_x, a_y, l_x, l_y);             /* hide box */
292          show_cursor();
293          box.xmin = mouse_xpos;
294          box.ymin = mouse_ypos;
# Line 310 | Line 308 | watch_mouse()                   /* look after mousie *
308   }
309  
310  
311 < revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
311 > revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
312   int  x0, y0, x1, y1;
313   {
314          _setplotaction(_GXOR);
# Line 324 | Line 322 | int  x0, y0, x1, y1;
322  
323  
324   int
325 < avgbox(clr)                             /* average color over current box */
325 > avgbox(clr)                             /* average color over current box */
326   COLOR  clr;
327   {
328          static COLOR  lc;
329          static int  ll, lr, lt, lb;
330          int  left, right, top, bottom;
331          int  y;
332 <        double  d;
332 >        double  d;
333          COLOR  ctmp;
334          register int  x;
335  
# Line 372 | Line 370 | COLOR  clr;
370   }
371  
372  
373 < setpalette()                    /* set our palette using clrtab */
373 > setpalette()                    /* set our palette using clrtab */
374   {
375 <        long    cvals[256];
376 <        register int    i;
375 >        long    cvals[256];
376 >        register int    i;
377  
378 <        if (minpix >= 2) {
379 <                cvals[ourblack] = _BLACK; cvals[ourwhite] = _BRIGHTWHITE;
382 <        }
378 >        cvals[ourblack] = _BLACK;
379 >        cvals[ourwhite] = _BRIGHTWHITE;
380          for (i = 0; i < maxcolors; i++)
381 <                cvals[i+minpix] = clrtab[i][BLU]<<14 & 0x3f0000L |
381 >                cvals[i+minpix] = (long)clrtab[i][BLU]<<14 & 0x3f0000L |
382                                    clrtab[i][GRN]<<6 & 0x3f00 |
383                                    clrtab[i][RED]>>2;
384          _remapallpalette(cvals);
385   }
386  
387  
388 < greyimage()                     /* display greyscale image */
388 > greyimage()                     /* display greyscale image */
389   {
390 <        short   thiscolor, lastcolor = -1;
391 <        int     y;
392 <        register int    x;
390 >        short   thiscolor, lastcolor = -1;
391 >        int     y;
392 >        register int    x;
393                                          /* set gamma correction */
394          setcolrgam(gamcor);
395                                          /* set up color map */
396          for (x = 0; x < maxcolors; x++)
397 <                clrtab[x][RED] = clrtab[x][GRN] =
398 <                        clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors;
397 >                clrtab[x][RED] = clrtab[x][GRN] = clrtab[x][BLU] =
398 >                                ((long)x*256 + 128)/maxcolors;
399          setpalette();
400          _setplotaction(_GPSET);
401                                          /* read and display file */
# Line 406 | Line 403 | greyimage()                     /* display greyscale i
403                  getscan(y);
404                  if (scale)
405                          shiftcolrs(scanline, xmax, scale);
406 +                for (x = 0; x < xmax; x++)
407 +                        scanline[x][GRN] = normbright(scanline[x]);
408                  colrs_gambs(scanline, xmax);
409                  if (maxcolors < 256)
410 <                        for (x = 0; x < xmax; x++) {
411 <                                thiscolor = ((long)normbright(scanline[x]) *
412 <                                        maxcolors + 128) >> 8;
410 >                        for (x = 0; x < xmax; x++) {
411 >                                thiscolor = ((long)scanline[x][GRN] *
412 >                                                maxcolors + maxcolors/2) / 256;
413                                  if (thiscolor != lastcolor)
414                                          _setcolor((lastcolor=thiscolor)+minpix);
415                                  _setpixel(x, y);
416                          }
417                  else
418                          for (x = 0; x < xmax; x++) {
419 <                                thiscolor = normbright(scanline[x]);
419 >                                thiscolor = scanline[x][GRN];
420                                  if (thiscolor != lastcolor)
421                                          _setcolor((lastcolor=thiscolor)+minpix);
422                                  _setpixel(x, y);
# Line 426 | Line 425 | greyimage()                     /* display greyscale i
425   }
426  
427  
428 < mappedimage()                   /* display color-mapped image */
428 > mappedimage()                   /* display color-mapped image */
429   {
430 <        BYTE    bscan[MAXWIDTH];
431 <        int     y;
432 <        register int    x;
430 >        BYTE    bscan[MAXWIDTH];
431 >        int     y;
432 >        register int    x;
433                                          /* set gamma correction */
434          setcolrgam(gamcor);
435                                          /* make histogram */
436 +        _outtext("Quantizing image -- Please wait...");
437          new_histo();
438          for (y = 0; y < ymax; y++) {
439                  if (getscan(y) < 0)
# Line 477 | Line 477 | int  y;
477                  if (fseek(fin, scanpos[y], 0) == -1)
478                          quiterr("fseek error");
479                  cury = y;
480 <        } else if (scanpos != NULL && scanpos[y] == -1)
480 >        } else if (fin != stdin && scanpos[y] == -1)
481                  scanpos[y] = ftell(fin);
482  
483          if (freadcolrs(scanline, xmax, fin) < 0)
# Line 499 | Line 499 | void _loadds far mouse_handler (int max, int mcx, int
499   #pragma aux mouse_handler parm [EAX] [ECX] [EDX]
500          mouse_event = max;
501          mouse_xpos = mcx;
502 <        mouse_ypos = mdx * (long)ymax / 200;       /* kludge */
503 <        if (mouse_xpos >= xmax) mouse_xpos = xmax-1;
504 <        if (mouse_ypos >= ymax) mouse_ypos = ymax-1;
502 >        mouse_ypos = mdx;
503   }
504   #pragma on (check_stack)
505  
506  
507   void
508 < move_cursor(newx, newy)         /* move cursor to new position */
508 > move_cursor(newx, newy)         /* move cursor to new position */
509   int  newx, newy;
510   {
511          static char  *imp = NULL;
512          static int  curx = -1, cury = -1;
513 < #define xcmin           (curx-crad<0 ? 0 : curx-crad)
514 < #define ycmin           (cury-crad<0 ? 0 : cury-crad)
515 < #define xcmax           (curx+crad>=xmax ? xmax-1 : curx+crad)
516 < #define ycmax           (cury+crad>=ymax ? ymax-1 : cury+crad)
513 > #define xcmin           (curx-crad<0 ? 0 : curx-crad)
514 > #define ycmin           (cury-crad<0 ? 0 : cury-crad)
515 > #define xcmax           (curx+crad>=xmax ? xmax-1 : curx+crad)
516 > #define ycmax           (cury+crad>=ymax ? ymax-1 : cury+crad)
517  
518          if (newx == curx & newy == cury)
519                  return;
520 <        if (imp == NULL &&
520 >        if (imp == NULL &&
521                  (imp = bmalloc(_imagesize(0,0,2*crad+1,2*crad+1))) == NULL) {
522                  quiterr("out of memory in move_cursor");
523          }
524 <        if (curx >= 0 & cury >= 0)      /* clear old cursor */
524 >        if (curx >= 0 & cury >= 0)      /* clear old cursor */
525                  _putimage(xcmin, ycmin, imp, _GPSET);
526                                          /* record new position */
527          curx = newx; cury = newy;
528          if (curx < 0 | cury < 0)
529 <                return;         /* no cursor */
529 >                return;         /* no cursor */
530                                          /* save under new cursor */
531          _getimage(xcmin, ycmin, xcmax, ycmax, imp);
532                                          /* draw new cursor */
# Line 567 | Line 565 | ms_init()
565      }
566  
567      crad = ymax/40;
568 <    
568 >
569 >    /* set screen limits */
570 >
571 >    inregs.w.ax = 0x7;          /* horizontal resolution */
572 >    inregs.w.cx = 0;
573 >    inregs.w.dx = xmax-1;
574 >    int386x( 0x33, &inregs, &outregs, &sregs );
575 >    inregs.w.ax = 0x8;          /* vertical resolution */
576 >    inregs.w.cx = 0;
577 >    inregs.w.dx = ymax-1;
578 >    int386x( 0x33, &inregs, &outregs, &sregs );
579 >
580      /* install watcher */
581  
582      inregs.w.ax = 0xC;
583      inregs.w.cx = M_LDOWN | M_LUP | M_MOTION;
584      function_ptr = mouse_handler;
585      inregs.x.edx = FP_OFF( function_ptr );
586 <    sregs.es     = FP_SEG( function_ptr );
586 >    sregs.es     = FP_SEG( function_ptr );
587      int386x( 0x33, &inregs, &outregs, &sregs );
588 <    
588 >
589      return(1);
590 + }
591 +
592 + ms_done()
593 + {
594 +    union REGS inregs, outregs;
595 +
596 +    /* uninstall watcher */
597 +
598 +    inregs.w.ax = 0;
599 +    int386 (0x33, &inregs, &outregs);
600   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines