Image::PNG::Libpng: "libpng error: Call to NULL write function"

Image::PNG::Libpng: "libpng error: Call to NULL write function"

我有一些代码应该使用 libpng 的 Perl 绑定修改 PNG 文件的 tEXt 或 zTXt 块。

代码:

use strict;
use warnings;
use Data::Dumper qw(Dumper);
use Image::PNG::Libpng qw(read_png_file write_png_file) ;


my $lpng = read_png_file ("testimg.png");

my @cmdAr = ();
for(my $i=0; $i<10; ++$i){
  push(@cmdAr, {compression => Image::PNG::Libpng::PNG_TEXT_COMPRESSION_zTXt ,
                key => sprintf("par_%02i",$i),
                text => sprintf("%06i", $i*$i)
               }
      );
}
print STDERR Dumper(\@cmdAr);
$lpng->set_text (\@cmdAr);
$lpng->write_png_file ("testimg2.png");

但是代码在写函数处失败:

libpng error: Call to NULL write function

关于如何解决这个问题的任何提示?文件存在,进程也有写入权限。

我的 Perl 版本(最新的 Strawberry Perl):

This is perl 5, version 26, subversion 0 (v5.26.0) built for MSWin32-x86-multi-thread-64int

libpng 模块的版本:

cpan -D Image::PNG::Libpng

CPAN: CPAN::SQLite loaded ok (v0.211)
Database was generated on Thu, 15 Jun 2017 10:43:51 GMT

Image::PNG::Libpng
-------------------------------------------------------------------------
    CPAN: Module::CoreList loaded ok (v5.20170530)
(no description)
    B/BK/BKB/Image-PNG-Libpng-0.43.tar.gz
    C:\strawberry-perl\perl\site\lib\Image\PNG\Libpng.pm
    Installed: 0.43
    CPAN:      0.43  up to date
    Ben Bullock (BKB)
    bkb@cpan.org

该模块的作者向我指出了以下功能文档: https://metacpan.org/pod/Image::PNG::Libpng#write_png_file 上面写着:

$png must be the object created by "create_write_struct", so "read_png_file" followed by a call to this does not work.

$png->copy_png 可用于创建 write-able 结构。

因此在调用 set_text 之前插入 $lpng = $lpng->copy_png(); 会产生预期的行为。

完整(和清理过的)代码供参考:

use strict;
use warnings;
use Image::PNG::Libpng qw(read_png_file write_png_file) ;



my @txts = ({compression => Image::PNG::Libpng::PNG_TEXT_COMPRESSION_zTXt,
             key          => "myImportentTextData",
             text         => "fooBar"
            });

my $png = read_png_file ("testimg.png");

$png = $png->copy_png();  #< this is important!

$png->set_text (\@txts);
$png->write_png_file ("testimg2.png");