note 82799 added to function.imagesetpixel
| From: | (doctor3datgmail) | Date: | Sat, 26 Apr 2008 01:25:28 +0000 |
| Subject: | note 82799 added to function.imagesetpixel | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138788@lists.php.net to get a copy of this message | ||
here's my version of imagecreatefromtga() that's been tested to work for targa 16 ..
adapted from offering by zehao dot chang at gmail dot com
function imagecreatefromtga($filename)
{
$data = file_get_contents($filename);
// Extract header information
$string_length = fileint($data, 1, 1);
$data_type = fileint($data, 2, 1);
$width = fileint($data, 12, 2);
$height = fileint($data, 14, 2);
$bits_per_pixel = fileint($data, 16, 1);
$bytes_per_pixel = (int) $bits_per_pixel / 8;
// Currently 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;
$x = 0; $y = $height - 1;
$image = imagecreatetruecolor($width, $height);
while ($pointer < strlen($data))
{
if ($bytes_per_pixel == 2) // TARGA 16 - ARRRRRGG GGGBBBBB
{
$word = fileint($data, $pointer, 2);
$r = ($word & 0x7C00) >> 10;
$g = ($word & 0x03E0) >> 5;
$b = ($word & 0x001F);
imagesetpixel($image, $x, $y, $r << 19 | $g << 11 | $b << 3);
}
else if ($bytes_per_pixel == 3) // TARGA 24 - RRRRRRRR GGGGGGGG BBBBBBBB
{
imagesetpixel($image, $x, $y, fileint($data, $pointer, 3));
}
else if ($bytes_per_pixel == 4) // TARGA 32 - AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB
{
imagesetpixel($image, $x, $y, fileint($data, $pointer + 1, 3));
}
if (++$x == $width)
{
$y--;
$x = 0;
}
$pointer += $bytes_per_pixel;
}
return $image;
}
function fileint($data, $pos, $len)
{
return base_convert(bin2hex(strrev(substr($data, $pos, $len))), 16, 10);
}
----
Server IP: 209.41.74.194
Probable Submitter: 72.70.34.145
----
Manual Page -- http://www.php.net/manual/en/function.imagesetpixel.php
Edit -- https://master.php.net/note/edit/82799
Del: integrated -- https://master.php.net/note/delete/82799/integrated
Del: useless -- https://master.php.net/note/delete/82799/useless
Del: bad code -- https://master.php.net/note/delete/82799/bad+code
Del: spam -- https://master.php.net/note/delete/82799/spam
Del: non-english -- https://master.php.net/note/delete/82799/non-english
Del: in docs -- https://master.php.net/note/delete/82799/in+docs
Del: other reasons-- https://master.php.net/note/delete/82799
Reject -- https://master.php.net/note/reject/82799
Search -- https://master.php.net/manage/user-notes.php