Before I reinvent the wheel yet again, does anyone have a function to convert a standard RGBA PNG image to a grayscale image? MBS or Einhugur are quite acceptable.
@Tim J Before I reinvent the wheel yet again, does anyone have a function to convert a standard RGBA PNG image to a grayscale image? MBS or Einhugur are quite acceptable.
How do you want to convert it to grayscale?
Luminosity, average, brightness or maximum, minimum, value (HSV), or a custom implementation where you can fiddle with the RGB channels?
function grayScale(c as color) as color return rgb((c.red * 0.299),(c.green * 0.587) , (c.Blue *0.114),c.alpha) end function
scan the RGBSurface and apply this equation to each color
you can also use a MAP and TRANSFORM, but I'd have to look up the exact code I used for that..... the Transform method is very fast
Einhugur plugin has the picture effects classes which allow you to chain together filters to convert to grayscale. You covert the picture to a raw data class, run all your effects and then get your finial image. It runs nice an fast and apparently can use up to 8 cores.
@Tim J Einhugur
You can read the PNG directly with GraphicsFormats and use PictureEffectsRaw to do the Grayscale conversion, with option of several variations of grayscale methods. You can do this all synchronous or asynchronous. TypeLibs RawBitmapConverter if at any point in the process you need it in Xojo Picture Object.
For @ChristianSchmitz - the GrayScaleMBS(3) is giving the best result for these images. But ...
When I set it to the RowPicture, the mask or alpha are being ignored.
I've tried using both CopyPictureWithMask and CopyPictureWithAlpha when creating the Picture, but the result always has a black background when I set the RowPicture (using GrayScaleMBS(3) doesn't matter either way):
The code looks like this when using the IconMBS picture (leaf is a folderitem):
Me.RowPicture(Me.LastIndex) = leaf.IconMBS(20).GrayScaleMBS(3)
And like this when using the NSImageMBS:
Dim i As NSImageMBS = NSWorkspaceMBS.iconForFileType("fcpxml") lbBackupFileSystem.RowPicture(lbBackupFileSystem.LastIndex) = i.CopyPictureWithMask.ScaleMBS(20, 20, True).GrayScaleMBS(3)
Hmm, I can get this sorted on the Mac side, but for Linux, LinuxIconMBS.FileIcon(f, 32) is returning a picture without a mask, so trying to get the mask from the icon fails with an UnsupportedFormatException with a message stating that you can't copy a mask from an image with an alpha channel, but the Picture returned doesn't have an alpha channel.
Calling the Linux code like this:
iconPic = LinuxIconMBS.FileIcon(leaf, 32)
Dim surf As RGBSurface = picture.RGBSurface
Dim c As Color
Dim w As Integer = picture.Width - 1
Dim h As Integer = picture.Height - 1
Dim cg As Integer // color grey
rem farbe in graustufen wandeln
For y As Integer = 0 To h
For x As Integer = 0 To w
c = surf.Pixel(x, y)
cg=(c.red +c.green + c. blue )/3
surf.Pixel(x, y) = RGB(cg,cg,cg)