home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
REND.LZH
/
REND
/
CRTX11.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-16
|
7KB
|
421 lines
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include "reader.h"
#include "crtX11.h"
char *program = "Do-GA C.G.A System Rendering Program for X11" ;
char app_name[] = "xrend";
extern int antiareas; /* in rend.c */
extern int XPixel,YPixel; /* in glib.c */
static u_int rshift,gshift,bshift;
static u_int rmaskl,gmaskl,bmaskl;
void
init_color(pixels,ncols)
u_long *pixels;
u_int ncols;
{
u_int ri,gi,bi;
u_int rstep,gstep,bstep;
u_int rlev,glev,blev;
switch( ncols ){
case 256:
rshift = 3;
gshift = 3;
bshift = 2;
break;
case 128:
rshift = 2;
gshift = 3;
bshift = 2;
break;
case 64:
rshift = 2;
gshift = 2;
bshift = 2;
break;
case 32:
rshift = 2;
gshift = 2;
bshift = 1;
break;
case 16:
rshift = 1;
gshift = 2;
bshift = 1;
break;
case 8:
rshift = 1;
gshift = 1;
bshift = 1;
break;
default:
fprintf(stderr,"init color error. illegal argument ncol:%d\n",ncols);
exit(1);
}
rstep = (1<<rshift) - 1;
rlev = 65535/rstep;
gstep = (1<<gshift) - 1;
glev = 65535/gstep;
bstep = (1<<bshift) - 1;
blev = 65535/bstep;
{
XColor c;
int i = 0;
for( ri=0; ri<=rstep; ri++ ){
for( gi=0; gi<=gstep; gi++ ){
for( bi=0; bi<=bstep; bi++ ){
c.pixel = pixels[i];
red[i] = c.red = rlev*ri;
green[i] = c.green = glev*gi;
blue[i] = c.blue = blev*bi;
c.flags = DoRed|DoGreen|DoBlue;
XStoreColor(display,cmap,&c);
i++;
}
}
}
}
}
u_int
getshift(mask,depth)
u_long mask;
int depth;
{
u_int shift;
for(shift=0;shift<depth;shift++){
if( mask & 1 )
break;
mask>>=1;
}
return shift;
}
u_int
getmasklen(mask,depth)
u_long mask;
int depth;
{
u_int i;
u_int len = 0;
mask >>= getshift(mask,depth);
for(len=0;len<depth;len++){
if(!(mask & 1))
break;
mask>>=1;
}
return len;
}
void crtinit( line )
int line;
{
display = XOpenDisplay(0);
if(!display){
fprintf(stderr, "rend: Can't open display\n");
exit(1);
}
screen = DefaultScreen(display);
bzero((char *)&hints, sizeof hints );
hints.flags = PPosition|USSize|PMinSize|PMaxSize;
if(
0 !=
(XGeometry(display,screen, 0,0,1,1,1,0,0,&gm_x,&gm_y,&gm_w,&gm_h) & (XValue|YValue))
){
hints.flags |= USPosition;
}
gm_w = XPixel/antiareas;
gm_h = YPixel/antiareas;
hints.x = gm_x;
hints.y = gm_y;
hints.min_width = hints.max_width = hints.width = gm_w;
hints.min_height = hints.max_height = hints.height = gm_h;
window = XCreateSimpleWindow(
display,
RootWindow( display, screen ),
gm_x,
gm_y,
gm_w,
gm_h,
4,
BlackPixel( display, screen ),
WhitePixel( display, screen )
);
if(!window){
printf("Can't create window\n");
exit(1);
}
XSetNormalHints( display, window, &hints );
XStoreName(display, window, app_name );
gc = XCreateGC( display, window, 0, 0 );
visual = DefaultVisual( display, screen );
vclass = visual->class;
{
char *classname[] = {
"StaticGray",
"GrayScale",
"StaticColor",
"PseudoColor",
"TrueColor",
"DirectColor"
};
switch( vclass ){
case PseudoColor:
break;
case TrueColor:
break;
case StaticGray:
break;
default:
fprintf(stderr,"Visualclass `%s' is not supported\n",classname[vclass]);
exit(1);
}
}
cmap = DefaultColormap( display, screen );
if( vclass == PseudoColor ){
int i;
XColor c;
ncols = NCOLS;
while(1){
result = XAllocColorCells( display, cmap, FALSE,
0,0,
pixels, ncols
);
if( result )
break;
if( ncols <= 8 ){
fprintf(stderr,"Cannot alloc %d color cells.\n",ncols);
exit(1);
}
ncols/=2;
}
init_color(pixels,ncols);
}
depth = DefaultDepth(display,screen);
if( vclass == PseudoColor ){
(void*)imagedata = malloc(gm_w*gm_h);
image = XCreateImage( display, visual, depth, ZPixmap, 0,
(char *)imagedata, gm_w, gm_h, 8, 0 );
image->bits_per_pixel = 8;
}
if( vclass == TrueColor ){
if( depth != 16 ){
fprintf(stderr,"not support %dbit depth - TrueColor\n",depth);
exit(1);
}
rshift = getshift(visual->red_mask,depth);
gshift = getshift(visual->green_mask,depth);
bshift = getshift(visual->blue_mask,depth);
rmaskl = getmasklen(visual->red_mask,depth);
gmaskl = getmasklen(visual->green_mask,depth);
bmaskl = getmasklen(visual->blue_mask,depth);
(void*)imagedata = malloc(gm_w*gm_h*(depth/8));
image = XCreateImage( display, visual, depth, ZPixmap, 0,
(char *)imagedata, gm_w, gm_h, 8, 0 );
image->bits_per_pixel = 16;
}
XSetWindowBackground( display, window, BlackPixel( display, screen ) );
pixmap = XCreatePixmap( display, window, gm_w, gm_h, depth );
XSetForeground(display, gc, BlackPixel(display,screen));
XFillRectangle( display, pixmap, gc, 0, 0, gm_w, gm_h );
XSetWindowBackgroundPixmap(display, window, pixmap);
XMapWindow( display, window );
XFlush( display );
x11_enable = 1;
}
static int buffline = 0;
/* 」テ」メ」ヤ、ホ・ッ・・「 */
void crtclr()
{
buffline = 0;
XSetForeground(display, gc, BlackPixel(display,screen) );
XFillRectangle( display, window, gc, 0, 0, gm_w, gm_h );
XFillRectangle( display, pixmap, gc, 0, 0, gm_w, gm_h );
}
/* 」テ」メ」ヤスミホマ */
void crtout( framebuf, xlen, y )
u_short *framebuf ;
int xlen ;
int y ;
{
u_short *p = framebuf;
u_long ccode;
int i;
if( y == 0 ){
XFlush(display);
}
if( vclass == PseudoColor ){
u_short r,g,b;
u_short c;
char *imgp = (char *)imagedata+(gm_w*y);
for( i=0; i<xlen; i++ ){
c = *p++;
r = (c & 0x07c0) >> ( 6 + 5 - rshift );
g = (c & 0xf800) >> (11 + 5 - gshift );
b = (c & 0x003e) >> ( 1 + 5 - bshift );
ccode = (r<<(gshift+bshift)) + (g<<(bshift)) + b;
*imgp++ = (u_char)pixels[ccode];
}
}
if( (vclass == TrueColor) && (depth == 16)){
u_short r,g,b;
u_short c;
u_short *imgp = (void *)imagedata+(gm_w*y*(depth/8));
u_long pixel;
for( i=0; i<xlen; i++ ){
c = *p++;
r = (c>> 6) & 0x1f;
g = (c>>11) & 0x1f;
b = (c>> 1) & 0x1f;
pixel = (r<<(rshift-(5-rmaskl))) +
(g<<(gshift-(5-gmaskl))) +
(b<<(bshift-(5-bmaskl)));
*imgp++ = (u_short)pixel;
}
}
/*#define BUFFERLINE 32 /**/
#ifdef BUFFERLINE
buffline++;
if( buffline >= BUFFERLINE ){
buffline = 0;
XPutImage(display, pixmap, gc, image, 0,y-BUFFERLINE+1,0,y-BUFFERLINE+1,
xlen, BUFFERLINE );
XCopyArea(display, pixmap, window, gc, 0,y-BUFFERLINE+1, xlen, BUFFERLINE, 0,y-BUFFERLINE+1,
}
#else
XPutImage(display, pixmap, gc, image, 0,y,0,y,
xlen, 1 );
XCopyArea(display, pixmap, window, gc, 0,y, xlen, 1, 0,y );
#endif
if( y == YPixel-1 ){
/* DONE */
}
}
void crtline( x1, y1, x2, y2 )
int x1, y1, x2, y2 ;
{
XSetForeground(display, gc, WhitePixel(display,screen) );
XDrawLine( display, pixmap, gc, x1, y1, x2-1, y2-1 );
XDrawLine( display, window, gc, x1, y1, x2-1, y2-1 );
}