#!/usr/bin/perl -w # # A subroutine for reading jpg and gif images, and converting # them to XPM format # # 060106 liverpole # ############## ### Strict ### ############## use strict; use warnings; ################# ### Libraries ### ################# use File::Basename; use FileHandle; use Image::Magick; ################### ### Subroutines ### ################### sub convert_image_to_xpm($) { my ($fname) = @_; print "=== Converting file $fname to XPM format ===\n"; my $image = new Image::Magick(); my $result = $image->Read($fname); $result and die "Error reading image file '$fname' ($result)\n"; print "Read image '$fname', now writing ...\n"; my $tmp = "convert.$$.xpm"; $result = $image->Write($tmp); $result and die "Error writing .xpm file '$tmp' ($result)\n"; my $fh = new FileHandle(); open($fh, "<$tmp") or die "Cannot read file '$fname' ($!)\n"; chomp (my @lines = <$fh>); close $fh; print "Converted image to .xpm format\n"; unlink $tmp; return \@lines; } #################### ### Main program ### #################### my $iam = basename $0; (my $img = shift) or die " syntax: $iam Demonstrates usage of the subroutine convert_image_to_xpm, for converting a given .jpg or .gif to XPM format. "; my $plines = convert_image_to_xpm($img); foreach my $line (@$plines) { print "$line\n"; }