Page 1 of 1

flip image horizontally

Posted: Sun Jun 14, 2015 8:27 pm
by Chuf
Hello,

I would like to flip the image horizontally on OSX and Windows
As an example I want a helmet is photographed by the left side, so pointing to the left
then points to the right so than when it is photographed by the right
I have been looking for a while in all the forums but found nothing
can anyone help me?
when it comes it should not be Windows-specific because I'm just about a program
writing under Windows and OSX

many Greetings

Re: flip image horizontally

Posted: Sun Jun 14, 2015 9:14 pm
by IdeasVacuum
What you are looking for is this really excellent code by Luis: http://www.purebasic.fr/english/viewtop ... 12&t=38975

Re: flip image horizontally

Posted: Sun Jun 14, 2015 9:24 pm
by davido
@chuf,

You might find this excellent code interesting, too:
http://www.purebasic.fr/english/viewtop ... 28#p445225

Re: flip image horizontally

Posted: Sun Jun 14, 2015 10:42 pm
by Chuf
many thanks for the answer, im using Luis code the first test by OS X Yosemite works very fine, my next test is by windows

Re: flip image horizontally

Posted: Sun Jun 14, 2015 10:55 pm
by infratec
Hi,

try this too:

Code: Select all

UseJPEGImageDecoder()
UseJPEGImageEncoder()

Filename$ = OpenFileRequester("Choose an image", "", "JPG|*.jpg", 0)
If Len(Filename$)
  OrgImg = LoadImage(#PB_Any, Filename$)
  If OrgImg
    
    Width = ImageWidth(OrgImg)
    Height = ImageHeight(OrgImg)
    
    NewImg = CreateImage(#PB_Any, Width, Height)
    If NewImg
      
      If StartDrawing(ImageOutput(NewImg))
        
        For x = Width - 1 To 0 Step -1
          StripeImg = GrabImage(OrgImg, #PB_Any, x, 0, 1, Height)
          If StripeImg
            DrawImage(ImageID(StripeImg), Width - x - 1, 0)
            FreeImage(StripeImg)
          EndIf
        Next x
        
        StopDrawing()
        
        SaveImage(NewImg, Filename$ + ".mirrored.jpg", #PB_ImagePlugin_JPEG)
        
      EndIf
      
      FreeImage(NewImg)
    EndIf
    
    FreeImage(OrgImg)
  EndIf
EndIf
It's short in code and I hope it's fast enough.
Since it uses only PB internal commands, it is crossplatform.

Bernd

Re: flip image horizontally

Posted: Sun Jun 14, 2015 11:34 pm
by Chuf
Hi Bernd, this is a very fine short code, i am have testing this with a .png file by UsePNGImageDecoder()
one problem by this solution is: the picture has a black background color can you help me to fix this?
i love your short code

Best Regards

Re: flip image horizontally

Posted: Mon Jun 15, 2015 4:55 am
by netmaestro
You just need two quick changes to support PNG transparency:

Code: Select all

UsePNGImageDecoder()
UsePNGImageEncoder()

Filename$ = OpenFileRequester("Choose an image", "", "PNG|*.png", 0)
If Len(Filename$)
  OrgImg = LoadImage(#PB_Any, Filename$)
  If OrgImg
    
    Width = ImageWidth(OrgImg)
    Height = ImageHeight(OrgImg)
    
    NewImg = CreateImage(#PB_Any, Width, Height, 32, #PB_Image_Transparent) ; <---------------- Changed line
    If NewImg
      
      If StartDrawing(ImageOutput(NewImg))
        
        For x = Width - 1 To 0 Step -1
          StripeImg = GrabImage(OrgImg, #PB_Any, x, 0, 1, Height)
          If StripeImg
            DrawAlphaImage(ImageID(StripeImg), Width - x - 1, 0) ; <--------------------Changed line
            FreeImage(StripeImg)
          EndIf
        Next x
        
        StopDrawing()
        SaveImage(NewImg, Filename$ + ".mirrored.png", #PB_ImagePlugin_JPEG)
        
      EndIf
      
      FreeImage(NewImg)
    EndIf
    
    FreeImage(OrgImg)
  EndIf
EndIf

Re: flip image horizontally

Posted: Mon Jun 15, 2015 7:13 am
by infratec
Hi,

netmaestro's code should fix this, else you can specify a backgroundcolor at CreateImage().

netmaestro made a typo:
use

Code: Select all

SaveImage(NewImg, Filename$ + ".mirrored.png", #PB_ImagePlugin_PNG)
Bernd

Re: flip image horizontally

Posted: Mon Jun 15, 2015 7:27 am
by netmaestro
Ha, I was checking results with the LibraryViewer so I didn't notice the oversight. One more failure to attend to detail to go with the other 117 for today! Thanks inf

Re: flip image horizontally

Posted: Mon Jun 15, 2015 9:53 am
by Chuf
many many Thanks now works fine for me