Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /****************************************************************************
- * based on: *
- * http://tfcduke.developpez.com/tutoriel/format/tga/fichiers/tga_specs.pdf *
- ****************************************************************************/
- define( "TGA_COLOR_MAP_TYPE_NO_COLOR_MAP", 0 );
- define( "TGA_COLOR_MAP_TYPE_COLOR_MAP", 1 );
- define( "TGA_IMAGE_TYPE_NONE_DATA", 0 );
- define( "TGA_IMAGE_TYPE_COLOR_MAPPED", 1 );
- define( "TGA_IMAGE_TYPE_TRUE_COLOR", 2 );
- define( "TGA_IMAGE_TYPE_BLACK_AND_WHITE", 3 );
- define( "TGA_IMAGE_TYPE_ENCODED_COLOR_MAPPED", 9 );
- define( "TGA_IMAGE_TYPE_ENCODED_TRUE_COLOR", 10 );
- define( "TGA_IMAGE_TYPE_ENCODED_BLACK_AND_WHITE", 11 );
- define( "TGA_IMAGE_ORIGIN_BOTTOM_LEFT", 0x0 );
- define( "TGA_IMAGE_ORIGIN_BOTTOM_RIGHT", 0x10 );
- define( "TGA_IMAGE_ORIGIN_TOP_LEFT", 0x20 );
- define( "TGA_IMAGE_ORIGIN_TOP_RIGHT", 0x30 );
- class TGA {
- private $imageHandle;
- private $width;
- private $height;
- private $imageHeader;
- private $imageId;
- private $colorMap;
- private $imageData;
- private $imageFooter;
- public function __construct( $imageHandle, $width = -1, $height = -1 ) {
- $this->imageHandle = $imageHandle;
- $this->width = $width;
- $this->height = $height;
- }
- public function parse() {
- fseek( $this->imageHandle, 0 );
- $this->imageHeader["IdentSize"] = implode( unpack( "C", fread( $this->imageHandle, 1 ) ) );
- $this->imageHeader["ColorMapType"] = implode( unpack( "C", fread( $this->imageHandle, 1 ) ) );
- $this->imageHeader["ImageType"] = implode( unpack( "C", fread( $this->imageHandle, 1 ) ) );
- $this->imageHeader["ColorMapStart"] = implode( unpack( "S", fread( $this->imageHandle, 2 ) ) );
- $this->imageHeader["ColorMapLength"] = implode( unpack( "S", fread( $this->imageHandle, 2 ) ) );
- $this->imageHeader["ColorMapBits"] = implode( unpack( "C", fread( $this->imageHandle, 1 ) ) );
- $this->imageHeader["XStart"] = implode( unpack( "S", fread( $this->imageHandle, 2 ) ) );
- $this->imageHeader["YStart"] = implode( unpack( "S", fread( $this->imageHandle, 2 ) ) );
- $this->imageHeader["Width"] = implode( unpack( "S", fread( $this->imageHandle, 2 ) ) );
- $this->imageHeader["Height"] = implode( unpack( "S", fread( $this->imageHandle, 2 ) ) );
- $this->imageHeader["Bits"] = implode( unpack( "C", fread( $this->imageHandle, 1 ) ) );
- $this->imageHeader["Descriptor"] = implode( unpack( "C", fread( $this->imageHandle, 1 ) ) );
- $this->imageHeader["ImageOrigin"] = $this->imageHeader["Descriptor"] & 0x30;
- $this->imageHeader["AttributeBits"] = $this->imageHeader["Descriptor"] & 0x0f;
- if ( $this->imageHeader["IdentSize"] > 0 ) {
- $this->imageId = implode( unpack( "a{$this->imageHeader["IdentSize"]}", fread( $this->imageHandle, $this->imageHeader["IdentSize"] ) ) );
- }
- if ( $this->imageHeader["ColorMapType"] != 0 ) {
- throw new Exception( "tga_not_implemented_colormap", $this->imageHeader["ColorMapType"] );
- }
- fseek( $this->imageHandle, -26, SEEK_END );
- $this->parseFooter();
- fseek( $this->imageHandle, 18 + $this->imageHeader["IdentSize"] );
- switch ( $this->imageHeader["ImageType"] ) {
- case TGA_IMAGE_TYPE_TRUE_COLOR : {
- $this->createImageTrueColor();
- break;
- }
- default : {
- throw new Exception( "tga_not_implemented_image_type", $this->imageHeader["ImageType"] );
- }
- }
- }
- public function close() {
- fclose( $this->imageHandle );
- }
- public function getHeader() {
- return $this->imageHeader;
- }
- public function getFooter() {
- return $this->imageFooter;
- }
- private function parseFooter() {
- $this->imageFooter = array();
- $this->imageFooter["extensionOffset"] = implode( unpack( "L", fread( $this->imageHandle, 4 ) ) );
- $this->imageFooter["developerOffset"] = implode( unpack( "L", fread( $this->imageHandle, 4 ) ) );
- $this->imageFooter["signature"] = implode( unpack( "a16", fread( $this->imageHandle, 16 ) ) );
- $this->imageFooter["dot"] = implode( unpack( "C", fread( $this->imageHandle, 1 ) ) );
- $this->imageFooter["null"] = implode( unpack( "C", fread( $this->imageHandle, 1 ) ) );
- }
- private function createImageTrueColor() {
- if ( $this->width != -1 ) {
- $width = $this->width;
- } else {
- $width = $this->imageHeader["Width"];
- }
- if ( $this->height != -1 ) {
- $height = $this->height;
- } else {
- $height = $this->imageHeader["Height"];
- }
- $this->imageData = imagecreatetruecolor( $width, $height );
- switch ( $this->imageHeader["Bits"] ) {
- case 24 : {
- $pixelDataBytes = $this->imageHeader["Width"] * $this->imageHeader["Height"] * 3;
- $pixelData = unpack( "C{$pixelDataBytes}", fread( $this->imageHandle, $pixelDataBytes ) );
- break;
- }
- default : {
- throw new Exception( "tga_not_implemented_color_depth", $this->imageHeader["Bits"] );
- }
- }
- $horizontalBorder = floor( ( $this->imageHeader["Width"] - $width ) / 2 );
- $verticalBorder = floor( ( $this->imageHeader["Height"] - $height ) / 2 );
- for ( $y = $verticalBorder; $y < $this->imageHeader["Height"] - $verticalBorder; $y++ ) {
- for ( $x = $horizontalBorder; $x < $this->imageHeader["Width"] - $horizontalBorder; $x++ ) {
- $pixelRed = $pixelData[( $y * $this->imageHeader["Width"] + $x ) * 3 + 3];
- $pixelGreen = $pixelData[( $y * $this->imageHeader["Width"] + $x ) * 3 + 2];
- $pixelBlue = $pixelData[( $y * $this->imageHeader["Width"] + $x ) * 3 + 1];
- if ( ( $pixelRed != 0 ) && ( $pixelGreen != 0 ) && ( $pixelBlue != 0 ) ) {
- $color = imagecolorallocate ( $this->imageData, $pixelRed, $pixelGreen, $pixelBlue );
- switch ( $this->imageHeader["ImageOrigin"] ) {
- case TGA_IMAGE_ORIGIN_BOTTOM_LEFT : {
- imagesetpixel( $this->imageData, $x - $horizontalBorder, $this->imageHeader["Height"] - $y - $verticalBorder, $color );
- break;
- }
- case TGA_IMAGE_ORIGIN_BOTTOM_RIGHT : {
- imagesetpixel( $this->imageData, $this->imageHeader["Width"] - $x - $horizontalBorder, $this->imageHeader["Height"] - $y - $verticalBorder, $color );
- break;
- }
- case TGA_IMAGE_ORIGIN_TOP_LEFT : {
- imagesetpixel( $this->imageData, $x - $horizontalBorder, $y - $verticalBorder, $color );
- break;
- }
- case TGA_IMAGE_ORIGIN_TOP_RIGHT : {
- imagesetpixel( $this->imageData, $this->imageHeader["Width"] - $x - $horizontalBorder, $y - $verticalBorder, $color );
- break;
- }
- }
- }
- }
- }
- }
- public function getImage() {
- return $this->imageData;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement