Completed
Push — master ( 312aa1...eb49de )
by Paul
9s
created

IconImage::isBmp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Elphin\IcoFileLoader;
4
5
/**
6
 * Holds data on an image within an Icon
7
 */
8
class IconImage
9
{
10
    /**
11
     * @var integer Width, in pixels, of the image
12
     */
13
    public $width;
14
    /**
15
     * @var integer Height, in pixels, of the image
16
     */
17
    public $height;
18
    /**
19
     * @var integer Number of colors in image
20
     */
21
    public $colorCount;
22
    /**
23
     * @var integer Reserved ( must be 0)
24
     */
25
    public $reserved;
26
    /**
27
     * @var integer Color Planes
28
     */
29
    public $planes;
30
    /**
31
     * @var integer bits per pixel
32
     */
33
    public $bitCount;
34
    /**
35
     * @var integer How many bytes in this resource?
36
     */
37
    public $sizeInBytes;
38
    /**
39
     * @var integer Where in the file is this image?
40
     */
41
    public $fileOffset;
42
43
    /**
44
     * @var integer size of BITMAPINFOHEADER structure
45
     */
46
    public $bmpHeaderSize;
47
48
    /**
49
     * @var integer image width from BITMAPINFOHEADER
50
     */
51
    public $bmpHeaderWidth;
52
53
    /**
54
     * @var integer image height from BITMAPINFOHEADER
55
     */
56
    public $bmpHeaderHeight;
57
58
    /**
59
     * @var string PNG file for icon images which use PNG
60
     */
61
    public $pngData = null;
62
63
    /**
64
     * @var string BMP bitmap data for images which use BMP
65
     */
66
    public $bmpData = null;
67
68
    public $palette = [];
69
70
    /**
71
     * IconEntry constructor.
72
     * @param array $data array of data extracted from a ICONDIRENTRY binary structure
73
     */
74 15
    public function __construct($data)
75
    {
76 15
        foreach ($data as $name => $value) {
77 15
            $this->$name = $value;
78 15
        }
79 15
    }
80
81 12
    public function getDescription()
82
    {
83 12
        return sprintf(
84 12
            '%dx%d pixel %s @ %d bits/pixel',
85 12
            $this->width,
86 12
            $this->height,
87 12
            $this->isPng() ? 'PNG' : 'BMP',
88 12
            $this->bitCount
89 12
        );
90
    }
91
92
    /**
93
     * Stores binary PNG file for the icon
94
     * @param string $pngData
95
     */
96 2
    public function setPngFile($pngData)
97
    {
98 2
        $this->pngData = $pngData;
99 2
    }
100
101 13
    public function isPng()
102
    {
103 13
        return !empty($this->pngData);
104
    }
105
106
    public function isBmp()
107
    {
108
        return empty($this->pngData);
109
    }
110
111 15
    public function setBitmapInfoHeader($bmpInfo)
112
    {
113 15
        if ($this->bitCount!=$bmpInfo['BitCount']) {
114
            //the original code updated the ICONDIRENTRY with this, but it doesn't seem necessary...
115
            throw new \Exception("bit count changed from ".$this->bitCount. " to ".$bmpInfo['BitCount']);
116
        }
117
        //we need this to calculate offsets when rendering
118 15
        $this->bmpHeaderWidth = $bmpInfo['Width'];
119 15
        $this->bmpHeaderHeight = $bmpInfo['Height'];
120 15
        $this->bmpHeaderSize = $bmpInfo['Size'];
121 15
    }
122
123 15
    public function setBitmapData($bmpData)
124
    {
125 15
        $this->bmpData = $bmpData;
126 15
    }
127
128 8
    public function addToBmpPalette($r, $g, $b, $reserved)
129
    {
130 8
        $this->palette[] = ['red' => $r, 'green' => $g, 'blue' => $b, 'reserved' => $reserved];
131 8
    }
132
}
133