Last active
August 29, 2015 14:21
-
-
Save kawanet/4d92f58b30be491008be to your computer and use it in GitHub Desktop.
JS_Packer の自己解答処理コードの代案
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @see https://github.com/yomotsu/JS_Packer | |
| // JS_Packer の自己解答処理コードの代案。for ループの文字列結合は遅そうなので join("") を使う。 | |
| // 画像全体を一括処理すると RangeError: Maximum call stack size exceeded. になりそうなので、行単位で変換してから join("") する。 | |
| // でも、元のコードよりも、長くなっちゃった。JS_Packer は小さくするのが主目的で、処理時間は関係ないのか。 | |
| function unpack_JS_Packer() { | |
| var i = document.getElementsByTagName("img")[0]; | |
| var c = document.createElement("canvas").getContext("2d"); | |
| c.drawImage(i, 0, 0); | |
| return [].map.call(new Uint32Array(i.height), function(a, y) { | |
| return String.fromCharCode.apply("", [].map.call(new Uint32Array(c.getImageData(0, y, i.width, 1).data.buffer), function(n) { | |
| return n & 0xffffff; | |
| })); | |
| }).join(""); | |
| } | |
| // JS_Packer 圧縮時は、8bit パレット形式の PNG ファイルを出力する。(日本語対応不可能) | |
| // JS_Packer 展開時は、R だけにデータを入っていて、G=0, B=0, A=255 の画像に見える。 | |
| // あるいは、Uint16Array を使って、RG+BA で2文字ずつデータを入れたほうが、展開処理は速くなりそう。(日本語対応可能) | |
| // しかし、8bit 文字が多い JavaScript ソースコードを UTF-16 で格納するのは、容量が大きくなってしまうので NG だった。 | |
| function unpack_uint16array() { | |
| var i = document.getElementsByTagName("img")[0]; | |
| var c = document.createElement("canvas").getContext("2d"); | |
| c.drawImage(i, 0, 0); | |
| return [].map.call(new Uint32Array(i.height), function(a, y) { | |
| return String.fromCharCode.apply("", new Uint16Array(c.getImageData(0, y, i.width, 1).data.buffer)); | |
| }).join(""); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment