/feidianxlate.php
PHP | 240 lines | 194 code | 26 blank | 20 comment | 64 complexity | d9fc51c07b9a18b97af52721d80443b6 MD5 | raw file
Possible License(s): BSD-2-Clause
- #!/usr/bin/php -q
- <?php
- /*
- feidianxlate
- A tool to convert FEIDIAN dump strings and tile definitions to mosaic presets.
- The tool is intended to help users speed up their migration to the new graphic
- utility.
- Version: 0.4
- Author: Derrick Sobodash <derrick@sobodash.com>
- Copyright: (c) 2012 Derrick Sobodash
- Web site: https://github.com/sobodash/feidianxlate/
- License: BSD License <http://opensource.org/licenses/bsd-license.php>
- */
- error_reporting (E_WARNING | E_PARSE);
- echo ("feidianxlate (mosaic Target) 0.4 (cli)\nCopyright (c) 2012 Derrick Sobodash\n");
- if ($argc < 4 && $argv[1] != "-h") { die(print "No input action given. Run with -h for a list of options."); }
- elseif ($argv[1] == "-h") { DisplayOptions(); die; }
- else {
- if($argv[2] == "i"){ $mode = $argv[1]; $command = $argv[3]; $out_file = $argv[4]; }
- else { $mode = $argv[1]; $command = $argv[2]; $out_file = $argv[3]; }
- }
- if ($mode == "-r") {
- list($width, $height, $columns, $rows, $start) = split(",", $command);
- // Make sure the command string isn't missing anything
- if($width==''||$height==''||$columns==''||$rows==''||$start=='') die(print "ERROR: Command string incomplete");
- // Check width/height/columns/rows
- if (substr($start, 0, 2) == "0x") $start = hexdec(substr($start, 2));
- if(!is_numeric($width)||!is_numeric($height)||!is_numeric($columns)||!is_numeric($rows)||!is_numeric($start)) die(print "ERROR: Width, height, columns or rows is a non-integer value");
- $out_file = str_replace(".bmp", "", $out_file);
- anytile($rows, $columns, $height, $width, $start, $out_file);
- }
- elseif ($mode == "-cr") {
- if($argc > 5) {
- if($argv[2] != "i") $tlp_pal = $argv[5];
- else $tlp_pal = $argv[6];
- }
- list($tiledef, $columns, $rows, $start) = split(",", $command);
- // Make sure the command string isn't missing anything
- if($tiledef==''||$columns==''||$rows==''||$start=='') die(print "ERROR: Command string incomplete");
- // Make sure columns/rows/offset are an integer
- if (substr($start, 0, 2) == "0x") $start = hexdec(substr($start, 2));
- if(!is_numeric($columns)||!is_numeric($rows)||!is_numeric($start)) die(print "ERROR: Columns, rows, or offeset is a non-integer value");
- else customtile($rows, $columns, $tiledef, $start, $out_file);
- }
- else die(print "\nNo input action given. Run with -h for a list of options.\n");
- function DisplayOptions() {
- echo <<<OPTIONS
- Feidian Translator enables the conversion of FEIDIAN dump strings and tile
- definitions to mosaic presets. The tool is intended to help users speed up
- their migration to the new graphic utility.
- Generic Tile Usage:
- feidianxlate.php -r [STRING] [OUTPUT]
- Custom Tile Usage:
- feidianxlate.php -cr [STRING] [OUTPUT]
- Where:
- STRING = FEIDIAN-compatible dump string
- OUTPUT = File to contain the mosaic preset
- OPTIONS;
- }
- function makecolor($color) {
- return("0x" . str_pad(dechex($color[0] * 0x10000 + $color[1] * 0x100 + $color[2]), 6, "0", STR_PAD_LEFT));
- }
- function feidianmap() {
- return(array('A','B','C','D','E','F','G','H','I','J','K','L','M',
- 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
- 'a','b','c','d','e','f','g','h','i','j','k','l','m',
- 'n','o','p','q','r','s','t','u','v','w','x','y','z',
- '2','3','4','5','6','7','8','9','!','?','@','*',
- chr(0xa0),chr(0xa1),chr(0xa2),chr(0xa3),chr(0xa4),chr(0xa5),chr(0xa6),chr(0xa7),
- chr(0xa8),chr(0xa9),chr(0xaa),chr(0xab),chr(0xac),chr(0xad),chr(0xae),chr(0xaf),
- chr(0xb0),chr(0xb1),chr(0xb2),chr(0xb3),chr(0xb4),chr(0xb5),chr(0xb6),chr(0xb7),
- chr(0xb8),chr(0xb9),chr(0xba),chr(0xbb),chr(0xbc),chr(0xbd),chr(0xbe),chr(0xbf),
- chr(0xc0),chr(0xc1),chr(0xc2),chr(0xc3),chr(0xc4),chr(0xc5),chr(0xc6),chr(0xc7),
- chr(0xc8),chr(0xc9),chr(0xca),chr(0xcb),chr(0xcc),chr(0xcd),chr(0xce),chr(0xcf),
- chr(0xd0),chr(0xd1),chr(0xd2),chr(0xd3),chr(0xd4),chr(0xd5),chr(0xd6),chr(0xd7),
- chr(0xd8),chr(0xd9),chr(0xda),chr(0xdb),chr(0xdc),chr(0xdd),chr(0xde),chr(0xdf),
- chr(0xe0),chr(0xe1),chr(0xe2),chr(0xe3),chr(0xe4),chr(0xe5),chr(0xe6),chr(0xe7),
- chr(0xe8),chr(0xe9),chr(0xea),chr(0xeb),chr(0xec),chr(0xed),chr(0xee),chr(0xef)));
- }
- function writepreset($outfile, $tile_width, $tile_height, $columns, $rows, $seekstart, $depth, $palette, $blocks, $order) {
- $fo = fopen("$outfile", "wb");
- fputs($fo, "//${tile_width}x${tile_height} (${depth}bpp)\n//Generated by FEIDIAN Translator\nTested for compatibility with mosaic 0.4\n//\n");
- fputs($fo, "offset: 8 * 0x" . dechex($seekstart) . "\n");
- fputs($fo, "width: $columns\n");
- fputs($fo, "height: $rows\n\n");
- fputs($fo, "depth: $depth\n");
- if($order == "linear")
- fputs($fo, "order: 0\n");
- else
- fputs($fo, "order: 1\n");
- for($i=0; $i<count($palette); $i++) {
- fputs($fo, "palette: ${palette[$i]}\n");
- }
- $pat_size = 0;
- if($order == "planar")
- for($i=0; $i<count($blocks); $i++) {
- fputs($fo, "block: " . implode(", ", $blocks[$i]) . "\n");
- $pat_size += count($blocks[$i]);
- }
- else {
- fputs($fo, "block: "); $temp = "";
- for($i=0; $i<count($blocks[0]); $i++) {
- for($z=0; $z<count($blocks); $z++)
- $temp .= $blocks[$z][$i] . ", ";
- $pat_size += count($blocks);
- }
- fputs($fo, substr($temp, 0, strlen($temp)-2) . "\n");
- }
- fputs($fo, "blockWidth: $tile_width\n");
- fputs($fo, "blockHeight: " . (count($blocks[0])/$tile_width) . "\n");
- fputs($fo, "blockStride: $pat_size\n\n");
- fputs($fo, "tileWidth: 1\n");
- fputs($fo, "tileHeight: " . ($tile_height/(count($blocks[0])/$tile_width)) . "\n");
- fclose($fo);
- }
- function interleave($block) {
- for ($i=0; $i<count($block); $i+=2) {
- $temp1 = $block[$i];
- $temp2 = $block[$i+1];
- $block[$i+1] = $temp1;
- $block[$i] = $temp2;
- }
- return($block);
- }
- function sanitize($string) {
- $string = preg_replace("/( *)/", "", $string);
- $string = preg_replace("/(\\r*)/", "", $string);
- return(preg_replace("/(\\n*)/", "", $string));
- }
- function customtile($rows, $columns, $tiledef, $seekstart, $out_file){
- include("tiles/$tiledef.php");
- print "Generating block pattern...\n";
-
- $base64 = feidianmap();
- $planes = array();
- $temp_pattern = array();
- $palette = array();
- if(COLOR_DEPTH=="2") {
- $planes = array(sanitize($plane1));
- $temp_pattern[0] = array(); for($i=0; $i<strlen($planes[0]); $i++) $temp_pattern[0][] = substr($planes[0], $i, 1);
- $palette[0] = "0x000000, 0xfcfcfc";
- $depth = 1;
- }
- if(COLOR_DEPTH=="4") {
- $planes = array(sanitize($plane1), sanitize($plane2));
- $temp_pattern[0] = array(); for($i=0; $i<strlen($planes[0]); $i++) $temp_pattern[0][] = substr($planes[0], $i, 1);
- $temp_pattern[1] = array(); for($i=0; $i<strlen($planes[1]); $i++) $temp_pattern[1][] = substr($planes[1], $i, 1);
- $palette[0] = makecolor($color0) . ", " . makecolor($color1) . ", " . makecolor($color2) . ", " . makecolor($color3);
- $depth = 2;
- }
- if(COLOR_DEPTH=="8") {
- $planes = array(sanitize($plane1), sanitize($plane2), sanitize($plane3));
- $temp_pattern[0] = array(); for($i=0; $i<strlen($planes[0]); $i++) $temp_pattern[0][] = substr($planes[0], $i, 1);
- $temp_pattern[1] = array(); for($i=0; $i<strlen($planes[1]); $i++) $temp_pattern[1][] = substr($planes[1], $i, 1);
- $temp_pattern[2] = array(); for($i=0; $i<strlen($planes[2]); $i++) $temp_pattern[2][] = substr($planes[2], $i, 1);
- $palette[0] = makecolor($color0) . ", " . makecolor($color1) . ", " . makecolor($color2) . ", " . makecolor($color3);
- $palette[1] = makecolor($color4) . ", " . makecolor($color5) . ", " . makecolor($color6) . ", " . makecolor($color7);
- $depth = 3;
- }
- if(COLOR_DEPTH=="16") {
- $planes = array(sanitize($plane1), sanitize($plane2), sanitize($plane3), sanitize($plane4));
- $temp_pattern[0] = array(); for($i=0; $i<strlen($planes[0]); $i++) $temp_pattern[0][] = substr($planes[0], $i, 1);
- $temp_pattern[1] = array(); for($i=0; $i<strlen($planes[1]); $i++) $temp_pattern[1][] = substr($planes[1], $i, 1);
- $temp_pattern[2] = array(); for($i=0; $i<strlen($planes[2]); $i++) $temp_pattern[2][] = substr($planes[2], $i, 1);
- $temp_pattern[3] = array(); for($i=0; $i<strlen($planes[3]); $i++) $temp_pattern[3][] = substr($planes[3], $i, 1);
- $palette[0] = makecolor($color0) . ", " . makecolor($color1) . ", " . makecolor($color2) . ", " . makecolor($color3);
- $palette[1] = makecolor($color4) . ", " . makecolor($color5) . ", " . makecolor($color6) . ", " . makecolor($color7);
- $palette[2] = makecolor($color8) . ", " . makecolor($color9) . ", " . makecolor($colorA) . ", " . makecolor($colorB);
- $palette[3] = makecolor($colorC) . ", " . makecolor($colorD) . ", " . makecolor($colorE) . ", " . makecolor($colorF);
- $depth = 4;
- }
-
- $chars = array(); for($i=0; $i<$pat_size; $i++) $chars[] = $base64[$i];
- $binstring = array(); for($i=0; $i<$pat_size*8; $i++) $binstring[] = "$i";
-
- foreach($chars as $letter) {
- if($order=="linear") {
- while(in_array($letter, $temp_pattern[0]) || in_array($letter, $temp_pattern[1]) || in_array($letter, $temp_pattern[2]) || in_array($letter, $temp_pattern[3])) {
- for($i = 0; $i < count($temp_pattern); $i++)
- if(in_array($letter, $temp_pattern[$i])) {
- $temp_pattern[$i][array_search($letter, $temp_pattern[$i])] = $binstring[0];
- unset($binstring[0]);
- $binstring = array_values($binstring);
- }
- }
- }
- else {
- for($i=0; $i<count($temp_pattern); $i++) {
- while(in_array($letter, $temp_pattern[$i])) {
- $temp_pattern[$i][array_search($letter, $temp_pattern[$i])] = $binstring[0];
- unset($binstring[0]);
- $binstring = array_values($binstring);
- }
- }
- }
- }
-
- if($interleave>0)
- for($i=0; $i<count($temp_pattern); $i++)
- $temp_pattern[$i] = interleave($temp_pattern[$i]);
-
- writepreset($out_file, $tile_width, $tile_height, $columns, $rows, $seekstart, $depth, $palette, $temp_pattern, $order);
-
- print "Wrote: $out_file";
- }
- function anytile($rows, $columns, $tile_height, $tile_width, $seekstart, $out_file) {
- $prefix = $tile_width . "x" . $tile_height;
-
- print "Generating block pattern...\n";
-
- $block = array();
-
- for ($i=0; $i<$tile_width; $i++) {
- $block[] = $i;
- }
-
- writepreset($out_file, $tile_width, $tile_height, $columns, $rows, $seekstart, 1, array("0x000000, 0xfcfcfc"), array($block), "planar");
-
- print "Wrote: $out_file";
- }
- ?>