Skip to content

Commit

Permalink
Fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
petereigenschink committed Sep 22, 2017
1 parent c304e34 commit 025bd9a
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
dist
docs
node_modules
.DS_Store
31 changes: 23 additions & 8 deletions build/steganography.js
@@ -1,5 +1,5 @@
/*
* steganography.js v1.0.2 2016-04-23
* steganography.js v1.0.3 2017-09-22
*
* Copyright (C) 2012 Peter Eigenschink (http://www.peter-eigenschink.at/)
* Dual-licensed under MIT and Beerware license.
Expand Down Expand Up @@ -59,10 +59,10 @@ var util = {
"loadImg": function(url) {
var image = new Image();
image.src = url;
while(image.hasOwnProperty('complete') && !image.complete) {}
return image;
}
};

Cover.prototype.config = {
"t": 3,
"threshold": 1,
Expand Down Expand Up @@ -94,8 +94,13 @@ Cover.prototype.getHidingCapacity = function(image, options) {
return t*width*height/codeUnitSize >> 0;
};
Cover.prototype.encode = function(message, image, options) {
// Handle image url
if(image.length) {
image = util.loadImg(image);
} else if(image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}

options = options || {};
Expand All @@ -108,7 +113,7 @@ Cover.prototype.encode = function(message, image, options) {
args = options.args || config.args,
messageDelimiter = options.messageDelimiter || config.messageDelimiter;

if(!t || t < 1 || t > 7) throw "Error: Parameter t = " + t + " is not valid: 0 < t < 8";
if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');

var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');
Expand Down Expand Up @@ -141,7 +146,10 @@ Cover.prototype.encode = function(message, image, options) {
dec = message.charCodeAt(i) || 0;
curOverlapping = (overlapping*i)%t;
if(curOverlapping > 0 && oldDec) {
// Mask for the new character, shifted with the count of overlapping bits
mask = Math.pow(2,t-curOverlapping) - 1;
// Mask for the old character, i.e. the t-curOverlapping bits on the right
// of that character
oldMask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2, -curOverlapping));
left = (dec & mask) << curOverlapping;
right = (oldDec & oldMask) >> (codeUnitSize - curOverlapping);
Expand Down Expand Up @@ -189,7 +197,7 @@ Cover.prototype.encode = function(message, image, options) {
}
for(i=offset*4; i<(offset+qS.length)*4 && i<data.length; i+=4)
data[i+3] = qS[(i/4)%threshold];

subOffset = qS.length;
}
// Write message-delimiter
Expand All @@ -203,23 +211,29 @@ Cover.prototype.encode = function(message, image, options) {

return shadowCanvas.toDataURL();
};

Cover.prototype.decode = function(image, options) {
// Handle image url
if(image.length) {
image = util.loadImg(image);
} else if(image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}

options = options || {};
var config = this.config;

var t = options.t || config.t,
threshold = options.threshold || config.threshold,
codeUnitSize = options.codeUnitSize || config.codeUnitSize,
prime = util.findNextPrime(Math.pow(2, t)),
args = options.args || config.args,
args = options.args || config.args,
messageCompleted = options.messageCompleted || config.messageCompleted;

if(!t || t < 1 || t > 7) throw "Error: Parameter t = " + t + " is not valid: 0 < t < 8";
if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');

var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');

Expand Down Expand Up @@ -306,5 +320,6 @@ Cover.prototype.decode = function(image, options) {

return message;
};

return new Cover();
});
4 changes: 2 additions & 2 deletions build/steganography.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -7,7 +7,7 @@
"type": "git",
"url": "https://github.com/petereigenschink/steganography.js"
},
"version": "1.0.2",
"version": "1.0.3",
"license": "MIT",
"devDependencies": {
"grunt": "0.4.5",
Expand Down
15 changes: 10 additions & 5 deletions src/decode.js
@@ -1,20 +1,25 @@
Cover.prototype.decode = function(image, options) {
// Handle image url
if(image.length) {
image = util.loadImg(image);
} else if(image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}

options = options || {};
var config = this.config;

var t = options.t || config.t,
threshold = options.threshold || config.threshold,
codeUnitSize = options.codeUnitSize || config.codeUnitSize,
prime = util.findNextPrime(Math.pow(2, t)),
args = options.args || config.args,
args = options.args || config.args,
messageCompleted = options.messageCompleted || config.messageCompleted;

if(!t || t < 1 || t > 7) throw "Error: Parameter t = " + t + " is not valid: 0 < t < 8";
if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');

var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');

Expand Down Expand Up @@ -100,4 +105,4 @@ Cover.prototype.decode = function(image, options) {
if(charCode !== 0) message += String.fromCharCode(charCode & mask);

return message;
};
};
14 changes: 11 additions & 3 deletions src/encode.js
@@ -1,6 +1,11 @@
Cover.prototype.encode = function(message, image, options) {
// Handle image url
if(image.length) {
image = util.loadImg(image);
} else if(image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}

options = options || {};
Expand All @@ -13,7 +18,7 @@ Cover.prototype.encode = function(message, image, options) {
args = options.args || config.args,
messageDelimiter = options.messageDelimiter || config.messageDelimiter;

if(!t || t < 1 || t > 7) throw "Error: Parameter t = " + t + " is not valid: 0 < t < 8";
if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');

var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');
Expand Down Expand Up @@ -46,7 +51,10 @@ Cover.prototype.encode = function(message, image, options) {
dec = message.charCodeAt(i) || 0;
curOverlapping = (overlapping*i)%t;
if(curOverlapping > 0 && oldDec) {
// Mask for the new character, shifted with the count of overlapping bits
mask = Math.pow(2,t-curOverlapping) - 1;
// Mask for the old character, i.e. the t-curOverlapping bits on the right
// of that character
oldMask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2, -curOverlapping));
left = (dec & mask) << curOverlapping;
right = (oldDec & oldMask) >> (codeUnitSize - curOverlapping);
Expand Down Expand Up @@ -94,7 +102,7 @@ Cover.prototype.encode = function(message, image, options) {
}
for(i=offset*4; i<(offset+qS.length)*4 && i<data.length; i+=4)
data[i+3] = qS[(i/4)%threshold];

subOffset = qS.length;
}
// Write message-delimiter
Expand All @@ -107,4 +115,4 @@ Cover.prototype.encode = function(message, image, options) {
shadowCtx.putImageData(imageData, 0, 0);

return shadowCanvas.toDataURL();
};
};
3 changes: 1 addition & 2 deletions src/util.js
Expand Up @@ -40,7 +40,6 @@ var util = {
"loadImg": function(url) {
var image = new Image();
image.src = url;
while(image.hasOwnProperty('complete') && !image.complete) {}
return image;
}
};
};
31 changes: 23 additions & 8 deletions test/js/steganography.js
@@ -1,5 +1,5 @@
/*
* steganography.js v1.0.2 2016-04-23
* steganography.js v1.0.3 2017-09-22
*
* Copyright (C) 2012 Peter Eigenschink (http://www.peter-eigenschink.at/)
* Dual-licensed under MIT and Beerware license.
Expand Down Expand Up @@ -59,10 +59,10 @@ var util = {
"loadImg": function(url) {
var image = new Image();
image.src = url;
while(image.hasOwnProperty('complete') && !image.complete) {}
return image;
}
};

Cover.prototype.config = {
"t": 3,
"threshold": 1,
Expand Down Expand Up @@ -94,8 +94,13 @@ Cover.prototype.getHidingCapacity = function(image, options) {
return t*width*height/codeUnitSize >> 0;
};
Cover.prototype.encode = function(message, image, options) {
// Handle image url
if(image.length) {
image = util.loadImg(image);
} else if(image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}

options = options || {};
Expand All @@ -108,7 +113,7 @@ Cover.prototype.encode = function(message, image, options) {
args = options.args || config.args,
messageDelimiter = options.messageDelimiter || config.messageDelimiter;

if(!t || t < 1 || t > 7) throw "Error: Parameter t = " + t + " is not valid: 0 < t < 8";
if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');

var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');
Expand Down Expand Up @@ -141,7 +146,10 @@ Cover.prototype.encode = function(message, image, options) {
dec = message.charCodeAt(i) || 0;
curOverlapping = (overlapping*i)%t;
if(curOverlapping > 0 && oldDec) {
// Mask for the new character, shifted with the count of overlapping bits
mask = Math.pow(2,t-curOverlapping) - 1;
// Mask for the old character, i.e. the t-curOverlapping bits on the right
// of that character
oldMask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2, -curOverlapping));
left = (dec & mask) << curOverlapping;
right = (oldDec & oldMask) >> (codeUnitSize - curOverlapping);
Expand Down Expand Up @@ -189,7 +197,7 @@ Cover.prototype.encode = function(message, image, options) {
}
for(i=offset*4; i<(offset+qS.length)*4 && i<data.length; i+=4)
data[i+3] = qS[(i/4)%threshold];

subOffset = qS.length;
}
// Write message-delimiter
Expand All @@ -203,23 +211,29 @@ Cover.prototype.encode = function(message, image, options) {

return shadowCanvas.toDataURL();
};

Cover.prototype.decode = function(image, options) {
// Handle image url
if(image.length) {
image = util.loadImg(image);
} else if(image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}

options = options || {};
var config = this.config;

var t = options.t || config.t,
threshold = options.threshold || config.threshold,
codeUnitSize = options.codeUnitSize || config.codeUnitSize,
prime = util.findNextPrime(Math.pow(2, t)),
args = options.args || config.args,
args = options.args || config.args,
messageCompleted = options.messageCompleted || config.messageCompleted;

if(!t || t < 1 || t > 7) throw "Error: Parameter t = " + t + " is not valid: 0 < t < 8";
if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');

var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');

Expand Down Expand Up @@ -306,5 +320,6 @@ Cover.prototype.decode = function(image, options) {

return message;
};

return new Cover();
});

0 comments on commit 025bd9a

Please sign in to comment.