Skip to content

Commit

Permalink
Image processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Golem211 committed Feb 13, 2024
0 parents commit 75f257c
Show file tree
Hide file tree
Showing 28 changed files with 441 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LogLevelTransformation</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Binary file added OutImg/image.bmp
Binary file not shown.
Binary file added OutImg/image_processed0.bmp
Binary file not shown.
Binary file added OutImg/image_processed1.bmp
Binary file not shown.
Binary file added OutImg/image_processed2.bmp
Binary file not shown.
Binary file added OutImg/image_processed3.bmp
Binary file not shown.
Binary file added OutImg/image_processed4.bmp
Binary file not shown.
Binary file added bin/test/MyMain.class
Binary file not shown.
Binary file added bin/work/BMPReader.class
Binary file not shown.
Binary file added bin/work/BMPWritable.class
Binary file not shown.
Binary file added bin/work/ImageModel.class
Binary file not shown.
Binary file added bin/work/ImageProcessor.class
Binary file not shown.
Binary file added bin/work/MyImage.class
Binary file not shown.
Binary file added bin/work/SharedBuffer.class
Binary file not shown.
Binary file added inImages/Lenabmp.bmp
Binary file not shown.
Binary file added inImages/image2.bmp
Binary file not shown.
Binary file added inImages/image3.bmp
Binary file not shown.
Binary file added inImages/image4.bmp
Binary file not shown.
Binary file added inImages/image5.bmp
Binary file not shown.
77 changes: 77 additions & 0 deletions src/test/MyMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package test;
import work.*;

public class MyMain {
public static void main(String[] args) {

if(args.length == 0){ // rulez aplicatia fara inputimag si outLocation din linia de comanda
SharedBuffer buffer = new SharedBuffer(1); // creez un buffer in care threadul reader poate pune imagini si in care alt thread poate procesa imaginile de acolo

// Creez BMPreader threads
BMPReader readerThread1 = new BMPReader(buffer, "inImages/Lenabmp.bmp");
BMPReader readerThread2 = new BMPReader(buffer, "inImages/image2.bmp");
BMPReader readerThread3 = new BMPReader(buffer, "inImages/image3.bmp");
BMPReader readerThread4 = new BMPReader(buffer, "inImages/image4.bmp");
BMPReader readerThread5 = new BMPReader(buffer, "inImages/image5.bmp");

//creez thredul de procesare
ImageProcessor processorThread = new ImageProcessor(buffer,null);

// pornesc toate threadurile
readerThread1.start();
readerThread2.start();
readerThread3.start();
readerThread4.start();
readerThread5.start();
processorThread.start();

// Astept ca thredurile de citire sa se termine si sa se uneasca cu threadul main
try {

readerThread1.join();
readerThread2.join();
readerThread3.join();
readerThread4.join();
readerThread5.join();


} catch (InterruptedException e) { // exceptie cand un thread nu se termina
Thread.currentThread().interrupt();
}

// in buffer pun valoarea null deoarece sa se stie ca nu mai este nicio imagine de produs
buffer.produce(null);

// Astept si threadul de procesare sa se termine
try {
processorThread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
else if(args.length == 2){ // in cazul in care am argumente de la tastatura
SharedBuffer buffer = new SharedBuffer(3); // buffer pentru resursele comune

BMPReader readerThread1 = new BMPReader(buffer, args[0]); // thread cu constructor in care folosesc primul argument
//de la linia de comanda ca imagine ce trebuie procesata

ImageProcessor processorThread = new ImageProcessor(buffer,args[1]); // in args[1] pun imaginea procesata
readerThread1.start();
processorThread.start();

try {
readerThread1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buffer.produce(null);
try {
processorThread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

}
}
}
74 changes: 74 additions & 0 deletions src/work/BMPReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package work;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class BMPReader extends Thread {
private final SharedBuffer buffer; //resursa comuna
private final String filePath; // locatia imaginii de intrare
static int counter; // counter pentru a vedea cate imagini procesez
static{//bloc static de initializare
counter = 0;
}

public BMPReader(SharedBuffer buffer, String filePath) { //constructor cu parametrii
this.buffer = buffer;
this.filePath = filePath;
}

@Override
public void run() { // implementez metoda run a threadului este thread de Producere

try {
sleep(1000);
MyImage myImage = BMPReader.readBMP(filePath); // creez o instanta a clasei MyImage in care stochez o imagine
buffer.produce(myImage); // ilustrez modul in care se produce o imagine
System.out.println("producatorul a produs imaginea: " + (counter));
} catch (InterruptedException e) { // tratez exceptia in cazul in care nu se poate pune threadul la sleep
// TODO Auto-generated catch block
e.printStackTrace();
}


}

public static MyImage readBMP(String filePath) { // stochez in obiect MyImage imaginea propiu zisa.bmp
try {
// Read the BMP file using javax.imageio
BufferedImage bufferedImage = ImageIO.read(new File(filePath)); // folosesc ImgageIO pt a citi fisiere de tip image

// Get width and height of the image
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
long startCitire= System.currentTimeMillis(); // incep masurarea timpului pentru citirea fisierului si stocarea sa
// Create a custom Image object
MyImage myImage = new MyImage(width, height);

// Populez myImage cu date pixel
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = bufferedImage.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;

myImage.setPixel(x, y, red, green, blue); // introduc cate un bit de informatie pe fiecare canal al imaginii mele
}
}
long finalCitire = System.currentTimeMillis();
long elapsedTimeProcessing = finalCitire - startCitire;
System.out.println("Elapsed time for Reading Image" + (++counter)+ ": "+ elapsedTimeProcessing + " milliseconds");
return myImage;

} catch (IOException e) {
e.printStackTrace();
}

return null;
}
}



5 changes: 5 additions & 0 deletions src/work/BMPWritable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package work;
// o interfata pentru a putea scrie in fisier
public interface BMPWritable {
void saveToBMP(String filePath); // metoda de implementat
}
54 changes: 54 additions & 0 deletions src/work/ImageModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package work;

// Clasa abstracta din care se extinde clasa MyImage, are o metoda abstracta

public abstract class ImageModel {
private int width; // latimea imaginii
private int height; // inaltimea imaginii
private int[][] redChannel; // matricea ce stocheaza valoarea de rosu al unei imagini
private int[][] greenChannel;// matricea ce stocheaza valoarea de verde al unei imagini
private int[][] blueChannel;// matricea ce stocheaza valoarea de albastru al unei imagini

public ImageModel(int width, int height) { //constructor
this.width = width;
this.height = height;
this.redChannel = new int[width][height];
this.greenChannel = new int[width][height];
this.blueChannel = new int[width][height];
}
public abstract void abstractMethod();


// seter si geters.
public int getWidth() {
return width;
}

public int getHeight() {
return height;
}


public int getRed(int x, int y) {
return redChannel[x][y];
}

public int getGreen(int x, int y) {
return greenChannel[x][y];
}

public int getBlue(int x, int y) {
return blueChannel[x][y];
}

//metoda pt a seta valoarea unui pixel.
public void setPixel(int x, int y, int red, int green, int blue) {
redChannel[x][y] = red;
greenChannel[x][y] = green;
blueChannel[x][y] = blue;
}




}
43 changes: 43 additions & 0 deletions src/work/ImageProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package work;
public class ImageProcessor extends Thread {
private final SharedBuffer buffer; // resursa comuna
private String outFile; // poate accepta si outfile pt a salva imaginea procesata
{ // bloc de initializare
outFile = null;
}
public ImageProcessor(SharedBuffer buffer, String file) {
this.buffer = buffer;
setOutFile(file);
}


public String getOutFile() {
return outFile;
}

public void setOutFile(String outFile) {
this.outFile = outFile;
}

@Override
public void run() { // implementarea run pt consumator
int i = 0;
while (true) { // ruleaza cat timp sunt imagini in buffer

MyImage myImage = buffer.consume(); // stochez din buffer imaginea mea
if (myImage == null) { // daca ce iau din buffer e nimic inchid threadul
// No more items in the buffer, exit the thread
break;
}
System.out.println("Consumatorul a citit imaginea: "+(i+1));
// Perform image processing on myImage
MyImage transformedImg = myImage.manipulateImage(); // se deschide metoda de procesare a imaginii
if(outFile ==null){ // in cazul in care nu sunt specificate fisierul de iesire
transformedImg.saveToBMP("OutImg/image_processed"+i+".bmp");
i++;
}
else
transformedImg.saveToBMP(outFile);
}
}
}
Loading

0 comments on commit 75f257c

Please sign in to comment.