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 / XPM.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-07  |  4.5 KB  |  188 lines

  1. package Image::Info::XPM;
  2. $VERSION = '1.03';
  3. #Path to X11 RGB database
  4. $RGBLIB ||= "/usr/X11R6/lib/X11/rgb.txt";
  5. use strict;
  6. use Image::Xpm 1.08;
  7.  
  8.  
  9. sub process_file{
  10.     my($info, $source, $opts) = @_;
  11.  
  12.     $SIG{__WARN__} = sub {
  13.     $info->push_info(0, "Warn", shift);
  14.     };
  15.  
  16.     my $i = Image::Xpm->new(-file, $source);
  17.  
  18.     $info->push_info(0, "color_type" => "Indexed-RGB");
  19.     $info->push_info(0, "file_ext" => "xpm");
  20.     $info->push_info(0, "file_media_type" => "image/x-xpixmap");
  21.     $info->push_info(0, "height", $i->get(-height));
  22.     $info->push_info(0, "resolution", "1/1");
  23.     $info->push_info(0, "width", $i->get(-width));
  24.     $info->push_info(0, "BitsPerSample" => 8);
  25.     $info->push_info(0, "SamplesPerPixel", 1);
  26.  
  27.     $info->push_info(0, "XPM_CharactersPerPixel" => $i->get(-cpp) );
  28.     # XXX is this always?
  29.     $info->push_info(0, "ColorResolution", 8);
  30.     $info->push_info(0, "ColorTableSize" => $i->get(-ncolours) );
  31.     if( $opts->{ColorPalette} ){
  32.     $info->push_info(0, "ColorPalette" => [keys %{$i->get(-cindex)}] );
  33.     }
  34.     if( $opts->{L1D_Histogram} ){
  35.     #Do Histograms
  36.     my(%RGB, @l1dhist, $R, $G, $B, $color);
  37.     for(my $y=0; $y<$i->get(-height); $y++){
  38.         for(my $x=0; $x<$i->get(-width); $x++){
  39.         $color = $i->xy($x, $y);
  40.         if( $color !~ /^#/ ){
  41.             unless( exists($RGB{white}) ){
  42.             local $_;
  43.             if( open(RGB, $Image::Info::XPM::RGBLIB) ){
  44.                 while(<RGB>){
  45.                 /(\d+)\s+(\d+)\s+(\d+)\s+(.*)/;
  46.                 $RGB{$4}=[$1,$2,$3];
  47.                 }
  48.             }
  49.             else{
  50.                 $RGB{white} = "0 but true";
  51.                 $info->push_info(0, "Warn", "Unable to open RGB database, you may need to set \$Image::Info::XPM::RGBLIB or define \$RGBLIB in ". __FILE__);
  52.             }
  53.             }
  54.             $R = $RGB{$color}->[0];
  55.             $G = $RGB{$color}->[1];
  56.             $B = $RGB{$color}->[2];
  57.         }
  58.         else{
  59.             $R = hex(substr($color,1,2));
  60.             $G = hex(substr($color,3,2));
  61.             $B = hex(substr($color,5,2));
  62.         }
  63.         if( $opts->{L1D_Histogram} ){
  64.             $l1dhist[(.3*$R + .59*$G + .11*$B)]++;
  65.         }
  66.         }
  67.     }
  68.     if( $opts->{L1D_Histogram} ){
  69.         $info->push_info(0, "L1D_Histogram", [@l1dhist]);
  70.     }
  71.     }
  72.     $info->push_info(0, "HotSpotX" => $i->get(-hotx) );
  73.     $info->push_info(0, "HotSpotY" => $i->get(-hoty) );
  74.     $info->push_info(0, 'XPM_Extension-'.ucfirst($i->get(-extname)) => $i->get(-extlines)) if
  75.     $i->get(-extname);
  76.  
  77.     for (@{$i->get(-comments)}) {
  78.     $info->push_info(0, "Comment", $_);
  79.     }
  80. }
  81. 1;
  82. __END__
  83. =pod
  84.  
  85. =head1 NAME
  86.  
  87. Image::Info::XPM - XPM support for Image::Info
  88.  
  89. =head1 SYNOPSIS
  90.  
  91.  use Image::Info qw(image_info dim);
  92.  
  93.  my $info = image_info("image.xpm");
  94.  if (my $error = $info->{error}) {
  95.      die "Can't parse image info: $error\n";
  96.  }
  97.  my $color = $info->{color_type};
  98.  
  99.  my($w, $h) = dim($info);
  100.  
  101. =head1 DESCRIPTION
  102.  
  103. This modules supplies the standard key names
  104. except for Compression, Gamma, Interlace, LastModificationTime, as well as:
  105.  
  106. =over
  107.  
  108. =item ColorPalette
  109.  
  110. Reference to an array of all colors used.
  111. This key is only present if C<image_info> is invoked
  112. as C<image_info({ColorPaletteE<gt>=1})>.
  113.  
  114. =item ColorTableSize
  115.  
  116. The number of colors the image uses.
  117.  
  118. =item HotSpotX
  119.  
  120. The x-coord of the image's hotspot.
  121. Set to -1 if there is no hotspot.
  122.  
  123. =item HotSpotY
  124.  
  125. The y-coord of the image's hotspot.
  126. Set to -1 if there is no hotspot.
  127.  
  128. =item L1D_Histogram
  129.  
  130. Reference to an array representing a one dimensioanl luminance
  131. histogram. This key is only present if C<image_info> is invoked
  132. as C<image_info($file, L1D_Histogram=E<gt>1)>. The range is from 0 to 255,
  133. however auto-vivification is used so a null field is also 0,
  134. and the array may not actually contain 255 fields.
  135.  
  136. =item XPM_CharactersPerPixel
  137.  
  138. This is typically 1 or 2. See L<Image::Xpm>.
  139.  
  140. =item XPM_Extension-.*
  141.  
  142. XPM Extensions (the most common is XPMEXT) if present.
  143.  
  144. =back
  145.  
  146. =item FILES
  147.  
  148. This module requires L<Image::Xpm>
  149.  
  150. I<$Image::Info::XPM::RGBLIB> is set to F</usr/X11R6/lib/X11/rgb.txt>
  151. by default, this is used to resolve textual color names to their RGB
  152. counterparts.
  153.  
  154. =head1 SEE ALSO
  155.  
  156. L<Image::Info>, L<Image::Xpm>
  157.  
  158. =head1 NOTES
  159.  
  160. For more information about XPM see:
  161.  
  162.  ftp://ftp.x.org/contrib/libraries/xpm-README.html
  163.  
  164. =head1 CAVEATS
  165.  
  166. While the module attempts to be as robust as possible, it may not recognize
  167. older XBMs (Versions 1-3), if this is the case try inserting S</* XPM */>
  168. as the first line.
  169.  
  170. =head1 AUTHOR
  171.  
  172. Jerrad Pierce <belg4mit@mit.edu>/<webmaster@pthbb.org>
  173.  
  174. This library is free software; you can redistribute it and/or
  175. modify it under the same terms as Perl itself.
  176.  
  177. =cut
  178.  
  179. =begin register
  180.  
  181. MAGIC: /(^\/\* XPM \*\/)|(static\s+char\s+\*\w+\[\]\s*=\s*{\s*"\d+)/
  182.  
  183. See L<Image::Info::XPM> for details.
  184.  
  185. =end register
  186.  
  187. =cut
  188.