Reading the orientation tag
A JPEG stores EXIF metadata in a block inside the file. The relevant entry is Orientation (0x0112), an integer 1 through 8: 1 is normal, 3 is 180 degrees, 6 is 90 degrees clockwise, 8 is 90 degrees counterclockwise, and 2, 4, 5, 7 are mirror variants. The tool locates the tag, reads the value, and determines the transform it calls for. A value of 1, or a file with no tag at all, means there is nothing to correct.