Commit 74bda77b authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

Homework Question 2 Code

parent 65490e00
# Default ignored files
/shelf/
/workspace.xml
<component name="CopyrightManager">
<copyright>
<option name="notice" value="Copyright (c) &amp;#36;originalComment.match(&quot;Copyright \(c\) (\d+)&quot;, 1, &quot;-&quot;, &quot;&amp;#36;today.year&quot;)&amp;#36;today.year. Michael Pogrebinsky - Top Developer Academy &#10;https://topdeveloperacademy.com&#10;All rights reserved" />
<option name="myName" value="Top Developer Academy" />
</copyright>
</component>
\ No newline at end of file
<component name="CopyrightManager">
<settings default="Top Developer Academy" />
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ASMIdeaPluginConfiguration">
<asm skipDebug="false" skipFrames="false" skipCode="false" expandFrames="false" />
<groovy codeStyle="LEGACY" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/optimizing-for-latency-example.iml" filepath="$PROJECT_DIR$/optimizing-for-latency-example.iml" />
</modules>
</component>
</project>
\ No newline at end of file
Copyright (c) 2019-2023. Michael Pogrebinsky - Top Developer Academy
https://topdeveloperacademy.com
All rights reserved
\ No newline at end of file
Code examples for the online course [Java Multithreading, Concurrency & Performance Optimization](https://www.udemy.com/java-multithreading-concurrency-performance-optimization)
Author : Michael Pogrebinsky
### To open the project using Intellij
1. File -> New -> "Project from Existing Sources" or "Import Project".
2. Select the project directory.
3. Select "Create Project from Existing Sources" and click "Next" repeatedly until the last window.
4. Click "Finish"
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
/*
* Copyright (c) 2019-2023. Michael Pogrebinsky - Top Developer Academy
* https://topdeveloperacademy.com
* All rights reserved
*/
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Optimizing for Latency Part 2 - Image Processing
* https://www.udemy.com/java-multithreading-concurrency-performance-optimization
*/
public class Main {
public static final String SOURCE_FILE = "./resources/many-flowers.jpg";
public static final String DESTINATION_FILE = "./out/many-flowers.jpg";
public static void main(String[] args) throws IOException {
BufferedImage originalImage = ImageIO.read(new File(SOURCE_FILE));
BufferedImage resultImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
long startTime = System.currentTimeMillis();
//recolorSingleThreaded(originalImage, resultImage);
int numberOfThreads = 1;
recolorMultithreaded(originalImage, resultImage, numberOfThreads);
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
File outputFile = new File(DESTINATION_FILE);
ImageIO.write(resultImage, "jpg", outputFile);
System.out.println(String.valueOf(duration));
}
public static void recolorMultithreaded(BufferedImage originalImage, BufferedImage resultImage, int numberOfThreads) {
List<Thread> threads = new ArrayList<>();
int width = originalImage.getWidth();
int height = originalImage.getHeight() / numberOfThreads;
for(int i = 0; i < numberOfThreads ; i++) {
final int threadMultiplier = i;
Thread thread = new Thread(() -> {
int xOrigin = 0 ;
int yOrigin = height * threadMultiplier;
recolorImage(originalImage, resultImage, xOrigin, yOrigin, width, height);
});
threads.add(thread);
}
for(Thread thread : threads) {
thread.start();
}
for(Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
}
}
}
public static void recolorSingleThreaded(BufferedImage originalImage, BufferedImage resultImage) {
recolorImage(originalImage, resultImage, 0, 0, originalImage.getWidth(), originalImage.getHeight());
}
public static void recolorImage(BufferedImage originalImage, BufferedImage resultImage, int leftCorner, int topCorner,
int width, int height) {
for(int x = leftCorner ; x < leftCorner + width && x < originalImage.getWidth() ; x++) {
for(int y = topCorner ; y < topCorner + height && y < originalImage.getHeight() ; y++) {
recolorPixel(originalImage, resultImage, x , y);
}
}
}
public static void recolorPixel(BufferedImage originalImage, BufferedImage resultImage, int x, int y) {
int rgb = originalImage.getRGB(x, y);
int red = getRed(rgb);
int green = getGreen(rgb);
int blue = getBlue(rgb);
int newRed;
int newGreen;
int newBlue;
if(isShadeOfGray(red, green, blue)) {
newRed = Math.min(255, red + 10);
newGreen = Math.max(0, green - 80);
newBlue = Math.max(0, blue - 20);
} else {
newRed = red;
newGreen = green;
newBlue = blue;
}
int newRGB = createRGBFromColors(newRed, newGreen, newBlue);
setRGB(resultImage, x, y, newRGB);
}
public static void setRGB(BufferedImage image, int x, int y, int rgb) {
image.getRaster().setDataElements(x, y, image.getColorModel().getDataElements(rgb, null));
}
public static boolean isShadeOfGray(int red, int green, int blue) {
return Math.abs(red - green) < 30 && Math.abs(red - blue) < 30 && Math.abs( green - blue) < 30;
}
public static int createRGBFromColors(int red, int green, int blue) {
int rgb = 0;
rgb |= blue;
rgb |= green << 8;
rgb |= red << 16;
rgb |= 0xFF000000;
return rgb;
}
public static int getRed(int rgb) {
return (rgb & 0x00FF0000) >> 16;
}
public static int getGreen(int rgb) {
return (rgb & 0x0000FF00) >> 8;
}
public static int getBlue(int rgb) {
return rgb & 0x000000FF;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment