1 package Image::Info::XPM;
3 #Path to X11 RGB database
4 $RGBLIB ||= "/usr/X11R6/lib/X11/rgb.txt";
10 my($info, $source, $opts) = @_;
11 my(@comments, @warnings, $i);
13 *Image::Xpm::carp = sub { push @warnings, @_; };
14 *Image::Xpm::croak = sub { $info->push_info(0, "error", @_); };
15 if( $Image::Xpm::Version cmp '1.08' < 1){
16 push @warnings, "This version of Image::Xpm does not support filehandles or scalar references";
17 $source = $info->get_info(0, "FileName");
19 if( $info->get_info(0, "error") ){
22 $i = Image::Xpm->new(-file, $source);
23 $info->push_info(0, "color_type" => "Indexed-RGB");
24 $info->push_info(0, "file_ext" => "xpm");
25 $info->push_info(0, "file_media_type" => "image/x-xpixmap");
26 $info->push_info(0, "height", $i->get(-height));
27 $info->push_info(0, "resolution", "1/1");
28 $info->push_info(0, "width", $i->get(-width));
29 $info->push_info(0, "BitsPerSample" => 8);
30 $info->push_info(0, "SamplesPerPixel", 1);
32 $info->push_info(0, "XPM_CharactersPerPixel" => $i->get(-cpp) );
34 $info->push_info(0, "ColorResolution", 8);
35 $info->push_info(0, "ColorTableSize" => $i->get(-ncolours) );
36 if( $opts->{ColorPalette} ){
37 $info->push_info(0, "ColorPalette" => [keys %{$i->get(-cindex)}] );
39 if( $opts->{L1D_Histogram} ){
41 my(%RGB, @l1dhist, $R, $G, $B, $color);
42 for(my $y=0; $y<$i->get(-height); $y++){
43 for(my $x=0; $x<$i->get(-width); $x++){
44 $color = $i->xy($x, $y);
46 unless( exists($RGB{white}) ){
48 if( open(RGB, $Image::Info::XPM::RGBLIB) ){
50 /(\d+)\s+(\d+)\s+(\d+)\s+(.*)/;
55 $RGB{white} = "0 but true";
56 push @warnings, "Unable to open RGB database, you may need to set \$Image::Info::XPM::RGBLIB or define \$RGBLIB in ". __FILE__;
59 $R = $RGB{$color}->[0];
60 $G = $RGB{$color}->[1];
61 $B = $RGB{$color}->[2];
64 $R = hex(substr($color,1,2));
65 $G = hex(substr($color,3,2));
66 $B = hex(substr($color,5,2));
68 if( $opts->{L1D_Histogram} ){
69 $l1dhist[(.3*$R + .59*$G + .11*$B)]++;
73 if( $opts->{L1D_Histogram} ){
74 $info->push_info(0, "L1D_Histogram", [@l1dhist]);
77 $info->push_info(0, "HotSpotX" => $i->get(-hotx) );
78 $info->push_info(0, "HotSpotY" => $i->get(-hoty) );
79 $info->push_info(0, 'XPM_Extension-'.ucfirst($i->get(-extname)) => $i->get(-extlines)) if
81 push @comments, @{$i->get(-comments)};
84 $info->push_info(0, "Comment", $_);
88 $info->push_info(0, "Warn", $_);
97 Image::Info::XPM - XPM support for Image::Info
101 use Image::Info qw(image_info dim);
103 my $info = image_info("image.xpm");
104 if (my $error = $info->{error}) {
105 die "Can't parse image info: $error\n";
107 my $color = $info->{color_type};
109 my($w, $h) = dim($info);
113 This modules supplies the standard key names
114 except for Compression, Gamma, Interlace, LastModificationTime, as well as:
120 Reference to an array of all colors used.
121 This key is only present if C<image_info> is invoked
122 as C<image_info({ColorPaletteE<gt>=1})>.
126 The number of colors the image uses.
130 The x-coord of the image's hotspot.
131 Set to -1 if there is no hotspot.
135 The y-coord of the image's hotspot.
136 Set to -1 if there is no hotspot.
140 Reference to an array representing a one dimensioanl luminance
141 histogram. This key is only present if C<image_info> is invoked
142 as C<image_info($file, L1D_Histogram=E<gt>1)>. The range is from 0 to 255,
143 however auto-vivification is used so a null field is also 0,
144 and the array may not actually contain 255 fields.
146 =item XPM_CharactersPerPixel
148 This is typically 1 or 2. See L<Image::Xpm>.
150 =item XPM_Extension-.*
152 XPM Extensions (the most common is XPMEXT) if present.
158 This module requires L<Image::Xpm>
160 I<$Image::Info::XPM::RGBLIB> is set to F</usr/X11R6/lib/X11/rgb.txt>
161 by default, this is used to resolve textual color names to their RGB
166 L<Image::Info>, L<Image::Xpm>
170 For more information about XPM see:
172 ftp://ftp.x.org/contrib/libraries/xpm-README.html
176 While the module attempts to be as robust as possible, it may not recognize
177 older XBMs (Versions 1-3), if this is the case try inserting S</* XPM */>
182 Jerrad Pierce <belg4mit@mit.edu>/<webmaster@pthbb.org>
184 This library is free software; you can redistribute it and/or
185 modify it under the same terms as Perl itself.
191 MAGIC: /(^\/\* XPM \*\/)|(static\s+char\s+\*\w+\[\]\s*=\s*{\s*"\d+)/
193 See L<Image::Info::XPM> for details.