Greyscale image with 1 byte per pixel

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
pgimeno
Party member
Posts: 3558
Joined: Sun Oct 18, 2015 2:58 pm

Greyscale image with 1 byte per pixel

Post by pgimeno »

I want to draw a greyscale image taken from an ImageData object that has 1 byte per pixel (pixel format "r8").

I want to show it as greyscale on screen, but when I use lg.newImage(imgdata) and then draw the resulting Image object, it appears red.

I can obviously use a shader that copies the red channel to the green and blue channels to get the result I want, but is there an easier way? E.g. some way to create the Image object from three copies of the r channel, or some way to force the red to appear as white somehow?
User avatar
slime
Solid Snayke
Posts: 3136
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Greyscale image with 1 byte per pixel

Post by slime »

A shader is the only way. Modern GPUs support color component swizzles when creating a texture which would let you do that, but not all GPUs support it and love doesn't provide a way to use that capability at the moment.

Here's an example of a basic shader:

Code: Select all

vec4 effect(vec4 vcolor, Image tex, vec2 tc, vec2 pc)
{
    return vcolor * vec4(Texel(tex, tc).rrr, 1.0));
}
User avatar
pgimeno
Party member
Posts: 3558
Joined: Sun Oct 18, 2015 2:58 pm

Re: Greyscale image with 1 byte per pixel

Post by pgimeno »

OK, thanks slime!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 9 guests