Category : C Source Code
Archive   : FRAC151S.ZIP
Filename : TARGA.C

 
Output of file : TARGA.C contained in archive : FRAC151S.ZIP

/** targa.c


**/


#ifdef __TURBOC__
# pragma warn -par
#endif


#include
#include
#include
#include "targa.h"
#include "fractint.h"

#ifndef true
# define true 1
# define false 0
#endif

/************* ****************/

extern unsigned int _dataseg_xx;

extern void helptitle();
extern int LoadColorMap( void );
extern char loadPalette;

/************* ****************/

typedef unsigned char uchar;

/************* ****************/

void WriteTGA( int x, int y, int index );
int ReadTGA ( int x, int y );
void LineTGA ( uchar far * lineData, int y, int cnt );
void EndTGA ( void );
void StartTGA( int xDim, int yDim );

/************* ****************/

int xorTARGA;
int targaType;
unsigned far *tga16 = NULL; /* [256] */
long far *tga32; /* [256] */

/************* ****************/

static int xdots;
static int ydots;
static int initialized;

/************* ****************/

void (*DoPixel) ( int x, int y, int index );
void (*PutPixel)( int x, int y, int index );
void (*PutLine) ( uchar far * lineData, int y, int cnt );
unsigned (*GetPixel)( int x, int y );

/**************************************************************************/

void
TErase( color )
int color;
{
int cnt, max, seg;

max = targa->MaxBanks;
seg = targa->memloc;
for( cnt = 0; cnt < max; cnt += 2 ) {
outp( DESTREG, cnt );
outp( SRCREG, cnt + 1 );
erasesegment(seg,color); /** general.asm ?? **/
}
}


/**************************************************************************/

unsigned
Row16Calculate( unsigned line, unsigned x1 )
{
unsigned page, offset, pixel;

page = (line >> 5) & 15;
outp( DESTREG, page );
offset = ((line - (page << 5)) << 10) | (x1 << 1); /* calc the pixel offset */
return( offset );
}


/**************************************************************************/

void
PutPix16( int x, int y, int index )
{
unsigned far * ip;

/**************/
#ifdef __TURBOC__
ip = MK_FP( MEMSEG, Row16Calculate( y, x ) );
#else
FP_SEG(ip) = MEMSEG;
FP_OFF(ip) = Row16Calculate( y, x );
#endif
if( ! xorTARGA )
*ip = tga16[index];
else
*ip = *ip ^ 0x7fff;
}


/**************************************************************************/

unsigned
GetPix16( int x, int y )
{
unsigned pixel, index;
unsigned far * ip;
static unsigned last;
/**************/
#ifdef __TURBOC__
ip = MK_FP( MEMSEG, Row16Calculate( y, x ) );
#else
FP_SEG(ip) = MEMSEG;
FP_OFF(ip) = Row16Calculate( y, x );
#endif
pixel = *ip & 0x7FFF;
if( pixel == tga16[last] ) return( last );
for( index = 0; index < 256; index++ )
if( pixel == tga16[index] ) {
last = index;
return( index );
}
return( 0 );
}


/**************************************************************************/

unsigned
Row32Calculate( unsigned line, unsigned x1 )
{
int page, offset, pixel;

page = (line >> 4) & 31;
outp( DESTREG, page );
offset = ((line - (page << 4)) << 11) | (x1 << 2); /* calc the pixel offset */
return( offset );
}


/**************************************************************************/

void
PutPix32( int x, int y, int index )
{
long far * lp;
/**************/
#ifdef __TURBOC__
lp = MK_FP( MEMSEG, Row32Calculate( y, x ) );
#else
FP_SEG(lp) = MEMSEG;
FP_OFF(lp) = Row32Calculate( y, x );
#endif

/****
printf( "%lx\n", tga32[index] );
****/

if( ! xorTARGA )
*lp = tga32[index];
else
*lp = *lp ^ 0x00FFFFFF;
}


/**************************************************************************/

unsigned
GetPix32( int x, int y )
{
int index;
long pixel;
long far * lp;
static int last;

/**************/
#ifdef __TURBOC__
lp = MK_FP( MEMSEG, Row32Calculate( y, x ) );
#else
FP_SEG(lp) = MEMSEG;
FP_OFF(lp) = Row32Calculate( y, x );
#endif
pixel = *lp & 0x00FFFFFF;
if( pixel == tga32[last] ) return( last );
for( index = 0; index < 256; index++ )
if( pixel == tga32[index] ) {
last = index;
return( index );
}
return( 0 );
}


/**************************************************************************/

void
DoFirstPixel( int x, int y, int index )
{
PutPixel = DoPixel;
TSetMode( targa->mode | 1 );
TErase( 0 );
(*PutPixel)( x, ydots-y, index&0xFF );
TSetMode( targa->mode & 0xFFFE );
}


/***************************************************************************/

void
WriteTGA( int x, int y, int index )
{
TSetMode( targa->mode | 1 );
(*PutPixel)( x, ydots-y, index&0xFF ); /* fix origin to match EGA/VGA */
TSetMode( targa->mode & 0xFFFE );
}


/***************************************************************************/

int
ReadTGA( int x, int y )
{
int val;
TSetMode( targa->mode | 1 );
val = (*GetPixel)( x, ydots-y );
TSetMode( targa->mode & 0xFFFE );
return( val );
}


/***************************************************************************/

void
LineTGA( uchar far * lineData, int y, int cnt )
{
/**** not yet
TSetMode( targa->mode | 1 );
TSetMode( targa->mode & 0xFFFE );
return;
****/
}


/***************************************************************************/


void
EndTGA( void )
{
if( initialized ) {
GraphEnd();
initialized = 0;
}
return;
}


/***************************************************************************/

void
StartTGA( int xDim, int yDim )
{
int itemp;
int v1, v2, v3, index;
union REGS regs;

static char far runningontarga[]={"\
\nFRACTINT Running On TrueVision TARGA Card\n"};

/****************/
xdots = xDim;
ydots = yDim;

/****************/
if( initialized ) return;
/* PB, 900829, removed atexit(EndTGA) stuff - video.asm covers it */
initialized = 1;

/****************/
/** first, set up so default palette will be loaded if we're on a VGA **/
regs.x.ax = 0x1200;
regs.x.bx = 0x0031;
int86( 0x10, ®s, ®s );
regs.x.ax = 3; /* 'cause TARGA can live at 0xA000, we DO NOT want to */
int86( 0x10, ®s, ®s ); /* have an EGA/VGA in graphics mode!! */
helptitle();
putstring(2,16,7,runningontarga);

/****************/
/*** look for and activate card ***/
itemp = GraphInit( 16 );
if( itemp == 0 ) { /* all is ok */
VCenterDisplay( ydots + 1 );
targaType = targa->boardType;
}
else { /* no targa found */
putstring(4,0,15,"Could not find Targa Card\n ...aborting!");
exit( 1 );
}
if (tga16 == NULL)
if ( (tga16 = (unsigned far *)farmemalloc(512L)) == NULL
|| (tga32 = (long far *)farmemalloc(1024L)) == NULL) {
putstring(4,0,15,"Insufficient memory for Targa\n ...aborting!");
exit( 1 );
}
SetTgaColors();
if( targaType == 16 ) {
GetPixel = GetPix16;
DoPixel = PutPix16;
}
else {
GetPixel = GetPix32;
DoPixel = PutPix32;
}
PutPixel = DoFirstPixel; /* on first pixel --> erase */
if( yDim == 482 ) SetOverscan( 1 );
TSetMode( targa->mode & 0xFFFE );

/****************/
if (loadPalette == 0 && SetColorPaletteName("default") != 0)
exit( 1 ); /* stopmsg has already been issued */

/****************/
return;
}