use warnings;
use strict;
use Image::PNG::Libpng ':all';
use Data::Dumper;

# Ok to connect to web?

my $webok;

eval {
    require Data::Validate::URI;
    Data::Validate::URI->import (qw/is_web_uri/);
    require LWP::Simple;
    LWP::Simple->import ();
    $webok = 1;

if ($@) {
    $webok = 0;

binmode STDOUT, ":utf8";

for my $file (@ARGV) {
    my $png;
    if (-f $file) {
	$png = read_png_file ($file);
    else {
	if ($webok && is_web_uri ($file)) {
	    print "Getting '$file' from web.\n";
	    my $png_data = get ($file);
	    if ($png_data) {
		$png = read_from_scalar ($png_data);
	    else {
		warn "Could not download '$file', skipping.\n";
	else {
	    warn "Cannot find a file called '$file', skipping.\n";

    my $ihdr = $png->get_IHDR ();
    for my $k (sort keys %$ihdr) {
	if ($k eq 'color_type') {
	    print "$k: ", color_type_name ($ihdr->{$k}), "\n";
	else {
	    print "$k: $ihdr->{$k}\n";
    my $valid = $png->get_valid ();
    for my $key (sort keys %$valid) {
	if ($valid->{$key}) {
	    print "Valid $key chunk.\n";
	    if ($key ne 'IDAT' && $key ne 'tIME') {
		my $value = $png->get_chunk ($key);
		print Dumper ($value);
    my $text = $png->get_text ();
    if ($text) {
	my $i = 0;
        for my $t (@$text) {
            print "Text chunk $i:\n";
            for my $k (sort keys %$t) {
                my $v = $t->{$k};
                if (! defined $v) {
                    $v = 'undefined';
		if ($k eq 'compression') {
		    $v = text_compression_name ($v);
                print "$k: $v\n";
            print "\n";
    my $time = $png->get_tIME ();
    if ($time) {
	print "Modification time:\n";
	for my $k (qw/year month day hour minute second/) {
	    print "$k: $time->{$k}\n";



=head1 NAME

pnginspect - print contents of PNG files


    pnginspect a.png b.png c.png


Print a text representation of the data within a PNG image file to
standard output. It reads the PNG file, prints out its header
information and its valid chunks, and then attempts to read all the
chunks. Most of the chunks are printed using
L<Data::Dumper>. Modification time and text segments are printed

The image data is not currently printed out.

If you have L<Data::Validate::URI> and L<LWP::Simple> installed, you
can also use it to examine PNG files on the web, like this:

    pnginspect http://libpng.org/pub/png/img_png/libpng-88x31.png

These modules are not installed by default.

=head1 SEE ALSO


=item L<Image::PNG::Libpng>


=head1 AUTHOR

Ben Bullock <bkb@cpan.org>


# Local variables:
# mode: perl
# End: