Optimized version of imagecreatefrombmp()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chung Leong

    #1

    Optimized version of imagecreatefrombmp()

    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_siz e) {
    $gd_header .= pack("n", $ncolor);
    }
    // no transparency
    $gd_header .= "\xFF\xFF\xFF\x FF";

    fwrite($dest_f, $gd_header);

    if($palette_siz e) {
    $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("\x0 0\x00\x00\x00", 256 - $ncolor);
    fwrite($dest_f, $gd_palette);
    }

    $scan_line_size = (($bits * $width) + 7) >> 3;
    $scan_line_alig n = ($scan_line_siz e & 0x03) ? 4 - ($scan_line_siz e & 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_si ze + $scan_line_alig n) * $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($pa th, $mode, $options, &$opened_pat h)
    {
    $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($co unt)
    {
    $ret = substr($this->buffer, $this->position, $count);
    $this->position += strlen($ret);
    return $ret;
    }

    function stream_write($d ata)
    {
    $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 imagecreatefrom bmp($filename) {
    // use a memory stream instead of a temp file
    // where possible
    if(function_exi sts('stream_wra pper_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(ConvertBMP2G D($filename, $tmp_name)) {
    $img = imagecreatefrom gd($tmp_name);
    if($del_tmp) {
    unlink($tmp_nam e);
    }
    return $img;
    } return false;
    }

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

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


  • cpweb

    #2
    Re: Optimized version of imagecreatefrom bmp()

    What about 16bit-Bitmaps?
    They don't seem to work..
    Do you have any idea?

    Michael

    Comment

    Working...