Пример #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!								

}
EImageReaderStatus ImageReader::LoadTGA(const char* filename, unsigned char** buffer, int* sizeX, int* sizeY, int* bpp, int* nChannels)
{
	FILE* pfile = NULL;
	EImageReaderStatus result = E_IMAGEREADER_SUCCESS;
	errno_t err = 0;
	unsigned char header[12];
	unsigned char UncompressedTGASigniture[12] = {0,0,2,0,0,0,0,0,0,0,0,0}; 
	unsigned char CompressedTGASigniture[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; 

	*buffer = NULL;
	err = fopen_s(&pfile, filename, "rb");

	if (err)
	{
		printf("Error opening image file: %s\n", filename);
		return E_IMAGEREADER_ERROR;
	}

	if(!pfile)
	{
		return E_IMAGEREADER_FILENOTFOUND;
	}

	if(fread(header, sizeof(header), 1, pfile) == 0)
	{
		return E_IMAGEREADER_ERROR;
	}
	
	if(memcmp(UncompressedTGASigniture, header, sizeof(header))==0)
	{
		result = LoadUncompressedTGA(buffer, sizeX, sizeY, bpp, nChannels, pfile);
	}
	else if(memcmp(UncompressedTGASigniture, header, sizeof(header))==0)
	{
		//TODO: load compressed tga
	}
	else //unrecognised header signiture.
	{
		
		fclose(pfile);
		return E_IMAGEREADER_ERROR;
	}
	
	fclose(pfile);

	return result;	
}
Пример #5
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
	}
Пример #6
0
bool TGA::LoadFile(wchar_t * filename)				// Load a TGA file
{
	FILE * fTGA;												// File pointer to texture file
	_wfopen_s(&fTGA,filename, L"rb");									// Open file for reading
	
	ADD_LOG_Q("Read TGA file.");
	ADD_LOG_Q(filename);
	if(fTGA == NULL)											// If it didn't open....
	{
		//MessageBox(NULL, "Could not open texture file", "ERROR", MB_OK);	// Display an error message
		TGAerror=TGA_ERROR_NOT_OPEN_FILE;
		ADD_LOG_Q("Can not open file.","#0000FF");
		return false;														// Exit function
	}

	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 
		TGAerror=TGA_ERROR_NOT_READ_FILE_HEADER;
		ADD_LOG_Q("Can not file head error.","#FF0000");
		if(fTGA != NULL)													// Check to seeiffile 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
	if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)				// See if header matches the predefined header of 
	{																		// an Uncompressed TGA image
		isRAM=LoadUncompressedTGA(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, 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
		ADD_LOG_Q("Can not read CompressedTGA.","#FF0000");
		TGAerror=TGA_ERROR_FILE_HEADER_TYPE;
		fclose(fTGA);
		return false;																// Exit function
	}
	return true;															// All went well, continue on
}
Пример #7
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
}
Пример #8
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
}
Пример #9
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 */
}