1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
package PDF::API2::Resource::XObject::Image::PNM;
our $VERSION = '2.019';
# For spec details, see man pages pam(5), pbm(5), pgm(5), pnm(5),
# ppm(5), which were pasted into the __END__ of this file in an
# earlier revision.
use base 'PDF::API2::Resource::XObject::Image';
use IO::File;
use PDF::API2::Util;
use PDF::API2::Basic::PDF::Utils;
no warnings qw[ deprecated recursion uninitialized ];
sub new {
my ($class,$pdf,$file,$name) = @_;
my $self;
my $fh = IO::File->new;
$class = ref $class if ref $class;
$self=$class->SUPER::new($pdf,$name || 'Nx'.pdfkey());
$pdf->new_obj($self) unless($self->is_obj($pdf));
$self->{' apipdf'}=$pdf;
$self->read_pnm($pdf,$file);
return($self);
}
sub new_api {
my ($class,$api,@opts)=@_;
my $obj=$class->new($api->{pdf},@opts);
$obj->{' api'}=$api;
return($obj);
}
# READPPMHEADER
# taken from Image::PBMLib
# Copyright by Benjamin Elijah Griffin (28 Feb 2003)
#
sub readppmheader($) {
my $gr = shift; # input file glob ref
my $in = '';
my $no_comments;
my %info;
my $rc;
$info{error} = undef;
$rc = read($gr, $in, 3);
if (!defined($rc) or $rc != 3) {
$info{error} = 'Read error or EOF';
return \%info;
}
if ($in =~ /^P([123456])\s/) {
$info{type} = $1;
if ($info{type} > 3) {
$info{raw} = 1;
} else {
$info{raw} = 0;
}
if ($info{type} == 1 or $info{type} == 4) {
$info{max} = 1;
$info{bgp} = 'b';
} elsif ($info{type} == 2 or $info{type} == 5) {
$info{bgp} = 'g';
} else {
$info{bgp} = 'p';
}
while(1) {
$rc = read($gr, $in, 1, length($in));
if (!defined($rc) or $rc != 1) {
$info{error} = 'Read error or EOF';
return \%info;
}
$no_comments = $in;
$info{comments} = '';
while ($no_comments =~ /#.*\n/) {
$no_comments =~ s/#(.*\n)/ /;
$info{comments} .= $1;
}
if ($info{bgp} eq 'b') {
if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s/) {
$info{width} = $1;
$info{height} = $2;
last;
}
} else {
if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s+(\d+)\s/) {
$info{width} = $1;
$info{height} = $2;
$info{max} = $3;
last;
}
}
} # while reading header
$info{fullheader} = $in;
} else {
$info{error} = 'Wrong magic number';
}
return \%info;
}
sub read_pnm {
my $self = shift @_;
my $pdf = shift @_;
my $file = shift @_;
my ($buf,$t,$s,$line);
my ($w,$h,$bpc,$cs,$img,@img)=(0,0,'','','');
open(INF,$file);
binmode(INF,':raw');
my $info=readppmheader(INF);
if($info->{type} == 4) {
$bpc=1;
read(INF,$self->{' stream'},($info->{width}*$info->{height}/8));
$cs='DeviceGray';
$self->{Decode}=PDFArray(PDFNum(1),PDFNum(0));
} elsif($info->{type} == 5) {
$buf.=<INF>;
if($info->{max}==255){
$s=0;
} else {
$s=255/$info->{max};
}
$bpc=8;
if($s>0) {
for($line=($info->{width}*$info->{height});$line>0;$line--) {
read(INF,$buf,1);
$self->{' stream'}.=pack('C',(unpack('C',$buf)*$s));
}
} else {
read(INF,$self->{' stream'},$info->{width}*$info->{height});
}
$cs='DeviceGray';
} elsif($info->{type} == 6) {
if($info->{max}==255){
$s=0;
} else {
$s=255/$info->{max};
}
$bpc=8;
if($s>0) {
for($line=($info->{width}*$info->{height});$line>0;$line--) {
read(INF,$buf,1);
$self->{' stream'}.=pack('C',(unpack('C',$buf)*$s));
read(INF,$buf,1);
$self->{' stream'}.=pack('C',(unpack('C',$buf)*$s));
read(INF,$buf,1);
$self->{' stream'}.=pack('C',(unpack('C',$buf)*$s));
}
} else {
read(INF,$self->{' stream'},$info->{width}*$info->{height}*3);
}
$cs='DeviceRGB';
}
close(INF);
$self->width($info->{width});
$self->height($info->{height});
$self->bpc($bpc);
$self->filters('FlateDecode');
$self->colorspace($cs);
return($self);
}
1;
|