[Closed] add one bitmap to another as alpha
I have bitmap_a and bitmap_b
both are 24 bit rgb images, but b is a grayscale
both show .hasAlpha=false, so they have no alpha channels.
I want bitmap_b to be the alpha channel of the bitmap_a.
getChannelAsMask looked promising but neither bitmap has channels.
i tried add a channel to bitmap_b like so
bitmap_b.channels=#(#materialColor)
getChannelAsMask bitmap_b #materialColor to: bitmap_a
but this crashed max…guess I don’t understand g-buffer channel data.
Paste bitmap seems to want an alpha channel to be pre existing, so i think i need to create some alpha channels where none exist…
any ideas?
my solution invoked creating a new image with all 4 channels
and pasting into it using pasteBitmap type:#function to composite each pixel.
--poplate variables
rgb_img=bitmap_a
alpha_img=bitmap_b
--make new blank rbga bitmap
rgba_img=bitmap rgb_img.width rgb_img.height color:(color 255 255 255 0)
--fn to copy pixel color1.value to color2.alpha, leaving rgb alone
fn mergeAlpha c1 p1 c2 p2 = (color c2.r c2.g c2.b c1.v)
--fn to copy pixel color1.rgb to color2.rgb, leavign alpha alone
fn mergeRGB c1 p1 c2 p2 = (color c1.r c1.g c1.b c2.a)
--paste sources into rgba using fns
pasteBitmap alpha_img rgba_img (box2 0 0 rgb_img.width rgb_img.height) [0,0] type:#function function:mergeAlpha
pasteBitmap rgb_img rgba_img (box2 0 0 rgb_img.width rgb_img.height) [0,0] type:#function function:mergeRGB
display rgba_img
I was going to post up a similar (the same really) solution, but dealing with 2 pastebitmaps on 512×512 images was very slow !!! actually it was the rendermap noise I was using to create the maps that was slow
You could use just one custom function for pasteBitmap and override the bitmap values instead of creating a new color to reduce time and memory usage.
(
fn SetBitmapAlpha imgRGB imgAlpha =
(
imgRGBA = copy imgRGB (bitmap imgRGB.width imgRGB.height)
fn SetAlpha c1 p1 c2 p2 = (c2.a = c1.v; c2)
pasteBitmap imgAlpha imgRGBA [0,0] [0,0] type:#function function:SetAlpha
return imgRGBA
)
)
Thanks Jorge!
I had tried a single function but in my initial testes it seemed I couldn’t paste into an alpha channel that did not exist.
I had not consider that setting the rgba color on each pixel during the pasteBitmp function call would effectively “add” an alpha channel.
You are right; you cant manipulate the alpha channel in a bitmap that does not have it.
Actually the alpha channel is added when you copy the 24bits bitmap onto the new one.
print imgRGB.hasAlpha -- false
imgRGBA = copy imgRGB (bitmap imgRGB.width imgRGB.height)
print imgRGBA.hasAlpha -- true
another Q about alpha channels…
I’m using mxs to scale my textures down with this function:
fn scaleBitmap theBitmap size =(
local tx = bitmaptexture bitmap:theBitmap
local scaledMap= rendermap tx size:([theBitmap.width*size,theBitmap.height*size]) filter:on display:off
return scaledMap
),
but rendermap appears to yield rgb ( not rgba) images.
I’ve been using the alpha composting stuff above to get around this
I s there some way have renderMap produce rgba bitmaps?
I have found this exact problem recently and posted a similar thread on the Area because I thought there was a problem with pasteBitmap() … but came to the same conclusion that there is a problem with renderMap.
It would be nice to know if this is a bug or can be set to get alpha into the rendermap() output.
well… let’s determine first what we need:
max bitmap with alpha as a final
texture file with alpha as a final (tga, png, tiff, …)
Although the RenderMap function outputs an RGBA 16bit per channel image, the source alpha of the texture seems to be lost or overwritten. Try this instead:
(
fn ScaleBitmap bmp factor =
(
scaledBitmap = bitmap (bmp.width*factor) (bmp.height*factor)
texture = bitmaptexture bitmap:bmp
rendermap texture into:scaledBitmap filter:on
)
)
You could also implement a .Net resizing (using the clipboard) which might be faster. I havent tested it against the RenderMap.