0

Basically my question is like this one, but from PNG instead of JPEG.

More specifically, I have a bunch of PNG images concatenated together and I want to discover their lengths, so I can split the stream in pieces corresponding to individual images.

I do not need to decode or validate the images. I can assume that the input stream is composed of valid PNG images and do not want to verify that. Instead, it is useful for me to do this as quickly as possible, so the less amount of decoding operations are required, the better.

1
  • 1
    That is way more easy than with JPEGs, because PNG files have a valid starting sig and end-of-file chunk. Add up chunk sizes until you encounter IEND, and you're done. See the specifications. Commented Jan 29, 2015 at 8:45

1 Answer 1

1

Here you have a perl script, based on this answer

#!/usr/bin/perl undef $/; $_ = <>; $n = 0; for $match (split(/(?=\x{89}PNG\x{0d}\x{0a}\x{1a}\x{0a})/)) { open(O, sprintf('>temp%04d.png',++$n)); print O $match; close(O); } 

Save this as, say splitpng.pl and run perl splitpng.pl < myfile

This is not 100% foolproof (the rigourous way would be to count chunks sizes, as per Jongware's comment), but the probabilty of having that signature inside a PNG should be small.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.