/// this is being phased out due to packing issues see TGAImage
instead
typedef PACKED struct tgaImageHeader_tag {
/* Offset
Meaning */
PACKED byte IdSize; /* 0 id field size
at end of header */
PACKED byte ColMapType; /* 1 0=>no color
map, 1=>color map */
PACKED byte TypeCode; /* 2 1=>type 1,
2=>type 2 images,... */
PACKED word ColMapOrigin; /* 3 index of first
color map entry */
PACKED word ColMapLength; /* 5 number of
entries in color map */
PACKED byte ColMapBits; /* 7 number of
bits/color map entry */
PACKED word XOrigin; /* 8 lower left
corner of image */
PACKED word YOrigin; /* 10 lower left
corner of image */
PACKED word Width; /* 12 width of image
in pixels */
PACKED word Height; /* 14 height of
image in pixels */
PACKED byte BitsPerPixel; /* 16 see note 1:
below */
PACKED byte Descriptor; /* 17 see note 2:
below */
PACKED byte FMield[256]; /* 18 image
identification field */
/* */
/* Note 1: The
"BitsPerPixel" field depends of the type of */
/* Targa file.
For Targa type 1 images, it is the */
/* number of
bits per pixel index. For a true- */
/* color Targa
image this is 24, since pixels need */
/* 1 byte each
for r, g, and b. */
/* Note 2:
Individual bits have meaning here. For example, */
/* toggling bit
5 flips the image upside down. */
} GCC_PACK tgaImageHeader;
typedef unsigned short tRGB;
/**
* Split a 565 RGB tuple to individual components 888
*
* @param rgb - 565 value to split
* @param *r - pointer to 8 bit R storage
* @param *g - pointer to 8 bit G storage
* @param *b - pointer to 8 bit B storage
*
* @returns - none
*/
void SplitRGB565(tRGB rgb , unsigned char *r,
unsigned char *g, unsigned char *b )
{
int ir,ig,ib;
ir = ( rgb & ( 0x1f << 11 ) ) >> 11;
ig = ( rgb & ( 0x3f << 5 ) ) >> 5;
ib = rgb & 0x1f ;
*r = ir << 3 ; *g = ig << 2 ; *b = ib << 3 ;
return ;
}
inline tRGB Peek(int x,int y ) {
return *(m_FrameBuffer + x + ( y * m_width ) );
};
/**
* Save a framebuffer to 24 TGA file (currently expects 565
framebuffer)
*
* @param name - save filename
* @param w - width
* @param h - height
* @param ptr - pointer to framebuffer to save
*
* @return none
*
* @todo convert to allow any framebuffer style
*/
void fm_Display::WriteTGA( const char *filename, int w,int h,void *ptr)
{
tgaImageHeader tgaHeader;
int j;
char name[512];
fm_prolog( FM_PRI_LOW,"WriteTGA");
fm_strcpy( name, filename );
fm_strcat( name, ".tga");
fm_assert( name );
fm_assert( ptr );
tgaHeader.IdSize = 0;
tgaHeader.ColMapType = 0;
tgaHeader.TypeCode = 2;
tgaHeader.ColMapOrigin = 0;
tgaHeader.ColMapLength = 0;
tgaHeader.ColMapBits = 0;
tgaHeader.XOrigin = 0;
tgaHeader.YOrigin = 0;
tgaHeader.Width = w;
tgaHeader.Height = h;
tgaHeader.BitsPerPixel = 24;
tgaHeader.Descriptor = 1<<5;
unsigned char *ch;
ch = (unsigned char *) fmMALLOC( ( w * h ) * 3 );
if( ch == FM_NULL )
return;
int x,y;
FM_FILE *fp = ::fm_fopen( name, "wb" );
if( fp ) {
j = 0;
for( y=0 ; y < h ; y++ )
{
for( x=0 ; x < w ; x++ )
{
unsigned char r,g,b;
tRGB rgb ;
rgb = Peek(x,y);
// Read pixel
and convert from 565 to 888 RGB
SplitRGB565( rgb , &r,&g,&b );
ch[j++]
= b ;
ch[j++]
= g ;
ch[j++]
= r ;
}
}
::fm_fwrite((void*)&tgaHeader,sizeof(tgaImageHeader)-256,1,fp);
::fm_fwrite( &ch[0], (w*h)*3,1,fp);
::fm_fclose( fp );
}
fm_free( ch );
}
////////////////////////////////////////////////////////////////////////////////////////////
BMP version
#define BMP_HEADER_SIZE ( 54 )
int WriteBMP( const
char *filename,
int width,
int height,void *data )
{
unsigned char
*buf = (unsigned
char *)data;
char name[512];
fm_strcpy( name,
filename );
fm_strcat( name,
".bmp");
unsigned char
h[ BMP_HEADER_SIZE
];
FM_FILE *fp;
int i;
int line_bytes;
line_bytes = (( width
* 2 + 2 ) / 4 ) * 4;
for ( i
= 0; i < BMP_HEADER_SIZE;
i++ ) {
h[ i ]
= 0;
}
h[ 0 ] = 'B';
h[ 1 ] = 'M';
*( int * )( h
+ 2 ) = ( int )( line_bytes
* height * 2 + BMP_HEADER_SIZE
);
*( int * )( h
+ 10 ) = ( int ) BMP_HEADER_SIZE;
*( int * )( h
+ 14 ) = ( int ) BMP_HEADER_SIZE
- 14;
*( int * )( h
+ 18 ) = ( int ) width;
*( int * )( h
+ 22 ) = ( int ) height;
*( short * )( h
+ 26 ) = ( short ) 1;
*( short * )( h
+ 28 ) = ( short ) 16;
if (( fp
= fm_fopen( name,
"wb" )) == FM_NULL
) {
return ( -1 );
}
fm_fwrite( h,
1, BMP_HEADER_SIZE, fp
);
for ( i
= height - 1; i
>= 0; i-- ) {
unsigned char
dummy[ 3 ] = { 0, 0, 0 };
fm_fwrite( buf
+ width * 2 * i,
1, width * 2, fp
);
if ( line_bytes
- width * 2 > 0 ) {
fm_fwrite( dummy,
1, line_bytes - width
* 2, fp );
}
}
fm_fclose( fp
);
return ( 0 );
}
//