[wosaic commit] r339 - in trunk/src/wosaic: . algorithms utilities

0 views
Skip to first unread message

codesite...@google.com

unread,
Sep 17, 2008, 5:11:15 PM9/17/08
to wosaic...@googlegroups.com
Author: swegner2
Date: Wed Sep 17 14:10:47 2008
New Revision: 339

Modified:
trunk/src/wosaic/Controller.java
trunk/src/wosaic/JAIProcessor.java
trunk/src/wosaic/algorithms/AbstractAlgorithm.java
trunk/src/wosaic/algorithms/BruteForceAlgorithm.java
trunk/src/wosaic/utilities/Mosaic.java

Log:
Start using alternative Algorithm interface (hooray)

Modified: trunk/src/wosaic/Controller.java
==============================================================================
--- trunk/src/wosaic/Controller.java (original)
+++ trunk/src/wosaic/Controller.java Wed Sep 17 14:10:47 2008
@@ -143,6 +143,7 @@
ThreadPool.shutdown();
ThreadPool.awaitTermination(THREAD_WAIT_SECS, TimeUnit.SECONDS);
// Signal to the JAIProcessor that we've finished
+ System.err.println("Interrupting JAIProcessor");
mosaicThread.interrupt();
mosaicThread.join();


Modified: trunk/src/wosaic/JAIProcessor.java
==============================================================================
--- trunk/src/wosaic/JAIProcessor.java (original)
+++ trunk/src/wosaic/JAIProcessor.java Wed Sep 17 14:10:47 2008
@@ -7,6 +7,7 @@

import java.awt.image.BufferedImage;

+import wosaic.algorithms.AbstractAlgorithm;
import wosaic.utilities.ImageBuffer;
import wosaic.utilities.Mosaic;
import wosaic.utilities.Parameters;
@@ -47,7 +48,22 @@
*/
public Pixel[][] wosaic;

- // private JPEGImageDecoder jpegDecoder;
+ private AbstractAlgorithm MatchingAlgorithm;
+
+ private AbstractAlgorithm.Algorithms AlgorithmType;
+
+ /**
+ * Sets the algorithm used for fitting Pixel objects into the
+ * mosaic.
+ *
+ * @param algorithm The new algorithm to use.
+ */
+ public void SetMatchingAlgorithm(AbstractAlgorithm.Algorithms algorithm) {
+ if (algorithm == null)
+ throw new IllegalArgumentException();
+
+ AlgorithmType = algorithm;
+ }

/**
* This constructor should be used by the threaded application.
@@ -66,6 +82,8 @@
sourcesBuffer = buf;
mosaic = mos;
statusObject = stat;
+
+ AlgorithmType = AbstractAlgorithm.Algorithms.BRUTE_FORCE;
}

/**
@@ -105,6 +123,8 @@
// Calculate average colors of the segments of the master
colorMap = analyzeSegments(params.resRows, params.resCols, master.width
/ params.resCols, master.height / params.resRows, master);
+
+ MatchingAlgorithm = AbstractAlgorithm.CreateAlgorithm(AlgorithmType,
mosaic, colorMap);

BufferedImage newImg = null;
while (sourcesBuffer.size() != 0 || !Thread.interrupted()) {
@@ -112,17 +132,16 @@
try {
newImg = sourcesBuffer.removeFromImageBuffer();
} catch (final InterruptedException e) {
- return;
+ MatchingAlgorithm.FinishedAddingPixels();
+ statusObject.setStatus("Mosaic Complete!");
}
final Pixel newPixel = new Pixel(newImg);

- mosaic.updateMosaic(newPixel, colorMap);
+ MatchingAlgorithm.AddPixel(newPixel);
Thread.yield();
}

+ MatchingAlgorithm.FinishedAddingPixels();
statusObject.setStatus("Mosaic Complete!");
-
- // System.out.println("JAIProcessor finished.");
- // System.out.println("Exiting MosaicThrd...");
}
}

Modified: trunk/src/wosaic/algorithms/AbstractAlgorithm.java
==============================================================================
--- trunk/src/wosaic/algorithms/AbstractAlgorithm.java (original)
+++ trunk/src/wosaic/algorithms/AbstractAlgorithm.java Wed Sep 17 14:10:47
2008
@@ -88,4 +88,18 @@
// together. Other weights can be added in the future.
return rmDiff + gmDiff + bmDiff;
}
+
+ public static final AbstractAlgorithm CreateAlgorithm(Algorithms
algorithm, Mosaic mos, int[][][] colorMap) {
+ switch (algorithm) {
+ case BRUTE_FORCE :
+ return new BruteForceAlgorithm(mos, colorMap);
+
+ default:
+ return null;
+ }
+ }
+
+ public enum Algorithms {
+ BRUTE_FORCE;
+ }
}

Modified: trunk/src/wosaic/algorithms/BruteForceAlgorithm.java
==============================================================================
--- trunk/src/wosaic/algorithms/BruteForceAlgorithm.java (original)
+++ trunk/src/wosaic/algorithms/BruteForceAlgorithm.java Wed Sep 17
14:10:47 2008
@@ -16,8 +16,6 @@
* mosaic region and see if the new distance score is better than any
existing
* score, and replace it if it is. This algorithm can be very slow, but is
also
* easy to implement
- *
- * @author swegner2
*/
public class BruteForceAlgorithm extends AbstractAlgorithm {

@@ -48,19 +46,23 @@
*/
@Override
public void AddPixel(Pixel pixel) {
+ try {
+ PixelQueue.put(pixel);
+ } catch (InterruptedException ex) {
+ // We've been canceled!
+ pluginFinished.set(true);
+ return;
+ }
+
// Start running if we're not
boolean wasRunning = running.getAndSet(true);
if (!wasRunning) {
Thread workThread = new Thread(this, "AlgorithmThread");
- workThread.run();
+ workThread.start();
}
-
- PixelQueue.add(pixel);
- PixelQueue.notifyAll();
}

private void processPixel(Pixel pixel) {
-
final int[] avgColors = new int[3];
for (int r = 0; r < Mos.getParams().resRows; r++)
for (int c = 0; c < Mos.getParams().resCols; c++) {
@@ -86,23 +88,44 @@
*/
@Override
public void AddPixels(ArrayList<Pixel> pixels) {
+ if (pixels.size() == 0)
+ return;
+
+ // Add the first pixel and get the program started if we haven't yet
+ try {
+ PixelQueue.put(pixels.remove(0));
+ } catch (InterruptedException ex) {
+ // We've been canceled!
+ pluginFinished.set(true);
+ return;
+ }
+
// Start running if we're not
boolean wasRunning = running.getAndSet(true);
if (!wasRunning) {
Thread workThread = new Thread(this, "AlgorithmThread");
- workThread.run();
+ workThread.start();
}

- PixelQueue.addAll(pixels);
- PixelQueue.notifyAll();
+ // Add the rest of the pixels
+ for (Pixel pixel : pixels)
+ try {
+ PixelQueue.put(pixel);
+ } catch (InterruptedException ex) {
+ // We've been canceled!
+ pluginFinished.set(true);
+ return;
+ }
}

@Override
public void run() {
+ System.err.println("Algorithm thread running");
+
boolean waitForMore = true;
boolean finished = false;

- while(!finished) {
+ while(!Thread.interrupted() && !finished) {
if (waitForMore)
waitForMore = !pluginFinished.get();

@@ -112,6 +135,7 @@
} catch (InterruptedException ex) {
// We've been canceled! Cleanup?
waitForMore = false;
+ pluginFinished.set(true);
}

if (pixel == null) {
@@ -124,10 +148,13 @@
} else // pixel != null
processPixel(pixel);
}
+
+ System.err.println("Algorithm thread exiting..");
}

@Override
public void FinishedAddingPixels() {
+ System.err.println("Plugin notified algorithm that it's finished.");
pluginFinished.set(true);
}
}

Modified: trunk/src/wosaic/utilities/Mosaic.java
==============================================================================
--- trunk/src/wosaic/utilities/Mosaic.java (original)
+++ trunk/src/wosaic/utilities/Mosaic.java Wed Sep 17 14:10:47 2008
@@ -292,5 +292,10 @@
final Pixel newPixel, int score) {
imageGrid[row][col] = newPixel;
scoreGrid[row][col] = score;
+
+ // Fire an update
+ ArrayList<Point> coords = new ArrayList<Point>(1);
+ coords.add(new Point(row, col));
+ _fire(coords, newPixel);
}
}

Reply all
Reply to author
Forward
0 new messages