| Revision 1 (by dpavlin, 2001/06/14 11:50:20) |
Initial revision
|
#!/usr/bin/perl -w
# xvpictoxpm.pl
# by Dobrica Pavlinusic <dpavlin@rot13.org> 2001-06-14
# based on info from xvthumb.c GIMP plugin, placed under GPL
# reads XV thumnail (from .xvpics directory) and dumps xpm to stdout (and
# errors/warnings to stderr)
use strict;
my $xv_pic=".xvpics/0610_011.jpg";
$xv_pic = $ARGV[0] if ($ARGV[0]);
my ($w,$h,$cols) = (0,0,0);
my @xv_pix;
open(XV,$xv_pic) || die "$xv_pic: $!";
my $l=<XV>; chomp $l;
die "$xv_pic not XV thumbnail file ($l)" if ($l!~m/^P7 332$/);
while(<XV>) {
chomp;
next if (/^#/);
if (/^(\d+) (\d+) (\d+)$/) {
($w,$h,$cols) = ($1,$2,$3);
last;
}
print STDERR "--$_--\n";
}
warn "$xv_pic number of colors is not 255 (strange)" if ($cols != 255);
my $buf; # tmp buffer for read;
# read pixels
read XV,$buf,($w*$h);
@xv_pix=unpack("C*",$buf);
print STDERR "$w $h $cols $#xv_pix (",length($buf)," bytes) file pos: ",tell(XV),"\n";
#/* width height ncolors chars_per_pixel */
my @xpm_data= ( "$w $h 256 2" );
# Generate 332 colour-cube colourmap
for (my $i=0; $i<256; $i++) {
push(@xpm_data,sprintf ("%02x c #%02x%02x%02x",$i,
(255*(($i&(7<<5))>>5))/7,
(255*(($i&(7<<2))>>2))/7,
(255*(($i&(3<<0))>>0))/3
));
}
my @xpm;
my $pix = 0;
for(my $y=0; $y<$h; $y++) {
for (my $x=0; $x<$w; $x++) {
if (defined($xv_pix[$y*$w+$x])) {
$pix++;
$xpm[$y*$w+$x] = sprintf("%02x",$xv_pix[$y*$w+$x]);
} else {
print STDERR "undef pixel $x,$y!\n";
}
}
}
print STDERR " w: $w h: $h pixela: $pix\n";
my $tmp=join('',@xpm);
print STDERR " xpm pixels len: ",length($tmp)," (should be ",$w*$h*2,")\n";
my $lw=$w*2;
while ($tmp=~s/^(.{$lw})//) {
push(@xpm_data,$1);
}
if ($tmp) { # this shouldn't happend!
push(@xpm_data,$tmp); # push rest of line
}
print "/* XPM */\nstatic char *noname[] = {\n";
foreach (@xpm_data) {
print "\"$_\",\n";
}
print "};\n";