예제 #1
0
bool LoadTGA(Texture* texture, const char* filename)
{
	FILE* fTGA;
	fTGA = fopen(filename, "rb");

	if(fTGA == NULL)//If it didn't open....
	{
		return false;
	}

	if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) == 0) //Attempt to read 12 byte header from file
	{
		if(fTGA != NULL) //Check to seeiffile is still open
		{
			fclose(fTGA); //If it is, close it
		}
		return false;
	}

	if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0) //See if header matches the predefined header of 
	{															//an Uncompressed TGA image
		LoadUncompressedTGA(texture, filename, 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
		LoadCompressedTGA(texture, filename, fTGA);						//If so, jump to Compressed TGA loading code
	}
	else																// If header matches neither type
	{
		fclose(fTGA);
		return false;
	}

	return true;
}
예제 #2
0
	bool IMAGE::LoadTGA (const char* szFile)
	{
		FILE* pFile = fopen (szFile, "rb");

		if (!pFile)
			return (false);

		// Read the header of the TGA, compare it with the known headers for compressed and uncompressed TGAs
		unsigned char ucHeader[18];
		fread (ucHeader, sizeof (unsigned char) * 18, 1, pFile);

		while (ucHeader[0] > 0)
		{
			--ucHeader[0];

			unsigned char temp;
			fread (&temp, sizeof (unsigned char), 1, pFile);
		}

		m_iImageWidth = ucHeader[13] * 256 + ucHeader[12];
		m_iImageHeight = ucHeader[15] * 256 + ucHeader[14];
		m_iBytesPerPixel = ucHeader[16] / 8;


		// check whether width, height an BitsPerPixel are valid
		if ((m_iImageWidth <= 0) || (m_iImageHeight <= 0) || ((m_iBytesPerPixel != 1) && (m_iBytesPerPixel != 3) && (m_iBytesPerPixel != 4)))
		{
			fclose (pFile);
			return (false);
		}

		// allocate the image-buffer
		m_Pixels.resize (m_iImageWidth * m_iImageHeight * 4);


		// call the appropriate loader-routine
		if (ucHeader[2] == 2)
		{
			LoadUncompressedTGA (pFile);
		}
		else 
		if (ucHeader[2] == 10)
		{
			LoadCompressedTGA (pFile);
		}
		else
		{
			fclose (pFile);
			return (false);
		}

		fclose (pFile);
		return (true);
	}
예제 #3
0
//TGA Loader
bool LoadTGA(const char *fname)
{
	
												// TGA image data

	FILE * fTGA;										// File pointer to texture file
	fTGA = fopen(fname, "rb");							// Open file for reading

	if(fTGA == NULL)											// If it didn't open....
	{
		std::stringstream warning;
		warning << "Could not open texture file: " << fname;
		//possibly do something other than a messagebox here. 
		MessageBox(NULL, warning.str().c_str(), "ERROR", MB_OK);	// Display an error message
		return false;														// Exit function
	}

	//we need to see what type of TGA we are loading. 
	if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) == 0)					// Attempt to read 12 byte header from file
	{
		MessageBox(NULL, "Could not read file header", "ERROR", MB_OK);		// If it fails, display an error message 
		if(fTGA != NULL)													// Check to seeiffile is still open
		{
			fclose(fTGA);													// If it is, close it
		}
		return false;														// Exit function
	}

	if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)				// See if header matches the predefined header of 
	{																		// an Uncompressed TGA image
		LoadUncompressedTGA( fname, 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
		LoadCompressedTGA( fname, fTGA);							// If so, jump to Compressed TGA loading code
	}
	else																	// If header matches neither type
	{
		MessageBox(NULL, "TGA file be type 2 or type 10 ", "Invalid Image", MB_OK);	// Display an error
		fclose(fTGA);
		return false;																// Exit function
	}

	

	return true;		//fingers crossed!								

}
예제 #4
0
	bool Image2D::LoadTGA(char* filename)							// Load a TGA file
	{
		FILE * fTGA;												// File pointer to texture file
		fTGA = fopen(filename, "rb");								// Open file for reading

		if(fTGA == NULL)											// If it didn't open....
		{
			cgl::Cout("Could not open file");
			return false;														// Exit function
		}

		if(fread(&this->header, sizeof(this->header), 1, fTGA) == 0)			// Attempt to read 12 byte header from file
		{
			cgl::Cout("Could not read file header");
			if(fTGA != NULL)													// Check to seeiffile is still open
			{
				fclose(fTGA);													// If it is, close it
			}
			return false;														// Exit function
		}

		if(memcmp(this->uTGAcompare, &this->header, sizeof(this->header)) == 0)				// See if header matches the predefined header of 
		{																		// an Uncompressed TGA image
			LoadUncompressedTGA(filename, fTGA);						// If so, jump to Uncompressed TGA loading code
		}
		else if(memcmp(this->cTGAcompare, &this->header, sizeof(this->header)) == 0)		// See if header matches the predefined header of
		{																		// an RLE compressed TGA image
			LoadCompressedTGA(filename, fTGA);							// If so, jump to Compressed TGA loading code
		}
		else																	// If header matches neither type
		{
			cgl::Cout("TGA file is not compressed or uncompressed (type 2 or 10)");
			fclose(fTGA);
			return false;														// Exit function
		}
		glGenTextures(1, &this->ID);
		glBindTexture(GL_TEXTURE_2D, this->ID);
		glTexImage2D(GL_TEXTURE_2D, 0, this->bpp / 8, this->width, this->height, 0,this->type, GL_UNSIGNED_BYTE, this->dataTGA);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		if (this->dataTGA)						// If Texture Image Exists ( CHANGE )
		{
			free(this->dataTGA);					// Free The Texture Image Memory ( CHANGE )
		}
		return true;															// All went well, continue on
	}
예제 #5
0
bool Texture::LoadTGA(std::string filename)
{
	FILE * fTGA;															// File pointer to texture file
	fTGA = fopen(filename.c_str(), "rb");									// Open file for reading

	if(fTGA == NULL)														// If it didn't open....
	{
		std::cout<< "Could not open texture file: " << filename <<std::endl;// Display an error message
		return false;														// Exit function
	}

	if(fread(&tgaheader, sizeof(Texture::TGAHeader), 1, fTGA) == 0)			// Attempt to read 12 byte header from file
	{
		std::cout<< "Could not read file header: " << filename <<std::endl;	// 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 false;														// Exit function
	}

	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};					// Compressed TGA Header

	if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)				// See if header matches the predefined header of
	{																		// an Uncompressed TGA image
		LoadUncompressedTGA(filename.c_str(), 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
		LoadCompressedTGA(filename.c_str(), fTGA);									// If so, jump to Compressed TGA loading code
	}
	else																	// If header matches neither type
	{
		std::cout<< "TGA file be type 2 or type 10: " << filename <<std::endl;	// Display an error
		fclose(fTGA);
		return false;														// Exit function
	}
	return true;															// All went well, continue on
}
예제 #6
0
bool LoadTGA(Texture * texture, char * filename)				// Load a TGA file
{
	FILE * fTGA;												// File pointer to texture file
	fTGA = fopen(filename, "rb");								// Open file for reading
	//fTGA = fopen("textura_perete.tga","rb");

	if(fTGA == NULL)											// If it didn't open....
	{		
//		MessageBoxA(NULL, "Could not open texture file", filename, MB_OK);	// Display an error message
		return false;														// Exit function
	}

	if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) == 0)					// Attempt to read 12 byte header from file
	{
//		MessageBoxA(NULL, "Could not read file header", filename, MB_OK);		// If it fails, display an error message 
		if(fTGA != NULL)													// Check to seeiffile is still open
		{
			fclose(fTGA);													// If it is, close it
		}
		return false;														// Exit function
	}

	if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)				// See if header matches the predefined header of 
	{																		// an Uncompressed TGA image
		LoadUncompressedTGA(texture, filename, 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
		LoadCompressedTGA(texture, filename, fTGA);							// If so, jump to Compressed TGA loading code
	}
	else																	// If header matches neither type
	{
//		MessageBoxA(NULL, "TGA file be type 2 or type 10 ", filename, MB_OK);	// Display an error
		fclose(fTGA);
		return false;																// Exit function
	}
	return true;															// All went well, continue on
}
예제 #7
0
BOOL LoadTGA(Texture * texture,const char * filename)				/* Load a TGA file */
{
	FILE * fTGA;													/* File pointer to texture file */
	fTGA = fopen(filename, "rb");									/* Open file for reading */

	if(fTGA == NULL)												/* If it didn't open.... */
	{
		printf("Error could not open texture file");				/* Display an error message */
		return False;												/* Exit function */
	}

	if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) == 0)			/* Attempt to read 12 byte header from file */
	{
		printf("Error could not read file header");					/* If it fails, display an error message */
		if(fTGA != NULL)												/* Check to seeiffile is still open */
		{
			fclose(fTGA);												/* If it is, close it */
		}
		return False;													/* Exit function */
	}

	if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)	/* See if header matches the predefined header of */
	{																/* an Uncompressed TGA image */
		LoadUncompressedTGA(texture, filename, 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 */
		LoadCompressedTGA(texture, filename, fTGA);						/* If so, jump to Compressed TGA loading code */
	}
	else																/* If header matches neither type */
	{
		printf("Error TGA file be type 2 or type 10 ");				/* Display an error */
		fclose(fTGA);
		return False;												/* Exit function */
	}
	return True;														/* All went well, continue on */
}