Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- ### WARNING, arguments are not sufficiently sanitised for untrusted use.
- ### Requires ImageMagick's 'identify' and the Unix 'file' command
- use MIME::Base64;
- my $ARGPROBS=0;
- my $inimg;
- my $outname;
- my $desc="";
- my $type=0;
- while (my $arg = shift @ARGV) {
- if ($arg eq "-i") {
- if (@ARGV < 1) {
- print "$arg requires an argument\n";
- $ARGPROBS=1;
- }
- else {
- $inimg = shift @ARGV;
- }
- }
- elsif ($arg eq "-o") {
- if (@ARGV < 1) {
- print "$arg requires an argument\n";
- $ARGPROBS=1;
- }
- else {
- $outname = shift @ARGV;
- }
- }
- elsif ($arg eq "-desc") {
- if (@ARGV < 1) {
- print "$arg requires an argument\n";
- $ARGPROBS=1;
- }
- else {
- $desc = shift @ARGV;
- }
- }
- elsif ($arg eq "-type") {
- if (@ARGV < 1) {
- print "$arg requires an argument\n";
- $ARGPROBS=1;
- }
- else {
- $type = shift @ARGV;
- }
- }
- else {
- print "$arg not recognised\n";
- $ARGPROBS=1;
- }
- }
- if (! defined $inimg ) {
- print "Need an input image\n";
- $ARGPROBS=1;
- }
- if (! defined $outname ) {
- print "Need an output name\n";
- $ARGPROBS=1;
- }
- elsif ( -e $outname ) {
- print "$outname already exists\n";
- $ARGPROBS=1;
- }
- if ( $type !~ /^\d+$/ || $type > 20 ) {
- # Regex doesn't allow <0
- print "Type must be in range [0:20]\n";
- $ARGPROBS=1;
- }
- if ($ARGPROBS) {
- exit 1;
- }
- my %blockinfo = (
- type => $type,
- mime => "",
- desc => $desc,
- width =>0,
- height =>0,
- depth =>0,
- palette =>0,
- imagesize =>0
- );
- if ( ! -f $inimg ) {
- print "Couldn't find $inimg\n";
- exit 1;
- }
- my $mimeinfo= `file --mime-type -b $inimg`;
- chomp $mimeinfo;
- if ( $mimeinfo ne "image/jpeg" ) {
- print "Got MIME info '$mimeinfo' for $inimg, stopping\n";
- exit 1;
- }
- else {
- $blockinfo{depth}=24;
- }
- $blockinfo{mime} = $mimeinfo;
- my $imggeom= `identify -format %G $inimg`;
- chomp $imggeom;
- if ( ! defined $imggeom || $imggeom !~ /^(\d+)x(\d+)$/ ) {
- print "Failed getting image geometry for $inimg\n";
- exit 1;
- }
- $blockinfo{width} = $1;
- $blockinfo{height} = $2;
- open FILE, "<", "$inimg" or die $!;
- binmode FILE;
- my $imgcontents;
- my $size=0;
- my $chunksize=200*1024;
- while ( ! eof(FILE) ) {
- my $read = read FILE, $imgcontents, $chunksize, $size;
- $size += $read;
- }
- $blockinfo{imagesize}=$size;
- if ($blockinfo{imagesize} < 1) {
- print "Couldn't read $inimg\n";
- exit 1;
- }
- close FILE;
- $blockinfo{mimelength}=length ($blockinfo{mime});
- $blockinfo{desclength}=length ($blockinfo{desc});
- my $packformat = sprintf "(L[2]A[%d]LA[%d]L[5]a[%d])>",
- $blockinfo{mimelength},
- $blockinfo{desclength},
- $blockinfo{imagesize};
- my $block = pack (
- $packformat,
- $blockinfo{type}, #32bit
- $blockinfo{mimelength}, #32bit
- $blockinfo{mime},
- $blockinfo{desclength}, #32bit
- $blockinfo{desc},
- $blockinfo{width}, #32bit
- $blockinfo{height}, #32bit
- $blockinfo{depth}, #32bit
- $blockinfo{palette}, #32bit
- $blockinfo{imagesize}, #32bit
- $imgcontents
- );
- for my $key (keys %blockinfo ) {
- print "$key $blockinfo{$key}\n"
- }
- my $encoded = encode_base64( $block, "" );
- if ( ! open (OUT, ">", $outname)
- ||
- ! print OUT "$encoded\n"
- ||
- ! close OUT
- )
- {
- print "Error writing to $outname\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement