home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / BMP.pm < prev    next >
Encoding:
Perl POD Document  |  2001-04-10  |  5.1 KB  |  207 lines

  1. package Image::Info::BMP;
  2. $VERSION = '1.01';
  3. use strict;
  4.  
  5. sub process_file{
  6.     my($info, $source, $opts) = @_;
  7.     my(@comments, @warnings, @header, %info, $buf, $total);
  8.  
  9.     read($source, $buf, 54) == 54 or die "Can't reread BMP header: $!";
  10.     @header = unpack("vVv2V2V2v2V2V2V2", $buf);
  11.     $total += length($buf);
  12.  
  13.     if( $header[9] && $header[9] < 24 ){
  14.     $info->push_info(0, "color_type" => "Indexed-RGB");
  15.     }
  16.     else{
  17.     $info->push_info(0, "color_type" => "RGB");
  18.     }
  19.     $info->push_info(0, "file_media_type" => "image/bmp");
  20.     if( $header[10] == 1 || $header[10] == 2){
  21.     $info->push_info(0, "file_ext" => "rle");
  22.     }
  23.     else{
  24.     $info->push_info(0, "file_ext" => "bmp"); # || dib
  25.     }
  26.     $info->push_info(0, "height", abs($header[7]));
  27.     $info->push_info(0, "resolution", "$header[12]/$header[13]");
  28.     $info->push_info(0, "width", $header[6]);
  29.     $info->push_info(0, "BitsPerSample" => $header[9]);
  30.     $info->push_info(0, "SamplesPerPixel", $header[8]);
  31.  
  32.     $info->push_info(0, "BMP_ColorsImportant", $header[15]);
  33.     $info->push_info(0, "BMP_Origin",
  34.              $header[7]>1 ? 1 : 0 );
  35.     $info->push_info(0, "ColorTableSize", $header[14]);
  36.     $info->push_info(0, "Compression", [
  37.                     'none',
  38.                     'RLE8',
  39.                     'RLE4',
  40.                     'BITFIELDS',    #V4
  41.                     'JPEG',        #V5
  42.                     'PNG',        #V5
  43.                     ]->[$header[10]]);
  44.     #Version 5 Header ammendements
  45.     # XXX Discard for now, need a test image
  46.     if( $header[5] > 40 ){
  47.     read($source, $buf, $header[5]-40);  # XXX test
  48.     $total += length($buf);
  49.     my @v5 = unpack("V38", $buf);
  50.     splice(@v5, 5, 27);
  51.     $info->push_info(0, "BMP_MaskRed", $v5[0]);
  52.     $info->push_info(0, "BMP_MaskGreen", $v5[1]);
  53.     $info->push_info(0, "BMP_MaskBlue", $v5[2]);
  54.     $info->push_info(0, "BMP_MaskAlpha", $v5[3]);
  55. #    $info->push_info(0, "BMP_color_type", $v5[4]);
  56.     $info->push_info(0, "BMP_GammaRed", $v5[5]);
  57.     $info->push_info(0, "BMP_GammaGreen", $v5[6]);
  58.     $info->push_info(0, "BMP_GammaBlue", $v5[7]);
  59.     }
  60.     if( $header[9] < 24 && $opts->{ColorPalette} ){
  61.     my(@color, @palette);
  62.     for(my $i=0; $i<$header[14]; $i++){
  63.         read($source, $buf, 4) == 4 or die "Can't read: $!";
  64.         $total += length($buf);
  65.         @color = unpack("C3", $buf);
  66.         # Damn M$, BGR instead of RGB
  67.         push @palette, sprintf("#%02x%02x%02x",
  68.                    $color[2], $color[1], $color[0]);
  69.     }
  70.     $info->push_info(0, "ColorPalette", @palette);
  71.     }
  72.  
  73.     #Verify size # XXX Cheat and do -s if it's an actual file?
  74.     while( read($source, $buf, 1024) ){
  75.     $total += length($buf);
  76.     }
  77.     if( $header[1] != $total ){
  78.     push @warnings, "Size mismatch."
  79.     }
  80.  
  81.     for (@comments) {
  82.     $info->push_info(0, "Comment", $_);
  83.     }
  84.  
  85.     for (@warnings) {
  86.     $info->push_info(0, "Warn", $_);
  87.     }
  88. }
  89. 1;
  90. __END__
  91. warn if height is negative and compress is not RGB or BITFILEDS (0 or 3)
  92. ICO and CUR support?
  93. ### v5
  94. If bit depth is 0, it relies upon underlying JPG/PNG :-(
  95. Extra Information
  96.     DWORD        bV5RedMask; 
  97.     DWORD        bV5GreenMask; 
  98.     DWORD        bV5BlueMask; 
  99.     DWORD        bV5AlphaMask; 
  100.     DWORD        bV5CSType; 
  101.     CIEXYZTRIPLE bV5EndPoints; #3*CIEXYZ #CIEXYZ = 3*FXPT2DOT30#FXPT2DOT30 = long
  102.     DWORD        bV5GammaRed; 
  103.     DWORD        bV5GammaGreen; 
  104.     DWORD        bV5GammaBlue; 
  105.     DWORD        bV5Intent; 
  106.     DWORD        bV5ProfileData; 
  107.     DWORD        bV5ProfileSize; 
  108.  
  109. =pod
  110.  
  111. =head1 NAME
  112.  
  113. Image::Info:BMP - Windows Device Indepdent Bitmap support for Image::Info
  114.  
  115. =head1 SYNOPSIS
  116.  
  117.  use Image::Info qw(image_info dim);
  118.  
  119.  my $info = image_info("image.bmp");
  120.  if (my $error = $info->{error}) {
  121.      die "Can't parse image info: $error\n";
  122.  }
  123.  my $color = $info->{color_type};
  124.  
  125.  my($w, $h) = dim($info);
  126.  
  127. =head1 DESCRIPTION
  128.  
  129. This modules supplies the standard key names
  130. except for Gamma, Interlace, LastModificationTime, as well as:
  131.  
  132. =over
  133.  
  134. =item BMP_ColorsImportant
  135.  
  136. Specifies the number of color indexes that are required for
  137. displaying the bitmap. If this value is zero, all colors are required. 
  138.  
  139. =item BMP_Origin
  140.  
  141. If true the bitmap is a bottom-up DIB and its origin is the lower-left corner.
  142. Otherwise,
  143. the bitmap is a top-down DIB and its origin is the upper-left corner. 
  144.  
  145. =item ColorPalette
  146.  
  147. Reference to an array of all colors used.
  148. This key is only present if C<image_info> is invoked
  149. as C<image_info($file, ColorPalette=E<gt>1)>.
  150.  
  151. =item ColorTableSize
  152.  
  153. The number of colors the image uses.
  154. If 0 then this is a true color image.
  155. The number of color I<available> is 2 ^ B<BitsPerSample>.
  156.  
  157. =back
  158.  
  159. =head1 SEE ALSO
  160.  
  161. L<Image::Info>
  162.  
  163. =head1 NOTES
  164.  
  165. For more information about BMP see:
  166.  
  167.  http://msdn.microsoft.com
  168.  
  169. =head1 DIAGNOSTICS
  170.  
  171. =over
  172.  
  173. =item Size mismatch
  174.  
  175. The image may be correct, the filesize does not match the internally stored
  176. value.
  177.  
  178. =back
  179.  
  180. =head1 BUGS
  181.  
  182. The current implementation only function on little-endian architectures.
  183. Consequently erroneous data concerning compression (including
  184. B<file_ext> and B<file_mime_type>) may be reported.
  185.  
  186. =head1 AUTHOR
  187.  
  188. Jerrad Pierce <belg4mit@mit.edu>/<webmaster@pthbb.org>
  189.  
  190. This library is free software; you can redistribute it and/or
  191. modify it under the same terms as Perl itself.
  192.  
  193. =cut
  194.  
  195. =begin register
  196.  
  197. MAGIC: /^BM/
  198.  
  199. This module supports the Microsoft Device Independent Bitmap format
  200. (BMP, DIB, RLE).
  201.  
  202. For more information see L<Image::Info::BMP>.
  203.  
  204. =end register
  205.  
  206. =cut
  207.