Loading image (not) rotated *FIXED*

Just starting out? Need help? Post your questions and find answers here.
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Loading image (not) rotated *FIXED*

Post by AMpos »

I am reading some photos from my camera with LoadImage(), but vertical photos are not rotated.

I guess my camera saved them not rotated and with EXIF rotate info.

Is there an "easy" way to load images, rotated according to EXIF info?

Thx.

=======================

The fix is here:

viewtopic.php?f=12&t=75889
Last edited by AMpos on Thu Aug 27, 2020 11:36 pm, edited 1 time in total.
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Re: Loading image (not) rotated

Post by AMpos »

Damm, I am looking how to rotate a loaded image, and it is not as easy as I though.

Someone has something?

Code: Select all

	Procedure RotateImage (image,angle)
		Select angle
			Case 0 ;none
			Case 1 ; 1=90
				rotate (image,90)
			Case 2 ; 2=180
				rotate(image,180)
			Case 3 ; 3=270 or -90
				rotate(image,270)
		EndSelect
	EndProcedure
I have been looking to do it using SPRITE on a new screen, and capture it

Also looking at VectorLibrary, but I do not understand it yet.
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Re: Loading image (not) rotated

Post by AMpos »

I have found this, but is faaaar beyond my knowledge:

https://github.com/johansatge/jpeg-autorotate
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Loading image (not) rotated

Post by infratec »

You have simply to code it :wink:

The following is only a 'quick hack', not of high quality :!:

Code: Select all

EnableExplicit

#DHT = $C4FF
#DAC = $CCFF

#APP0 = $E0FF
#APP1 = $E1FF

#RST0 = $D0FF
#RST1 = $D1FF
#RST2 = $D2FF
#RST3 = $D3FF
#RST4 = $D4FF
#RST5 = $D5FF
#RST6 = $D6FF
#RST7 = $D7FF
#SOI = $D8FF
#EOI = $D9FF
#SOS = $DAFF
#DQT = $DBFF
#DRI = $DDFF

#Intel = 1
#Motorola = 2

#EXIF_OrientationTag = $0112



IncludeFile "RotateImage.pbi"


Define.i File, Ptr, Orientation, OrientationPtr, Marker, Img, ImgOk
Define Filename$
Define *Buffer


Filename$ = OpenFileRequester("Choose a jpg file", "", "JPG|*.jpg", 0)
If Filename$
  File = ReadFile(#PB_Any, Filename$)
  If File
    *Buffer = AllocateMemory(Lof(File), #PB_Memory_NoClear)
    If *Buffer
      If ReadData(File, *Buffer, MemorySize(*Buffer)) = MemorySize(*Buffer)
        If PeekU(*Buffer + Ptr) = #SOI
          ;Debug "Ok"
          Ptr + 2
          Repeat
            Marker = PeekU(*Buffer + Ptr)
            ;Debug Hex(Marker)
            Ptr + 2
            Select Marker
              Case #APP1
                Break
              Case #DRI
                Ptr + 4
              Case #RST0, #RST1, #RST2, #RST3, #RST4, #RST5, #RST6, #RST7
              Default
                ;Debug Hex(PeekA(*Buffer + Ptr) << 8 | PeekA(*Buffer + Ptr + 1))
                Ptr + (PeekA(*Buffer + Ptr) << 8 | PeekA(*Buffer + Ptr + 1))
            EndSelect
            
          Until Ptr >= MemorySize(*Buffer) Or Marker = #SOS
          
          If Marker = #APP1
            ;Debug PeekS(*Buffer + Ptr + 2, 4, #PB_Ascii)
            If PeekS(*Buffer + Ptr + 2, 4, #PB_Ascii) = "Exif"
              ;Debug "Exif found"
              
              
              Ptr + 8
              
              ;ShowMemoryViewer(*Buffer + Ptr, 200)
              
              If PeekU(*Buffer + Ptr) = $4949
                
                Repeat
                  If CompareMemory(?Intel, *Buffer + Ptr + 4 + OrientationPtr, 8)
                    Orientation = PeekA(*Buffer + Ptr + 4 + OrientationPtr + 8)
                    Break
                  Else
                    OrientationPtr + 1
                  EndIf
                Until Orientation = 1024
                
              ElseIf PeekU(*Buffer + Ptr) = $4D4D
                
                Repeat
                  If CompareMemory(?Motorola, *Buffer + Ptr + 4 + OrientationPtr, 8)
                    Orientation = PeekA(*Buffer + Ptr + 4 + OrientationPtr + 8 + 1)
                    Break
                  Else
                    OrientationPtr + 1
                  EndIf
                Until OrientationPtr = 1024
                
              EndIf
              
            EndIf
            
            Debug Orientation
            
          EndIf
          
        Else
          Debug "No Exif found"
        EndIf
        
      EndIf
    EndIf
    FreeMemory(*Buffer)
  EndIf
  
  CloseFile(File)
  
  
  UseJPEGImageDecoder()
  
  Img = LoadImage(#PB_Any, Filename$)
  If Img
    
    Select Orientation
      Case 0, 1
        ImgOk = Img
      Case 2
        ImgOk = MirrorImage(Img)
        FreeImage(Img)
      Case 3
        ImgOk = RotateImage(Img, 180)
        FreeImage(Img)
      Case 4
        ImgOk = FlipImage(Img)
        FreeImage(Img)
      Case 5
        ImgOk = MirrorImage(Img)
        FreeImage(Img)
        Img = ImgOk
        ImgOk = RotateImage(Img, 90)
        FreeImage(Img)
      Case 6
        ImgOk = RotateImage(Img, -90)
        FreeImage(Img)
      Case 7
        ImgOk = MirrorImage(Img)
        FreeImage(Img)
        Img = ImgOk
        ImgOk = RotateImage(Img, -90)
        FreeImage(Img)
      Case 8
        ImgOk = RotateImage(Img, 90)
        FreeImage(Img)
    EndSelect
    
        
    OpenWindow(0, 0, 0, 800, 600, "JPGViewer", #PB_Window_SystemMenu)
    
    ScrollAreaGadget(0, 0, 0, 800, 600, ImageWidth(ImgOk), ImageHeight(ImgOk))
    
    ImageGadget(1, 0, 0, 0, 0, ImageID(ImgOk))
    
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
    
    FreeImage(ImgOk)
    
    
  EndIf
EndIf

DataSection
  Intel:
  Data.a $12, $01, $03, $00, $01, $00, $00, $00
  Motorola:
  Data.a $01, $12, $00, $03, $00, $00, $00, $01
EndDataSection
The included file is here:
viewtopic.php?p=298873#p298873

I tested it with picture from here:
https://github.com/recurser/exif-orientation-examples
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Re: Loading image (not) rotated *FIXED*

Post by AMpos »

Thank you, @infratec

The solution is here:

viewtopic.php?f=12&t=75889
Post Reply