Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- ////////////////////////////////////////////////////////////////////////
- // //
- // PHP VMlib 2.2 //
- // by Speud (speud@free.fr) - http://blueswirl.shorturl.com //
- // //
- // Last updated on 05/06/2005 //
- // Original lib can be found at http://bswirl.kitsunet.org/vmlib.inc //
- // //
- // This is a PHP library that contains all sort of functions to //
- // easily create VMU related files, it can be used to make online //
- // tools for the dreamcast //
- // //
- // This library can be freely used by anybody who needs it as long //
- // as you credit me and link to the original lib //
- // //
- // To include this library insert the following line in your code: //
- // include('vmlib.inc'); //
- // //
- ////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- // //
- // Table of content: //
- // //
- // 1) Functions used to get entire files content: //
- // getVmInput() //
- // getVmi() //
- // getVms() //
- // //
- // 2) Functions used to get specific parts of files content: //
- // getData() //
- // getDataIndex() //
- // getDataLen() //
- // vmiDecode() //
- // vmsDecode() //
- // //
- // 3) Functions used to create files content: //
- // makeVmi() //
- // makeVms() //
- // //
- // 4) Functions used to create specific parts of files content: //
- // calcCRC() //
- // pngToAsm() (deprecated) //
- // gdToAsm() (deprecated) //
- // gdToIcon() //
- // lcdToGd() //
- // doWord() //
- // dciToVm() //
- // vmToDci() //
- // //
- // 5) Functions used to modify files content: //
- // insertIcon() //
- // //
- // 6) Functions used to inspect files content: //
- // iconExtract() (deprecated) //
- // alphaBlend() //
- // iconExtractAlpha() //
- // //
- ////////////////////////////////////////////////////////////////////////
- define('VMLIB_TYPE_DATA', 0);
- define('VMLIB_TYPE_GAME', 1);
- define('VMLIB_TYPE_ICON', 2);
- ////////////////////////////////////////////////////////////////////////
- // //
- // 1) Functions used to get entire files content //
- // //
- ////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- // Function: getVmInput() //
- // Description: separate the value of the vm input returned by the //
- // form into header and body parts //
- // Parameters: //
- // $vminput: string containing the value of the vminput field sent by //
- // the form //
- // Return Values: an array containing header and body parts //
- // array[0]: header part //
- // array[1]: body part //
- ////////////////////////////////////////////////////////////////////////
- function getVmInput($vminput) {
- // splits $vminput into header and body parts
- // and place them in the array $vmarray
- $vmarray = explode("\r\n\r\n", $vminput, 2);
- // returns the array
- return $vmarray;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: getVmi() //
- // Description: create a VMI file string from the header part of the //
- // vmfile field's value returned by the form and //
- // several complementar parameters //
- // Parameters: //
- // $header: string containing the header part of the vm input's value //
- // $title: [0-32] chars string containing the description of the file //
- // shown in the download window //
- // $creator: [0-32] chars string containing the author of the file //
- // shown in the download window //
- // $resource: [0-8] chars string containing the vms file name without //
- // the extension //
- // Return Values: 108 chars string containing the VMI file //
- ////////////////////////////////////////////////////////////////////////
- function getVmi($header, $title, $creator, $resource) {
- // creates an array containing the arguments
- // necessary to make a VMI file
- $vmheader = vmiDecode($header);
- // put the string containing a new vmi file into $vmi
- $vmi = makeVmi($title, $creator, $vmheader['tm'], $resource,
- $vmheader['filename'], $vmheader['tp'], $vmheader['fs']);
- // returns a string containing the VMI file
- return $vmi;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: getVms() //
- // Description: create a VMS file string from the body part of the //
- // vmfile field's value returned by the form //
- // Parameters: //
- // $body: string containing the header part of the vmfile field's //
- // value returned by the form //
- // Return Values: a string containing the VMS file //
- ////////////////////////////////////////////////////////////////////////
- function getVms($body) {
- // encoded characters
- $enc_chars = 'AZOLYNdnETmP6ci3Sze9IyXBhDgfQq7l5batM4rpKJj8CusxRF+k2V0wUGo1vWH/=';
- // decoded characters
- $dec_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
- $base64 = '';
- // replace encoded chars by decoded chars in $body
- for ($i=0; $i<strlen($body); $i++)
- if (strstr($enc_chars, $body[$i]))
- $base64 .= $dec_chars[strpos($enc_chars, $body[$i])];
- // decodes $base64 string using base64
- $vms = base64_decode($base64);
- // returns a string containing the decoded VMS file
- return $vms;
- }
- ////////////////////////////////////////////////////////////////////////
- // //
- // 2) Functions used to get specific parts of files content //
- // //
- ////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- // Function: getData() //
- // Description: get the actual data that follows the header in VMS //
- // files //
- // Parameters: //
- // $vms: string containing the VMS file //
- // Return Values: a string containing the data //
- ////////////////////////////////////////////////////////////////////////
- function getData($vms) {
- $data_index = getDataIndex($vms);
- $data_length = getDataLen($vms);
- if ($data_index < 640)
- $data_index = 640;
- if ($data_index > (128 + 3*512 + 8064))
- $data_index = 128 + 3*512 + 8064;
- if (($data_length < (strlen($vms) - $data_index - 512))
- || ($data_length > (strlen($vms) - $data_index)))
- $data_length = strlen($vms) - $data_index;
- // substrings $data_length bytes in $vms from $data_index position
- $data = substr($vms, $data_index, $data_length);
- // returns the data string
- return $data;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: getDataIndex() //
- // Description: get the index of actual file data following //
- // header, icon(s) and graphic eyecatch in a VMS file //
- // Parameters: //
- // $vms: string containing the VMS file //
- // Return Values: an int corresponding to the data index //
- ////////////////////////////////////////////////////////////////////////
- function getDataIndex($vms) {
- // every length possible for the optionnal eyecatch
- $eyec_len = array(0, 8064, 4544, 2048);
- $data_index = 128 // icon bitmap(s) index
- + (ord($vms[64]) * 512) // bitmap(s) length
- + $eyec_len[ord($vms[68])]; // graphic eyecatch length
- // returns the data index
- return $data_index;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: getDataLen() //
- // Description: get the size in bytes of actual file data following //
- // header, icon(s) and graphic eyecatch in a VMS file //
- // Parameters: //
- // $vms: string containing the VMS file //
- // Return Values: an int corresponding to the data length //
- ////////////////////////////////////////////////////////////////////////
- function getDataLen($vms) {
- $data_length = ((ord($vms[72]) << 0) & 0xff)
- | ((ord($vms[73]) << 8) & 0xff00)
- | ((ord($vms[74]) << 16) & 0xff0000)
- | ((ord($vms[75]) << 24) & 0xff000000);
- // returns the data length
- return $data_length;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: vmiDecode() //
- // Description: decode the header part of the value returned by the //
- // vm input of a form into an array used to create a VMI //
- // file //
- // Parameters: //
- // $header: string containing the header part of the vmfile field's //
- // value sent by the form //
- // Return Values: an array containing variables used to create a VMI //
- // file //
- // VMI array keys: //
- // 'filename': 12 chars string containing the name of the vm file //
- // 'fs': int corresponding to the size of the VMS file in bytes //
- // 'bl': int corresponding to the size of the VMS file in blocks //
- // 'tp': boolean corresponding to the type of file: 0=DATA, 1=GAME //
- // 'fl': boolean always 0 unknown (maybe copy protection) //
- // 'of': boolean always 0 unknown (maybe copy protection) //
- // 'tm': 15 chars string containing the creation date and time of the //
- // VM file using YmdHisw format: //
- // Y: full numeric representation of the year, 4 digits //
- // m: numeric representation of the month, with leading zeros //
- // d: day of the month, 2 digits with leading zeros //
- // H: 24-hour format of the hour with leading zeros //
- // i: minutes with leading zeros //
- // s: seconds, with leading zeros //
- // w: numeric representation of the day of the week (0=sunday) //
- ////////////////////////////////////////////////////////////////////////
- function vmiDecode($header) {
- // creates name=value pairs by splitting the header by each '&'
- $pairs = explode('&', $header);
- // creates array keys and values by splitting the pairs by each '='
- foreach ($pairs as $pair) {
- list($key, $value) = split('=', $pair, 2);
- $vmiarr[$key] = urldecode($value);
- }
- // returns the VMI array
- return $vmiarr;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: vmsDecode() //
- // Description: decode the body part of the value returned by the //
- // vm input of a form into an array containing infos //
- // about the VMS file //
- // Parameters: //
- // $body: string containing the body part of the vmfile field's //
- // value sent by the form //
- // Return Values: an array containing infos about the VMS file //
- // VMS array keys: //
- // 'vmdesc': 16 chars string containing the description shown in the //
- // VMU file manager //
- // 'bootdesc': 32 chars string containing the description shown in //
- // the DC boot file manager //
- // 'appname': 16 chars string containing the name of the application //
- // that created the file //
- // 'icons_nb': [1-3] int corresponding to the number of icons //
- // (>1 for animated icons) //
- // 'icons_anim_spd': int corresponding to the icon animation speed //
- // 'eyec_type': [0-3] int corresponding to the graphic eyecatch type //
- // (0=none) //
- // 'crc': int corresponding to the CRC checksum //
- // (ignored for GAME files) //
- // 'data_len': int corresponding to the size in bytes of actual file //
- // data following header, icon(s) and graphic eyecatch //
- // 'icons_pal': 32 chars string corresponding to the icon's palette //
- // in asm //
- // 'icons_bit': (512*'icons_nb') chars string containing the icon(s) //
- // bitmap(s) in asm //
- // 'eyec': string containing the graphic eyecatch palette and bitmap //
- // in asm. The number of chars depends on 'eyec_type' //
- // (type 0: 0, type 1: 8064, type 2: 4544, type 3: 2048) //
- // 'data': 'data_len' chars string containing the actual file data //
- // following header, icon(s) and graphic eyecatch //
- // Todo: seek to the actual header start for GAME files //
- ////////////////////////////////////////////////////////////////////////
- function vmsDecode($body) {
- // gets the vms content string
- $vms = getVms($body);
- // gets the vm description string
- $vmsarr['vmdesc'] = substr($vms, 0, 16);
- // gets the boot description string
- $vmsarr['bootdesc'] = substr($vms, 16, 32);
- // gets the application name string
- $vmsarr['appname'] = substr($vms, 32, 16);
- // gets the icons number short little endian int
- $vmsarr['icons_nb'] = ord($vms[64]) | ((ord($vms[65]) << 8) & 0xff00);
- // gets the icons animation speed short little endian int
- $vmsarr['icons_anim_spd'] = ord($vms[66]) | ((ord($vms[67]) << 8) & 0xff00);
- // gets the graphic eyecatch type short little endian int
- $vmsarr['eyec_type'] = ord($vms[68]) | ((ord($vms[69]) << 8) & 0xff00);
- // gets the CRC value short little endian int
- $vmsarr['crc'] = ord($vms[70]) | ((ord($vms[71]) << 8) & 0xff00);
- // gets the data length long little endian int
- $vmsarr['data_len'] = getDataLen($vms);
- // gets the icon palette string
- $vmsarr['icons_pal'] = '';
- for ($i=96; $i<128; $i++) $vmsarr['icons_pal'] .= $vms[$i];
- // gets the icon bitmaps string
- $icons_bit_start = 128;
- $icons_bit_end = 128;
- $vmsarr['icons_bit'] = '';
- for ($i=0; $i<$vmsarr['icons_nb']; $i++) {
- $icons_bit_end = $icons_bit_start + 512;
- for ($j=$icons_bit_start; $j<$icons_bit_end; $j++)
- $vmsarr['icons_bit'] .= $vms[$j];
- $icons_bit_start = $icons_bit_end;
- }
- // gets the graphic eyecatch string
- $eyec_len = array(0, 8064, 4544, 2048);
- $eyec_end = $icons_bit_end + $eyec_len[$vmsarr[$eyec_type]];
- $vmsarr['eyec'] = '';
- for ($i=$icons_bit_end; $i<$eyec_end; $i++) $vmsarr['eyec'] .= $vms[$i];
- // gets the data string
- $length = ($vmsarr['data_len'] > 0) ? $vmsarr['data_len'] : (strlen($vms)-$eyec_end);
- $vmsarr['data'] = substr($vms, $eyec_end, $length);
- // returns the VMS array
- return $vmsarr;
- }
- ////////////////////////////////////////////////////////////////////////
- // //
- // 3) Functions used to create files content //
- // //
- ////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- // Function: makeVmi() //
- // Description: creates a string containing the VMI file //
- // Parameters: //
- // $title: [0-32] chars string containing the description of the file //
- // shown in the download window //
- // $creator: [0-32] chars string containing the author of the file //
- // shown in the download window //
- // $resource: [0-8] chars string containing the vms file name without //
- // the extension //
- // $tm: null, '' or 15 chars string containing the creation date and //
- // time of the VM file using YmdHisw format: //
- // Y: full numeric representation of the year, 4 digits //
- // m: numeric representation of the month, with leading zeros //
- // d: day of the month, 2 digits with leading zeros //
- // H: 24-hour format of the hour with leading zeros //
- // i: minutes with leading zeros //
- // s: seconds, with leading zeros //
- // w: numeric representation of the day of the week (0=sunday) //
- // (if null or '' the current date and time will be used) //
- // $filename: [0-12] chars string containing the name of the vm file //
- // $tp: boolean corresponding to the type of file: 0=DATA, 1=GAME //
- // $fs: int corresponding to the size of the VMS file in bytes //
- // Return Values: 108 chars string containing the VMI file //
- ////////////////////////////////////////////////////////////////////////
- function makeVmi($title, $creator, $tm, $resource, $filename, $tp, $fs) {
- $vmi = '';
- // SEGA checksum
- $cs = "SEGA";
- for ($i=0; $i<4; $i++) $vmi .= chr(ord($cs[$i]) & ord($resource[$i]));
- // title string, 32 bytes padded with spaces (0x20)
- for ($i=0; $i<32; $i++) $vmi .= (ord($title[$i]) != 0) ? $title[$i] : " ";
- // creator string, 32 bytes padded with spaces (0x20)
- $vmi .= "http://blueswirl.shorturl.com ";
- // creation date and time string, 15 bytes:
- // if $tm is null or '' it will use the current date and time
- if (!$tm || $tm=='') $tm = date("YmdHisw", time());
- $y = substr($tm, 0, 4);
- $vmi .= chr($y % 256).chr($y / 256); // year (0000-9999)
- $vmi .= chr(substr($tm, 4, 2)); // month (01-12)
- $vmi .= chr(substr($tm, 6, 2)); // monthday (01-31)
- $vmi .= chr(substr($tm, 8, 2)); // hours (00-23)
- $vmi .= chr(substr($tm, 10, 2)); // minutes (00-59)
- $vmi .= chr(substr($tm, 12, 2)); // seconds (00-59)
- $vmi .= chr(substr($tm, 14, 1)); // weekday (0-7, 0=sun, 7=sat)
- // version, set to 2.2
- $vmi .= chr(20).chr(2);
- // number of files, set to 1
- $vmi .= chr(1).chr(0);
- // resource name corresponding to the vms file name without extension
- // 8 bytes padded with NUL (0x00)
- for ($i=0; $i<8; $i++) $vmi .= (ord($resource[$i]) != 0) ? $resource[$i] : chr(0);
- // vm file name string used by the Dreamcast application
- // 12 bytes padded with space (0x20)
- for ($i=0; $i<12; $i++) $vmi .= (ord($filename[$i]) != 0) ? $filename[$i] : chr(0);
- // file type, 0=DATA, 1=GAME
- // the copy protection is disabled
- $vmi .= chr(BinDec($tp."0")).chr(0);
- // reserved
- $vmi .= chr(0).chr(0);
- // vms file size, long int (little endian)
- $vmi .= doWord($fs, 4, 'le');
- // returns the vmi string
- return $vmi;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: makeVms() //
- // Description: creates a string containing the VMS file //
- // Parameters: //
- // $vmdesc: [0-16] chars string containing the description shown in //
- // the VMU file manager //
- // $bootdesc: [0-32] chars string containing the description shown in //
- // the DC boot file manager //
- // $appname': [0-16] chars string containing the name of the //
- // application that created the file //
- // $data: string containing the actual file data following header, //
- // icon(s) and graphic eyecatch //
- // Return Values: a string containing the VMS file //
- // Todo: accept $icons, $icons_anim_speed, $eyec_type and //
- // $eyec arguments //
- ////////////////////////////////////////////////////////////////////////
- function makeVms($vmdesc, $bootdesc, $appname, $data) {
- $vms = '';
- // vm description string, 16 bytes padded with spaces (0x20)
- for ($i=0; $i<16; $i++) $vms .= (ord($vmdesc[$i]) != 0) ? $vmdesc[$i] : " ";
- // boot description string, 32 bytes padded with spaces (0x20)
- for ($i=0; $i<32; $i++) $vms .= (ord($bootdesc[$i]) != 0) ? $bootdesc[$i] : " ";
- // application name string, 16 bytes padded with spaces (0x20)
- for ($i=0; $i<16; $i++) $vms .= (ord($appname[$i]) != 0) ? $appname[$i] : " ";
- // inserts NUL chars where the icon must reside
- for ($i=64; $i<640; $i++) $vms .= chr(0);
- // data string
- $vms .= $data;
- // inserts data length infos in the vms file content string
- $len = strlen($data);
- $vms[72] = chr((($len & 0x000000ff) >> 0) & 0xff);
- $vms[73] = chr((($len & 0x0000ff00) >> 8) & 0xff);
- $vms[74] = chr((($len & 0x00ff0000) >> 16) & 0xff);
- $vms[75] = chr((($len & 0xff000000) >> 24) & 0xff);
- // calculates new CRC
- $crc = calcCRC($vms, strlen($vms));
- $vms[70] = chr((($crc & 0x00ff) >> 0) & 0xff);
- $vms[71] = chr((($crc & 0xff00) >> 8) & 0xff);
- // padds with NUL chars (0x00) until the file size
- // in bytes is a multiple of 512
- while ((strlen($vms)%512) != 0) $vms .= chr(0);
- // returns the VMS string
- return $vms;
- }
- ////////////////////////////////////////////////////////////////////////
- // //
- // 4) Functions used to create specific parts of the files content //
- // //
- ////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- // Function: calcCRC() //
- // Description: calculates the CRC to insert in a VMS file //
- // Parameters: //
- // $vms: string containing the VMS file //
- // $size: int corresponding to the size in bytes of the VMS file //
- // without padding //
- // Return Values: an int corresponding to the CRC value or -1 if an //
- // error occurs //
- ////////////////////////////////////////////////////////////////////////
- function calcCRC($vms, $size) {
- if ($size > strlen($vms)) return -1;
- $n = 0;
- for ($i=0; $i<$size; $i++) {
- // the actual value of the CRC field is ignored
- $vms[$i] = ($i==70 || $i==71) ? chr(0) : $vms[$i];
- $n ^= (ord($vms[$i]) << 8);
- for ($c=0; $c<8; $c++)
- $n = ($n & 0x8000) ? ($n << 1) ^ 4129 : ($n << 1);
- }
- // returns the CRC as a short little endian int
- return $n & 0xffff;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: pngToAsm() //
- // Requirements: image library GD //
- // Description: converts a png image to a string containing the //
- // icon to insert in a VMS file //
- // Note: only there for backward compability, use gdToIcon() instead //
- // Parameters: //
- // $png: string containing the path of the png image //
- // Return Values: a string containing the icon //
- ////////////////////////////////////////////////////////////////////////
- function pngToAsm($png) {
- // creates a new image object with GD from the png image
- $img = imagecreatefrompng($png);
- // creates the icon
- return gdToAsm($img);
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: gdToAsm() //
- // Requirements: image library GD //
- // Description: converts a GD image object to a string containing the //
- // icon to insert in a VMS file //
- // Note: only there for backward compability, use gdToicon() instead //
- // Parameters: //
- // $img: GD image object //
- // Return Values: a string containing the icon //
- ////////////////////////////////////////////////////////////////////////
- function gdToAsm($img) {
- // creates the icon
- return gdToIcon($img);
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: gdToIcon() //
- // Requirements: image library GD //
- // Description: creates a string containing the icon to insert //
- // in a VMS file from a GD image object //
- // Parameters: //
- // $img: GD image object //
- // Return Values: a string containing the icon //
- ////////////////////////////////////////////////////////////////////////
- function gdToIcon($img) {
- $icon = "";
- for ($i=0; $i<16; $i++)
- $pal[$i] = 0;
- // loops the 32 pixels rows of the image
- for ($y=0; $y<32; $y++) {
- // loops the 32 pixels cols of the image
- for ($x=0; $x<32; $x++) {
- // gets colour index of each pixel
- $idx = imagecolorat($img, $x, $y);
- // gets the ARGB values of the colour from the palette
- $col = imagecolorsforindex($img, $idx);
- $a = 15;
- $r = intval($col['red'] / 17);
- $g = intval($col['green'] / 17);
- $b = intval($col['blue'] / 17);
- // assigns the ARGB component to the palette entry
- $pal[$idx] = ($a << 12) | ($r << 8) | ($g << 4) | ($b << 0);
- // assigns color index to a new nybble
- $nyb[sizeof($nyb)] = $idx;
- }
- }
- // destroys the image object created by GD
- //imagedestroy($img);
- // creates the icon's palette from the $pal array
- // 1 colour is composed of 2 bytes
- for ($i=0; $i<16; $i++) {
- $pal1 = ($pal[$i] & 0x00ff) >> 0;
- $pal2 = ($pal[$i] & 0xff00) >> 8;
- $icon .= chr($pal1).chr($pal2);
- }
- // creates the icon's bitmap from the $nyb array
- // 2 nybbles correspond to 1 byte
- for ($i=0; $i<1024; $i+=2)
- $icon .= chr($nyb[$i+1] | ($nyb[$i] << 4));
- // returns a string containing the icon
- // composed of the palette and the bitmap
- return $icon;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: lcdToGD() //
- // Requirements: image library GD //
- // Description: creates a GD image object from the content of a LCD //
- // icon //
- // Parameters: //
- // $lcdcontent: string containing a LCD file //
- // $framenum: [1-255] int corresponding to the frame's rank for //
- // animated LCD icons //
- // Return Values: an image object for GD //
- ////////////////////////////////////////////////////////////////////////
- function lcdtoGD($lcdcontent, $framenum) {
- // creates the GD image object
- $im = imagecreate(48, 32);
- // define the 2 colors of the image
- $green = imagecolorallocate($im, 143, 207, 143);
- $black = imagecolorallocate($im, 0, 0, 0);
- // get the icon frames number
- $frames = ord($lcdcontent[14]) | ((ord($lcdcontent[15]) << 8) & 0xff00);
- if ($frames < 1) $frames = 1;
- // if $framenum is out of the anim limits or is not precised
- // the default frame used will be the first frame
- $argsnum = func_num_args();
- if ($framenum<0 || $framenum>($frames-1) || $argsnum<2) $framenum = 0;
- // get the bitmap start offset
- $start = 16 + (4 * $frames) + ($framenum * 1536);
- // read each pixel of the specific frame
- for ($y=0; $y<32; $y++) {
- for ($x=0; $x<48; $x++) {
- $n = ($y * 48) + $x + $start;
- $pxl = ord($lcdcontent[$n]);
- // set the image pixel color
- // depending of the icon pixel value
- $color = ($pxl == 8) ? $black : $green;
- imagesetpixel($im, $x, $y, $color);
- }
- }
- // returns the GD image object
- return $im;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: doWord() //
- // Description: creates a string from an int //
- // Parameters: //
- // $int: int corresponding to the value to convert //
- // $len: int corresponding to the size in bytes of the string to //
- // create //
- // $order: 2 chars string corresponding to the order of the string: //
- // 'be': the string is made using big endian order //
- // 'le': the string is made using little endian order //
- // anything but 'be' will be read as 'le' //
- // Return Values: a $len chars string //
- ////////////////////////////////////////////////////////////////////////
- function doWord($int, $len, $order) {
- $word = '';
- while ($int >= 256) {
- $n = $int % 256;
- $word = chr($n).$word;
- $int = floor($int / 256);
- }
- $word = chr($int).$word;
- if (strlen($word) > $len) $word = substr($word, strlen($word)-$len);
- while (strlen($word) < $len) $word = chr(0).$word;
- if ($order != 'be') $word = strrev($word);
- return $word;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: dciToVm() //
- // Description: creates an array containing an array used to create //
- // VMI files and a string containing the VMS files from //
- // the content of a DCI file (Nexus cards format) //
- // Parameters: //
- // $dci: string containing the DCI file //
- // Return Values: -1 if an error occurs or an array composed of //
- // 2 elements as follows: //
- // array[0]: an array used to create VMI files similar to the one //
- // returned by the function vmiDecode() //
- // array[1]: a string containing the VMS file //
- ////////////////////////////////////////////////////////////////////////
- function dciToVm($dci) {
- $header = substr($dci, 0, 32);
- $vmiarray['tp'] = (ord($header[0]) == 204) ? '1' : '0';
- $vmiarray['fl'] = '0';
- $vmiarray['of'] = '0';
- $vmiarray['filename'] = substr($header, 4, 12);
- for ($i=16; $i<16+7; $i++) {
- $tm = DecHex(ord($header[$i]));
- $vmiarray['tm'] .= ($tm < 10) ? '0'.$tm : $tm;
- }
- $tm = DecHex(ord($header[23])) + 1;
- if ($tm > 6) $tm = 0;
- $vmiarray['tm'] .= $tm;
- $vmiarray['bl'] = ord($header[24]) | ((ord($header[25]) << 8) & 0xff00);
- $vmiarray['fs'] = $vmiarray['bl'] * 512;
- $vms = '';
- for ($i=32; $i<strlen($dci); $i+=4)
- for ($j=3; $j>=0; $j--)
- $vms .= $dci[$i+$j];
- if ($vmiarray['fs'] != strlen($vms)) return -1;
- return array($vmiarray, $vms);
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: vmToDci() //
- // Description: creates a string containing the DCI file from VMI and //
- // VMS files contents //
- // Parameters: //
- // $vmi: string containing the VMI file //
- // $vms: string containing the VMS file //
- // Return Values: a string containing the DCI file or -1 if an error //
- // occurs //
- ////////////////////////////////////////////////////////////////////////
- function vmToDci($vmi, $vms) {
- $dci = '';
- //TYPE
- $mode = DecBin(ord($vmi[100]));
- $type = $mode[strlen($mode)-2];
- if (chr($type) == chr(0)) $dci .= chr(51);
- elseif (chr($type) == chr(1)) $dci .= chr(204);
- else $dci .= chr(0);
- //COPY
- $dci .= chr(0);
- //1ST BLOCK LOCATION
- $dci .= chr(0).chr(0);
- //FILENAME
- for ($i=88; $i<100; $i++) $dci .= $vmi[$i];
- //CREATION DATE
- //YEAR
- $y = ord($vmi[68]) + (ord($vmi[69]) * 256);
- while (strlen($y) < 4) $y = chr(48).$y;
- $dci .= chr(HexDec(substr($y,0,2))).chr(HexDec(substr($y,2,2)));
- //MONTH
- $dci .= chr(HexDec(ord($vmi[70])));
- //DAY
- $dci .= chr(HexDec(ord($vmi[71])));
- //HOURS
- $dci .= chr(HexDec(ord($vmi[72])));
- //MINS
- $dci .= chr(HexDec(ord($vmi[73])));
- //SECS
- $dci .= chr(HexDec(ord($vmi[74])));
- //WEEKDAY
- $wd = chr(HexDec(ord($vmi[75]))) - 1;
- if ($wd < 0) $wd = 6;
- $dci .= $wd;
- //SIZE IN BLOCKS
- $fs = ((ord($vmi[104]) << 0) & 0xff)
- | ((ord($vmi[105]) << 8) & 0xff00)
- | ((ord($vmi[106]) << 16) & 0xff0000)
- | ((ord($vmi[107]) << 24) & 0xff000000);
- if ($fs != strlen($vms)) return -1;
- $dci .= chr($fs / 512).chr(0);
- //HEADER OFFSET
- $dci .= chr($type).chr(0);
- //UNUSED
- $dci .= chr(0).chr(0).chr(0).chr(0);
- //BODY
- if (strlen($dci) != 32) return -1;
- for ($i=0; $i<strlen($vms); $i+=4)
- for ($j=3; $j>=0; $j--)
- $dci .= $vms[$i + $j];
- if (strlen($dci) != 32 + $fs) return -1;
- return $dci;
- }
- ////////////////////////////////////////////////////////////////////////
- // //
- // 5) Functions used to modify files content //
- // //
- ////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- // Function: insertIcon() //
- // Description: insert a string corresponding to the icon's palette //
- // and bitmap into a VMS file's header //
- // Parameters: //
- // $icon: string containing the icon palette and bitmap //
- // $vms: string containing the VMS file //
- // $type: [0-2] int corresponding to the type of VM file //
- // type 0: DATA //
- // type 1: GAME //
- // type 2: ICONDATA //
- // Return Values: a string containing the modified VMS file //
- // Todo: - inserting instead of replacing in the case of several //
- // bitmaps //
- // - add $animspeed parametter //
- ////////////////////////////////////////////////////////////////////////
- function insertIcon($icon, $vms, $type) {
- // separates the assembly icon into palette and bitmap(s)
- $icons_pal = substr($icon, 0, 32);
- $icons_bit = substr($icon, 32);
- // calculates icons number
- $icons_nb = strlen($icons_bit) / 512;
- // for DATA files the icon starts at offset 0x60
- if ($type == 0) $pal_start=96;
- // for GAME files the icon starts at offset 0x260
- elseif ($type == 1) $pal_start=608;
- // for ICONDATA files the icon starts at offset 0xA0
- else $pal_start=160;
- $bit_start = $pal_start + 32;
- // sets vms header infos for a DATA file
- if ($type == 0) {
- $vms[64] = chr($icons_nb); // number of icons
- $vms[65] = chr(0);
- $vms[66] = chr(0); // animation speed
- $vms[67] = chr(0);
- // sets vms header infos for a GAME file
- } elseif ($type == 1) {
- $vms[576] = chr($icons_nb); // number of icons
- $vms[577] = chr(0);
- $vms[578] = chr(0); // animation speed
- $vms[579] = chr(0);
- // sets vms header infos for an ICONDATA file
- } elseif ($type == 2) {
- $vms[20] = chr(160); // offset of colour icon
- }
- // replaces the bytes of the icon palette
- // in the vms file content string
- for ($i=$pal_start; $i<$bit_start; $i++)
- $vms[$i] = $icon[$i-$pal_start];
- // replaces the bytes of the icon bitmaps
- // in the vms file content string
- for ($i=0; $i<$icons_nb; $i++) {
- for ($i=$bit_start; $i<$bit_start+512; $i++)
- $vms[$i] = $icon[$i-($bit_start-32)];
- $bit_start += 512;
- }
- // calculates new CRC
- $crc = calcCRC($vms, getDataLen($vms));
- $vms[70] = chr((($crc & 0x00ff) >> 0) & 0xff);
- $vms[71] = chr((($crc & 0xff00) >> 8) & 0xff);
- // returns the string corresponding
- // to the modified vms file content
- return $vms;
- }
- ////////////////////////////////////////////////////////////////////////
- // //
- // 6) Functions used to inspect files content //
- // //
- ////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- // Function: iconExtract() //
- // Description: creates an array used to create an image from the //
- // content of a VMS file //
- // Note: only there for backward compability, use //
- // iconExtractAlpha() instead //
- // Parameters: //
- // $vms: string containing the VMS file //
- // $type: [0-2] int corresponding to the type of VM file //
- // type 0: DATA //
- // type 1: GAME //
- // type 2: ICONDATA //
- // $frame: for DATA or GAME file: //
- // [0-2] int corresponding to the index of the bitmap to //
- // extract, 0 is the first bitmap //
- // for ICONDATA file: //
- // boolean: 0 for monochrome or 1 for 16 colours (if exists) //
- // Return Values: an array containing 2 elements as follow: //
- // array[0]: array containing the 16 ARGB components of the palette, //
- // each entry is an array composed of the following keys: //
- // 'alpha': alpha component of the palette entry //
- // 'red': red component of the palette entry //
- // 'green': green component of the palette entry //
- // 'blue': blue component of the palette entry //
- // array[1]: GD image object containing the extracted frame //
- ////////////////////////////////////////////////////////////////////////
- function iconExtract($vms, $type, $frame) {
- // returns the array
- return iconExtractAlpha($vms, $type, $frame, -1);
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: alphaBlend() //
- // Description: creates blended RGB values from bg/fg colors and //
- // alpha opacity //
- // Parameters: //
- // $a: alpha opacity //
- // $r1: background red component //
- // $g1: background green component //
- // $b1: background blue component //
- // $r1: foreground red component //
- // $g1: foreground green component //
- // $b1: foreground blue component //
- // Return Values: 24-bit RGB triplet //
- ////////////////////////////////////////////////////////////////////////
- function alphaBlend($a, $r1,$g1,$b1, $r2,$g2,$b2) {
- $a1 = $a / 255;
- $a2 = 1.0 - $a1;
- $r = ($r1 * $a1) + ($r2 * $a2);
- $g = ($g1 * $a1) + ($g2 * $a2);
- $b = ($b1 * $a1) + ($b2 * $a2);
- $col = ($r << 16) | ($g << 8) | ($b << 0);
- return $col;
- }
- ////////////////////////////////////////////////////////////////////////
- // Function: iconExtractAlpha() //
- // Description: creates an array used to create an image from the //
- // content of a VMS file with correct support of alpha //
- // channel for opacity //
- // Requirements: image library GD //
- // Parameters: //
- // $vms: string containing the VMS file //
- // $type: [0-2] int corresponding to the type of VM file //
- // type 0: DATA //
- // type 1: GAME //
- // type 2: ICONDATA //
- // $frame: for DATA or GAME file: //
- // [0-2] int corresponding to the index of the bitmap to //
- // extract, 0 is the first bitmap //
- // for ICONDATA file: //
- // boolean: 0 for monochrome or 1 for 16 colours (if exists) //
- // $bgcolor: background color of the image as a 24-bit RGB triplet //
- // or -1 if you don't want to use alpha channel //
- // Return Values: -1 if an error occurs or an array containing 2 //
- // elements as follow: //
- // array[0]: array containing the 16 ARGB components of the palette, //
- // each entry is an array composed of the following keys: //
- // 'alpha': alpha component of the palette entry //
- // 'red': red component of the palette entry //
- // 'green': green component of the palette entry //
- // 'blue': blue component of the palette entry //
- // array[1]: GD image object containing the extracted frame //
- ////////////////////////////////////////////////////////////////////////
- function iconExtractAlpha($vms, $type, $frame, $bgcolor) {
- // used for backward compatibility
- if ($type == 'data') $type = VMLIB_TYPE_DATA;
- elseif ($type == 'game') $type = VMLIB_TYPE_GAME;
- elseif ($type == 'icon') $type = VMLIB_TYPE_ICON;
- // the vms file contains a save
- if ($type == VMLIB_TYPE_DATA) {
- $pal_pos = 96;
- $bit_pos = 128 + ($frame * 512);
- // the vms file contains a mini-game
- } elseif ($type == VMLIB_TYPE_GAME) {
- $pal_pos = 512 + 96;
- $bit_pos = 512 + 128 + ($frame * 512);
- // the vms file contains a vm icon
- } elseif ($type == VMLIB_TYPE_ICON) {
- $pal_pos = ((ord($vms[20]) << 0) & 0xff)
- | ((ord($vms[21]) << 8) & 0xff00)
- | ((ord($vms[22]) << 16) & 0xff0000)
- | ((ord($vms[23]) << 24) & 0xff000000);
- $bit_pos = $pal_pos + 32;
- // unknown vms file type
- } else {
- return -1;
- }
- // creates a new 32x32 image object with GD
- $im = ImageCreate(32, 32);
- // icon for ICONDATA file in monochrome mode
- if ($type==VMLIB_TYPE_ICON && ($frame==0 || $pal_pos==0)) {
- $start = ((ord($vms[16]) << 0) & 0xff)
- | ((ord($vms[17]) << 8) & 0xff00)
- | ((ord($vms[18]) << 16) & 0xff0000)
- | ((ord($vms[19]) << 24) & 0xff000000);
- $palette[0] = array('alpha' => 255,
- 'red' => 255,
- 'green' => 255,
- 'blue' => 255);
- for ($i=1; $i<16; $i++)
- $palette[$i] = array('alpha' => 255,
- 'red' => 0,
- 'green' => 0,
- 'blue' => 0);
- $off = imagecolorallocate($im, $palette[0]['red'],
- $palette[0]['green'],
- $palette[0]['blue']);
- $on = imagecolorallocate($im, $palette[1]['red'],
- $palette[1]['green'],
- $palette[1]['blue']);
- for ($y=0; $y<32; $y++) {
- for ($x=0; $x<32; $x+=8,$start++) {
- for ($i=0; $i<8; $i++) {
- $bit = ord($vms[$start]) & (0x80 >> $i);
- imagesetpixel($im, $x+$i, $y, ($bit ? $on : $off));
- }
- }
- }
- return array($palette, $im);
- }
- // regular 16 colours icon for DATA and GAME file
- // or ICONDATA in 16 colours mode
- // creates the image's palette
- if ($bgcolor >= 0) {
- $bg = array( 'red' => (($bgcolor & 0xff0000) >> 16) & 0xff,
- 'green' => (($bgcolor & 0x00ff00) >> 8) & 0xff,
- 'blue' => (($bgcolor & 0x0000ff) >> 0) & 0xff);
- }
- $j = -1;
- for ($i=0; $i<32; $i+=2) {
- $j ++;
- $argb = ord($vms[$pal_pos+$i]) | (ord($vms[$pal_pos+$i+1]) << 8);
- $a = (($argb & 0xf000) >> 8) & 0xff;
- $r = (($argb & 0x0f00) >> 4) & 0xff;
- $g = (($argb & 0x00f0) >> 0) & 0xff;
- $b = (($argb & 0x000f) << 4) & 0xff;
- if ($pal_pos == 0) {
- $alpha = 0;
- $red = 0;
- $green = 0;
- $blue = 0;
- } else {
- // pseudo-alpha rendering
- if ($bgcolor >= 0) {
- $col = alphaBlend(240-$a, $bg['red'],$bg['green'],$bg['blue'], $r,$g,$b);
- $red = (($col & 0xff0000) >> 16) & 0xff;
- $green = (($col & 0x00ff00) >> 8) & 0xff;
- $blue = (($col & 0x0000ff) >> 0) & 0xff;
- // normal rendering
- } else {
- $red = $r;
- $green = $g;
- $blue = $b;
- $alpha = $a;
- }
- }
- ImageColorAllocate($im, $red, $green, $blue);
- $palette[$j] = array('alpha' => $alpha,
- 'red' => $red,
- 'green' => $green,
- 'blue' => $blue);
- }
- // colours the image's pixels
- $j = 0;
- for ($y=0; $y<32; $y++) {
- for ($i=0; $i<16; $i++)
- $buffer[$i] = $vms[$y*16 + $i + $bit_pos];
- for ($x=0; $x<32; $x+=2) {
- $nyb = (ord($buffer[$x/2]) & 0xf0) >> 4; //normal
- // $nyb = (ord($buffer[$x/2]) & 0x0f) >> 0; //reversed
- ImageSetPixel($im, $x, $y, $nyb);
- $nyb = (ord($buffer[$x/2]) & 0x0f) >> 0; //normal
- // $nyb = (ord($buffer[$x/2]) & 0xf0) >> 4; //reversed
- ImageSetPixel($im, $x+1, $y, $nyb);
- }
- }
- // returns the array
- return array($palette, $im);
- }
- ?>
Add Comment
Please, Sign In to add comment