Hide And Extract Image 2 bits





//Function returns chopped pixel color values
function pixchange(pixval){
    var x = Math.floor(pixval/4) * 4;
    return x;
}

//Code section chops bits of pixel colors of image that hides another image
function chop2hide(image){
    for(var px of image.values()){
        px.setRed(pixchange(px.getRed()));
        px.setGreen(pixchange(px.getGreen()));
        px.setBlue(pixchange(px.getBlue()));
    }
    return image;
}

//Code Section used to shift bits of pixel colors of image to be hidden
function shift(im){
  var nim = new SimpleImage(im.getWidth(),im.getHeight());
  for(var px of im.values()){
    var x = px.getX();
    var y = px.getY();
    var npx = nim.getPixel(x,y);
    npx.setRed(Math.floor(px.getRed()/64));
    npx.setGreen(Math.floor(px.getGreen()/64));
    npx.setBlue(Math.floor(px.getBlue()/64));
  }
  return nim;
}

//Function returns cropped images
function crop(image,w,h){
    var img = new SimpleImage(w,h);
    for (var pix of img.values()){
        var x = pix.getX();
        var y = pix.getY();
        var p = image.getPixel(x,y);
        pix.setRed(p.getRed());
        pix.setGreen(p.getGreen());
        pix.setBlue(p.getBlue());
    }
    return img;
}

//Code Section combines the 2 images into a single one
function combine(image1,image2){
    var nimg = new SimpleImage(image1.getWidth(),image1.getHeight());
    for (var pix of image1.values()){
        var x = pix.getX();
        var y = pix.getY();
        var p = image2.getPixel(x,y);
        var pixel = nimg.getPixel(x,y);
        pixel.setRed(pix.getRed()+p.getRed());
        pixel.setGreen(pix.getGreen()+p.getGreen());
        pixel.setBlue(pix.getBlue()+p.getBlue());
    }
    return nimg;
}

//Function returns extracted Image's pixel values
function pixExtracted(px, pixel){
    var num = pixel.getRed();
    px.setRed((num - ( Math.floor(num /4) * 4)) * 64);
    num = pixel.getGreen();
    px.setGreen((num - ( Math.floor(num /4) * 4)) * 64);
    num = pixel.getBlue();
    px.setBlue((num - ( Math.floor(num /4) * 4)) * 64);
    return px;
}

var start = new SimpleImage("eastereggs.jpg");
print(start);
var hide = new SimpleImage("csife.png");
print (hide);
//Cropping Images
var cropw = hide.getWidth();
if(start.getWidth() < cropw){
    cropw = start.getWidth();
}
var croph = hide.getHeight();
if(start.getHeight() < croph){
    croph = start.getHeight();
}
start = crop(start, cropw, croph);
hide = crop(hide, cropw, croph);

start = chop2hide(start);
hide = shift(hide);

var newImg = combine(start,hide);
print(newImg);

var outimage = new SimpleImage(newImg.getWidth(), newImg.getHeight());

for(var pixel of newImg.values()){
    var x = pixel.getX();
    var y = pixel.getY();
    var px = outimage.getPixel(x, y);
    px = pixExtracted(px, pixel);
    //Code Section shows how image will look after hidden image is extracted from it.
    pixel.setRed(pixel.getRed()-(px.getRed()/64));
    pixel.setGreen(pixel.getGreen()-(px.getGreen()/64));
    pixel.setBlue(pixel.getBlue()-(px.getBlue()/64));
}

//print extracted image
print(outimage);
//print remaining image after extraction
print(newImg);







Comments

Popular posts from this blog

Choose Your Social Media Channels Week 5 Quiz