remove png functions, better versions in Ox.Image
This commit is contained in:
parent
3f35003ac9
commit
6fcca8f3a9
1 changed files with 1 additions and 96 deletions
|
@ -322,101 +322,6 @@
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
|
||||||
Ox.encodePNG <f> Encodes a string into an image, returns a new image
|
|
||||||
The string is compressed with deflate (by proxy of canvas), prefixed
|
|
||||||
with its length (four bytes), and encoded bitwise into the red, green
|
|
||||||
and blue bytes of all fully opaque pixels of the image, by flipping, if
|
|
||||||
necessary, the least significant bit, so that for every byte, the total
|
|
||||||
number of bits set to to 1, modulo 2, is the bit that we are encoding.
|
|
||||||
(img, src) -> <e> An image into which the string has been encoded
|
|
||||||
img <e> Any JavaScript PNG image object
|
|
||||||
str <s> The string to be encoded
|
|
||||||
@*/
|
|
||||||
// fixme: remove, exists in image module
|
|
||||||
Ox.encodePNG = function(img, str) {
|
|
||||||
var c = Ox.canvas(img), i = 0;
|
|
||||||
// Compress the string
|
|
||||||
str = Ox.encodeDeflate(str);
|
|
||||||
// Prefix the string with its length, as a four-byte value
|
|
||||||
str = Ox.pad(Ox.encodeBase256(str.length), 4, Ox.char(0)) + str;
|
|
||||||
// Create an array of bit values
|
|
||||||
Ox.forEach(Ox.flatten(Ox.map(str, function(chr) {
|
|
||||||
return Ox.map(Ox.range(8), function(i) {
|
|
||||||
return chr.charCodeAt(0) >> 7 - i & 1;
|
|
||||||
});
|
|
||||||
})), function(bit) {
|
|
||||||
// Skip all pixels that are not fully opaque
|
|
||||||
while (i < c.data.length && i % 4 == 0 && c.data[i + 3] < 255) {
|
|
||||||
i += 4;
|
|
||||||
}
|
|
||||||
if (i == c.data.length) {
|
|
||||||
throw new RangeError('PNG codec can\'t encode data');
|
|
||||||
}
|
|
||||||
// If the number of bits set to one, modulo 2 is equal to the bit,
|
|
||||||
// do nothing, otherwise, flip the least significant bit.
|
|
||||||
c.data[i] += c.data[i].toString(2).replace(/0/g, '').length % 2
|
|
||||||
== bit ? 0 : c.data[i] % 2 ? -1 : 1;
|
|
||||||
i++;
|
|
||||||
});
|
|
||||||
c.context.putImageData(c.imageData, 0, 0);
|
|
||||||
img = new Image();
|
|
||||||
img.src = c.canvas.toDataURL();
|
|
||||||
return img;
|
|
||||||
/*
|
|
||||||
wishlist:
|
|
||||||
- only use deflate if it actually shortens the message
|
|
||||||
- encode a decoy message into the least significant bit
|
|
||||||
(and flip the second least significant bit, if at all)
|
|
||||||
- write an extra png chunk containing some key
|
|
||||||
*/
|
|
||||||
};
|
|
||||||
|
|
||||||
/*@
|
|
||||||
Ox.decodePNG <f> Decodes an image, returns a string
|
|
||||||
For every red, green and blue byte of every fully opaque pixel of the
|
|
||||||
image, one bit, namely the number of bits of the byte set to one, modulo
|
|
||||||
2, is being read, the result being the string, prefixed with its length
|
|
||||||
(four bytes), which is decompressed with deflate (by proxy of canvas).
|
|
||||||
(img, callback) -> <u> undefined
|
|
||||||
img <e> The image into which the string has been encoded
|
|
||||||
callback <f> Callback function
|
|
||||||
str <s> The decoded string
|
|
||||||
@*/
|
|
||||||
|
|
||||||
Ox.decodePNG = function(img, callback) {
|
|
||||||
var bits = '', data = Ox.canvas(img).data, flag = false, i = 0,
|
|
||||||
len = 4, str = '';
|
|
||||||
while (len) {
|
|
||||||
// Skip all pixels that are not fully opaque
|
|
||||||
while (i < data.length && i % 4 == 0 && data[i + 3] < 255) {
|
|
||||||
i += 4;
|
|
||||||
}
|
|
||||||
if (i == data.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Read the number of bits set to one, modulo 2
|
|
||||||
bits += data[i].toString(2).replace(/0/g, '').length % 2;
|
|
||||||
if (++i % 8 == 0) {
|
|
||||||
// Every 8 bits, add one byte
|
|
||||||
str += Ox.char(parseInt(bits, 2));
|
|
||||||
bits = '';
|
|
||||||
// When length reaches 0 for the first time,
|
|
||||||
// decode the string and treat it as the new length
|
|
||||||
if (--len == 0 && !flag) {
|
|
||||||
flag = true;
|
|
||||||
len = Ox.decodeBase256(str);
|
|
||||||
str = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Ox.decodeDeflate(str, callback);
|
|
||||||
} catch (e) {
|
|
||||||
throw new RangeError('PNG codec can\'t decode image');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
Ox.encodeUTF8 <f> Encodes a string as UTF-8
|
Ox.encodeUTF8 <f> Encodes a string as UTF-8
|
||||||
see http://en.wikipedia.org/wiki/UTF-8
|
see http://en.wikipedia.org/wiki/UTF-8
|
||||||
|
@ -496,4 +401,4 @@
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in a new issue