Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use ReadRGBAImageOriented with Exif Orientation? #95

Open
catester opened this issue Mar 17, 2023 · 1 comment
Open

How to use ReadRGBAImageOriented with Exif Orientation? #95

catester opened this issue Mar 17, 2023 · 1 comment

Comments

@catester
Copy link

For example I read Orientation exif value from tiff file like this:

var orientation = Orientation.BOTLEFT;
var value = tiff.GetField(TiffTag.ORIENTATION);
if (value != null)
    orientation = value[0].ToUShort();

And for example if orientation value is RIGHTTOP and reading the image like this

tiff.ReadRGBAImageOriented(width, height, raster, Orientation.RIGHTTOP)

The raster image is flipped vertically, but RIGHTTOP means rotate 90 degrees clockwise.

In you docs, I read that orientation parameter in ReadRGBAImageOriented method is "The raster origin position".
So how do we map exif orientation value to this raster origin position?

I noticed you need to use BOTLEFT to get correct image when the exif value is TOPLEFT which I thought should be the default value for orientation. So we need to reverse every value? But by which logic?

I thought ReadRGBAImageOriented method was intended for easily getting an oriented frame.

@catester
Copy link
Author

Ok I found a partial solution:

tiff.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT); //image orientation
tiff.ReadRGBAImageOriented(width, height, raster, Orientation.BOTLEFT) //requested orientation

So I reset the image orientation tag to the default value before calling ReadRGBAImageOriented (requested orientation should also be the default BOTLEFT) so that at least I can get the image as it is (no strange flipping). Later I can transform the image with the original orientation tag (backed up) with my own code.

var originalOrientation = Orientation.TOPLEFT; //default
var value = tiff.GetField(TiffTag.ORIENTATION);
if (value != null)
    originalOrientation = value[0].ToUShort(); //back up original orientation if exists

tiff.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT); //image orientation
tiff.ReadRGBAImageOriented(width, height, raster, Orientation.BOTLEFT) //requested orientation

//Now process originalOrientation here (flip/rotate raster)

Further findings: The tile and strip get methods call setorientation method in TiffRgbaImage which I cannot make sense of. I guess this library does not internally handle rotation at all? Also in my case the flipping was also not correct.

private int setorientation()
{
switch (orientation)
{
case Orientation.TOPLEFT:
case Orientation.LEFTTOP:
if (req_orientation == Orientation.TOPRIGHT || req_orientation == Orientation.RIGHTTOP)
return FLIP_HORIZONTALLY;
else if (req_orientation == Orientation.BOTRIGHT || req_orientation == Orientation.RIGHTBOT)
return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
else if (req_orientation == Orientation.BOTLEFT || req_orientation == Orientation.LEFTBOT)
return FLIP_VERTICALLY;
return 0;
case Orientation.TOPRIGHT:
case Orientation.RIGHTTOP:
if (req_orientation == Orientation.TOPLEFT || req_orientation == Orientation.LEFTTOP)
return FLIP_HORIZONTALLY;
else if (req_orientation == Orientation.BOTRIGHT || req_orientation == Orientation.RIGHTBOT)
return FLIP_VERTICALLY;
else if (req_orientation == Orientation.BOTLEFT || req_orientation == Orientation.LEFTBOT)
return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
return 0;
case Orientation.BOTRIGHT:
case Orientation.RIGHTBOT:
if (req_orientation == Orientation.TOPLEFT || req_orientation == Orientation.LEFTTOP)
return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
else if (req_orientation == Orientation.TOPRIGHT || req_orientation == Orientation.RIGHTTOP)
return FLIP_VERTICALLY;
else if (req_orientation == Orientation.BOTLEFT || req_orientation == Orientation.LEFTBOT)
return FLIP_HORIZONTALLY;
return 0;
case Orientation.BOTLEFT:
case Orientation.LEFTBOT:
if (req_orientation == Orientation.TOPLEFT || req_orientation == Orientation.LEFTTOP)
return FLIP_VERTICALLY;
else if (req_orientation == Orientation.TOPRIGHT || req_orientation == Orientation.RIGHTTOP)
return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
else if (req_orientation == Orientation.BOTRIGHT || req_orientation == Orientation.RIGHTBOT)
return FLIP_HORIZONTALLY;
return 0;
}
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants