Embedding ExifTool Perl module in my C++ image codec

Started by boxerab, December 29, 2020, 07:20:34 PM

Previous topic - Next topic

boxerab

Hello!
I have embedded a Perl interpreter into my C++ JPEG 2000 codec using
this guide:  https://perldoc.perl.org/perlembed

Next, I would like to run a small script that uses the ExifTool Perl module,
to copy Exif data from source TIFF image to compressed JPEG 2000 file.

I would like to call the equivalent of this command line:

$ exiftool -TagsFromFile foo.tif "-all:all>all:all" foo.jp2

Has anyone done something like this before, and if so, any caveats before I take the plunge ?

Thank you!
Aaron

Phil Harvey

Hi Aaron,

I've never done this from C++, but the basic Perl script is trival:

use Image::ExifTool;
my $exifTool = new Image::ExifTool;
$exifTool->SetNewValuesFromFile('foo.tif', 'all:all');
$exifTool->WriteInfo('foo.jp2');


- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

boxerab

Hi Phil,
Thanks for the quick reply!
Unfortunately, that script does not replicate the command line.

Here is the script I am using

use Image::ExifTool qw(ImageInfo);

$srcFile = $ARGV[0];
$outFile = $ARGV[1];

my $exifTool = new Image::ExifTool;

# set new values from all information in a file...
my $info = $exifTool->SetNewValuesFromFile($srcFile, 'all:all');
# ...then write these values to another image
my $result = $exifTool->WriteInfo($outFile);


Thanks,
Aaron

boxerab


boxerab

Here is how I integrated the Perl module:

void transferTags(std::string src, std::string dest){
    std::string script {R"x(
use Image::ExifTool qw(ImageInfo);
use strict;
use warnings;
my $srcFile = $ARGV[0];
my $outFile = $ARGV[1];
my $exifTool = new Image::ExifTool;
my $info = $exifTool->SetNewValuesFromFile($srcFile, 'all:all');
my $result = $exifTool->WriteInfo($outFile);
    )x"};
    static constexpr int NUM_ARGS = 5;
    const char* embedding[NUM_ARGS] = { "", "-e", "0", src.c_str(), dest.c_str() };
    my_perl = perl_alloc();
    perl_construct( my_perl );
    perl_parse(my_perl, NULL, NUM_ARGS, (char**)embedding, NULL);
    perl_run(my_perl);
    eval_pv(script.c_str(), TRUE);
    perl_destruct(my_perl);
    perl_free(my_perl);
}

Phil Harvey

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).