-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from 0xABADCAFE/issues/issue-6/more-routines
Image loader - absolute mvp
- Loading branch information
Showing
11 changed files
with
333 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"displays": { | ||
"default": { | ||
"sType": "DoubleVerticalRGB", | ||
"iWidth": 100, | ||
"iHeight": 100, | ||
"iMaxFPS": 30 | ||
} | ||
}, | ||
"routines": { | ||
"image": { | ||
"sType": "RGBImage", | ||
"iPriority": 0, | ||
"aParameters": { | ||
"sPath": "images/slipped_disc_1.ppm" | ||
} | ||
} | ||
}, | ||
"events": [ | ||
{ | ||
"at": 0.0, | ||
"on": "routine/image", | ||
"do": "enable" | ||
}, | ||
{ | ||
"at": 0.1, | ||
"do": "end" | ||
} | ||
] | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* ______ __ | ||
* __ /\\\\\\\\_ /\\\ | ||
* /\\\ /\\\//////\\\_ \/\\\ | ||
* /\\\// \/// \//\\\ ________ ___\/\\\ _______ | ||
* /\\\// /\\\ /\\\\\\\\\_ /\\\\\\\\\ /\\\\\\\\_ | ||
* /\\\//_ /\\\\/ /\\\/////\\\ /\\\////\\\ /\\\/////\\\ | ||
* \////\\\ __ /\\\/ \/\\\ \/\\\ \/\\\ \/\\\ /\\\\\\\\\\\ | ||
* \////\\\ __ \///_ \/\\\___\/\\\ \/\\\__\/\\\ \//\\\//////_ | ||
* \////\\\ /\\\ \/\\\\\\\\\\ \//\\\\\\\\\ \//\\\\\\\\\ | ||
* \/// \/// \/\\\////// \///////// \///////// | ||
* \/\\\ | ||
* \/// | ||
* | ||
* /P(?:ointless|ortable|HP) Demo Engine/ | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ABadCafe\PDE\Routine; | ||
|
||
use ABadCafe\PDE; | ||
use \SPLFixedArray; | ||
|
||
/** | ||
* Display an image | ||
* | ||
* TODO controls and optimise | ||
*/ | ||
class RGBImage extends Base implements IResourceLoader { | ||
|
||
use TResourceLoader; | ||
|
||
private int $iWidth, $iHeight, $iViewWidth, $iViewHeight; | ||
|
||
private SPLFixedArray $oPixels; | ||
|
||
const DEFAULT_PARAMETERS = [ | ||
'sPath' => 'required' | ||
]; | ||
|
||
public function preload() : self { | ||
$this->loadPNM($this->oParameters->sPath); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function setDisplay(PDE\IDisplay $oDisplay) : self { | ||
$this->bCanRender = ($oDisplay instanceof PDE\Display\IPixelled); | ||
$this->oDisplay = $oDisplay; | ||
$this->iViewWidth = $oDisplay->getWidth(); | ||
$this->iViewHeight = $oDisplay->getHeight(); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function render(int $iFrameNumber, float $fTimeIndex) : self { | ||
if ($this->canRender($iFrameNumber, $fTimeIndex)) { | ||
$oBuffer = $this->oDisplay->getPixelBuffer(); | ||
if ($this->iWidth == $this->iViewWidth && $this->iHeight == $this->iViewHeight) { | ||
foreach ($oBuffer as $i => $iBufferRGB) { | ||
$oBuffer[$i] = $this->oPixels[$i]; | ||
} | ||
} | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function parameterChange() { | ||
} | ||
|
||
/** | ||
* Load a PNM image | ||
*/ | ||
protected function loadPNM(string $sPath) { | ||
$sRaw = $this->loadFile($sPath); | ||
if (preg_match('/^(\d+)\s+(\d+)$/m', $sRaw, $aMatches)) { | ||
$this->iWidth = (int)$aMatches[1]; | ||
$this->iHeight = (int)$aMatches[2]; | ||
$iArea = $this->iWidth * $this->iHeight; | ||
$this->oPixels = new SPLFixedArray($iArea); | ||
$sData = substr($sRaw, ($iArea * -3)); | ||
$iDataOffset = 0; | ||
for ($i = 0; $i < $iArea; ++$i) { | ||
$this->oPixels[$i] = | ||
(ord($sData[$iDataOffset++]) << 16) | | ||
(ord($sData[$iDataOffset++]) << 8) | | ||
(ord($sData[$iDataOffset++])); | ||
} | ||
} else { | ||
throw new \Exception('Invalid PNM Format'); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* ______ __ | ||
* __ /\\\\\\\\_ /\\\ | ||
* /\\\ /\\\//////\\\_ \/\\\ | ||
* /\\\// \/// \//\\\ ________ ___\/\\\ _______ | ||
* /\\\// /\\\ /\\\\\\\\\_ /\\\\\\\\\ /\\\\\\\\_ | ||
* /\\\//_ /\\\\/ /\\\/////\\\ /\\\////\\\ /\\\/////\\\ | ||
* \////\\\ __ /\\\/ \/\\\ \/\\\ \/\\\ \/\\\ /\\\\\\\\\\\ | ||
* \////\\\ __ \///_ \/\\\___\/\\\ \/\\\__\/\\\ \//\\\//////_ | ||
* \////\\\ /\\\ \/\\\\\\\\\\ \//\\\\\\\\\ \//\\\\\\\\\ | ||
* \/// \/// \/\\\////// \///////// \///////// | ||
* \/\\\ | ||
* \/// | ||
* | ||
* /P(?:ointless|ortable|HP) Demo Engine/ | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ABadCafe\PDE\Routine; | ||
|
||
use ABadCafe\PDE; | ||
|
||
/** | ||
* IResourceLoader | ||
* | ||
* Tag interface for routines that need to be able to load additional data | ||
*/ | ||
interface IResourceLoader { | ||
|
||
/** | ||
* Set the base path | ||
* | ||
* @param string $sBasePath | ||
* @return self | ||
*/ | ||
public function setBasePath(string $sBasePath) : self; | ||
|
||
/** | ||
* Preload any resources | ||
* | ||
* @return self | ||
*/ | ||
public function preload() : self; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* ______ __ | ||
* __ /\\\\\\\\_ /\\\ | ||
* /\\\ /\\\//////\\\_ \/\\\ | ||
* /\\\// \/// \//\\\ ________ ___\/\\\ _______ | ||
* /\\\// /\\\ /\\\\\\\\\_ /\\\\\\\\\ /\\\\\\\\_ | ||
* /\\\//_ /\\\\/ /\\\/////\\\ /\\\////\\\ /\\\/////\\\ | ||
* \////\\\ __ /\\\/ \/\\\ \/\\\ \/\\\ \/\\\ /\\\\\\\\\\\ | ||
* \////\\\ __ \///_ \/\\\___\/\\\ \/\\\__\/\\\ \//\\\//////_ | ||
* \////\\\ /\\\ \/\\\\\\\\\\ \//\\\\\\\\\ \//\\\\\\\\\ | ||
* \/// \/// \/\\\////// \///////// \///////// | ||
* \/\\\ | ||
* \/// | ||
* | ||
* /P(?:ointless|ortable|HP) Demo Engine/ | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ABadCafe\PDE\Routine; | ||
|
||
use ABadCafe\PDE; | ||
|
||
/** | ||
* TResourceLoader | ||
* | ||
* Common implementation for IResourceLoader | ||
*/ | ||
trait TResourceLoader { | ||
|
||
public function setBasePath(string $sBasePath) : self { | ||
$this->sBasePath = $sBasePath; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Load a file. | ||
* | ||
* @param string $sRelativePath | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
private function loadFile(string $sRelativePath) : string { | ||
$sPath = $this->sBasePath . $sRelativePath; | ||
if (file_exists($sPath) && is_readable($sPath)) { | ||
return file_get_contents($sPath); | ||
} | ||
throw new \Exception($sPath . ' could not be read'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.