(This is in the Java language. I just need help with these methods the construct
ID: 3602201 • Letter: #
Question
(This is in the Java language. I just need help with these methods the constructor code is already written) (Need method to increase red, grayscale, and the last method) Thank you!
Question 1.
Write a method named makeSunset3() to make a sunset. Don not decrease blue and green, but increase red. You can use beach.jpg to test this method.
Question 2. Write a method blackWhite() that create a black and white picture from a color picture. The idea is as follows: For each pixel, let avg = (r+g+b)/3. If avg < 64, set the color black. If avg >= 64, set the color white.
Question 3. Write a method changeBlue(weight). Do not change any color of the pixels in the first half of a picture. For the second half, change blue value of each pixel by new blue=original blue * weight. For example, if a pixel has values (200,100,100) and weight is 0.5, then the new values will be (200,100,50). If the weight is 1.5, then the new values will be (200,100,150).
Explanation / Answer
1. makeSunset3()-.
public static void makeSunset3(){
BufferedImage image = null;
File file = null;
//read image from computer
try{
file = new File("C:\Users\Users\Desktop\Maldives-beach.jpg");
image = ImageIO.read(file);
}catch(IOException e){
System.out.println(e);
}
//image width and height
int width = image.getWidth();
int height = image.getHeight();
//get each and every pixel of image
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int pixel = image.getRGB(x,y);
//get red, green and blue value of pixel
int r = (pixel>>16)&0xff;
int g = (pixel>>8)&0xff;
int b = pixel&0xff;
//increase red by 40 or whatever you want
r=r+40;
//update pixel value with new red
pixel=(255<<24) | (r<<16) | (g<<8) | b;
//set new pixel to image
image.setRGB(x,y,pixel);
}
}
//saving file to computer
try{
file = new File("C:\Users\Users\Desktop\demoOut.jpg");
ImageIO.write(image, "jpg", file);
}catch(IOException e){
System.out.println(e);
}
}
2. blackWhite()-
public static void blackWhite(){
BufferedImage image = null;
File file = null;
//read image from computer
try{
file = new File("C:\Users\Users\Desktop\Maldives-beach.jpg");
image = ImageIO.read(file);
}catch(IOException e){
System.out.println(e);
}
//image width and height
int width = image.getWidth();
int height = image.getHeight();
//get each and every pixel of image
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int pixel = image.getRGB(x,y);
int r = (pixel>>16)&0xff;
int g = (pixel>>8)&0xff;
int b = pixel&0xff;
//get red, green and blue value of pixel
int avg = (r+g+b)/3;
if (avg<64){
//If avg<64 make pixel black
pixel = (255<<24) | (0<<16) | (0<<8) | 0;
}
else
//avg>=64 make pixel white
pixel=(255<<24) | (255<<16) | (255<<8) | 255;
//set new pixel to image
image.setRGB(x,y,pixel);
}
}
//saving file to computer
try{
file = new File("C:\Users\Users\Desktop\demoOut.jpg");
ImageIO.write(image, "jpg", file);
}catch(IOException e){
System.out.println(e);
}
}
3. changeBlue(weight)-
public static void blackWhite(double weight){
BufferedImage image = null;
File file = null;
//read image from computer
try{
file = new File("C:\Users\Users\Desktop\Maldives-beach.jpg");
image = ImageIO.read(file);
}catch(IOException e){
System.out.println(e);
}
//image width and height
int width = image.getWidth();
int height = image.getHeight();
//get each and every pixel of second half of image
for(int y = height/2; y < height; y++){
for(int x = 0; x < width; x++){
int pixel = image.getRGB(x,y);
//get RGB value of pixel
int r = (pixel>>16)&0xff;
int g = (pixel>>8)&0xff;
int b = pixel&0xff;
//Multiply Blue*weight
b=(int)(b*weight);
//set pixel with new blue value
pixel = (255<<24) | (r<<16) | (g<<8) | b;
//set new pixel to image
image.setRGB(x,y,pixel);
}
}
//saving file to computer
try{
file = new File("C:\Users\Users\Desktop\demoOut.jpg");
ImageIO.write(image, "jpg", file);
}catch(IOException e){
System.out.println(e);
}
}
A pixel contains 4 information-Alpha(transparency), Red, Green, and Blue. Each of them is 8 bits(0-255), therefore total size of 1 pixel is 8*4=32bits.
[{---Alpha-8--}{----Red-8--}{----Green-8--}{---Blue-8--}]
Therefore in order to set the value of the Red pixel, we need to shift 16 bits left(look position of red in the diagram). The same concept applies while getting Red pixel information. We will shift 16bit towards the right-hand side with logical & 0xFF.
Likewise, it is same for Green and Blue also. For blue as it is already on the left-hand side, no shifting needed.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.