From 6fcca8f3a9e7e3c5be86d57b2e360145a744811f Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 22 May 2012 19:49:29 +0200 Subject: [PATCH] remove png functions, better versions in Ox.Image --- source/Ox/js/Encoding.js | 97 +--------------------------------------- 1 file changed, 1 insertion(+), 96 deletions(-) diff --git a/source/Ox/js/Encoding.js b/source/Ox/js/Encoding.js index 276894c5..d237e306 100644 --- a/source/Ox/js/Encoding.js +++ b/source/Ox/js/Encoding.js @@ -322,101 +322,6 @@ ); }; - /*@ - Ox.encodePNG 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) -> An image into which the string has been encoded - img Any JavaScript PNG image object - str 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 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) -> undefined - img The image into which the string has been encoded - callback Callback function - str 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 Encodes a string as UTF-8 see http://en.wikipedia.org/wiki/UTF-8 @@ -496,4 +401,4 @@ return ret; }; -})(); \ No newline at end of file +})();