【发布时间】:2016-03-15 13:22:06
【问题描述】:
这里我有一些代码可以创建一个隐藏另一个图像的图像。
我打印出一些 rgb 值来查看变化(在实现了函数 chopToHide 和函数 shift 之后,但我不明白这些数字是通过什么计算改变的)。这段代码是一个更大的程序的一部分,用于将一个图像隐藏在另一个图像中(隐写术)。
这是我工作的环境:http://www.dukelearntoprogram.com/course1/example/index.php
var start = new SimpleImage("astrachan.jpg");
var hide = new SimpleImage("duvall.jpg");
print (start);
print(hide);
print ("width and height of astrachan picture");
print (start.getHeight(), start.getWidth());
print ("width and height of duvall picture")
print (hide.getHeight(), hide.getWidth());
var cropWidth = start.getWidth();
if (hide.getWidth() < cropWidth) {
cropWidth = hide.getWidth();
}
var cropHeight = start.getHeight();
if (hide.getHeight() < cropHeight) {
cropHeight = hide.getHeight();
}
start = crop(start,cropWidth, cropHeight);
hide = crop(hide,cropWidth, cropHeight);
print(start);
print(hide);
function crop(image, width, height){
var n = new SimpleImage(width,height);
for(var p of image.values()){
var x = p.getX();
var y = p.getY();
if (x < width && y < height){
var np = n.getPixel(x,y);
np.setRed(p.getRed());
np.setBlue(p.getBlue());
np.setGreen(p.getGreen());
}
}
return n;
}
//print (start);
//print(hide);
print ("cropped width and height of astrachan picture");
print (start.getHeight(), start.getWidth());
print ("cropped width and height of duvall picture")
print (hide.getHeight(), hide.getWidth());
function chopToHide(image){
for(var px of image.values()){
px.setRed(pixchange(px.getRed()));
px.setGreen(pixchange(px.getGreen()));
px.setBlue(pixchange(px.getBlue()));
}
return image;
}
function pixchange(pixval){
var x = Math.floor(pixval/16) * 16;
return x;
}
function shift(oldImage){
var newImage = new SimpleImage(oldImage.getWidth(), oldImage.getHeight());
for(var oldPixel of oldImage.values()){
var x = oldPixel.getX();
var y = oldPixel.getY();
var newPixel = newImage.getPixel(x, y);
newPixel.setRed( Math.floor(oldPixel.getRed()/16) );
newPixel.setGreen( Math.floor(oldPixel.getGreen()/16) );
newPixel.setBlue( Math.floor(oldPixel.getBlue()/16) );
}
return newImage;
}
print("before applying the chopToHide function to the image start:");
for(i = 1; i <= 100; i+=20 ){
var pixel = start.getPixel(i, i+5);
print("pixel at (" + pixel.getX() + "," + pixel.getY() + ")-> R= " +
pixel.getRed() + " : G= " + pixel.getGreen() + " : B= " + pixel.getBlue() );
}
//print (start);
start = chopToHide(start);
print("After applying the chopToHide function to the image start:");
for(i = 1; i <= 100; i+=20 ){
var pixel = start.getPixel(i, i+5);
print("pixel at (" + pixel.getX() + "," + pixel.getY() + ")-> R= " +
pixel.getRed() + " : G= " + pixel.getGreen() + " : B= " + pixel.getBlue() );
}
//print (start);
//print (hide);
print("Before applying the shift function to the image hide:");
for(i = 1; i <= 100; i+=20 ){
var pixel = hide.getPixel(i, i+5);
print("pixel at (" + pixel.getX() + "," + pixel.getY() + ")-> R= " +
pixel.getRed() + " : G= " + pixel.getGreen() + " : B= " + pixel.getBlue() );
}
hide = shift(hide);
print("After applying the shift function to the image hide:");
for(i = 1; i <= 100; i+=20 ){
var pixel = hide.getPixel(i, i+5);
print("pixel at (" + pixel.getX() + "," + pixel.getY() + ")-> R= "
+ pixel.getRed() + " : G= " + pixel.getGreen() + " : B= " +
pixel.getBlue() );
}
【问题讨论】:
标签: javascript binary hex decimal steganography