File Coverage

blib/lib/PDF/API2/Resource/XObject/Image/JPEG.pm
Criterion Covered Total %
statement 69 71 97.1
branch 18 30 60.0
condition 12 30 40.0
subroutine 9 9 100.0
pod 1 2 50.0
total 109 142 76.7


line stmt bran cond sub pod time code
1             package PDF::API2::Resource::XObject::Image::JPEG;
2              
3 3     3   3979 use base 'PDF::API2::Resource::XObject::Image';
  3         9  
  3         1509  
4              
5 3     3   22 use strict;
  3         6  
  3         87  
6 3     3   40 use warnings;
  3         8  
  3         200  
7              
8             our $VERSION = '2.048'; # VERSION
9              
10 3     3   18 use IO::File;
  3         7  
  3         618  
11 3     3   20 use PDF::API2::Util;
  3         5  
  3         449  
12 3     3   17 use PDF::API2::Basic::PDF::Utils;
  3         7  
  3         282  
13 3     3   19 use Scalar::Util qw(weaken);
  3         7  
  3         2095  
14              
15             sub new {
16 4     4 1 13 my ($class, $pdf, $file, $name) = @_;
17 4         26 my $fh = IO::File->new();
18              
19 4 50       188 $class = ref($class) if ref($class);
20              
21 4   33     28 my $self = $class->SUPER::new($pdf, $name || 'Jx' . pdfkey());
22 4 50       19 $pdf->new_obj($self) unless $self->is_obj($pdf);
23              
24 4         10 $self->{' apipdf'} = $pdf;
25 4         7 weaken $self->{' apipdf'};
26              
27 4 100       13 if (ref($file)) {
28 1         4 $fh = $file;
29             }
30             else {
31 3 100       204 open $fh, "<", $file or die "$!: $file";
32             }
33 3         19 binmode $fh, ':raw';
34              
35 3         13 $self->read_jpeg($fh);
36              
37 3 100       17 if (ref($file)) {
38 1         8 seek $fh, 0, 0;
39 1         32 $self->{' stream'} = '';
40 1         3 my $buf = '';
41 1         9 while (!eof($fh)) {
42 2         6 read $fh, $buf, 512;
43 2         6 $self->{' stream'} .= $buf;
44             }
45 1         4 $self->{'Length'} = PDFNum(length $self->{' stream'});
46             }
47             else {
48 2         44 $self->{'Length'} = PDFNum(-s $file);
49 2         8 $self->{' streamfile'} = $file;
50             }
51              
52 3         37 $self->filters('DCTDecode');
53 3         7 $self->{' nofilt'} = 1;
54              
55 3         87 return $self;
56             }
57              
58             sub read_jpeg {
59 3     3 0 9 my ($self, $fh) = @_;
60 3         7 my ($buf, $p, $h, $w, $c, $ff, $mark, $len);
61              
62 3         44 $fh->seek(0,0);
63 3         44 $fh->read($buf,2);
64 3         77 while (1) {
65 15         36 $fh->read($buf, 4);
66 15         77 my ($ff, $mark, $len) = unpack('CCn', $buf);
67 15 50       32 last if $ff != 0xFF;
68 15 50 33     46 last if $mark == 0xDA || $mark == 0xD9; # SOS/EOI
69 15 50       25 last if $len < 2;
70 15 50       41 last if $fh->eof();
71 15         85 $fh->read($buf, $len - 2);
72 15 100       66 next if $mark == 0xFE;
73 12 100 66     72 next if $mark >= 0xE0 && $mark <= 0xEF;
74 9 50 66     85 if ($mark >= 0xC0 && $mark <= 0xCF && $mark != 0xC4 && $mark != 0xC8 && $mark != 0xCC) {
      66        
      66        
      33        
75 3         20 ($p, $h, $w, $c) = unpack('CnnC', substr($buf, 0, 6));
76 3         11 last;
77             }
78             }
79              
80 3         41 $self->width($w);
81 3         14 $self->height($h);
82 3         23 $self->bpc($p);
83              
84 3 50 33     17 if (defined($c) and $c == 3) {
    0 0        
    0 0        
85 3         14 $self->colorspace('DeviceRGB');
86             }
87             elsif (defined($c) and $c == 4) {
88 0         0 $self->colorspace('DeviceCMYK');
89             }
90             elsif (defined($c) and $c == 1) {
91 0         0 $self->colorspace('DeviceGray');
92             }
93              
94 3         6 return $self;
95             }
96              
97             1;