Image Masking Mystery

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Image Masking Mystery

Post by RCozens » Thu Aug 05, 2021 8:03 am

Hello,

I apparently need a better understanding of the format of image properties and masking protocol. Q1: Are alphaData and maskData synonymous? I am guessing not from the results below and I need to understand how they interact.

I am attempting to render all pixels in an image that match the background color of a particular object transparent. I scan the imageData and compare the RGB values of each pixel with the RGB values of the backgroundColor, and when they match I change the value of the pixel's alpha byte to numToChar(0). Since that did not produce the result I expected, I am now also setting the corresponding byte in the maskData and alphaData respectively to 0. But the changes I make to the imageData and maskData properties seem to get lost when I reset them. Here's a section of the handler I'm trying to debug:

answer "Char 41 =" && (charToNum(char 41 of imagePixels)) && "Mask 11 =" && (charToNum(char 11 of maskPixels)) && \
"Alpha 11 =" && (charToNum(char 11 of alphaPixels))
set the imageData of image id imageIdNumber to imagePixels
if maskData then
set the maskData of image id imageIdNumber to maskPixels
set the alphaData of image id imageIdNumber to alphaPixels
end if
answer "Char 41 =" && charToNum(char 41 of the imageData of image id imageIdNumber) && "Mask 11 =" && \
(charToNum(char 11 of the maskData of image id imageIdNumber)) && "Alpha 11 =" && (charToNum(char 11 of alphaPixels))

The same answer statement returns the values 255 255 255 before the scan
The first answer statement above returns the values 0 0 0 after the scan
The second answer statement above returns the values 255 255 0 after the image properties are reset ???

Q2: What could cause these results?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Image Masking Mystery

Post by bn » Thu Aug 05, 2021 5:14 pm

Hi Rob,

welcome to the forum.
I suppose you are Rob Cozens, well known since long time from the use-list.

If you want to change the transparency of a pixel you can just change the correspoinding pixel in the alphaData and set the alphaData accordingly.
MaskData adjusts automatically.
And you do not have to set the imagedata if you did not change it.

To make the alphaData of a pixel set its value to numToByte(0)

Code: Select all

put numToByte(0) into tTransparent
Please note that charToNum and numToChar are deprecated since 7.0. Although they do still work they are very slow compared to numToByte and byteToNum.

I never found the first byte of the 4 bytes of imageData for 1 pixel useful nor informative. I just ignore it although it is supposed to be an alpha value.

The second answer statement above returns the values 255 255 0 after the image properties are reset ???
If you see the result you want then it would not worry me. The only thing that surprises me is 255 for maskData. That should be 0 too.

If you set the mask data instead of the alphaData in your special case you should see the same result. Since complete transparency in alphaData translates into complete transparency of maskData.
As you probably already know maskdata contains only on and off (255 or 0) whereas alphadata is gradual from 0 = complete transparent to 255 complete opaque

One thing that is very annoying when loading alphaData or maskData into a variable and you debug the handler with loaded mask/alphadata the debugger gets slow or completely unresponsive in current versions of LC. That is caused by a bug in the display of the debugger that checks for display width of a char to decide when to stop displaying since e.g. a lot of byte 0s in a row do not have a width to stop the debugger to attempt to display them.
https://quality.livecode.com/show_bug.cgi?id=19157
Please save often...

Kind regards
Bernd

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: Image Masking Mystery

Post by RCozens » Thu Aug 05, 2021 6:30 pm

Thank you all for responding and reducing my ignorance.

I will substitute numToByte for numToChar and check further.

And greetings to those who remember The Triple Foole from the HyperCard and RunRev lists. I left programming for environmental advocacy in the early 2000s and, now retired, I have resumed the process of converting an old wine production app from HyperCard to LiveCode. I am happy to be among you again.

Best wishes to all old friends and acquaintances.

Rob Cozens
Manchester, CA

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: Image Masking Mystery

Post by RCozens » Thu Aug 05, 2021 7:17 pm

Special thanks for the heads up re: performance degradation when debugging changes to alphaData and maskData. I was experiencing it (including when I was just working with imageData) and wondering why.

Rob
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Image Masking Mystery

Post by jmburnod » Thu Aug 05, 2021 10:33 pm

After read this post I thought Bernd will come soon 8)
Salut Bernd
Thanks again
Jean-Marc
https://alternatic.ch

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Image Masking Mystery

Post by FourthWorld » Thu Aug 05, 2021 11:29 pm

Good to see you here, Rob.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: Image Masking Mystery

Post by RCozens » Fri Aug 06, 2021 8:35 am

I changed charToNum references to byteToNum and perhaps there is some increase in speed.

But still no success....

I tried modifying only the alphaData but no go. And even though the answer statement showed the change was in effect after I set the alphaData property, when the handler finished and I ran another script that displays the imageData, alphaData, & maskData pixel-by-pixel the 0s had been replaced by 255s.

I then tried setting all four bytes of each masked pixel to 0, as some of my current images that include transparent pixels do. My changes to the RGB bytes were retained, but the alpha channel byte was replaced by 255 so my masked pixels turned black.

Some property or circumstance or misunderstanding on my part is preventing me from changing the first byte of each pixel. I presume that when an image's imageData property is set the alphaData and maskData properties are updated. According to the docs, maskData should override imageData and alphaData settings, but like the first byte of the imageData, my changes to maskData do not persist.

Not sure what to try next.

Hi Richard! I'm still near Anderson Valley. Do you still have an aunt there?

Rob
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Image Masking Mystery

Post by bn » Fri Aug 06, 2021 9:02 am

Hi Rob,

It is difficult to advise without further details. Best would be to upload a small sample stack with a type of image you want to modify. But I think you need 8 posts to the forum before you can upload anything as a measure against the massive spam attacks againsts forums in general.

If you have still access to the use-list you could send me the image or a stack that shows the problem.
Sometimes the order of changes matters. Some pngs are weird. And and and.

Or I try to send you a privat message with my email, I don't know if that works.

@Jean-Marc: Salut.

Kind regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Image Masking Mystery

Post by bn » Fri Aug 06, 2021 9:05 am

Rob,

I have sent you a private message through the forum. I hope you can get at it due to the number of posts.
You could just post a couple of messages here and then the limitatioins go away.

Kind regards
Bernd

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Image Masking Mystery

Post by Klaus » Fri Aug 06, 2021 10:13 am

Hi Rob,

welcome to the forum! :-)


Best

Klaus

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Image Masking Mystery

Post by jiml » Fri Aug 06, 2021 6:23 pm

Rob,

Welcome back!

Jim Lambert

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: Image Masking Mystery

Post by RCozens » Sat Aug 07, 2021 6:33 pm

Morning ...and hi back to all.

I was going to send a copy of my stack to Bernd privately, but decide to post it to everyone.

Serendipity Editors is a work in progress. It is a tool I am building for myself with the intention of making it a gift to the LiveCode community. I have yet to incorporate the Geometry Manager or complete input/output and processing for the Icon Editor (card 2). "Compare Images" may also require further debugging.

The Image Editor shows two overlapping images, one whose name I can post because it ends in . & gif., and "About Icon". (About Icon was created by copying and pasting the about image located on the top right corner of the card. If you run the Export Image selection the image should turn black and will need replacement to test a second time.) Before you run the test you can use Scan Image to see the imageData, alphaData, and maskData pixel-by-pixel.

To test my issue, click on the "Export Image" button and select "About Icon" from the pulldown menu WITH THE CONTROL KEY DOWN (this tells the Editor you are simply masking an existing image rather than exporting it to a file). Unless the gods are afflicting this problem on moi alone, About Icon will turn black instead of displaying a portion of the image it partially covers. If you then run Scan Image you will see that the RGB bytes for the white pixels are now 0, but the alpha channel byte and the respective alphaData and maskData bytes are 255.

If someone confirms my problem exists, I will report it as a bug.

A la Jacque Gay, you can reach me privately at cozensrob @ gmail . com. Hi Jacque!

Cheers!

Rob

PS: It looks like I will have to send the stack to Bernd privately. When I try to attach it I receive a message saying "livecode" it not a valid file extension. Anyone else who would like a copy should eMail be directly.
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Image Masking Mystery

Post by Klaus » Sat Aug 07, 2021 6:36 pm

Hi Rob,

some hints:
1. you need at least 7 postings before you can add attachments, links etc. here in the forum
2. Then you need to ZIP the livecode stack to post it here.


Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Image Masking Mystery

Post by jacque » Sun Aug 08, 2021 5:18 pm

Hi Rob! I smiled when I saw you here. Welcome home!
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Image Masking Mystery

Post by SparkOut » Sun Aug 08, 2021 9:51 pm

Even I recognise the name Rob Cozens :)

Post Reply

Return to “Talking LiveCode”