Archived

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

loading an image

This topic is 5272 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

hy there i want to use *.tga files for my textures...so i want to write my own picture loader the *.tga file specs dont look hard to understand but my problem is...whats the best and fast way to store some picture in memory? so that iam able to present the data to opengl and opengl will show it i dont want to see any sourccode about tga files but if anybody knows some easy implementations of a *.bmp loader or something, give it to me bye

Share this post


Link to post
Share on other sites
quote:
so i want to write my own picture loader the *.tga file

quote:
i dont want to see any sourccode about tga files

You really don''t know what you want, do you?
quote:
but if anybody knows some easy implementations of a *.bmp loader or something, give it to me

Look up the NeHe tutorials (http://nehe.gamedev.net/). He''s got a working implementation of a BMP texture loader in his tuts.

Share this post


Link to post
Share on other sites
I''m assuming you''re using OpenGL since Direct3D has the D3DX library to load images for you.

My advice would be to load the image file header into a matching structure (you may have to use #pragma pack to make sure your structure is tightly aligned), allocate a buffer big enough to hold the image data in, read in the image data, pass it to opengl using the a format compatible with that in the image header, then free your buffer.


Why you shouldn''t use iostream.h - ever! | A Good free online C++ book

Share this post


Link to post
Share on other sites
kingston
********

i already have the file format specification

sisapete
********

>pass it to opengl using the a format compatible with that in
>the image header

where can i look up wich formats r compatible with tga? because i also want to use the alpha value...

extracting the image data is no problem, i know where to find and how to determine the length...

bye

Share this post


Link to post
Share on other sites
so it''s easy to pass the data to GL use the BMP format.

windows has the header structs of the BMP frmt already defined
BMPFILEHEADER & BMPINFOHEADER

just read a BMPFILEHEADER and a BMPINFOHEADER from the start of the file, then seek to the fileheader.bioffBits position and read the data. one tricky thing is that bmp has the images rows aligned to 4bytes. that means that if the image is 255X255 each column in fact has 256 bytes the last one beeing discardable.

its extremely easy. the aux lib of GL has a loading func for dibs (auxDIBimageLoaded or smth like that)

the TGA format is quite similar only the headers differ.

/*ilici*/

Share this post


Link to post
Share on other sites
ahh..i understood...i only have to make a header, that iam able to provide opengl with information like height,width,pixel depth, using alpha or not etc., so that open gl is able to switch in the correct display mode. and i dont need a complete file format description right?

bye

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Targa is usually stored bottom-to-top, just like OpenGL wants it (t == 0 is bottom; t == 1 is top). OpenGL needs 4-byte-aligned data for optimal performance. Targa stores data in BGRA order, which is optimal to upload as-is.


file = fopen( "my_stupid_image.tga", "rb" );

unsigned short header[ 9 ];
fread( header, 2, 9, file );
assert( (header[ 8 ] & 0x3f) == 32 );

unsigned int width = header[ 6 ];
unsigned int height = header[ 7 ];
assert( !(width & (width-1)) );
assert( !(height & (height-1)) );

unsigned int * data = new unsigned int[ width*height ];
fread( data, 4, width*height, file );
fclose( file );

// I forget the exact order of the arguments
glTexImage2D( 0, GL_RGBA8, width, height, GL_BGRA8, GL_UNSIGNED_CHAR, data );
delete[] data;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The GL_BGRA8 should be plain GL_BGRA because the size is given by the GL_UNSIGNED_BYTE argument. The first argument should be GL_RGBA8, as indicated, to tell GL what components matter and what size to use per component.

Share this post


Link to post
Share on other sites
anonymouse
**********

this is the "very old" tga file format, isnt it? my documentation says something different about the header

how do i the 4 byte alignment? is this just a linker setting? or is there something that i have to put into my sourcecode (i would prefer the second method)

bye

Share this post


Link to post
Share on other sites
I've never heard about the 4 byte alignment before, but it should just happen automatically for you since your textures need to be powers of two in width and height (you know that right?).

Also, you may have to swap the red and blue channels of your texture around to get the colours to come out right, but that's of course work to be done AFTER you get anything at all being sent to OpenGL.


Why you shouldn't use iostream.h - ever! | A Good free online C++ book


[edited by - siaspete on July 6, 2003 5:54:50 PM]

Share this post


Link to post
Share on other sites
sisapete
********
i guess the 4byte alignment is for faster accessing memory (the thing with the adresses) on intel machines...but u waste 1 byte per pixel

and yes i know that the texture need to be a power of 2

anonymous
*********

ah...sorry i overread the short int... headersize is 18 bytes...and yes tga files (32bit) will be stored in bgra format


thx for help

bye

Share this post


Link to post
Share on other sites
quote:
Original post by apocalexiz
sisapete
********
i guess the 4byte alignment is for faster accessing memory (the thing with the adresses) on intel machines...but u waste 1 byte per pixel



Ah, this is not true. I wrote a TGA loader a while ago, and 24-bit images are stored tightly packed.


Why you shouldn''t use iostream.h - ever! | A Good free online C++ book

Share this post


Link to post
Share on other sites