473,516 Members | 2,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4037
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
23625
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 "Erwin Bon" <ernie@verkerk.nl> wrote in message news:<3fc9d260$0$4671$1b62eedf@news.euronet.nl>... > Hello, >
0
1335
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 web_modules where title='$module'", $dbi); list ($act) = sql_fetch_row($result, $dbi);
1
3072
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? Wbmp is said there to be wireless bitmap. So how can i create gd image from bitmap?
8
1325
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
2128
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 compiler? Are simple common sub-expressions and loop invariants optimized out in the current optimizer? thanks, m
12
2162
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. The size of the vector went from 3 (RGB) to 151 (400 nm to 700 with a sample every 2nm). The variables are using a simple Vector class defined as...
7
5608
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 because the code of the current method is optimized."). This happens alltough the checkbock "optimize code" in the project settings is switched off....
4
23198
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 the code is optimized" I'm trying to debug, and it's rather difficult when I can't find the values of expressions because of whatever is causing...
4
3365
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
7276
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7581
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5714
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.