c2ephp

sprites/S16Frame.php

Go to the documentation of this file.
00001 <?php
00002 require_once(dirname(__FILE__).'/SpriteFrame.php');
00003 require_once(dirname(__FILE__).'/../support/IReader.php');
00004 
00006 
00007 class S16Frame extends SpriteFrame {
00008 
00010 
00011     private $offset;
00012     private $reader;
00013     private $decoded;
00014     private $encoding;
00015 
00017     
00019     
00020     public function S16Frame($reader,$encoding='565',$width=false,$height=false,$offset=false)
00021     {
00022         if($reader instanceof IReader) {
00023             $this->reader = $reader;
00024             $this->encoding = $encoding;
00025             if($width === false || $height === false || $offset === false) {
00026                 $this->offset = $this->reader->ReadInt(4);
00027                 parent::SpriteFrame($this->reader->ReadInt(2), $this->reader->ReadInt(2));
00028             } else {
00029                 parent::SpriteFrame($width,$height);
00030                 $this->offset = $offset;
00031             }
00032         } else if(get_resource_type($reader) == 'gd') {
00033             $this->gdImage = $reader;
00034             $this->decoded = true;
00035             $this->encoding = $encoding;
00036             parent::SpriteFrame(imagesx($reader), imagesy($reader));
00037 
00038         }
00039     }
00040 
00042 
00047     public function Encode($format='565') {
00048         $this->EnsureDecoded();
00049         $data = ''; 
00050         for($y = 0; $y < $this->GetHeight(); $y++) {
00051             for($x = 0; $x < $this->GetWidth(); $x++ ) {
00052 
00053                 $pixel = $this->GetPixel($x,$y);
00054                 if($pixel['red'] > 255 || $pixel['green'] > 255 || $pixel['blue'] > 255) {
00055                     throw new Exception('Pixel colour out of range.');
00056                 }
00057                 $newpixel = 0;
00058                 if($this->encoding == '555') {
00059                     $newpixel = (($pixel['red'] << 7) & 0xF800) | (($pixel['green'] << 2) & 0x03E0) | (($pixel['blue'] >> 3) & 0x001F);
00060                 } else {
00061                     $newpixel = (($pixel['red'] << 8) & 0xF800) | (($pixel['green'] << 3) & 0x07E0) | (($pixel['blue'] >> 3) & 0x001F);
00062                 }
00063                 $data .= pack('v',$newpixel);
00064             }
00065         }
00066         return $data;
00067     }
00068 
00070     
00071     // @brief Decodes the S16Frame and creates a gdimage.
00075     protected function Decode() {
00076         if($this->decoded) { return $this->gdImage; }
00077 
00078             $image = imagecreatetruecolor($this->GetWidth(),
00079                 $this->GetHeight());
00080         $this->reader->Seek($this->offset);
00081         for($y = 0; $y < $this->GetHeight(); $y++)
00082         {
00083             for($x = 0; $x < $this->GetWidth(); $x++)
00084             {
00085                 $pixel = $this->reader->ReadInt(2);
00086                 $red = 0; $green = 0; $blue = 0;
00087                 if($this->encoding == "565")
00088                 {
00089                     $red   = ($pixel & 0xF800) >> 8;
00090                     $green = ($pixel & 0x07E0) >> 3;
00091                     $blue  = ($pixel & 0x001F) << 3;
00092                 }
00093                 else if($this->encoding == "555")
00094                 {
00095                     $red   = ($pixel & 0x7C00) >> 7;
00096                     $green = ($pixel & 0x03E0) >> 2;
00097                     $blue  = ($pixel & 0x001F) << 3;
00098                 }
00099                 $colour = imagecolorallocate($image, $red, $green, $blue);
00100                 imagesetpixel($image, $x, $y, $colour);
00101             }
00102         }
00103         $this->gdImage = $image;
00104         $this->decoded = true;
00105         return $image;
00106     }
00108 }
00109 ?>
 All Classes Files Functions Variables Enumerations