SOIL: new lightweight image loading lib

Started by
129 comments, last by GenPFault 13 years, 10 months ago
Quote:Original post by lonesock
Regarding the NPOT textures, what if the user does not specify SOIL_FLAG_POWER_OF_TWO or SOIL_FLAG_MIPMAPS and the texture is not already a POT, then SOIL could use GL_TEXTURE_RECTANGLE_ARB automatically?


I didn't read this carefully enough the first time round - one needs to be able to specify that the texture be loaded as GL_TEXTURE_RECTANGLE, even if the image is already Po2. This is because Po2 textures use pixel-based texture coordinates rather than normalised, so if one loads the wrong type by mistake, the GUI texturing blows up.

Quote:Original post by Merick Zero
When using the SOIL_LOAD_RGBA flag, how about adding another argument that can let you specify a color value for a transparency mask for images that don't normally have transparency like bmp?


I don't like this idea. You want to increase the library complexity to make up for a deficiency in the art pipeline - it offers much more control if you use photoshop/Gimp to generate the alpha channel (and then use a format that support alpha, i.e. png), than if the programmer does it from a color/pixel value.

Basically, it is a classic case of separation of responsibility: SOIL's job is to load images into byte buffers/textures, and the artist's job is to ensure the images contain the necessary data.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement
OK, today's version of SOIL:

* adds SOIL_FLAG_TEXTURE_RECTANGLE (thanks swiftcoder!)
* now forces SOIL_FLAG_POWER_OF_TWO if the user did not specify SOIL_FLAG_TEXTURE_RECTANGLE and if the hardware does not support ARB_texture_non_power_of_two (thanks Starfox!)

I only tested the SOIL_FLAG_TEXTURE_RECTANGLE on one image, so please let me know if I messed anything up. Note that if the user specifies SOIL_FLAG_TEXTURE_RECTANGLE and the hardware does not support it the function will return a 0 (error) because of the difference in addressing (pixels versus normalized [0,1]). If you load a cubemap, SOIL_FLAG_TEXTURE_RECTANGLE is automatically disabled. If SOIL_FLAG_TEXTURE_RECTANGLE is used successfully, mipmaps and texture repeats are disabled.

@Merick Zero: I'm sorry, I couldn't think of an easy way to add in that functionality without making the interface kludgy. The closest I came up with was the old Win2k (I think) icon hack where it would take the color in the corners as the transparency color. I think the best solution would be to just wrap SOIL_load_image() and SOIL_create_OGL_texture() in a function with your "sample to alpha" code sandwiched in between.

Thanks to everyone for their input and feedback!
Quote:Original post by lonesock
OK, today's version of SOIL:

* adds SOIL_FLAG_TEXTURE_RECTANGLE (thanks swiftcoder!)
* now forces SOIL_FLAG_POWER_OF_TWO if the user did not specify SOIL_FLAG_TEXTURE_RECTANGLE and if the hardware does not support ARB_texture_non_power_of_two (thanks Starfox!)

I only tested the SOIL_FLAG_TEXTURE_RECTANGLE on one image, so please let me know if I messed anything up. Note that if the user specifies SOIL_FLAG_TEXTURE_RECTANGLE and the hardware does not support it the function will return a 0 (error) because of the difference in addressing (pixels versus normalized [0,1]). If you load a cubemap, SOIL_FLAG_TEXTURE_RECTANGLE is automatically disabled. If SOIL_FLAG_TEXTURE_RECTANGLE is used successfully, mipmaps and texture repeats are disabled.


Line 961 (SOIL.c):
Quote:opengl_texture_target = SOIL_TEXTURE_RECTANGLE_ARB;

Needs to be changed to:
Quote:opengl_texture_target = opengl_texture_type = SOIL_TEXTURE_RECTANGLE_ARB;


To get this to work with any image. I am not entirely clear why there is both a target and a type argument here, but presumably it is needed for the cubemaps?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Hi,

I was using SFML for image loading (it's Laurent Gomila's library), but since it didn't have any functions for loading number of mipmaps for DDS files, he gave me the link to your library, which is one of the simplest I've never tried :p.

But I have a few questions :

- Which method do you use for generating mipmaps ? gluBuild2DMipmaps, GL_MIPMAP generation or your own method ?
- Is there any way to get the width and height of a texture ?
- I have a problem for loading DDS files (while jpg, gif... there isn't any problem). The function is really simple :

[source="cpp"]void Texture2D::LoadCompressedTextureFromFile (const std::string & fileName_){	// We check if the hardware supports compressed texture	if (sf::OpenGLCaps::CheckExtension ("GL_ARB_texture_compression"))	{		GLuint imageID = SOIL_load_OGL_texture (fileName_.c_str(), SOIL_LOAD_AUTO, id, SOIL_FLAG_DDS_LOAD_DIRECT);		if (imageID == 0)			std::cerr << "Failed to load texture : " << fileName_ << std::endl;	}	else		std::cerr << "Your hardware doesn't support GL_ARB_texture_compression" << std::endl;}


And I've effectively got an error. The DDS file is generated with "The Compressonator" (It's an ATI software I think), the mipmap were generated with it, it's a power of two texture, so I don't understand why it doesn't work.
Quote:Original post by Bakura
...
- Which method do you use for generating mipmaps ? gluBuild2DMipmaps, GL_MIPMAP generation or your own method ?
- Is there any way to get the width and height of a texture ?
- I have a problem for loading DDS files (while jpg, gif... there isn't any problem). The function is really simple :
...

Hi!

* mipmaps are done in software in SOIL, each pixel is the average of the 4 pixels in the mipmap level above. If you request mipmaps then I force the textures to a power of 2 so the mipmapping is really simple.

* SOIL doesn't report the size when directly uploading images to OpenGL textures, so you have two options:
1) use the OpenGL function glGetTexLevelParameter once the texture object is bound (which it is right after a call to SOIL's upload functions)
2) use a 2-step process where you call SOIL_load_image and get the image data (including size), then call SOIL_create_OGL_texture with the image data you just loaded.

* a few things about the source snippet and DDS loading:
1) what is "id"? If that isn't initialized to a value of 0 (a.k.a. SOIL_CREATE_NEW_ID), then SOIL will attempt to load the image into an existing ID. If "id" is a random number this will most likely fail.
2) SOIL checks for the necessary OpenGL extensions automatically. If DXT isn't supported, for example, SOIL will decompress the image then upload it as an uncompressed texture.
3) if the ID comes back 0, you can use the following code to see what SOIL was thinking:
std::cerr << SOIL_last_result() << std::endl;
4) it could very well be a bug in my DDS loading code. If you would like to send me the offending DDS file via email I'd be happy to debug SOIL using it. My email address is the same as my profile name, at gmail.com

Thanks for giving SOIL a try! [8^)
Quote:* mipmaps are done in software in SOIL, each pixel is the average of the 4 pixels in the mipmap level above. If you request mipmaps then I force the textures to a power of 2 so the mipmapping is really simple.

Ok :)

Quote:
* SOIL doesn't report the size when directly uploading images to OpenGL textures, so you have two options:
1) use the OpenGL function glGetTexLevelParameter once the texture object is bound (which it is right after a call to SOIL's upload functions)
2) use a 2-step process where you call SOIL_load_image and get the image data (including size), then call SOIL_create_OGL_texture with the image data you just loaded.

I didn't know glGetTexLevelParameter, that's exactly what I wanted, thanks ;)

Quote:* a few things about the source snippet and DDS loading:
1) what is "id"? If that isn't initialized to a value of 0 (a.k.a. SOIL_CREATE_NEW_ID), then SOIL will attempt to load the image into an existing ID. If "id" is a random number this will most likely fail.
2) SOIL checks for the necessary OpenGL extensions automatically. If DXT isn't supported, for example, SOIL will decompress the image then upload it as an uncompressed texture.
3) if the ID comes back 0, you can use the following code to see what SOIL was thinking:
std::cerr << SOIL_last_result() << std::endl;
4) it could very well be a bug in my DDS loading code. If you would like to send me the offending DDS file via email I'd be happy to debug SOIL using it. My email address is the same as my profile name, at gmail.com


I'm using my own id number generated with glGenTextures, and I just don't want you create another ID :p. And my id is not 0, and it works with other format (jpg, bmp, gif...) so this is not a problem with the id.

But your function effectively return 0, so it fails, and when I call SOIL_last_result() it tells me : "Unable to open file".

I send you the file :)

Hi, I linked in SOIL and included it in files where I use SOIL functions, but I still get this during the compilation process:


make client
gcc -Wall -O2 -std=c99 -c -o renderinggl.o renderinggl.c
gcc -lm -lSDL -lSDL_image -lSDL_gfx -lSDL_ttf -lGL -lSOIL client.o utils.o clientutils.o rendering.o options.o input.o renderinggl.o renderingsw.o utils.h clientutils.h client.h rendering.h options.h input.h renderingsw.h -o client
renderinggl.o: In function `init_gl':
renderinggl.c:(.text+0x397): undefined reference to `SOIL_load_OGL_texture'
collect2: ld returned 1 exit status
make: *** [client] Error 1
No updates since 2007, this sucks. :D

j/k, great work. :)
To lonesock,

the "inline void check_for_GL_errors()" in SOIL.c stops it from compiling, so I removed the inline part. Doesn't inline have to be in the header?


To the corn guy,

try moving -lSOIL to the end of the command, sometimes gcc keeps a list of unresolved symbols so you might need that after the offending .o

[Edited by - Boder on February 6, 2008 8:21:53 PM]
@Boder: I'm mostly a C++ guy, I hadn't realized the C "inline" keyword was so complicated. I'll try to look into this, but the inlining is not critical. Thanks for helping cornmander.

@Grantax: thanks!

@cornmander: it really does just sound like a linking-order issue, as Boder said.

@Bakura: I never heard the final result after our emails...did it work out OK?

To everyone, I'm interested in supporting loading of Radiance RGBE files into standard RGBA formats (8-bit or DXT5). Right now loading an HDR file rescales it to a LDR RGB image via simple scaling. I propose an alternate loading path (with all the same options) for loading HDR files into RGBA in one of 3 formats:

(range is in base 10 orders of magnitude, monotonic refers to the results of bilinear sampling)
RGBE: RGB * pow( 2.0, A - 128.0 )
range ~76 (orders of magnitude), error 0.2% to 0.4% throughout, NOT monotonic
(note: if this format was monotonic, it would be the clear winner, but since it isn't I personally would go with RGB/(A*A))

RGBdivA: RGB / A
range ~4.8, error 0% to 0.4% at the high end, IS monotonic

RGBdivA2: RGB / (A*A)
range ~7.2, error 0% to 0.77% at high end, IS monotonic
(note: the error is <= 0.43% if limited to 6.6 orders of magnitude, my personal favorite)

Does anyone have any opinions or advice on this proposed feature?

This topic is closed to new replies.

Advertisement