| 1 | package Image::JpegCheck; | ||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use bytes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Fcntl ':seek'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | our $VERSION = '0.10'; | |||||||||||||||||||||||||||
| 9 | 5 | our @ISA = qw/Exporter/; | |||||||||||||||||||||||||||
| 10 | 5 | our @EXPORT = ('is_jpeg'); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4112 | ▶ | sub is_jpeg { | ||||||||||||||||||||||||||
|
Subroutine
is_jpeg: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 4112 | my ($file, ) = @_; | |||||||||||||||||||||||||||
| 14 | 4112 | ▶ | if (ref $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 4108 | ▶ | if (ref $file eq 'GLOB') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 16 | 4 | return Image::JpegCheck::_is_jpeg($file); | |||||||||||||||||||||||||||
| 17 | } elsif (ref $file eq 'SCALAR') { | ||||||||||||||||||||||||||||
| 18 | 4101 | ▶ | open my $fh, '<', $file or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 4101 | return Image::JpegCheck::_is_jpeg($fh); | |||||||||||||||||||||||||||
| 20 | } elsif (ref $file eq 'Path::Class::File') { | ||||||||||||||||||||||||||||
| 21 | 2 | return Image::JpegCheck::_is_jpeg($file->openr); | |||||||||||||||||||||||||||
| 22 | } else { | ||||||||||||||||||||||||||||
| 23 | 1 | Carp::croak('is_jpeg requires file-glob or filename'); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | } else { | ||||||||||||||||||||||||||||
| 26 | 4 | ▶ | open my $fh, '<', $file or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 4 | binmode $fh; | |||||||||||||||||||||||||||
| 28 | 4 | my $ret = Image::JpegCheck::_is_jpeg($fh); | |||||||||||||||||||||||||||
| 29 | 4 | close $fh; | |||||||||||||||||||||||||||
| 30 | 4 | return $ret; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 5 | SIZE_FIRST => 0xC0, # Range of segment identifier codes | |||||||||||||||||||||||||||
| 36 | SIZE_LAST => 0xC3, # that hold size info. | ||||||||||||||||||||||||||||
| 37 | SECTION_MARKER => "\xFF", | ||||||||||||||||||||||||||||
| 38 | SOI => "\xFF\xD8", | ||||||||||||||||||||||||||||
| 39 | EOI => "\xFF\xD9", | ||||||||||||||||||||||||||||
| 40 | EOI_RE => qr/\xFF\xD9\xFF*$/, | ||||||||||||||||||||||||||||
| 41 | READ_SIZE => 512, | ||||||||||||||||||||||||||||
| 42 | BYTE_STUFFING => "\xFF"x512, | ||||||||||||||||||||||||||||
| 43 | 5 | }; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4111 | ▶ | sub _is_jpeg { | ||||||||||||||||||||||||||
|
Subroutine
_is_jpeg: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 46 | 4111 | my $fh = $_[0]; | |||||||||||||||||||||||||||
| 47 | 4111 | my ($buf, $code, $marker, $len); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4111 | read($fh, $buf, 2); | |||||||||||||||||||||||||||
| 50 | 4111 | ▶ | return 0 if $buf ne SOI; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4107 | while (1) { | |||||||||||||||||||||||||||
| 53 | 20529 | read($fh, $buf, 2); | |||||||||||||||||||||||||||
| 54 | 20529 | ($marker, $code) = unpack("a a", $buf); # read segment header | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 20529 | ▶ | while ( $code eq SECTION_MARKER && ($marker = $code) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$code eq "\377" and $marker = $code
MC/DC50%1/2
$code eq "\377" and $marker = $code
$code eq "\377"
$marker = $code
Truth table66%2/3
A and B
A= $code eq "\377"B= $marker = $code
| |||||||||||||||||||||||||||||
| 57 | 108 | read($fh, $buf, 1); | |||||||||||||||||||||||||||
| 58 | 108 | ($code) = unpack("a", $buf); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 20529 | read($fh, $buf, 2); | |||||||||||||||||||||||||||
| 61 | 20529 | $len = unpack( "n", $buf ); | |||||||||||||||||||||||||||
| 62 | 20529 | $code = ord($code); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 20529 | ▶ | if ($marker ne SECTION_MARKER) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 65 | 0 | return 0; # invalid marker | |||||||||||||||||||||||||||
| 66 | ▶ | } elsif (($code >= SIZE_FIRST) && ($code <= SIZE_LAST)) { | |||||||||||||||||||||||||||
|
Condition66%2/3
$code >= 192 and $code <= 195
MC/DC50%1/2
$code >= 192 and $code <= 195
$code >= 192
$code <= 195
Truth table66%2/3
A and B
A= $code >= 192B= $code <= 195
| |||||||||||||||||||||||||||||
| 67 | 4107 | return 1; # got a size info | |||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||
| 69 | 16422 | seek $fh, $len-2, SEEK_CUR; # skip segment body | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 0 | die "should not reach here"; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 5 | 1; | |||||||||||||||||||||||||||
| 76 | __END__ | ||||||||||||||||||||||||||||