Optimized version of imagecreatefrombmp()

From: Chung Leong (chernyshevsky_at_hotmail.com)
Date: 06/30/04

  • Next message: Chung Leong: "Re: substitution in variables"
    Date: Tue, 29 Jun 2004 21:16:03 -0400
    
    

    Unrolled the scanline conversion loop into a lamda function. Should be
    slightly quicker.

    <?

    function ConvertBMP2GD($src, $dest = false) {
     if(!($src_f = fopen($src, "rb"))) {
      trigger_error("Can't open $src", E_WARNING);
      return false;
     }
     if(!($dest_f = fopen($dest, "wb"))) {
      trigger_error("Can't open $dest", E_WARNING);
      return false;
     }
     $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f, 14));
     $info =
    unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyr
    es/Vncolor/Vimportant", fread($src_f, 40));

     extract($info);
     extract($header);

     if($type != 0x4D42) { // signature "BM"
      return false;
     }

     $palette_size = $offset - 54;
     $ncolor = $palette_size / 4;
     $gd_header = "";
     // true-color vs. palette
     $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
     $gd_header .= pack("n2", $width, $height);
     $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
     if($palette_size) {
      $gd_header .= pack("n", $ncolor);
     }
     // no transparency
     $gd_header .= "\xFF\xFF\xFF\xFF";

     fwrite($dest_f, $gd_header);

     if($palette_size) {
      $palette = fread($src_f, $palette_size);
      $gd_palette = "";
      $j = 0;
      while($j < $palette_size) {
       $b = $palette{$j++};
       $g = $palette{$j++};
       $r = $palette{$j++};
       $a = $palette{$j++};
       $gd_palette .= "$r$g$b$a";
      }
      $gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
      fwrite($dest_f, $gd_palette);
     }

     $scan_line_size = (($bits * $width) + 7) >> 3;
     $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03)
    : 0;

     if($bits == 24) {
      $j = 0;
      $k = 1;
      $m = 2;
      $function = 'return "';
      while($j < $scan_line_size) {
       $function .= "\\0\{\$s\{$m}}\{\$s\{$k}}\{\$s\{$j}}";
       $j += 3;
       $k += 3;
       $m += 3;
      }
      $function .= '";';
     }
     else if($bits == 32) {
      $function = 'return "';
      $j = 0;
      $k = 1;
      $m = 2;
      $n = 3;
      while($j < $scan_line_size) {
       $function .= "\\0\{\$s\{$m}}\{\$s\{$k}}\{\$s\{$j}}";
       $j += 4;
       $k += 4;
       $m += 4;
       $n += 4;
      }
      $function .= '";';
     }
     else if($bits == 8) {
      $function = 'return $s;';
     }
     else if($bits == 4) {
      $j = 0;
      $function = '';
      while($j < $scan_line_size) {
       $function .= "\$b=ord(\$s\{$j});";
       $function .= "\$a[]=chr(\$b>>4);";
       $function .= "\$a[]=chr(\$b&0x0F);";
       $j++;
      }
      $function .= "return substr(implode(\$a), 0, \$width);";
     }
     else if($bits == 1) {
      $j = 0;
      $function = '';
      while($j < $scan_line_size) {
       $function .= "\$b=ord(\$s\{$j});";
       $function .= "\$a[]=chr((int)((\$b&0x80)!=0));";
       $function .= "\$a[]=chr((int)((\$b&0x40)!=0));";
       $function .= "\$a[]=chr((int)((\$b&0x20)!=0));";
       $function .= "\$a[]=chr((int)((\$b&0x10)!=0));";
       $function .= "\$a[]=chr((int)((\$b&0x08)!=0));";
       $function .= "\$a[]=chr((int)((\$b&0x04)!=0));";
       $function .= "\$a[]=chr((int)((\$b&0x02)!=0));";
       $function .= "\$a[]=chr((int)((\$b&0x01)!=0));\n";
       $j++;
      }
     $function .= "return substr(implode(\$a), 0, \$width);";
     }

     $f = create_function('$s, $width', $function);

     for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
      // BMP stores scan lines starting from bottom
      fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
      $scan_line = fread($src_f, $scan_line_size);
      $gd_scan_line = $f($scan_line, $width);

      fwrite($dest_f, $gd_scan_line);
     }
     fclose($src_f);
     fclose($dest_f);
     return true;
    }

    class MemoryStream {
        var $position;
        var $varname;
        var $buffer;

        function stream_open($path, $mode, $options, &$opened_path)
        {
            $url = parse_url($path);
            $this->varname = $url["host"];
            $this->position = 0;
            $this->buffer = @$GLOBALS[$this->varname];

            return true;
        }

        function stream_close()
        {
            $GLOBALS[$this->varname] = $this->buffer;
        }

        function stream_read($count)
        {
            $ret = substr($this->buffer, $this->position, $count);
            $this->position += strlen($ret);
            return $ret;
        }

        function stream_write($data)
        {
            $this->buffer .= $data;
            $this->position += strlen($data);
            return strlen($data);
        }

        function stream_tell()
        {
            return $this->position;
        }

        function stream_eof()
        {
            return $this->position >= strlen($this->buffer);
        }

        function stream_stat() {
            return array( 'size' => strlen($this->buffer) );
        }
    }

    function imagecreatefrombmp($filename) {
     // use a memory stream instead of a temp file
     // where possible
     if(function_exists('stream_wrapper_register')
     && stream_wrapper_register("mem", "MemoryStream")) {
      $tmp_name = "mem://GD_TMP_FILE";
     $del_tmp = false;
     }
     else {
     $tmp_name = tempnam("/tmp", "GD");
     $del_tmp = true;
     }
     if(ConvertBMP2GD($filename, $tmp_name)) {
      $img = imagecreatefromgd($tmp_name);
      if($del_tmp) {
     unlink($tmp_name);
      }
      return $img;
     } return false;
    }

    $img = imagecreatefrombmp("test24bit.bmp");
    imagepng($img, "test.png");

    ?>
    <img src="test.png">


  • Next message: Chung Leong: "Re: substitution in variables"