c2ephp
|
00001 <?php 00002 require_once(dirname(__FILE__).'/SpriteFrame.php'); 00003 require_once(dirname(__FILE__).'/../support/IReader.php'); 00004 require_once(dirname(__FILE__).'/../support/FileReader.php'); 00005 require_once(dirname(__FILE__).'/../support/StringReader.php'); 00006 00007 00009 class SPRFrame extends SpriteFrame { 00010 00012 00013 private $offset; 00014 private $reader; 00015 public static $sprToRGB; 00016 00018 00020 00028 public function SPRFrame($reader,$width=0,$height=0,$offset=false) { 00029 if($reader instanceof IReader) { 00030 $this->reader = $reader; 00031 parent::SpriteFrame($width,$height); 00032 if($offset !== false) { 00033 $this->offset = $offset; 00034 } else { 00035 $this->offset = $this->reader->GetPosition(); 00036 } 00037 00038 //initialise palette if necessary. 00039 if(empty(self::$sprToRGB)) { 00040 $paletteReader = new FileReader(dirname(__FILE__).'/palette.dta'); 00041 for($i = 0;$i<256;$i++) { 00042 self::$sprToRGB[$i] = array('r'=>$paletteReader->ReadInt(1)*4,'g'=>$paletteReader->ReadInt(1)*4,'b'=>$paletteReader->ReadInt(1)*4); 00043 } 00044 unset($paletteReader); 00045 } 00046 } else if(get_resource_type($reader) == 'gd') { 00047 parent::SpriteFrame(imagesx($reader),imagesy($reader),true); 00048 $this->gdImage = $reader; 00049 } else { 00050 throw new Exception('$reader was not an IReader or a gd image.'); 00051 } 00052 } 00053 00054 00056 00060 public function Flip() { 00061 if($this->HasBeenDecoded()) { 00062 throw new Exception('Too late!'); 00063 return; 00064 } 00065 $tempData = ''; 00066 for($i=($this->GetHeight()-1);$i>-1;$i--) { 00067 $tempData .= $this->reader->GetSubString($this->offset+($this->GetWidth())*$i,($this->GetWidth())); 00068 } 00069 $this->reader = new StringReader($tempData); 00070 $this->offset = 0; 00071 00072 } 00073 00074 00076 00078 protected function Decode() { 00079 $image = imagecreatetruecolor($this->GetWidth(), $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 $colour = self::$sprToRGB[$this->reader->ReadInt(1)]; 00086 imagesetpixel($image,$x,$y,imagecolorallocate($image,$colour['r'],$colour['g'],$colour['b'])); 00087 } 00088 } 00089 $this->gdImage = $image; 00090 } 00091 00093 00094 00096 00102 public function Encode() { 00103 $data = ''; 00104 for($y = 0; $y < $this->GetHeight(); $y++ ) { 00105 for($x = 0; $x < $this->GetWidth(); $x++ ) { 00106 $color = $this->GetPixel($x, $y); 00107 $data .= pack('C',$this->RGBToSPR($color['red'], $color['green'], $color['blue'])); 00108 } 00109 } 00110 return $data; 00111 } 00112 00114 00116 00120 private function RGBToSPR($r,$g,$b) { 00121 //start out with the maximum distance. 00122 $minDistance = ($r^2) + ($g^2) + ($b^2); 00123 $minKey = 0; 00124 foreach(self::$sprToRGB as $key => $colour) { 00125 $distance = pow(($r-$colour['r']),2) + pow(($g-$colour['g']),2) + pow(($b-$colour['b']),2); 00126 if($distance == 0) { 00127 return $key; 00128 } else if ($distance < $minDistance) { 00129 $minKey = $key; 00130 $minDistance = $distance; 00131 } 00132 } 00133 return $key; 00134 } 00136 } 00137 ?>