[soved]help with watermark

Just starting out? Need help? Post your questions and find answers here.
quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

[soved]help with watermark

Post by quasiperfect »

hi

i want to watermark some pictures and i'm almost there but i don't understand the drawing modes
please see image for what i have done and what i need to do

Image

Code: Select all

Procedure DEG2RAD (x)
  ProcedureReturn (x / 180 * #PI) 
EndProcedure

UseJPEGImageDecoder() 
UsePNGImageDecoder() 
UseTIFFImageDecoder() 
UseTGAImageDecoder() 
UseJPEGImageEncoder()

Global fontsize.l = 20
Global fontangle = 30
Global twidth
Global theight
Global xpos
Global ypos
Global mytext.s = "any text here"

Enumeration
  #MyImage
EndEnumeration

If OpenWindow(0, 0, 0, 600, 400, "watermark", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  image_path.s = OpenFileRequester("Select image", "","Images (JPG,PNG,TIFF,TGA) | *.jpg;*.jpeg;*.png;*.tif;*.tiff;*.tga", 0)
  If image_path
    If LoadImage(#MyImage,image_path) <> 0
      ResizeWindow(0,100,100,ImageWidth(#MyImage),ImageHeight(#MyImage))
      LoadFont(0, "Arial",fontsize)
      StartDrawing(ImageOutput(#MyImage))
      DrawingFont(FontID(0))  
      twidth = TextWidth(mytext)
      theight = TextHeight(mytext)
      xpos = ((ImageWidth(#MyImage) / 2) - (Cos(deg2rad(fontangle)) * (twidth/2)))
		  ypos = ((ImageHeight(#MyImage) / 2) + (Sin(deg2rad(fontangle)) * (twidth / 2)) + (Cos(deg2rad(fontangle))* (theight / 2)))
      DrawingMode(#PB_2DDrawing_Default )
      DrawRotatedText(xpos,ypos, mytext,fontangle,RGB(255,0,0))
      StopDrawing()
      ImageGadget(0, 0, 0, ImageWidth(#MyImage) , ImageHeight(#MyImage), ImageID(#MyImage))
    Else
      MessageRequester("Error", "Error loading image", 0) 
    EndIf  
  Else
    MessageRequester("Error", "No image selected", 0) 
  EndIf
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Last edited by quasiperfect on Thu Feb 04, 2010 9:25 am, edited 1 time in total.
Registered user of PureBasic
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: help with watermark

Post by Trond »

Code: Select all

Procedure DEG2RAD (x)
  ProcedureReturn (x / 180 * #PI) 
EndProcedure

UseJPEGImageDecoder() 
UsePNGImageDecoder() 
UseTIFFImageDecoder() 
UseTGAImageDecoder() 
UseJPEGImageEncoder()

Global fontsize.l = 30
Global fontname.s = "impact"
Global fontangle = 35
Global twidth
Global theight
Global xpos
Global ypos
Global mytext.s = "any text here"
Global Alpha = 128

Enumeration
  #MyImage
EndEnumeration

If OpenWindow(0, 0, 0, 600, 400, "watermark", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  image_path.s = OpenFileRequester("Select image", "","Images (JPG,PNG,TIFF,TGA) | *.jpg;*.jpeg;*.png;*.tif;*.tiff;*.tga", 0)
  If image_path
    If LoadImage(#MyImage,image_path) <> 0
      ResizeWindow(0,100,100,ImageWidth(#MyImage),ImageHeight(#MyImage))
      LoadFont(0, fontname,fontsize)
      StartDrawing(ImageOutput(#MyImage))
      DrawingFont(FontID(0))  
      twidth = TextWidth(mytext)
      theight = TextHeight(mytext)
      xpos = ((ImageWidth(#MyImage) / 2) - (Cos(deg2rad(fontangle)) * (twidth/2)))
        ypos = ((ImageHeight(#MyImage) / 2) + (Sin(deg2rad(fontangle)) * (twidth / 2)) + (Cos(deg2rad(fontangle))* (theight / 2)))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      DrawRotatedText(xpos,ypos, mytext,fontangle,RGBA(255,0,0, Alpha))
      StopDrawing()
      ImageGadget(0, 0, 0, ImageWidth(#MyImage) , ImageHeight(#MyImage), ImageID(#MyImage))
    Else
      MessageRequester("Error", "Error loading image", 0) 
    EndIf  
  Else
    MessageRequester("Error", "No image selected", 0) 
  EndIf
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: help with watermark

Post by srod »

I am not sure what effect you are after, but try switching the relevant two lines for :

Code: Select all

DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawRotatedText(xpos,ypos, mytext,fontangle,RGBA(255,0,0,255))
**EDIT : too slow, beaten by Speedy-Gon-Trond! :)
I may look like a mule, but I'm not a complete ass.
quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

Re: help with watermark

Post by quasiperfect »

thanks to both

Trond code is pretty close it got the transparency effect but not the outline
i need the text to have a white outline of 1 or 2 pixels
Registered user of PureBasic
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: help with watermark

Post by srod »

For that you will need to create a GDI path, but I am pretty sure that this will not be compatible with PB's image library in that GDI will ignore the alpha channel and your path will thus effectively retain the original per-pixel alpha settings after you have filled the text.

One way around this would be to copy your image (without the text) to a 24-bit bitmap (use DrawAlphaImage() onto a suitable background) and then to use GDI to create a path etc. A bit fiddly, but very possible.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: help with watermark

Post by srod »

A cheat is to draw the text several times, in two different colors, offsetting the text slightly each time...

Code: Select all

Procedure DEG2RAD (x)
  ProcedureReturn (x / 180 * #PI) 
EndProcedure

UseJPEGImageDecoder() 
UsePNGImageDecoder() 
UseTIFFImageDecoder() 
UseTGAImageDecoder() 
UseJPEGImageEncoder()

Global fontsize.l = 30
Global fontangle = 30
Global twidth
Global theight
Global xpos
Global ypos
Global mytext.s = "any text here"

Enumeration
  #MyImage
EndEnumeration

If OpenWindow(0, 0, 0, 600, 400, "watermark", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  image_path.s = OpenFileRequester("Select image", "","Images (JPG,PNG,TIFF,TGA) | *.jpg;*.jpeg;*.png;*.tif;*.tiff;*.tga", 0)
  If image_path
    If LoadImage(#MyImage,image_path) <> 0
      ResizeWindow(0,100,100,ImageWidth(#MyImage),ImageHeight(#MyImage))
      LoadFont(0, "Arial",fontsize)
      hdc = StartDrawing(ImageOutput(#MyImage))
      DrawingFont(FontID(0))  
      twidth = TextWidth(mytext)
      theight = TextHeight(mytext)
      xpos = ((ImageWidth(#MyImage) / 2) - (Cos(deg2rad(fontangle)) * (twidth/2)))
        ypos = ((ImageHeight(#MyImage) / 2) + (Sin(deg2rad(fontangle)) * (twidth / 2)) + (Cos(deg2rad(fontangle))* (theight / 2)))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      DrawRotatedText(xpos-3,ypos, mytext,fontangle,RGBA(255,255,255,255))
      DrawRotatedText(xpos+3,ypos, mytext,fontangle,RGBA(255,255,255,255))
      DrawRotatedText(xpos,ypos, mytext,fontangle,RGBA(255,0,0,128))
      StopDrawing()
      ImageGadget(0, 0, 0, ImageWidth(#MyImage) , ImageHeight(#MyImage), ImageID(#MyImage))
    Else
      MessageRequester("Error", "Error loading image", 0) 
    EndIf  
  Else
    MessageRequester("Error", "No image selected", 0) 
  EndIf
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

If that's not quite what you're after then you will probably need to look into the GDI paths.
I may look like a mule, but I'm not a complete ass.
quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

Re: help with watermark

Post by quasiperfect »

not bad srod but is to obtrusive

i played a bit with the code and made the text autofit (very ugly code but i don't have any better ideea) based on the image size and used the text another time to give a nice effect

maybe someone will like it and use it

Code: Select all

    Procedure DEG2RAD (x)
      ProcedureReturn (x / 180 * #PI)
    EndProcedure

    UseJPEGImageDecoder()
    UsePNGImageDecoder()
    UseTIFFImageDecoder()
    UseTGAImageDecoder()
    UseJPEGImageEncoder()

    Global fontsize.l = 30
    Global fontname.s = "arial"
    Global fontangle = 35
    Global twidth
    Global theight
    Global xpos
    Global ypos
    Global mytext.s = "any text here"
    Global Alpha = 50

    Enumeration
      #MyImage
    EndEnumeration

    If OpenWindow(0, 0, 0, 600, 400, "watermark", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
      image_path.s = OpenFileRequester("Select image", "","Images (JPG,PNG,TIFF,TGA) | *.jpg;*.jpeg;*.png;*.tif;*.tiff;*.tga", 0)
      If image_path
        If LoadImage(#MyImage,image_path) <> 0
          ResizeWindow(0,100,100,ImageWidth(#MyImage),ImageHeight(#MyImage))
          LoadFont(0, fontname,fontsize,#PB_Font_Bold )
          StartDrawing(ImageOutput(#MyImage))
          DrawingFont(FontID(0)) 
          twidth = TextWidth(mytext)
          theight = TextHeight(mytext)
          StopDrawing()
          
          While (twidth < (ImageWidth(#MyImage)-50))
            fontsize = fontsize * 1.1
            LoadFont(0, fontname,fontsize,#PB_Font_Bold )
            StartDrawing(ImageOutput(#MyImage))
            DrawingFont(FontID(0))  
            twidth = TextWidth(mytext)
            theight = TextHeight(mytext)
            StopDrawing()
          Wend
          
          xpos = ((ImageWidth(#MyImage) / 2) - (Cos(deg2rad(fontangle)) * (twidth/2)))
          ypos = ((ImageHeight(#MyImage) / 2) + (Sin(deg2rad(fontangle)) * (twidth / 2)) + (Cos(deg2rad(fontangle))* (theight / 2)))

          LoadFont(0, fontname,fontsize,#PB_Font_Bold )
          StartDrawing(ImageOutput(#MyImage))
          DrawingFont(FontID(0)) 
          
          DrawingMode(#PB_2DDrawing_AlphaBlend)
          DrawRotatedText(xpos,ypos, mytext,fontangle,RGBA(255,0,0, Alpha))
          
          
          DrawingMode(#PB_2DDrawing_AlphaBlend)
          DrawRotatedText(xpos,ypos-2, mytext,fontangle,RGBA(255,255,255, Alpha+50))
          
          StopDrawing()
          ImageGadget(0, 0, 0, ImageWidth(#MyImage) , ImageHeight(#MyImage), ImageID(#MyImage))
          
        Else
          MessageRequester("Error", "Error loading image", 0)
        EndIf 
      Else
        MessageRequester("Error", "No image selected", 0)
      EndIf
      Repeat
      Until WaitWindowEvent() = #PB_Event_CloseWindow
    EndIf
Last edited by quasiperfect on Wed Feb 03, 2010 4:31 pm, edited 1 time in total.
Registered user of PureBasic
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: help with watermark

Post by srod »

Not sure what you mean by autofit exactly?

It looks good when I use a 24-bit jpeg, not so good on a png with full alpha-channel. The text is almost transparent here. Still, altering the alpha value does make a difference. :)
I may look like a mule, but I'm not a complete ass.
quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

Re: help with watermark

Post by quasiperfect »

i edited the code a bit it wasn't the right version
i meant it writes the text (at max size possible) on middle of the image (any image size)
but after some testing my formula sucks (dosen't work)
i wanted the text to go from bottom left corner to top right corner
Registered user of PureBasic
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: help with watermark

Post by Trond »

Here's an outline (it's a mess and it's not exactly fast, but it works - at least when cleartype is turned off...)

Code: Select all


h = 500
w = 500
CreateImage(0, h, w, 24)
LoadFont(0, "Impact", 40)

text.s = "In my pants!"
tcol = #Red
ocol = #White ; can't be the same as tcol!
owidth = 2

StartDrawing(ImageOutput(0))
  DrawingFont(FontID(0))
  FrontColor(tcol)
  DrawRotatedText(100, 350, text, 45)
  For y = owidth To h-owidth-1
    For x = owidth To w-owidth-1
      c1 = Point(x, y)
      If c1 <> tcol
        For y2 = -owidth To owidth
          For x2 = -owidth To owidth
            If Point(x+x2, y+y2) = tcol
              Plot(x, y, ocol)
              Break 2
            EndIf
          Next
        Next
      EndIf
    Next
  Next
StopDrawing()


OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
ImageGadget(0, 0, 0, 0, 0, ImageID(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver


quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

Re: help with watermark

Post by quasiperfect »

nice but can be integrated with my code ? because i tried and no luck or i'm missing something
Registered user of PureBasic
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: help with watermark

Post by Kaeru Gaman »

is it necessary to chose a purebasic solution?

with inkscape, it would be a three-click-action to add a watermark...
oh... and have a nice day.
quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

Re: help with watermark

Post by quasiperfect »

yap the watermark is part of a bigger application
Registered user of PureBasic
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: help with watermark

Post by Trond »

Code: Select all


UsePNGImageDecoder()
LoadImage(1, "c:\out.png")

h = 400
w = 400
CreateImage(0, h, w, 32)
LoadFont(0, "Impact", 40)

text.s = "Bottom secret"
alpha = 128
tcol = RGBA(255, 0, 0, alpha)
ocol = RGBA(255, 255, 255, alpha) ; can't be the same as tcol!
owidth = 2
tx = 50
ty = 250

StartDrawing(ImageOutput(0))
  
  DrawingMode(#PB_2DDrawing_AlphaChannel)
  Box(0, 0, w, h, RGBA(255, 0, 0, 0))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  
  DrawingFont(FontID(0))
  FrontColor(tcol)
  DrawRotatedText(tx, ty, text, 45)
  For y = owidth To h-owidth-1
    For x = owidth To w-owidth-1
      c1 = Point(x, y)
      If c1 <> tcol
        For y2 = -owidth To owidth
          For x2 = -owidth To owidth
            If Point(x+x2, y+y2) = tcol
              Plot(x, y, ocol)
              Break 2
            EndIf
          Next
        Next
      EndIf
    Next
  Next
StopDrawing()

StartDrawing(ImageOutput(1))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  DrawImage(ImageID(0), 0, 0)
StopDrawing()

OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
ImageGadget(0, 0, 0, 0, 0, ImageID(1))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver


Edit: Now it's starting to look cool. You should change Outline() so that it takes an x and y value as well, and then you just pass TextWidth(TEXT) and TextHeight(text) for w and h, so that it processes only the part of the image where the text was drawn. Then it will be faster. (You don't have to use the double outline, just delete the second call to Outline().)

Code: Select all

Procedure Outline(tcol, ocol, owidth, w, h)
  For y = owidth To h-owidth-1
    For x = owidth To w-owidth-1
      c1 = Point(x, y)
      If c1 <> tcol
        For y2 = -owidth To owidth
          For x2 = -owidth To owidth
            If Point(x+x2, y+y2) = tcol
              Plot(x, y, ocol)
              Break 2
            EndIf
          Next
        Next
      EndIf
    Next
  Next
EndProcedure

UsePNGImageDecoder()
LoadImage(1, "c:\out.png")

h = 400
w = 400
CreateImage(0, h, w, 32)
LoadFont(0, "Impact", 40)

text.s = "Ministry of silly walks"
alpha = 255
tcol = RGBA(255, 0, 0, alpha)
ocol = RGBA(255, 1, 0, alpha) ; can't be the same as tcol!
ocol2 = RGBA(255, 255, 255, alpha) ; can't be the same as ocol!
owidth = 1
tx = 10
ty = 350

StartDrawing(ImageOutput(0))
  
  DrawingMode(#PB_2DDrawing_AlphaChannel)
  Box(0, 0, w, h, RGBA(255, 0, 0, 0))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  
  DrawingFont(FontID(0))
  FrontColor(tcol)
  DrawRotatedText(tx, ty, text, 45)
  Outline(tcol, ocol, 2, w, h)
  Outline(ocol, ocol2, owidth, w, h)
  Outline(ocol2, ocol, owidth, w, h)
StopDrawing()

StartDrawing(ImageOutput(1))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  DrawImage(ImageID(0), 0, 0)
StopDrawing()

OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
ImageGadget(0, 0, 0, 0, 0, ImageID(1))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: help with watermark

Post by Rook Zimbabwe »

When this paint program is bbeta I want to test it! :D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply