Question: Look over ImageBlur.java and ImageRotate.java. See how they work. You can try on sample photos posted or any png image you want. 3 . Now

Look over ImageBlur.java and ImageRotate.java. See how they work. You can try on
sample photos posted or any png image you want.
3. Now Try to multithread these two programs. Hint: Think about dividing up the image
to be processed by separate threads.
4. Compare the runtime of the algorithms on the images for the parallel and regular
versions
"ImageBlur.java"
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageBlur
{
public static BufferedImage gaussianBlur(BufferedImage src)
{
int radius =5; // Blur radius
int size = radius *2+1;
// Create a Gaussian kernel
double[][] kernel = new double[size][size];
double sigma = radius /3.0;
double sum =0.0;
for (int x =-radius; x <= radius; x++)
{
for (int y =-radius; y <= radius; y++)
{
double value =(double) Math.exp(-(x * x + y * y)/(2* sigma * sigma));
kernel[x + radius][y + radius]= value;
sum += value;
}
}
// Normalize the kernel
for (int x =0; x < size; x++)
{
for (int y =0; y < size; y++)
{
kernel[x][y]/= sum;
}
}
// Create a new image to store the blurred result
BufferedImage blurred = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
// Apply the kernel to each pixel
for (int x =0; x < src.getWidth(); x++)
{
for (int y =0; y < src.getHeight(); y++)
{
double r =0, g =0, b =0;
// Apply the kernel
for (int kernelX =-radius; kernelX <= radius; kernelX++)
{
for (int kernelY =-radius; kernelY <= radius; kernelY++)
{
int imageX = Math.min(Math.max(x + kernelX, 0), src.getWidth()-1);
int imageY = Math.min(Math.max(y + kernelY, 0), src.getHeight()-1);
Color color = new Color(src.getRGB(imageX, imageY));
double kernelValue = kernel[kernelX + radius][kernelY + radius];
r += color.getRed()* kernelValue;
g += color.getGreen()* kernelValue;
b += color.getBlue()* kernelValue;
}
}
// Set the new pixel color
blurred.setRGB(x, y, new Color((int) r,(int) g,(int) b).getRGB());
}
}
return blurred;
}
public static void main(String[] args){
try {
// Load the image
BufferedImage image = ImageIO.read(new File(args[0]));
System.out.println(image);
// Apply Gaussian blur
BufferedImage blurredImage = gaussianBlur(image);
// Save the blurred image
ImageIO.write(blurredImage,"png", new File("blurred_"+args[0].split("\\.")[0]+".png"));
} catch (IOException e){
e.printStackTrace();
}
}
}
"ImageRotate.java"
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageRotate
{
public static BufferedImage rotateImage(BufferedImage src, double angle)
{
// Calculate the new dimensions of the rotated image
double radians = Math.toRadians(angle);
int width = src.getWidth();
int height = src.getHeight();
// Calculate the size of the new image
int newWidth =(int) Math.abs(width * Math.cos(radians))+(int) Math.abs(height * Math.sin(radians));
int newHeight =(int) Math.abs(height * Math.cos(radians))+(int) Math.abs(width * Math.sin(radians));
// Create a new image with the calculated dimensions
BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, src.getType());
Graphics2D g2d = rotatedImage.createGraphics();
// Set the rotation point to the center of the new image
g2d.translate(newWidth /2, newHeight /2);
g2d.rotate(radians);
g2d.translate(-width /2,-height /2);
// Draw the original image onto the rotated image
g2d.drawImage(src,0,0, null);
g2d.dispose();
return rotatedImage;
}
public static void main(String[] args)
{
try
{
// Load the image
BufferedImage image = ImageIO.read(new File(args[0]));
// Rotate the image by 90 degrees
BufferedImage rotatedImage = rotateImage(image,90);
// Save the rotated image
ImageIO.write(rotatedImage,"png", new File("rotated_"+args[0].split("\\.")[0]+".png"));
} catch (IOException e)
{
e.printStackTrace();
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!