home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 6479 / Kanji.7z / kanji.js < prev    next >
Encoding:
Text File  |  2013-06-23  |  7.4 KB  |  195 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.     var inp=Environment.GetCommandLineArgs();
  170.     if(inp.length>1){
  171.         inp=inp[1];
  172.         var bin:byte[] = File.ReadAllBytes(inp),
  173.             width:int = 512, height:int = 1024,
  174.             bm:Bitmap,
  175.             i,j,n,o;
  176.         File.WriteAllBytes(inp+'.dat',bin.slice(262144));//Save character-size data
  177.         bin = Reform4.Decode4(Reform32.Encode32(bin,width/128,height/128), width/128, height/128);
  178.         //bin matches here [1]
  179.         //File.WriteAllBytes('outD',bin.slice(0,262144));
  180.         bm = new Bitmap(width,height);
  181.         height/=2;
  182.     
  183.         for(i=0;i<height;i++){
  184.             for(j=0;j<width;j++){
  185.                 n = bin[j + (width * i)];
  186.                 o = (n&0xC0) | ((n&0x0C)<<2);
  187.                 n = ((n&0x30)<<2) |  ((n&0x03)<<4);
  188.                 bm.SetPixel(j, i, Color.FromArgb(n,n,n));
  189.                 bm.SetPixel(j, i+height, Color.FromArgb(o,o,o));
  190.             }
  191.         }
  192.         bm.Save(inp+'.bmp');//Save Image data
  193.     }else{Console.Write('Drag-and-Drop Kanji.knj onto this program to convert it!\n');}
  194.     
  195. Console.Write("End\nPress enter to exit...");Console.Read();