Hello monks,
I've been reading about PNG files and pack/unpack for a little while, and thought I understood it enough to perform a simple operation, but apparently I was mistaken. I created a PNG file with another script, and now I'm trying to get to know pack/unpack so I can read/write the associated binary data. The script doesn't error, but only makes a single byte file with "very short lines (no magic)" as a result. Here is the code:
#!/usr/bin/perl
use strict;
use warnings;
my $ifh = open("<:raw", "./file-bar.png");
my $data = unpack('a*', $ifh);
my $outputfile = "newfile.png";
open (my $ofh, '>:raw', $outputfile)
or die "Could not open '$outputfile' $!";
print $ofh pack('a*', $data);
close $ofh;
print("Image created.\n");
Here are the file types of the initial and created files:
$ file file-bar.png
file-bar.png: PNG image data, 400 x 300, 4-bit colormap, interlaced
$ file newfile.png
newfile.png: very short file (no magic)
I've tried to piece together bits of code from various sources, so that's why it's not of the same style, but feel there's no point in cleaning up code that doesn't work yet. Any help is greatly appreciated.