Guest User

dreamcast vmu php lib

a guest
Jul 8th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 49.98 KB | None | 0 0
  1. <?
  2.  
  3. ////////////////////////////////////////////////////////////////////////
  4. //                                                                    //
  5. // PHP VMlib 2.2                                                      //
  6. // by Speud (speud@free.fr) - http://blueswirl.shorturl.com           //
  7. //                                                                    //
  8. // Last updated on 05/06/2005                                         //
  9. // Original lib can be found at http://bswirl.kitsunet.org/vmlib.inc  //
  10. //                                                                    //
  11. // This is a PHP library that contains all sort of functions to       //
  12. // easily create VMU related files, it can be used to make online     //
  13. // tools for the dreamcast                                            //
  14. //                                                                    //
  15. // This library can be freely used by anybody who needs it as long    //
  16. // as you credit me and link to the original lib                      //
  17. //                                                                    //
  18. // To include this library insert the following line in your code:    //
  19. // include('vmlib.inc');                                              //
  20. //                                                                    //
  21. ////////////////////////////////////////////////////////////////////////
  22.  
  23.  
  24.  
  25.  
  26. ////////////////////////////////////////////////////////////////////////
  27. //                                                                    //
  28. // Table of content:                                                  //
  29. //                                                                    //
  30. // 1) Functions used to get entire files content:                     //
  31. //     getVmInput()                                                   //
  32. //     getVmi()                                                       //
  33. //     getVms()                                                       //
  34. //                                                                    //
  35. // 2) Functions used to get specific parts of files content:          //
  36. //     getData()                                                      //
  37. //     getDataIndex()                                                 //
  38. //     getDataLen()                                                   //
  39. //     vmiDecode()                                                    //
  40. //     vmsDecode()                                                    //
  41. //                                                                    //
  42. // 3) Functions used to create files content:                         //
  43. //     makeVmi()                                                      //
  44. //     makeVms()                                                      //
  45. //                                                                    //
  46. // 4) Functions used to create specific parts of files content:       //
  47. //     calcCRC()                                                      //
  48. //     pngToAsm() (deprecated)                                        //
  49. //     gdToAsm() (deprecated)                                         //
  50. //     gdToIcon()                                                     //
  51. //     lcdToGd()                                                      //
  52. //     doWord()                                                       //
  53. //     dciToVm()                                                      //
  54. //     vmToDci()                                                      //
  55. //                                                                    //
  56. // 5) Functions used to modify files content:                         //
  57. //     insertIcon()                                                   //
  58. //                                                                    //
  59. // 6) Functions used to inspect files content:                        //
  60. //     iconExtract() (deprecated)                                     //
  61. //     alphaBlend()                                                   //
  62. //     iconExtractAlpha()                                             //
  63. //                                                                    //
  64. ////////////////////////////////////////////////////////////////////////
  65.  
  66.  
  67.  
  68.  
  69. define('VMLIB_TYPE_DATA', 0);
  70. define('VMLIB_TYPE_GAME', 1);
  71. define('VMLIB_TYPE_ICON', 2);
  72.  
  73.  
  74.  
  75.  
  76. ////////////////////////////////////////////////////////////////////////
  77. //                                                                    //
  78. // 1) Functions used to get entire files content                      //
  79. //                                                                    //
  80. ////////////////////////////////////////////////////////////////////////
  81.  
  82. ////////////////////////////////////////////////////////////////////////
  83. // Function: getVmInput()                                             //
  84. // Description: separate the value of the vm input returned by the    //
  85. //              form into header and body parts                       //
  86. // Parameters:                                                        //
  87. // $vminput: string containing the value of the vminput field sent by //
  88. //           the form                                                 //
  89. // Return Values: an array containing header and body parts           //
  90. // array[0]: header part                                              //
  91. // array[1]: body part                                                //
  92. ////////////////////////////////////////////////////////////////////////
  93.  
  94. function getVmInput($vminput) {
  95.     // splits $vminput into header and body parts
  96.     // and place them in the array $vmarray
  97.     $vmarray = explode("\r\n\r\n", $vminput, 2);
  98.  
  99.     // returns the array
  100.     return $vmarray;
  101. }
  102.  
  103.  
  104.  
  105.  
  106. ////////////////////////////////////////////////////////////////////////
  107. // Function: getVmi()                                                 //
  108. // Description: create a VMI file string from the header part of the  //
  109. //              vmfile field's value returned by the form and         //
  110. //              several complementar parameters                       //
  111. // Parameters:                                                        //
  112. // $header: string containing the header part of the vm input's value //
  113. // $title: [0-32] chars string containing the description of the file //
  114. //         shown in the download window                               //
  115. // $creator: [0-32] chars string containing the author of the file    //
  116. //           shown in the download window                             //
  117. // $resource: [0-8] chars string containing the vms file name without //
  118. //            the extension                                           //
  119. // Return Values: 108 chars string containing the VMI file            //
  120. ////////////////////////////////////////////////////////////////////////
  121.  
  122. function getVmi($header, $title, $creator, $resource) {
  123.     // creates an array containing the arguments
  124.     // necessary to make a VMI file
  125.     $vmheader = vmiDecode($header);
  126.  
  127.     // put the string containing a new vmi file into $vmi
  128.     $vmi = makeVmi($title, $creator, $vmheader['tm'], $resource,
  129.            $vmheader['filename'], $vmheader['tp'], $vmheader['fs']);
  130.  
  131.     // returns a string containing the VMI file
  132.     return $vmi;
  133. }
  134.  
  135.  
  136.  
  137.  
  138. ////////////////////////////////////////////////////////////////////////
  139. // Function: getVms()                                                 //
  140. // Description: create a VMS file string from the body part of the    //
  141. //              vmfile field's value returned by the form             //
  142. // Parameters:                                                        //
  143. // $body: string containing the header part of the vmfile field's     //
  144. //        value returned by the form                                  //
  145. // Return Values: a string containing the VMS file                    //
  146. ////////////////////////////////////////////////////////////////////////
  147.  
  148. function getVms($body) {
  149.     // encoded characters
  150.     $enc_chars = 'AZOLYNdnETmP6ci3Sze9IyXBhDgfQq7l5batM4rpKJj8CusxRF+k2V0wUGo1vWH/=';
  151.     // decoded characters
  152.     $dec_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  153.  
  154.     $base64 = '';
  155.     // replace encoded chars by decoded chars in $body
  156.     for ($i=0; $i<strlen($body); $i++)
  157.         if (strstr($enc_chars, $body[$i]))
  158.             $base64 .= $dec_chars[strpos($enc_chars, $body[$i])];
  159.  
  160.     // decodes $base64 string using base64
  161.     $vms = base64_decode($base64);
  162.  
  163.     // returns a string containing the decoded VMS file
  164.     return $vms;
  165. }
  166.  
  167.  
  168.  
  169.  
  170. ////////////////////////////////////////////////////////////////////////
  171. //                                                                    //
  172. // 2) Functions used to get specific parts of files content           //
  173. //                                                                    //
  174. ////////////////////////////////////////////////////////////////////////
  175.  
  176. ////////////////////////////////////////////////////////////////////////
  177. // Function: getData()                                                //
  178. // Description: get the actual data that follows the header in VMS    //
  179. //              files                                                 //
  180. // Parameters:                                                        //
  181. // $vms: string containing the VMS file                               //
  182. // Return Values: a string containing the data                        //
  183. ////////////////////////////////////////////////////////////////////////
  184.  
  185. function getData($vms) {
  186.     $data_index = getDataIndex($vms);
  187.     $data_length = getDataLen($vms);
  188.  
  189.     if ($data_index < 640)
  190.         $data_index = 640;
  191.  
  192.     if ($data_index > (128 + 3*512 + 8064))
  193.         $data_index = 128 + 3*512 + 8064;
  194.  
  195.     if (($data_length < (strlen($vms) - $data_index - 512))
  196.  || ($data_length > (strlen($vms) - $data_index)))
  197.         $data_length = strlen($vms) - $data_index;
  198.  
  199.     // substrings $data_length bytes in $vms from $data_index position
  200.     $data = substr($vms, $data_index, $data_length);
  201.  
  202.     // returns the data string
  203.     return $data;
  204. }
  205.  
  206.  
  207.  
  208.  
  209. ////////////////////////////////////////////////////////////////////////
  210. // Function: getDataIndex()                                           //
  211. // Description: get the index of actual file data following           //
  212. //              header, icon(s) and graphic eyecatch in a VMS file    //
  213. // Parameters:                                                        //
  214. // $vms: string containing the VMS file                               //
  215. // Return Values: an int corresponding to the data index              //
  216. ////////////////////////////////////////////////////////////////////////
  217.  
  218. function getDataIndex($vms) {
  219.     // every length possible for the optionnal eyecatch
  220.     $eyec_len = array(0, 8064, 4544, 2048);
  221.  
  222.     $data_index = 128           // icon bitmap(s) index
  223.                 + (ord($vms[64]) * 512) // bitmap(s) length
  224.                 + $eyec_len[ord($vms[68])]; // graphic eyecatch length
  225.  
  226.     // returns the data index
  227.     return $data_index;
  228. }
  229.  
  230.  
  231.  
  232.  
  233. ////////////////////////////////////////////////////////////////////////
  234. // Function: getDataLen()                                             //
  235. // Description: get the size in bytes of actual file data following   //
  236. //              header, icon(s) and graphic eyecatch in a VMS file    //
  237. // Parameters:                                                        //
  238. // $vms: string containing the VMS file                               //
  239. // Return Values: an int corresponding to the data length             //
  240. ////////////////////////////////////////////////////////////////////////
  241.  
  242. function getDataLen($vms) {
  243.     $data_length = ((ord($vms[72]) << 0) & 0xff)
  244.                  | ((ord($vms[73]) << 8) & 0xff00)
  245.                  | ((ord($vms[74]) << 16) & 0xff0000)
  246.                  | ((ord($vms[75]) << 24) & 0xff000000);
  247.  
  248.     // returns the data length
  249.     return $data_length;
  250. }
  251.  
  252.  
  253.  
  254.  
  255. ////////////////////////////////////////////////////////////////////////
  256. // Function: vmiDecode()                                              //
  257. // Description: decode the header part of the value returned by the   //
  258. //              vm input of a form into an array used to create a VMI //
  259. //              file                                                  //
  260. // Parameters:                                                        //
  261. // $header: string containing the header part of the vmfile field's   //
  262. //          value sent by the form                                    //
  263. // Return Values: an array containing variables used to create a VMI  //
  264. //                file                                                //
  265. // VMI array keys:                                                    //
  266. // 'filename': 12 chars string containing the name of the vm file     //
  267. // 'fs': int corresponding to the size of the VMS file in bytes       //
  268. // 'bl': int corresponding to the size of the VMS file in blocks      //
  269. // 'tp': boolean corresponding to the type of file: 0=DATA, 1=GAME    //
  270. // 'fl': boolean always 0 unknown (maybe copy protection)             //
  271. // 'of': boolean always 0 unknown (maybe copy protection)             //
  272. // 'tm': 15 chars string containing the creation date and time of the //
  273. //       VM file using YmdHisw format:                                //
  274. //       Y: full numeric representation of the year, 4 digits         //
  275. //       m: numeric representation of the month, with leading zeros   //
  276. //       d: day of the month, 2 digits with leading zeros             //
  277. //       H: 24-hour format of the hour with leading zeros             //
  278. //       i: minutes with leading zeros                                //
  279. //       s: seconds, with leading zeros                               //
  280. //       w: numeric representation of the day of the week (0=sunday)  //
  281. ////////////////////////////////////////////////////////////////////////
  282.  
  283. function vmiDecode($header) {
  284.     // creates name=value pairs by splitting the header by each '&'
  285.     $pairs = explode('&', $header);
  286.  
  287.     // creates array keys and values by splitting the pairs by each '='
  288.     foreach ($pairs as $pair) {
  289.         list($key, $value) = split('=', $pair, 2);
  290.         $vmiarr[$key] = urldecode($value);
  291.     }
  292.  
  293.     // returns the VMI array
  294.     return $vmiarr;
  295. }
  296.  
  297.  
  298.  
  299.  
  300. ////////////////////////////////////////////////////////////////////////
  301. // Function: vmsDecode()                                              //
  302. // Description: decode the body part of the value returned by the     //
  303. //              vm input of a form into an array containing infos     //
  304. //              about the VMS file                                    //
  305. // Parameters:                                                        //
  306. // $body: string containing the body part of the vmfile field's       //
  307. //        value sent by the form                                      //
  308. // Return Values: an array containing infos about the VMS file        //
  309. // VMS array keys:                                                    //
  310. // 'vmdesc': 16 chars string containing the description shown in the  //
  311. //           VMU file manager                                         //
  312. // 'bootdesc': 32 chars string containing the description shown in    //
  313. //             the DC boot file manager                               //
  314. // 'appname': 16 chars string containing the name of the application  //
  315. //            that created the file                                   //
  316. // 'icons_nb': [1-3] int corresponding to the number of icons         //
  317. //             (>1 for animated icons)                                //
  318. // 'icons_anim_spd': int corresponding to the icon animation speed    //
  319. // 'eyec_type': [0-3] int corresponding to the graphic eyecatch type  //
  320. //              (0=none)                                              //
  321. // 'crc': int corresponding to the CRC checksum                       //
  322. //        (ignored for GAME files)                                    //
  323. // 'data_len': int corresponding to the size in bytes of actual file  //
  324. //             data following header, icon(s) and graphic eyecatch    //
  325. // 'icons_pal': 32 chars string corresponding to the icon's palette   //
  326. //              in asm                                                //
  327. // 'icons_bit': (512*'icons_nb') chars string containing the icon(s)  //
  328. //              bitmap(s) in asm                                      //
  329. // 'eyec': string containing the graphic eyecatch palette and bitmap  //
  330. //         in asm. The number of chars depends on 'eyec_type'         //
  331. //         (type 0: 0, type 1: 8064, type 2: 4544, type 3: 2048)      //
  332. // 'data': 'data_len' chars string containing the actual file data    //
  333. //         following header, icon(s) and graphic eyecatch             //
  334. // Todo: seek to the actual header start for GAME files               //
  335. ////////////////////////////////////////////////////////////////////////
  336.  
  337. function vmsDecode($body) {
  338.     // gets the vms content string
  339.     $vms = getVms($body);
  340.  
  341.     // gets the vm description string
  342.     $vmsarr['vmdesc'] = substr($vms, 0, 16);
  343.  
  344.     // gets the boot description string
  345.     $vmsarr['bootdesc'] = substr($vms, 16, 32);
  346.  
  347.     // gets the application name string
  348.     $vmsarr['appname'] = substr($vms, 32, 16);
  349.  
  350.     // gets the icons number short little endian int
  351.     $vmsarr['icons_nb'] = ord($vms[64]) | ((ord($vms[65]) << 8) & 0xff00);
  352.  
  353.     // gets the icons animation speed short little endian int
  354.     $vmsarr['icons_anim_spd'] = ord($vms[66]) | ((ord($vms[67]) << 8) & 0xff00);
  355.  
  356.     // gets the graphic eyecatch type short little endian int
  357.     $vmsarr['eyec_type'] = ord($vms[68]) | ((ord($vms[69]) << 8) & 0xff00);
  358.  
  359.     // gets the CRC value short little endian int
  360.     $vmsarr['crc'] = ord($vms[70]) | ((ord($vms[71]) << 8) & 0xff00);
  361.  
  362.     // gets the data length long little endian int
  363.     $vmsarr['data_len'] = getDataLen($vms);
  364.  
  365.     // gets the icon palette string
  366.     $vmsarr['icons_pal'] = '';
  367.     for ($i=96; $i<128; $i++) $vmsarr['icons_pal'] .= $vms[$i];
  368.  
  369.     // gets the icon bitmaps string
  370.     $icons_bit_start = 128;
  371.     $icons_bit_end = 128;
  372.     $vmsarr['icons_bit'] = '';
  373.     for ($i=0; $i<$vmsarr['icons_nb']; $i++) {
  374.         $icons_bit_end = $icons_bit_start + 512;
  375.         for ($j=$icons_bit_start; $j<$icons_bit_end; $j++)
  376.             $vmsarr['icons_bit'] .= $vms[$j];
  377.         $icons_bit_start = $icons_bit_end;
  378.     }
  379.  
  380.     // gets the graphic eyecatch string
  381.     $eyec_len = array(0, 8064, 4544, 2048);
  382.     $eyec_end = $icons_bit_end + $eyec_len[$vmsarr[$eyec_type]];
  383.     $vmsarr['eyec'] = '';
  384.     for ($i=$icons_bit_end; $i<$eyec_end; $i++) $vmsarr['eyec'] .= $vms[$i];
  385.  
  386.     // gets the data string
  387.     $length = ($vmsarr['data_len'] > 0) ? $vmsarr['data_len'] : (strlen($vms)-$eyec_end);
  388.     $vmsarr['data'] = substr($vms, $eyec_end, $length);
  389.  
  390.     // returns the VMS array
  391.     return $vmsarr;
  392. }
  393.  
  394.  
  395.  
  396.  
  397. ////////////////////////////////////////////////////////////////////////
  398. //                                                                    //
  399. // 3) Functions used to create files content                          //
  400. //                                                                    //
  401. ////////////////////////////////////////////////////////////////////////
  402.  
  403. ////////////////////////////////////////////////////////////////////////
  404. // Function: makeVmi()                                                //
  405. // Description: creates a string containing the VMI file              //
  406. // Parameters:                                                        //
  407. // $title: [0-32] chars string containing the description of the file //
  408. //         shown in the download window                               //
  409. // $creator: [0-32] chars string containing the author of the file    //
  410. //           shown in the download window                             //
  411. // $resource: [0-8] chars string containing the vms file name without //
  412. //            the extension                                           //
  413. // $tm: null, '' or 15 chars string containing the creation date and  //
  414. //      time of the VM file using YmdHisw format:                     //
  415. //       Y: full numeric representation of the year, 4 digits         //
  416. //       m: numeric representation of the month, with leading zeros   //
  417. //       d: day of the month, 2 digits with leading zeros             //
  418. //       H: 24-hour format of the hour with leading zeros             //
  419. //       i: minutes with leading zeros                                //
  420. //       s: seconds, with leading zeros                               //
  421. //       w: numeric representation of the day of the week (0=sunday)  //
  422. //      (if null or '' the current date and time will be used)        //
  423. // $filename: [0-12] chars string containing the name of the vm file  //
  424. // $tp: boolean corresponding to the type of file: 0=DATA, 1=GAME     //
  425. // $fs: int corresponding to the size of the VMS file in bytes        //
  426. // Return Values: 108 chars string containing the VMI file            //
  427. ////////////////////////////////////////////////////////////////////////
  428.  
  429. function makeVmi($title, $creator, $tm, $resource, $filename, $tp, $fs) {
  430.     $vmi = '';
  431.     // SEGA checksum
  432.     $cs = "SEGA";
  433.     for ($i=0; $i<4; $i++) $vmi .= chr(ord($cs[$i]) & ord($resource[$i]));
  434.  
  435.     // title string, 32 bytes padded with spaces (0x20)
  436.     for ($i=0; $i<32; $i++) $vmi .= (ord($title[$i]) != 0) ? $title[$i] : " ";
  437.  
  438.     // creator string, 32 bytes padded with spaces (0x20)
  439.     $vmi .= "http://blueswirl.shorturl.com   ";
  440.  
  441.     // creation date and time string, 15 bytes:
  442.     // if $tm is null or '' it will use the current date and time
  443.     if (!$tm || $tm=='') $tm = date("YmdHisw", time());
  444.     $y = substr($tm, 0, 4);
  445.     $vmi .= chr($y % 256).chr($y / 256);    // year (0000-9999)
  446.     $vmi .= chr(substr($tm, 4, 2));     // month (01-12)
  447.     $vmi .= chr(substr($tm, 6, 2));     // monthday (01-31)
  448.     $vmi .= chr(substr($tm, 8, 2));     // hours (00-23)
  449.     $vmi .= chr(substr($tm, 10, 2));    // minutes (00-59)
  450.     $vmi .= chr(substr($tm, 12, 2));    // seconds (00-59)
  451.     $vmi .= chr(substr($tm, 14, 1));    // weekday (0-7, 0=sun, 7=sat)
  452.  
  453.     // version, set to 2.2
  454.     $vmi .= chr(20).chr(2);
  455.  
  456.     // number of files, set to 1
  457.     $vmi .= chr(1).chr(0);
  458.  
  459.     // resource name corresponding to the vms file name without extension
  460.     // 8 bytes padded with NUL (0x00)
  461.     for ($i=0; $i<8; $i++) $vmi .= (ord($resource[$i]) != 0) ? $resource[$i] : chr(0);
  462.  
  463.     // vm file name string used by the Dreamcast application
  464.     // 12 bytes padded with space (0x20)
  465.     for ($i=0; $i<12; $i++) $vmi .= (ord($filename[$i]) != 0) ? $filename[$i] : chr(0);
  466.  
  467.     // file type, 0=DATA, 1=GAME
  468.     // the copy protection is disabled
  469.     $vmi .= chr(BinDec($tp."0")).chr(0);
  470.  
  471.     // reserved
  472.     $vmi .= chr(0).chr(0);
  473.  
  474.     // vms file size, long int (little endian)
  475.     $vmi .= doWord($fs, 4, 'le');
  476.  
  477.     // returns the vmi string
  478.     return $vmi;
  479. }
  480.  
  481.  
  482.  
  483.  
  484. ////////////////////////////////////////////////////////////////////////
  485. // Function: makeVms()                                                //
  486. // Description: creates a string containing the VMS file              //
  487. // Parameters:                                                        //
  488. // $vmdesc: [0-16] chars string containing the description shown in   //
  489. //          the VMU file manager                                      //
  490. // $bootdesc: [0-32] chars string containing the description shown in //
  491. //            the DC boot file manager                                //
  492. // $appname': [0-16] chars string containing the name of the          //
  493. //            application that created the file                       //
  494. // $data: string containing the actual file data following header,    //
  495. //        icon(s) and graphic eyecatch                                //
  496. // Return Values: a string containing the VMS file                    //
  497. // Todo: accept $icons, $icons_anim_speed, $eyec_type and             //
  498. //       $eyec arguments                                              //
  499. ////////////////////////////////////////////////////////////////////////
  500.  
  501. function makeVms($vmdesc, $bootdesc, $appname, $data) {
  502.     $vms = '';
  503.     // vm description string, 16 bytes padded with spaces (0x20)
  504.     for ($i=0; $i<16; $i++) $vms .= (ord($vmdesc[$i]) != 0) ? $vmdesc[$i] : " ";
  505.  
  506.     // boot description string, 32 bytes padded with spaces (0x20)
  507.     for ($i=0; $i<32; $i++) $vms .= (ord($bootdesc[$i]) != 0) ? $bootdesc[$i] : " ";
  508.  
  509.     // application name string, 16 bytes padded with spaces (0x20)
  510.     for ($i=0; $i<16; $i++) $vms .= (ord($appname[$i]) != 0) ? $appname[$i] : " ";
  511.  
  512.     // inserts NUL chars where the icon must reside
  513.     for ($i=64; $i<640; $i++) $vms .= chr(0);
  514.  
  515.     // data string
  516.     $vms .= $data;
  517.  
  518.     // inserts data length infos in the vms file content string
  519.     $len = strlen($data);
  520.     $vms[72] = chr((($len & 0x000000ff) >> 0) & 0xff);
  521.     $vms[73] = chr((($len & 0x0000ff00) >> 8) & 0xff);
  522.     $vms[74] = chr((($len & 0x00ff0000) >> 16) & 0xff);
  523.     $vms[75] = chr((($len & 0xff000000) >> 24) & 0xff);
  524.  
  525.     // calculates new CRC
  526.     $crc = calcCRC($vms, strlen($vms));
  527.     $vms[70] = chr((($crc & 0x00ff) >> 0) & 0xff);
  528.     $vms[71] = chr((($crc & 0xff00) >> 8) & 0xff);
  529.  
  530.     // padds with NUL chars (0x00) until the file size
  531.     // in bytes is a multiple of 512
  532.     while ((strlen($vms)%512) != 0) $vms .= chr(0);
  533.  
  534.     // returns the VMS string
  535.     return $vms;
  536. }
  537.  
  538.  
  539.  
  540.  
  541. ////////////////////////////////////////////////////////////////////////
  542. //                                                                    //
  543. // 4) Functions used to create specific parts of the files content    //
  544. //                                                                    //
  545. ////////////////////////////////////////////////////////////////////////
  546.  
  547. ////////////////////////////////////////////////////////////////////////
  548. // Function: calcCRC()                                                //
  549. // Description: calculates the CRC to insert in a VMS file            //
  550. // Parameters:                                                        //
  551. // $vms: string containing the VMS file                               //
  552. // $size: int corresponding to the size in bytes of the VMS file      //
  553. //        without padding                                             //
  554. // Return Values: an int corresponding to the CRC value or -1 if an   //
  555. //                error occurs                                        //
  556. ////////////////////////////////////////////////////////////////////////
  557.  
  558. function calcCRC($vms, $size) {
  559.     if ($size > strlen($vms)) return -1;
  560.  
  561.     $n = 0;
  562.     for ($i=0; $i<$size; $i++) {
  563.  
  564.         // the actual value of the CRC field is ignored
  565.         $vms[$i] = ($i==70 || $i==71) ? chr(0) : $vms[$i];
  566.  
  567.         $n ^= (ord($vms[$i]) << 8);
  568.         for ($c=0; $c<8; $c++)
  569.             $n = ($n & 0x8000) ? ($n << 1) ^ 4129 : ($n << 1);
  570.     }
  571.  
  572.     // returns the CRC as a short little endian int
  573.     return $n & 0xffff;
  574. }
  575.  
  576.  
  577.  
  578.  
  579. ////////////////////////////////////////////////////////////////////////
  580. // Function: pngToAsm()                                               //
  581. // Requirements: image library GD                                     //
  582. // Description: converts a png image to a string containing the       //
  583. //              icon to insert in a VMS file                          //
  584. // Note: only there for backward compability, use gdToIcon() instead  //
  585. // Parameters:                                                        //
  586. // $png: string containing the path of the png image                  //
  587. // Return Values: a string containing the icon                        //
  588. ////////////////////////////////////////////////////////////////////////
  589.  
  590. function pngToAsm($png) {
  591.     // creates a new image object with GD from the png image
  592.     $img = imagecreatefrompng($png);
  593.  
  594.     // creates the icon
  595.     return gdToAsm($img);
  596. }
  597.  
  598.  
  599.  
  600.  
  601. ////////////////////////////////////////////////////////////////////////
  602. // Function: gdToAsm()                                                //
  603. // Requirements: image library GD                                     //
  604. // Description: converts a GD image object to a string containing the //
  605. //              icon to insert in a VMS file                          //
  606. // Note: only there for backward compability, use gdToicon() instead  //
  607. // Parameters:                                                        //
  608. // $img: GD image object                                              //
  609. // Return Values: a string containing the icon                        //
  610. ////////////////////////////////////////////////////////////////////////
  611.  
  612. function gdToAsm($img) {
  613.     // creates the icon
  614.     return gdToIcon($img);
  615. }
  616.  
  617.  
  618.  
  619.  
  620. ////////////////////////////////////////////////////////////////////////
  621. // Function: gdToIcon()                                               //
  622. // Requirements: image library GD                                     //
  623. // Description: creates a string containing the icon to insert        //
  624. //              in a VMS file from a GD image object                  //
  625. // Parameters:                                                        //
  626. // $img: GD image object                                              //
  627. // Return Values: a string containing the icon                        //
  628. ////////////////////////////////////////////////////////////////////////
  629.  
  630. function gdToIcon($img) {
  631.     $icon = "";
  632.  
  633.     for ($i=0; $i<16; $i++)
  634.         $pal[$i] = 0;
  635.  
  636.     // loops the 32 pixels rows of the image
  637.     for ($y=0; $y<32; $y++) {
  638.  
  639.         // loops the 32 pixels cols of the image
  640.         for ($x=0; $x<32; $x++) {
  641.  
  642.             // gets colour index of each pixel
  643.             $idx = imagecolorat($img, $x, $y);
  644.  
  645.             // gets the ARGB values of the colour from the palette
  646.             $col = imagecolorsforindex($img, $idx);
  647.             $a = 15;
  648.             $r = intval($col['red']     / 17);
  649.             $g = intval($col['green']   / 17);
  650.             $b = intval($col['blue']    / 17);
  651.  
  652.             // assigns the ARGB component to the palette entry
  653.             $pal[$idx] = ($a << 12) | ($r << 8) | ($g << 4) | ($b << 0);
  654.  
  655.             // assigns color index to a new nybble
  656.             $nyb[sizeof($nyb)] = $idx;
  657.         }
  658.     }
  659.  
  660.     // destroys the image object created by GD
  661.     //imagedestroy($img);
  662.  
  663.     // creates the icon's palette from the $pal array
  664.     // 1 colour is composed of 2 bytes
  665.     for ($i=0; $i<16; $i++) {
  666.         $pal1 = ($pal[$i] & 0x00ff) >> 0;
  667.         $pal2 = ($pal[$i] & 0xff00) >> 8;
  668.         $icon .= chr($pal1).chr($pal2);
  669.     }
  670.  
  671.     // creates the icon's bitmap from the $nyb array
  672.     // 2 nybbles correspond to 1 byte
  673.     for ($i=0; $i<1024; $i+=2)
  674.         $icon .= chr($nyb[$i+1] | ($nyb[$i] << 4));
  675.  
  676.     // returns a string containing the icon
  677.     // composed of the palette and the bitmap
  678.     return $icon;
  679. }
  680.  
  681.  
  682.  
  683.  
  684. ////////////////////////////////////////////////////////////////////////
  685. // Function: lcdToGD()                                                //
  686. // Requirements: image library GD                                     //
  687. // Description: creates a GD image object from the content of a LCD   //
  688. //              icon                                                  //
  689. // Parameters:                                                        //
  690. // $lcdcontent: string containing a LCD file                          //
  691. // $framenum: [1-255] int corresponding to the frame's rank for       //
  692. //            animated LCD icons                                      //
  693. // Return Values: an image object for GD                              //
  694. ////////////////////////////////////////////////////////////////////////
  695.  
  696. function lcdtoGD($lcdcontent, $framenum) {
  697.     // creates the GD image object
  698.     $im = imagecreate(48, 32);
  699.  
  700.     // define the 2 colors of the image
  701.     $green = imagecolorallocate($im,    143,    207,    143);
  702.     $black = imagecolorallocate($im,    0,  0,  0);
  703.  
  704.     // get the icon frames number
  705.     $frames = ord($lcdcontent[14]) | ((ord($lcdcontent[15]) << 8) & 0xff00);
  706.     if ($frames < 1) $frames = 1;
  707.  
  708.     // if $framenum is out of the anim limits or is not precised
  709.     // the default frame used will be the first frame
  710.     $argsnum = func_num_args();
  711.     if ($framenum<0 || $framenum>($frames-1) || $argsnum<2) $framenum = 0;
  712.  
  713.     // get the bitmap start offset
  714.     $start = 16 + (4 * $frames) + ($framenum * 1536);
  715.  
  716.     // read each pixel of the specific frame
  717.     for ($y=0; $y<32; $y++) {
  718.         for ($x=0; $x<48; $x++) {
  719.             $n = ($y * 48) + $x + $start;
  720.             $pxl = ord($lcdcontent[$n]);
  721.  
  722.             // set the image pixel color
  723.             // depending of the icon pixel value
  724.             $color = ($pxl == 8) ? $black : $green;
  725.             imagesetpixel($im, $x, $y, $color);
  726.         }
  727.     }
  728.  
  729.     // returns the GD image object
  730.     return $im;
  731. }
  732.  
  733.  
  734.  
  735.  
  736. ////////////////////////////////////////////////////////////////////////
  737. // Function: doWord()                                                 //
  738. // Description: creates a string from an int                          //
  739. // Parameters:                                                        //
  740. // $int: int corresponding to the value to convert                    //
  741. // $len: int corresponding to the size in bytes of the string to      //
  742. //       create                                                       //
  743. // $order: 2 chars string corresponding to the order of the string:   //
  744. //         'be': the string is made using big endian order            //
  745. //         'le': the string is made using little endian order         //
  746. //         anything but 'be' will be read as 'le'                     //
  747. // Return Values: a $len chars string                                 //
  748. ////////////////////////////////////////////////////////////////////////
  749.  
  750. function doWord($int, $len, $order) {
  751.     $word = '';
  752.  
  753.     while ($int >= 256) {
  754.         $n = $int % 256;
  755.         $word = chr($n).$word;
  756.         $int = floor($int / 256);
  757.     }
  758.  
  759.     $word = chr($int).$word;
  760.  
  761.     if (strlen($word) > $len) $word = substr($word, strlen($word)-$len);
  762.     while (strlen($word) < $len) $word = chr(0).$word;
  763.  
  764.     if ($order != 'be') $word = strrev($word);
  765.  
  766.     return $word;
  767. }
  768.  
  769.  
  770.  
  771.  
  772. ////////////////////////////////////////////////////////////////////////
  773. // Function: dciToVm()                                                //
  774. // Description: creates an array containing an array used to create   //
  775. //              VMI files and a string containing the VMS files from  //
  776. //              the content of a DCI file (Nexus cards format)        //
  777. // Parameters:                                                        //
  778. // $dci: string containing the DCI file                               //
  779. // Return Values: -1 if an error occurs or an array composed of       //
  780. //                2 elements as follows:                              //
  781. // array[0]: an array used to create VMI files similar to the one     //
  782. //           returned by the function vmiDecode()                     //
  783. // array[1]: a string containing the VMS file                         //
  784. ////////////////////////////////////////////////////////////////////////
  785.  
  786. function dciToVm($dci) {
  787.     $header = substr($dci, 0, 32);
  788.  
  789.     $vmiarray['tp'] = (ord($header[0]) == 204) ? '1' : '0';
  790.     $vmiarray['fl'] = '0';
  791.     $vmiarray['of'] = '0';
  792.     $vmiarray['filename'] = substr($header, 4, 12);
  793.  
  794.     for ($i=16; $i<16+7; $i++) {
  795.         $tm = DecHex(ord($header[$i]));
  796.         $vmiarray['tm'] .= ($tm < 10) ? '0'.$tm : $tm;
  797.     }
  798.     $tm = DecHex(ord($header[23])) + 1;
  799.     if ($tm > 6) $tm = 0;
  800.     $vmiarray['tm'] .= $tm;
  801.  
  802.     $vmiarray['bl'] = ord($header[24]) | ((ord($header[25]) << 8) & 0xff00);
  803.     $vmiarray['fs'] = $vmiarray['bl'] * 512;
  804.  
  805.     $vms = '';
  806.     for ($i=32; $i<strlen($dci); $i+=4)
  807.         for ($j=3; $j>=0; $j--)
  808.             $vms .= $dci[$i+$j];
  809.  
  810.     if ($vmiarray['fs'] != strlen($vms)) return -1;
  811.  
  812.     return array($vmiarray, $vms);
  813. }
  814.  
  815.  
  816.  
  817.  
  818. ////////////////////////////////////////////////////////////////////////
  819. // Function: vmToDci()                                                //
  820. // Description: creates a string containing the DCI file from VMI and //
  821. //              VMS files contents                                    //
  822. // Parameters:                                                        //
  823. // $vmi: string containing the VMI file                               //
  824. // $vms: string containing the VMS file                               //
  825. // Return Values: a string containing the DCI file or -1 if an error  //
  826. //                occurs                                              //
  827. ////////////////////////////////////////////////////////////////////////
  828.  
  829. function vmToDci($vmi, $vms) {
  830.     $dci = '';
  831.  
  832.     //TYPE
  833.     $mode = DecBin(ord($vmi[100]));
  834.     $type = $mode[strlen($mode)-2];
  835.     if (chr($type) == chr(0)) $dci .= chr(51);
  836.     elseif (chr($type) == chr(1)) $dci .= chr(204);
  837.     else $dci .= chr(0);
  838.  
  839.     //COPY
  840.     $dci .= chr(0);
  841.  
  842.     //1ST BLOCK LOCATION
  843.     $dci .= chr(0).chr(0);
  844.  
  845.     //FILENAME
  846.     for ($i=88; $i<100; $i++) $dci .= $vmi[$i];
  847.  
  848.     //CREATION DATE
  849.     //YEAR
  850.     $y = ord($vmi[68]) + (ord($vmi[69]) * 256);
  851.     while (strlen($y) < 4) $y = chr(48).$y;
  852.     $dci .= chr(HexDec(substr($y,0,2))).chr(HexDec(substr($y,2,2)));
  853.  
  854.     //MONTH
  855.     $dci .= chr(HexDec(ord($vmi[70])));
  856.  
  857.     //DAY
  858.     $dci .= chr(HexDec(ord($vmi[71])));
  859.  
  860.     //HOURS
  861.     $dci .= chr(HexDec(ord($vmi[72])));
  862.  
  863.     //MINS
  864.     $dci .= chr(HexDec(ord($vmi[73])));
  865.  
  866.     //SECS
  867.     $dci .= chr(HexDec(ord($vmi[74])));
  868.  
  869.     //WEEKDAY
  870.     $wd = chr(HexDec(ord($vmi[75]))) - 1;
  871.     if ($wd < 0) $wd = 6;
  872.     $dci .= $wd;
  873.  
  874.     //SIZE IN BLOCKS
  875.     $fs = ((ord($vmi[104]) << 0) & 0xff)
  876.         | ((ord($vmi[105]) << 8) & 0xff00)
  877.         | ((ord($vmi[106]) << 16) & 0xff0000)
  878.         | ((ord($vmi[107]) << 24) & 0xff000000);
  879.     if ($fs != strlen($vms)) return -1;
  880.     $dci .= chr($fs / 512).chr(0);
  881.  
  882.     //HEADER OFFSET
  883.     $dci .= chr($type).chr(0);
  884.  
  885.     //UNUSED
  886.     $dci .= chr(0).chr(0).chr(0).chr(0);
  887.  
  888.     //BODY
  889.     if (strlen($dci) != 32) return -1;
  890.     for ($i=0; $i<strlen($vms); $i+=4)
  891.         for ($j=3; $j>=0; $j--)
  892.             $dci .= $vms[$i + $j];
  893.  
  894.     if (strlen($dci) != 32 + $fs) return -1;
  895.  
  896.     return $dci;
  897. }
  898.  
  899.  
  900.  
  901.  
  902. ////////////////////////////////////////////////////////////////////////
  903. //                                                                    //
  904. // 5) Functions used to modify files content                          //
  905. //                                                                    //
  906. ////////////////////////////////////////////////////////////////////////
  907.  
  908. ////////////////////////////////////////////////////////////////////////
  909. // Function: insertIcon()                                             //
  910. // Description: insert a string corresponding to the icon's palette   //
  911. //              and bitmap into a VMS file's header                   //
  912. // Parameters:                                                        //
  913. // $icon: string containing the icon palette and bitmap               //
  914. // $vms: string containing the VMS file                               //
  915. // $type: [0-2] int corresponding to the type of VM file              //
  916. //        type 0: DATA                                                //
  917. //        type 1: GAME                                                //
  918. //        type 2: ICONDATA                                            //
  919. // Return Values: a string containing the modified VMS file           //
  920. // Todo: - inserting instead of replacing in the case of several      //
  921. //       bitmaps                                                      //
  922. //       - add $animspeed parametter                                  //
  923. ////////////////////////////////////////////////////////////////////////
  924.  
  925. function insertIcon($icon, $vms, $type) {
  926.     // separates the assembly icon into palette and bitmap(s)
  927.     $icons_pal = substr($icon, 0, 32);
  928.     $icons_bit = substr($icon, 32);
  929.  
  930.     // calculates icons number
  931.     $icons_nb = strlen($icons_bit) / 512;
  932.  
  933.     // for DATA files the icon starts at offset 0x60
  934.     if ($type == 0) $pal_start=96;
  935.     // for GAME files the icon starts at offset 0x260
  936.     elseif ($type == 1) $pal_start=608;
  937.     // for ICONDATA files the icon starts at offset 0xA0
  938.     else $pal_start=160;
  939.  
  940.     $bit_start = $pal_start + 32;
  941.  
  942.     // sets vms header infos for a DATA file
  943.     if ($type == 0) {
  944.         $vms[64] = chr($icons_nb);  // number of icons
  945.         $vms[65] = chr(0);
  946.  
  947.         $vms[66] = chr(0);      // animation speed
  948.         $vms[67] = chr(0);
  949.     // sets vms header infos for a GAME file
  950.     } elseif ($type == 1) {
  951.         $vms[576] = chr($icons_nb); // number of icons
  952.         $vms[577] = chr(0);
  953.  
  954.         $vms[578] = chr(0);     // animation speed
  955.         $vms[579] = chr(0);
  956.     // sets vms header infos for an ICONDATA file
  957.     } elseif ($type == 2) {
  958.         $vms[20] = chr(160);        // offset of colour icon
  959.     }
  960.  
  961.     // replaces the bytes of the icon palette
  962.     // in the vms file content string
  963.     for ($i=$pal_start; $i<$bit_start; $i++)
  964.         $vms[$i] = $icon[$i-$pal_start];
  965.  
  966.     // replaces the bytes of the icon bitmaps
  967.     // in the vms file content string
  968.     for ($i=0; $i<$icons_nb; $i++) {
  969.         for ($i=$bit_start; $i<$bit_start+512; $i++)
  970.             $vms[$i] = $icon[$i-($bit_start-32)];
  971.  
  972.         $bit_start += 512;
  973.     }
  974.  
  975.     // calculates new CRC
  976.     $crc = calcCRC($vms, getDataLen($vms));
  977.     $vms[70] = chr((($crc & 0x00ff) >> 0) & 0xff);
  978.     $vms[71] = chr((($crc & 0xff00) >> 8) & 0xff);
  979.  
  980.     // returns the string corresponding
  981.     // to the modified vms file content
  982.     return $vms;
  983. }
  984.  
  985.  
  986.  
  987.  
  988. ////////////////////////////////////////////////////////////////////////
  989. //                                                                    //
  990. // 6) Functions used to inspect files content                         //
  991. //                                                                    //
  992. ////////////////////////////////////////////////////////////////////////
  993.  
  994. ////////////////////////////////////////////////////////////////////////
  995. // Function: iconExtract()                                            //
  996. // Description: creates an array used to create an image from the     //
  997. //              content of a VMS file                                 //
  998. // Note: only there for backward compability, use                     //
  999. //       iconExtractAlpha() instead                                   //
  1000. // Parameters:                                                        //
  1001. // $vms: string containing the VMS file                               //
  1002. // $type: [0-2] int corresponding to the type of VM file              //
  1003. //        type 0: DATA                                                //
  1004. //        type 1: GAME                                                //
  1005. //        type 2: ICONDATA                                            //
  1006. // $frame: for DATA or GAME file:                                     //
  1007. //         [0-2] int corresponding to the index of the bitmap to      //
  1008. //         extract, 0 is the first bitmap                             //
  1009. //         for ICONDATA file:                                         //
  1010. //         boolean: 0 for monochrome or 1 for 16 colours (if exists)  //
  1011. // Return Values: an array containing 2 elements as follow:           //
  1012. // array[0]: array containing the 16 ARGB components of the palette,  //
  1013. //           each entry is an array composed of the following keys:   //
  1014. //           'alpha':  alpha component of the palette entry           //
  1015. //           'red':  red component of the palette entry               //
  1016. //           'green':  green component of the palette entry           //
  1017. //           'blue':  blue component of the palette entry             //
  1018. // array[1]: GD image object containing the extracted frame           //
  1019. ////////////////////////////////////////////////////////////////////////
  1020.  
  1021. function iconExtract($vms, $type, $frame) {
  1022.     // returns the array
  1023.     return iconExtractAlpha($vms, $type, $frame, -1);
  1024. }
  1025.  
  1026.  
  1027.  
  1028.  
  1029. ////////////////////////////////////////////////////////////////////////
  1030. // Function: alphaBlend()                                             //
  1031. // Description: creates blended RGB values from bg/fg colors and      //
  1032. //              alpha opacity                                         //
  1033. // Parameters:                                                        //
  1034. // $a: alpha opacity                                                  //
  1035. // $r1: background red component                                      //
  1036. // $g1: background green component                                    //
  1037. // $b1: background blue component                                     //
  1038. // $r1: foreground red component                                      //
  1039. // $g1: foreground green component                                    //
  1040. // $b1: foreground blue component                                     //
  1041. // Return Values: 24-bit RGB triplet                                  //
  1042. ////////////////////////////////////////////////////////////////////////
  1043.  
  1044. function alphaBlend($a, $r1,$g1,$b1, $r2,$g2,$b2) {
  1045.  
  1046.     $a1 = $a / 255;
  1047.     $a2 = 1.0 - $a1;
  1048.  
  1049.     $r = ($r1 * $a1) + ($r2 * $a2);
  1050.     $g = ($g1 * $a1) + ($g2 * $a2);
  1051.     $b = ($b1 * $a1) + ($b2 * $a2);
  1052.  
  1053.     $col = ($r << 16) | ($g << 8) | ($b << 0);
  1054.  
  1055.     return $col;
  1056. }
  1057.  
  1058.  
  1059.  
  1060.  
  1061. ////////////////////////////////////////////////////////////////////////
  1062. // Function: iconExtractAlpha()                                       //
  1063. // Description: creates an array used to create an image from the     //
  1064. //              content of a VMS file with correct support of alpha   //
  1065. //              channel for opacity                                   //
  1066. // Requirements: image library GD                                     //
  1067. // Parameters:                                                        //
  1068. // $vms: string containing the VMS file                               //
  1069. // $type: [0-2] int corresponding to the type of VM file              //
  1070. //        type 0: DATA                                                //
  1071. //        type 1: GAME                                                //
  1072. //        type 2: ICONDATA                                            //
  1073. // $frame: for DATA or GAME file:                                     //
  1074. //         [0-2] int corresponding to the index of the bitmap to      //
  1075. //         extract, 0 is the first bitmap                             //
  1076. //         for ICONDATA file:                                         //
  1077. //         boolean: 0 for monochrome or 1 for 16 colours (if exists)  //
  1078. // $bgcolor: background color of the image as a 24-bit RGB triplet    //
  1079. //           or -1 if you don't want to use alpha channel             //
  1080. // Return Values: -1 if an error occurs or an array containing 2      //
  1081. //                elements as follow:                                 //
  1082. // array[0]: array containing the 16 ARGB components of the palette,  //
  1083. //           each entry is an array composed of the following keys:   //
  1084. //           'alpha':  alpha component of the palette entry           //
  1085. //           'red':  red component of the palette entry               //
  1086. //           'green':  green component of the palette entry           //
  1087. //           'blue':  blue component of the palette entry             //
  1088. // array[1]: GD image object containing the extracted frame           //
  1089. ////////////////////////////////////////////////////////////////////////
  1090.  
  1091. function iconExtractAlpha($vms, $type, $frame, $bgcolor) {
  1092.     // used for backward compatibility
  1093.     if  ($type == 'data') $type = VMLIB_TYPE_DATA;
  1094.     elseif  ($type == 'game') $type = VMLIB_TYPE_GAME;
  1095.     elseif  ($type == 'icon') $type = VMLIB_TYPE_ICON;
  1096.  
  1097.     // the vms file contains a save
  1098.     if ($type == VMLIB_TYPE_DATA) {
  1099.         $pal_pos = 96;
  1100.         $bit_pos = 128 + ($frame * 512);
  1101.     // the vms file contains a mini-game
  1102.     } elseif ($type == VMLIB_TYPE_GAME) {
  1103.         $pal_pos = 512 + 96;
  1104.         $bit_pos = 512 + 128 + ($frame * 512);
  1105.     // the vms file contains a vm icon
  1106.     } elseif ($type == VMLIB_TYPE_ICON) {
  1107.         $pal_pos = ((ord($vms[20]) << 0) & 0xff)
  1108.                  | ((ord($vms[21]) << 8) & 0xff00)
  1109.                  | ((ord($vms[22]) << 16) & 0xff0000)
  1110.                  | ((ord($vms[23]) << 24) & 0xff000000);
  1111.         $bit_pos = $pal_pos + 32;
  1112.     // unknown vms file type
  1113.     } else {
  1114.         return -1;
  1115.     }
  1116.  
  1117.     // creates a new 32x32 image object with GD
  1118.     $im = ImageCreate(32, 32);
  1119.  
  1120.     // icon for ICONDATA file in monochrome mode
  1121.     if ($type==VMLIB_TYPE_ICON && ($frame==0 || $pal_pos==0)) {
  1122.         $start = ((ord($vms[16]) << 0) & 0xff)
  1123.                | ((ord($vms[17]) << 8) & 0xff00)
  1124.                | ((ord($vms[18]) << 16) & 0xff0000)
  1125.                | ((ord($vms[19]) << 24) & 0xff000000);
  1126.  
  1127.         $palette[0] = array('alpha' =>  255,
  1128.                             'red'   =>  255,
  1129.                             'green' =>  255,
  1130.                             'blue'  =>  255);
  1131.         for ($i=1; $i<16; $i++)
  1132.             $palette[$i] = array('alpha'    =>  255,
  1133.                                  'red'  =>  0,
  1134.                                  'green'    =>  0,
  1135.                                  'blue' =>  0);
  1136.  
  1137.         $off = imagecolorallocate($im, $palette[0]['red'],
  1138.                                        $palette[0]['green'],
  1139.                                        $palette[0]['blue']);
  1140.         $on = imagecolorallocate($im, $palette[1]['red'],
  1141.                                       $palette[1]['green'],
  1142.                                       $palette[1]['blue']);
  1143.  
  1144.         for ($y=0; $y<32; $y++) {
  1145.             for ($x=0; $x<32; $x+=8,$start++) {
  1146.                 for ($i=0; $i<8; $i++) {
  1147.                     $bit = ord($vms[$start]) & (0x80 >> $i);
  1148.                     imagesetpixel($im, $x+$i, $y, ($bit ? $on : $off));
  1149.                 }
  1150.             }
  1151.         }
  1152.  
  1153.         return array($palette, $im);
  1154.     }
  1155.  
  1156.  
  1157.     // regular 16 colours icon for DATA and GAME file
  1158.     // or ICONDATA in 16 colours mode
  1159.     // creates the image's palette
  1160.  
  1161.     if ($bgcolor >= 0) {
  1162.         $bg = array(    'red'   => (($bgcolor & 0xff0000) >> 16) & 0xff,
  1163.                 'green' => (($bgcolor & 0x00ff00) >> 8) & 0xff,
  1164.                 'blue'  => (($bgcolor & 0x0000ff) >> 0) & 0xff);
  1165.     }
  1166.  
  1167.     $j = -1;
  1168.     for ($i=0; $i<32; $i+=2) {
  1169.         $j ++;
  1170.  
  1171.         $argb = ord($vms[$pal_pos+$i]) | (ord($vms[$pal_pos+$i+1]) << 8);
  1172.  
  1173.         $a = (($argb & 0xf000) >> 8) & 0xff;
  1174.         $r = (($argb & 0x0f00) >> 4) & 0xff;
  1175.         $g = (($argb & 0x00f0) >> 0) & 0xff;
  1176.         $b = (($argb & 0x000f) << 4) & 0xff;
  1177.  
  1178.         if ($pal_pos == 0) {
  1179.             $alpha  = 0;
  1180.             $red    = 0;
  1181.             $green  = 0;
  1182.             $blue   = 0;
  1183.         } else {
  1184.             // pseudo-alpha rendering
  1185.             if ($bgcolor >= 0) {
  1186.                 $col    = alphaBlend(240-$a, $bg['red'],$bg['green'],$bg['blue'], $r,$g,$b);
  1187.                 $red    = (($col & 0xff0000) >> 16) & 0xff;
  1188.                 $green  = (($col & 0x00ff00) >> 8) & 0xff;
  1189.                 $blue   = (($col & 0x0000ff) >> 0) & 0xff;
  1190.             // normal rendering
  1191.             } else {
  1192.                 $red    = $r;
  1193.                 $green  = $g;
  1194.                 $blue   = $b;
  1195.                 $alpha  = $a;
  1196.             }
  1197.         }
  1198.  
  1199.         ImageColorAllocate($im, $red, $green, $blue);
  1200.         $palette[$j] = array('alpha'    =>  $alpha,
  1201.                              'red'  =>  $red,
  1202.                              'green'    =>  $green,
  1203.                              'blue' =>  $blue);
  1204.     }
  1205.  
  1206.     // colours the image's pixels
  1207.     $j = 0;
  1208.  
  1209.     for ($y=0; $y<32; $y++) {
  1210.         for ($i=0; $i<16; $i++)
  1211.             $buffer[$i] = $vms[$y*16 + $i + $bit_pos];
  1212.  
  1213.         for ($x=0; $x<32; $x+=2) {
  1214.             $nyb = (ord($buffer[$x/2]) & 0xf0) >> 4;    //normal
  1215. //          $nyb = (ord($buffer[$x/2]) & 0x0f) >> 0;    //reversed
  1216.             ImageSetPixel($im, $x, $y, $nyb);
  1217.  
  1218.             $nyb = (ord($buffer[$x/2]) & 0x0f) >> 0;    //normal
  1219. //          $nyb = (ord($buffer[$x/2]) & 0xf0) >> 4;    //reversed
  1220.             ImageSetPixel($im, $x+1, $y, $nyb);
  1221.         }
  1222.     }
  1223.  
  1224.     // returns the array
  1225.     return array($palette, $im);
  1226. }
  1227.  
  1228. ?>
Tags: dreamcast
Add Comment
Please, Sign In to add comment