Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This program checks whether an image is able to be embedded by AtomicParsley.
#!/usr/bin/env perl
=pod
This program checks whether an image is able to be embedded by AtomicParsley.
=cut
use strict;
use warnings;
my $img = $ARGV[0] or die "usage: $0 image\n";
open IN, "<$img" or die "image file could not be opened: $!\n";
my $header = undef;
my $status = read IN, $header, 8;
close IN;
die "image file could not be read.\n" unless( defined $status );
my @bytes = unpack("C*", $header);
printf '%d bytes read, %s%s', $status, join(' ', map { sprintf('%02X',$_) } @bytes), "\n";
my $head8 = join '', ( map { sprintf('\x%02X', $_) } @bytes );
my $head4 = join '', ( map { sprintf('\x%02X', $_) } (@bytes[0..3]) );
if( $head4 eq q'\xFF\xD8\xFF\xE0' || $head4 eq q'\xFF\xD8\xFF\xE1' ){
print "jpeg\n";
}elsif( $head8 eq q'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A' ){
print "png\n";
}else{
print "image file is not jpg/png and cannot be embedded.\n";
}
1;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment