bmp.php 7.7 KB

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