Korge: What does the bitmap `premultiplied` variable do? - korge

In the Bitmap documentation, I noticed that there was this premultiplied variable in the bitmap. What does it mean and what does it do?

That property indicates how the Bitmap RGB (red, green, blue) components of each pixel are stored based on the alpha component. This is typically named premultiplied alpha.
premultiplied=true means that the RGB components are stored after being multiplied by the alpha component.
For example:
StoredRGBA=R*A,G*A,B*A,A
while premultiplied=false:
StoredRGBA=R,G,B,A
So this refers to how the storage should be interpreted regarding to the real components of each pixel.
Premultiplied alpha allows to perform blending operations much faster and in OpenGL it is typically required when the render buffer also has alpha.

Related

Small sample in opengl es 3, wrong gamma correction

I have a small sample, es-300-fbo-srgb, supposed to showing how to manage gamma correction in opengl es3.
Essentially I have:
a GL_SRGB8_ALPHA8 texture TEXTURE_DIFFUSE
a framebuffer with another GL_SRGB8_ALPHA8 texture on GL_COLOR_ATTACHMENT0 and a GL_DEPTH_COMPONENT24 texture on GL_DEPTH_ATTACHMENT
the back buffer of my default fbo is GL_LINEAR
GL_FRAMEBUFFER_SRGB initially disabled.
I get
instead of
Now, if I recap the display metho, this is what I do:
I render the TEXTURE_DIFFUSE texture on the sRGB fbo and since the source texture is in sRGB space, my fragment shader will read automatically a linear value and write it to the fbo. Fbo should contain now linear values, although it is sRGB, because GL_FRAMEBUFFER_SRGB is disabled, so no linear->sRGB conversion is executed.
I blit the content of the fbo to the default fbo back buffer (through a program). But since the texture of this fbo has the sRGB component, on the read values a wrong gamma operation will be performed because they are assumed in sRGB space when they are not.
a second gamma operation is performed by my monitor when it renders the content of the default fbo
So my image is, if I am right, twice as wrong..
Now, if I glEnable(GL_FRAMEBUFFER_SRGB); I get instead
The image looks like it have been too many times sRGB corrected..
If I, instead, leave the GL_FRAMEBUFFER_SRGB disabled and change the format of the GL_COLOR_ATTACHMENT0 texture of my fbo, I get finally the right image..
Why do I not get the correct image with glEnable(GL_FRAMEBUFFER_SRGB);?
I think you are basically right: you get the net effect of two decoding conversions where one (the one in your monitor) would be enough. I suppose that either your driver or your code breaks something so OpenGL doesn't 'connect the dots' properly; perhaps this answer helps you:
When to call glEnable(GL_FRAMEBUFFER_SRGB)?

OpenGL ES2.0 Load 8-bit image into Stencil Buffer

I am learning OpenGL ES2.0. I need a stencil buffer in my project.
What I am going to do:
1) Create a stencil buffer.
2) Load a 8-bit gray color image into this stencil buffer (which is also 8-bit per pixel).
3) The gray color image has different area (by setting different part a different value), so I can render for each area by changing the stencil test value.
I've searched for a lot time, still have no idea on how to load the image into stencil buffer.
So for the image above, I set stencil value as 1 for the blue area, and set 2 for the greeen area. How to implement this?
If your bitmap were 1 bit, just write a shader that either discards or allows pixels to proceed based on an alpha test, or use glAlphaFunc to do the same thing if under the fixed pipeline, and draw a suitable quad with the appropriate glStencilFunc.
If it's 8-bit and you genuinely want all 8 transferring to the stencil, the best cross-platform solutions I can think of either involve 8 draws — start from 0, use glStencilMask to isolate individual bits, set glStencilOp to invert, test for non-zero in the relevant bit in your shader — or just using the regular texture and implementing the equivalent of a stencil test directly in your shader.

What does it mean to change the color channel?

Does it mean to control the combination between an image and a color overlay applied to it depending on the color space used (RGB, RGBA, CMYK, Lab, Grayscale, HSL, HSLA)? Or does it mean to change the color layer used in combination with other layers to form the final image? (if so, what could be changed in what regard?).
RGB are abbreviations for three color channels (red, green and blue). They represent specific frequencies of light. Inside each color channel is a range of intensity and a level of saturation. This model of colors is commonly taught in school and is how most people understand colors and mixing them. A different way to represent colors is HSL which stands for Hue, Saturation and Level. Here the Hue is the frequency of the color, while the Saturation can be like the contrast level, and Level is the amount of black. HSL (A stands for Alpha or transparency) is actually a much more programmer centric way of working with color (although most programmers seem to learn the RGB Hex values for colors). There is a great website called Mothereffing HSL which lets you play with HSL values to better understand them. CMYK is for pigments (which mix differently than light) and is found on printers. Same basic idea as RGB just with Cyan Magenta Yellow and Black. Now because light and pigments don't mix the same way there is a lot of work devoted to converting one color system to another (so you can see on your screen what will eventually come out of your printer). These systems are not perfectly aligned however so the goal is to get acceptability close.
All of these colors when presented on a graph are called the color space.

OpenGL ES: altering alpha of render without glColor?

I have a few VBOs with color data, and I wish to draw them with varying transparency.
However, I realise that to do this normally, I'd have to either change the alpha value on the color array, or to use a shader, but I'm on OpenGL ES 1.1 so that's no go..
Is there any other way to change transparency of objects as a whole, without having to go glColor?

Transparency to text in GDI

i have created a Bitmap using GDI+.I am drawing text on to that bitmap using GDI Drawtext.Using Drawtext i am unable to apply tranparency.
Any help or code will be appreciated.
If you want to draw text without a background fill, SetBkMode(hdc,TRANSPARENT) will tell GDI to leave the background when drawing text.
To actually render the foreground color of the text with alpha... is going to be more complicated. GDI does not actually support alpha channels all that widely in its APIs. Outside of AlphaBlend actually all it does is preserve the channel. Its actually not valid to set the upper bits of a COLOREF to alpha values as the high byte is actually used for flags to indicate whether the COLOREF is (rather than an RGB value) a palette entry.
So, unfortunately, your only real way forward is to:
Create a 32bit DIBSection. (CreateDIBSection). This gives you an HBITMAP that is guaranteed to be able to hold alpha information. If you create a bitmap via one of the other bitmap creation functions its going to be at the device colordepth that might not be 32bpp.
DrawText onto the DIBSection.
When you created the DIBSection you got a pointer to the actual memory. At this point you need to go through the memory and set the alpha values. I don't think that DrawText is going to do anything to the alpha channel by itself at all. Im thinking a simple check of the RGB components of each DWORD of the bitmap - if theyre the forground color, rewrite the DWORD with a 50% (or whatever) alpha in the alpha byte, if theyre the background color, rewrite with a 100% alpha in the alpha byte. *
AlphaBlend the bitmap onto the final destination. AlphaBlend requires the alpha channel in the source to be pre-multiplied.
*
It might be sufficient to simply memset the DIBSection with a 50% alpha before doing the DrawText, and ensure that the BKColor is black. I don't know what DrawText might do to the alpha channel though. Some experimentation is called for.
SIMPLE and EASY solution:)
Had this problem, i tried to change alpha values and premultiply, but there was another problem - antialiased and cleartype fonts where not shown correctly (ugly edges). So what i did...
Compose your scene (bitmaps, graphics, etc.)
BitBlt required rectangle from this scene (same as your text rectangle, from the place where you want your text to be) to memory DC with compatible bitmap selected at 0,0 destination coordinates
Draw Your text to that rectangle in memory DC.
Now AlphaBlend that rectangle without AC_SRC_ALPHA in the BLENDFUNCTION and with desired SourceConstantAlpha from this memory DC back to your scene DC.
I think You got it :)
Hmmmm - trying to do same here - wondering - I see that when you create a dib section youi specify the "masks" that is a R,G,B (and alpha) mask.
IF and thats a big if it really does not alter the alpha chhannel, then you might specify the mask differently for two bitmap headers. ONe specifies thr RGB in the proper places, the other makes them all have their bits assigned to the alpha channel. (set the text color to white in this case) then render in two passes, one to load the color values, the other to load the alpha values.
???? anyway just musing :)
While this question is about making text semi-transparent, I had the opposite problem.
DrawText was making the text in my layered window (UpdateLayeredWindow) semi-transparent ... and I didn't want it to be.
Take a look at this other question ... since in the other question I post some code that you could easily modify ... and is almost exactly what Chris Becke suggests in his answer.
A limited answer for a specific situation:
If you have a graphic with alpha channel and you want to draw opaque text over a locally opaque background, first prepare your 32 bit bitmap with 32 bit brushes created with CreateDIBPatternBrushPt. Then scan through the bitmap bits inverting the alpha channel, draw your text as you normally would (including SetBkMode to TRANSPARENT), then invert the alpha in the bitmap again. You can skip the first inversion if you invert the alpha of your brushes.

Resources