I have been using some variation of this code to create masked PNG images for years
Now all of a sudden it doesn’t work, heck doesn’t even find the right values to begin with
Dim dlg As OpenDialog
Dim f As FolderItem
Dim pic As picture
Dim pic_rgb As RGBSurface
'
Dim msk As picture
Dim msk_rgb As RGBSurface
'
Dim x As Integer
Dim y As Integer
dlg = New OpenDialog
dlg.Title = "Select an Image file"
f = dlg.ShowModal
If f <> Nil Then
pic = picture.Open(f)
pic_rgb = pic.RGBSurface
msk = New picture(pic.Width,pic.Height)
msk_rgb = msk.Rgbsurface
msk_rgb.FloodFill(0,0,&c000000)
For x=0 To pic.Width
For y=0 To pic.Height
Dim q As Color = pic_rgb.Pixel(x,y)
If q=&cFF000000 Then
// if main pic color is RED mask it to transparent
msk_rgb.pixel(x,y)=&cffffff
Else
msk_rgb.pixel(x,y)=&c000000
End If
Next y
Next x
pic.ApplyMask msk
pic.Save(f,picture.SaveAsPNG)
MsgBox "done"
Quit
End If
The idea is is scans the main image (pic) looking for RED pixels (&cFF000000) and sets the corresponding pixel to transparent.
The odd thing… “Q” is never RED, as a matter of fact it is only ever WHITE or BLACK when in fact the image is made up of 3,135 different colors
Can you see something that I don’t (Xojo 2019r1.1)
The most likely cause of the difference in values, is probably color spaces. IIRC this is an area that Xojo is weak at and to handle correctly requires the use of a 3rd party library (even though support is built-into the OS).
Xojo is optimised for displaying graphics on-screen which means there is a high chance the pixels you access in the picture are not the same as what was stored in the image:
• If your source image is a PNG, a gamma value could be getting applied.
• If your source image has an alpha channel the pixels may be getting pre-multiplied.
• If you are on a Mac, the source image may be getting converted to the device colour space.
I think the only way to avoid this is to work at a lower level than what Xojo provides - ie: the operating system APIs or libraries such as libPNG, libTIFF, libJPEG.
We mainly use the MBS plugins to facilitate this but Einhuger also has similar plugins.