Humm, the logic of the program is not good enough. Why do I send pImageData as parameter mask_apply function if it is not being manipulated there?
Then the name of that function
mask_apply is really not a good description of what it does. What the function does, really is upon receiving the imagedata of an image, convert any black area into an alpha transparency value. Ok, let's call it
imagedata.convertBlack2Alpha
Hey, what is recommended elsewhere is a verb-object pair (convertBlack2Alpha). Where does that
imagedata.convertBlack2Alpha type of naming convention come from?
I personally find it easier to understand what the function is about this way (especially when trying to use it 3 months after having written it). The truth is that the verb-object naming construct is recommended for use in object oriented languages, where the function is enclosed within a object class. I add imagedata in a way that somehow parallels this. In a sense, I consider black2alpha to be a method of the class of objects of type imagedata.
Okay, okay... but then why use "." when "_" could do? There is no object notation in xtalk. However the "." is allowed within function names. I use an object.xxx notation for all functions that are enclosed in class-like libraries. More about that later. That's just for me a way to rapidly identify functions that are ideal candidates to be moved into a common library.
(TBD stands for "To Be Done". Easy to use for a search)
Code: Select all
on mouseUp
--
--| 1. extracting the alpha values from an image mask
put the imagedata of image "a1" into tMaskData
put imagedata.convertBlack2Alpha(tMaskData) into tAlphaData
--
--| 2. extracting the imagedata of an image.
put the imagedata of image "a2" into tImageData
--
--| 3. checking that mask and imagedata have the same size
-- TBD. It would be cleaner to use tAlphaData instead of tMaskData
-- (tAlphaData is of a length 1/4 of the imagedata)
if the length of tMaskData <> the length of tImageData then
answer "Mask and Image are of different sizes ", the length of tImageData, the length of tImageData
exit mouseup
end if
--
--| 4. Using alpha and image data to define a target image
set the alphadata of image "t1" to tAlphaData
set the imagedata of image "t1" to tImageData
end mouseUp
/* ____________________________________________________________
|
| imagedata.convertBlack2Alpha
|
| @Description: Given some imagedata, convert any black pixel
| into a transparent one and returns the corresponding
| alphadata
| @tested: 18 Nov 2006, 10pm -- working
*/
function imagedata.convertBlack2Alpha pMaskData
/* TBD
some validation is required. Should exit if pMaskData is
empty or not of the right type.
if datatype.validate("imagedata", pMaskData) is false then ...
*/
---
put 0 into tPos
put empty into tAlpha
---
put the length of pMaskData into maskLength
repeat for (maskLength/4) times
put charToNum(char tPos + 2 of pMaskData) into tR1
put charToNum(char tPos + 3 of pMaskData) into tG1
put charToNum(char tPos + 4 of pMaskData) into tB1
if max(tR1,tG1,tB1) = 0 then put 1 into tA1 else put 255 into tA1
put binaryEncode("C",tA1) after tAlpha
add 4 to tPos
end repeat
---
return tAlpha
end imagedata.convertBlack2Alpha
I didn't need it so I didn't implement it. But it wouldn't be that difficult to change this function to have the alpha value based on the % of opacity (% of blackness) of the points in the image.
Hint: The solution can be found in the stack
testComposite.rev that can be found on Chipp's website.
PS. Thanks Darr for the intent
