 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
Seagaetcm
Joined: 30 Jun 2007 Posts: 10 Location: Peking, China
|
Posted: Fri Jul 06, 2007 12:42 pm Post subject: Problem puzzle me. Reading Tga file and caculate data size.. |
|
|
I wrote code to read tge files. the code like that:
| Code: |
#pack(1)
struct TgaFileHead
{
... // some value define
short w;
short h;
... // some value define
}
#pack()
// sizeof(TgaFileHead) = 18
// in function:
int DataSize = 0;
TgaFileHead Head;
fread(&f, 18, 1, file); // read structure succeed. w is 256 and h is 256
DataSize = Head.w * Head.h;
|
But the result of DataSize is zero. I have no idea for that. Please help!!!
btw:sorry for my terrible english. |
|
| Back to top |
|
 |
be2003
Joined: 20 Apr 2006 Posts: 144
|
Posted: Fri Jul 06, 2007 3:12 pm Post subject: |
|
|
idk... i must be really out of it right now.
but it seems to me that you are reading into a structure called "f" instead of "Head", which would be the appropriate structure. since you are not reading anything to "Head" then all of the values in "Head" are still zero
that is why it is returning zero... try this...
| Code: |
int DataSize = 0;
TgaFileHead Head;
fread(&Head, sizeof(TgaFileHead), 1, file); //this is where your mistake was before
DataSize = Head.w * Head.h;
|
good luck! _________________ - be2003
blog |
|
| Back to top |
|
 |
Seagaetcm
Joined: 30 Jun 2007 Posts: 10 Location: Peking, China
|
Posted: Fri Jul 06, 2007 7:37 pm Post subject: |
|
|
Sorry, I make a mistake in the post. I do not copy the code from file, so the code in my program really is
| Code: |
fread(&Head, sizeof(TgaFileHead), 1, file);
|
I debug the Head value by GDB, it is correct. but the next line
| Code: |
DataSize = Head.w * Head.h;
|
...DataSize is zero. |
|
| Back to top |
|
 |
dot_blank

Joined: 28 Sep 2005 Posts: 498 Location: Brasil
|
Posted: Sat Jul 07, 2007 11:35 am Post subject: |
|
|
do you have
| Code: |
typedef struct TgaFileHead ;
// if not then
/////// TgaFileHead Head ;
// would need to be
struct TgaFileHead Head ; |
or maybe more of your code is needed to find bug ;) _________________ 10011011 00101010 11010111 10001001 10111010 |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Sat Jul 07, 2007 1:10 pm Post subject: |
|
|
Sure you haven't defined DataSize as short by mistake?
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Seagaetcm
Joined: 30 Jun 2007 Posts: 10 Location: Peking, China
|
Posted: Sat Jul 07, 2007 3:06 pm Post subject: |
|
|
I had tested the code at VS2005, it is correct.
here it is the code:
| Code: |
#pragma pack(1)
struct TGAHeader
{
char idlength;
char colourmaptype;
char datatypecode;
short int colourmaporigin;
short int colourmaplength;
char colourmapdepth;
short int x_origin;
short int y_origin;
short width;
short height;
char bitsperpixel;
char imagedescriptor;
};
#pragma pack()
GLuint loadTGA(const char *filename, bool mipmaps)
{
int size = 0;
FILE *f = fopen(filename,"rb");
if (f == NULL)
return 0;
TGAHeader h;
fread(&h,18,1,f);
if (h.datatypecode != 2)
return 0;
size = h.width * h.height;
GLint bppformat;
GLint format;
//int bypp = h.bitsperpixel / 8;
if (h.bitsperpixel == 24) {
size *= 3;
format = GL_RGB;
bppformat = GL_RGB8;
} else if (h.bitsperpixel == 32) {
size *= 4;
format = GL_RGBA;
bppformat = GL_RGBA8;
} else return 0;
unsigned char *buf = new unsigned char[size];
fread(buf,size,1,f);
fclose(f);
GLuint t;
glGenTextures(1,&t);
glBindTexture(GL_TEXTURE_2D, t);
if (mipmaps) {
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps (GL_TEXTURE_2D, bppformat, h.width, h.height, format, GL_UNSIGNED_BYTE, buf);
} else {
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, h.width, h.height, 0, format, GL_UNSIGNED_BYTE, buf);
}
delete[] buf;
return t;
}
|
I compiled this code with devkitpro and pspgl.
thanks for help. |
|
| Back to top |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Sun Jul 08, 2007 12:21 am Post subject: |
|
|
GCC doesn't support the #pragma afaik. Hence you'd have to resolve to a #define that checks which compiler is used and pack your structures depending on that:
VS:
| Code: |
#pragma pack(1)
struct TGAHeader
{
char idlength;
char colourmaptype;
char datatypecode;
short int colourmaporigin;
short int colourmaplength;
char colourmapdepth;
short int x_origin;
short int y_origin;
short width;
short height;
char bitsperpixel;
char imagedescriptor;
};
#pragma pack()
|
GCC:
| Code: |
struct __attribute__ ((__packed__)) TGAHeader
{
char idlength;
char colourmaptype;
char datatypecode;
short int colourmaporigin;
short int colourmaplength;
char colourmapdepth;
short int x_origin;
short int y_origin;
short width;
short height;
char bitsperpixel;
char imagedescriptor;
};
|
_________________ <Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki
Alexander Berl |
|
| Back to top |
|
 |
ufoz
Joined: 10 Nov 2005 Posts: 86 Location: Tokyo
|
Posted: Sun Jul 08, 2007 12:58 am Post subject: |
|
|
Something like this, I guess...
| Code: | #ifdef WIN32
#pragma pack(push)
#pragma pack(1)
#define __PACKED__
#else
#define __PACKED__ __attribute__((packed))
#endif
struct bitmap_header {
uint16 id;
uint32 size;
uint32 reserved;
uint32 offset;
uint32 headersize;
uint32 width;
uint32 height;
uint16 planes;
uint16 bpp;
uint32 compression;
uint32 imagesize;
uint32 xdpi;
uint32 ydpi;
uint32 colors;
uint32 important_colors;
} __PACKED__;
#ifdef WIN32
#pragma pack(pop)
#endif
#undef __PACKED__
|
|
|
| Back to top |
|
 |
Seagaetcm
Joined: 30 Jun 2007 Posts: 10 Location: Peking, China
|
Posted: Sun Jul 08, 2007 2:04 pm Post subject: |
|
|
My gcc version is "psp-gcc (GCC) 4.1.2 devkitPSP release 11 (PSPDEV 20060507)"
I tried struct __attribute__ ((__packed__)) TGAHeader, the value is
(gdb) p h
$1 = {idlength = 0 '\0', colourmaptype = 0 '\0', datatypecode = 2 '\002',
colourmaporigin = 0, colourmaplength = 0, colourmapdepth = 0 '\0',
x_origin = 0, y_origin = 256, width = 256, height = 8224,
bitsperpixel = -1 '', imagedescriptor = -1 ''}
and #pragma pack(1) is really work, the value is
(gdb) p h
$1 = {idlength = 0 '\0', colourmaptype = 0 '\0', datatypecode = 2 '\002',
colourmaporigin = 0, colourmaplength = 0, colourmapdepth = 0 '\0',
x_origin = 0, y_origin = 0, width = 256, height = 256,
bitsperpixel = 32 ' ', imagedescriptor = 32 ' '}
I dont know why... |
|
| Back to top |
|
 |
Seagaetcm
Joined: 30 Jun 2007 Posts: 10 Location: Peking, China
|
Posted: Sun Jul 08, 2007 4:10 pm Post subject: |
|
|
I think there it is not problem with data alignment. I modify code as it:
| Code: |
unsigned char unCompressHeader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned char tgaHeader[12];
unsigned char header[6];
fread( &tgaHeader, 1, sizeof(tgaHeader), f );
//read only uncompressed TGA's
if( memcmp( unCompressHeader, tgaHeader, sizeof(unCompressHeader)) != 0 )
{
fclose( f );
return false;
}
// Read Image info
fread( header, 1, sizeof(header), f );
// Calculate and save the Width & Height of Image
int iImageWidth = header[1] * 256 + header[0];
int iImageHeight = header[3] * 256 + header[2];
unsigned int c = iImageWidth * iImageHeight;
|
iImageWidth and iImageHeight is 256, but the value c is still zero. I debug the code with gdb, step with asm. the last line asm code like this:
(gdb) info r
| Code: |
zero at v0 v1 a0 a1 a2 a3
R0 00000000 0008ff00 00000100 00000100 09f3eb9a 09f3eb98 00000000 00000001
t0 t1 t2 t3 t4 t5 t6 t7
R8 09f3eb98 089b2540 09f3eb94 089b253c 00000000 00000e00 08973508 00088600
s0 s1 s2 s3 s4 s5 s6 s7
R16 089b0ae0 09f3ee44 00000001 09f3eef0 0000000d 00000013 deadbeef deadbeef
t8 t9 k0 k1 gp sp s8 ra
R24 0000012f 0000013c 09f3ef00 00000000 089a8870 09f3eb68 09f3eb68 0892a324
sr lo hi bad cause pc
60088613 00000000 00000009 00010004 10000024 0892a34c
fsr fir
00000e00 00003351
|
the code:
319 unsigned int c = iImageWidth * iImageHeight;
1: x/i $pc 0x892a34c <_Z7loadTGAPKcb+300>: lw v1,8(s8)
1: x/i $pc 0x892a350 <_Z7loadTGAPKcb+304>: lw v0,4(s8)
1: x/i $pc 0x892a354 <_Z7loadTGAPKcb+308>: mult v1,v0
1: x/i $pc 0x892a358 <_Z7loadTGAPKcb+312>: mflo v0
after this step ,registers v0 changed:
(gdb) info r
| Code: |
zero at v0 v1 a0 a1 a2 a3
R0 00000000 0008ff00 deadbeef 00000100 09f3eb9a 09f3eb98 00000000 00000001
t0 t1 t2 t3 t4 t5 t6 t7
R8 09f3eb98 089b2540 09f3eb94 089b253c 00000000 00000e00 08973508 00088600
s0 s1 s2 s3 s4 s5 s6 s7
R16 089b0ae0 09f3ee44 00000001 09f3eef0 0000000d 00000013 deadbeef deadbeef
t8 t9 k0 k1 gp sp s8 ra
R24 0000012f 0000013c 09f3ef00 00000000 089a8870 09f3eb68 09f3eb68 0892a324
sr lo hi bad cause pc
60088613 deadbeef deadbeef 00010004 10000024 0892a35c
fsr fir
00000e00 00003351
|
the last line
1: x/i $pc 0x892a35c <_Z7loadTGAPKcb+316>: sw v0,0(s8)
save the v0 value 0xdeadbeef into c. I dont know "mflo v0" means what but this step cause c error. |
|
| Back to top |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Tue Jul 10, 2007 6:08 am Post subject: |
|
|
It moves the value from the lo register after the mul (which contain the lower 32bits of the multiplied value) into the destination register. So this doesn't make much sense. _________________ <Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki
Alexander Berl |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|