--- ray/src/px/vgaimage.c 1992/10/14 18:56:58 2.2 +++ ray/src/px/vgaimage.c 2004/01/02 12:47:01 2.11 @@ -1,13 +1,12 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: vgaimage.c,v 2.11 2004/01/02 12:47:01 schorsch Exp $"; #endif - /* * vgaimage.c - driver for VGA board under DOS */ +#include + #include "standard.h" #include #include "color.h" @@ -16,64 +15,64 @@ static char SCCSid[] = "$SunId$ LBL"; #include #include -#define M_RDOWN 0x8 -#define M_RUP 0x10 -#define M_LDOWN 0x2 -#define M_LUP 0x4 -#define M_MOTION 0x1 +#define M_RDOWN 0x8 +#define M_RUP 0x10 +#define M_LDOWN 0x2 +#define M_LUP 0x4 +#define M_MOTION 0x1 int crad; int mouse_event = 0; int mouse_xpos = -1; int mouse_ypos = -1; -#define hide_cursor move_cursor(-1,-1) -#define show_cursor move_cursor(mouse_xpos,mouse_ypos) +#define hide_cursor() move_cursor(-1,-1) +#define show_cursor() move_cursor(mouse_xpos,mouse_ypos) -#define CTRL(c) ((c)-'@') +#define CTRL(c) ((c)-'@') -#define MAXWIDTH 1024 -#define MAXHEIGHT 768 +#define MAXWIDTH 1024 +#define MAXHEIGHT 768 short ourblack = 0; ourwhite = 1; -double gamcor = 2.2; /* gamma correction */ +double gamcor = 2.2; /* gamma correction */ -int dither = 1; /* dither colors? */ +int dither = 1; /* dither colors? */ -int maxcolors = 0; /* maximum colors */ -int minpix = 0; /* minimum pixel value */ -int greyscale = 0; /* in grey */ +int maxcolors = 0; /* maximum colors */ +int minpix = 0; /* minimum pixel value */ +int greyscale = 0; /* in grey */ -int scale = 0; /* scalefactor; power of two */ +int scale = 0; /* scalefactor; power of two */ -COLR scanline[MAXWIDTH]; /* scan line buffer */ +COLR scanline[MAXWIDTH]; /* scan line buffer */ -int xmax, ymax; /* picture dimensions */ -FILE *fin = stdin; /* input file */ -long scanpos[MAXHEIGHT]; /* scan line positions in file */ -int cury = 0; /* current scan location */ +int xmax, ymax; /* picture dimensions */ +FILE *fin = stdin; /* input file */ +long scanpos[MAXHEIGHT]; /* scan line positions in file */ +int cury = 0; /* current scan location */ -double exposure = 1.0; /* exposure compensation used */ +double exposure = 1.0; /* exposure compensation used */ -int wrongformat = 0; /* input in another format? */ +int wrongformat = 0; /* input in another format? */ struct { int xmin, ymin, xsiz, ysiz; -} box = {0, 0, 0, 0}; /* current box */ +} box = {0, 0, 0, 0}; /* current box */ int initialized = 0; int cheight, cwidth; -#define postext(x,y) _settextposition(1+(y)/cheight,1+(x)/cwidth) +#define postext(x,y) _settextposition(1+(y)/cheight,1+(x)/cwidth) char *progname; char errmsg[128]; -extern BYTE clrtab[256][3]; /* global color map */ +extern BYTE clrtab[256][3]; /* global color map */ -extern long ftell(); +static gethfunc headline; main(argc, argv) @@ -82,11 +81,10 @@ char *argv[]; { extern char *getenv(), *fixargv0(); char *gv; - int headline(); int i; - + progname = argv[0] = fixargv0(argv[0]); - if ((gv = getenv("GAMMA")) != NULL) + if ((gv = getenv("DISPLAY_GAMMA")) != NULL) gamcor = atof(gv); for (i = 1; i < argc; i++) @@ -118,7 +116,7 @@ char *argv[]; else break; - if (i == argc-1) { /* open picture */ + if (i == argc-1) { /* open picture */ fin = fopen(argv[i], "r"); if (fin == NULL) { sprintf(errmsg, "cannot open file \"%s\"", argv[i]); @@ -135,9 +133,9 @@ char *argv[]; if (xmax > MAXWIDTH | ymax > MAXHEIGHT) quiterr("input picture too large for VGA"); - init(); /* initialize and display */ + init(); /* initialize and display */ - while (docommand()) /* loop on command */ + while (docommand()) /* loop on command */ ; quiterr(NULL); userr: @@ -147,8 +145,11 @@ userr: } -headline(s) /* get relevant info from header */ -char *s; +static int +headline( /* get relevant info from header */ + char *s, + void *p +) { char fmt[32]; @@ -158,23 +159,24 @@ char *s; formatval(fmt, s); wrongformat = strcmp(fmt, COLRFMT); } + return(0); } -init() /* initialize and load display */ +init() /* initialize and load display */ { static struct { - short mode; - short xsiz, ysiz; - } video[] = { + short mode; + short xsiz, ysiz; + } video[] = { {_MRES256COLOR, 320, 200}, - {_VRES256COLOR, 640, 400}, + {_VRES256COLOR, 640, 480}, {_SVRES256COLOR, 800, 600}, {_XRES256COLOR, 1024, 768}, - -1 + {-1, 0, 0} }; - struct videoconfig config; - register int i; + struct videoconfig config; + register int i; /* pick a card... */ for (i = 0; video[i].mode != -1; i++) if (video[i].xsiz >= xmax && video[i].ysiz >= ymax) @@ -183,14 +185,16 @@ init() /* initialize and load display quiterr("input picture too large"); if (_setvideomode(video[i].mode) == 0) quiterr("inadequate display card for picture"); + ms_init(); initialized = 1; _getvideoconfig(&config); - if (maxcolors == 0) - maxcolors = config.numcolors; - if (maxcolors > 4) { - maxcolors -= minpix = 2; - _settextcolor(ourwhite); - } + if (maxcolors == 0 | maxcolors > config.numcolors) + maxcolors = config.numcolors-2; + if (maxcolors <= config.numcolors-2) + minpix = 2; + else + ourwhite = maxcolors-1; + _settextcolor(ourwhite); cheight = config.numypixels/config.numtextrows; cwidth = config.numxpixels/config.numtextcols; /* clear scan position array */ @@ -204,11 +208,13 @@ init() /* initialize and load display } -quiterr(err) /* print message and exit */ +quiterr(err) /* print message and exit */ char *err; { - if (initialized) + if (initialized) { + ms_done(); _setvideomode(_DEFAULTMODE); + } if (err != NULL) { fprintf(stderr, "%s: %s\n", progname, err); exit(1); @@ -218,35 +224,35 @@ char *err; int -docommand() /* execute command */ +docommand() /* execute command */ { char buf[64]; COLOR cval; int com; - double comp; + double comp; while (!kbhit()) watch_mouse(); com = getch(); - switch (com) { /* interpret command */ + switch (com) { /* interpret command */ case 'q': - case CTRL('Z'): /* quit */ + case CTRL('Z'): /* quit */ return(0); case '\n': case '\r': case 'l': - case 'c': /* value */ + case 'c': /* value */ if (avgbox(cval) == -1) return(-1); switch (com) { case '\n': - case '\r': /* radiance */ + case '\r': /* radiance */ sprintf(buf, "%.3f", intens(cval)/exposure); break; - case 'l': /* luminance */ + case 'l': /* luminance */ sprintf(buf, "%.0fL", luminance(cval)/exposure); break; - case 'c': /* color */ + case 'c': /* color */ comp = pow(2.0, (double)scale); sprintf(buf, "(%.2f,%.2f,%.2f)", colval(cval,RED)*comp, @@ -265,22 +271,17 @@ docommand() /* execute command */ } -watch_mouse() /* look after mousie */ +watch_mouse() /* look after mousie */ { - static int mouse_installed = 0; - int a_x, a_y, l_x, l_y; + int a_x, a_y, l_x, l_y; - if (!mouse_installed) { - ms_init(); - mouse_installed = 1; - } if (mouse_event & M_MOTION) move_cursor(mouse_xpos, mouse_ypos); if (!(mouse_event & M_LDOWN)) return; l_x = a_x = mouse_xpos; l_y = a_y = mouse_ypos; hide_cursor(); - revbox(a_x, a_y, l_x, l_y); /* show box */ + revbox(a_x, a_y, l_x, l_y); /* show box */ do { mouse_event = 0; while (!mouse_event) @@ -290,7 +291,7 @@ watch_mouse() /* look after mousie * revbox(a_x, a_y, l_x=mouse_xpos, l_y=mouse_ypos); } } while (!(mouse_event & M_LUP)); - revbox(a_x, a_y, l_x, l_y); /* hide box */ + revbox(a_x, a_y, l_x, l_y); /* hide box */ show_cursor(); box.xmin = mouse_xpos; box.ymin = mouse_ypos; @@ -310,7 +311,7 @@ watch_mouse() /* look after mousie * } -revbox(x0, y0, x1, y1) /* draw box with reversed lines */ +revbox(x0, y0, x1, y1) /* draw box with reversed lines */ int x0, y0, x1, y1; { _setplotaction(_GXOR); @@ -324,14 +325,14 @@ int x0, y0, x1, y1; int -avgbox(clr) /* average color over current box */ +avgbox(clr) /* average color over current box */ COLOR clr; { static COLOR lc; static int ll, lr, lt, lb; int left, right, top, bottom; int y; - double d; + double d; COLOR ctmp; register int x; @@ -372,33 +373,32 @@ COLOR clr; } -setpalette() /* set our palette using clrtab */ +setpalette() /* set our palette using clrtab */ { - long cvals[256]; - register int i; + long cvals[256]; + register int i; - if (minpix >= 2) { - cvals[ourblack] = _BLACK; cvals[ourwhite] = _BRIGHTWHITE; - } + cvals[ourblack] = _BLACK; + cvals[ourwhite] = _BRIGHTWHITE; for (i = 0; i < maxcolors; i++) - cvals[i+minpix] = clrtab[i][BLU]<<14 & 0x3f0000L | + cvals[i+minpix] = (long)clrtab[i][BLU]<<14 & 0x3f0000L | clrtab[i][GRN]<<6 & 0x3f00 | clrtab[i][RED]>>2; _remapallpalette(cvals); } -greyimage() /* display greyscale image */ +greyimage() /* display greyscale image */ { - short thiscolor, lastcolor = -1; - int y; - register int x; + short thiscolor, lastcolor = -1; + int y; + register int x; /* set gamma correction */ setcolrgam(gamcor); /* set up color map */ for (x = 0; x < maxcolors; x++) - clrtab[x][RED] = clrtab[x][GRN] = - clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors; + clrtab[x][RED] = clrtab[x][GRN] = clrtab[x][BLU] = + ((long)x*256 + 128)/maxcolors; setpalette(); _setplotaction(_GPSET); /* read and display file */ @@ -406,18 +406,20 @@ greyimage() /* display greyscale i getscan(y); if (scale) shiftcolrs(scanline, xmax, scale); + for (x = 0; x < xmax; x++) + scanline[x][GRN] = normbright(scanline[x]); colrs_gambs(scanline, xmax); if (maxcolors < 256) - for (x = 0; x < xmax; x++) { - thiscolor = ((long)normbright(scanline[x]) * - maxcolors + 128) >> 8; + for (x = 0; x < xmax; x++) { + thiscolor = ((long)scanline[x][GRN] * + maxcolors + maxcolors/2) / 256; if (thiscolor != lastcolor) _setcolor((lastcolor=thiscolor)+minpix); _setpixel(x, y); } else for (x = 0; x < xmax; x++) { - thiscolor = normbright(scanline[x]); + thiscolor = scanline[x][GRN]; if (thiscolor != lastcolor) _setcolor((lastcolor=thiscolor)+minpix); _setpixel(x, y); @@ -426,14 +428,15 @@ greyimage() /* display greyscale i } -mappedimage() /* display color-mapped image */ +mappedimage() /* display color-mapped image */ { - BYTE bscan[MAXWIDTH]; - int y; - register int x; + BYTE bscan[MAXWIDTH]; + int y; + register int x; /* set gamma correction */ setcolrgam(gamcor); /* make histogram */ + _outtext("Quantizing image -- Please wait..."); new_histo(); for (y = 0; y < ymax; y++) { if (getscan(y) < 0) @@ -477,7 +480,7 @@ int y; if (fseek(fin, scanpos[y], 0) == -1) quiterr("fseek error"); cury = y; - } else if (scanpos != NULL && scanpos[y] == -1) + } else if (fin != stdin && scanpos[y] == -1) scanpos[y] = ftell(fin); if (freadcolrs(scanline, xmax, fin) < 0) @@ -499,36 +502,34 @@ void _loadds far mouse_handler (int max, int mcx, int #pragma aux mouse_handler parm [EAX] [ECX] [EDX] mouse_event = max; mouse_xpos = mcx; - mouse_ypos = mdx * (long)ymax / 200; /* kludge */ - if (mouse_xpos >= xmax) mouse_xpos = xmax-1; - if (mouse_ypos >= ymax) mouse_ypos = ymax-1; + mouse_ypos = mdx; } #pragma on (check_stack) void -move_cursor(newx, newy) /* move cursor to new position */ +move_cursor(newx, newy) /* move cursor to new position */ int newx, newy; { static char *imp = NULL; static int curx = -1, cury = -1; -#define xcmin (curx-crad<0 ? 0 : curx-crad) -#define ycmin (cury-crad<0 ? 0 : cury-crad) -#define xcmax (curx+crad>=xmax ? xmax-1 : curx+crad) -#define ycmax (cury+crad>=ymax ? ymax-1 : cury+crad) +#define xcmin (curx-crad<0 ? 0 : curx-crad) +#define ycmin (cury-crad<0 ? 0 : cury-crad) +#define xcmax (curx+crad>=xmax ? xmax-1 : curx+crad) +#define ycmax (cury+crad>=ymax ? ymax-1 : cury+crad) if (newx == curx & newy == cury) return; - if (imp == NULL && + if (imp == NULL && (imp = bmalloc(_imagesize(0,0,2*crad+1,2*crad+1))) == NULL) { quiterr("out of memory in move_cursor"); } - if (curx >= 0 & cury >= 0) /* clear old cursor */ + if (curx >= 0 & cury >= 0) /* clear old cursor */ _putimage(xcmin, ycmin, imp, _GPSET); /* record new position */ curx = newx; cury = newy; if (curx < 0 | cury < 0) - return; /* no cursor */ + return; /* no cursor */ /* save under new cursor */ _getimage(xcmin, ycmin, xcmax, ycmax, imp); /* draw new cursor */ @@ -567,15 +568,36 @@ ms_init() } crad = ymax/40; - + + /* set screen limits */ + + inregs.w.ax = 0x7; /* horizontal resolution */ + inregs.w.cx = 0; + inregs.w.dx = xmax-1; + int386x( 0x33, &inregs, &outregs, &sregs ); + inregs.w.ax = 0x8; /* vertical resolution */ + inregs.w.cx = 0; + inregs.w.dx = ymax-1; + int386x( 0x33, &inregs, &outregs, &sregs ); + /* install watcher */ inregs.w.ax = 0xC; inregs.w.cx = M_LDOWN | M_LUP | M_MOTION; function_ptr = mouse_handler; inregs.x.edx = FP_OFF( function_ptr ); - sregs.es = FP_SEG( function_ptr ); + sregs.es = FP_SEG( function_ptr ); int386x( 0x33, &inregs, &outregs, &sregs ); - + return(1); +} + +ms_done() +{ + union REGS inregs, outregs; + + /* uninstall watcher */ + + inregs.w.ax = 0; + int386 (0x33, &inregs, &outregs); }