Exemplo n.º 1
0
bool Image::loadTGA( TexImage* texture, LPCSTR filename )
{
	if ( filename == NULL )
		return FALSE;
		
	Header uTGAcompare = { 0,0,2,0,0,0,0,0};		//2为非压缩RGB格式		3  -  未压缩的,黑白图像
	Header cTGAcompare = { 0,0,10,0,0,0,0,0};		//10为压缩RGB格式

	TGAHeader header;
	FILE* file = fopen( filename, "rb" );
	if ( !file ){
		gkLogWarning("Openf file %s failed!\n", filename );
		return FALSE;
	}
	
	if ( fread( &header, 1, sizeof(TGAHeader), file ) != sizeof( TGAHeader ) ){		//读取TGA整个头结构体
		if ( file )
			fclose( file );
		gkLogWarning("Read data failed\n");
		return FALSE;
	}
	
	texture->width = header.width;
	texture->height = header.height;
	texture->bpp = header.bpp;

	if ( header.bpp == 32 )
		texture->imageType = GL_RGBA;
	else if ( header.bpp = 24 )
		texture->imageType = GL_RGB;
	else{
		gkLogWarning("Image type error!\n");
		return FALSE;
	}
		

	if ( memcmp( &uTGAcompare, &header.head, sizeof(header.head) )== 0 ){		//未压缩TGA
		texture->bCompressed = FALSE;
		if ( !loadUncompressedTGA( texture, file ) ){
			gkLogWarning("Load uncompressed TGA failed!\n");
			return FALSE;
		}
	}else if ( memcmp( &cTGAcompare, &header.head ,sizeof(header.head) ) == 0 ){	//压缩TGA
		texture->bCompressed = TRUE;
		if ( !loadCompressedTGA( texture, file ) ){
			gkLogWarning("Load compressed TGA failed!\n");
			return FALSE;
		}
	}else{
		gkLogWarning("Error TGA type!\n");
		return FALSE;
	}

	return TRUE;
}
int loadTGA(Texture *texture, char *filename)
{
	FILE * fTGA;
	TGAHeader tgaheader;

	GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header
	GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; // RLE Compressed TGA Header

	fTGA = fopen(filename, "rb");

	if(fTGA == NULL) // If the file didn't open...
	{
		fprintf(stderr, "Could not open texture file.\n");	// Display an error message
		return GL_FALSE;									// Exit function with "failure"
	}

	if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) == 0)	// Attempt to read 12 byte header from file
	{
		fprintf(stderr, "Could not read file header.\n");	// If it fails, display an error message 
		if(fTGA != NULL)									// Check to see if file is still open
		{
			fclose(fTGA);									// If it is, close it
		}
		return GL_FALSE;									// Exit with failure
	}

	if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)	// See if header matches the predefined header of 
	{															// an Uncompressed TGA image
		loadUncompressedTGA(texture, fTGA);			// If so, jump to Uncompressed TGA loading code
	}
	else if(memcmp(cTGAcompare, &tgaheader, sizeof(tgaheader)) == 0) // See if header matches the predefined header of
	{																 // an RLE compressed TGA image
		fprintf(stderr, "RLE compressed TGA files are not supported.\n");
	}
	else															// If header matches neither type
	{
		fprintf(stderr, "Unsupported image file format.\n");		// Unknown file type, or unknown TGA version
		fclose(fTGA);
		return GL_FALSE;											// Exit with failure
	}
	return GL_TRUE;													// All is well, return "success"
}
Exemplo n.º 3
0
SImageDataRead* ImageLoaderTGA::loadImageData()
{
    /* Check if the file has opened correct */
    if (!File_ || !File_->hasReadAccess())
        return 0;
    
    /* Allocate new texture RAW data & header buffer */
    SImageDataRead* texture = new SImageDataRead();
    
    /* Read the header */
    
    if (File_->readBuffer(&MainHeader_, sizeof(SHeaderTGA)) <= 0)
    {
        io::Log::error("Could not read TGA header");
        MemoryManager::deleteMemory(texture);
        return 0;
    }
    
    if (MainHeader_.ImageType == TGA_IMGDATA_RGB)
    {
        if (!loadUncompressedTGA(texture))
            return 0;
    }
    else if (MainHeader_.ImageType == TGA_IMGDATA_RGB_COMPRESSED)
    {
        if (!loadCompressedTGA(texture))
            return 0;
    }
    else
    {
        io::Log::error("TGA header type must be 2 or 10");
        MemoryManager::deleteMemory(texture);
        return 0;
    }
    
    // Flip image vertical if the origin is at the bottom
    if (!(MainHeader_.ImageDescriptor & 0x20))
        ImageConverter::flipImageVert(texture->ImageBuffer, texture->Width, texture->Height, texture->FormatSize);
    
    return texture;
}