| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Imager::ExifOrientation; | 
| 2 | 2 |  |  | 2 |  | 11 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 74 |  | 
| 3 | 2 |  |  | 2 |  | 9 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 135 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = '0.07'; | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 11 | use Carp; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 156 |  | 
| 7 | 2 |  |  | 2 |  | 1363 | use Imager; | 
|  | 2 |  |  |  |  | 52830 |  | 
|  | 2 |  |  |  |  | 14 |  | 
| 8 | 2 |  |  | 2 |  | 4968 | use Image::ExifTool 'ImageInfo'; | 
|  | 2 |  |  |  |  | 115749 |  | 
|  | 2 |  |  |  |  | 359 |  | 
| 9 | 2 |  |  | 2 |  | 3671 | use Image::ExifTool::Exif; | 
|  | 2 |  |  |  |  | 59790 |  | 
|  | 2 |  |  |  |  | 1009 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | my $orientation_revers = {}; | 
| 12 |  |  |  |  |  |  | while (my($k, $v) = each %Image::ExifTool::Exif::orientation) { | 
| 13 |  |  |  |  |  |  | $orientation_revers->{$v} = $k; | 
| 14 |  |  |  |  |  |  | } | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub rotate { | 
| 17 | 0 |  |  | 0 | 0 | 0 | my($class, %opts) = @_; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 0 |  |  |  |  | 0 | my $img = Imager->new; | 
| 20 | 0 |  |  |  |  | 0 | my $exif; | 
| 21 | 0 | 0 |  |  |  | 0 | if ($opts{data}) { | 
|  |  | 0 |  |  |  |  |  | 
| 22 | 0 |  |  |  |  | 0 | $exif = ImageInfo(\$opts{data}); | 
| 23 | 0 |  |  |  |  | 0 | $img->read( data => $opts{data}, type => 'jpeg' ); | 
| 24 |  |  |  |  |  |  | } elsif ($opts{path}) { | 
| 25 | 0 |  |  |  |  | 0 | $exif = ImageInfo($opts{path}); | 
| 26 | 0 |  |  |  |  | 0 | $img->read( file => $opts{path}, type => 'jpeg' ); | 
| 27 |  |  |  |  |  |  | } else { | 
| 28 | 0 |  |  |  |  | 0 | croak "usage: | 
| 29 |  |  |  |  |  |  | Imager::ExifOrientation->rotate( path => '/foo/bar/baz.jpg' ) | 
| 30 |  |  |  |  |  |  | Imager::ExifOrientation->rotate( data => \$jpeg_data ) | 
| 31 |  |  |  |  |  |  | "; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  | 0 |  |  | 0 | my $orientation = $class->get_orientation_by_exiftool($exif || {}); | 
| 35 | 0 |  |  |  |  | 0 | $class->_rotate($img, $orientation); | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub get_orientation_by_exiftool { | 
| 39 | 0 |  |  | 0 | 0 | 0 | my($class, $exif) = @_; | 
| 40 | 0 | 0 |  |  |  | 0 | return 1 unless $exif->{Orientation}; | 
| 41 | 0 |  | 0 |  |  | 0 | return $orientation_revers->{$exif->{Orientation}} || 1; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | my $rotate_maps = { | 
| 45 |  |  |  |  |  |  | 1 => { right => 0,   mirror => undef }, # Horizontal (normal) | 
| 46 |  |  |  |  |  |  | 2 => { right => 0,   mirror => 'h'   }, # Mirror horizontal | 
| 47 |  |  |  |  |  |  | 3 => { right => 0,   mirror => 'hv'  }, # Rotate 180 (rotate is too noisy) | 
| 48 |  |  |  |  |  |  | 4 => { right => 0,   mirror => 'v'   }, # Mirror vertical | 
| 49 |  |  |  |  |  |  | 5 => { right => 270, mirror => 'h'   }, # Mirror horizontal and rotate 270 CW | 
| 50 |  |  |  |  |  |  | 6 => { right => 90,  mirror => undef }, # Rotate 90 CW | 
| 51 |  |  |  |  |  |  | 7 => { right => 90,  mirror => 'h'   }, # Mirror horizontal and rotate 90 CW | 
| 52 |  |  |  |  |  |  | 8 => { right => 270, mirror => undef }, # Rotate 270 CW | 
| 53 |  |  |  |  |  |  | }; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub _rotate { | 
| 56 | 16 |  |  | 16 |  | 33 | my($class, $img, $orientation) = @_; | 
| 57 | 16 |  |  |  |  | 37 | my $map = $rotate_maps->{$orientation}; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 16 | 100 |  |  |  | 63 | if ($map->{mirror}) { | 
| 60 | 10 |  |  |  |  | 44 | $img->flip( dir => $map->{mirror} ); | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 16 | 100 |  |  |  | 477 | if ($map->{right}) { | 
| 64 | 8 |  |  |  |  | 31 | return $img->rotate( right => $map->{right} ); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 8 |  |  |  |  | 24 | $img; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | 1; | 
| 71 |  |  |  |  |  |  | __END__ |