Archived

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

DanielH

Loading RAW with colors

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
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