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

1 view
Skip to first unread message

codesite...@google.com

unread,
Sep 10, 2008, 2:29:50 PM9/10/08
to wosaic...@googlegroups.com
Author: csvenss2
Date: Wed Sep 10 11:28:50 2008
New Revision: 333

Modified:
trunk/src/wosaic/JAIProcessor.java
trunk/src/wosaic/WosaicUI.java
trunk/src/wosaic/utilities/Mosaic.java
trunk/src/wosaic/utilities/Parameters.java
trunk/src/wosaic/utilities/Pixel.java

Log:
Added support for tinting... beta.

Modified: trunk/src/wosaic/JAIProcessor.java
==============================================================================
--- trunk/src/wosaic/JAIProcessor.java (original)
+++ trunk/src/wosaic/JAIProcessor.java Wed Sep 10 11:28:50 2008
@@ -19,8 +19,6 @@
*/
public class JAIProcessor implements Runnable {

- private int[][][] colorMap;
-
/**
* This is the Pixel object for the master image.
*/
@@ -68,31 +66,7 @@
statusObject = stat;
}

- /**
- * Split an image up into segments, and calculate its average color.
- *
- * @param numRows Number of rows in the mosaic
- * @param numCols Number of columns in the mosaic
- * @param width the width of a segment
- * @param height the height of a segment
- * @param mPixel the source image
- * @return the average colors of each segment
- */
- public int[][][] analyzeSegments(final int numRows, final int numCols,
- final int width, final int height, final Pixel mPixel) {
-
- final int[][][] avgColors = new int[numRows][numCols][3];
-
- for (int r = 0; r < numRows; r++)
- for (int c = 0; c < numCols; c++) {
- final int startY = r * height;
- final int startX = c * width;
- mPixel.getAvgColor(startX, startY, width, height,
- avgColors[r][c]);
- }

- return avgColors;
- }

/**
* Creates a mosaic by analyzing the master image, and then getting images
@@ -103,7 +77,8 @@
// System.out.println("Running MosaicThrd...");

// Calculate average colors of the segments of the master
- colorMap = analyzeSegments(params.resRows, params.resCols, master.width
+ //colorMap = analyzeSegments(params.resRows, params.resCols, master.width
+ mosaic.analyzeSegments(params.resRows, params.resCols, master.width
/ params.resCols, master.height / params.resRows, master);

BufferedImage newImg = null;
@@ -116,7 +91,7 @@
}
final Pixel newPixel = new Pixel(newImg);

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


Modified: trunk/src/wosaic/WosaicUI.java
==============================================================================
--- trunk/src/wosaic/WosaicUI.java (original)
+++ trunk/src/wosaic/WosaicUI.java Wed Sep 10 11:28:50 2008
@@ -930,6 +930,9 @@

// Setup the parameters
final Parameters params = GenParams(SourceImage);
+
+ // Choose algorithm type
+ params.alg = Parameters.Algorithm.NoRepeats;

// Create the grid elements in our mosaic panel
MosaicDisplay.setGrid(params.resRows, params.resCols);

Modified: trunk/src/wosaic/utilities/Mosaic.java
==============================================================================
--- trunk/src/wosaic/utilities/Mosaic.java (original)
+++ trunk/src/wosaic/utilities/Mosaic.java Wed Sep 10 11:28:50 2008
@@ -3,8 +3,10 @@
*/
package wosaic.utilities;

+import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
+import java.awt.image.RescaleOp;
import java.awt.image.WritableRaster;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -31,6 +33,9 @@
private Parameters params;

private int[][] scoreGrid;
+
+ int[][][] colorMap;
+

/**
* Constructor for a mosaic object called by the Controller.
@@ -212,6 +217,10 @@

public void save(final BufferedImage img, final String file,
final String type) throws IOException {
+
+ // DEBUG Apply tinting...
+ tint(1f);
+
final FileOutputStream os = new FileOutputStream(file);
final JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(img);
@@ -237,8 +246,7 @@
* @param colorMap the 3D array containing color information about the
* master image
*/
- public synchronized void updateMosaic(final Pixel srcPixel,
- final int[][][] colorMap) {
+ public synchronized void updateMosaic(final Pixel srcPixel) {
// Check all the segments to see where this might fit
final ArrayList<Point> updatedCoords = new ArrayList<Point>();

@@ -255,11 +263,17 @@
// Like in golf, a lower score is better. This is simply
// made up of the total difference in each channel, added
// together. Other weights can be added in the future.
- final int matchScore = rmDiff + gmDiff + bmDiff;
+ int matchScore = rmDiff + gmDiff + bmDiff;
+
+ // Decrease the score if this has been used before...
+ if(params.alg == Parameters.Algorithm.NoRepeats) {
+ matchScore += srcPixel.used * 10;
+ }

if (imageGrid[r][c] != null) {

if (matchScore < scoreGrid[r][c]) {
+ srcPixel.used++;
imageGrid[r][c] = srcPixel;
scoreGrid[r][c] = matchScore;
updatedCoords.add(new Point(r, c));
@@ -267,6 +281,7 @@

} else {
// Just assign this Pixel to this spot
+ srcPixel.used++;
imageGrid[r][c] = srcPixel;
scoreGrid[r][c] = matchScore;

@@ -279,6 +294,89 @@
notifyAll();
}

+
+ /**
+ * Split an image up into segments, and calculate its average color.
+ *
+ * @param numRows
+ * @param numCols
+ * @param width
+ * the width of a segment
+ * @param height
+ * the height of a segment
+ * @param mPixel
+ * the source image
+ * @return the average colors of each segment
+ */
+ public void analyzeSegments(final int numRows, final int numCols,
+ final int width, final int height, final Pixel mPixel) {
+
+ final int[][][] avgColors = new int[numRows][numCols][3];
+
+ for (int r = 0; r < numRows; r++)
+ for (int c = 0; c < numCols; c++) {
+ final int startY = r * height;
+ final int startX = c * width;
+ mPixel.getAvgColor(startX, startY, width, height,
+ avgColors[r][c]);
+ }
+
+ colorMap = avgColors;
+ }
+
+ /**
+ *
+ * @param correction - Amount of tinting to apply
+ *
+ * Tints the tiles in the mosaic to more closely
+ * match the master image.
+ */
+ public void tint(float correction) {
+ int i, j, width, height;
+
+ width = imageGrid.length;
+ height = imageGrid[0].length;
+
+ for(i = 0; i < width; i++) {
+ for (j=0; j < height; j++) {
+ // Grab Image
+ BufferedImage img = imageGrid[i][j].getImage();
+
+ // Calculate tinting ratios
+ float rRatio, gRatio, bRatio;
+ /*rRatio = correction * ((float) colorMap[i][j][0] / (float)
imageGrid[i][j].getAvgImageColor(null)[0]);
+ gRatio = correction * ((float) colorMap[i][j][1] / (float)
imageGrid[i][j].getAvgImageColor(null)[1]);
+ bRatio = correction * ((float) colorMap[i][j][2] / (float)
imageGrid[i][j].getAvgImageColor(null)[2]);*/
+
+ // Calculate offsets...
+ float rOff, bOff, gOff;
+ rOff = colorMap[i][j][0] - imageGrid[i][j].getAvgImageColor(null)[0];
+ gOff = colorMap[i][j][1] - imageGrid[i][j].getAvgImageColor(null)[1];
+ bOff = colorMap[i][j][2] - imageGrid[i][j].getAvgImageColor(null)[2];
+
+ /*rRatio = ((float) imageGrid[i][j].getAvgImageColor(null)[0] /
(float) colorMap[i][j][0]);
+ gRatio = ((float) imageGrid[i][j].getAvgImageColor(null)[1] / (float)
colorMap[i][j][1]);
+ bRatio = ((float) imageGrid[i][j].getAvgImageColor(null)[2] / (float)
colorMap[i][j][2]);*/
+
+ System.out.print("r: " + rOff);
+ System.out.print(" g: " + gOff);
+ System.out.print(" b: " + bOff + "\n");
+
+ //float[] scales = { rRatio, gRatio, bRatio};
+ float[] scales = { 1f, 1f, 1f};
+ float[] offsets = {rOff, bOff, gOff};
+ //float[] offsets = new float[3];
+ RescaleOp rop = new RescaleOp(scales, offsets, null);
+
+ /* Draw the image, applying the filter */
+ Graphics2D g2d = (Graphics2D) img.getGraphics();
+ g2d.drawImage(img, rop, 0, 0);
+ }
+ }
+
+ }
+
+
/**
* Update the Pixel in a given coordinate with a new one.
*
@@ -292,4 +390,5 @@
imageGrid[row][col] = newPixel;
scoreGrid[row][col] = score;
}
+
}

Modified: trunk/src/wosaic/utilities/Parameters.java
==============================================================================
--- trunk/src/wosaic/utilities/Parameters.java (original)
+++ trunk/src/wosaic/utilities/Parameters.java Wed Sep 10 11:28:50 2008
@@ -37,6 +37,13 @@
* Physical width of each segment
*/
public int sWidth;
+
+ public static enum Algorithm {
+ BestFit,
+ NoRepeats
+ }
+
+ public Algorithm alg;

/**
* Creates a fully initialized parameter set.

Modified: trunk/src/wosaic/utilities/Pixel.java
==============================================================================
--- trunk/src/wosaic/utilities/Pixel.java (original)
+++ trunk/src/wosaic/utilities/Pixel.java Wed Sep 10 11:28:50 2008
@@ -43,6 +43,8 @@
* The image's current width.
*/
public int width;
+
+ public int used = 0;

/**
* Creates a Pixel object from a BufferedImage
@@ -185,6 +187,14 @@
public Raster getImageRaster() {
if (cachedRaster == null) cachedRaster = image.getData();
return cachedRaster;
+ }
+
+ /**
+ *
+ * @return the buffered image for this pixel
+ */
+ public BufferedImage getImage() {
+ return image;
}

/**

Reply all
Reply to author
Forward
0 new messages