Posted on 2009-08-31 10:22:08-07 by inla
How to enable support for grayscale images
Here a solution to change your script to enable support for importing grayscale images:

1. the call of prJpeg:

my $info = image_info($file);
my $colortype = $info->{color_type}; # get color space
my $intName = prJpeg("$file",$width,$height,$colortype); # new parameter: $colortype !

2. change of sub prJpeg in "Reuse.pm":

sub prJpeg
{ my ($iFile, $iWidth, $iHeight, $iColortype) = @_; # color space parameter added
my ($iLangd, $namnet, $utrad);
if (! $pos) # If no output is active, it is no use to continue
{ return undef;
}
my $checkidOld = $checkId;
($iFile, $checkId) = findGet($iFile, $checkidOld);
if ($iFile)
{ $iLangd = (stat($iFile))[7];
$imageNr++;
$namnet = 'Ig' . $imageNr;
$objNr++;
$objekt[$objNr] = $pos;
open (BILDFIL, "<$iFile") || errLog("Couldn't open $iFile, $!, aborts");
binmode BILDFIL;
my $iStream;
sysread BILDFI4L, $iStream, $iLangd;
# Change to enable grayscale Images in the pdf file
if ($iColortype =~ /Gray/i)
{ $utrad = "$objNr 0 obj\n<</Type/XObject/Subtype/Image/Name/$namnet" .
"/Width $iWidth /Height $iHeight /BitsPerComponent 8/Filter/DCTDecode/ColorSpace/DeviceGray" .
"/Length $iLangd >>stream\n$iStream\nendstream\nendobj\n";
}
else
{ $utrad = "$objNr 0 obj\n<</Type/XObject/Subtype/Image/Name/$namnet" .
"/Width $iWidth /Height $iHeight /BitsPerComponent 8/Filter/DCTDecode/ColorSpace/DeviceRGB" .
"/Length $iLangd >>stream\n$iStream\nendstream\nendobj\n";
}
close BILDFIL;
$pos += syswrite UTFIL, $utrad;
if ($runfil)
{ $log .= "Cid~$checkId\n";
$log .= "Jpeg~$iFile~$iWidth~$iHeight\n";
}
$objRef{$namnet} = $objNr;
}
if (! $pos)
{ errLog("No output file, you have to call prFile first");
}
undef $checkId;
return $namnet;
}

(Remember: subs are autoloaded, so have a look at 'prJpeg.al', too, if it already exists on your server)
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.