58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
onmessage = function(e) {
|
|
if (e.data.message) {
|
|
|
|
} else {
|
|
|
|
}
|
|
}
|
|
|
|
function encode() {
|
|
str = Ox.encodeUTF8(str);
|
|
var c = Ox.canvas(img), len = str.length;
|
|
if (len > maxLength(img.width, img.height)) {
|
|
throw new RangeError("PNG codec can't encode " + len +
|
|
" bytes into " + (img.width * img.height) + " pixels");
|
|
}
|
|
len = Ox.pad(Ox.encodeBase256(len), 4, Ox.char(0));
|
|
Ox.each(Ox.map(len + str, function(byte) {
|
|
return Ox.map(new Array(8), function(v, i) {
|
|
return byte.charCodeAt(0) >> 7 - i & 1;
|
|
}).join("");
|
|
}).join(""), function(i, bit) {
|
|
var index, byte = c.data[index = parseInt(i * 4/3)];
|
|
c.data[index] = bit == xor(byte) ? byte :
|
|
byte & 254 | !(byte & 1);
|
|
});
|
|
c.context.putImageData(c.imageData, 0, 0);
|
|
postMessage(c.canvas.toDataURL());
|
|
}
|
|
|
|
function decode() {
|
|
var c = Ox.canvas(img), bits = "", str = "", i = 0; len = 4, flag = false;
|
|
do {
|
|
bits += xor(c.data[parseInt(i * 4/3)]);
|
|
if (++i % 8 == 0) {
|
|
str += Ox.char(parseInt(bits, 2));
|
|
bits = "";
|
|
len--;
|
|
if (len == 0 && !flag) {
|
|
if ((len = Ox.decodeBase256(str)) >
|
|
maxLength(img.width, img.height)) {
|
|
throwPNGError();
|
|
}
|
|
str = "";
|
|
flag = true;
|
|
}
|
|
}
|
|
} while (len);
|
|
try {
|
|
return Ox.decodeUTF8(str);
|
|
} catch(e) {
|
|
throwPNGError();
|
|
}
|
|
}
|
|
|
|
function throwError() {
|
|
throw new RangeError("PNG codec can't decode image");
|
|
}
|