Monday, 4 July 2016

Select two images to use. You will hide one image in another image. They most likely will be different sizes and in the next step we will crop both of them to be the same size. In order to hide one image inside of another image, the images need to be the exact same size. Write the function crop(image, width, height) that has three parameters: image is a complete image, and width and height are numbers. This function creates and returns a new image from the parameter image, with the new image having the width and height of the specified width and height parameters, thus cropping the picture by ignoring the pixels on the right side or bottom that are beyond the specified width or height of the picture.

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 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);

No comments:

Post a Comment