Advertisement
Guest User

basic jpeg to flac converter

a guest
Apr 14th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.40 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. ### WARNING, arguments are not sufficiently sanitised for untrusted use.
  4. ### Requires ImageMagick's 'identify' and the Unix 'file' command
  5.  
  6. use MIME::Base64;
  7.  
  8. my $ARGPROBS=0;
  9. my $inimg;
  10. my $outname;
  11. my $desc="";
  12. my $type=0;
  13.  
  14. while (my $arg = shift @ARGV) {
  15.     if ($arg eq "-i") {
  16.     if (@ARGV < 1) {
  17.         print "$arg requires an argument\n";
  18.         $ARGPROBS=1;
  19.     }
  20.     else {
  21.         $inimg = shift @ARGV;
  22.     }
  23.     }
  24.     elsif ($arg eq "-o") {
  25.     if (@ARGV < 1) {
  26.         print "$arg requires an argument\n";
  27.         $ARGPROBS=1;
  28.     }
  29.     else {
  30.         $outname = shift @ARGV;
  31.     }
  32.     }
  33.     elsif ($arg eq "-desc") {
  34.     if (@ARGV < 1) {
  35.         print "$arg requires an argument\n";
  36.         $ARGPROBS=1;
  37.     }
  38.     else {
  39.         $desc = shift @ARGV;
  40.     }
  41.     }
  42.     elsif ($arg eq "-type") {
  43.     if (@ARGV < 1) {
  44.         print "$arg requires an argument\n";
  45.         $ARGPROBS=1;
  46.     }
  47.     else {
  48.         $type = shift @ARGV;
  49.     }
  50.     }
  51.     else {
  52.     print "$arg not recognised\n";
  53.     $ARGPROBS=1;
  54.     }
  55. }
  56.  
  57. if (! defined $inimg ) {
  58.     print "Need an input image\n";
  59.     $ARGPROBS=1;
  60. }
  61.  
  62. if (! defined $outname ) {
  63.     print "Need an output name\n";
  64.     $ARGPROBS=1;
  65. }
  66. elsif ( -e $outname ) {
  67.     print "$outname already exists\n";
  68.     $ARGPROBS=1;
  69. }
  70.  
  71. if ( $type !~ /^\d+$/ || $type > 20 ) {
  72.     # Regex doesn't allow <0
  73.     print "Type must be in range [0:20]\n";
  74.     $ARGPROBS=1;
  75. }
  76.  
  77. if ($ARGPROBS) {
  78.     exit 1;
  79. }
  80.  
  81. my %blockinfo = (
  82.     type => $type,
  83.     mime => "",
  84.     desc => $desc,
  85.     width     =>0,
  86.     height    =>0,
  87.     depth     =>0,
  88.     palette   =>0,
  89.     imagesize =>0
  90.     );
  91.  
  92. if ( ! -f $inimg ) {
  93.     print "Couldn't find $inimg\n";
  94.     exit 1;
  95. }
  96. my $mimeinfo= `file --mime-type -b $inimg`;
  97. chomp $mimeinfo;
  98. if ( $mimeinfo ne "image/jpeg" ) {
  99.     print "Got MIME info '$mimeinfo' for $inimg, stopping\n";
  100.     exit 1;
  101. }
  102. else {
  103.     $blockinfo{depth}=24;
  104. }
  105. $blockinfo{mime} = $mimeinfo;
  106.  
  107. my $imggeom= `identify -format %G $inimg`;
  108. chomp $imggeom;
  109. if ( ! defined $imggeom || $imggeom !~ /^(\d+)x(\d+)$/ ) {
  110.     print "Failed getting image geometry for $inimg\n";
  111.     exit 1;
  112. }
  113. $blockinfo{width}  = $1;
  114. $blockinfo{height} = $2;
  115.  
  116. open FILE, "<", "$inimg" or die $!;
  117. binmode FILE;
  118.  
  119. my $imgcontents;
  120. my $size=0;
  121. my $chunksize=200*1024;
  122. while ( ! eof(FILE) ) {
  123.     my $read = read FILE, $imgcontents, $chunksize, $size;
  124.     $size += $read;
  125. }
  126. $blockinfo{imagesize}=$size;
  127.  
  128. if ($blockinfo{imagesize} < 1) {
  129.     print "Couldn't read $inimg\n";
  130.     exit 1;
  131. }
  132. close FILE;
  133.  
  134. $blockinfo{mimelength}=length ($blockinfo{mime});
  135. $blockinfo{desclength}=length ($blockinfo{desc});
  136.  
  137. my $packformat = sprintf "(L[2]A[%d]LA[%d]L[5]a[%d])>",
  138.     $blockinfo{mimelength},
  139.     $blockinfo{desclength},
  140.     $blockinfo{imagesize};
  141.  
  142. my $block = pack (
  143.     $packformat,
  144.     $blockinfo{type},       #32bit
  145.     $blockinfo{mimelength}, #32bit
  146.     $blockinfo{mime},
  147.     $blockinfo{desclength}, #32bit
  148.     $blockinfo{desc},
  149.     $blockinfo{width},      #32bit
  150.     $blockinfo{height},     #32bit
  151.     $blockinfo{depth},      #32bit
  152.     $blockinfo{palette},    #32bit
  153.     $blockinfo{imagesize},  #32bit
  154.     $imgcontents
  155.     );
  156.  
  157. for my $key (keys %blockinfo ) {
  158.     print "$key $blockinfo{$key}\n"
  159. }
  160.  
  161.  
  162. my $encoded = encode_base64( $block, "" );
  163.  
  164. if ( ! open (OUT, ">", $outname)
  165.      ||
  166.      ! print OUT "$encoded\n"
  167.      ||
  168.      ! close OUT
  169.     )
  170. {
  171.     print "Error writing to $outname\n";
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement