| 1 |
<?php |
| 2 |
|
| 3 |
class bmp |
| 4 |
{ |
| 5 |
|
| 6 |
var $mpdf = null; |
| 7 |
|
| 8 |
public function __construct(mPDF $mpdf) |
| 9 |
{ |
| 10 |
$this->mpdf = $mpdf; |
| 11 |
} |
| 12 |
|
| 13 |
function _getBMPimage($data, $file) |
| 14 |
{ |
| 15 |
$info = array(); |
| 16 |
// Adapted from script by Valentin Schmidt |
| 17 |
// http://staff.dasdeck.de/valentin/fpdf/fpdf_bmp/ |
| 18 |
$bfOffBits = $this->_fourbytes2int_le(substr($data, 10, 4)); |
| 19 |
$width = $this->_fourbytes2int_le(substr($data, 18, 4)); |
| 20 |
$height = $this->_fourbytes2int_le(substr($data, 22, 4)); |
| 21 |
$flip = ($height < 0); |
| 22 |
if ($flip) |
| 23 |
$height = -$height; |
| 24 |
$biBitCount = $this->_twobytes2int_le(substr($data, 28, 2)); |
| 25 |
$biCompression = $this->_fourbytes2int_le(substr($data, 30, 4)); |
| 26 |
$info = array('w' => $width, 'h' => $height); |
| 27 |
if ($biBitCount < 16) { |
| 28 |
$info['cs'] = 'Indexed'; |
| 29 |
$info['bpc'] = $biBitCount; |
| 30 |
$palStr = substr($data, 54, ($bfOffBits - 54)); |
| 31 |
$pal = ''; |
| 32 |
$cnt = strlen($palStr) / 4; |
| 33 |
for ($i = 0; $i < $cnt; $i++) { |
| 34 |
$n = 4 * $i; |
| 35 |
$pal .= $palStr[$n + 2] . $palStr[$n + 1] . $palStr[$n]; |
| 36 |
} |
| 37 |
$info['pal'] = $pal; |
| 38 |
} else { |
| 39 |
$info['cs'] = 'DeviceRGB'; |
| 40 |
$info['bpc'] = 8; |
| 41 |
} |
| 42 |
|
| 43 |
if ($this->mpdf->restrictColorSpace == 1 || $this->mpdf->PDFX || $this->mpdf->restrictColorSpace == 3) { |
| 44 |
if (($this->mpdf->PDFA && !$this->mpdf->PDFAauto) || ($this->mpdf->PDFX && !$this->mpdf->PDFXauto)) { |
| 45 |
$this->mpdf->PDFAXwarnings[] = "Image cannot be converted to suitable colour space for PDFA or PDFX file - " . $file . " - (Image replaced by 'no-image'.)"; |
| 46 |
} |
| 47 |
return array('error' => "BMP Image cannot be converted to suitable colour space - " . $file . " - (Image replaced by 'no-image'.)"); |
| 48 |
} |
| 49 |
|
| 50 |
$biXPelsPerMeter = $this->_fourbytes2int_le(substr($data, 38, 4)); // horizontal pixels per meter, usually set to zero |
| 51 |
//$biYPelsPerMeter=$this->_fourbytes2int_le(substr($data,42,4)); // vertical pixels per meter, usually set to zero |
| 52 |
$biXPelsPerMeter = round($biXPelsPerMeter / 1000 * 25.4); |
| 53 |
//$biYPelsPerMeter=round($biYPelsPerMeter/1000 *25.4); |
| 54 |
$info['set-dpi'] = $biXPelsPerMeter; |
| 55 |
|
| 56 |
switch ($biCompression) { |
| 57 |
case 0: |
| 58 |
$str = substr($data, $bfOffBits); |
| 59 |
break; |
| 60 |
case 1: # BI_RLE8 |
| 61 |
$str = $this->rle8_decode(substr($data, $bfOffBits), $width); |
| 62 |
break; |
| 63 |
case 2: # BI_RLE4 |
| 64 |
$str = $this->rle4_decode(substr($data, $bfOffBits), $width); |
| 65 |
break; |
| 66 |
} |
| 67 |
$bmpdata = ''; |
| 68 |
$padCnt = (4 - ceil(($width / (8 / $biBitCount))) % 4) % 4; |
| 69 |
switch ($biBitCount) { |
| 70 |
case 1: |
| 71 |
case 4: |
| 72 |
case 8: |
| 73 |
$w = floor($width / (8 / $biBitCount)) + ($width % (8 / $biBitCount) ? 1 : 0); |
| 74 |
$w_row = $w + $padCnt; |
| 75 |
if ($flip) { |
| 76 |
for ($y = 0; $y < $height; $y++) { |
| 77 |
$y0 = $y * $w_row; |
| 78 |
for ($x = 0; $x < $w; $x++) |
| 79 |
$bmpdata .= $str[$y0 + $x]; |
| 80 |
} |
| 81 |
} else { |
| 82 |
for ($y = $height - 1; $y >= 0; $y--) { |
| 83 |
$y0 = $y * $w_row; |
| 84 |
for ($x = 0; $x < $w; $x++) |
| 85 |
$bmpdata .= $str[$y0 + $x]; |
| 86 |
} |
| 87 |
} |
| 88 |
break; |
| 89 |
|
| 90 |
case 16: |
| 91 |
$w_row = $width * 2 + $padCnt; |
| 92 |
if ($flip) { |
| 93 |
for ($y = 0; $y < $height; $y++) { |
| 94 |
$y0 = $y * $w_row; |
| 95 |
for ($x = 0; $x < $width; $x++) { |
| 96 |
$n = (ord($str[$y0 + 2 * $x + 1]) * 256 + ord($str[$y0 + 2 * $x])); |
| 97 |
$b = ($n & 31) << 3; |
| 98 |
$g = ($n & 992) >> 2; |
| 99 |
$r = ($n & 31744) >> 7128; |
| 100 |
$bmpdata .= chr($r) . chr($g) . chr($b); |
| 101 |
} |
| 102 |
} |
| 103 |
} else { |
| 104 |
for ($y = $height - 1; $y >= 0; $y--) { |
| 105 |
$y0 = $y * $w_row; |
| 106 |
for ($x = 0; $x < $width; $x++) { |
| 107 |
$n = (ord($str[$y0 + 2 * $x + 1]) * 256 + ord($str[$y0 + 2 * $x])); |
| 108 |
$b = ($n & 31) << 3; |
| 109 |
$g = ($n & 992) >> 2; |
| 110 |
$r = ($n & 31744) >> 7; |
| 111 |
$bmpdata .= chr($r) . chr($g) . chr($b); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
break; |
| 116 |
|
| 117 |
case 24: |
| 118 |
case 32: |
| 119 |
$byteCnt = $biBitCount / 8; |
| 120 |
$w_row = $width * $byteCnt + $padCnt; |
| 121 |
|
| 122 |
if ($flip) { |
| 123 |
for ($y = 0; $y < $height; $y++) { |
| 124 |
$y0 = $y * $w_row; |
| 125 |
for ($x = 0; $x < $width; $x++) { |
| 126 |
$i = $y0 + $x * $byteCnt; # + 1 |
| 127 |
$bmpdata .= $str[$i + 2] . $str[$i + 1] . $str[$i]; |
| 128 |
} |
| 129 |
} |
| 130 |
} else { |
| 131 |
for ($y = $height - 1; $y >= 0; $y--) { |
| 132 |
$y0 = $y * $w_row; |
| 133 |
for ($x = 0; $x < $width; $x++) { |
| 134 |
$i = $y0 + $x * $byteCnt; # + 1 |
| 135 |
$bmpdata .= $str[$i + 2] . $str[$i + 1] . $str[$i]; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
break; |
| 140 |
|
| 141 |
default: |
| 142 |
return array('error' => 'Error parsing BMP image - Unsupported image biBitCount'); |
| 143 |
} |
| 144 |
if ($this->mpdf->compress) { |
| 145 |
$bmpdata = gzcompress($bmpdata); |
| 146 |
$info['f'] = 'FlateDecode'; |
| 147 |
} |
| 148 |
$info['data'] = $bmpdata; |
| 149 |
$info['type'] = 'bmp'; |
| 150 |
return $info; |
| 151 |
} |
| 152 |
|
| 153 |
function _fourbytes2int_le($s) |
| 154 |
{ |
| 155 |
//Read a 4-byte integer from string |
| 156 |
return (ord($s[3]) << 24) + (ord($s[2]) << 16) + (ord($s[1]) << 8) + ord($s[0]); |
| 157 |
} |
| 158 |
|
| 159 |
function _twobytes2int_le($s) |
| 160 |
{ |
| 161 |
//Read a 2-byte integer from string |
| 162 |
return (ord(substr($s, 1, 1)) << 8) + ord(substr($s, 0, 1)); |
| 163 |
} |
| 164 |
|
| 165 |
# Decoder for RLE8 compression in windows bitmaps |
| 166 |
# see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_6x0u.asp |
| 167 |
function rle8_decode($str, $width) |
| 168 |
{ |
| 169 |
$lineWidth = $width + (3 - ($width - 1) % 4); |
| 170 |
$out = ''; |
| 171 |
$cnt = strlen($str); |
| 172 |
for ($i = 0; $i < $cnt; $i++) { |
| 173 |
$o = ord($str[$i]); |
| 174 |
switch ($o) { |
| 175 |
case 0: # ESCAPE |
| 176 |
$i++; |
| 177 |
switch (ord($str[$i])) { |
| 178 |
case 0: # NEW LINE |
| 179 |
$padCnt = $lineWidth - strlen($out) % $lineWidth; |
| 180 |
if ($padCnt < $lineWidth) |
| 181 |
$out .= str_repeat(chr(0), $padCnt);# pad line |
| 182 |
break; |
| 183 |
case 1: # END OF FILE |
| 184 |
$padCnt = $lineWidth - strlen($out) % $lineWidth; |
| 185 |
if ($padCnt < $lineWidth) |
| 186 |
$out .= str_repeat(chr(0), $padCnt);# pad line |
| 187 |
break 3; |
| 188 |
case 2: # DELTA |
| 189 |
$i += 2; |
| 190 |
break; |
| 191 |
default: # ABSOLUTE MODE |
| 192 |
$num = ord($str[$i]); |
| 193 |
for ($j = 0; $j < $num; $j++) |
| 194 |
$out .= $str[++$i]; |
| 195 |
if ($num % 2) |
| 196 |
$i++; |
| 197 |
} |
| 198 |
break; |
| 199 |
default: |
| 200 |
$out .= str_repeat($str[++$i], $o); |
| 201 |
} |
| 202 |
} |
| 203 |
return $out; |
| 204 |
} |
| 205 |
|
| 206 |
# Decoder for RLE4 compression in windows bitmaps |
| 207 |
# see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_6x0u.asp |
| 208 |
function rle4_decode($str, $width) |
| 209 |
{ |
| 210 |
$w = floor($width / 2) + ($width % 2); |
| 211 |
$lineWidth = $w + (3 - ( ($width - 1) / 2) % 4); |
| 212 |
$pixels = array(); |
| 213 |
$cnt = strlen($str); |
| 214 |
for ($i = 0; $i < $cnt; $i++) { |
| 215 |
$o = ord($str[$i]); |
| 216 |
switch ($o) { |
| 217 |
case 0: # ESCAPE |
| 218 |
$i++; |
| 219 |
switch (ord($str[$i])) { |
| 220 |
case 0: # NEW LINE |
| 221 |
while (count($pixels) % $lineWidth != 0) |
| 222 |
$pixels[] = 0; |
| 223 |
break; |
| 224 |
case 1: # END OF FILE |
| 225 |
while (count($pixels) % $lineWidth != 0) |
| 226 |
$pixels[] = 0; |
| 227 |
break 3; |
| 228 |
case 2: # DELTA |
| 229 |
$i += 2; |
| 230 |
break; |
| 231 |
default: # ABSOLUTE MODE |
| 232 |
$num = ord($str[$i]); |
| 233 |
for ($j = 0; $j < $num; $j++) { |
| 234 |
if ($j % 2 == 0) { |
| 235 |
$c = ord($str[++$i]); |
| 236 |
$pixels[] = ($c & 240) >> 4; |
| 237 |
} else |
| 238 |
$pixels[] = $c & 15; |
| 239 |
} |
| 240 |
if ($num % 2) |
| 241 |
$i++; |
| 242 |
} |
| 243 |
break; |
| 244 |
default: |
| 245 |
$c = ord($str[++$i]); |
| 246 |
for ($j = 0; $j < $o; $j++) |
| 247 |
$pixels[] = ($j % 2 == 0 ? ($c & 240) >> 4 : $c & 15); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
$out = ''; |
| 252 |
if (count($pixels) % 2) |
| 253 |
$pixels[] = 0; |
| 254 |
$cnt = count($pixels) / 2; |
| 255 |
for ($i = 0; $i < $cnt; $i++) |
| 256 |
$out .= chr(16 * $pixels[2 * $i] + $pixels[2 * $i + 1]); |
| 257 |
return $out; |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
|