SHARE
TWEET

PNG decoder

Clowerweb Oct 8th, 2016 188 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function pad(byte) {
  2.   "use strict";
  3.   return byte.toString().replace(/^(.(..)*)$/, '0$1');
  4. }
  5.  
  6. function stringy(byte) {
  7.   "use strict";
  8.   return pad(byte.toString(16));
  9. }
  10.  
  11. function buildString(str, len) {
  12.   "use strict";
  13.   var reg = new RegExp('.{1,' + len + '}', 'g');
  14.  
  15.   str = str.match(reg);
  16.   str.forEach(function (m, idx) {
  17.     str[idx] += "\n";
  18.   });
  19.  
  20.   return str.join('');
  21. }
  22.  
  23. function decompress(code) {
  24.   "use strict";
  25.   var mode = 0, copy, i = 8, lua = '', codelen = (code.charCodeAt(4) << 8) | code.charCodeAt(5);
  26.   var dict = "\n 0123456789abcdefghijklmnopqrstuvwxyz!#%(){}[]<>+=/*:;.,~_".split('');
  27.   var byte, offset, length, buffer;
  28.  
  29.   while (lua.length < codelen) {
  30.     byte = code.charCodeAt(i);
  31.  
  32.     if (mode === 1) {
  33.       lua += code.charAt(i);
  34.       mode = 0;
  35.     } else if (mode === 2) {
  36.       offset = lua.length - ((copy - 60) * 16 + (byte & 15));
  37.       length = (byte >> 4) + 2;
  38.       buffer = lua.substring(offset, offset + length);
  39.       lua   += buffer;
  40.       mode   = 0;
  41.     } else if (byte === 0) {
  42.       mode = 1;
  43.     } else if (byte <= 59) {
  44.       lua += dict[byte - 1];
  45.     } else if (byte >= 60) {
  46.       mode = 2;
  47.       copy = byte;
  48.     }
  49.  
  50.     i++;
  51.   }
  52.  
  53.   return lua;
  54. }
  55.  
  56. function getData(bmp) {
  57.   "use strict";
  58.   var gfx = '', map = '', gff = '', music = '', sfx = '', lua  = '', final, imgData = bmp.bitmap, dataLen = imgData.length, i = 0, n = 0, r, g, b, a, byte, compressed, loop, lastbyte, loops = [], mode, speed, start, end, notes = '', tmp_music = [], track, step, note, version;
  59.  
  60.   if (bmp.width !== 160 || bmp.height !== 205) {
  61.     return alert('Image is the wrong size.');
  62.   }
  63.  
  64.   while (i < dataLen) {
  65.     // get the last 2 bytes of each value and shift left if necessary
  66.     r = (imgData[i++] & 3) << 4;
  67.     g = (imgData[i++] & 3) << 2;
  68.     b = (imgData[i++] & 3);
  69.     a = (imgData[i++] & 3) << 6;
  70.  
  71.     // compile new byte, convert to hex and pad left if needed
  72.     byte = (r | g | b | a);
  73.  
  74.     if (n < 8192) {
  75.       // change endianness and append to output string
  76.       gfx += stringy(byte).split('').reverse().join('');
  77.     } else if (n < 12288) {
  78.       map += stringy(byte);
  79.     } else if (n < 12544) {
  80.       gff += stringy(byte);
  81.     } else if (n < 12800) {
  82.       track = Math.floor((n - 12544) / 4);
  83.       note  = stringy(byte & 127);
  84.  
  85.       if (n % 4 === 0) {
  86.         tmp_music.push([null, '']);
  87.       }
  88.  
  89.       tmp_music[track][0] = stringy(((byte & 128) >> 7 - n % 4) | tmp_music[track][0]);
  90.       tmp_music[track][1] += note;
  91.     } else if (n < 17152) {
  92.       step = (n - 12800) % 68;
  93.  
  94.       if (step < 64 && n % 2 === 1) {
  95.         note = (byte << 8) + lastbyte;
  96.         notes += (stringy(note & 63) + ((note & 448) >> 6).toString(16) + ((note & 3584) >> 9).toString(16) + ((note & 28672) >> 12).toString(16));
  97.       } else if (step === 64) {
  98.         mode = pad(byte);
  99.       } else if (step === 65) {
  100.         speed = byte;
  101.       } else if (step === 66) {
  102.         start = byte;
  103.       } else if (step === 67) {
  104.         end = byte;
  105.         sfx += mode + stringy(speed) + stringy(start) + stringy(end) + notes + "\n";
  106.         notes = '';
  107.       }
  108.     } else if (n < 32768) {
  109.       if (n === 17152) {
  110.         compressed = (byte === 58);
  111.       }
  112.  
  113.       lua += String.fromCharCode(byte);
  114.     } else if (n === 32768) {
  115.       version = byte;
  116.     }
  117.  
  118.     lastbyte = byte;
  119.     n++;
  120.   }
  121.  
  122.   if (compressed) lua = decompress(lua);
  123.   gfx = buildString(gfx, 128);
  124.   gff = buildString(gff, 256);
  125.   map = buildString(map, 256);
  126.  
  127.   tmp_music.forEach(function (m) {
  128.     music += m[0] + ' ' + m[1] + "\n";
  129.   });
  130.  
  131.   final  = "pico-8 cartridge // http://www.pico-8.com\n";
  132.   final += 'version ' + version + "\n";
  133.   final += "__lua__\n" + lua + "\n__gfx__\n" + gfx + "__gff__\n" + gff + "__map__\n" + map + "__sfx__\n" + sfx + "__music__\n" + music + "\n\n";
  134.  
  135.   return final;
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top