1
0
Fork 0
forked from 0x2620/oxjs

move image encoding functions to image module

This commit is contained in:
rlx 2011-09-08 01:53:19 +00:00
commit 842d536dc8
4 changed files with 416 additions and 147 deletions

View file

@ -309,12 +309,15 @@ Ox.flatten = function(arr) {
Ox.merge <f> Merges an array with one or more other arrays
> Ox.merge([1], [2, 3, 2], [1])
[1, 2, 3, 2, 1]
> Ox.merge(1, [2, 3, 2], 1)
[1, 2, 3, 2, 1]
@*/
Ox.merge = function(arr) {
arr = Ox.isArray(arr) ? arr : [arr];
Ox.forEach(Array.prototype.slice.call(arguments, 1), function(arg) {
Ox.forEach(arg, function(val) {
Ox.isArray(arg) ? Ox.forEach(arg, function(val) {
arr.push(val);
});
}) : arr.push(arg);
});
return arr;
};
@ -2082,14 +2085,15 @@ Ox.element = function(str) {
head, tail and chunk names are removed.
(str) -> <s> The encoded string
str <s> The string to be encoded
# Test with: Ox.decodeDeflate(Ox.encodeDeflate('foo'), alert)
@*/
Ox.encodeDeflate = function(str) {
Ox.encodeDeflate = function(str, callback) {
// Make sure we can encode the full unicode range of characters.
str = Ox.encodeUTF8(str);
// We can only safely write to RGB, so we need 1 pixel for 3 bytes.
var len = str.length, c = Ox.canvas(Math.ceil((4 + len) / 3), 1),
data = '', idat, ihdr;
data = '', idat;
// Prefix the string with its length, left-padded with 0-bytes to
// length of 4 bytes, and right-pad the result with non-0-bytes to a
// length that is a multiple of 3.
@ -2105,7 +2109,7 @@ Ox.element = function(str) {
// The first 16 and the last 12 bytes of a PNG are always the same and
// can be discarded, the first 17 remaining bytes are part of the IHDR
// chunk, and the rest are IDAT chunks.
ihdr = Ox.sub(str, 16, 33); idat = Ox.sub(str, 33, -12);
data = Ox.sub(str, 16, 33); idat = Ox.sub(str, 33, -12);
while (idat) {
// Each IDAT chunk consists of 4 bytes length, 4 bytes "IDAT" and
// length bytes data, so we can optimize by removing each "IDAT".
@ -2113,7 +2117,8 @@ Ox.element = function(str) {
data += len + idat.substr(8, 12 + (len = Ox.decodeBase256(len)));
idat = idat.substr(12 + len);
}
return ihdr + data;
callback && callback(data);
return data;
}
/*@
@ -2127,7 +2132,6 @@ Ox.element = function(str) {
str <s> The string to be decoded
callback <f> Callback function
str <s> The decoded string
# Test with: Ox.decodeDeflate(Ox.encodeDeflate('foo'), alert)
@*/
Ox.decodeDeflate = function(str, callback) {