[Patchanim-commit] SF.net SVN: patchanim: [26] trunk/patchanim/src/com/mebigfatguy/patchanim
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2008-01-27 00:17:29
|
Revision: 26
http://patchanim.svn.sourceforge.net/patchanim/?rev=26&view=rev
Author: dbrosius
Date: 2008-01-26 16:17:33 -0800 (Sat, 26 Jan 2008)
Log Message:
-----------
break out surface routines to a separate class
Modified Paths:
--------------
trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchSamplePanel.java
Added Paths:
-----------
trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchGenerator.java
Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchSamplePanel.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchSamplePanel.java 2008-01-26 07:16:38 UTC (rev 25)
+++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchSamplePanel.java 2008-01-27 00:17:33 UTC (rev 26)
@@ -22,12 +22,10 @@
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
-import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
-import java.awt.image.IndexColorModel;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
@@ -35,11 +33,10 @@
import com.mebigfatguy.patchanim.PatchColor;
import com.mebigfatguy.patchanim.surface.CombinedPatch;
-import com.mebigfatguy.patchanim.surface.PatchCoords;
+import com.mebigfatguy.patchanim.surface.PatchGenerator;
public class JPatchSamplePanel extends JPanel {
private static final int SAMPLE_SIZE = 200;
- private static final int ORDER = 4;
private PatchColor color;
private Color rgb;
private BufferedImage image;
@@ -67,7 +64,7 @@
}
private void initComponents() {
- buildImage();
+ image = PatchGenerator.buildImage(rgb, SAMPLE_SIZE, SAMPLE_SIZE);
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
Dimension d = new Dimension(SAMPLE_SIZE, SAMPLE_SIZE);
setMinimumSize(d);
@@ -100,9 +97,9 @@
Thread t = new Thread(new Runnable() {
public void run() {
if (color == PatchColor.Combined) {
- recalcCombinedImage(patch);
+ PatchGenerator.recalcCombinedImage(patch, image);
} else {
- recalcIndexedImage(color, patch);
+ PatchGenerator.recalcIndexedImage(color, patch, image);
}
SwingUtilities.invokeLater(new Runnable() {
@@ -117,150 +114,6 @@
t.start();
}
- private void recalcCombinedImage(CombinedPatch patch) {
- PatchCoords[] coords = new PatchCoords[3];
-
- coords[0] = patch.getPatch(PatchColor.Red);
- coords[1] = patch.getPatch(PatchColor.Green);
- coords[2] = patch.getPatch(PatchColor.Blue);
-
- double u, u2, u3, oneMinusU, oneMinusU2, oneMinusU3;
- double v, v2, v3, oneMinusV, oneMinusV2, oneMinusV3;
- double[] uCoeffs = new double[ORDER];
- double[] vCoeffs = new double[ORDER];
- double[] value = new double[3];
- int[] iValue = new int[3];
-
- for (int iu = 0; iu < SAMPLE_SIZE; iu++) {
- u = (double)iu / (double)SAMPLE_SIZE;
- u2 = u * u;
- u3 = u2 * u;
- oneMinusU = 1.0 - u;
- oneMinusU2 = oneMinusU * oneMinusU;
- oneMinusU3 = oneMinusU2 * oneMinusU;
-
- uCoeffs[0] = oneMinusU3;
- uCoeffs[1] = 3.0 * u * oneMinusU2;
- uCoeffs[2] = 3.0 * u2 * oneMinusU;
- uCoeffs[3] = u3;
-
- for (int iv = 0; iv < SAMPLE_SIZE; iv++) {
- v = (double)iv / (double)SAMPLE_SIZE;
- v2 = v * v;
- v3 = v2 * v;
- oneMinusV = 1.0 - v;
- oneMinusV2 = oneMinusV * oneMinusV;
- oneMinusV3 = oneMinusV2 * oneMinusV;
-
- vCoeffs[0] = oneMinusV3;
- vCoeffs[1] = 3.0 * v * oneMinusV2;
- vCoeffs[2] = 3.0 * v2 * oneMinusV;
- vCoeffs[3] = v3;
-
- value[0] = value[1] = value[2] = 0.0;
-
- for (int i = 0; i < ORDER; i++) {
- for (int j = 0; j < ORDER; j++) {
- double coeff = uCoeffs[i] * vCoeffs[j];
- for (int k = 0; k < 3; k++) {
- value[k] += coords[k].getCoordinate(i, j).getColor() * coeff;
- }
- }
- }
-
- for (int k = 0; k < 3; k++) {
- iValue[k] = (int)value[k];
- if (iValue[k] > 255)
- iValue[k] = 255;
- else if (iValue[k] < 0)
- iValue[k] = 0;
- }
- int compValue = 0xFF000000 | (iValue[0] << 16) | (iValue[1] << 8) | iValue[2];
- image.setRGB(iu, iv, compValue);
- }
- }
- }
-
- private void recalcIndexedImage(PatchColor color, CombinedPatch patch) {
- PatchCoords coords = patch.getPatch(color);
-
- double u, u2, u3, oneMinusU, oneMinusU2, oneMinusU3;
- double v, v2, v3, oneMinusV, oneMinusV2, oneMinusV3;
- double[] uCoeffs = new double[ORDER];
- double[] vCoeffs = new double[ORDER];
-
- for (int iu = 0; iu < SAMPLE_SIZE; iu++) {
- u = (double)iu / (double)SAMPLE_SIZE;
- u2 = u * u;
- u3 = u2 * u;
- oneMinusU = 1.0 - u;
- oneMinusU2 = oneMinusU * oneMinusU;
- oneMinusU3 = oneMinusU2 * oneMinusU;
-
- uCoeffs[0] = oneMinusU3;
- uCoeffs[1] = 3.0 * u * oneMinusU2;
- uCoeffs[2] = 3.0 * u2 * oneMinusU;
- uCoeffs[3] = u3;
-
- for (int iv = 0; iv < SAMPLE_SIZE; iv++) {
- v = (double)iv / (double)SAMPLE_SIZE;
- v2 = v * v;
- v3 = v2 * v;
- oneMinusV = 1.0 - v;
- oneMinusV2 = oneMinusV * oneMinusV;
- oneMinusV3 = oneMinusV2 * oneMinusV;
-
- vCoeffs[0] = oneMinusV3;
- vCoeffs[1] = 3.0 * v * oneMinusV2;
- vCoeffs[2] = 3.0 * v2 * oneMinusV;
- vCoeffs[3] = v3;
-
- double value = 0.0;
- for (int i = 0; i < ORDER; i++) {
- for (int j = 0; j < ORDER; j++) {
- value += coords.getCoordinate(i, j).getColor() * uCoeffs[i] * vCoeffs[j];
- }
- }
-
- int iValue = (int)value;
- if (iValue > 255)
- iValue = 255;
- else if (iValue < 0)
- iValue = 0;
- if (color == PatchColor.Red)
- iValue <<= 16;
- else if (color == PatchColor.Green)
- iValue <<= 8;
- iValue |= 0xFF000000;
-
- image.setRGB(iu, iv, iValue);
- }
- }
- }
-
- private void buildImage() {
- if (rgb == null) {
- image = new BufferedImage(SAMPLE_SIZE, SAMPLE_SIZE, BufferedImage.TYPE_3BYTE_BGR);
- } else {
- byte[] r = new byte[256];
- byte[] g = new byte[256];
- byte[] b = new byte[256];
-
- for (int i = 0; i < 256; i++) {
- r[i] = (byte)((i * rgb.getRed()) / 255);
- g[i] = (byte)((i * rgb.getGreen()) / 255);
- b[i] = (byte)((i * rgb.getBlue()) / 255);
- }
- IndexColorModel icm = new IndexColorModel(8, 256, r, g, b);
-
- image = new BufferedImage(SAMPLE_SIZE, SAMPLE_SIZE, BufferedImage.TYPE_BYTE_INDEXED, icm);
- }
-
- Graphics graphics = image.getGraphics();
- graphics.setColor(rgb);
- graphics.fillRect(0, 0, SAMPLE_SIZE, SAMPLE_SIZE);
- }
-
@Override
public void paintComponent(Graphics g) {
Rectangle bounds = getBounds();
Added: trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchGenerator.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchGenerator.java (rev 0)
+++ trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchGenerator.java 2008-01-27 00:17:33 UTC (rev 26)
@@ -0,0 +1,187 @@
+/*
+ * patchanim - A bezier surface patch color blend gif builder
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package com.mebigfatguy.patchanim.surface;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.image.BufferedImage;
+import java.awt.image.IndexColorModel;
+
+import com.mebigfatguy.patchanim.PatchColor;
+
+public class PatchGenerator {
+ private static final int ORDER = 4;
+
+ private PatchGenerator() {
+ }
+
+ public static void recalcCombinedImage(CombinedPatch patch, BufferedImage image) {
+ PatchCoords[] coords = new PatchCoords[3];
+
+ coords[0] = patch.getPatch(PatchColor.Red);
+ coords[1] = patch.getPatch(PatchColor.Green);
+ coords[2] = patch.getPatch(PatchColor.Blue);
+
+ double u, u2, u3, oneMinusU, oneMinusU2, oneMinusU3;
+ double v, v2, v3, oneMinusV, oneMinusV2, oneMinusV3;
+ double[] uCoeffs = new double[ORDER];
+ double[] vCoeffs = new double[ORDER];
+ double[] value = new double[3];
+ int[] iValue = new int[3];
+
+ int sampleSizeX = image.getWidth();
+ int sampleSizeY = image.getHeight();
+
+ for (int iu = 0; iu < sampleSizeX; iu++) {
+ u = (double)iu / (double)sampleSizeX;
+ u2 = u * u;
+ u3 = u2 * u;
+ oneMinusU = 1.0 - u;
+ oneMinusU2 = oneMinusU * oneMinusU;
+ oneMinusU3 = oneMinusU2 * oneMinusU;
+
+ uCoeffs[0] = oneMinusU3;
+ uCoeffs[1] = 3.0 * u * oneMinusU2;
+ uCoeffs[2] = 3.0 * u2 * oneMinusU;
+ uCoeffs[3] = u3;
+
+ for (int iv = 0; iv < sampleSizeY; iv++) {
+ v = (double)iv / (double)sampleSizeY;
+ v2 = v * v;
+ v3 = v2 * v;
+ oneMinusV = 1.0 - v;
+ oneMinusV2 = oneMinusV * oneMinusV;
+ oneMinusV3 = oneMinusV2 * oneMinusV;
+
+ vCoeffs[0] = oneMinusV3;
+ vCoeffs[1] = 3.0 * v * oneMinusV2;
+ vCoeffs[2] = 3.0 * v2 * oneMinusV;
+ vCoeffs[3] = v3;
+
+ value[0] = value[1] = value[2] = 0.0;
+
+ for (int i = 0; i < ORDER; i++) {
+ for (int j = 0; j < ORDER; j++) {
+ double coeff = uCoeffs[i] * vCoeffs[j];
+ for (int k = 0; k < 3; k++) {
+ value[k] += coords[k].getCoordinate(i, j).getColor() * coeff;
+ }
+ }
+ }
+
+ for (int k = 0; k < 3; k++) {
+ iValue[k] = (int)value[k];
+ if (iValue[k] > 255)
+ iValue[k] = 255;
+ else if (iValue[k] < 0)
+ iValue[k] = 0;
+ }
+ int compValue = 0xFF000000 | (iValue[0] << 16) | (iValue[1] << 8) | iValue[2];
+ image.setRGB(iu, iv, compValue);
+ }
+ }
+ }
+
+ public static void recalcIndexedImage(PatchColor color, CombinedPatch patch, BufferedImage image) {
+ PatchCoords coords = patch.getPatch(color);
+
+ double u, u2, u3, oneMinusU, oneMinusU2, oneMinusU3;
+ double v, v2, v3, oneMinusV, oneMinusV2, oneMinusV3;
+ double[] uCoeffs = new double[ORDER];
+ double[] vCoeffs = new double[ORDER];
+
+ int sampleSizeX = image.getWidth();
+ int sampleSizeY = image.getHeight();
+
+ for (int iu = 0; iu < sampleSizeX; iu++) {
+ u = (double)iu / (double)sampleSizeX;
+ u2 = u * u;
+ u3 = u2 * u;
+ oneMinusU = 1.0 - u;
+ oneMinusU2 = oneMinusU * oneMinusU;
+ oneMinusU3 = oneMinusU2 * oneMinusU;
+
+ uCoeffs[0] = oneMinusU3;
+ uCoeffs[1] = 3.0 * u * oneMinusU2;
+ uCoeffs[2] = 3.0 * u2 * oneMinusU;
+ uCoeffs[3] = u3;
+
+ for (int iv = 0; iv < sampleSizeY; iv++) {
+ v = (double)iv / (double)sampleSizeY;
+ v2 = v * v;
+ v3 = v2 * v;
+ oneMinusV = 1.0 - v;
+ oneMinusV2 = oneMinusV * oneMinusV;
+ oneMinusV3 = oneMinusV2 * oneMinusV;
+
+ vCoeffs[0] = oneMinusV3;
+ vCoeffs[1] = 3.0 * v * oneMinusV2;
+ vCoeffs[2] = 3.0 * v2 * oneMinusV;
+ vCoeffs[3] = v3;
+
+ double value = 0.0;
+ for (int i = 0; i < ORDER; i++) {
+ for (int j = 0; j < ORDER; j++) {
+ value += coords.getCoordinate(i, j).getColor() * uCoeffs[i] * vCoeffs[j];
+ }
+ }
+
+ int iValue = (int)value;
+ if (iValue > 255)
+ iValue = 255;
+ else if (iValue < 0)
+ iValue = 0;
+ if (color == PatchColor.Red)
+ iValue <<= 16;
+ else if (color == PatchColor.Green)
+ iValue <<= 8;
+ iValue |= 0xFF000000;
+
+ image.setRGB(iu, iv, iValue);
+ }
+ }
+ }
+
+ public static BufferedImage buildImage(Color color, int sampleSizeX, int sampleSizeY) {
+ BufferedImage image = null;
+
+ if (color == null) {
+ image = new BufferedImage(sampleSizeX, sampleSizeY, BufferedImage.TYPE_3BYTE_BGR);
+ } else {
+ byte[] r = new byte[256];
+ byte[] g = new byte[256];
+ byte[] b = new byte[256];
+
+ for (int i = 0; i < 256; i++) {
+ r[i] = (byte)((i * color.getRed()) / 255);
+ g[i] = (byte)((i * color.getGreen()) / 255);
+ b[i] = (byte)((i * color.getBlue()) / 255);
+ }
+ IndexColorModel icm = new IndexColorModel(8, 256, r, g, b);
+
+ image = new BufferedImage(sampleSizeX, sampleSizeY, BufferedImage.TYPE_BYTE_INDEXED, icm);
+ }
+
+ Graphics graphics = image.getGraphics();
+ graphics.setColor(color);
+ graphics.fillRect(0, 0, sampleSizeX, sampleSizeY);
+ return image;
+ }
+
+}
Property changes on: trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchGenerator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|