#------------------------------------------------------------------------------
# File:         convert_regions.config
#
# Description:  User-defined Composite tag definitions to allow conversion of
#               face regions between Microsoft Windows Live Photo Gallery (WLPG)
#               and Metadata Working Group (MWG) formats
#
# Usage:        1) Convert from MP WLPG regions to MWG regions:
#
#               exiftool -config convert_regions.config "-regioninfo<myregion" FILE
#
#               2) Convert from MWG to WLPG regions:
#
#               exiftool -config convert_regions.config "-regioninfomp<myregionmmp" FILE
#
# Requires:     ExifTool version 8.82 or later
#
# Revisions:    2012/12/27 - P. Harvey Created
#               2013/02/20 - PH Don't add ignored MP faces
#
# References:   http://www.metadataworkinggroup.org/specs/
#------------------------------------------------------------------------------

%Image::ExifTool::UserDefined = (

    'Image::ExifTool::Composite' => {

        # create an MWG RegionInfo structure from a Microsoft RegionInfoMP structure
        MyRegion => {
            Require => {
                0 => 'RegionInfoMP',
                1 => 'ImageWidth',
                2 => 'ImageHeight',
            },
            ValueConv => q{
                my ($rgn, @newRgns);
                foreach $rgn (@{$val[0]{Regions}}) {
                    # don't add ignored faces
                    next if $$rgn{PersonDisplayName} eq 'ffffffffffffffff';
                    my @rect = split /\s*,\s*/, $$rgn{Rectangle};
                    my %newRgn = (
                        Area => {
                            X => $rect[0] + $rect[2]/2,
                            Y => $rect[1] + $rect[3]/2,
                            W => $rect[2],
                            H => $rect[3],
                            Unit => 'normalized',
                        },
                        Name => $$rgn{PersonDisplayName},
                        Type => 'Face',
                    );
                    push @newRgns, \%newRgn;
                }
                return {
                    AppliedToDimensions => { W => $val[1], H => $val[2], Unit => 'pixel' },
                    RegionList => \@newRgns,
                };
            },
        },

        # create a Microsoft RegionInfoMP structure from an MWG RegionInfo structure
        MyRegionMP => {
            Require => 'RegionInfo',
            ValueConv => q{
                my ($rgn, @newRgns);
                foreach $rgn (@{$val[0]{RegionList}}) {
                    my @rect = @{$$rgn{Area}}{'X','Y','W','H'};
                    $rect[0] -= $rect[2]/2;
                    $rect[1] -= $rect[3]/2;
                    push @newRgns, {
                        PersonDisplayName => $$rgn{Name},
                        Rectangle => join(', ', @rect),
                    };
                }
                return { Regions => \@newRgns };
            },
        },
    },
);

1;  #end
