#------------------------------------------------------------------------------
# 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<myregionmp" 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
#               2017/02/13 - PH Handle MP regions without Rectangle or Name entries
#
# 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}}) {
                    my $name = $$rgn{PersonDisplayName};
                    next unless $$rgn{Rectangle} or defined $name;
                    my %newRgn = ( Type => 'Face' );
                    if (defined $name) {
                        # don't add ignored faces
                        next if $name eq 'ffffffffffffffff';
                        $newRgn{Name} = $name;
                    }
                    if ($$rgn{Rectangle}) {
                        my @rect = split /\s*,\s*/, $$rgn{Rectangle};
                        $newRgn{Area} = {
                            X => $rect[0] + $rect[2]/2,
                            Y => $rect[1] + $rect[3]/2,
                            W => $rect[2],
                            H => $rect[3],
                            Unit => 'normalized',
                        } if @rect == 4;
                    }
                    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}}) {
                    next unless $$rgn{Area} or defined $$rgn{Name};
                    my %newRgn;
                    if ($$rgn{Area}) {
                        my @rect = @{$$rgn{Area}}{'X','Y','W','H'};
                        $rect[0] -= $rect[2]/2;
                        $rect[1] -= $rect[3]/2;
                        $newRgn{Rectangle} = join(', ', @rect);
                    }
                    $newRgn{PersonDisplayName} = $$rgn{Name} if defined $$rgn{Name};
                    push @newRgns, \%newRgn;
                }
                return { Regions => \@newRgns };
            },
        },
    },
);

1;  #end
