Thursday, 7 July 2016

EXTRACTION OF IMAGE code

// write your code here
function clearbits(colorval){
    //compute the same color value with the lowest bits zeroed
    var x = Math.floor(colorval/16)*16;
    return x;
}
function chop2hide(image){
    //for each pixel in the image
    for (var px of image.values()){
        //clear the lower values of red
        px.setRed(clearbits(px.getRed()));
        //clear the lower values of green
        px.setGreen(clearbits(px.getGreen()));
        //clear the lower values of blue
        px.setBlue(clearbits(px.getBlue()));
    }
    return image;
}
function shift(image){
    //for each pixel in an image
    for (var px of image.values()){
        //shift the red bits over
        px.setRed(px.getRed()/16);
        //shift the Green bits over
        px.setGreen(px.getGreen()/16);
        //shift the Blue bits over
        px.setBlue(px.getBlue()/16);
    }
    return image;
}
function open(imagetoopen){
    for (var px of imagetoopen.values()){
    var or = px.getRed();
    var og = px.getGreen();
    var ob = px.getBlue();
    px.setRed((or - (clearbits(or)))*16);
    px.setGreen((og - (clearbits(og)) )*16);
    px.setBlue((ob - (clearbits(ob)))*16);
    }

    return imagetoopen;
}  
function combine(start,Hide){
    //make a new image of the same size as SHOW (Call it Answer)
    var answer = new SimpleImage(start.getWidth() , start.getHeight());
    //for every pixel of new image
    for (var px of answer.values()){
        var x = px.getX();
        var y = px.getY();
        //get the pixel in the same place from show
        var startPixel = start.getPixel(x,y);
        //get the pixel in the same place from hide
        var hidePixel = hide.getPixel(x,y);
        //set the red pixel to the sum of the show pixel red + hide pixel red
        px.setRed(startPixel.getRed() + hidePixel.getRed());
        //set the Green pixel to the sum of the show pixel Green + hide pixel Green
        px.setGreen(startPixel.getGreen() + hidePixel.getGreen());
        //set the Blue pixel to the sum of the show pixel Blue + hide pixel Blue
        px.setBlue(startPixel.getBlue() + hidePixel.getBlue());
    }
    return answer;
}
var start = new SimpleImage("dinos.png");
var Hide = new SimpleImage("drewRobert.png");
start = chop2hide(start);
hide = shift(Hide);
var answer = combine (start,hide);

print (answer);
var undo =  open(answer);
print (undo);

No comments:

Post a Comment