Showing posts with label Programming and the Web for Beginners. Show all posts
Showing posts with label Programming and the Web for Beginners. Show all posts

http://www.dukelearntoprogram.com/course1/example/index.php

// write your code here
//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("rsz_canvas6.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);



<p>The following picture was given with a hidden message to extract:</p>
<center><img src = "http://i36.photobucket.com/albums/e42/Samksha/hiltonHiding2bitMessage_zpsze7d7x8e.png" /></center>
<p>After extraction, this is what the hidden message was!</p>
<center><img src = "http://i36.photobucket.com/albums/e42/Samksha/Part%203_zpsmdhehkly.png"/></center>

<h3> Algorithm</h3>
<p> The algorithm used for the extraction is similar to the one used for extraction in the first part.</p>

<h3> Code</h3>

<pre>
function pchange(pixval)
{
    var x = (pixval-Math.floor(pixval/4)*4)*64;
    return x;
}

function extract(image)
{
    for(var pixel of image.values())
    {
        pixel.setRed(pchange(pixel.getRed()));
        pixel.setGreen(pchange(pixel.getGreen()));
        pixel.setBlue(pchange(pixel.getBlue()));
    }
    return image;
}

var combinedimage = new SimpleImage("hiltonHiding2bitMessage.png");
hiddenimage = extract(combinedimage);

print(hiddenimage);
</pre>
  </html>

nice one

 Steganography

Original Images

This is the image that will be used to hide the second image!
And this is the image that will be hidden in the first image!
Sources for the images:
https://www.hdwallpapers.net/abstract/colorful-diamond-wallpaper-674.htm
https://www.hdwallpapers.net/abstract/dark-wood-texture-wallpaper-383.htm

After the Hiding

This is what the image looks like with the hidden value:
And this is what the hidden image looks like after it is extracted:

Algorithm

The algorithm used to hide an image inside another using 2 bits is quite similar to the 4 bits, but has some distinctive differences. Instead of using 2^4, or 16, as the number used for dividing, we will be using 2^2, or 4.

The first two bits of the image to be hidden are selected using the shift function. Since it's a 2 bit hidden message, we take the two most significant bits of the image we are hiding, i.e., "Diamond.jpg", by dividing the pixel values by 64.

The first six bits of the image, "Dark Wood.jpg", are obtained using the chop2hide function.

Finally, the combined image is formed by adding the bits of the modified images together.

To get the hidden value back, we simply reverse what we have done to modify the image. This is implemented using the extract function.

Code

var start = new SimpleImage("Dark Wood.jpg");
var hide = new SimpleImage("Diamond.jpg");

function pixchange(pixval)
{
//Since it's only two bits, instead of 2^4, we use 2^2
var x = Math.floor(pixval/4) * 4;
return x;
}

//Function to alter the image to be hidden
function chop2hide(image)
{
for(var p of image.values())
{
p.setRed(pixchange(p.getRed()));
p.setGreen(pixchange(p.getGreen()));
p.setBlue(pixchange(p.getBlue()));
}
return image;
}

//Function to shift the bits of the main image
function shift(im)
{
var nim = new SimpleImage(im.getWidth(), im.getHeight());
for(var p of im.values())
{
var x = p.getX();
var y = p.getY();
var np = nim.getPixel(x,y);
np.setRed(Math.floor(p.getRed()/64));
np.setGreen(Math.floor(p.getGreen()/64));
np.setBlue(Math.floor(p.getBlue()/64));
}
return nim;
}

start_mod = chop2hide(start);
hide_mod = shift(hide);
print(starts); //Prints the first image modified
print(hides); // Prints the second image modified

//Function to add the two numbers
function add(a, b)
{
var ans = a + b;
return ans;
}

//Function to combine both images
function combine(st,hi)
{
var fin = new SimpleImage (st.getWidth(),st.getHeight());
for (var pixel_st of st.values())
{
var x = pixel_st.getX();
var y = pixel_st.getY();
var pixel_hi = hi.getPixel(x,y);
var pixel_fin = fin.getPixel(x,y);
pixel_fin.setRed(add(pixel_st.getRed(),pixel_hi.getRed()));
pixel_fin.setGreen(add(pixel_st.getGreen(),pixel_hi.getGreen()));
pixel_fin.setBlue(add(pixel_st.getBlue(),pixel_hi.getBlue()));
}
return fin;
}

combinedimage=combine(start_mod,hide_mod);
print(combinedimage); //Prints combined image

//Function to get the hidden value back
function pchange(pixval)
{
var x = (pixval-Math.floor(pixval/4)*4)*64;
return x;
}

//Function to extract the hidden image
function extract(image)
{
for(var pixel of image.values())
{
pixel.setRed(pchange(pixel.getRed()));
pixel.setGreen(pchange(pixel.getGreen()));
pixel.setBlue(pchange(pixel.getBlue()));
}
return image;
}

hiddenimage = extract(combinedimage);
print(hiddenimage); //Prints hidden image

Hidden Message in Given Picture

The following picture was given with a hidden message to extract:
After extraction, this is what the hidden message was!

Algorithm

The algorithm used for the extraction is similar to the one used for extraction in the first part.

Code


function pchange(pixval)
{
var x = (pixval-Math.floor(pixval/4)*4)*64;
return x;
}

function extract(image)
{
for(var pixel of image.values())
{
pixel.setRed(pchange(pixel.getRed()));
pixel.setGreen(pchange(pixel.getGreen()));
pixel.setBlue(pchange(pixel.getBlue()));
}
return image;
}

var combinedimage = new SimpleImage("hiltonHiding2bitMessage.png");
hiddenimage = extract(combinedimage);

print(hiddenimage);

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







Modify Image Algorithmically


function ensureInImage(coordinate, size){
    //coordinate cannot be negative
    if (coordinate < 0){
        return 0;
    }
    //coordinate size must be between [0....size-1]
    if (coordinate >= size){
        return size-1;
    }
    else{
        return coordinate;
    }
 
}
function getPixelNearby(image , x, y, diameter){
    var dx = Math.random() * diameter - diameter/2;
    var dy = Math.random() * diameter - diameter/2;
    var nx = ensureInImage(x + dx, image.getWidth());
    var ny = ensureInImage(y + dy, image.getHeight());
    return image.getPixel(nx, ny);
}
//Select An Image from the list
var image = new SimpleImage ("chapel.png");
//I'm gonna create a new image that is the same size as my starting image.
var output = new SimpleImage(image.getWidth() , image.getHeight() );
//for all the values of pixel of the image the values of x and y are taken and math.random is done since we dont want the new pixel more than the radius of some walue we enter 10 or any random number depending uopn the number we enter the blurrier the image
for (var pixel of image.values()){
    var x = pixel.getX();
    var y = pixel.getY();
    if (Math.random() > 0.5) {
        //we call the getPixelNearby(image , x, y, diameter) which inturn calls the ensureInImage(coordinate, size)
        var other = getPixelNearby(image , x, y, 10);
        output.setPixel(x, y, other);
    }
    else{
        output.setPixel(x, y, pixel);
    }
}
print (output);
print (image);


Creating a Image Algorithmic-ally

function dist(pixel,x,y){
    var dx = pixel.getX() - x ;
    var dy = pixel.getY() - y ;
    return Math.sqrt(dx * dx + dy * dy);
}
// start with a blank image
var output = new SimpleImage(320,320);
// write something here
for (var pixel of output.values()){
    if (dist(pixel,100,100) < 50 ){
        pixel.setRed(255 - 4 * dist (pixel,100,100));
    }
    else if (dist(pixel,200,200) < 80 ){
        pixel.setGreen(255 - 4 * dist (pixel,200,200));
    }  
 
     
    else if (Math.random() > 0.995 ){
        pixel.setRed(255);
        pixel.setGreen(255);
    }
    pixel.setBlue(Math.max(1.05 * pixel.getY() - pixel.getX() ,  pixel.getX() - 1.5 * pixel.getY() ))
}
print (output);




OR


//all the points here are numbered in the same way the numbers are displayed in code as comments
// Since The Function Is Called It runs And creates a circle
function dist(pixel,x,y){
    
    var dx = pixel.getX() - x ;
    var dy = pixel.getY() - y ;
    return Math.sqrt(dx * dx + dy * dy);
}
// I started with a blank image of size 256*256 as shown in the code below with Comment as 1  
var output = new SimpleImage(256,256);
// now will will take the cordinates of the image(which i consider as mercury) as 80,80 & green(earth) as 255,255 blue(venus) as 150,150 and so on and create a circle of size 25 by calling the function dist 
for (var pixel of output.values()){
    if (dist(pixel,80,80) < 25 ){
        pixel.setRed(255 - 4 * dist (pixel,80,80));
    }
    else if (dist(pixel,255,255) < 80 ){
        pixel.setGreen(255 - 2.5 * dist (pixel,255,255));
    }  
    else if  (dist(pixel,150,150) < 60 ){
        pixel.setBlue(255 - 3.5 * dist (pixel,150,150));
    }  
    else if  (dist(pixel,1,1) < 80 ){
        (pixel.setGreen(205)+pixel.setRed(205)) - 3.5 * dist (pixel,1,1);
    }  
       
    else if (Math.random() > 0.995 ){
        pixel.setRed(255);
        pixel.setGreen(255);
    }
    //pixel.setBlue(Math.max( 1.05 * pixel.getX() - pixel.getY() ,  pixel.getY() -  1.05 * pixel.getX() ))
    
}
print (output);




QUIZ Steganography Extraction and Duplication solutions



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

Suppose we decide to write a function to set the color of a pixel by giving it three RGB values. Think about what the parameters and the code would need to be for this function. Let’s call the function setColor. Which one of the following is a correct setColor function?

// 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 setColor( r, g, b) {
    for (var pixel of start.values()){
        pixel.setRed(r);
     pixel.setGreen(g);
   pixel.setBlue(b);
    }
return start;
}
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("test2.png");
var newcolor = setColor(232,12,20)
//var Hide = new SimpleImage("drewRobert.png");
//start = chop2hide(start);
//hide = shift(Hide);
//var answer = combine (start,hide);

//print (answer);
//var undo =  open(start);
//print (undo);
print (newcolor);

doubling image size

// write your code here
//selecting the image i wanted to enlarge
var smallImage = ("smalllion.jpg");
//creating a blank image which is having twice as size of original image
var bigImage =   ("smallImage.getWidth() *2 , smallImage.getHeight() *2");
for (var px of bigImage.values()){
    var x = px.getX();
    var y = px.getY();
    var calx = Math.floor(x/2);
    var caly = Math.floor(y/2);
    var smallPixel = smallImage.getPixel(calx,caly);
    px.setRed(smallPixel.getRed());
    px.setGreen(smallPixel.getGreen());
    px.setBlue(smallPixel.getBlue());
}
print (bigImage);

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

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

function pixelOnEdgeDifferentThicknesses (pixel, image, vWidth, hWidth) {
     var x = pixel.getX();
     var y = pixel.getY();
     if (x < vWidth) return true;
     if (y < hWidth) return true;
     if (x >= image.getWidth() - vWidth) return true;
     if (y >= image.getHeight() - hWidth) return true;
     return false;
}
var image = new SimpleImage("usain.jpg");
for (var pixel of image.values()) {
     if (pixelOnEdgeDifferentThicknesses (pixel, image, 10, 40)) {      
          pixel = setBlack(pixel);
     }
}

print(image);

Consider the following program from the lesson, shown below with missing code to add a border of 30 to the image. Consider the following sections of code that call the pixelOnEdge function in different ways. Select the three of them that will still run correctly and put a border of 30 around the image.

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;
     if (y < borderWidth) return true;
     if (x >= image.getWidth() - borderWidth) return true;
     if (y >= image.getHeight() - borderWidth) return true;
     return false;
}

var image = new SimpleImage("usain.jpg");
for (var pixel of image.values()) {
    if (pixelOnEdge(pixel, image, 30)) {
      pixel = setBlack(pixel);
     }
}

print(image);




OR


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;
     if (y < borderWidth) return true;
     if (x >= image.getWidth() - borderWidth) return true;
     if (y >= image.getHeight() - borderWidth) return true;
     return false;
}

var image = new SimpleImage("usain.jpg");
for (var px of image.values()) {
     if (pixelOnEdge(px, image, 30)) {      
          px = setBlack(px);
     }
}

print(image);

or


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;
     if (y < borderWidth) return true;
     if (x >= image.getWidth() - borderWidth) return true;
     if (y >= image.getHeight() - borderWidth) return true;
     return false;
}

var image = new SimpleImage("usain.jpg");
for (var px of image.values()) {
     if (pixelOnEdge(px, image, 30)) {      
          px = setBlack(px);
     }
}

print(image);

The function moreRed should increase the color of the red value of a pixel by a specified amount so that after executing the code which calls this function (shown below) then the picture is more red by adding 100 to each pixel’s red value.









Which one of the following is the correct implementation of moreRed?


// write your code here
function moreRed(pixel, amount) {
     var r = pixel.getRed() + amount;
     if (r > 255)  r = 255;
     pixel.setRed(r);
     return pixel;
}
var image = new SimpleImage ("eastereggs.jpg");
print (image);
for ( var pixel of image.values()){
    if (moreRed(pixel, 200) ){
       
    }
   
}
print (image);

The function swapRedGreen has one parameter, a pixel. This function swaps the red and green values and returns the resulting red, green and blue values somehow. Which one of the following is the correct code for this function?

// write your code here
function swapRedGreen(pixel) {
     var newGreen = pixel.getRed();
     var newRed = pixel.getGreen();
     pixel.setGreen(newGreen);
     pixel.setRed(newRed);
     return pixel;
}
var image = new SimpleImage ("eastereggs.jpg");
print (image);
for ( var pixel of image.values()){
    if (swapRedGreen(pixel) ){
       
    }
   
}
print (image);

Now modify the program one more time to replace the two functions pixelOnVerticalEdge and pixelOnHorizontalEdge with one function to do the same thing called pixelOnEdgeDifferentThicknesses( ). The parameters are not shown. You should decide on the parameters. Write the function and test it to make sure it works the same as the two functions it replaces, so you can generate pictures with borders that have different thicknesses for the vertical borders vs. the horizontal borders. Here is an example: Here is an island picture:

// write your code here
function setBlack(pixel){
    pixel.setBlue(0);
    pixel.setGreen(0);
    pixel.setRed(0);
    return pixel;
}
function pixelOnEdgeDifferentThicknesses(pixel, image, borderWidth){
    var x = pixel.getX();
    var y = pixel.getY();
 
    if (x < borderWidth) return true;
    if (y < (borderWidth+40)) return true;
    if (x >= image.getWidth() - borderWidth) return true;
    if (y >= image.getHeight() - (borderWidth+40)) return true;
    return false;
}

var image = new SimpleImage ("lion.jpg");
print (image);
for ( var pixel of image.values()){
    if (pixelOnEdgeDifferentThicknesses(pixel, image, 25) ){
        pixel = setBlack(pixel);
    }
   
}
print (image);

Here is the island picture with a border around it:
Here is a picture of the island with different thicknesses for the vertical versus horizontal borders:

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.




// write your code here
function setBlack(pixel){
    pixel.setBlue(0);
    pixel.setGreen(0);
    pixel.setRed(0);
    return pixel;
}
function pixelOnVerticalEdge(pixel, image, borderWidth){
    var x = pixel.getX();
    var y = pixel.getY();
 
    if (x < borderWidth) return true;
   
    if (x >= image.getWidth() - borderWidth) return true;
    return false;
}
function pixelOnHorizontalEdge(pixel, image, borderWidth){
    var x = pixel.getX();
    var y = pixel.getY();
    if (y < borderWidth) return true;
   
    if (y >= image.getHeight() - borderWidth) return true;
   
    return false;
}
var image = new SimpleImage ("lion.jpg");
print (image);
for ( var pixel of image.values()){
    if (pixelOnHorizontalEdge(pixel, image, 25) ){
        pixel = setBlack(pixel);
    }
    if (pixelOnVerticalEdge(pixel, image, 40)){
        pixel = setBlack(pixel);
    }
   
}
print (image);


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.

// write your code here
function setBlack(pixel){
    pixel.setBlue(0);
    pixel.setGreen(0);
    pixel.setRed(0);
    return pixel;
}
function pixelOnEdge(pixel,image,borderWidth){
    var x = pixel.getX();
    var y = pixel.getY();
    if (x < borderWidth) return true;
    if (y < borderWidth) return true;
    if (x >= image.getWidth() - borderWidth) return true;
    if (y >= image.getHeight() - borderWidth) return true;
    return false;
}
var image = new SimpleImage ("lion.jpg");
print (image);
for ( var pixel of image.values()){
    if (pixelOnEdge(pixel,image,10)){
        pixel = setBlack(pixel);
    }
}
print (image);

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.




// write your code here
function swapRedGreen(pixel){
    G = pixel.getGreen();
    R = pixel.getRed();
    if (G >= 200 ){
        pixel.setRed(255);
        pixel.setGreen(0);
        return true
    }
    if (R >= 200){
        pixel.setRed(0);
        pixel.setGreen(255);
        return true;
    }
}
var image = new SimpleImage("smallhands.png");
print (image);
for (pixel of image.values()){
    pixel = swapRedGreen(pixel);
}
print (image);



కరోనా కోవిడ్ -19 గురించి ఏ వికీపీడియా మీకు చెప్పలేము?

కరోనా కోవిడ్ -19 గురించి ఏ వికీపీడియా మీకు చెప్పలేము? మిమ్మల్ని మీరు రక్షించుకోండి  Your మీ చేతులను తరచుగా కడగాలి Eyes మీ కళ్ళు, న...