Skip to content

Commit

Permalink
Fixing image splitting (ndg8743#38)
Browse files Browse the repository at this point in the history
* Try to fix splitting images

* Fixes and changes by lettuce

* Fix CLI output

* Fix tabbing
  • Loading branch information
iron768 authored May 10, 2024
1 parent 2126b0f commit e43e90c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 35 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/cliclient/CliClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ private static void handleCompletedResults(Result<OutputPayloadImpl> result) {
System.out.println();
}

saveImage(outputPayload.getOutputImage(), userSpecifiedFileName + ".png");
for (int i = 0; i < fibCalcResults.size(); i++) {
saveImage(outputPayload.getOutputImage(i), userSpecifiedFileName + i + ".png");
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/fibsters/BufferedImageTypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -54,8 +55,11 @@ public void write(JsonWriter jsonWriter, BufferedImage image) throws IOException

@Override
public BufferedImage read(JsonReader jsonReader) throws IOException {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
byte[] imageBytes = Base64.getDecoder().decode(jsonReader.nextString());

return ImageIO.read(new ByteArrayInputStream(imageBytes));
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/fibsters/ComputeJobPoolImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ private void processJob(ComputeJob job) {
int[] subJobs = job.getInputPayload().getPayloadDataParsed(); // ex [1, 10, 25]

int[] threadsPerSubjob = this.handleThreadPooling(subJobs, numThreads);

int threadCount = 0;

for (int i = 0; i < threadsPerSubjob.length; i++) {
Expand All @@ -149,13 +148,16 @@ private void processJob(ComputeJob job) {
jobClone.setStartIndex(start);
jobClone.setEndIndex(end);
jobClone.setChunk(i);
if (jobClone instanceof FibSpiralComputeEngineImpl) {
System.out.println("Job Pool: Chunk" + i + " fib" + jobClone.getTotalSize(i));
}


futureFibTasks[threadCount++] = executor.submit(jobClone);
}
}

}
System.out.println("Thread count: " + threadCount);

/*int threadGroupSize = Math.round(job.getTotalSize() / numThreads); // 90 fib numbers / 4 threads = 22.5
for (int i = numThreads; i >= 0; i--) {
int start = i * threadGroupSize;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/fibsters/FibCalcComputeEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void setStatus(ComputeJobStatus status) {
}

@Override
public int getTotalSize() {
public int getTotalSize(int chunk) {
int sum = 0;

for (int num : inputPayload.getPayloadDataParsed()) {
Expand Down
47 changes: 27 additions & 20 deletions src/main/java/org/fibsters/FibSpiralComputeEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import java.awt.geom.Arc2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;

public class FibSpiralComputeEngineImpl implements FibSpiralComputeEngine {

private ComputeJobStatus status;
private int[] fibonacci;
private int startIndex;
private int endIndex;
private InputPayload inputPayload; // used for reference potentially(has uuid)
Expand All @@ -44,9 +44,9 @@ public FibSpiralComputeEngineImpl(OutputPayloadImpl outputPayload, int chunk) {

this.outputPayload = outputPayload;
this.chunk = chunk;
this.fibonacci = this.outputPayload.getFibCalcResultsInteger2dList().get(this.chunk);
this.endIndex = this.fibonacci.length - 1;
this.maxElement = this.fibonacci.length;
int[] fibonacci = this.outputPayload.getFibCalcResultsInteger2dList().get(this.chunk);
this.endIndex = fibonacci.length - 1;
this.maxElement = fibonacci.length;
}

@Override
Expand All @@ -70,8 +70,8 @@ public void setStatus(ComputeJobStatus status) {
}

@Override
public int getTotalSize() {
return this.fibonacci.length;
public int getTotalSize(int chunk) {
return this.outputPayload.getFibCalcResultsInteger2dList().get(this.chunk).length;
}

@Override
Expand Down Expand Up @@ -129,6 +129,12 @@ public void run() {
}

private void generateValues() {
int[] fibonacci = this.outputPayload.getFibCalcResultsInteger2dList().get(this.chunk);

this.maxElement = fibonacci.length - 1;


System.out.println("CHUNK " + this.chunk + " LENGTH " + fibonacci.length + " fib: " + Arrays.toString(fibonacci));
int centerX = WIDTH / 2;
int centerY = HEIGHT / 2;

Expand Down Expand Up @@ -167,8 +173,8 @@ private void generateValues() {
}

System.out.println(currentFib + ", " + previousFib + ", " + previousPreviousFib);
System.out.println("Angle: " + angle);
System.out.println("DeltaXY: " + deltaXY[0] + ", " + deltaXY[1]);
//System.out.println("Angle: " + angle);
//System.out.println("DeltaXY: " + deltaXY[0] + ", " + deltaXY[1]);

x += deltaXY[0];
y += deltaXY[1];
Expand All @@ -191,15 +197,16 @@ private void generateValues() {
}

private void draw() {
BufferedImage image = this.outputPayload.getOutputImage(); //image to write to
BufferedImage image = this.outputPayload.getOutputImage(this.chunk); //image to write to
//TODO: do the writing that's in the legacy class

Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.BLUE);
System.out.println("CHUNK " + this.chunk + " img: " + image);

String debugMessage = "[Fib] (Draw) - ";
//String debugMessage = "[Fib] (Draw) - " + this.chunk;

System.out.println(debugMessage + " start angle: " + angle);
//System.out.println(debugMessage + " start angle: " + angle);

synchronized (image) {
// draw a dot at the center of the image
Expand All @@ -212,28 +219,28 @@ private void draw() {

graphics.setColor(Color.GRAY);

System.out.println(debugMessage + " <gray> " + "arcX: " + arcX + ", arcY: " + arcY);
//System.out.println(debugMessage + " <gray> " + "arcX: " + arcX + ", arcY: " + arcY);
} else if (angle == 90) {
arcX = posX;
arcY = posY;

graphics.setColor(Color.WHITE);

System.out.println(debugMessage + " <white> " + "arcX: " + arcX + ", arcY: " + arcY);
// System.out.println(debugMessage + " <white> " + "arcX: " + arcX + ", arcY: " + arcY);
} else if (angle == 180) {
arcX = posX;
arcY = posY - scaledFib;

graphics.setColor(Color.ORANGE);

System.out.println(debugMessage + " <orange> " + "arcX: " + arcX + ", arcY: " + arcY);
//System.out.println(debugMessage + " <orange> " + "arcX: " + arcX + ", arcY: " + arcY);
} else if (angle == 270) {
arcX = posX - scaledFib;
arcY = posY - scaledFib;

graphics.setColor(Color.GREEN);

System.out.println(debugMessage + " <green> " + "arcX: " + arcX + ", arcY: " + arcY);
//System.out.println(debugMessage + " <green> " + "arcX: " + arcX + ", arcY: " + arcY);
}

Shape shape = new BigIntRectangle(BigIntUtil.toBigInt(posX), BigIntUtil.toBigInt(posY), BigIntUtil.toBigInt(scaledFib), BigIntUtil.toBigInt(scaledFib));
Expand All @@ -252,13 +259,13 @@ private void draw() {
}

public void saveBuffer() {
BufferedImage image = this.outputPayload.getOutputImage(); //image to write to
for (int i = 0; i < this.outputPayload.getFibCalcResultsInteger2dList().size(); i++) {
BufferedImage image = this.outputPayload.getOutputImage(i); //image to write to

if (this.fileName.isEmpty()) {
this.fileName = "fib" + this.chunk;
}
this.fileName = "fib" + i;

saveImage(image, fileName + ".png");
saveImage(image, fileName + ".png");
}
}

public void saveImage(BufferedImage image, String fileName) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/fibsters/MultipartComputeJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public void setStatus(ComputeJobStatus status) {
}

@Override
public int getTotalSize() {
return this.fibCalcCE.getTotalSize();
public int getTotalSize(int chunk) {
return this.fibCalcCE.getTotalSize(chunk);
}

@Override
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/org/fibsters/OutputPayloadImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class OutputPayloadImpl implements OutputPayload {

private String uniqueID;
private int index;
private InputPayloadImpl inputPayload;
private ComputeJobStatus status;
private List<int[]> fibCalcResults;

// need the output for the spiral as a variable here
private BufferedImage fibSpiralResult;
private CopyOnWriteArrayList<BufferedImage> fibSpiralResults;
private List<List<String>> fibCalcStrings;

public OutputPayloadImpl(int index, InputPayloadImpl inputPayload, ComputeJobStatus status) {
Expand All @@ -32,7 +34,7 @@ public OutputPayloadImpl(int index, InputPayloadImpl inputPayload, ComputeJobSta

// need to parse the height/width
// temporarily set it as whatever
this.fibSpiralResult = new BufferedImage(2000, 2000, BufferedImage.TYPE_INT_RGB);
this.fibSpiralResults = new CopyOnWriteArrayList<>();

for (int i = 0; i < totalSize; i++) {
this.fibCalcResults.add(new int[payloadDataParsed[i]]);
Expand All @@ -43,6 +45,13 @@ public OutputPayloadImpl(int index, InputPayloadImpl inputPayload, ComputeJobSta
for (int i = 0; i < totalSize; i++) {
this.fibCalcStrings.add(new ArrayList<>(payloadDataParsed.length));
}

int chunks = fibCalcResults.size();

for (int i = 0; i < chunks; i++) {
// TODO: hardcoded and height
this.fibSpiralResults.add(new BufferedImage(2000, 2000, BufferedImage.TYPE_INT_RGB));
}
}

@Override
Expand All @@ -55,8 +64,8 @@ protected void setUniqueID(String uniqueID) {
}

@Override
public BufferedImage getOutputImage() {
return this.fibSpiralResult;
public BufferedImage getOutputImage(int chunk) {
return this.fibSpiralResults.get(chunk);
}

@Override
Expand Down Expand Up @@ -131,7 +140,7 @@ public OutputPayloadImpl clone() {
outputPayload.setUniqueID(this.uniqueID);
outputPayload.setIndex(this.index);
outputPayload.fibCalcResults = this.fibCalcResults;
outputPayload.fibSpiralResult = this.fibSpiralResult;
outputPayload.fibSpiralResults = this.fibSpiralResults;
outputPayload.fibCalcStrings = this.fibCalcStrings;

return outputPayload;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/fibsters/interfaces/ComputeJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface ComputeJob extends Runnable {

void setStatus(ComputeJobStatus status);

int getTotalSize();
int getTotalSize(int chunk);

void setStartIndex(int startIndex);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/fibsters/interfaces/OutputPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface OutputPayload {

String getUniqueID();

BufferedImage getOutputImage();
BufferedImage getOutputImage(int chunk);

Object toJSON();

Expand Down

0 comments on commit e43e90c

Please sign in to comment.