X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=scripts%2Ftextureattr.pl;fp=scripts%2Ftextureattr.pl;h=a12c2fdbfdb5ca430c84719bb723c7446f68fb6f;hp=0000000000000000000000000000000000000000;hb=6c9943707d4f33035830eba0587a61a34eaecbc2;hpb=af88821a172c4dfd138b91b2a5148ae50b502fa2 diff --git a/scripts/textureattr.pl b/scripts/textureattr.pl new file mode 100755 index 0000000..a12c2fd --- /dev/null +++ b/scripts/textureattr.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl + +# +# A wrapper around pngcrush to read and write the texture attributes of a +# Yoink texture in PNG format. +# + +my $infile = shift or die "Missing argument"; +my $outfile = shift; + +if ($infile eq "-h" or $infile eq "--help") { + print <; +close(FILE); + +# See if we can't determine what the text currently is. +my $text = ""; +if ($bytes =~ m/(.{4})tEXt$key(\0)/g) +{ + my $offset = pos($bytes); + my $size = unpack("N", $1) - (length($key) + 1); + $text = substr($bytes, $offset, $size); +} + +# Write that text to a temporary file. +$tmpfile = new File::Temp(UNLINK => 1); +print $tmpfile $text; + +# Allow the user a chance to edit the text. +system("\${EDITOR:-vi} $tmpfile"); +(0 == $? >> 8) or die "Editor exited with an error"; + +# Reread the text from the temporary file. +seek $tmpfile, 0, 0; +$text = <$tmpfile>; + +# Use pngcrush to rewrite the image file with the new text. +system("pngcrush", "-fix", "-rem", "text", "-text", "b", + $key, $text, $infile, $tmpfile); +if (0 == $? >> 8) { + not $outfile and $outfile = $infile; + seek $tmpfile, 0, 0; + copy($tmpfile, $outfile) or die $!; +} else { + die "pngcrush exited with an error"; +} +