Archived

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

tuxx

Can I load a whole structure at once?

Recommended Posts

I've been working on a tga loader, and when I get to the part where it checks that the type is right, it says that the file is wrong:
    
int load(char *filename, texture &tex)
{
	FILE *fp;
	int  img_size;
	int  temp;
	
	if((fp = fopen(filename, "rb")) == NULL)
	{
		// error

		return 1;
	}
 
	// read the header

	fread(&tex.image.header, sizeof(tex.image.header), 1, fp);
 
	// check for the right type of image **ERRONEOUS**

	if(((tex.image.header.img_type != 2)  || (tex.image.header.img_type != 3))  ||
	   ((tex.image.header.img_type != 10) || (tex.image.header.img_type != 11)) ||
	   ((tex.image.header.bpp != 16) || (tex.image.header.bpp != 24) || (tex.image.header.bpp != 32)))
	{
		// error

		return 1;
	}

	// get the image data size and allocate memory for it

	img_size = tex.image.header.w * tex.image.header.h * (tex.image.header.bpp / 8);
	tex.image.data = (unsigned char*)malloc(sizeof(unsigned char)*img_size);

	// read the image data

	fread(&tex.image.data, sizeof(img_size), 1, fp);

	// swap BGR values to RGB

	for(int i=0; i<img_size; i += (tex.image.header.bpp / 8))
	{
		temp = tex.image.data[i];
		tex.image.data[i] = tex.image.data[i+2];
		tex.image.data[i+2] = temp;
	}

	fclose(fp);
 
	// generate a texture object

	glGenTextures(1, &tex.obj);
 
	// set texture filtering

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, tex.mag_filter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, tex.min_filter);
 
	// load the texture image

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.image.header.w,
				 tex.image.header.h, 0, GL_RGB, GL_UNSIGNED_BYTE,
				 tex.image.data);
 
	return 0;
}
    
I get the error that I'm loading the wrong type of image. When I log what type of image type it is, this is what I get: Do you think I need to read the header in variable by variable, instead of in one stroke? In case you're interested, here's my header structure:
struct tga_header
{
	unsigned char id_len;
	unsigned char cmap_type;
	unsigned char img_type;
	short int     cmap_fe_idx;
	short int     cmap_len;
	unsigned char cmp_entry_size;
	short int     x;
	short int	  y;
	short int	  w;
	short int	  h;
	unsigned char bpp;
	unsigned char img_desc;
};
  
I believe the header should be right because I checked it against the Truevision spec multiple times. Can anyone help me? Thanks! [edited by - tuxx on May 3, 2003 1:00:50 PM]

Share this post


Link to post
Share on other sites

if(((tex.image.header.img_type != 2) || (tex.image.header.img_type != 3)) ||
((tex.image.header.img_type != 10) || (tex.image.header.img_type != 11)) ||
((tex.image.header.bpp != 16) || (tex.image.header.bpp != 24) || (tex.image.header.bpp != 32)))
{
// error
return 1;
}


Looks like you''ve got some faulty logic here. You want to use && instead of || in this case. Notice that no matter what number is used, the bpp can''t be 16, 24 and 32 at the same time. Same with the img_type.

Share this post


Link to post
Share on other sites
You can and you should read the whole image in one stroke!

got it wrong with the testing of tex.image.header.img_type!


    
int a = anything;
//this is wrong:

if ( (a != 0) || (a != 1)... ){
//this will always run!!

};
//you need to write:

if ( (a != 0) && (a != 1)... ){
//to get error!!!!

}

//so it sohuld be:


if(((tex.image.header.img_type != 2) && (tex.image.header.img_type != 3)) && ((tex.image.header.img_type != 10) && (tex.image.header.img_type != 11)) && ((tex.image.header.bpp != 16) && (tex.image.header.bpp != 24) && (tex.image.header.bpp != 32))) {
// error

return 1;
}


practice your logics, and you sould be fine!

Melekor: by seconds!!! :-)

[edited by - uncutno on May 3, 2003 1:19:31 PM]

Share this post


Link to post
Share on other sites
Thanks a lot. I guess I just got cought up on ''if it doesn''t equal this OR this'', when it should be ''if it doesn''t equal this and it doesn''t equal this''. Now if I could just get the loader to work...

Share this post


Link to post
Share on other sites