Javascript programs

1) Write a JavaScript program that has a function named swapRedGreen with one parameter pixel. This function should swap the red and green values of the pixel. Pick an image, print the image, then apply swapRedGreen to every pixel in the image, and print the new image. The choice of your image is important. For some images you may not notice any change. Think about what type of image you should use for testing your function.


function swapRedGreen (pixel){
    var swap1 = pix.getRed();
    var swap2 = pix.getGreen();
    pix.setRed(swap2);
    pix.setGreen(swap1);
}

var image = new SimpleImage("palm-and-beach.png");
print(image);

for(var pix of image.values()){
    pix=swapRedGreen(pix);
}
print(image);

2) Write a JavaScript program to make an image have more red in it, by adding a given value to the red, making sure it doesn’t go over 255. Your program should have a function called moreRed with two parameters, a pixel and a value to increase the red by. Run your program on an image to see it get redder.


function moreRed (pixel,increasevalue){
    var currentRed = pix.getRed();
    var total = currentRed + increasevalue;
    if(total <= 255){
        pix.setRed(total);
        return true;
    }
    else
        return false;
}

var image = new SimpleImage("duke_blue_devil.png");
print(image);

for(var pix of image.values()){
    var a = moreRed(pix,100);
}
print(image);


3)Write the complete JavaScript program to put the border around a picture, and include the following functions that are included from the lesson. You should be able to write these functions without looking at the code from the lesson. Be sure to print the image so you can see it and run the program with different border values. a) function setBlack(pixel) – This function has a parameter pixel that represents a single pixel, and returns a pixel that has been changed to be the color black. b) function pixelOnEdge(pixel, image, borderWidth) – This function has three parameters where pixel is a single pixel, image is the complete image, and borderWidth is an integer specifying the thickness of the borders. This function returns true if the pixel’s location is within borderWidth of any of the four borders, and thus on the border. Otherwise it returns false.


function setBlack(pixel){
    pixel.setRed(0);
    pixel.setGreen(0);
    pixel.setBlue(0);
    
    return pixel;
}

function pixelOnEdge(pixel, image, borderWidth) {
    var x = pixel.getX();
    var y = pixel.getY();
    
    if(x <= borderWidth) {return true;}
    else if(y <= borderWidth) {return true;} 
    else if(x >= image.getWidth()-borderWidth) {return true;}
    else if(y >= image.getHeight()-borderWidth) {return true;}
    else return false;
}

var image = new SimpleImage("duke_blue_devil.png");
print(image);

for(var pix of image.values()){
    if(pixelOnEdge(pix, image, 10)){
        setBlack(pix);
    }
}
print(image);

4)Now modify the border program to specify two thicknesses, one for the vertical borders and one for the horizontal borders. You should write the two boolean functions shown below. Be sure to print the image and run your program with different border values for the horizontal and vertical edges. a) function pixelOnVerticalEdge(pixel, image, borderWidth)—This function has three parameters where pixel is a single pixel, image is the complete image, and borderWidth is an integer specifying the thickness of the vertical borders. This function returns true if the pixel’s location is within borderWidth of any of the two vertical borders, and thus on the border. Otherwise it returns false. b) function pixelOnHorizontalEdge(pixel, image, borderWidth)—This function has three parameters where pixel is a single pixel, image is the complete image, and borderWidth is an integer specifying the thickness of the horizontal borders. This function returns true if the pixel’s location is within borderWidth of any of the two horizontal borders, and thus on the border. Otherwise it returns false.


function setBlack(pixel){
    pixel.setRed(0);
    pixel.setGreen(0);
    pixel.setBlue(0);

    return pixel;
}
function pixelOnVerticalEdge(pixel, image, borderWidth) {
    var x = pixel.getX();
    if(x <= borderWidth) {return true;}   
    else if(x >= image.getWidth()-borderWidth) {return true;}
    else return false;
}
function pixelOnHorizontalEdge(pixel, image, borderWidth) {
    var y = pixel.getY();
    if(y <= borderWidth) {return true;}  
    else if(y >= image.getHeight()-borderWidth) {return true;}
    else return false;
}

var image = new SimpleImage("duke_blue_devil.png");
print(image);

for(var pix of image.values()){
    if(pixelOnVerticalEdge(pix, image, 10)){
        setBlack(pix);
    }
    else if (pixelOnHorizontalEdge(pix, image, 20)){
        setBlack(pix);
    }
    else 
        continue;
}
print(image);

 
5)Program to crop Image


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

var givenImage = new SimpleImage("drewRobert.png");
var croppedImage = crop(givenImage,1500,1000);
print(croppedImage);
print(givenImage);

5)Program to enlarge Image


var inImage = new SimpleImage("chapel.png");
var outImage = new SimpleImage(inImage.getWidth()*2,inImage.getHeight()*2);

for(var pix of outImage.values()){
    var x = Math.floor(pix.getX()/2);
    var y = Math.floor(pix.getY()/2);
    
    var inPixel = inImage.getPixel(x,y);
    
    pix.setRed(inPixel.getRed());
    pix.setGreen(inPixel.getGreen());
    pix.setBlue(inPixel.getBlue());
}
print(inImage);
print(outImage);
Advertisements

2 thoughts on “Javascript programs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s