bmp

The Gravity PDF bmp class.

Defined (1)

The class is defined in the following location(s).

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