Specular channel embedder in PHP
by Konrad Kiss · 11/04/2009 (2:58 am) · 2 comments
This short php script uses the GD library to embed the average of the rgb channels of one image into another's alpha channel. Useful if you want to embed specular maps into diffuse (TGEA) or normal (Torque 3D) maps.
I created a php script a while ago that automatically embeds my specular maps into the normal maps, and creates proper material scripts (also checking for transparency in the diffuse texture). This snippet is a part of that solution. It can stand on its own, so I thought I'd share it.
The first parameter is the image which needs the alpha information. The second is the specular map. The result will be a file called the same as the first parameter with a .png added - so nothing is overwritten, unless there's already a file with that name from a previous conversion.
If you are looking for the same thing with a gui, be sure to check out Jaimi McEntire's Bitmap Channel Embedder which has saved me many many hours of image editing awkwardness.
Hope it saves someone a little time.
-- Konrad
I created a php script a while ago that automatically embeds my specular maps into the normal maps, and creates proper material scripts (also checking for transparency in the diffuse texture). This snippet is a part of that solution. It can stand on its own, so I thought I'd share it.
The first parameter is the image which needs the alpha information. The second is the specular map. The result will be a file called the same as the first parameter with a .png added - so nothing is overwritten, unless there's already a file with that name from a previous conversion.
If you are looking for the same thing with a gui, be sure to check out Jaimi McEntire's Bitmap Channel Embedder which has saved me many many hours of image editing awkwardness.
// -----------------------------------------------------------------------------------------
function imgcreate($path) {
// -----------------------------------------------------------------------------------------
switch (strtolower(strrchr($path, "."))) {
case ".jpg":
return imagecreatefromjpeg($path);
break;
case ".png":
return imagecreatefrompng($path);
break;
default:
return false;
}
}
// -----------------------------------------------------------------------------------------
function embedToAlpha($base, $source) {
// -----------------------------------------------------------------------------------------
if (!($rgb = imgcreate($base)))
return false;
if (!($aaa = imgcreate($source)))
return false;
$width = imagesx($rgb);
$height = imagesy($rgb);
if ($width != imagesx($aaa) || $height != imagesy($aaa)) {
imagedestroy($aaa);
imagedestroy($rgb);
return false;
}
$dst = imagecreatetruecolor($width,$height);
imagealphablending($dst, false);
imagesavealpha($dst, true);
for ($y=0;$y<$height;$y++) {
for ($x=0;$x<$width;$x++) {
$ndx = imagecolorat($aaa, $x, $y);
$col = imagecolorsforindex($aaa, $ndx);
$rgbndx = imagecolorat($rgb, $x, $y);
$rgbcol = imagecolorsforindex($rgb, $rgbndx);
$intensity = 127-round(($col['red'] + $col['green'] + $col['blue'])/6);
if ($intensity<0) $intensity = 0;
if ($intensity>127) $intensity = 127;
$nc = imagecolorallocatealpha($dst, $rgbcol["red"], $rgbcol["red"], $rgbcol["blue"], $intensity);
imagesetpixel($dst, $x, $y ,$nc );
}
}
imagedestroy($aaa);
imagedestroy($rgb);
imagepng($dst, $base.".png");
imagedestroy($dst);
}Hope it saves someone a little time.
-- Konrad
About the author
See www.bitgap.com.
Torque Owner SETTIMO EUGEN CRISTIAN
i have think abotu use php for do this because have many code implementation for the image specularity.