Created
January 5, 2012 10:04
-
-
Save moriyoshi/1564530 to your computer and use it in GitHub Desktop.
This file contains 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
function declitimg(img) { | |
var cv = document.createElement('canvas'); | |
cv.width = img.width; | |
cv.height = img.height; | |
var ctx = cv.getContext('2d'); | |
ctx.drawImage(img, 0, 0); | |
var imd = ctx.getImageData(0, 0, img.width, img.height); | |
var s = ''; | |
for (var i = 0; i < imd.data.length && imd.data[i] !== 0;) { | |
var c1 = imd.data[i++]; | |
if (i % 4 == 3) | |
i++; | |
if (c1 < 0x80) { | |
s += String.fromCharCode(c1); | |
} else if (c1 < 0xc2) { | |
throw "Invalid byte sequence"; | |
} else if (c1 < 0xe0) { | |
if (i >= img.data.length) | |
throw "Invalid byte sequence"; | |
var c2 = imd.data[i++]; | |
if (i % 4 == 3) | |
i++; | |
if (c2 < 0x80 || c2 >= 0xc0) | |
throw "Invalid byte sequence"; | |
s += String.fromCharCode(((c1 & 0x1f) << 6) | (c2 & 0x3f)); | |
} else if (c1 < 0xf0) { | |
if (i >= imd.data.length) | |
throw "Invalid byte sequence"; | |
var c2 = imd.data[i++]; | |
if (i % 4 == 3) | |
i++; | |
if (c2 < 0x80 || c2 >= 0xc0) | |
throw "Invalid byte sequence"; | |
if (i >= imd.data.length) | |
throw "Invalid byte sequence"; | |
var c3 = imd.data[i++]; | |
if (i % 4 == 3) | |
i++; | |
if (c3 < 0x80 || c3 >= 0xc0) | |
throw "Invalid byte sequence"; | |
s += String.fromCharCode(((c1 & 0x0f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f)); | |
} else if (c1 < 0xf8) { | |
if (i >= imd.data.length) | |
throw "Invalid byte sequence"; | |
var c2 = imd.data[i++]; | |
if (i % 4 == 3) | |
i++; | |
if (c2 < 0x80 || c2 >= 0xc0) | |
throw "Invalid byte sequence"; | |
if (i >= imd.data.length) | |
throw "Invalid byte sequence"; | |
var c3 = imd.data[i++]; | |
if (i % 4 == 3) | |
i++; | |
if (c3 < 0x80 || c3 >= 0xc0) | |
throw "Invalid byte sequence"; | |
if (++i >= imd.data.length) | |
throw "Invalid byte sequence"; | |
var c4 = imd.data[i++]; | |
if (i % 4 == 3) | |
i++; | |
if (c4 < 0x80 || c4 >= 0xc0) | |
throw "Invalid byte sequence"; | |
s += String.fromCharCode(((c1 & 0x07) << 18) | ((c2 & 0x3f) << 12) | ((c3 & 0x3f) << 6) | (c4 & 0x3f)); | |
} else { | |
throw "Invalid byte sequence"; | |
} | |
} | |
return s; | |
} |
This file contains 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
#!/usr/bin/env python | |
# encoding: utf-8 | |
from PIL import Image | |
from cStringIO import StringIO | |
from base64 import b64encode | |
def encode(s): | |
# convert str to octets if it's Unicode | |
if isinstance(s, unicode): | |
s = s.encode('UTF-8') | |
# pad it | |
s += "\x00" * (2 - (len(s) + 2) % 3) | |
# create an Image object from the buffer | |
im = Image.frombuffer('RGB', (len(s) / 4, 1), s, 'raw', 'RGB', 0, 1) | |
out = StringIO() | |
im.save(out, 'png', gamma=0.) | |
return 'data:image/png;base64,' + b64encode(out.getvalue()) | |
print encode(u'テスト') |
This file contains 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
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<script type="text/javascript" src="declitimg.js"></script> | |
<script type="text/javascript"> | |
var im = new Image(); | |
im.onload = function() { | |
var out = document.getElementById('out'); | |
out.appendChild(document.createTextNode(declitimg(im))); | |
}; | |
im.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAABCAIAAACUgoPjAAAAEklEQVR4nGN83NzG8N+YgfE8ABZpA/Cma4j9AAAAAElFTkSuQmCC'; | |
</script> | |
</head> | |
<body> | |
<div id="out"> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment