note 64069 added to function.imagesetpixel
| From: | zehao dot chang at gmail dot com | Date: | Thu, 06 Apr 2006 20:00:14 +0000 |
| Subject: | note 64069 added to function.imagesetpixel | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107468@lists.php.net to get a copy of this message | ||
Here's my stab at the imagecreatefromtga function. I used code from send at mail dot 2aj dot
net and others below as a basis, and added support for targa 16, targa 24 and targa 32. However, I
only support uncompressed RBG data type as that's the only one I need. (I removed the
return_array feature since you can simply use imagesx() and imagesy() to get the image size).
Please note that I have not tested this with a targa 16 since I don't have one handy at the
moment.
<?php
function imagecreatefromtga( $filename )
{
$handle = fopen( $filename, 'rb' );
$data = fread( $handle, filesize( $filename ) );
fclose( $handle );
// Extract header information
$string_length = base_convert( bin2hex( substr($data,1,1) ), 16, 10 );
$data_type = base_convert( bin2hex( substr($data,2,1) ), 16, 10 );
$width = base_convert( bin2hex( strrev( substr($data,12,2) ) ), 16, 10 );
$height = base_convert( bin2hex( strrev( substr($data,14,2) ) ), 16, 10 );
$bits_per_pixel = base_convert( bin2hex( substr($data,16,1) ), 16, 10 );
// Currenly I'm only supporting RGB Data type
switch( $data_type ) // Header information taken from http://astronomy.swin.edu.au/~pbourke/dataformats/tga/
{
case 2: // Uncompressed RGB image
break;
case 0: // No attached image data
case 1: // Uncompressed color-mapped image
case 3: // Uncompressed black and white image
case 9: // Runlength encoded color-mapped image
case 10: // Runlength encoded RGB image
case 11: // Compressed black and white image
case 32: // Compressed color-mapped data, using Huffman, Delta, and runlength encoding
case 33: // Compressed color-mapped data, using Huffman, Delta, and runlength encoding. 4-pass
quadtree-type process
default:
return NULL; // No support for any of these types
}
// Compute things we need from the header information
$pointer = 18 + $string_length;
$bytes_per_pixel = (int) $bits_per_pixel/8;
$x = 0; $y = $height;
$image = imagecreatetruecolor($width, $height);
while ( $pointer < strlen($data) )
{
if( $bytes_per_pixel == 2 ) // TARGA 16 - ARRRRRGG GGGBBBBB
{
$low_byte = bin2hex( strrev( substr($data, $pointer, $bytes_per_pixel)));
$high_byte = bin2hex( strrev( substr($data, $pointer, $bytes_per_pixel)));
$r = base_convert( ($high_byte & 0x7C)>>2, 16, 10);
$g = base_convert( (($high_byte & 0x03)<<3) | (($low_byte & 0xE0)>>5), 16,
10);
$b = base_convert( $low_byte & 0x1F, 16, 10);
imagesetpixel( $image, $x, $y, $r<<16 | $g<<8 | $b);
}
else if( $bytes_per_pixel == 3 ) // TARGA 24 - RRRRRRRR GGGGGGGG BBBBBBBB
{
imagesetpixel( $image, $x, $y, base_convert( bin2hex( strrev( substr($data, $pointer,
$bytes_per_pixel))), 16, 10));
}
else if( $bytes_per_pixel == 4 ) // TARGA 32 - AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB
{
imagesetpixel( $image, $x, $y, base_convert( bin2hex( strrev( substr($data, $pointer,
$bytes_per_pixel-1))), 16, 10));
}
if(++$x == $width)
{
$y--;
$x=0;
}
$pointer += $bytes_per_pixel;
}
return $image;
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 128.97.129.115
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.imagesetpixel.php
Edit -- http://master.php.net/note/edit/64069
Del: integrated -- http://master.php.net/note/delete/64069/integrated
Del: useless -- http://master.php.net/note/delete/64069/useless
Del: bad code -- http://master.php.net/note/delete/64069/bad+code
Del: spam -- http://master.php.net/note/delete/64069/spam
Del: non-english -- http://master.php.net/note/delete/64069/non-english
Del: in docs -- http://master.php.net/note/delete/64069/in+docs
Del: other reasons-- http://master.php.net/note/delete/64069
Reject -- http://master.php.net/note/reject/64069
Search -- http://master.php.net/manage/user-notes.php