It is currently Sun Dec 15, 2019 9:26 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [Source] Image 2 Grayscale + ignore custom areas!
PostPosted: Sun Jun 02, 2019 10:53 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 577
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:
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: [Source] Image 2 Grayscale + ignore custom areas!
PostPosted: Sun Jun 02, 2019 10:58 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Apr 18, 2019 8:17 am
Posts: 451
Wow, thank you! Just what I needed. :)

But there's a typo: ImageGayscaleForms().


Top
 Profile  
Reply with quote  
 Post subject: Re: [Source] Image 2 Grayscale + ignore custom areas!
PostPosted: Sun Jun 02, 2019 11:14 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 577
Thanks i fixed the typo :lol:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye