Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sRGB color space problem? #264

Closed
Rydgel opened this issue Feb 6, 2017 · 11 comments

Comments

@Rydgel
Copy link

commented Feb 6, 2017

Hello,

It seems I have an issue to display my texture with the correct colours. It seems like everything I tried didn't change anything.

Here is a few excerpt of how I load the texture and draw it:

use opengl_graphics::{GlGraphics, Texture, TextureSettings};
use graphics::{Context, Transformed};
use graphics::draw_state::DrawState;
use graphics::Image;
// ...
let mut texture_settings = TextureSettings::new();
texture_settings.set_convert_gamma(true);
texture_settings.set_compress(true);
let texture = Texture::from_image(&img, &texture_settings);
// ...
// in the drawing method
Image::new().draw(&self.texture, &DrawState::new_alpha(), transf, gl);

So changing the set_convert_gamma value to false or true does nothing.
Creating the Window with .srgb(true) or .srgb(false) does nothing.

I'm on OS X. I'm using Rust nightly rustc 1.16.0-nightly (24055d0f2 2017-01-31)
Cargo packages:

[dependencies]
itertools = "0.5.9"
piston = "0.31.3"
piston_window = "0.64.0"
piston2d-graphics = "0.21.1"
pistoncore-sdl2_window = "0.40.0"
piston2d-opengl_graphics = "0.40.0"
rand = "0.3.15"
find_folder = "0.3.0"
image = "0.12.2"

And here is the texture image itself

scavengers_spritesheet

@bvssvni bvssvni added the draft label Feb 6, 2017

@bvssvni

This comment has been minimized.

Copy link
Member

commented Feb 6, 2017

Seems that none of the backends implements gamma conversion for textures.

@Rydgel

This comment has been minimized.

Copy link
Author

commented Feb 6, 2017

Maybe I can try to convert my texture with something like gamma_srgb_to_linear?

@bvssvni

This comment has been minimized.

Copy link
Member

commented Feb 6, 2017

Yes, you can do this manually.

I wonder why it shows wrong colors, because it seems your texture is in sRGB. This should be enabled here https://github.com/PistonDevelopers/opengl_graphics/blob/master/src/back_end.rs#L338, converting to linear color space in the shaders, and back when writing to the frame buffer.

@Rydgel

This comment has been minimized.

Copy link
Author

commented Feb 6, 2017

Yeah I saw that, it's weird. I even tried with other images, same problem.

@Rydgel

This comment has been minimized.

Copy link
Author

commented Feb 6, 2017

I even tried several Window libraries, currently using SDL2Window, but tried GLFW and Glut with the same result.

@Rydgel

This comment has been minimized.

Copy link
Author

commented Feb 6, 2017

Doing a gamma_srgb_to_linear conversion on my texture fixes the issue for me.
It's weird since it seems that the reverse function is done inside the drawing function.

@adfaure

This comment has been minimized.

Copy link

commented Jun 6, 2017

Hello,
I have the same issue here.

I will do it manually too, but will it be fixed in a future release ?
Thanks a lot !

@expenses

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2017

@Rydgel @adfaure Would you mind sharing the code that you used to perform the conversion? I'm having trouble with this step.

Edit: this is what I've got so far:

fn to_f(value: u8) -> f32 {
    value as f32 / 255.0
}

fn to_u(value: f32) -> u8 {
    (value * 255.0) as u8
}

fn load_texture(filename: &str, texture_settings: &TextureSettings) -> Texture {
    let mut image = image::open(filename).unwrap().to_rgba();

    for pixel in image.pixels_mut() {
        let conv = gamma_srgb_to_linear([to_f(pixel[0]), to_f(pixel[1]), to_f(pixel[2]), to_f(pixel[3])]);
        pixel.data = [to_u(conv[0]), to_u(conv[1]), to_u(conv[2]), to_u(conv[3])];
    }

    Texture::from_image(&image, texture_settings)
}
@expenses

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2017

@Rydgel So I was on the right track! Thank you very much!

@Aerolivier

This comment has been minimized.

Copy link

commented Jan 31, 2018

Am I right in thinking this can be solved by tagging the texture data with the GL_SRGB8 or GL_SRGB8_ALPHA8 formats? ( according to https://www.khronos.org/opengl/wiki/Image_Format#sRGB_colorspace )

As far as I understand, quantising the linear values to 8 bits (like suggested as a workaround) leads to a loss of integrity in the image ( as demonstrated at http://tulrich.com/webgl/rgb/linear_vs_srgb.html ).

I'd like to fix this, but I'm not familiar with the code base. Any pointers or should I just start getting stuck in?
Edit: Sorry for retracting the above, but I'm now considering a different course of action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.