File Coverage

blib/lib/Image/MetaData/JPEG/access/various.pl
Criterion Covered Total %
statement 34 34 100.0
branch 6 10 60.0
condition 4 4 100.0
subroutine 6 6 100.0
pod 3 3 100.0
total 53 57 92.9


line stmt bran cond sub pod time code
1             ###########################################################
2             # A Perl package for showing/modifying JPEG (meta)data. #
3             # Copyright (C) 2004,2005,2006 Stefano Bettelli #
4             # See the COPYING and LICENSE files for license terms. #
5             ###########################################################
6             package Image::MetaData::JPEG;
7 14     14   57 no integer;
  14         22  
  14         68  
8 14     14   362 use strict;
  14         16  
  14         329  
9 14     14   50 use warnings;
  14         19  
  14         4498  
10              
11             ###########################################################
12             # This method is for display/debug pourpouse. It returns #
13             # a string describing the details of the structure of the #
14             # JPEG file linked to the current object. It can ask #
15             # details to sub-objects. #
16             ###########################################################
17             sub get_description {
18 29     29 1 134170 my ($this) = @_;
19             # prepare the string to be returned and store
20             # a bar and the associated filename
21 29         112 my $description = "Original JPEG file: $this->{filename}\n";
22             # Print the image size
23 29         100 $description .= sprintf "(%dx%d)\n", $this->get_dimensions();
24             # Loop over all segments (use the order of the array)
25 29         45 $description .= $_->get_description() foreach @{$this->{segments}};
  29         164  
26             # return the string which was cooked up
27 29         508 return $description;
28             }
29              
30             ###########################################################
31             # This method returns the image size from two specific #
32             # record values in the SOF segment. The return value is #
33             # (x-dimension, y- dimension). If there is no SOF segment #
34             # (or more than one), the return value is (0,0). In this #
35             # case one should investigate, because this is not normal.#
36             #=========================================================#
37             # Ref: .... ? #
38             ###########################################################
39             sub get_dimensions {
40 35     35 1 2203 my ($this) = @_;
41             # find the start of frame segments
42 35         148 my @sofs = $this->get_segments("SOF");
43             # if there is more than one such segment, there is something
44             # wrong. In this case it is better to return (0,0) and debug.
45 35 100       209 return (0,0) if (scalar @sofs) != 1;
46             # same if there is an error in the segment
47 21         32 my $segment = $sofs[0];
48 21 50       93 return (0,0) if $segment->{error};
49             # search the relevant records and get their values: if they are
50             # not there, we get undef, which we promptly transform into zero
51 21   100     111 my $xdim = $segment->search_record_value('MaxSamplesPerLine') || 0;
52 21   100     61 my $ydim = $segment->search_record_value('MaxLineNumber') || 0;
53             # return dimension values
54 21         93 return ( $xdim, $ydim );
55             }
56              
57             ###########################################################
58             # This method returns a reference to a hash with a plain #
59             # translation of the content of the first interesting #
60             # APP0 segment (this is the first 'JFXX' APP0 segment, #
61             # if present, the first 'JFIF' APP0 segment otherwise). #
62             # Segments with errors are excluded. An empty hash means #
63             # that no valid APP0 segment is present. #
64             # See Segment::parse_app0 for further details. #
65             #=========================================================#
66             # JFIF JFXX JFXX JFXX #
67             # (basic) (RGB 1 byte) (RGB 3 bytes) (JPEG) #
68             # ----------- ------------ ------------- ----------- #
69             # Identifier Identifier Identifier Identifier #
70             # MajorVersion ExtensionCode ExtensionCode ExtensionCode #
71             # MinorVersion XThumbnail XThumbnail JPEGThumbnail #
72             # Units YThumbnail YThumbnail #
73             # XDensity ColorPalette 3BytesThumbnail #
74             # YDensity 1ByteThumbnail #
75             # XThumbnail #
76             # YThumbnail #
77             # ThumbnailData #
78             ###########################################################
79             sub get_app0_data {
80 1     1 1 345 my ($this) = @_;
81             # find all APP0 segments, excluding segments with errors
82 1         7 my @app0s = grep { ! $_->{error} } $this->get_segments("APP0");
  1         4  
83             # select extended JFIF segments (the identifier contains JFXX)
84 1         2 my @jfxxs = grep { my $id = $_->search_record_value('Identifier');
  1         4  
85 1 50       10 defined $id && $id =~ /JFXX/ } @app0s;
86             # select a segment (try JFXX, then plain APP0, otherwise undef)
87 1 50       11 my $segment = @jfxxs ? $jfxxs[0] : (@app0s ? $app0s[0] : undef);
    50          
88             # prepare a hash with the records in the APP0 segment
89 1         2 my %data = map { $_->{key} => $_->get_value() } @{$segment->{records}};
  8         14  
  1         3  
90             # return a reference to the filled hash
91 1         5 return \ %data;
92             }
93              
94             # successful package load
95             1;