Completed
Push — master ( eb49de...2435ec )
by Paul
8s
created

IconImage::setBitmapInfoHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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 18
    public function __construct($data)
75
    {
76 18
        foreach ($data as $name => $value) {
77 17
            $this->$name = $value;
78 18
        }
79 18
    }
80
81 13
    public function getDescription()
82
    {
83 13
        return sprintf(
84 13
            '%dx%d pixel %s @ %d bits/pixel',
85 13
            $this->width,
86 13
            $this->height,
87 13
            $this->isPng() ? 'PNG' : 'BMP',
88 13
            $this->bitCount
89 13
        );
90
    }
91
92
    /**
93
     * Stores binary PNG file for the icon
94
     * @param string $pngData
95
     */
96 3
    public function setPngFile($pngData)
97
    {
98 3
        $this->pngData = $pngData;
99 3
    }
100
101 15
    public function isPng()
102
    {
103 15
        return !empty($this->pngData);
104
    }
105
106 1
    public function isBmp()
107
    {
108 1
        return empty($this->pngData);
109
    }
110
111 17
    public function setBitmapInfoHeader($bmpInfo)
112
    {
113
        //@codeCoverageIgnoreStart
114
        if ($this->bitCount != $bmpInfo['BitCount']) {
115
            //the original code updated the ICONDIRENTRY with this, but it doesn't seem necessary...
116
            throw new \Exception("bit count changed from " . $this->bitCount . " to " . $bmpInfo['BitCount']);
117
        }
118
        //@codeCoverageIgnoreEnd
119
120
        //we need this to calculate offsets when rendering
121 17
        $this->bmpHeaderWidth = $bmpInfo['Width'];
122 17
        $this->bmpHeaderHeight = $bmpInfo['Height'];
123 17
        $this->bmpHeaderSize = $bmpInfo['Size'];
124 17
    }
125
126 17
    public function setBitmapData($bmpData)
127
    {
128 17
        $this->bmpData = $bmpData;
129 17
    }
130
131 10
    public function addToBmpPalette($r, $g, $b, $reserved)
132
    {
133 10
        $this->palette[] = ['red' => $r, 'green' => $g, 'blue' => $b, 'reserved' => $reserved];
134 10
    }
135
}
136