Skip to content

Instantly share code, notes, and snippets.

@wittyfool
Created February 26, 2015 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wittyfool/8fc20baffaeab2990a2d to your computer and use it in GitHub Desktop.
Save wittyfool/8fc20baffaeab2990a2d to your computer and use it in GitHub Desktop.
Remove IDAT chunk of length 0 for png
#!/usr/bin/perl -s
# photohop 等で作成するpng には
# 長さ0のIDAT チャンクが作成される事がある
# ImageMagick等でエラーとなり読み込めない事があるので取り除く
#
$src = shift || usage();
$dst = shift || usage();
sub usage {
print "Remove IDAT chunk of size 0\n";
print "$0 src-pngfile dst-pngfile\n";
exit 0;
}
open(FP, '<', $src) || die "Can't open [$src] to read";
if(read(FP, $id, 8) != 8){
die "Can't read identifier...";
};
if(! $id =~ /PNG/){
die "not png???\n";
}
open(OUT, '>', $dst) || die;
print OUT $id;
while(1){
(read(FP, $_, 8) == 8) || last;
($len, $name) = unpack("N a4", $_);
print "[$len] [$name]\n" if($debug);
read(FP, $data, $len);
read(FP, $crc, 4);
next if(($len == 0)&& $name =~ /IDAT/);
print OUT $_;
print OUT $data;
print OUT $crc;
}
print OUT $_;
close(FP);
close(OUT);
print "Done\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment