summaryrefslogtreecommitdiffstats
path: root/modules/getid3/module.graphic.bmp.php
blob: ca1ce87f57650a549cbcfb51563646f9426643a2 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2 of the GPL license,         |
// | that is bundled with this package in the file license.txt and is     |
// | available through the world-wide-web at the following url:           |
// | http://www.gnu.org/copyleft/gpl.html                                 |
// +----------------------------------------------------------------------+
// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
// +----------------------------------------------------------------------+
// | Authors: James Heinrich <info�getid3*org>                            |
// |          Allan Hansen <ah�artemis*dk>                                |
// +----------------------------------------------------------------------+
// | module.graphic.bmp.php                                               |
// | Module for analyzing BMP graphic files.                              |
// | dependencies: NONE                                                   |
// +----------------------------------------------------------------------+
//
// $Id: module.graphic.bmp.php,v 1.4 2006/11/02 10:48:02 ah Exp $

        
        
class getid3_bmp extends getid3_handler
{


    public function Analyze() {

        $getid3 = $this->getid3;

        // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp
        // all versions
        // WORD    bfType;
        // DWORD   bfSize;
        // WORD    bfReserved1;
        // WORD    bfReserved2;
        // DWORD   bfOffBits;
        
        // shortcuts
        $getid3->info['bmp']['header']['raw'] = array ();
        $info_bmp            = &$getid3->info['bmp'];
        $info_bmp_header     = &$info_bmp['header'];
        $info_bmp_header_raw = &$info_bmp_header['raw'];

        fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
        $bmp_header = fread($getid3->fp, 14 + 40);

        // Magic bytes
        $info_bmp_header_raw['identifier'] = 'BM';
        
        getid3_lib::ReadSequence('LittleEndian2Int', $info_bmp_header_raw, $bmp_header, 2,
            array (
                'filesize'    => 4,
                'reserved1'   => 2,
                'reserved2'   => 2,
                'data_offset' => 4,
                'header_size' => 4
            )
        );
        
        // Check if the hardcoded-to-1 "planes" is at offset 22 or 26
        $planes22 = getid3_lib::LittleEndian2Int(substr($bmp_header, 22, 2));
        $planes26 = getid3_lib::LittleEndian2Int(substr($bmp_header, 26, 2));
        if (($planes22 == 1) && ($planes26 != 1)) {
            $info_bmp['type_os']      = 'OS/2';
            $info_bmp['type_version'] = 1;
        } 
        elseif (($planes26 == 1) && ($planes22 != 1)) {
            $info_bmp['type_os']      = 'Windows';
            $info_bmp['type_version'] = 1;
        } 
        elseif ($info_bmp_header_raw['header_size'] == 12) {
            $info_bmp['type_os']      = 'OS/2';
            $info_bmp['type_version'] = 1;
        } 
        elseif ($info_bmp_header_raw['header_size'] == 40) {
            $info_bmp['type_os']      = 'Windows';
            $info_bmp['type_version'] = 1;
        } 
        elseif ($info_bmp_header_raw['header_size'] == 84) {
            $info_bmp['type_os']      = 'Windows';
            $info_bmp['type_version'] = 4;
        } 
        elseif ($info_bmp_header_raw['header_size'] == 100) {
            $info_bmp['type_os']      = 'Windows';
            $info_bmp['type_version'] = 5;
        } 
        else {
            throw new getid3_exception('Unknown BMP subtype (or not a BMP file)');
        }

        $getid3->info['fileformat']                  = 'bmp';
        $getid3->info['video']['dataformat']         = 'bmp';
        $getid3->info['video']['lossless']           = true;
        $getid3->info['video']['pixel_aspect_ratio'] = (float)1;

        if ($info_bmp['type_os'] == 'OS/2') {

            // OS/2-format BMP
            // http://netghost.narod.ru/gff/graphics/summary/os2bmp.htm

            // DWORD  Size;             /* Size of this structure in bytes */
            // DWORD  Width;            /* Bitmap width in pixels */
            // DWORD  Height;           /* Bitmap height in pixel */
            // WORD   NumPlanes;        /* Number of bit planes (color depth) */
            // WORD   BitsPerPixel;     /* Number of bits per pixel per plane */
            
            getid3_lib::ReadSequence('LittleEndian2Int', $info_bmp_header_raw, $bmp_header, 18,
                array (
                    'width'          => 2,
                    'height'         => 2,
                    'planes'         => 2,
                    'bits_per_pixel' => 2
                )
            );
            
            $getid3->info['video']['resolution_x']    = $info_bmp_header_raw['width'];
            $getid3->info['video']['resolution_y']    = $info_bmp_header_raw['height'];
            $getid3->info['video']['codec']           = 'BI_RGB '.$info_bmp_header_raw['bits_per_pixel'].'-bit';
            $getid3->info['video']['bits_per_sample'] = $info_bmp_header_raw['bits_per_pixel'];
            
            if ($info_bmp['type_version'] >= 2) {
                // DWORD  Compression;      /* Bitmap compression scheme */
                // DWORD  ImageDataSize;    /* Size of bitmap data in bytes */
                // DWORD  XResolution;      /* X resolution of display device */
                // DWORD  YResolution;      /* Y resolution of display device */
                // DWORD  ColorsUsed;       /* Number of color table indices used */
                // DWORD  ColorsImportant;  /* Number of important color indices */
                // WORD   Units;            /* Type of units used to measure resolution */
                // WORD   Reserved;         /* Pad structure to 4-byte boundary */
                // WORD   Recording;        /* Recording algorithm */
                // WORD   Rendering;        /* Halftoning algorithm used */
                // DWORD  Size1;            /* Reserved for halftoning algorithm use */
                // DWORD  Size2;            /* Reserved for halftoning algorithm use */
                // DWORD  ColorEncoding;    /* Color model used in bitmap */
                // DWORD  Identifier;       /* Reserved for application use */
                
                getid3_lib::ReadSequence('LittleEndian2Int', $info_bmp_header_raw, $bmp_header, 26,
                    array (
                        'compression'      => 4,
                        'bmp_data_size'    => 4,
                        'resolution_h'     => 4,
                        'resolution_v'     => 4,
                        'colors_used'      => 4,
                        'colors_important' => 4,
                        'resolution_units' => 2,
                        'reserved1'        => 2,
                        'recording'        => 2,
                        'rendering'        => 2,
                        'size1'            => 4,
                        'size2'            => 4,
                        'color_encoding'   => 4,
                        'identifier'       => 4
                    )
                );

                $info_bmp_header['compression'] = getid3_bmp::BMPcompressionOS2Lookup($info_bmp_header_raw['compression']);
                $getid3->info['video']['codec'] = $info_bmp_header['compression'].' '.$info_bmp_header_raw['bits_per_pixel'].'-bit';
            }
            
            return true;
        } 
    
        
        if ($info_bmp['type_os'] == 'Windows') {

            // Windows-format BMP

            // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp
            // all versions
            // DWORD  biSize;
            // LONG   biWidth;
            // LONG   biHeight;
            // WORD   biPlanes;
            // WORD   biBitCount;
            // DWORD  biCompression;
            // DWORD  biSizeImage;
            // LONG   biXPelsPerMeter;
            // LONG   biYPelsPerMeter;
            // DWORD  biClrUsed;
            // DWORD  biClrImportant;

            getid3_lib::ReadSequence('LittleEndian2Int', $info_bmp_header_raw, $bmp_header, 18,
                array (
                    'width'            => -4,        //signed
                    'height'           => -4,        //signed
                    'planes'           => 2,
                    'bits_per_pixel'   => 2,
                    'compression'      => 4,
                    'bmp_data_size'    => 4,
                    'resolution_h'     => -4,        //signed
                    'resolution_v'     => -4,        //signed
                    'colors_used'      => 4,
                    'colors_important' => 4
                )
            );
            foreach (array ('width', 'height', 'resolution_h', 'resolution_v') as $key) {
                $info_bmp_header_raw[$key] = getid3_lib::LittleEndian2Int($info_bmp_header_raw[$key], true);
            }
            
            $info_bmp_header['compression']           = getid3_bmp::BMPcompressionWindowsLookup($info_bmp_header_raw['compression']);
            $getid3->info['video']['resolution_x']    = $info_bmp_header_raw['width'];
            $getid3->info['video']['resolution_y']    = $info_bmp_header_raw['height'];
            $getid3->info['video']['codec']           = $info_bmp_header['compression'].' '.$info_bmp_header_raw['bits_per_pixel'].'-bit';
            $getid3->info['video']['bits_per_sample'] = $info_bmp_header_raw['bits_per_pixel'];

            // should only be v4+, but BMPs with type_version==1 and BI_BITFIELDS compression have been seen
            if (($info_bmp['type_version'] >= 4) || ($info_bmp_header_raw['compression'] == 3)) {
  	            
                
                $bmp_header .= fread($getid3->fp, 44);

                // BITMAPV4HEADER - [44 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_2k1e.asp
                // Win95+, WinNT4.0+
                // DWORD        bV4RedMask;
                // DWORD        bV4GreenMask;
                // DWORD        bV4BlueMask;
                // DWORD        bV4AlphaMask;
                // DWORD        bV4CSType;
                // CIEXYZTRIPLE bV4Endpoints;
                // DWORD        bV4GammaRed;
                // DWORD        bV4GammaGreen;
                // DWORD        bV4GammaBlue;
                
                getid3_lib::ReadSequence('LittleEndian2Int', $info_bmp_header_raw, $bmp_header, 54,
                    array (
                        'red_mask'     => 4,
                        'green_mask'   => 4,
                        'blue_mask'    => 4,
                        'alpha_mask'   => 4,
                        'cs_type'      => 4,
                        'ciexyz_red'   => -4,       //string
                        'ciexyz_green' => -4,       //string
                        'ciexyz_blue'  => -4,       //string
                        'gamma_red'    => 4,
                        'gamma_green'  => 4,
                        'gamma_blue'   => 4
                    )
                );
                
                $info_bmp_header['ciexyz_red']   = getid3_bmp::FixedPoint2_30(strrev($info_bmp_header_raw['ciexyz_red']));
                $info_bmp_header['ciexyz_green'] = getid3_bmp::FixedPoint2_30(strrev($info_bmp_header_raw['ciexyz_green']));
                $info_bmp_header['ciexyz_blue']  = getid3_bmp::FixedPoint2_30(strrev($info_bmp_header_raw['ciexyz_blue']));
            

                if ($info_bmp['type_version'] >= 5) {
                    $bmp_header .= fread($getid3->fp, 16);
    
                    // BITMAPV5HEADER - [16 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_7c36.asp
                    // Win98+, Win2000+
                    // DWORD        bV5Intent;
                    // DWORD        bV5ProfileData;
                    // DWORD        bV5ProfileSize;
                    // DWORD        bV5Reserved;
                    
                    getid3_lib::ReadSequence('LittleEndian2Int', $info_bmp_header_raw, $bmp_header, 98,
                        array (
                            'intent'              => 4,
                            'profile_data_offset' => 4,
                            'profile_data_size'   => 4,
                            'reserved3'           => 4
                        )
                    );
                    
                }
            }
            
            return true;
        }


        throw new getid3_exception('Unknown BMP format in header.');
        
    }



    public static function BMPcompressionWindowsLookup($compression_id) {

        static $lookup = array (
            0 => 'BI_RGB',
            1 => 'BI_RLE8',
            2 => 'BI_RLE4',
            3 => 'BI_BITFIELDS',
            4 => 'BI_JPEG',
            5 => 'BI_PNG'
        );
        return (isset($lookup[$compression_id]) ? $lookup[$compression_id] : 'invalid');
    }



    public static function BMPcompressionOS2Lookup($compression_id) {
        
        static $lookup = array (
            0 => 'BI_RGB',
            1 => 'BI_RLE8',
            2 => 'BI_RLE4',
            3 => 'Huffman 1D',
            4 => 'BI_RLE24',
        );
        return (isset($lookup[$compression_id]) ? $lookup[$compression_id] : 'invalid');
    }
    
    
    public static function FixedPoint2_30($raw_data) {
        
        $binary_string = getid3_lib::BigEndian2Bin($raw_data);
        return bindec(substr($binary_string, 0, 2)) + (float)(bindec(substr($binary_string, 2, 30)) / 1073741824);        // pow(2, 30) = 1073741824
    }


}


?>