Re: Canal Alpha and Image Loading
| From: | Tom Rogers | Date: | Thu, 14 Jul 2005 15:29:31 +0000 |
| Subject: | Re: Canal Alpha and Image Loading | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-6021@lists.php.net to get a copy of this message | ||
Hi,
Friday, July 15, 2005, 12:44:10 AM, you wrote:
PBC> Hello,
PBC> I'm currently creating a image "editor" for a especific aplication, and
PBC> for a while i've been using a custom conversor from png to xpm files,
PBC> even it being slow it is the unique way I found to load
PBC> PNG files into XPM format to display in a drawing window, but know I
PBC> have a problem I can't handle with it, I can't display ALPHA channels,
PBC> it becomes blank since the conversor doesnt support, and i dont know
PBC> even if XPM has alpha support.
PBC> The question is, there is someone loading png files with another method?
PBC> And if there is, does it support Alpha channels?
PBC> Thanks
PBC> Piero
Here is a function I use to create xpm from png and uses the alpha
channel. The function is called with the input file and the background
rgb values. It should be easy to modify to your needs.
<?php
function pngtoxpm($input,$bg_red,$bg_green,$bg_blue){
$chars = array(
'
','.','+','@','#','%','&','*','=','-',';','>',',',')','!',
'~','{',']','^','/','(','_',':','<','[','}','|','1','2','3',
'4','5','6','7','8','9','0','a','b','c','d','e','f','g','h',
'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w',
'x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','`');
$len = count($chars);
$a = 2;
$b = 0;
$colors = array('000000'=>'. '); //preset black
$img = '';
$name = basename($input,".png");
$info = getimagesize($input);
$im = imagecreatefrompng($input);
for($y=0;$y<$info[1];$y++){
$img .= '"';
for($x=0;$x<$info[0];$x++){
$px = imagecolorat($im,$x,$y);
$rgb = imagecolorsforindex($im,$px);
if($rgb['alpha'] == 127){
$img .= ' '; // completly transparent
}elseif($rgb['alpha']){
// Calculate the colour mix depending on alpha
$bg = sprintf("%0.2f",($rgb['alpha']/127)); //%
of background colour
$fg = 1 - $bg; // % of foreground colour
$red = intval($bg * $bg_red) + intval($rgb['red']*$fg);
if($red > 255) $red = 255;
$green = intval($bg * $bg_green) +
intval($rgb['green']*$fg);
if($green > 255) $green = 255;
$blue = intval($bg * $bg_blue) + intval($rgb['blue']*$fg);
if($blue > 255) $blue = 255;
$i = sprintf("%02x%02x%02x",$red,$green,$blue);
if(!isset($colors[$i])){
$colors[$i] = $chars[$a].$chars[$b];
$a++;
if($a == $len){
$b++;
$a = 1;
if($b == $len){
echo "Overflow - too many
colours\n";
return false;
}
}
}
$img .= $colors[$i]; //add pixel to output
}else{
$i =
sprintf("%02x%02x%02x",$rgb['red'],$rgb['green'],$rgb['blue']);
if(!isset($colors[$i])){
$colors[$i] = $chars[$a].$chars[$b];
$a++;
if($a == $len){
$b++;
$a = 1;
if($b == $len){
echo "Overflow\n";
return false;
}
}
}
$img .= $colors[$i]; //add pixel to output
}
}
$img .='",'."\n"; //new row
}
$c = count($colors)+1;
$r = '';
$r .= "/* XPM */\n";
$r .= 'static char* '.$name.'[] = {'."\n";
$r .= '"'.$info[0].' '.$info[1].' '.$c.'
2"'.",\n";
$r .= '" c None s None",'."\n";
foreach($colors as $hex=>$idx){
$r .= '"'.$idx.' c
#'.$hex.'",'."\n";
}
$r .= substr($img,0,-2); //lose the last newline and comma
$r .= '};'."\n";
return $r;
}
$input = 'C:\openoffice-stock\stock_text_unindent.png';
//function called with filename and background colour (r,g,b)
echo pngtoxpm($input,214,214,214);
--
regards,
Tom