engine/EngineImage.c
/*	Writes RGB or RGBA data to a TGA file

	returns TRUE if successful

	TODO: support BGRA and BGR formats (since opengl can return them, and we don't have to swap)
*/


support BGRA and BGR formats (since opengl can return them, and we don't have to swap)
support BGRA and BGR formats ( since opengl can return them and we don ' t have to swap )
bool Image_WriteTGA(char *name, uint8_t *data,int width,int height,int bpp,bool upsidedown)
{
	int	handle, i, size, temp, bytes;
	char pathname[PLATFORM_MAX_PATH];
	uint8_t	header[TARGAHEADERSIZE];
	if (!plCreateDirectory(com_gamedir)) 
		Sys_Error("Failed to create directory!\n");
	sprintf (pathname, "%s/%s", com_gamedir, name);
	handle = Sys_FileOpenWrite (pathname);
	if (handle == -1)
		return false;
	memset(&header, 0, TARGAHEADERSIZE);
	header[2]	= 2; 
	header[12]	= width&255;
	header[13]	= width>>8;
	header[14]	= height&255;
	header[15]	= height>>8;
	header[16]	= bpp; 
	if (upsidedown)
		header[17] = 0x20; 
	bytes = bpp/8;
	size = width*height*bytes;
	for (i=0; i<size; i+=bytes)
	{
		temp = data[i];
		data[i] = data[i+2];
		data[i+2] = temp;
	}
	Sys_FileWrite (handle, &header, TARGAHEADERSIZE);
	Sys_FileWrite (handle, data, size);
	Sys_FileClose (handle);
	return true;
}
bool Image_WriteTGA ( char * name , uint8_t * data , int width , int height , int bpp , bool upsidedown ) { int handle , i , size , temp , bytes ; char pathname [ PLATFORM_MAX_PATH ] ; uint8_t header [ TARGAHEADERSIZE ] ; if ( ! plCreateDirectory ( com_gamedir ) ) Sys_Error ( " \n " ) ; sprintf ( pathname , " " , com_gamedir , name ) ; handle = Sys_FileOpenWrite ( pathname ) ; if ( handle == -1 ) return false ; memset ( & header , 0 , TARGAHEADERSIZE ) ; header [ 2 ] = 2 ; header [ 12 ] = width & 255 ; header [ 13 ] = width >> 8 ; header [ 14 ] = height & 255 ; header [ 15 ] = height >> 8 ; header [ 16 ] = bpp ; if ( upsidedown ) header [ 17 ] = 0x20 ; bytes = bpp / 8 ; size = width * height * bytes ; for ( i = 0 ; i < size ; i += bytes ) { temp = data [ i ] ; data [ i ] = data [ i + 2 ] ; data [ i + 2 ] = temp ; } Sys_FileWrite ( handle , & header , TARGAHEADERSIZE ) ; Sys_FileWrite ( handle , data , size ) ; Sys_FileClose ( handle ) ; return true ; }
Writes RGB or RGBA data to a TGA file
returns TRUE if successful
Writes RGB or RGBA data to a TGA file returns TRUE if successful
//if we've switched to a nonexistant gamedir, create it now so we don't crash // uncompressed type // pixel size //upside-down attribute // swap red and blue bytes