[Source] Image 2 Grayscale + ignore custom areas!

Share your advanced PureBasic knowledge/code with the community.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

[Source] Image 2 Grayscale + ignore custom areas!

Post by Mijikai »

I worte a function that converts a image to grayscale while ignoring custom areas.
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
Last edited by Mijikai on Sun Jun 02, 2019 11:25 am, edited 3 times in total.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: [Source] Image 2 Grayscale + ignore custom areas!

Post by BarryG »

Wow, thank you! Just what I needed. :)

But there's a typo: ImageGayscaleForms().
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: [Source] Image 2 Grayscale + ignore custom areas!

Post by Mijikai »

Thanks i fixed the typo :lol:
Post Reply