Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Using pack/unpack on a PNG file

by sierpinski (Chaplain)
on May 25, 2015 at 21:44 UTC ( [id://1127724]=perlquestion: print w/replies, xml ) Need Help??

sierpinski has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Using pack/unpack on a PNG file
by AnomalousMonk (Archbishop) on May 26, 2015 at 03:12 UTC
    my $ifh = open("<:raw", "./file-bar.png");

    Wishful, also quite surprising that Perl seems happy with it even with warnings and strictures enabled. In any event, the "successful" result (1) of open-ing the file is assigned to  $ifh and this is packed and written (apparently also "successfully") to another file. But it's still junk.

    c:\@Work\Perl\monks\sierpinski>perl -wMstrict -MData::Dump -le "my $ifh = open('<:raw', 'junk.txt'); dd $ifh; " 1
    Can anyone explain to me how this works — with strictures enabled?


    Give a man a fish:  <%-(-(-(-<

      Documented behaviour:
      (...) if FILEHANDLE is an expression, its value is the real filehandle. (This is considered a symbolic reference, so use strict "refs" should not be in effect.)

      system 'echo abc > 1.txt'; open '<:raw', '1.txt' or die $!; print while readline '<:raw'; unlink '1.txt';
      Update: I wouldn't recomend it, though, as it's confusing to a reader :-) The file is still opened with default layers:
      print PerlIO::get_layers("<:raw"); # unixperlio
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        This is considered a symbolic reference, so use strict "refs" should not be in effect.

        Yes, but if strictures are enabled the example still runs (and strictures were also enabled above), so I'm still confused:

        c:\@Work\Perl\monks\sierpinski>perl -wMstrict -le "system 'echo abc > 1.txt'; open '<:raw', '1.txt' or die $!; print while readline '<:raw'; " abc

        Update: I wouldn't recomend it, though, as it's confusing to a reader ... Darn right!


        Give a man a fish:  <%-(-(-(-<

Re: Using pack/unpack on a PNG file
by RichardK (Parson) on May 25, 2015 at 22:49 UTC
Re: Using pack/unpack on a PNG file
by Anonymous Monk on May 26, 2015 at 00:42 UTC

    Also, this is wishful thinking :) my $ifh = open("<:raw", "./file-bar.png");

Re: Using pack/unpack on a PNG file
by sierpinski (Chaplain) on May 26, 2015 at 03:03 UTC

      See also perlpacktut.

      Update: In general, pack/unpack are almost as much fun as regexes!


      Give a man a fish:  <%-(-(-(-<

      Also consider:

      c:\@Work\Perl\monks\sierpinski>perl -wMstrict -le "my $s = 'some string -- could be any length'; ;; my $up = unpack 'a*', $s; $s eq $up or die 'A: not identity operation'; ;; my $p = pack 'a*', $up; $s eq $p or die 'B: not identity operation'; ;; print 'a-ok'; " a-ok


      Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1127724]
Approved by philipbailey
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-19 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found