don't click here

High Seas Havoc Map16 & Map256 Viewer

Discussion in 'Technical Discussion' started by Techokami, Mar 28, 2009.

  1. Techokami

    Techokami

    For use only on NTSC Genesis systems Researcher
    1,375
    86
    28
    HoleNet!
    Sonic Worlds Next
    So after learning quite a bit with Desert Demolition, I took a stab at another obscure title that piqued my interests: High Seas Havoc (AKA Cap'n Havoc)<br />Interestingly, it uses the very same Map16 format as Sonic 2/3K! Maybe Sonic 1 as well but there's basically nothing on the Retro wiki about metatile formats in Sonic 1...<br /><br />Again, it's a PHP-based tool, but this one is MUCH better organized. Functions aren't all over the place and code is slimmed down a bit. It takes two parameters from the URL: file, the filename and extension of the savestate; and savepath, the directory you want to save the Map256 images to. By default, the savestate it loads is named hsh.gs0 and the save path is the same directory as the script.<br /><br /><div class='codetop'>CODE</div><div class='codemain' style='height:480px;white-space:pre;overflow:auto'><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);">&lt;?php</span><span style="color: rgb(255, 128, 0);">//High Seas Havoc Level Data Ripper//By Techokami - Released under the BSD license//High Seas Havoc is a much easier game to rip from//than Desert Demolition! It uses the exact same Map16//format as Sonic 2/3K. Maybe Sonic 1 as well, but that//is poorly documented and should be seriously fixed.//Map256 is very very straightforward as collision data//is tied to Map16 entries.//Lots of garbage data may be present as I dunno the extent//of the Map256 data yet. However you may notice that the//Map256 fits in rather well with the level layout in how//it is ordered...//MAIN CODE//First, do we want to load a specific file, or go with the default (hsh.gs0)</span><span style="color: rgb(0, 0, 187);">$file </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(221, 0, 0);">"hsh.gs0"</span><span style="color: rgb(0, 119, 0);">;if(isset(</span><span style="color: rgb(0, 0, 187);">$_GET</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'file'</span><span style="color: rgb(0, 119, 0);">])){ </span><span style="color: rgb(0, 0, 187);">$file </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$_GET</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'file'</span><span style="color: rgb(0, 119, 0);">];}</span><span style="color: rgb(255, 128, 0);">//Next, do we want to save it somewhere specific, or do you want to put it //all in the current working directory? PROTIP: Make a new directory.</span><span style="color: rgb(0, 0, 187);">$savepath </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(221, 0, 0);">""</span><span style="color: rgb(0, 119, 0);">;if(isset(</span><span style="color: rgb(0, 0, 187);">$_GET</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'savepath'</span><span style="color: rgb(0, 119, 0);">])){ </span><span style="color: rgb(0, 0, 187);">$savepath </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$_GET</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'savepath'</span><span style="color: rgb(0, 119, 0);">].</span><span style="color: rgb(221, 0, 0);">"\\"</span><span style="color: rgb(0, 119, 0);">;}</span><span style="color: rgb(255, 128, 0);">//Make the images to hold the tile data</span><span style="color: rgb(0, 0, 187);">$tilesone </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagecreate</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">256</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">384</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$tilestwo </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagecreate</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">256</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">384</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$tilesthree </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagecreate</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">256</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">384</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$tilesfour </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagecreate</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">256</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">384</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(255, 128, 0);">//Open the savestate, seek to the location of the pallete</span><span style="color: rgb(0, 0, 187);">$ram </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">fopen</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$file</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(221, 0, 0);">"r"</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">fseek</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0x11B78</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(255, 128, 0);">//Make the palletes</span><span style="color: rgb(0, 0, 187);">$palone </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">makePallete</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesone</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$paltwo </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">makePallete</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilestwo</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$palthree </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">makePallete</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesthree</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$palfour </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">makePallete</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesfour</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(255, 128, 0);">//Build the 8x8 tile images</span><span style="color: rgb(0, 0, 187);">$tilesaddr </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0x12478</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">$tilesone </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">make8x8Tiles</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesone</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$palone</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesaddr</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$tilestwo </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">make8x8Tiles</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilestwo</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$paltwo</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesaddr</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$tilesthree </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">make8x8Tiles</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesthree</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$palthree</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesaddr</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$tilesfour </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">make8x8Tiles</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesfour</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$palfour</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesaddr</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(255, 128, 0);">//Okay we got the tiles! Let's start building the Map16.</span><span style="color: rgb(0, 0, 187);">$map16 </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagecreatetruecolor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">512</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">512</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$mapaddr </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0x6C78</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">fseek</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$mapaddr</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">;for(</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">++){ </span><span style="color: rgb(0, 0, 187);">$j </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$j</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$j </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$j</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$y</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">2</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$y</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$x</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">2</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$x</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$tile </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">getWord</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(255, 128, 0);">// PCCY XAAA AAAA AAAA //0110 0000 0000 0000 - 0x6000 - determine pallete //0 = Pallete 0, 8192 = Pallete 1, 16384 = Pallete 2, 24576 = Pallete 3 </span><span style="color: rgb(0, 0, 187);">$tilepallete </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$tile </span><span style="color: rgb(0, 119, 0);">& </span><span style="color: rgb(0, 0, 187);">0x6000</span><span style="color: rgb(0, 119, 0);">; switch(</span><span style="color: rgb(0, 0, 187);">$tilepallete</span><span style="color: rgb(0, 119, 0);">) { case </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">: </span><span style="color: rgb(0, 0, 187);">$tilepallete </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; break; case </span><span style="color: rgb(0, 0, 187);">8192</span><span style="color: rgb(0, 119, 0);">: </span><span style="color: rgb(0, 0, 187);">$tilepallete </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">; break; case </span><span style="color: rgb(0, 0, 187);">16384</span><span style="color: rgb(0, 119, 0);">: </span><span style="color: rgb(0, 0, 187);">$tilepallete </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">2</span><span style="color: rgb(0, 119, 0);">; break; case </span><span style="color: rgb(0, 0, 187);">24576</span><span style="color: rgb(0, 119, 0);">: </span><span style="color: rgb(0, 0, 187);">$tilepallete </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">3</span><span style="color: rgb(0, 119, 0);">; break; default: </span><span style="color: rgb(0, 0, 187);">$tilepallete </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; break; } </span><span style="color: rgb(255, 128, 0);">//0001 0000 0000 0000 - 0x1000 - determine Y flip //0 = No, 4096 = Yes </span><span style="color: rgb(0, 0, 187);">$tileyflip </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$tile </span><span style="color: rgb(0, 119, 0);">& </span><span style="color: rgb(0, 0, 187);">0x1000</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(255, 128, 0);">//0000 1000 0000 0000 - 0x0800 - determine X flip //0 = No, 2048 = Yes </span><span style="color: rgb(0, 0, 187);">$tilexflip </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$tile </span><span style="color: rgb(0, 119, 0);">& </span><span style="color: rgb(0, 0, 187);">0x800</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(255, 128, 0);">//0000 0111 1111 1111 - 0x07FF - get tile ID </span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$tile </span><span style="color: rgb(0, 119, 0);">& </span><span style="color: rgb(0, 0, 187);">0x07FF</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$tempimg </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagecreatetruecolor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">,</span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">); switch(</span><span style="color: rgb(0, 0, 187);">$tilepallete</span><span style="color: rgb(0, 119, 0);">) { case </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">: </span><span style="color: rgb(0, 0, 187);">imagecopyresampled</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tempimg</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesone</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">% </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">/ </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">)) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">); break; case </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">: </span><span style="color: rgb(0, 0, 187);">imagecopyresampled</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tempimg</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilestwo</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">% </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">/ </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">)) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">); break; case </span><span style="color: rgb(0, 0, 187);">2</span><span style="color: rgb(0, 119, 0);">: </span><span style="color: rgb(0, 0, 187);">imagecopyresampled</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tempimg</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesthree</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">% </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">/ </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">)) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">); break; case </span><span style="color: rgb(0, 0, 187);">3</span><span style="color: rgb(0, 119, 0);">: </span><span style="color: rgb(0, 0, 187);">imagecopyresampled</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tempimg</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesfour</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">% </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">/ </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">)) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">); break; default: </span><span style="color: rgb(0, 0, 187);">imagecopyresampled</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tempimg</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tilesone</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">% </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tileID </span><span style="color: rgb(0, 119, 0);">/ </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">)) * </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">); break; } </span><span style="color: rgb(0, 0, 187);">$flipflag </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$tileyflip </span><span style="color: rgb(0, 119, 0);">+ </span><span style="color: rgb(0, 0, 187);">$tilexflip</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$tempimg </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">image_flip</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tempimg</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$flipflag</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">imagecopyresampled</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$map16</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tempimg</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">) + (</span><span style="color: rgb(0, 0, 187);">$j </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">), (</span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">) + (</span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">imagedestroy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tempimg</span><span style="color: rgb(0, 119, 0);">); } } }}</span><span style="color: rgb(255, 128, 0);">//And now, the Map256. Friggin' gigantic, I know.</span><span style="color: rgb(0, 0, 187);">$map256 </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagecreatetruecolor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">256</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">256</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$map256addr </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0x2478</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">fseek</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$map256addr</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">;for(</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">++){ </span><span style="color: rgb(0, 0, 187);">$j </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$j</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$j </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$j</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$y</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$y</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$x</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$x</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$tile16 </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">getByte</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$ram</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">imagecopy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$map256</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$map16</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">), (</span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">), (</span><span style="color: rgb(0, 0, 187);">$tile16 </span><span style="color: rgb(0, 119, 0);">% </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">) * </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$tile16 </span><span style="color: rgb(0, 119, 0);">/ </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">)) * </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">); } } </span><span style="color: rgb(255, 128, 0);">//Save the Map256 tile! </span><span style="color: rgb(0, 0, 187);">imagepng</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$map256</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$savepath</span><span style="color: rgb(0, 119, 0);">.((</span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">) + </span><span style="color: rgb(0, 0, 187);">$j</span><span style="color: rgb(0, 119, 0);">).</span><span style="color: rgb(221, 0, 0);">".png"</span><span style="color: rgb(0, 119, 0);">); }}</span><span style="color: rgb(255, 128, 0);">//Output the image!</span><span style="color: rgb(0, 0, 187);">header</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'Content-Type: image/png'</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">imagepng</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$map16</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(255, 128, 0);">//FUNCTIONS//Read a single byte from the file</span><span style="color: rgb(0, 119, 0);">function </span><span style="color: rgb(0, 0, 187);">getByte</span><span style="color: rgb(0, 119, 0);">( &</span><span style="color: rgb(0, 0, 187);">$source </span><span style="color: rgb(0, 119, 0);">){ return </span><span style="color: rgb(0, 0, 187);">ord</span><span style="color: rgb(0, 119, 0);">( </span><span style="color: rgb(0, 0, 187);">fread</span><span style="color: rgb(0, 119, 0);">( </span><span style="color: rgb(0, 0, 187);">$source</span><span style="color: rgb(0, 119, 0);">,</span><span style="color: rgb(0, 0, 187);">1 </span><span style="color: rgb(0, 119, 0);">) );}</span><span style="color: rgb(255, 128, 0);">//Read a 16-bit WORD from the file</span><span style="color: rgb(0, 119, 0);">function </span><span style="color: rgb(0, 0, 187);">getWord</span><span style="color: rgb(0, 119, 0);">( &</span><span style="color: rgb(0, 0, 187);">$source </span><span style="color: rgb(0, 119, 0);">){ return (</span><span style="color: rgb(0, 0, 187);">getByte</span><span style="color: rgb(0, 119, 0);">( </span><span style="color: rgb(0, 0, 187);">$source </span><span style="color: rgb(0, 119, 0);">) * </span><span style="color: rgb(0, 0, 187);">0x100</span><span style="color: rgb(0, 119, 0);">) + </span><span style="color: rgb(0, 0, 187);">getByte</span><span style="color: rgb(0, 119, 0);">( </span><span style="color: rgb(0, 0, 187);">$source </span><span style="color: rgb(0, 119, 0);">);}</span><span style="color: rgb(255, 128, 0);">//Generate a 16-color pallete</span><span style="color: rgb(0, 119, 0);">function </span><span style="color: rgb(0, 0, 187);">makePallete</span><span style="color: rgb(0, 119, 0);">( </span><span style="color: rgb(0, 0, 187);">$source</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$gd </span><span style="color: rgb(0, 119, 0);">){ </span><span style="color: rgb(0, 0, 187);">$result </span><span style="color: rgb(0, 119, 0);">= array(); </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$color </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">getByte</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$source</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">$color2 </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">getByte</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$source</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(255, 128, 0);">// Genesis pallete entries are words - 0B GR </span><span style="color: rgb(0, 0, 187);">$red </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">16 </span><span style="color: rgb(0, 119, 0);">* (</span><span style="color: rgb(0, 0, 187);">$color2 </span><span style="color: rgb(0, 119, 0);">% </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">$green </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">16 </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$color2 </span><span style="color: rgb(0, 119, 0);">/ </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">$blue </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">16 </span><span style="color: rgb(0, 119, 0);">* (</span><span style="color: rgb(0, 0, 187);">$color </span><span style="color: rgb(0, 119, 0);">% </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">); if (</span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">== </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">) </span><span style="color: rgb(0, 0, 187);">$result</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">] = </span><span style="color: rgb(0, 0, 187);">imagecolorallocate</span><span style="color: rgb(0, 119, 0);">( </span><span style="color: rgb(0, 0, 187);">$gd</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">255</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">255 </span><span style="color: rgb(0, 119, 0);">); else </span><span style="color: rgb(0, 0, 187);">$result</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">] = </span><span style="color: rgb(0, 0, 187);">imagecolorallocate</span><span style="color: rgb(0, 119, 0);">( </span><span style="color: rgb(0, 0, 187);">$gd</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$red</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$green</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$blue </span><span style="color: rgb(0, 119, 0);">); } return </span><span style="color: rgb(0, 0, 187);">$result</span><span style="color: rgb(0, 119, 0);">;}</span><span style="color: rgb(255, 128, 0);">//Build a set of 8x8 tiles</span><span style="color: rgb(0, 119, 0);">function </span><span style="color: rgb(0, 0, 187);">make8x8Tiles</span><span style="color: rgb(0, 119, 0);">( </span><span style="color: rgb(0, 0, 187);">$source</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$gd</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$pal</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tileAddr </span><span style="color: rgb(0, 119, 0);">) { </span><span style="color: rgb(0, 0, 187);">fseek</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$source</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$tileAddr</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">48</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$j </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$j</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$j </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">32</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$j</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$y</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$y</span><span style="color: rgb(0, 119, 0);">++) { </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; for(</span><span style="color: rgb(0, 0, 187);">$x</span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">< </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">; </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">+ </span><span style="color: rgb(0, 0, 187);">2</span><span style="color: rgb(0, 119, 0);">) { </span><span style="color: rgb(0, 0, 187);">$byte </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">getByte</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$source</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(255, 128, 0);">// Split a byte into nybbles </span><span style="color: rgb(0, 0, 187);">$leftpixel </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$byte </span><span style="color: rgb(0, 119, 0);">/ </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">$rightpixel </span><span style="color: rgb(0, 119, 0);">= (</span><span style="color: rgb(0, 0, 187);">$byte </span><span style="color: rgb(0, 119, 0);">% </span><span style="color: rgb(0, 0, 187);">16</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(255, 128, 0);">//Paint the two pixels </span><span style="color: rgb(0, 0, 187);">imagesetpixel</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$gd</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">+ (</span><span style="color: rgb(0, 0, 187);">$j </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">+ (</span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">$pal</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(0, 0, 187);">$leftpixel</span><span style="color: rgb(0, 119, 0);">]); </span><span style="color: rgb(0, 0, 187);">imagesetpixel</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$gd</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$x </span><span style="color: rgb(0, 119, 0);">+ </span><span style="color: rgb(0, 0, 187);">1 </span><span style="color: rgb(0, 119, 0);">+ (</span><span style="color: rgb(0, 0, 187);">$j </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">$y </span><span style="color: rgb(0, 119, 0);">+ (</span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">* </span><span style="color: rgb(0, 0, 187);">8</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">$pal</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(0, 0, 187);">$rightpixel</span><span style="color: rgb(0, 119, 0);">]); } } } } return </span><span style="color: rgb(0, 0, 187);">$gd</span><span style="color: rgb(0, 119, 0);">;}</span><span style="color: rgb(255, 128, 0);">//Borrowed from something open source Lightning linked me to.</span><span style="color: rgb(0, 119, 0);">function </span><span style="color: rgb(0, 0, 187);">image_flip</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$img</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$type</span><span style="color: rgb(0, 119, 0);">){ </span><span style="color: rgb(0, 0, 187);">$width </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagesx</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$img</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">$height </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagesy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$img</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">$dest </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagecreatetruecolor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$width</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$height</span><span style="color: rgb(0, 119, 0);">); switch(</span><span style="color: rgb(0, 0, 187);">$type</span><span style="color: rgb(0, 119, 0);">){ case </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">: return </span><span style="color: rgb(0, 0, 187);">$img</span><span style="color: rgb(0, 119, 0);">; break; case </span><span style="color: rgb(0, 0, 187);">4096</span><span style="color: rgb(0, 119, 0);">: for(</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">=</span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);"><</span><span style="color: rgb(0, 0, 187);">$height</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">++){ </span><span style="color: rgb(0, 0, 187);">imagecopy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$dest</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$img</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$height </span><span style="color: rgb(0, 119, 0);">- </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">- </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$width</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">); } break; case </span><span style="color: rgb(0, 0, 187);">2048</span><span style="color: rgb(0, 119, 0);">: for(</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">=</span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);"><</span><span style="color: rgb(0, 0, 187);">$width</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">++){ </span><span style="color: rgb(0, 0, 187);">imagecopy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$dest</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$img</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$width </span><span style="color: rgb(0, 119, 0);">- </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">- </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$height</span><span style="color: rgb(0, 119, 0);">); } break; case </span><span style="color: rgb(0, 0, 187);">6144</span><span style="color: rgb(0, 119, 0);">: for(</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">=</span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);"><</span><span style="color: rgb(0, 0, 187);">$width</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">++){ </span><span style="color: rgb(0, 0, 187);">imagecopy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$dest</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$img</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$width </span><span style="color: rgb(0, 119, 0);">- </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">- </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$height</span><span style="color: rgb(0, 119, 0);">); } </span><span style="color: rgb(0, 0, 187);">$buffer </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">imagecreatetruecolor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$width</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">); for(</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">=</span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">;</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);"><(</span><span style="color: rgb(0, 0, 187);">$height</span><span style="color: rgb(0, 119, 0);">/</span><span style="color: rgb(0, 0, 187);">2</span><span style="color: rgb(0, 119, 0);">);</span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">++){ </span><span style="color: rgb(0, 0, 187);">imagecopy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$buffer</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$dest</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$height </span><span style="color: rgb(0, 119, 0);">- </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">-</span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">$width</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">imagecopy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$dest</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$dest</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, (</span><span style="color: rgb(0, 0, 187);">$height </span><span style="color: rgb(0, 119, 0);">- </span><span style="color: rgb(0, 0, 187);">$I </span><span style="color: rgb(0, 119, 0);">- </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">), </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$width</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">); </span><span style="color: rgb(0, 0, 187);">imagecopy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$dest</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$buffer</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$I</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">$width</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">); } </span><span style="color: rgb(0, 0, 187);">imagedestroy</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$buffer</span><span style="color: rgb(0, 119, 0);">); break; default: return </span><span style="color: rgb(0, 0, 187);">$img</span><span style="color: rgb(0, 119, 0);">; break; } return </span><span style="color: rgb(0, 0, 187);">$dest</span><span style="color: rgb(0, 119, 0);">;}</span><span style="color: rgb(0, 0, 187);">?></span></span></div>