Notifications
Clear all

[Closed] Bitmap alpha

Hi, I’d like to know how I can write pixels in the alpha channel of a bitmap? Also how do I add an alphachannel to a bitmap that has been opened with openbitmap()? Is it possible? Thanks

CML

7 Replies
 rdg

Hi Rivendale,

you can write pixels using the setPixels() method

This creates a bitmap and fills the alpha with noise:


theBitmap=bitmap 300 300
for x=1 to 300 do(
	for y=1 to 300 do(
		thePixel=getPixels theBitmap [x,y] 1
 		-- this adds a random alpha value to the existing pixel
		thePixel.alpha=random 0 255
		setPixels theBitmap [x,y] thePixel
	)
)
display theBitmap

As the openBitmap() method returns a bitmap, you should be able to use it the same.

regards,

Georg

rdg, thanks a lot! that works. Only if the bitmap already has an alpha though, now I need a way to add an alphachannel too. I’m opening a .tif image with openbitmap() that has no alphachannel so I need to add it in the process. Any ideas?

CML

 rdg

Hi Rivendale,

this one is quite a hack, but you can
1: read the image
2: create a temp image the size of the read image
3: copy the original pixels to the new image
4: manipulate the alpha

theBitmap_orig=openBitmap "d:\overview.bmp"
  -- create a new bitmap the size of the original one
  theBitmap=bitmap theBitmap_orig.width theBitmap_orig.height
  
  -- copy the original pixels into the new bitmap
  copy theBitmap_orig theBitmap
  -- manipulate the alpha
  for x=1 to theBitmap_orig.width do(
  	for y=1 to theBitmap_orig.height do(
  		-- thePixel=getPixels theBitmap_orig [x,y] 1
  		thePixel=getPixels theBitmap [x,y] 1
  		thePixel.alpha=random 0 255
  		setPixels theBitmap [x,y] thePixel
  	)
  )
  
  display theBitmap

It has some potential for optimizing.

This one is faster:
copy the whole bitmap at once than per pixel.
I modified the sample …

note also that bitmaps seem to be 0-index -> for x=0 to (width-1) …

Georg

rdg – Copying the bitmap should do it nicely. Thanks a lot man, I owe you one.

CML

hmm, the only problem now is that when I copy a bitmap that has been opened with openbitmap into another the alpha will default to pure white. I would rather want it to be set to black like it is when you create a bitmap manually. I might have to live with this though and invert the alpha later…unless somebody has got a solution. Stepping through the whole bitmap is of course an option but would take uneccesarily long time.

CML

 rdg

Hi Rivendale,

if you are using the alpha in a map you could also treat the alpha as extra bitmap in the opacity slot …

So may I ask you why you want to insert an alpha?

Georg

Thanks George for the suggestions, yes I believe I might go with an additional bitmap instead for the alpha. Why? Well maybe I’m silly but I don’t want to reveal it just yet. It’s good stuff though.

CML