473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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_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">
Jul 17 '05 #1
1 4032
What about 16bit-Bitmaps?
They don't seem to work..
Do you have any idea?

Michael

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brian | last post by:
Someone posted a function to implement imagecreatefrombmp()but it fails in imagecreatedfromgd() in my test. Can the author check to see if theres a typo? Its very useful if it will work! Brian ...
0
by: StinkFinger | last post by:
All, Been reading other posts on other forums, i.e. Nukecops. My original code is this: function is_active($module) { global $prefix, $dbi; $result = sql_query("select active from...
1
by: Perttu Pulkkinen | last post by:
I'd like to support all common iamge formats in my admin system, but i don't see imagecreatefrombmp anywhere in php manual. How can that be? Bitmap is quite common still. And xbm - what is that?...
8
by: Jarod | last post by:
Hey Is VS2005 optimized for Intel or AMD processor ? What gives best speed when I develop webprojects ? What really means in the term of speed 2 cores or frequency I assume 64bits. Jarod
10
by: Mike | last post by:
Is it still true that the managed C++ compiler will produce much better opimizations than the C# compiler, or have some of the more global/aggressive opimizations been rolled into the 2005...
12
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum....
7
by: bonk | last post by:
I have a c# project as part of a larger VS 2005 solution that always gets build optimized and I therefore can not evaluate any values while debugging through the code ("Cannot evaluate expression...
4
by: Glenn | last post by:
OK, I've looked up this message but am not finding how to get rid of it: "Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because...
4
by: mingkin | last post by:
<?php function ConvertBMP2GD($src, $dest = false) { if(!($src_f = fopen($src, "rb"))) { return false; } if(!($dest_f = fopen($dest, "wb"))) { return false; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.