From 336647b95b0296fb69445aaca14794f0752e3ca5 Mon Sep 17 00:00:00 2001 From: "T. van der Zwan" Date: Mon, 25 Nov 2013 12:16:16 +0100 Subject: [PATCH] Added multi-color specifications to the model of HyperCon (not GUI yet) Former-commit-id: 3b3f38f1514bd2925e043333555461b3af1f7d88 --- .../hyperion/hypercon/ConfigurationFile.java | 107 +++++++++++++++--- .../org/hyperion/hypercon/gui/ColorPanel.java | 5 +- .../hyperion/hypercon/spec/ColorConfig.java | 98 +++------------- .../hypercon/spec/TransformConfig.java | 101 +++++++++++++++++ 4 files changed, 210 insertions(+), 101 deletions(-) create mode 100644 src/config-tool/ConfigTool/src/org/hyperion/hypercon/spec/TransformConfig.java diff --git a/src/config-tool/ConfigTool/src/org/hyperion/hypercon/ConfigurationFile.java b/src/config-tool/ConfigTool/src/org/hyperion/hypercon/ConfigurationFile.java index 285978f8..1abd46c1 100644 --- a/src/config-tool/ConfigTool/src/org/hyperion/hypercon/ConfigurationFile.java +++ b/src/config-tool/ConfigTool/src/org/hyperion/hypercon/ConfigurationFile.java @@ -8,19 +8,19 @@ import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; import java.util.Properties; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; +import java.util.Vector; public class ConfigurationFile { - private final Properties pProps = new Properties(); + private final Properties mProps = new Properties(); public void load(String pFilename) { - pProps.clear(); + mProps.clear(); // try (InputStream in = new InflaterInputStream(new FileInputStream(pFilename))){ - try (InputStream in = new GZIPInputStream(new FileInputStream(pFilename))){ -// try (InputStream in = new FileInputStream(pFilename)) { - pProps.load(in); +// try (InputStream in = new GZIPInputStream(new FileInputStream(pFilename))){ + try (InputStream in = new FileInputStream(pFilename)) { + mProps.load(in); } catch (Throwable t) { // TODO Auto-generated catch block t.printStackTrace(); @@ -29,15 +29,18 @@ public class ConfigurationFile { public void save(String pFilename) { // try (OutputStream out = new DeflaterOutputStream(new FileOutputStream(pFilename))) { - try (OutputStream out = new GZIPOutputStream(new FileOutputStream(pFilename))) { -// try (OutputStream out = (new FileOutputStream(pFilename))) { - pProps.store(out, "Pesistent settings file for HyperCon"); +// try (OutputStream out = new GZIPOutputStream(new FileOutputStream(pFilename))) { + try (OutputStream out = (new FileOutputStream(pFilename))) { + mProps.store(out, "Pesistent settings file for HyperCon"); } catch (IOException e) { e.printStackTrace(); } } public void store(Object pObj) { + store(pObj, pObj.getClass().getSimpleName(), ""); + } + public void store(Object pObj, String preamble, String postamble) { String className = pObj.getClass().getSimpleName(); // Retrieve the member variables Field[] fields = pObj.getClass().getDeclaredFields(); @@ -48,27 +51,99 @@ public class ConfigurationFile { continue; } - String key = className + "." + field.getName(); + String key = preamble + "." + field.getName() + postamble; try { Object value = field.get(pObj); if (value.getClass().isEnum()) { - pProps.setProperty(key, ((Enum)value).name()); + mProps.setProperty(key, ((Enum)value).name()); + } else if (value.getClass().isAssignableFrom(Vector.class)) { + @SuppressWarnings("unchecked") + Vector v = (Vector) value; + for (int i=0; i vector; + try { + vector = (Vector)field.get(pObj); + } catch (Throwable t) { + t.printStackTrace(); + break; + } + // Clear existing elements from the vector + vector.clear(); + + // Iterate through the properties to find the indices of the vector + int i=0; + while (true) { + String curIndexKey = pPreamble + field.getName() + "[" + i + "]"; + Properties elemProps = new Properties(); + // Find all the elements for the current vector index + for (Object keyObj : pProps.keySet()) { + String keyStr = (String)keyObj; + if (keyStr.startsWith(curIndexKey)) { + // Remove the name and dot + elemProps.put(keyStr.substring(curIndexKey.length()+1), pProps.get(keyStr)); + } + } + if (elemProps.isEmpty()) { + // Found no more elements for the vector + break; + } + + // Construct new instance of vectors generic type + ParameterizedType vectorElementType = (ParameterizedType) field.getGenericType(); + Class vectorElementClass = (Class) vectorElementType.getActualTypeArguments()[0]; + // Find the constructor with no arguments and create a new instance + Object newElement = null; + try { + newElement = vectorElementClass.getConstructor().newInstance(); + } catch (Throwable t) { + System.err.println("Failed to find empty default constructor for " + vectorElementClass.getName()); + break; + } + if (newElement == null) { + System.err.println("Failed to construct instance for " + vectorElementClass.getName()); + break; + } + + // Restore the instance members from the collected properties + restore(newElement, elemProps, ""); + + // Add the instance to the vector + vector.addElement(newElement); + + ++i; + } + + continue; + } + + String key = pPreamble + field.getName(); String value = pProps.getProperty(key); if (value == null) { System.out.println("Persistent settings does not contain value for " + key); @@ -97,6 +172,6 @@ public class ConfigurationFile { @Override public String toString() { - return pProps.toString(); + return mProps.toString(); } } diff --git a/src/config-tool/ConfigTool/src/org/hyperion/hypercon/gui/ColorPanel.java b/src/config-tool/ConfigTool/src/org/hyperion/hypercon/gui/ColorPanel.java index 531aad0f..52c16b16 100644 --- a/src/config-tool/ConfigTool/src/org/hyperion/hypercon/gui/ColorPanel.java +++ b/src/config-tool/ConfigTool/src/org/hyperion/hypercon/gui/ColorPanel.java @@ -16,6 +16,7 @@ import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import org.hyperion.hypercon.spec.ColorConfig; +import org.hyperion.hypercon.spec.TransformConfig; /** * Configuration panel for the ColorConfig. @@ -24,7 +25,7 @@ import org.hyperion.hypercon.spec.ColorConfig; */ public class ColorPanel extends JPanel { - private final ColorConfig mColorConfig; + private final TransformConfig mColorConfig; private JPanel mRgbTransformPanel; private JLabel mThresholdLabel; @@ -56,7 +57,7 @@ public class ColorPanel extends JPanel { public ColorPanel(ColorConfig pColorConfig) { super(); - mColorConfig = pColorConfig; + mColorConfig = pColorConfig.mTransforms.get(0); initialise(); } diff --git a/src/config-tool/ConfigTool/src/org/hyperion/hypercon/spec/ColorConfig.java b/src/config-tool/ConfigTool/src/org/hyperion/hypercon/spec/ColorConfig.java index ea4c2279..c9baf768 100644 --- a/src/config-tool/ConfigTool/src/org/hyperion/hypercon/spec/ColorConfig.java +++ b/src/config-tool/ConfigTool/src/org/hyperion/hypercon/spec/ColorConfig.java @@ -1,42 +1,19 @@ package org.hyperion.hypercon.spec; +import java.util.List; import java.util.Locale; +import java.util.Vector; /** * The color tuning parameters of the different color channels (both in RGB space as in HSV space) */ public class ColorConfig { - /** The saturation gain (in HSV space) */ - public double mSaturationGain = 1.0; - /** The value gain (in HSV space) */ - public double mValueGain = 1.5; - /** The minimum required RED-value (in RGB space) */ - public double mRedThreshold = 0.1; - /** The gamma-curve correct for the RED-value (in RGB space) */ - public double mRedGamma = 2.0; - /** The black-level of the RED-value (in RGB space) */ - public double mRedBlacklevel = 0.0; - /** The white-level of the RED-value (in RGB space) */ - public double mRedWhitelevel = 0.8; - - /** The minimum required GREEN-value (in RGB space) */ - public double mGreenThreshold = 0.1; - /** The gamma-curve correct for the GREEN-value (in RGB space) */ - public double mGreenGamma = 2.0; - /** The black-level of the GREEN-value (in RGB space) */ - public double mGreenBlacklevel = 0.0; - /** The white-level of the GREEN-value (in RGB space) */ - public double mGreenWhitelevel = 1.0; - - /** The minimum required BLUE-value (in RGB space) */ - public double mBlueThreshold = 0.1; - /** The gamma-curve correct for the BLUE-value (in RGB space) */ - public double mBlueGamma = 2.0; - /** The black-level of the BLUE-value (in RGB space) */ - public double mBlueBlacklevel = 0.0; - /** The white-level of the BLUE-value (in RGB space) */ - public double mBlueWhitelevel = 1.0; + /** List with color transformations */ + public List mTransforms = new Vector<>(); + { + mTransforms.add(new TransformConfig()); + } public boolean mSmoothingEnabled = false; /** The type of smoothing algorithm */ @@ -70,65 +47,20 @@ public class ColorConfig { strBuf.append("\t\"color\" :\n"); strBuf.append("\t{\n"); - strBuf.append(hsvToJsonString() + ",\n"); - strBuf.append(rgbToJsonString() + ",\n"); + + strBuf.append("\t\t\"transform\" :\n"); + strBuf.append("\t\t[\n"); + for (TransformConfig transform : mTransforms) { + strBuf.append(transform.toJsonString()); + } + strBuf.append("\t\t]\n"); + strBuf.append(smoothingToString() + "\n"); strBuf.append("\t}"); return strBuf.toString(); } - /** - * Creates the JSON string of the HSV-subconfiguration as used in the Hyperion deamon configfile - * - * @return The JSON string of the HSV-config - */ - private String hsvToJsonString() { - StringBuffer strBuf = new StringBuffer(); - strBuf.append("\t\t\"hsv\" :\n"); - strBuf.append("\t\t{\n"); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"saturationGain\" : %.4f,\n", mSaturationGain)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"valueGain\" : %.4f\n", mValueGain)); - - strBuf.append("\t\t}"); - return strBuf.toString(); - } - - /** - * Creates the JSON string of the RGB-subconfiguration as used in the Hyperion deamon configfile - * - * @return The JSON string of the RGB-config - */ - private String rgbToJsonString() { - StringBuffer strBuf = new StringBuffer(); - - strBuf.append("\t\t\"red\" :\n"); - strBuf.append("\t\t{\n"); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"threshold\" : %.4f,\n", mRedThreshold)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"gamma\" : %.4f,\n", mRedGamma)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"blacklevel\" : %.4f,\n", mRedBlacklevel)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"whitelevel\" : %.4f\n", mRedWhitelevel)); - strBuf.append("\t\t},\n"); - - strBuf.append("\t\t\"green\" :\n"); - strBuf.append("\t\t{\n"); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"threshold\" : %.4f,\n", mGreenThreshold)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"gamma\" : %.4f,\n", mGreenGamma)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"blacklevel\" : %.4f,\n", mGreenBlacklevel)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"whitelevel\" : %.4f\n", mGreenWhitelevel)); - strBuf.append("\t\t},\n"); - - strBuf.append("\t\t\"blue\" :\n"); - strBuf.append("\t\t{\n"); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"threshold\" : %.4f,\n", mBlueThreshold)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"gamma\" : %.4f,\n", mBlueGamma)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"blacklevel\" : %.4f,\n", mBlueBlacklevel)); - strBuf.append(String.format(Locale.ROOT, "\t\t\t\"whitelevel\" : %.4f\n", mBlueWhitelevel)); - strBuf.append("\t\t}"); - - return strBuf.toString(); - } - /** * Creates the JSON string of the smoothing subconfiguration as used in the Hyperion deamon configfile * diff --git a/src/config-tool/ConfigTool/src/org/hyperion/hypercon/spec/TransformConfig.java b/src/config-tool/ConfigTool/src/org/hyperion/hypercon/spec/TransformConfig.java new file mode 100644 index 00000000..e3ddf221 --- /dev/null +++ b/src/config-tool/ConfigTool/src/org/hyperion/hypercon/spec/TransformConfig.java @@ -0,0 +1,101 @@ +package org.hyperion.hypercon.spec; + +import java.util.Locale; + +public class TransformConfig { + /** The identifier of this ColorTransform configuration */ + public String mId = ""; + + /** The saturation gain (in HSV space) */ + public double mSaturationGain = 1.0; + /** The value gain (in HSV space) */ + public double mValueGain = 1.5; + + /** The minimum required RED-value (in RGB space) */ + public double mRedThreshold = 0.1; + /** The gamma-curve correct for the RED-value (in RGB space) */ + public double mRedGamma = 2.0; + /** The black-level of the RED-value (in RGB space) */ + public double mRedBlacklevel = 0.0; + /** The white-level of the RED-value (in RGB space) */ + public double mRedWhitelevel = 0.8; + + /** The minimum required GREEN-value (in RGB space) */ + public double mGreenThreshold = 0.1; + /** The gamma-curve correct for the GREEN-value (in RGB space) */ + public double mGreenGamma = 2.0; + /** The black-level of the GREEN-value (in RGB space) */ + public double mGreenBlacklevel = 0.0; + /** The white-level of the GREEN-value (in RGB space) */ + public double mGreenWhitelevel = 1.0; + + /** The minimum required BLUE-value (in RGB space) */ + public double mBlueThreshold = 0.1; + /** The gamma-curve correct for the BLUE-value (in RGB space) */ + public double mBlueGamma = 2.0; + /** The black-level of the BLUE-value (in RGB space) */ + public double mBlueBlacklevel = 0.0; + /** The white-level of the BLUE-value (in RGB space) */ + public double mBlueWhitelevel = 1.0; + + public String toJsonString() { + StringBuffer strBuf = new StringBuffer(); + + strBuf.append("\t\t{\n"); + strBuf.append(hsvToJsonString() + ",\n"); + strBuf.append(rgbToJsonString() + ",\n"); + strBuf.append("\t\t}"); + + return strBuf.toString(); + } + /** + * Creates the JSON string of the HSV-subconfiguration as used in the Hyperion deamon configfile + * + * @return The JSON string of the HSV-config + */ + private String hsvToJsonString() { + StringBuffer strBuf = new StringBuffer(); + strBuf.append("\t\t\t\"hsv\" :\n"); + strBuf.append("\t\t\t{\n"); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"saturationGain\" : %.4f,\n", mSaturationGain)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"valueGain\" : %.4f\n", mValueGain)); + + strBuf.append("\t\t\t}"); + return strBuf.toString(); + } + + /** + * Creates the JSON string of the RGB-subconfiguration as used in the Hyperion deamon configfile + * + * @return The JSON string of the RGB-config + */ + private String rgbToJsonString() { + StringBuffer strBuf = new StringBuffer(); + + strBuf.append("\t\t\t\"red\" :\n"); + strBuf.append("\t\t\t{\n"); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"threshold\" : %.4f,\n", mRedThreshold)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"gamma\" : %.4f,\n", mRedGamma)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"blacklevel\" : %.4f,\n", mRedBlacklevel)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"whitelevel\" : %.4f\n", mRedWhitelevel)); + strBuf.append("\t\t},\n"); + + strBuf.append("\t\t\t\"green\" :\n"); + strBuf.append("\t\t\t{\n"); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"threshold\" : %.4f,\n", mGreenThreshold)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"gamma\" : %.4f,\n", mGreenGamma)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"blacklevel\" : %.4f,\n", mGreenBlacklevel)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"whitelevel\" : %.4f\n", mGreenWhitelevel)); + strBuf.append("\t\t\t},\n"); + + strBuf.append("\t\t\t\"blue\" :\n"); + strBuf.append("\t\t\t{\n"); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"threshold\" : %.4f,\n", mBlueThreshold)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"gamma\" : %.4f,\n", mBlueGamma)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"blacklevel\" : %.4f,\n", mBlueBlacklevel)); + strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"whitelevel\" : %.4f\n", mBlueWhitelevel)); + strBuf.append("\t\t\t}"); + + return strBuf.toString(); + } +}