On Thu Dec 18 08:56:12 2008, DMOW wrote:
Show quoted text> (as '/Filter/DCTDecode/ColorSpace/DeviceRGB' is hardcoded)
I have created a patch to fix this and make prJpeg a bit more friendly.
Sorry for the duplication but I submitted it via email before I had a
bitcard account.
*** Reuse.orig Tue Apr 20 16:43:44 2010
--- Reuse.pm Tue Apr 20 17:02:57 2010
***************
*** 2472,2500 ****
=head2 prJpeg - import a jpeg-image
! prJpeg ( $imageFile, $width, $height )
! B<$imageFile> contains 1 single jpeg-image. B<$width> and B<$height>
! also have to be specified. Returns the B<$internalName>
use PDF::Reuse;
- use Image::Info qw(image_info dim);
use strict;
my $file = 'myImage.jpg';
- my $info = image_info($file);
- my ($width, $height) = dim($info); # Get the dimensions
prFile('myFile.pdf');
! my $intName = prJpeg("$file", # Define the image
! $width, # in the document
! $height);
my $str = "q\n";
! $str .= "$width 0 0 $height 10 10 cm\n";
$str .= "/$intName Do\n";
$str .= "Q\n";
prAdd($str);
prEnd();
This is a little like an extra or reserve routine to add images to the document.
--- 2472,2517 ----
=head2 prJpeg - import a jpeg-image
! prJpeg ( $imageFile, [$width, $height, [$colourMap, $bitsPerColour]] )
! B<$imageFile> contains 1 single jpeg-image.
! Returns the B<$internalName> B<$pixwidth> B<$pixheight> B<$xdpi> and B<$ydpi>
! The preferred calling method is to call with only the filename and prJpeg will
! use Image::Info to determine image size and colourmap, and then returns image
! size and resolution information. You can then display the image repeatedly
! if you wish and scale it as you like.
+ For backward compatibility you may assess (or assume) the image characteristics
+ yourself and pass these details to prJpeg. As long as you pass at least the
+ dimensions, prJpeg will not invoke the Image::Info module. This means if you
+ are certain that all images have the same characteristics you can save the
+ overhead as you could with the original calling method.
+
use PDF::Reuse;
use strict;
my $file = 'myImage.jpg';
prFile('myFile.pdf');
! my ($intName, $pixwidth, $pixheight, $xdpi, $ydpi) = prJpeg($file);
! my $width = $pixwidth * 72 / $xdpi;
! my $height = $pixheight * 72 / $ydpi;
+ my $ypos = 10; # Initial position of scaled image
+ my $xpos = 10;
my $str = "q\n";
! $str .= "$width 0 0 $height $xpos $ypos cm\n"; # Display scaled
$str .= "/$intName Do\n";
$str .= "Q\n";
prAdd($str);
+
+ $ypos += $height + 10;
+
+ $str = "q\n";
+ $str .= "$pixwidth 0 0 $pixheight $xpos $ypos cm\n"; # 1 pixel per point
+ $str .= "/$intName Do\n";
+ $str .= "Q\n";
+ prAdd($str);
prEnd();
This is a little like an extra or reserve routine to add images to the document.
***************
*** 3803,3810 ****
}
sub prJpeg
! { my ($iFile, $iWidth, $iHeight) = @_;
my ($iLangd, $namnet, $utrad);
if (! $pos) # If no output is active, it is no use to continue
{ return undef;
}
--- 3820,3830 ----
}
sub prJpeg
! { my ($iFile, $iWidth, $iHeight, $colourMap, $bits) = @_;
my ($iLangd, $namnet, $utrad);
+ my $xres = 300;
+ my $yres = 300;
+
if (! $pos) # If no output is active, it is no use to continue
{ return undef;
}
***************
*** 3812,3817 ****
--- 3832,3898 ----
($iFile, $checkId) = findGet($iFile, $checkidOld);
if ($iFile)
{ $iLangd = (stat($iFile))[7];
+ if (defined($iWidth) && defined($iHeight)) {
+ # Original calling method - caller has assessed image size
+ $colourMap ||= "DeviceRGB"; # Let them override colour map
+ $bits ||= 8; # Let them override bits per colour
+ } else {
+ my $unit = "dpi";
+ # Avoid runtime overhead of Image::Info unless it's wanted
+ eval "use Image::Info qw(image_info dim)";
+
+ # Get image attributes from file
+ my $info = image_info($iFile);
+ unless (defined($info)) {
+ STDERR->print("Unable to get image info for $iFile\n");
+ return undef;
+ }
+ ($iWidth, $iHeight) = dim($info); # Get the dimensions
+ my $resolution = $$info{resolution}; # and resolution
+
+ if (ref($resolution) eq "ARRAY") {
+ # Turn resolution array into string
+ my ($xval, $yval) = @$resolution;
+ if ($xval =~ /(\d+)\s*(\w+)/) {
+ $xres = $1;
+ $unit = $2;
+ $yval =~ s/^[^\d]*(\d+).*/$1/;
+ $yres = $yval;
+ }
+ } elsif ($resolution =~ /^\s*(\d+)\s*(?:\/\s*(\d+))?\s*(\w+)?\s*$/) {
+ $xres = $1;
+ $yres = $2 || $xres;
+ $unit = $3 || "ratio"; # How do we process this?
+ }
+
+ my $mult;
+
+ if ($unit eq "dpcm") {
+ $mult = 1 / 2.54;
+ } elsif ($unit eq "dpm") {
+ $mult = 100 / 2.54;
+ } else {
+ $mult = 1; # Assume dpi
+ }
+
+ $xres *= $mult;
+ $yres *= $mult;
+
+ if ($$info{SamplesPerPixel} == 1) {
+ $colourMap = "DeviceGray";
+ } elsif ($$info{SamplesPerPixel} == 4) {
+ $colourMap = "DeviceCMYK";
+ } else {
+ $colourMap = "DeviceRGB";
+ }
+ if (ref($$info{BitsPerSample}) eq "ARRAY") {
+ # Assume all colour planes are the same - PDF format seems to!
+ $bits = $$info{BitsPerSample}[0];
+ } else {
+ $bits = $$info{BitsPerSample};
+ }
+ }
+
$imageNr++;
$namnet = 'Ig' . $imageNr;
$objNr++;
***************
*** 3821,3827 ****
my $iStream;
sysread BILDFIL, $iStream, $iLangd;
$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;
--- 3902,3908 ----
my $iStream;
sysread BILDFIL, $iStream, $iLangd;
$utrad = "$objNr 0 obj\n<</Type/XObject/Subtype/Image/Name/$namnet" .
! "/Width $iWidth /Height $iHeight /BitsPerComponent $bits /Filter/DCTDecode/ColorSpace/$colourMap"
. "/Length $iLangd >>stream\n$iStream\nendstream\nendobj\n";
close BILDFIL;
$pos += syswrite UTFIL, $utrad;
***************
*** 3835,3841 ****
{ errLog("No output file, you have to call prFile first");
}
undef $checkId;
! return $namnet;
}
sub checkContentStream
--- 3916,3926 ----
{ errLog("No output file, you have to call prFile first");
}
undef $checkId;
! if (wantarray) {
! return $namnet, $iWidth, $iHeight, $xres, $yres;
! } else {
! return $namnet;
! }
}
sub checkContentStream