{"id":8594,"date":"2013-12-08T00:00:11","date_gmt":"2013-12-07T15:00:11","guid":{"rendered":"http:\/\/labs.gree.jp\/blog\/?p=8594"},"modified":"2013-12-09T00:49:59","modified_gmt":"2013-12-08T15:49:59","slug":"canvas-%e3%81%8b%e3%82%89%e7%94%9f%e6%88%90%e3%81%97%e3%81%9f-png-%e7%94%bb%e5%83%8f%e3%81%ab%e7%8b%ac%e8%87%aa%e3%81%ae%e6%83%85%e5%a0%b1%e3%82%92%e5%9f%8b%e3%82%81%e8%be%bc%e3%82%80","status":"publish","type":"post","link":"https:\/\/labs.gree.jp\/blog\/2013\/12\/8594\/","title":{"rendered":"Canvas \u304b\u3089\u751f\u6210\u3057\u305f PNG \u753b\u50cf\u306b\u72ec\u81ea\u306e\u60c5\u5831\u3092\u57cb\u3081\u8fbc\u3080"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001Multimedia Engineering Team \u306e\u3044\u307e\u3084\u3067\u3059\u3002<br \/>\nAdvent Calendar 8\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059\u304c\u3001\u7279\u306b\u305d\u3046\u3044\u3046\u306e\u3068\u306f\u95a2\u4fc2\u306a\u304f\u597d\u304d\u306a\u3053\u3068\u3092\u66f8\u304d\u307e\u3059\u3002<\/p>\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>Canvas \u3067\u8272\u3005\u3084\u3063\u3066\u3044\u308b\u3068\u3001 <code>Canvas#toDataURL()<\/code> \u3092\u4f7f\u3063\u3066\u751f\u6210\u3057\u305f PNG \u753b\u50cf\u306b\u72ec\u81ea\u306e\u60c5\u5831\u3092\u57cb\u3081\u8fbc\u307f\u305f\u304f\u306a\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u306f\u3001\u3053\u306e\u751f\u6210\u3057\u305f\u753b\u50cf\u306b\u72ec\u81ea\u306e\u60c5\u5831\u3092\u57cb\u3081\u8fbc\u3080\u65b9\u6cd5\u3092\u8aac\u660e\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>PNG \u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u304a\u3055\u3089\u3044<\/h2>\n<p>PNG \u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306b\u3064\u3044\u3066\u306f\uff08\u4ee5\u524d\u500b\u4eba\u30d6\u30ed\u30b0\u3067\u3059\u304c\uff09\u7d39\u4ecb\u3057\u305f\u306e\u3067\u3001\u8a73\u3057\u304f\u77e5\u308a\u305f\u3044\u65b9\u306f\u305d\u3061\u3089\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<ul>\n<li>PNG \u753b\u50cf\u306e\u89e3\u6790\u3068\u6700\u9069\u5316\u30c4\u30fc\u30eb: <a href=\"http:\/\/imaya.blog.jp\/archives\/6136997.html\">http:\/\/imaya.blog.jp\/archives\/6136997.html<\/a><\/li>\n<\/ul>\n<p>\u3053\u3053\u3067\u306f\u7c21\u5358\u306a\u304a\u3055\u3089\u3044\u306e\u307f\u306b\u3057\u307e\u3059\u3002<br \/>\nPNG \u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u69cb\u9020\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>PNG \u30b7\u30b0\u30cd\u30c1\u30e3(8\u30d0\u30a4\u30c8)<\/li>\n<li>\u30c1\u30e3\u30f3\u30af\u7fa4(\u4ee5\u4e0b\u306e\u30c7\u30fc\u30bf\u304c\u9023\u7d9a\u3057\u3066\u914d\u7f6e)\n<ul>\n<li>\u30c7\u30fc\u30bf\u306e\u9577\u3055(4\u30d0\u30a4\u30c8)<\/li>\n<li>\u30c1\u30e3\u30f3\u30af\u30bf\u30a4\u30d7(4\u30d0\u30a4\u30c8)<\/li>\n<li>\u30c7\u30fc\u30bf(\u53ef\u5909)<\/li>\n<li>CRC32(4\u30d0\u30a4\u30c8)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u7c21\u5358\u3067\u3059\u306d\u3002<br \/>\n\u3042\u3068\u3067\u51fa\u3066\u304f\u308b\u306e\u3067\u5c11\u3057\u8a18\u61b6\u306b\u3068\u3069\u3081\u3066\u304a\u3044\u3066\u6b32\u3057\u3044\u306e\u3067\u3059\u304c\u3001\u30c1\u30e3\u30f3\u30af\u306e\u30b5\u30a4\u30ba\u306f\u30c7\u30fc\u30bf+12\u30d0\u30a4\u30c8\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001IDAT\u30c1\u30e3\u30f3\u30af\u306e\u524d\u306b\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30c1\u30e3\u30f3\u30af(\u4ed5\u69d8\u306b\u5b58\u5728\u3057\u306a\u3044\u3051\u3069\u52dd\u624b\u306b\u3064\u304f\u3063\u3066\u3044\u3044\u30c1\u30e3\u30f3\u30af)\u3092\u3064\u304f\u3063\u3066\u633f\u5165\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>\u6e96\u5099<\/h2>\n<p>PNG \u306e\u30c1\u30e3\u30f3\u30af\u3092\u3064\u304f\u308b\u306b\u306f\u3001\u3044\u304f\u3064\u304b\u306e\u6c7a\u307e\u308a\u4e8b\u3092\u77e5\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u308a\u3001\u5fc5\u8981\u306a\u8a08\u7b97\u306a\u3069\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u5177\u4f53\u7684\u306b\u306f\u4ee5\u4e0b\u306e\u3082\u306e\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u30c1\u30e3\u30f3\u30af\u540d\u306e\u547d\u540d\u898f\u5247<\/li>\n<li>\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30c1\u30e3\u30f3\u30af\u306e\u633f\u5165\u4f4d\u7f6e<\/li>\n<li>CRC32 \u306e\u8a08\u7b97<\/li>\n<\/ul>\n<p>\u3053\u308c\u3089\u306b\u3064\u3044\u3066\u9806\u306b\u8aac\u660e\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h3>\u30c1\u30e3\u30f3\u30af\u540d\u306e\u547d\u540d\u898f\u5247<\/h3>\n<p>\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30c1\u30e3\u30f3\u30af\u306f\u52dd\u624b\u306b\u4f5c\u3063\u3066\u3044\u3044\u30c1\u30e3\u30f3\u30af\u3067\u3059\u304c\u3001\u30c1\u30e3\u30f3\u30af\u540d\u306b\u306f\u30eb\u30fc\u30eb\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u307e\u305a\u3001\u524d\u8ff0\u306e\u304a\u3055\u3089\u3044\u3067\u3082\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u304c\u30014\u30d0\u30a4\u30c8\u306a\u306e\u3067\uff08\u57fa\u672c\u7684\u306b\u306f\u4eba\u9593\u306e\u8aad\u3081\u308b\uff094\u6587\u5b57\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n\u305d\u3057\u3066\u3001\u305d\u308c\u305e\u308c\u306e\u6587\u5b57\u306e\u5148\u982d\u306ebit\u304c\u7acb\u3063\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u306b\u610f\u5473\u304c\u3042\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\uff08\u4eba\u9593\u304c\u7c21\u5358\u306b\u5224\u5225\u3059\u308b\u306b\u306f\u3001\u305d\u308c\u305e\u308c\u306e\u6587\u5b57\u304c\u5927\u6587\u5b57(0)\u304b\u5c0f\u6587\u5b57(1)\u304b\u3067\u5224\u65ad\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff09<\/p>\n<p>\u305d\u308c\u305e\u308c\u306e\u6587\u5b57\u306e\u5148\u982d\u30d3\u30c3\u30c8\u304c\u3069\u3046\u3044\u3046\u610f\u5473\u3092\u6301\u3064\u306e\u304b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>1\u6587\u5b57\u76ee: \u5fc5\u9808\u30c1\u30e3\u30f3\u30af\u304b\u3069\u3046\u304b\u3002\u4eca\u56de\u306f\u5fc5\u9808\u30c1\u30e3\u30f3\u30af\u3067\u306f\u306a\u3044\u306e\u3067\u5c0f\u6587\u5b57(1)<\/li>\n<li>2\u6587\u5b57\u76ee: \u30d1\u30d6\u30ea\u30c3\u30af\u306a\u30c1\u30e3\u30f3\u30af\u304b\u3069\u3046\u304b\u3002\u4eca\u56de\u306f\u52dd\u624b\u306b\u3064\u304f\u308b\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u306a\u30c1\u30e3\u30f3\u30af\u306a\u306e\u3067\u5c0f\u6587\u5b57(1)<\/li>\n<li>3\u6587\u5b57\u76ee: \u4e88\u7d04\u3002\u73fe\u5728\u306e\u4ed5\u69d8\u3067\u306f\u304b\u306a\u3089\u305a\u5927\u6587\u5b57(0)<\/li>\n<li>4\u6587\u5b57\u76ee: \u8907\u5199\u53ef\u80fd\u304b\u3069\u3046\u304b\u3002\u753b\u50cf\u306e\u5185\u5bb9\u304c\u5909\u5316\u3057\u3066\u3082\u30b3\u30d4\u30fc\u3057\u3066\u3088\u3044\u306a\u3089\u3070\u5c0f\u6587\u5b57(1)\u3001\u30b3\u30d4\u30fc\u3057\u3066\u306f\u3044\u3051\u306a\u3044\u306a\u3089\u5927\u6587\u5b57(0)<\/li>\n<\/ul>\n<p>\u4e0a\u8a18\u306e\u6761\u4ef6\u3092\u8e0f\u307e\u3048\u3066\u3001\u4eca\u56de\u306f \"hoGe\" \u30c1\u30e3\u30f3\u30af\u3092\u4f5c\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u3087\u3046\u3002<br \/>\n\u300c\u5fc5\u9808\u3067\u306f\u306a\u304f\u300d\u300c\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u306a\u300d\u300c\u753b\u50cf\u306e\u5185\u5bb9\u304c\u5909\u308f\u3063\u3066\u3082\u30b3\u30d4\u30fc\u53ef\u80fd\u306a\u300d\u30c1\u30e3\u30f3\u30af\u3067\u3059\u3002<\/p>\n<h3>\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30c1\u30e3\u30f3\u30af\u306e\u633f\u5165\u4f4d\u7f6e<\/h3>\n<p>hoGe \u30c1\u30e3\u30f3\u30af\u3092\u3064\u304f\u308b\u306e\u3092\u6c7a\u3081\u305f\u306e\u306f\u826f\u3044\u3067\u3059\u304c\u3001\u3069\u3053\u306b\u633f\u5165\u3059\u308b\u304b\u3082\u6c7a\u3081\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u57fa\u672c\u7684\u306b\u306f\u5fc5\u9808\u30c1\u30e3\u30f3\u30af\u3067\u3042\u308b IHDR, PLTE, IDAT, IEND \u306e\u3069\u3053\u306b\u3044\u308c\u308b\u304b\u3068\u3044\u3046\u8a71\u3057\u306b\u306a\u308a\u307e\u3059\u3002(\u3053\u306e\u3046\u3061\u3001IHDR, IEND \u306f\u5148\u982d\u3068\u6700\u5f8c\u306b\u306a\u304f\u3066\u306f\u3044\u3051\u306a\u3044\u3068\u3044\u3046\u6c7a\u307e\u308a\u304c\u3042\u308a\u307e\u3059\u3002)<br \/>\n\u4ed5\u69d8\u306b\u8a18\u8ff0\u3055\u308c\u3066\u3044\u308b\u306e\u306f\u3069\u306e\u30c1\u30e3\u30f3\u30af\u306e\u524d\u5f8c\u306b\u306a\u304f\u3066\u306f\u3044\u3051\u306a\u3044\u306a\u3069\u6c7a\u3081\u3089\u308c\u3066\u3044\u308b\u30c1\u30e3\u30f3\u30af\u3082\u3042\u308a\u307e\u3059\u304c\u3001\u4eca\u56de\u306f\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30c1\u30e3\u30f3\u30af\u3067\u3059\u306e\u3067\u9069\u5f53\u306b\u6700\u521d\u306e IDAT \u306e\u524d\u306b\u3044\u308c\u308b\u3053\u3068\u306b\u3057\u307e\u3059\u3002<\/p>\n<h3>CRC32 \u306e\u8a08\u7b97<\/h3>\n<p>CRC32 \u3068\u3044\u3046\u306e\u306f\u30c1\u30a7\u30c3\u30af\u30b5\u30e0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u4e00\u7a2e\u3067\u3059\u3002\u7c21\u5358\u306a\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306a\u306e\u3067\u8abf\u3079\u308c\u3070\u3059\u3050\u306b\u5b9f\u88c5\u3067\u304d\u308b\u3068\u304a\u3082\u3044\u307e\u3059\u304c\u3001\u4eca\u56de\u306f\u62d9\u4f5c\u306e zlib.js \u3067\u4f7f\u7528\u3057\u3066\u3044\u308b\u3082\u306e\u3092\u5229\u7528\u3057\u307e\u3059\u3002<br \/>\nCRC32 \u306e\u5bfe\u8c61\u3068\u306a\u308b\u306e\u306f\u30c1\u30e3\u30f3\u30af\u30bf\u30a4\u30d7\u3068\u30c7\u30fc\u30bf\u90e8\u5206\u3067\u3059\u3002<\/p>\n<h2>\u5b9f\u88c5<\/h2>\n<p>\u3067\u306f\u3001\u3055\u3063\u305d\u304f\u5b9f\u88c5\u306b\u5165\u308a\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u3053\u3053\u304b\u3089\u306f\u30b3\u30fc\u30c9\u304c\u4e2d\u5fc3\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u3055\u307b\u3069\u96e3\u3057\u304f\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h3>CRC32 \u3092\u8a08\u7b97\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30ed\u30fc\u30c9<\/h3>\n<p>\u4e8b\u524d\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u3067\u8aad\u307f\u8fbc\u3093\u3067\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"lang-html\"><script src=\"https:\/\/rawgithub.com\/imaya\/zlib.js\/master\/bin\/crc32.min.js\"><\/script><\/code><\/pre>\n<h3>DataURL \u306e\u4f5c\u6210<\/h3>\n<p>\u307e\u305a\u306f\u57cb\u3081\u8fbc\u3080\u5bfe\u8c61\u3068\u306a\u308b PNG \u753b\u50cf\u3092 <code>Canvas#toDataURL()<\/code> \u3067\u4f5c\u6210\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code class=\"lang-js\">var canvas = document.createElement('canvas');\nvar dataurl = canvas.toDataURL();<\/code><\/pre>\n<p>\u3053\u3053\u3067\u306f\u9069\u5f53\u306b Canvas \u3092\u4f5c\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u3082\u3061\u308d\u3093\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306a\u3069\u3067\u63cf\u753b\u3057\u305f Canvas \u3067\u3082\u304b\u307e\u3044\u307e\u305b\u3093\u3002<\/p>\n<h3>Base64 \u30c7\u30b3\u30fc\u30c9<\/h3>\n<p>base64 \u6587\u5b57\u5217\u306e\u30c7\u30b3\u30fc\u30c9\u306f\u3001window.atob \u3092\u7528\u3044\u307e\u3059\u3002\u4f7f\u3048\u306a\u3044\u74b0\u5883\u306e\u5834\u5408\u306f\u62d9\u4f5c\u306e base64.js \u306a\u3069\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3084\u308a\u307e\u3059\u3002<br \/>\n\u307e\u305f\u3001\u3053\u306e\u6bb5\u968e\u304b\u3089\u3064\u3044\u3067\u306b Uint8Array \u306b\u5909\u66f4\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"lang-js\">\/\/ Data URL \u304b\u3089\u30c7\u30fc\u30bf\u90e8\u5206\u3092\u629c\u304d\u51fa\u3057\nvar b64data = dataurl.split(',', 2);\n\n\/\/ Base64 \u30c7\u30b3\u30fc\u30c9\nvar decoded = window.atob(b64data.pop());\n\n\/\/ Uint8Array \u306b\u5909\u63db\nvar png = new Uint8Array(\n    decoded.split('').map(function(char) {\n        return char.charCodeAt(0);\n    })\n);<\/code><\/pre>\n<h3>PNG \u3078\u306e\u57cb\u3081\u8fbc\u307f\u306e\u6e96\u5099<\/h3>\n<p>\u3053\u3053\u304b\u3089 PNG \u30d0\u30a4\u30ca\u30ea\u3092\u63a2\u7d22\u3057\u3066\u30c1\u30e3\u30f3\u30af\u306e\u633f\u5165\u3092\u884c\u3046\u8a33\u3067\u3059\u304c\u3001PNG \u30d0\u30a4\u30ca\u30ea\u306e\u3069\u3053\u3092\u8aad\u3093\u3067\u3044\u308b\u304b\u899a\u3048\u308b\u5909\u6570\u304c\u5fc5\u8981\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n\u307e\u305f\u3001\u57cb\u3081\u8fbc\u3093\u3060\u5f8c\u306e\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u30d0\u30c3\u30d5\u30a1\u3068\u3001\u3069\u3053\u307e\u3067\u66f8\u304d\u8fbc\u3093\u3060\u304b\u3082\u899a\u3048\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u57cb\u3081\u8fbc\u3093\u3060\u5f8c\u306e\u5168\u4f53\u306e\u30b5\u30a4\u30ba\u306f<\/p>\n<ul>\n<li> \u73fe\u5728\u306ePNG\u30d0\u30a4\u30ca\u30ea\u306e\u30b5\u30a4\u30ba + \u57cb\u3081\u8fbc\u3080\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba + 12<\/li>\n<\/ul>\n<p>\u3068\u306a\u308a\u307e\u3059\u3002\u306a\u305c\u305d\u3046\u306a\u308b\u304b\u5206\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u3001\u6700\u521d\u306b\u304a\u3055\u3089\u3044\u3057\u305f PNG \u306e\u30c1\u30e3\u30f3\u30af\u306e\u8aac\u660e\u3092\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><code class=\"lang-js\">\/\/ \u57cb\u3081\u8fbc\u3080\u30c7\u30fc\u30bf\nvar data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];\n\n\/\/ \u66f8\u304d\u8fbc\u307f\u7528\u30d0\u30c3\u30d5\u30a1\nvar implanted = new Uint8Array(png.length + data.length + 12);<\/code><\/pre>\n<h3>\u30b7\u30b0\u30cd\u30c1\u30e3\u306e\u30c1\u30a7\u30c3\u30af<\/h3>\n<p>\u307e\u305a\u3001\u30d5\u30a1\u30a4\u30eb\u5148\u982d\u306e PNG \u30b7\u30b0\u30cd\u30c1\u30e3\u304c\u4e00\u81f4\u3057\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002<br \/>\n\u3053\u308c\u306b\u3088\u3063\u3066 PNG \u30d5\u30a1\u30a4\u30eb\u304b\u3069\u3046\u304b\u3092\u7c21\u5358\u306b\u5224\u5225\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"lang-js\">var Signature = String.fromCharCode(137, 80, 78, 71, 13, 10, 26, 10);\n\nif (String.fromCharCode.apply(null, png.subarray(rpos, rpos += 8)) !== Signature) {\n    throw new Error('invalid signature');\n}<\/code><\/pre>\n<h3>PNG \u306e\u30c1\u30e3\u30f3\u30af\u3092\u63a2\u7d22\u3059\u308b<\/h3>\n<p>\u30b7\u30b0\u30cd\u30c1\u30e3\u306e\u78ba\u8a8d\u304c\u6e08\u3093\u3060\u3089\u3001\u3053\u3053\u304b\u3089\u306f\u30c1\u30e3\u30f3\u30af\u304c\u9023\u7d9a\u3057\u3066\u914d\u7f6e\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\n\u5fc5\u8981\u306e\u306a\u3044\u30c1\u30e3\u30f3\u30af\u306f\u8aad\u307f\u98db\u3070\u3059\u4e8b\u3067\u9ad8\u901f\u306b IDAT \u30c1\u30e3\u30f3\u30af\u3092\u63a2\u3059\u4e8b\u304c\u51fa\u6765\u307e\u3059\u3002<br \/>\n\u524d\u8ff0\u306e\u30b7\u30b0\u30cd\u30c1\u30e3\u306e\u30c1\u30a7\u30c3\u30af\u3068\u307e\u3068\u3081\u3066 function \u306b\u3057\u3066\u304a\u304f\u3068\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<pre><code class=\"lang-js\">function process(png, type, handler) {\n    var dataLength;\n    var chunkType;\n    var nextChunkPos;\n    var Signature = String.fromCharCode(137, 80, 78, 71, 13, 10, 26, 10);\n    var rpos = 0;\n    \n    \/\/ \u30b7\u30b0\u30cd\u30c1\u30e3\u306e\u78ba\u8a8d\n    if (String.fromCharCode.apply(null, png.subarray(rpos, rpos += 8)) !== Signature) {\n        throw new Error('invalid signature');\n    }\n    \n    \/\/ \u30c1\u30e3\u30f3\u30af\u306e\u63a2\u7d22\n    while (rpos < png.length) {\n        dataLength = (\n            (png[rpos++] << 24) |\n            (png[rpos++] << 16) |\n            (png[rpos++] <<  8) |\n            (png[rpos++]      )\n        ) >>> 0;\n    \n        nextChunkPos = rpos + dataLength + 8;\n    \n        chunkType = String.fromCharCode.apply(null, png.subarray(rpos, rpos += 4));\n        \n        if (chunkType === type) {\n            return handler(png, rpos, dataLength);\n        }\n        \n        rpos = nextChunkPos;\n    }\n}\n\nprocess(png, 'IDAT', function(png, rpos, length) {\n    \/\/ rpos - 8 = \u30c1\u30e3\u30f3\u30af\u306e\u958b\u59cb\u4f4d\u7f6e\n    insertHogeChunk(implanted, data, png, rpos - 8);\n});<\/code><\/pre>\n<p><code>insertHogeChunk<\/code> \u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u8eab\u306f\u5f8c\u3067\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<h3>\u30c1\u30e3\u30f3\u30af\u306e\u4f5c\u6210<\/h3>\n<p>IDAT \u30c1\u30e3\u30f3\u30af\u3092\u898b\u3064\u3051\u305f\u3089\u3001\u305d\u306e\u76f4\u524d\u306b hoGe \u30c1\u30e3\u30f3\u30af\u3092\u57cb\u3081\u8fbc\u3080\u306e\u3067\u3001\u57cb\u3081\u8fbc\u3080\u30c1\u30e3\u30f3\u30af\u3092\u4f5c\u6210\u3059\u308b function \u3092\u4f5c\u3063\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"lang-js\">function createHogeChunk(data) {\n    var dataLength = data.length;\n    var chunk = new Uint8Array(4 + 4 + dataLength + 4);\n    var type = [104, 111,  71, 101];\n    var crc;\n    var pos = 0;\n    var i;\n    \n    \/\/ length\n    chunk[pos++] = (dataLength >> 24) & 0xff;\n    chunk[pos++] = (dataLength >> 16) & 0xff;\n    chunk[pos++] = (dataLength >>  8) & 0xff;\n    chunk[pos++] = (dataLength      ) & 0xff;\n    \n    \/\/ type\n    chunk[pos++] = type[0];\n    chunk[pos++] = type[1];\n    chunk[pos++] = type[2];\n    chunk[pos++] = type[3];\n    \n    \/\/ data\n    for (i = 0; i < dataLength; ++i) {\n        chunk[pos++] = data[i];\n    }\n    \n    \/\/crc\n    crc = Zlib.CRC32.calc(type);\n    crc = Zlib.CRC32.update(data, crc);\n    chunk[pos++] = (crc >> 24) & 0xff;\n    chunk[pos++] = (crc >> 16) & 0xff;\n    chunk[pos++] = (crc >>  8) & 0xff;\n    chunk[pos++] = (crc      ) & 0xff;\n    \n    return chunk;\n}<\/code><\/pre>\n<h3>\u4f5c\u6210\u3057\u305f\u30c1\u30e3\u30f3\u30af\u306e\u633f\u5165<\/h3>\n<p>IDAT \u30c1\u30e3\u30f3\u30af\u306e\u524d\u306b\u5148\u307b\u3069\u306e function \u3092\u3064\u304b\u3063\u3066 hoGe \u30c1\u30e3\u30f3\u30af\u3092\u633f\u5165\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"lang-js\">function insertHogeChunk(implanted, data, png, rpos) {\n    var chunk = createHogeChunk(data);\n    var pos = 0;\n    \n    \/\/ IDAT \u30c1\u30e3\u30f3\u30af\u306e\u524d\u307e\u3067\u30b3\u30d4\u30fc\n    implanted.set(png.subarray(0, rpos), pos);\n    pos += rpos;\n    \n    \/\/ hoGe \u30c1\u30e3\u30f3\u30af\u3092\u30b3\u30d4\u30fc\n    implanted.set(chunk, pos);\n    pos += chunk.length;\n    \n    \/\/ IDAT \u30c1\u30e3\u30f3\u30af\u4ee5\u964d\u3092\u30b3\u30d4\u30fc\n    implanted.set(png.subarray(rpos), pos);\n    \n    return implanted;\n}<\/code><\/pre>\n<h3>Base64 \u30a8\u30f3\u30b3\u30fc\u30c9<\/h3>\n<p>\u3053\u3053\u307e\u3067\u3067\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30c1\u30e3\u30f3\u30af\u3092\u57cb\u3081\u8fbc\u3093\u3060 PNG \u306e\u4f5c\u6210\u306f\u3067\u304d\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001<br \/>\nCanvas#toDataURL \u304c DataURL \u3092\u8fd4\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u5f62\u5f0f\u3092\u3042\u308f\u305b\u307e\u3059\u3002<br \/>\nBase64 \u30c7\u30b3\u30fc\u30c9 \u3068\u304a\u306a\u3058\u3088\u3046\u306b window.btoa \u304c\u4f7f\u3048\u308b\u5834\u5408\u306f\u305d\u3061\u3089\u3092\u3001\u4f7f\u3048\u306a\u3044\u5834\u5408\u306f base64.js \u306a\u3069\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre><code class=\"lang-js\">\/\/ Uint8Array \u304b\u3089 bytestring \u306b\u5909\u63db\nvar implantedString = \"\";\nfor (i = 0, il = implanted.length; i < il; ++i) {\n    implantedString += String.fromCharCode(implanted[i]);\n}\n\n\/\/ Base64 \u306b\u5909\u63db\nvar implantedBase64 = window.btoa(implantedString);<\/code><\/pre>\n<h3>DataURL \u306e\u4f5c\u6210<\/h3>\n<p>Base64 \u306b\u5909\u63db\u3057\u305f\u306e\u3067\u3042\u3068\u306f DataURL \u5f62\u5f0f\u306b\u3059\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<pre><code class=\"lang-js\">var implantedDataURL = 'data:image\/png;base64,' + implantedBase64;<\/code><\/pre>\n<h3>\u30c7\u30fc\u30bf\u306e\u53d6\u308a\u51fa\u3057<\/h3>\n<p>\u57fa\u672c\u7684\u306b\u306f\u633f\u5165\u6642\u3068\u540c\u3058\u3088\u3046\u306a\u51e6\u7406\u3067 hoGe \u30c1\u30e3\u30f3\u30af\u3092\u63a2\u3057\u3066\u30c7\u30fc\u30bf\u90e8\u5206\u3092\u629c\u304d\u51fa\u3059\u3060\u3051\u3067\u3059\u3002<br \/>\n\u57cb\u3081\u8fbc\u307f\u6642\u306b\u4f7f\u3063\u305f <code>process()<\/code> \u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre><code class=\"lang-js\">var extractedData = process(implanted, \"hoGe\", function(png, rpos, length) {\n    return png.subarray(rpos, rpos += length);\n});<\/code><\/pre>\n<h2>\u7d42\u308f\u308a\u306b<\/h2>\n<p>\u307f\u306a\u3055\u3093\u3069\u3093\u3069\u3093\u753b\u50cf\u306b\u4f59\u8a08\u306a\u60c5\u5831\u3092\u57cb\u3081\u8fbc\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n\u660e\u65e5\u306f\u77f3\u5ddd\u3055\u3093\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001Multimedia Engineering Team \u306e\u3044\u307e\u3084\u3067\u3059\u3002 Advent Calendar 8\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059\u304c\u3001\u7279\u306b\u305d\u3046\u3044\u3046\u306e\u3068\u306f\u95a2\u4fc2\u306a\u304f\u597d\u304d\u306a\u3053\u3068\u3092\u66f8\u304d\u307e\u3059\u3002 \u306f\u3058\u3081\u306b Canvas \u3067\u8272\u3005\u3084\u3063 [&hellip;]<\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[54,32,27],"class_list":["post-8594","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","tag-advent-calendar","tag-javascript","tag-png"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/posts\/8594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/comments?post=8594"}],"version-history":[{"count":3,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/posts\/8594\/revisions"}],"predecessor-version":[{"id":9649,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/posts\/8594\/revisions\/9649"}],"wp:attachment":[{"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/media?parent=8594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/categories?post=8594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labs.gree.jp\/blog\/wp-json\/wp\/v2\/tags?post=8594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}