Unexpected rotation of an image when resizing

When resizing a JPG file originated in a phone and uploaded to a server with the following method:

The resized JPG file is rotated 90 degrees clockwise.
I suspect, for what I investigated, that the problem could be solved looking at the EXIF metadata in the JPG and depends on that it could need or not a rotation.

I couldn’t find a method to evaluate the EXIF metadata and if needed rotate the JPG.

May be @Christian_Schmitz could give some advice with a method CorrectOrientation using MBS plugins or someone else in the forum using only pure Xojo functions.

Thanks

Also, I suppose NewWodth and NewHeight have positive values.

Have you looked at the blog ? If my memory is correct there is oe to Read (I do not remember if ther is one to Write).

There is no ToC (I found none) in the Blog.

But I found Tip to get Meta Data (MacOS)

Search meta data, there may be an entry (or read the linled page, there may be tips for WIndows)

You’re certainly right about the exif data (or whatever equivalent for jpeg pictures). Graphics.DrawPicture may not retain the existing data. Now, is this a bug or a limitation? :thinking:

A JPEG file contains compressed picture data and metadata – JPEG-specific metadata, Exif metadata, possibly other kinds of metadata such as IPTC. A Picture object in Xojo contains only (uncompressed) picture data. While reading the picture data from the file Xojo uses the orientation metadata and rotates the picture data accordingly but the Picture object itself doesn’t contain those metadata.

Thanks all for your comenta and help.
Someone in the fórum could share a piece of code that rotates a JPG clickwise 90 degrees ?

See Rotate that Picture! – Xojo Programming Blog

Correct, but this doesn’t answer the question: should a Xojo picture keep the metadata (so this is a bug) or it’s a valid limitation?

Like I said: Picture is for picture data, not metadata. There are all kinds of metadata that could be stored in image files and it wouldn’t make much sense to store all of these in a Picture object.

Right, I should have said “keep the metadata in another class, possibly a property of the picture”.
In all means, we (Xojo users) should be able to deal with the metadata, especially when the ignored data produce a picture that gets rotated.

But it isn’t ignored – not under macOS anyway and I would be surprised if this was an issue under Windows. Having said that there could be an issue with conflicting orientation tags as there can be more than one (just like there is more than just one kind of metadata).

I guess Xojo is trying to hide the complexity for us, but if we can’t retrieve the orientation, how can we deal with using DrawPicture (for instance), where the orientation is reset?

Not to go off the thread too much, but on OSX you can call mdls from a shell to get a file’s metadata and for an image file this has the exif data.
Regards - Richard.

@Michael_Hußmann Thanks for your response. I forgot to say that I am using XOJO 2019 and on that version Translate and Rotate are not members of Graphics type.

Public Function RotateClockwise90Degrees(aPicture as Picture) As Picture
  var originalWidth as Integer = aPicture.Width
  var maxX as Integer = originalWidth - 1
  var originalHeight as Integer = aPicture.Height
  var maxY as Integer = originalHeight - 1
  var rotatedPicture as new Picture(originalHeight, originalWidth)
  var originalRGBSurface as RGBSurface = aPicture.RGBSurface
  var rotatedRGBSurface as RGBSurface = rotatedPicture.RGBSurface
  var x, y as Integer
  
  for y = 0 to maxY
    for x = 0 to maxX
      rotatedRGBSurface.Pixel(maxY - y, maxX - x) = originalRGBSurface.Pixel(x, y)
    next x
  next y
  
  return rotatedPicture
End Function
1 Like

And with MBS:

dim options as new Dictionary
options.Value(CGImageSourceMBS.kCGImageSourceThumbnailMaxPixelSize) = myPreviewSize
options.Value(CGImageSourceMBS.kCGImageSourceCreateThumbnailFromImageAlways) = true
options.Value(CGImageSourceMBS.kCGImageSourceCreateThumbnailWithTransform) = True

dim theCGImage as CGImageMBS = CGImageSourceMBS.CreateThumbnailMT(MimeData, 0, options)
PreviewPic = theCGImage.Picture

In general the images show up rotated 180 degrees. And yes, this should be handled by Xojo.

Have we established that it isn’t? Under Windows that is, because under macOS, Xojo does respect orientation tags and can deal with rotated pictures just fine.

Perhaps this changed since I implemented my code for macOS. I remember seeing rotated images. Was this after resizing?

In the project I’m working on (under macOS) I’m dealing with all kinds of pictures from various sources and they all open with the correct orientation; resizing (I’m creating scaled down previews and thumbnails) doesn’t change that.

As I was curious I took a photograph with my iPhone, first in landscape orientation, then in portrait orientation, then again in landscape orientation, but I rotated it in the Photos app. All three JPEG files had the correct orientation tag in their Exif data (default – i.e. landscape –, portrait, and again portrait) and they were opened by Xojo in the correct orientation as specified by those tags.

In the MBS plugins, you can often make use of the GraphicsMagik functions to detect the orientation, and to rotate.

//rotation
dim p as picture =  somepicture.Rotate90MBS

//orientation

dim gorig as  GMImageMBS
try
  gorig = new GMImageMBS(f)
catch
end try


  if gorig <> nil then
    
    try
      gorig.autoOrient    //detect orientation info and auto rotate to compensate
    catch
    end try
end if
1 Like