home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 6479 / Kanji.7z / mkkanji.js < prev   
Encoding:
Text File  |  2013-06-23  |  8.0 KB  |  217 lines

  1. import System;
  2. import System.IO;    //Filesystem
  3. import System.Drawing;    //Bitmap
  4. /* ----- Helper Functions ----- */
  5.  
  6. var Reform32={
  7.     tbl32bc:[0, 1, 4, 5, 16, 17, 20, 21, 2, 3, 6, 7, 18, 19, 22, 23, 8, 9, 12, 13, 24, 25, 28, 29, 10, 11, 14, 15, 26, 27, 30, 31],
  8.     tbl32pao:[0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15],
  9.     Decode32:function(bin:byte[], bw:int, bh:int){
  10. Console.WriteLine('Decode32-BEGIN');
  11.         var buf:byte[] = new byte[bin.Length],
  12.             i:int=0,j:int,k:int,m:int,n:int,p:int,
  13.             c1:int,c2:int,c3:int,c4:int,c5:int,ind:int,indO:int;
  14.         for(i=0;i<(32*bh);i+=32){
  15.             for(j=0;j<(64*bw);j+=64){
  16.                 c1 = 8192 * ((j / 64) + (bw * (i / 32)));
  17.                 for(k=0;k<32;k+=8){
  18.                     for(m=0;m<64;m+=8){
  19.                         c2 = 256 * Reform32.tbl32bc[(m / 8) + ((k / 8) * 8)];
  20.                         for(n=0;n<4;n++){
  21.                             c3 = 64 * n;
  22.                             for(p=0;p<16;p++){
  23.                                 c4 = (j + m) + (p % 8);
  24.                                 c5 = int(((i + k) + (2 * n)) + (p / 8));
  25.                                 ind = 4 * (c4 + ((64 * bw) * c5));
  26.                                 indO = (((4 * Reform32.tbl32pao[p]) + c3) + c2) + c1;
  27.                                 buf[ind] = bin[indO];
  28.                                 buf[ind + 1] = bin[indO + 1];
  29.                                 buf[ind + 2] = bin[indO + 2];
  30.                                 buf[ind + 3] = bin[indO + 3];
  31.                             }
  32.                         }
  33.                     }
  34.                 }
  35.             }
  36.         }
  37. Console.WriteLine('Decode32-END');
  38.         return buf;
  39.     },
  40.     Encode32:function(bin:byte[], bw:int, bh:int){
  41. Console.WriteLine('Encode32-BEGIN');
  42.         var buf:byte[] = new byte[bin.Length],
  43.             i:int=0,j:int,k:int,m:int,n:int,p:int,
  44.             c1:int,c2:int,c3:int,c4:int,c5:int,ind:int,indO:int;
  45.         for(i=0;i<(32*bh);i+=32){
  46.             for(j=0;j<(64*bw);j+=64){
  47.                 c1 = 8192 * ((j / 64) + (bw * (i / 32)));
  48.                 for(k=0;k<32;k+=8){
  49.                     for(m=0;m<64;m+=8){
  50.                         c2 = int(256 * Reform32.tbl32bc[(m / 8) + ((k / 8) * 8)]);
  51.                         for(n=0;n<4;n++){
  52.                             c3 = 64 * n;
  53.                             for(p=0;p<16;p++){
  54.                                 c4 = (j + m) + (p % 8);
  55.                                 c5 = int(((i + k) + (2 * n)) + (p / 8));
  56.                                 ind = 4 * (c4 + ((64 * bw) * c5));
  57.                                 indO = (((4 * Reform32.tbl32pao[p]) + c3) + c2) + c1;
  58.                                 buf[indO] = bin[ind];
  59.                                 buf[indO + 1] = bin[ind + 1];
  60.                                 buf[indO + 2] = bin[ind + 2];
  61.                                 buf[indO + 3] = bin[ind + 3];
  62.                             }
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.         }
  68. Console.WriteLine('Encode32-END');
  69.         return buf;
  70.     }
  71. };
  72. var Reform4={
  73.     tbl4bc:[0, 2, 8, 10, 1, 3, 9, 11, 4, 6, 12, 14, 5, 7, 13, 15, 16, 18, 24, 26, 17, 19, 25, 27, 20, 22, 28, 30, 21, 23, 29, 31],
  74.     tbl4col0:[0, 0x20, 0x80, 160, 0x100, 0x120, 0x180, 0x1a0, 8, 40, 0x88, 0xa8, 0x108, 0x128, 0x188, 0x1a8, 0x10, 0x30, 0x90, 0xb0, 0x110, 0x130, 400, 0x1b0, 0x18, 0x38, 0x98, 0xb8, 280, 0x138, 0x198, 440, 0x40, 0x60, 0xc0, 0xe0, 320, 0x160, 0x1c0, 480, 0x48, 0x68, 200, 0xe8, 0x148, 360, 0x1c8, 0x1e8, 80, 0x70, 0xd0, 240, 0x150, 0x170, 0x1d0, 0x1f0, 0x58, 120, 0xd8, 0xf8, 0x158, 0x178, 0x1d8, 0x1f8, 260, 0x124, 0x184, 420, 4, 0x24, 0x84, 0xa4, 0x10c, 300, 0x18c, 0x1ac, 12, 0x2c, 140, 0xac, 0x114, 0x134, 0x194, 0x1b4, 20, 0x34, 0x94, 180, 0x11c, 0x13c, 0x19c, 0x1bc, 0x1c, 60, 0x9c, 0xbc, 0x144, 0x164, 0x1c4, 0x1e4, 0x44, 100, 0xc4, 0xe4, 0x14c, 0x16c, 460, 0x1ec, 0x4c, 0x6c, 0xcc, 0xec, 340, 0x174, 0x1d4, 500, 0x54, 0x74, 0xd4, 0xf4, 0x15c, 380, 0x1dc, 0x1fc, 0x5c, 0x7c, 220, 0xfc],
  75.     tbl4col1:[0x100, 0x120, 0x180, 0x1a0, 0, 0x20, 0x80, 160, 0x108, 0x128, 0x188, 0x1a8, 8, 40, 0x88, 0xa8, 0x110, 0x130, 400, 0x1b0, 0x10, 0x30, 0x90, 0xb0, 280, 0x138, 0x198, 440, 0x18, 0x38, 0x98, 0xb8, 320, 0x160, 0x1c0, 480, 0x40, 0x60, 0xc0, 0xe0, 0x148, 360, 0x1c8, 0x1e8, 0x48, 0x68, 200, 0xe8, 0x150, 0x170, 0x1d0, 0x1f0, 80, 0x70, 0xd0, 240, 0x158, 0x178, 0x1d8, 0x1f8, 0x58, 120, 0xd8, 0xf8, 4, 0x24, 0x84, 0xa4, 260, 0x124, 0x184, 420, 12, 0x2c, 140, 0xac, 0x10c, 300, 0x18c, 0x1ac, 20, 0x34, 0x94, 180, 0x114, 0x134, 0x194, 0x1b4, 0x1c, 60, 0x9c, 0xbc, 0x11c, 0x13c, 0x19c, 0x1bc, 0x44, 100, 0xc4, 0xe4, 0x144, 0x164, 0x1c4, 0x1e4, 0x4c, 0x6c, 0xcc, 0xec, 0x14c, 0x16c, 460, 0x1ec, 0x54, 0x74, 0xd4, 0xf4, 340, 0x174, 0x1d4, 500, 0x5c, 0x7c, 220, 0xfc, 0x15c, 380, 0x1dc, 0x1fc],
  76.     Decode4:function(bin:byte[], bw:int, bh:int){
  77. Console.WriteLine('Decode4-BEGIN');
  78.         var buf:byte[] = new byte[bin.Length],
  79.             i:int,j:int,k:int,m:int,n:int,p:int,c1:int,c2:int,c3:int,b2:int[],
  80.             c4:int,c5:int,c6:byte,c7:int,c8:int,c9:int,ca:byte;
  81.         for(i=0;i<(128*bh);i+=128){
  82.             for(j=0;j<(128*bw);j+=128){
  83.                 c1 = 8192 * ((j / 128) + (bw * (i / 128)));
  84.                 for(k=0;k<128;k+=16){
  85.                     for(m=0;m<128;m+=32){
  86.                         c2 = 256 * Reform4.tbl4bc[(m / 32) + (4 * (k / 16))];
  87.                         for(n=0;n<4;n++){
  88.                             c3 = 64 * n;
  89.                             b2 = ((n & 1) === 0) ? Reform4.tbl4col0 : Reform4.tbl4col1;
  90.                             for(p=0;p<128;p++){
  91.  
  92.                                 c4 = int(b2[p] / 8);
  93.                                 c5 = b2[p] % 8;
  94.                                 c6 = (bin[((c1 + c2) + c3) + c4] >> c5) & 15;
  95.                                 c7 = (j + m) + (p % 32);
  96.                                 c8 = int(((i + k) + (4 * n)) + (p / 32));
  97.                                 c9 = c7 + ((128 * bw) * c8);
  98.                                 ca = byte(buf[int(c9 / 2)]);
  99.                                 if((c9 & 1) === 0){
  100.                                     ca = ca & 15;
  101.                                     ca = ca | ((c6 << 4)&0xFF);
  102.                                 }else{
  103.                                     ca = ca & 240;
  104.                                     ca = ca | c6;
  105.                                 }
  106.                                 buf[int(c9 / 2)] = ca;
  107.  
  108.                             }
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.         }
  114. Console.WriteLine('Decode4-END');
  115.         return buf;
  116.     },
  117.     Encode4:function(bin:byte[], bw:int, bh:int){
  118. Console.WriteLine('Encode4-BEGIN');
  119.         var buf:byte[] = new byte[bin.Length],
  120.             i:int,j:int,k:int,m:int,n:int,p:int,c1:int,c2:int,c3:int,b2:int[],
  121.             c4:int,c5:int,c6:byte,c7:int,c8:int,c9:int,ca:byte,ind:int;
  122.         for(i=0;i<(128*bh);i+=128){
  123.             for(j=0;j<(128*bw);j+=128){
  124.                 c1 = 8192 * ((j / 128) + (bw * (i / 128)));
  125.                 for(k=0;k<128;k+=16){
  126.                     for(m=0;m<128;m+=32){
  127.                         c2 = 256 * Reform4.tbl4bc[(m / 32) + (4 * (k / 16))];
  128.                         for(n=0;n<4;n++){
  129.                             c3 = 64 * n;
  130.                             b2 = ((n & 1) === 0) ? Reform4.tbl4col0 : Reform4.tbl4col1;
  131.                             for(p=0;p<128;p++){
  132.                                 c4 = (j + m) + (p % 0x20);
  133.                                 c5 = int(((i + k) + (4 * n)) + (p / 0x20));
  134.                                 c7 = c4 + ((0x80 * bw) * c5);
  135.                                 c6 = bin[int(c7 / 2)];
  136.                                 if ((c7 & 1) !== 0){
  137.                                     c6 = (c6 & 15);
  138.                                 }else{
  139.                                     c6 = (c6 >> 4);
  140.                                 }
  141.                                 c8 = int(b2[p] / 8);
  142.                                 c9 = b2[p] % 8;
  143.                                 ind = ((c1 + c2) + c3) + c8;
  144.                                 ca = buf[ind];
  145.                                 switch(c9){
  146.                                     case 0:
  147.                                         ca = (ca & 240);
  148.                                         ca = (ca | c6);
  149.                                         break;
  150.                                     case 4:
  151.                                         ca = (ca & 15);
  152.                                         ca = (ca | ((byte)(c6 << 4)));
  153.                                         break;
  154.                                 }
  155.                                 buf[ind] = ca;
  156.                             }
  157.                         }
  158.                     }
  159.                 }
  160.             }
  161.         }
  162. Console.WriteLine('Encode4-END');
  163.         return buf;
  164.     }
  165. };
  166.  
  167.  
  168. Console.WriteLine("Start");
  169. function main(){
  170.     var inp=Environment.GetCommandLineArgs(),l=0,i=null;
  171.     if(inp.length>1){
  172.         inp=inp[1];
  173.         do{
  174.             Console.WriteLine('Write file for Final mix [j] or US [u]?');
  175.             i=Console.ReadLine().Trim().toLowerCase();
  176.         }while(i!=='j'&&i!=='u');
  177.         l=i==='j'?1892:1472;
  178.         var bm:Bitmap = new Bitmap(inp),bin:byte[]=new byte[(512*512)+l],
  179.             j,n,o,h=512;
  180.         if(bm.Width!==512){
  181.             Console.WriteLine("WIDTH MUST BE 512!");
  182.             return;
  183.         }else if(bm.Height!==1024){
  184.             Console.WriteLine("HEIGHT MUST BE 1024!");
  185.             return;
  186.         }
  187.         for(i=0;i<h;i++){
  188.             for(j=0;j<512;j++){
  189.                 n = (Math.ceil(bm.GetPixel(j,i).G/16)*16)|0;
  190.                 o = (Math.ceil(bm.GetPixel(j,i+h).G/16)*16)|0;
  191.                 bin[j + (512 * i)] = (o&0xC0) | ((n&0xC0) >> 2) | ((o&0x30) >> 2) | ((n&0x30) >> 4);
  192.             }
  193.         }
  194.         //bin matches here [1]
  195.         //File.WriteAllBytes('outE',bin);
  196.         h=1024/128;
  197.         bin = Reform32.Decode32(Reform4.Encode4(bin,4,h), 4, h);
  198.         Console.Write("Import file for widths? (If no, 0 is used for all characters): ");
  199.         
  200.         if(File.Exists(j=inp.replace(/\.bmp$/,'.dat'))){i=j;Console.WriteLine(''+j);}else{i=Console.ReadLine();}
  201.         if(File.Exists(i)){
  202.             i=File.ReadAllBytes(i);
  203.             if(i.length!==l){
  204.                 Console.WriteLine('File has length '+i.length+', but this file needs '+l+'!\Using 0s!');
  205.             }else{
  206.                 for(j=0;j<l;j++){
  207.                     bin[j+262144]=i[j];
  208.                 }
  209.                 Console.WriteLine("Copied!");
  210.             }
  211.         }else{Console.WriteLine("Using 0s!");}
  212.         
  213.         File.WriteAllBytes('NEW-kanji.knj',bin);
  214.     }else{Console.Write('Drag-and-Drop a BMP onto this program to convert it!\n');}
  215. }main();
  216.  
  217. Console.WriteLine("End\nPress enter to exit...");Console.Read();