V
V
Vitaly2014-02-19 22:40:13
C++ / C#
Vitaly, 2014-02-19 22:40:13

How to define transparent areas of a sprite using a mask?

We have two images: the
original one, with a black background: goomba1.gif
and its mask, with a white background: goomba1-mask.gif
I need to:
Get transparency instead of a black background so that if the sprite has black outlines, they are displayed. Because if you just perform a chroma key on a black background, then the contours will also be transparent. It is known that it is possible to get around the situation by painting the outlines SLIGHTLY lighter than black (for example, the background is #000000 and the outlines are #000001), but this is not suitable, because the project is planned for custom sprites. Transparency should appear exactly where the white background on the mask matches the black background on the original.
goomba1.gif+ goomba1-mask.gif= goomba1_2.png
No need for an alpha channel.
I use the SDL library in C ++ for work.
________________________________________________________________________
Although there is an assumption:
Color on the mask: should subtract the color from the alpha channel of the corresponding pixel in each image, but on the test software I saw that if a non-black color fell under the mask, it starts to mix with the background and dirt is obtained:
Experiment in the test software with the wrong mask:
Source and mask:
background-9.gifbackground-9m.gif
Result:
Scr_2014.02.19_23-32-28-560.jpg
UPD: After a while, I found out what algorithm for drawing such images is actually used:
For this, the bit mask algorithm was used .
those.:

  • First, a mask is drawn with a bitwise "AND" pixel-by-pixel with screen pixels.
  • Then the picture itself is drawn in the same place, but bitwise OR pixel by pixel with screen pixels

Such an algorithm was used in the program under study for the reason that VisualBasic 6, on which the latter was written, did not natively support the alpha channel and the rendering of transparent images. Why even in some examples and tutorials this method was used. In my engine, I use PNG images, and I transform the old ones by mixing them with a gray background, assigning an alpha channel depending on the sum of the pixels of the mask and the image itself.
You can also use the mask as an inverted alpha channel, but if the mask is wrong (users of the old engine very often made "lazy" masks - they copied the picture itself, just filling the background with white), then the picture turns out to be a curve with translucencies of different levels (which is strictly requires the correct mask to be used). Finally when I didimage converter with masks to PNG , also made a utility that fixes "lazy" masks automatically.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Teivaz, 2014-02-20
@Wohlstand

A shader in openGL solves this elementarily.
Pass both textures to the shader. From the first texture we take r, g, b. Then we calculate the brightness of the second texture at the point and put one minus this value in the transparency.

vec4 tex1 = texture2D(texture1, position);
vec4 tex2 = texture2D(texture2, position);
float luma2 = (tex2.r + tex2.g + tex2.b)/3.0;
gl_FragColor = vec4(tex1.r, tex1.g, tex1.b, 1.0 - luma2);

V
Vitaly, 2014-02-20
@xytop

And why not look for a white color on the mask and make the color in the same coordinates on the original transparent?
I didn't understand about contours.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question