getid3_tiff::Analyze()   F
last analyzed

Complexity

Conditions 42
Paths 14448

Size

Total Lines 148
Code Lines 107

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 42
eloc 107
c 1
b 0
f 0
nc 14448
nop 0
dl 0
loc 148
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------+
3
// | PHP version 5                                                        |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
6
// +----------------------------------------------------------------------+
7
// | This source file is subject to version 2 of the GPL license,         |
8
// | that is bundled with this package in the file license.txt and is     |
9
// | available through the world-wide-web at the following url:           |
10
// | http://www.gnu.org/copyleft/gpl.html                                 |
11
// +----------------------------------------------------------------------+
12
// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
13
// +----------------------------------------------------------------------+
14
// | Authors: James Heinrich <info�getid3*org>                            |
15
// |          Allan Hansen <ah�artemis*dk>                                |
16
// +----------------------------------------------------------------------+
17
// | module.graphic.tiff.php                                              |
18
// | Module for analyzing TIFF graphic files.                             |
19
// | dependencies: NONE                                                   |
20
// +----------------------------------------------------------------------+
21
//
22
// $Id: module.graphic.tiff.php,v 1.2 2006/11/02 10:48:02 ah Exp $
23
24
class getid3_tiff extends getid3_handler
25
{
26
27
    public function Analyze()
28
    {
29
        $getid3 = $this->getid3;
30
31
        fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
32
        $tiff_header = fread($getid3->fp, 4);
33
34
        $getid3->info['tiff']['byte_order'] = 'II' == substr($tiff_header, 0, 2) ? 'Intel' : 'Motorola';
35
        $endian2int                         = 'II' == substr($tiff_header, 0, 2) ? 'LittleEndian2Int' : 'BigEndian2Int';
36
37
        $getid3->info['fileformat']          = 'tiff';
38
        $getid3->info['video']['dataformat'] = 'tiff';
39
        $getid3->info['video']['lossless']   = true;
40
        $getid3->info['tiff']['ifd']         = [];
41
        $current_ifd                         = [];
42
43
        $field_type_byte_length = [1 => 1, 2 => 1, 3 => 2, 4 => 4, 5 => 8];
44
45
        $next_ifd_offset = getid3_lib::$endian2int(fread($getid3->fp, 4));
46
47
        while ($next_ifd_offset > 0) {
48
            $current_ifd['offset'] = $next_ifd_offset;
49
50
            fseek($getid3->fp, $getid3->info['avdataoffset'] + $next_ifd_offset, SEEK_SET);
51
            $current_ifd['fieldcount'] = getid3_lib::$endian2int(fread($getid3->fp, 2));
52
53
            for ($i = 0; $i < $current_ifd['fieldcount']; $i++) {
54
                // shortcut
55
                $current_ifd['fields'][$i] = [];
56
                $current_ifd_fields_i      = &$current_ifd['fields'][$i];
57
58
                $current_ifd_fields_i['raw']['tag']    = getid3_lib::$endian2int(fread($getid3->fp, 2));
59
                $current_ifd_fields_i['raw']['type']   = getid3_lib::$endian2int(fread($getid3->fp, 2));
60
                $current_ifd_fields_i['raw']['length'] = getid3_lib::$endian2int(fread($getid3->fp, 4));
61
                $current_ifd_fields_i['raw']['offset'] = fread($getid3->fp, 4);
62
63
                switch ($current_ifd_fields_i['raw']['type']) {
64
                    case 1: // BYTE  An 8-bit unsigned integer.
65
                        if ($current_ifd_fields_i['raw']['length'] <= 4) {
66
                            $current_ifd_fields_i['value'] = getid3_lib::$endian2int(substr($current_ifd_fields_i['raw']['offset'], 0, 1));
67
                        } else {
68
                            $current_ifd_fields_i['offset'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']);
69
                        }
70
                        break;
71
72
                    case 2: // ASCII 8-bit bytes  that store ASCII codes; the last byte must be null.
73
                        if ($current_ifd_fields_i['raw']['length'] <= 4) {
74
                            $current_ifd_fields_i['value'] = substr($current_ifd_fields_i['raw']['offset'], 3);
75
                        } else {
76
                            $current_ifd_fields_i['offset'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']);
77
                        }
78
                        break;
79
80
                    case 3: // SHORT A 16-bit (2-byte) unsigned integer.
81
                        if ($current_ifd_fields_i['raw']['length'] <= 2) {
82
                            $current_ifd_fields_i['value'] = getid3_lib::$endian2int(substr($current_ifd_fields_i['raw']['offset'], 0, 2));
83
                        } else {
84
                            $current_ifd_fields_i['offset'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']);
85
                        }
86
                        break;
87
88
                    case 4: // LONG  A 32-bit (4-byte) unsigned integer.
89
                        if ($current_ifd_fields_i['raw']['length'] <= 1) {
90
                            $current_ifd_fields_i['value'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']);
91
                        } else {
92
                            $current_ifd_fields_i['offset'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']);
93
                        }
94
                        break;
95
96
                    case 5: // RATIONAL   Two LONG_s:  the first represents the numerator of a fraction, the second the denominator.
97
                        break;
98
                }
99
            }
100
101
            $getid3->info['tiff']['ifd'][] = $current_ifd;
102
            $current_ifd                   = [];
103
            $next_ifd_offset               = getid3_lib::$endian2int(fread($getid3->fp, 4));
104
        }
105
106
        foreach ($getid3->info['tiff']['ifd'] as $ifd_id => $ifd_array) {
107
            foreach ($ifd_array['fields'] as $key => $field_array) {
108
                switch ($field_array['raw']['tag']) {
109
                    case 256: // ImageWidth
110
                    case 257: // ImageLength
111
                    case 258: // BitsPerSample
112
                    case 259: // Compression
113
                        if (!isset($field_array['value'])) {
114
                            fseek($getid3->fp, $field_array['offset'], SEEK_SET);
115
                            $getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data'] = fread($getid3->fp, $field_array['raw']['length'] * $field_type_byte_length[$field_array['raw']['type']]);
116
                        }
117
                        break;
118
119
                    case 270: // ImageDescription
120
                    case 271: // Make
121
                    case 272: // Model
122
                    case 305: // Software
123
                    case 306: // DateTime
124
                    case 315: // Artist
125
                    case 316: // HostComputer
126
                        if (isset($field_array['value'])) {
127
                            $getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data'] = $field_array['value'];
128
                        } else {
129
                            fseek($getid3->fp, $field_array['offset'], SEEK_SET);
130
                            $getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data'] = fread($getid3->fp, $field_array['raw']['length'] * $field_type_byte_length[$field_array['raw']['type']]);
131
                        }
132
                        break;
133
                }
134
                switch ($field_array['raw']['tag']) {
135
                    case 256: // ImageWidth
136
                        $getid3->info['video']['resolution_x'] = $field_array['value'];
137
                        break;
138
139
                    case 257: // ImageLength
140
                        $getid3->info['video']['resolution_y'] = $field_array['value'];
141
                        break;
142
143
                    case 258: // BitsPerSample
144
                        if (isset($field_array['value'])) {
145
                            $getid3->info['video']['bits_per_sample'] = $field_array['value'];
146
                        } else {
147
                            $getid3->info['video']['bits_per_sample'] = 0;
148
                            for ($i = 0; $i < $field_array['raw']['length']; $i++) {
149
                                $getid3->info['video']['bits_per_sample'] += getid3_lib::$endian2int(substr($getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data'], $i * $field_type_byte_length[$field_array['raw']['type']], $field_type_byte_length[$field_array['raw']['type']]));
150
                            }
151
                        }
152
                        break;
153
154
                    case 259: // Compression
155
                        $getid3->info['video']['codec'] = getid3_tiff::TIFFcompressionMethod($field_array['value']);
156
                        break;
157
158
                    case 270: // ImageDescription
159
                    case 271: // Make
160
                    case 272: // Model
161
                    case 305: // Software
162
                    case 306: // DateTime
163
                    case 315: // Artist
164
                    case 316: // HostComputer
165
                        @$getid3->info['tiff']['comments'][getid3_tiff::TIFFcommentName($field_array['raw']['tag'])][] = $getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data'];
166
                        break;
167
168
                    default:
169
                        break;
170
                }
171
            }
172
        }
173
174
        return true;
175
    }
176
177
    public static function TIFFcompressionMethod($id)
178
    {
179
        static $lookup = [
180
            1     => 'Uncompressed',
181
            2     => 'Huffman',
182
            3     => 'Fax - CCITT 3',
183
            5     => 'LZW',
184
            32773 => 'PackBits',
185
        ];
186
        return (isset($lookup[$id]) ? $lookup[$id] : 'unknown/invalid (' . $id . ')');
187
    }
188
189
    public static function TIFFcommentName($id)
190
    {
191
        static $lookup = [
192
            270 => 'imagedescription',
193
            271 => 'make',
194
            272 => 'model',
195
            305 => 'software',
196
            306 => 'datetime',
197
            315 => 'artist',
198
            316 => 'hostcomputer',
199
        ];
200
        return (isset($lookup[$id]) ? $lookup[$id] : 'unknown/invalid (' . $id . ')');
201
    }
202
203
}
204
205
206