Advertisement

A bitmap file loader in c++

Started by January 06, 2003 02:09 PM
0 comments, last by LongJohn 22 years, 1 month ago
hi, i´m using the linux GLC Nehe-Basecode. As i want to make my own project fully in c++,im trying to recode the bitmap loader wich i think is pure c. here´ s my problem: how to i check the filetype and filesizes WITHOUT using bfType and/or fread and so on.. the idea is to first make a little prog that only does the loadBMP() part. original code: /* make sure the file is there and open it read-only (binary) */ if ((file = fopen(filename, "rb")) == NULL) { printf("File not found : %s\n", filename); return 0; } if(!fread(&bfType, sizeof(short int), 1, file)) { printf("Error reading file!\n"); return 0; } /* check if file is a bitmap */ if (bfType != 19778) { printf("Not a Bitmap-File!\n"); return 0; } /* get the file size */ /* skip file size and reserved fields of bitmap file header */ fseek(file, 8, SEEK_CUR); /* get the position of the actual bitmap data */ if (!fread(&bfOffBits, sizeof(long int), 1, file)) { printf("Error reading file!\n"); return 0; } printf("Data at Offset: %ld\n", bfOffBits); My beginning: ifstream file; //This is my file-handle cout << "Texturname ? "; cin >> Texturename; strcpy (filename, Texturename); //make texturename "comparable" file.open(filename); //open it /*checking if file is there,if not exit prog */ if(!file) { cout << "could not find file texturename ! \n"; return 0; }
hi i came across the same thing... do it all in c++ .. i ''ll post a code for loading a tga.. in which he has in c and i put to c++ ... so you can use as a reference.


  bool isoX_LoaderTgaLoad (char* Filename,isoXloadertga &Tga){	unsigned int BitsPerPixel;	unsigned int BytesPerPixel;	unsigned int ImageSize;	unsigned char TgaHeader [12] = {0,0,2,0,0,0,0,0,0,0,0,0};	unsigned char TgaCompare[12];	unsigned char Header[6];	ifstream File (Filename,ios::binary);	if (File == NULL)		return false;	File.read (reinterpret_cast <char*> (TgaCompare),sizeof(TgaCompare));	if (File.gcount () != sizeof (TgaCompare) || memcmp (TgaCompare,TgaHeader,sizeof (TgaHeader)) != 0)	{		if (File != NULL)			File.close ();		return false;	}	File.read (reinterpret_cast <char*> (Header),sizeof (Header));	if (File.gcount () != sizeof (Header))	{		if (File != NULL)			File.close();		return false;	}	Tga.m_Width  = Header[1] * 256 + Header[0];	Tga.m_Height = Header[3] * 256 + Header[2];	if (Tga.m_Width <= 0 || Tga.m_Height <= 0 || (Header[4] != 24 && Header[4] != 32))	{		File.close ();		return false;	}	BitsPerPixel = Header[4];	BytesPerPixel = BitsPerPixel / 8;	ImageSize = Tga.m_Width * Tga.m_Height * BytesPerPixel;	Tga.m_Data = new unsigned char [ImageSize];	File.read (reinterpret_cast <char*> (Tga.m_Data),ImageSize);	if (File.gcount () != ImageSize)	{		if (Tga.m_Data != NULL)			delete [] Tga.m_Data;		File.close ();		return false;	}			for (int Loop = 0; Loop < (int) ImageSize; Loop +=  BytesPerPixel)	{		Tga.m_Data[Loop] ^= Tga.m_Data[Loop + 2] ^=		Tga.m_Data[Loop] ^= Tga.m_Data[Loop + 2]; 	}	if (BitsPerPixel == 24)	{Tga.m_Type = 0x1907;}	else	{Tga.m_Type = 0x1908;}	File.close ();	return true;}  
Metal Typhoon

This topic is closed to new replies.

Advertisement