#------------------------------------------------------------------------------
# File:         rotate_regions.config
#
# Description:  User-defined Composite tag definitions to rotate MWG region tags
#               (Metadata Working Group region, used by Picasa) and MP region tags
#               (used by Microsoft Photo Library).
#
# Tag definitions and examples:
#
#   RotateMWGRegionCW90
#   RotateMWGRegionCW180
#   RotateMWGRegionCW270
#     These tags will rotate a MWG Region clockwise 90, 180, or 270 degrees.
#     Example:
#       exiftool -config rotate_regions.config "-RegionInfo<RotateMWGRegionCW90" FILE
#
#   RotateMPRegionCW90
#   RotateMPRegionCW180
#   RotateMPRegionCW270
#     These tags will rotate a MWG Region clockwise 90, 180, or 270 degrees.
#     Example:
#       exiftool -config rotate_regions.config "-RegionInfoMP<RotateMPRegionCW90" FILE
#
# Revisions:    2015/05/08 - Bryan K. Williams AKA StarGeek Created
#------------------------------------------------------------------------------

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        RotateMWGRegionCW90 =>{
            Require => 'RegionInfo',
            ValueConv => q{
                my ($rgn, @newRgns);
                foreach $rgn (@{$val[0]{RegionList}}) {
                    my @rect = @{$$rgn{Area}}{'X','Y','W','H'};
                    my %newRgn = (
                        Area => {
                            X => 1-$rect[1],
                            Y => $rect[0],
                            W => $rect[3],
                            H => $rect[2],
                            Unit => 'normalized',
                        },
                        Name => $$rgn{Name},
                        Type => 'Face',
                    );
                    push @newRgns, \%newRgn;
                }
                return {
                    AppliedToDimensions => {
                        W => $val[0]{AppliedToDimensions}{W},
                        H => $val[0]{AppliedToDimensions}{H},
                        Unit => $val[0]{AppliedToDimensions}{Unit},
                    },
                    RegionList => \@newRgns,
                };
            },
        }, #End RotateMWGRegionCW90
        RotateMWGRegionCW180 =>{
            Require => 'RegionInfo',
            ValueConv => q{
                my ($rgn, @newRgns);
                foreach $rgn (@{$val[0]{RegionList}}) {
                    my @rect = @{$$rgn{Area}}{'X','Y','W','H'};
                    my %newRgn = (
                        Area => {
                            X => 1-$rect[0],
                            Y => 1-$rect[1],
                            W => $rect[2],
                            H => $rect[3],
                            Unit => 'normalized',
                        },
                        Name => $$rgn{Name},
                        Type => 'Face',
                    );
                    push @newRgns, \%newRgn;
                }
                return {
                    AppliedToDimensions => {
                        W => $val[0]{AppliedToDimensions}{W},
                        H => $val[0]{AppliedToDimensions}{H},
                        Unit => $val[0]{AppliedToDimensions}{Unit},
                    },
                    RegionList => \@newRgns,
                };
            },
        }, #End RotateMWGRegionCW180
        RotateMWGRegionCW270 =>{
            Require => 'RegionInfo',
            ValueConv => q{
                my ($rgn, @newRgns);
                foreach $rgn (@{$val[0]{RegionList}}) {
                    my @rect = @{$$rgn{Area}}{'X','Y','W','H'};
                    my %newRgn = (
                        Area => {
                            X => $rect[1],
                            Y => 1-$rect[0],
                            W => $rect[3],
                            H => $rect[2],
                            Unit => 'normalized',
                        },
                        Name => $$rgn{Name},
                        Type => 'Face',
                    );
                    push @newRgns, \%newRgn;
                }
                return {
                    AppliedToDimensions => {
                        W => $val[0]{AppliedToDimensions}{W},
                        H => $val[0]{AppliedToDimensions}{H},
                        Unit => $val[0]{AppliedToDimensions}{Unit},
                    },
                    RegionList => \@newRgns,
                };
            },
        }, #End RotateMWGRegionCW270
        RotateMPRegionCW90=>{
            Require => 'RegionInfoMP',
            ValueConv => q{
                my ($rgn, @newRgns);
                foreach $rgn (@{$val[0]{Regions}}) {
                    my @rect = split /\s*,\s*/, $$rgn{Rectangle};
                    my $temp = $rect[0];
                    $rect[0] = 1-$rect[1]-$rect[3];
                    $rect[1] = $temp;
                    ($rect[2], $rect[3]) = ($rect[3],$rect[2]);  #Swap W and H
                    push @newRgns, {
                        PersonDisplayName => $$rgn{PersonDisplayName},
                        Rectangle => join(', ', @rect),
                    };
                }
                return { Regions => \@newRgns };
            }
        }, #end RotateMPRegionCW90
        RotateMPRegionCW180=>{
            Require => 'RegionInfoMP',
            ValueConv => q{
                my ($rgn, @newRgns);
                foreach $rgn (@{$val[0]{Regions}}) {
                    my @rect = split /\s*,\s*/, $$rgn{Rectangle};
                    my $tempX = $rect[0];
                    my $tempY = $rect[1];
                    $rect[0] = 1-$tempX-$rect[2];
                    $rect[1] = 1-$tempY-$rect[3];
                    push @newRgns, {
                        PersonDisplayName => $$rgn{PersonDisplayName},
                        Rectangle => join(', ', @rect),
                    };
                }
                return { Regions => \@newRgns };
            }
        }, #end RotateMPRegionCW180
        RotateMPRegionCW270=>{
            Require => 'RegionInfoMP',
            ValueConv => q{
                my ($rgn, @newRgns);
                foreach $rgn (@{$val[0]{Regions}}) {
                    my @rect = split /\s*,\s*/, $$rgn{Rectangle};
                    my $temp = $rect[1];
                    $rect[1] = 1-$rect[0]-$rect[2];
                    $rect[0] = $temp;
                    ($rect[2], $rect[3]) = ($rect[3],$rect[2]);  #Swap W and H
                    push @newRgns, {
                        PersonDisplayName => $$rgn{PersonDisplayName},
                        Rectangle => join(', ', @rect),
                    };
                }
                return { Regions => \@newRgns };
            }
        }, #end RotateMPRegionCW270
    },
);

1;  #end
