]> Dogcows Code - chaz/yoink/blob - scripts/textureattr.pl
fixed documentation about where to find licenses
[chaz/yoink] / scripts / textureattr.pl
1 #!/usr/bin/env perl
2
3 #
4 # A wrapper around pngcrush to read and write the texture attributes of a
5 # Yoink texture in PNG format.
6 #
7
8 my $infile = shift or die "Missing argument";
9 my $outfile = shift;
10
11 if ($infile eq "-h" or $infile eq "--help") {
12 print <<END
13 Edit the texture attributes of a Yoink texture file.
14 Usage: texture_edit.pl infile.png [outfile.png]
15 If outfile.png is not specified, infile.png will be written
16 back onto itself.
17 END
18 ;
19 exit 0;
20 }
21
22 my $key = "X-Yoink-Texture";
23
24 use File::Copy;
25 use File::Temp;
26
27 # Handle file contents as a whole, not line-by-line.
28 local $/;
29
30 # Read in the image file for the first time.
31 open FILE, $infile or die $!;
32 my $bytes = <FILE>;
33 close(FILE);
34
35 # See if we can't determine what the text currently is.
36 my $text = "";
37 if ($bytes =~ m/(.{4})tEXt$key(\0)/g)
38 {
39 my $offset = pos($bytes);
40 my $size = unpack("N", $1) - (length($key) + 1);
41 $text = substr($bytes, $offset, $size);
42 }
43
44 # Write that text to a temporary file.
45 $tmpfile = new File::Temp(UNLINK => 1);
46 print $tmpfile $text;
47
48 # Allow the user a chance to edit the text.
49 system("\${EDITOR:-vi} $tmpfile");
50 (0 == $? >> 8) or die "Editor exited with an error";
51
52 # Reread the text from the temporary file.
53 seek $tmpfile, 0, 0;
54 $text = <$tmpfile>;
55
56 # Use pngcrush to rewrite the image file with the new text.
57 system("pngcrush", "-fix", "-oldtimestamp", "-rem", "text", "-text", "b",
58 $key, $text, $infile, $tmpfile);
59 if (0 == $? >> 8) {
60 not $outfile and $outfile = $infile;
61 seek $tmpfile, 0, 0;
62 copy($tmpfile, $outfile) or die $!;
63 } else {
64 die "pngcrush exited with an error";
65 }
66
This page took 0.038765 seconds and 5 git commands to generate.