It uses a callback so u can draw all kids of forms or even text.
Have fun
Code:
Code: Select all
EnableExplicit
;------------------------------------------------------------------
;Author Mijikai
;ImageGrayscaleForms()
;------------------------------------------------------------------
;Supports all intenal image formats (24/32 bits inverted or not)
;Converts a image to grayscale while ignoring all forms/areas
;defined/drawn in the callback (use color $FFFFFFFF)
;------------------------------------------------------------------
;Make sure to use the appropriate image plugins before testing!
UseJPEGImageDecoder()
UsePNGImageEncoder()
Global image.i
Global image_test.i
Procedure.i ImageGrayscaleForms(Image.i,*Callback,*Parameter)
Structure COLOR_STRUCT_556785
StructureUnion
rgba.a[4]
code.l
EndStructureUnion
EndStructure
Protected width.i
Protected height.i
Protected copy.i
Protected stencil.i
Protected *buffer
Protected pitch.i
Protected depth.i
Protected px.i
Protected py.i
Protected offset.i
Protected *mask.Ascii
Protected color.COLOR_STRUCT_556785
Protected rgb.i
Protected flip.i
Protected gray.i
If *Callback And IsImage(Image)
width = ImageWidth(Image)
height = ImageHeight(Image)
copy = CopyImage(Image,#PB_Any)
If IsImage(copy)
stencil = CreateImage(#PB_Any,width,height,32)
If IsImage(stencil)
If CallFunctionFast(*Callback,stencil,*Parameter)
If StartDrawing(ImageOutput(stencil))
Select DrawingBufferPixelFormat()
Case #PB_PixelFormat_24Bits_RGB
rgb = #True
Case #PB_PixelFormat_24Bits_RGB|#PB_PixelFormat_ReversedY
rgb = #True
flip = #True
Case #PB_PixelFormat_32Bits_RGB
rgb = #True
Case #PB_PixelFormat_32Bits_RGB|#PB_PixelFormat_ReversedY
rgb = #True
flip = #True
Case #PB_PixelFormat_24Bits_BGR|#PB_PixelFormat_ReversedY
flip = #True
Case #PB_PixelFormat_32Bits_BGR|#PB_PixelFormat_ReversedY
flip = #True
EndSelect
*buffer = DrawingBuffer()
pitch = DrawingBufferPitch()
depth = OutputDepth()
StopDrawing()
If depth = 32
If StartDrawing(ImageOutput(copy))
width - 1
height - 1
If rgb
If flip
For py = 0 To height
offset = *buffer + ((height - py) * Pitch)
For px = 0 To width
*mask = offset + (px * 4)
If Not *mask\a
color\code = Point(px,py)
gray = color\rgba[0] * 0.2126 + color\rgba[1] * 0.7152 + color\rgba[2] * 0.0722
color\rgba[0] = gray
color\rgba[1] = gray
color\rgba[2] = gray
Plot(px,py,color\code)
EndIf
Next
Next
Else
For py = 0 To height
offset = *buffer + (py * Pitch)
For px = 0 To width
*mask = offset + (px * 4)
If Not *mask\a
color\code = Point(px,py)
gray = color\rgba[0] * 0.2126 + color\rgba[1] * 0.7152 + color\rgba[2] * 0.0722
color\rgba[0] = gray
color\rgba[1] = gray
color\rgba[2] = gray
Plot(px,py,color\code)
EndIf
Next
Next
EndIf
Else
If flip
For py = 0 To height
offset = *buffer + ((height - py) * Pitch)
For px = 0 To width
*mask = offset + (px * 4)
If Not *mask\a
color\code = Point(px,py)
gray = color\rgba[0] * 0.0722 + color\rgba[1] * 0.7152 + color\rgba[2] * 0.2126
color\rgba[0] = gray
color\rgba[1] = gray
color\rgba[2] = gray
Plot(px,py,color\code)
EndIf
Next
Next
Else
For py = 0 To height
offset = *buffer + (py * Pitch)
For px = 0 To width
*mask = offset + (px * 4)
If Not *mask\a
color\code = Point(px,py)
gray = color\rgba[0] * 0.0722 + color\rgba[1] * 0.7152 + color\rgba[2] * 0.2126
color\rgba[0] = gray
color\rgba[1] = gray
color\rgba[2] = gray
Plot(px,py,color\code)
EndIf
Next
Next
EndIf
EndIf
StopDrawing()
FreeImage(stencil)
ProcedureReturn copy
EndIf
EndIf
EndIf
EndIf
FreeImage(stencil)
EndIf
FreeImage(copy)
EndIf
EndIf
ProcedureReturn #Null
EndProcedure
Procedure.i ImageCallback(Image.i,*Parameter)
If StartDrawing(ImageOutput(image))
DrawText(10,10,"Hallo!",$FFFFFFFF)
Ellipse(200,200,80,80,$FFFFFFFF)
StopDrawing()
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
image = LoadImage(#PB_Any,"test.jpg")
image_test = ImageGrayscaleForms(image,@ImageCallback(),#Null)
If image_test
Debug "Image created!"
Debug "Save: " + Str(SaveImage(image_test,"test_image.bmp"))
Else
Debug "Something went wrong!"
EndIf
End