Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

DanielH

Loading RAW with colors

This topic is 5724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do i load a 128x128 raw image with colors? I use this code but I''m not sure it work corectly!
	cout << "Ange färgkarta: ";
	cin >> filename;

	cout << "Laddar färgkarta...\n";
	pFile = fopen( filename, "rb" );

	if ( pFile == NULL )	
	{
		cout << "Fel: Kunde inte öppna filen!\n";
		system("pause");
		return 0;
	}

	char colormap[128][128][3];
	fread( colormap, sizeof (char), sizeof colormap, pFile);

	error = ferror( pFile );

	if (error)
	{
		cout << "Fel: Ett fel uppstod vid läsning av filen!";
		system("pause");
		return 0;
	}

	fclose(pFile);
 

Share this post


Link to post
Share on other sites
Advertisement
you might not be reading in the right amount of data. it could be a pointer problem, i''m not sure

try this:


  
struct pixel
{
unsigned char r,g,b;
};


cout << "Ange färgkarta: "; cin >> filename; cout << "Laddar färgkarta...\n"; pFile = fopen( filename, "rb" ); if ( pFile == NULL ) { cout << "Fel: Kunde inte öppna filen!\n"; system("pause"); return 0; } struct pixel colormap[128*128]; fread( colormap, sizeof (pixel), 128*128, pFile); error = ferror( pFile ); if (error) { cout << "Fel: Ett fel uppstod vid läsning av filen!"; system("pause"); return 0; } fclose(pFile);


if you need to access the pixel data, the format for that would be:

pixel = (x + (y*image width))

Share this post


Link to post
Share on other sites
I think i made it work with loading colors but then i changed some stuff in the code. so the images (thats ment to be converted to game levels) could be diffrent size. For some reason the map array is always 4 bytes big....


  
//************************************

// SAVE TO DAT-FILE

//************************************


cout << "Ange filnamn för den färdiga filen: ";
cin >> filename;

cout << "Genererar fil...\n";

tileData * map = new tileData [width*height*128*128];
int counter;
int num;
for (int x1=0;x1<width;x1++)
{
for (int y1=0;y1<height;y1++)
{
for (int x2=0;x2<128;x2++)
{
for (int y2=0;y2<128;y2++)
{
counter++;
num = Ind(x1*128+x2,y1*128+y2);

map[num].height[0] = heightmap[Ind(x1*128+x2,y1*128+y2)];
map[num].height[1] = heightmap[Ind(x1*128+x2+1,y1*128+y2)];
map[num].height[2] = heightmap[Ind(x1*128+x2+1,y1*128+y2+1)];
map[num].height[3] = heightmap[Ind(x1*128+x2,y1*128+y2+1)];

// map[num].color = colormap[x1*128*y1*128 + Ind(x2,y2)];


map[num].texID = 0;
map[num].obj = 0;
}
}
}
}

cout << "Börjar skriva till fil...\n";

cout << "Vektorn 'map' är " << sizeof map << " bytes\n";
cout << "Counter: " << counter << "\n";

pFile = fopen( filename, "w+t" );

if ( pFile == NULL )
{
cout << "Fel: Kunde inte skapa filen!\n";
system("pause");
return 0;
}

fwrite( map, sizeof (char), sizeof (tileData)*width*height*128*128, pFile );

error = ferror( pFile );

if (error)
{
cout << "Fel: Ett fel uppstod vid skrivning till filen!\n";
system("pause");
return 0;
}

fclose(pFile);

cout << "Klart!\n";
system("pause");


[edited by - DanielH on January 20, 2003 10:27:32 AM]

Share this post


Link to post
Share on other sites
its'' worth mentioning here that RAW''s can come in RRGGBB and RGB
flavors.. make sure you know which one you''re loading

-eldee
;another space monkey;
[ Forced Evolution Studios ]


::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

Share this post


Link to post
Share on other sites
They can be BGR too, and black and white !


Noone knows why my other saving code doesnt work???
Do i create a dynamic array in the right way?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!