PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/feidianxlate.php

https://github.com/sobodash/feidianxlate
PHP | 240 lines | 194 code | 26 blank | 20 comment | 64 complexity | d9fc51c07b9a18b97af52721d80443b6 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. #!/usr/bin/php -q
  2. <?php
  3. /*
  4. feidianxlate
  5. A tool to convert FEIDIAN dump strings and tile definitions to mosaic presets.
  6. The tool is intended to help users speed up their migration to the new graphic
  7. utility.
  8. Version: 0.4
  9. Author: Derrick Sobodash <derrick@sobodash.com>
  10. Copyright: (c) 2012 Derrick Sobodash
  11. Web site: https://github.com/sobodash/feidianxlate/
  12. License: BSD License <http://opensource.org/licenses/bsd-license.php>
  13. */
  14. error_reporting (E_WARNING | E_PARSE);
  15. echo ("feidianxlate (mosaic Target) 0.4 (cli)\nCopyright (c) 2012 Derrick Sobodash\n");
  16. if ($argc < 4 && $argv[1] != "-h") { die(print "No input action given. Run with -h for a list of options."); }
  17. elseif ($argv[1] == "-h") { DisplayOptions(); die; }
  18. else {
  19. if($argv[2] == "i"){ $mode = $argv[1]; $command = $argv[3]; $out_file = $argv[4]; }
  20. else { $mode = $argv[1]; $command = $argv[2]; $out_file = $argv[3]; }
  21. }
  22. if ($mode == "-r") {
  23. list($width, $height, $columns, $rows, $start) = split(",", $command);
  24. // Make sure the command string isn't missing anything
  25. if($width==''||$height==''||$columns==''||$rows==''||$start=='') die(print "ERROR: Command string incomplete");
  26. // Check width/height/columns/rows
  27. if (substr($start, 0, 2) == "0x") $start = hexdec(substr($start, 2));
  28. 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");
  29. $out_file = str_replace(".bmp", "", $out_file);
  30. anytile($rows, $columns, $height, $width, $start, $out_file);
  31. }
  32. elseif ($mode == "-cr") {
  33. if($argc > 5) {
  34. if($argv[2] != "i") $tlp_pal = $argv[5];
  35. else $tlp_pal = $argv[6];
  36. }
  37. list($tiledef, $columns, $rows, $start) = split(",", $command);
  38. // Make sure the command string isn't missing anything
  39. if($tiledef==''||$columns==''||$rows==''||$start=='') die(print "ERROR: Command string incomplete");
  40. // Make sure columns/rows/offset are an integer
  41. if (substr($start, 0, 2) == "0x") $start = hexdec(substr($start, 2));
  42. if(!is_numeric($columns)||!is_numeric($rows)||!is_numeric($start)) die(print "ERROR: Columns, rows, or offeset is a non-integer value");
  43. else customtile($rows, $columns, $tiledef, $start, $out_file);
  44. }
  45. else die(print "\nNo input action given. Run with -h for a list of options.\n");
  46. function DisplayOptions() {
  47. echo <<<OPTIONS
  48. Feidian Translator enables the conversion of FEIDIAN dump strings and tile
  49. definitions to mosaic presets. The tool is intended to help users speed up
  50. their migration to the new graphic utility.
  51. Generic Tile Usage:
  52. feidianxlate.php -r [STRING] [OUTPUT]
  53. Custom Tile Usage:
  54. feidianxlate.php -cr [STRING] [OUTPUT]
  55. Where:
  56. STRING = FEIDIAN-compatible dump string
  57. OUTPUT = File to contain the mosaic preset
  58. OPTIONS;
  59. }
  60. function makecolor($color) {
  61. return("0x" . str_pad(dechex($color[0] * 0x10000 + $color[1] * 0x100 + $color[2]), 6, "0", STR_PAD_LEFT));
  62. }
  63. function feidianmap() {
  64. return(array('A','B','C','D','E','F','G','H','I','J','K','L','M',
  65. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  66. 'a','b','c','d','e','f','g','h','i','j','k','l','m',
  67. 'n','o','p','q','r','s','t','u','v','w','x','y','z',
  68. '2','3','4','5','6','7','8','9','!','?','@','*',
  69. chr(0xa0),chr(0xa1),chr(0xa2),chr(0xa3),chr(0xa4),chr(0xa5),chr(0xa6),chr(0xa7),
  70. chr(0xa8),chr(0xa9),chr(0xaa),chr(0xab),chr(0xac),chr(0xad),chr(0xae),chr(0xaf),
  71. chr(0xb0),chr(0xb1),chr(0xb2),chr(0xb3),chr(0xb4),chr(0xb5),chr(0xb6),chr(0xb7),
  72. chr(0xb8),chr(0xb9),chr(0xba),chr(0xbb),chr(0xbc),chr(0xbd),chr(0xbe),chr(0xbf),
  73. chr(0xc0),chr(0xc1),chr(0xc2),chr(0xc3),chr(0xc4),chr(0xc5),chr(0xc6),chr(0xc7),
  74. chr(0xc8),chr(0xc9),chr(0xca),chr(0xcb),chr(0xcc),chr(0xcd),chr(0xce),chr(0xcf),
  75. chr(0xd0),chr(0xd1),chr(0xd2),chr(0xd3),chr(0xd4),chr(0xd5),chr(0xd6),chr(0xd7),
  76. chr(0xd8),chr(0xd9),chr(0xda),chr(0xdb),chr(0xdc),chr(0xdd),chr(0xde),chr(0xdf),
  77. chr(0xe0),chr(0xe1),chr(0xe2),chr(0xe3),chr(0xe4),chr(0xe5),chr(0xe6),chr(0xe7),
  78. chr(0xe8),chr(0xe9),chr(0xea),chr(0xeb),chr(0xec),chr(0xed),chr(0xee),chr(0xef)));
  79. }
  80. function writepreset($outfile, $tile_width, $tile_height, $columns, $rows, $seekstart, $depth, $palette, $blocks, $order) {
  81. $fo = fopen("$outfile", "wb");
  82. fputs($fo, "//${tile_width}x${tile_height} (${depth}bpp)\n//Generated by FEIDIAN Translator\nTested for compatibility with mosaic 0.4\n//\n");
  83. fputs($fo, "offset: 8 * 0x" . dechex($seekstart) . "\n");
  84. fputs($fo, "width: $columns\n");
  85. fputs($fo, "height: $rows\n\n");
  86. fputs($fo, "depth: $depth\n");
  87. if($order == "linear")
  88. fputs($fo, "order: 0\n");
  89. else
  90. fputs($fo, "order: 1\n");
  91. for($i=0; $i<count($palette); $i++) {
  92. fputs($fo, "palette: ${palette[$i]}\n");
  93. }
  94. $pat_size = 0;
  95. if($order == "planar")
  96. for($i=0; $i<count($blocks); $i++) {
  97. fputs($fo, "block: " . implode(", ", $blocks[$i]) . "\n");
  98. $pat_size += count($blocks[$i]);
  99. }
  100. else {
  101. fputs($fo, "block: "); $temp = "";
  102. for($i=0; $i<count($blocks[0]); $i++) {
  103. for($z=0; $z<count($blocks); $z++)
  104. $temp .= $blocks[$z][$i] . ", ";
  105. $pat_size += count($blocks);
  106. }
  107. fputs($fo, substr($temp, 0, strlen($temp)-2) . "\n");
  108. }
  109. fputs($fo, "blockWidth: $tile_width\n");
  110. fputs($fo, "blockHeight: " . (count($blocks[0])/$tile_width) . "\n");
  111. fputs($fo, "blockStride: $pat_size\n\n");
  112. fputs($fo, "tileWidth: 1\n");
  113. fputs($fo, "tileHeight: " . ($tile_height/(count($blocks[0])/$tile_width)) . "\n");
  114. fclose($fo);
  115. }
  116. function interleave($block) {
  117. for ($i=0; $i<count($block); $i+=2) {
  118. $temp1 = $block[$i];
  119. $temp2 = $block[$i+1];
  120. $block[$i+1] = $temp1;
  121. $block[$i] = $temp2;
  122. }
  123. return($block);
  124. }
  125. function sanitize($string) {
  126. $string = preg_replace("/( *)/", "", $string);
  127. $string = preg_replace("/(\\r*)/", "", $string);
  128. return(preg_replace("/(\\n*)/", "", $string));
  129. }
  130. function customtile($rows, $columns, $tiledef, $seekstart, $out_file){
  131. include("tiles/$tiledef.php");
  132. print "Generating block pattern...\n";
  133. $base64 = feidianmap();
  134. $planes = array();
  135. $temp_pattern = array();
  136. $palette = array();
  137. if(COLOR_DEPTH=="2") {
  138. $planes = array(sanitize($plane1));
  139. $temp_pattern[0] = array(); for($i=0; $i<strlen($planes[0]); $i++) $temp_pattern[0][] = substr($planes[0], $i, 1);
  140. $palette[0] = "0x000000, 0xfcfcfc";
  141. $depth = 1;
  142. }
  143. if(COLOR_DEPTH=="4") {
  144. $planes = array(sanitize($plane1), sanitize($plane2));
  145. $temp_pattern[0] = array(); for($i=0; $i<strlen($planes[0]); $i++) $temp_pattern[0][] = substr($planes[0], $i, 1);
  146. $temp_pattern[1] = array(); for($i=0; $i<strlen($planes[1]); $i++) $temp_pattern[1][] = substr($planes[1], $i, 1);
  147. $palette[0] = makecolor($color0) . ", " . makecolor($color1) . ", " . makecolor($color2) . ", " . makecolor($color3);
  148. $depth = 2;
  149. }
  150. if(COLOR_DEPTH=="8") {
  151. $planes = array(sanitize($plane1), sanitize($plane2), sanitize($plane3));
  152. $temp_pattern[0] = array(); for($i=0; $i<strlen($planes[0]); $i++) $temp_pattern[0][] = substr($planes[0], $i, 1);
  153. $temp_pattern[1] = array(); for($i=0; $i<strlen($planes[1]); $i++) $temp_pattern[1][] = substr($planes[1], $i, 1);
  154. $temp_pattern[2] = array(); for($i=0; $i<strlen($planes[2]); $i++) $temp_pattern[2][] = substr($planes[2], $i, 1);
  155. $palette[0] = makecolor($color0) . ", " . makecolor($color1) . ", " . makecolor($color2) . ", " . makecolor($color3);
  156. $palette[1] = makecolor($color4) . ", " . makecolor($color5) . ", " . makecolor($color6) . ", " . makecolor($color7);
  157. $depth = 3;
  158. }
  159. if(COLOR_DEPTH=="16") {
  160. $planes = array(sanitize($plane1), sanitize($plane2), sanitize($plane3), sanitize($plane4));
  161. $temp_pattern[0] = array(); for($i=0; $i<strlen($planes[0]); $i++) $temp_pattern[0][] = substr($planes[0], $i, 1);
  162. $temp_pattern[1] = array(); for($i=0; $i<strlen($planes[1]); $i++) $temp_pattern[1][] = substr($planes[1], $i, 1);
  163. $temp_pattern[2] = array(); for($i=0; $i<strlen($planes[2]); $i++) $temp_pattern[2][] = substr($planes[2], $i, 1);
  164. $temp_pattern[3] = array(); for($i=0; $i<strlen($planes[3]); $i++) $temp_pattern[3][] = substr($planes[3], $i, 1);
  165. $palette[0] = makecolor($color0) . ", " . makecolor($color1) . ", " . makecolor($color2) . ", " . makecolor($color3);
  166. $palette[1] = makecolor($color4) . ", " . makecolor($color5) . ", " . makecolor($color6) . ", " . makecolor($color7);
  167. $palette[2] = makecolor($color8) . ", " . makecolor($color9) . ", " . makecolor($colorA) . ", " . makecolor($colorB);
  168. $palette[3] = makecolor($colorC) . ", " . makecolor($colorD) . ", " . makecolor($colorE) . ", " . makecolor($colorF);
  169. $depth = 4;
  170. }
  171. $chars = array(); for($i=0; $i<$pat_size; $i++) $chars[] = $base64[$i];
  172. $binstring = array(); for($i=0; $i<$pat_size*8; $i++) $binstring[] = "$i";
  173. foreach($chars as $letter) {
  174. if($order=="linear") {
  175. 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])) {
  176. for($i = 0; $i < count($temp_pattern); $i++)
  177. if(in_array($letter, $temp_pattern[$i])) {
  178. $temp_pattern[$i][array_search($letter, $temp_pattern[$i])] = $binstring[0];
  179. unset($binstring[0]);
  180. $binstring = array_values($binstring);
  181. }
  182. }
  183. }
  184. else {
  185. for($i=0; $i<count($temp_pattern); $i++) {
  186. while(in_array($letter, $temp_pattern[$i])) {
  187. $temp_pattern[$i][array_search($letter, $temp_pattern[$i])] = $binstring[0];
  188. unset($binstring[0]);
  189. $binstring = array_values($binstring);
  190. }
  191. }
  192. }
  193. }
  194. if($interleave>0)
  195. for($i=0; $i<count($temp_pattern); $i++)
  196. $temp_pattern[$i] = interleave($temp_pattern[$i]);
  197. writepreset($out_file, $tile_width, $tile_height, $columns, $rows, $seekstart, $depth, $palette, $temp_pattern, $order);
  198. print "Wrote: $out_file";
  199. }
  200. function anytile($rows, $columns, $tile_height, $tile_width, $seekstart, $out_file) {
  201. $prefix = $tile_width . "x" . $tile_height;
  202. print "Generating block pattern...\n";
  203. $block = array();
  204. for ($i=0; $i<$tile_width; $i++) {
  205. $block[] = $i;
  206. }
  207. writepreset($out_file, $tile_width, $tile_height, $columns, $rows, $seekstart, 1, array("0x000000, 0xfcfcfc"), array($block), "planar");
  208. print "Wrote: $out_file";
  209. }
  210. ?>