]> Dogcows Code - chaz/yoink/blobdiff - scripts/textureattr.pl
build system enhancements
[chaz/yoink] / scripts / textureattr.pl
diff --git a/scripts/textureattr.pl b/scripts/textureattr.pl
new file mode 100755 (executable)
index 0000000..a12c2fd
--- /dev/null
@@ -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 <<END
+Edit the texture attributes of a Yoink texture file.
+Usage: texture_edit.pl infile.png [outfile.png]
+If outfile.png is not specified, infile.png will be written
+back onto itself.
+END
+;
+       exit 0;
+}
+
+my $key = "X-Yoink-Texture";
+
+use File::Copy;
+use File::Temp;
+
+# Handle file contents as a whole, not line-by-line.
+local $/;
+
+# Read in the image file for the first time.
+open FILE, $infile or die $!;
+my $bytes = <FILE>;
+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";
+}
+
This page took 0.02074 seconds and 4 git commands to generate.