I know this isn't exactly what you want, and it is a little slow, but I converted some code I found on the net. It has procedures to convert color to grayscale/black and white. Maybe someone can optimize and improve it.
Code: Select all
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
Procedure ConvertColorToGray(cImage$)
LoadImage(0, cImage$)
StartDrawing(ImageOutput())
Dim newColor(ImageWidth(), ImageHeight())
For x = 0 To ImageWidth() - 1
For y = 0 To ImageHeight() - 1
PixelColor = Point(x, y)
Red = PixelColor &$FF
Green = PixelColor >>8 &$FF
Blue = PixelColor >>16 &$FF
Gray = Round(0.299 * Red + 0.587 * Green + 0.114 * Blue, 0)
newColor(x, y) = RGB(Gray, Gray, Gray)
Next y
Next x
StopDrawing()
CreateImage(1, ImageWidth(), ImageHeight())
StartDrawing(ImageOutput())
For x = 0 To ImageWidth() - 1
For y = 0 To ImageHeight() - 1
Locate(x, y)
Plot(x, y, newColor(x, y))
Next y
Next x
StopDrawing()
SetGadgetAttribute(1, #PB_ScrollArea_InnerWidth, ImageWidth())
SetGadgetAttribute(1, #PB_ScrollArea_InnerHeight, ImageHeight())
SetGadgetState(2, UseImage(1))
SaveImage(1, "c:\image_gray.jpg", #PB_ImagePlugin_JPEG)
ProcedureReturn
EndProcedure
Procedure ConvertColorToBW(cImage$)
LoadImage(0, cImage$)
StartDrawing(ImageOutput())
Dim newColor(ImageWidth(), ImageHeight())
For x = 0 To ImageWidth() - 1
For y = 0 To ImageHeight() - 1
PixelColor = Point(x, y)
Red = PixelColor &$FF
Green = PixelColor >>8 &$FF
Blue = PixelColor >>16 &$FF
Gray = Round(0.299 * Red + 0.587 * Green + 0.114 * Blue, 0)
If Gray < 128
newColor(x, y) = RGB(0, 0, 0)
Else
newColor(x, y) = RGB(255, 255, 255)
EndIf
Next y
Next x
StopDrawing()
CreateImage(1, ImageWidth(), ImageHeight())
StartDrawing(ImageOutput())
For x = 0 To ImageWidth() - 1
For y = 0 To ImageHeight() - 1
Locate(x, y)
Plot(x, y, newColor(x, y))
Next y
Next x
StopDrawing()
SetGadgetAttribute(1, #PB_ScrollArea_InnerWidth, ImageWidth())
SetGadgetAttribute(1, #PB_ScrollArea_InnerHeight, ImageHeight())
SetGadgetState(2, UseImage(1))
SaveImage(1, "c:\image_bw.jpg", #PB_ImagePlugin_JPEG)
ProcedureReturn
EndProcedure
quit = #False
If OpenWindow(0, 0, 0, 700, 500, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Grayscale") And CreateGadgetList(WindowID(0))
CreateMenu(0, WindowID())
MenuTitle("Image")
MenuItem(0, "Image to Grayscale...")
MenuItem(1, "Image to B/W...")
MenuBar()
MenuItem(3, "Exit")
ScrollAreaGadget(1, 0, 0, 700, 475, 700, 475, 10)
ImageGadget(2, 0, 0, 100, 100, 0)
CloseGadgetList()
Repeat
event = WaitWindowEvent()
Select event
Case #PB_EventMenu
Select EventMenuID()
Case 0
openFile$ = (OpenFileRequester("Select image", "c:\", "Images |*.tif;*.tiff;*.bmp;*png;*jpg;*jpeg", 0))
If openFile$
ConvertColorToGray(openFile$)
EndIf
Case 1
openFile$ = (OpenFileRequester("Select image", "c:\", "Images |*.tif;*.tiff;*.bmp;*png;*jpg;*jpeg", 0))
If openFile$
ConvertColorToBW(openFile$)
EndIf
Case 3
quit = #True
EndSelect
Case #PB_EventCloseWindow
quit = #True
EndSelect
Until quit
EndIf
End