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.
Now Try to multithread these two programs. Hint: Think about dividing up the image
to be processed by separate threads.
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.ioFile;
import java.ioIOException;
public class ImageBlur
public static BufferedImage gaussianBlurBufferedImage src
int radius ; Blur radius
int size radius ;
Create a Gaussian kernel
double kernel new doublesizesize;
double sigma radius ;
double sum ;
for int x radius; x radius; x
for int y radius; y radius; y
double value double Math.expx x y y sigma sigma;
kernelx radiusy radius value;
sum value;
Normalize the kernel
for int x ; x size; x
for int y ; y size; y
kernelxy sum;
Create a new image to store the blurred result
BufferedImage blurred new BufferedImagesrcgetWidth srcgetHeight srcgetType;
Apply the kernel to each pixel
for int x ; x srcgetWidth; x
for int y ; y srcgetHeight; y
double r g b ;
Apply the kernel
for int kernelX radius; kernelX radius; kernelX
for int kernelY radius; kernelY radius; kernelY
int imageX Math.minMathmaxx kernelX, srcgetWidth;
int imageY Math.minMathmaxy kernelY, srcgetHeight;
Color color new ColorsrcgetRGBimageX imageY;
double kernelValue kernelkernelX radiuskernelY radius;
r color.getRed kernelValue;
g color.getGreen kernelValue;
b color.getBlue kernelValue;
Set the new pixel color
blurred.setRGBx y new Colorint rint gint bgetRGB;
return blurred;
public static void mainString args
try
Load the image
BufferedImage image ImageIO.readnew Fileargs;
System.out.printlnimage;
Apply Gaussian blur
BufferedImage blurredImage gaussianBlurimage;
Save the blurred image
ImageIO.writeblurredImagepng new Fileblurredargssplitpng;
catch IOException e
eprintStackTrace;
"ImageRotate.java"
import javax.imageio.ImageIO;
import java.awt.;
import java.awt.image.BufferedImage;
import java.ioFile;
import java.ioIOException;
public class ImageRotate
public static BufferedImage rotateImageBufferedImage src double angle
Calculate the new dimensions of the rotated image
double radians Math.toRadiansangle;
int width srcgetWidth;
int height srcgetHeight;
Calculate the size of the new image
int newWidth int Math.abswidth Math.cosradiansint Math.absheight Math.sinradians;
int newHeight int Math.absheight Math.cosradiansint Math.abswidth Math.sinradians;
Create a new image with the calculated dimensions
BufferedImage rotatedImage new BufferedImagenewWidth newHeight, srcgetType;
GraphicsD gd rotatedImage.createGraphics;
Set the rotation point to the center of the new image
gdtranslatenewWidth newHeight ;
gdrotateradians;
gdtranslatewidth height ;
Draw the original image onto the rotated image
gddrawImagesrc null;
gddispose;
return rotatedImage;
public static void mainString args
try
Load the image
BufferedImage image ImageIO.readnew Fileargs;
Rotate the image by degrees
BufferedImage rotatedImage rotateImageimage;
Save the rotated image
ImageIO.writerotatedImagepng new Filerotatedargssplitpng;
catch IOException e
eprintStackTrace;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
