OSDN Git Service

support non destructive edits of Curves
authorJohn Hoford <hoford@google.com>
Mon, 24 Jun 2013 22:13:13 +0000 (15:13 -0700)
committerJohn Hoford <hoford@google.com>
Wed, 26 Jun 2013 23:52:37 +0000 (16:52 -0700)
Change-Id: I597be6570a17072a4116bf6227a6a35796266bc3

src/com/android/gallery3d/filtershow/filters/FilterCurvesRepresentation.java
src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
src/com/android/gallery3d/filtershow/presets/ImagePreset.java

index 4554f9b..a71aa88 100644 (file)
@@ -1,17 +1,24 @@
 package com.android.gallery3d.filtershow.filters;
 
+import android.util.JsonReader;
+import android.util.JsonWriter;
 import android.util.Log;
 
 import com.android.gallery3d.R;
+import com.android.gallery3d.filtershow.ui.ControlPoint;
 import com.android.gallery3d.filtershow.ui.Spline;
 
+import java.io.IOException;
+
 /**
  * TODO: Insert description here. (generated by hoford)
  */
 public class FilterCurvesRepresentation extends FilterRepresentation {
     private static final String LOGTAG = "FilterCurvesRepresentation";
+    public static final String SERIALIZATION_NAME = "Curve";
+    private static final int MAX_SPLINE_NUMBER = 4;
 
-    private Spline[] mSplines = new Spline[4];
+    private Spline[] mSplines = new Spline[MAX_SPLINE_NUMBER];
 
     public FilterCurvesRepresentation() {
         super("Curves");
@@ -39,7 +46,7 @@ public class FilterCurvesRepresentation extends FilterRepresentation {
             return;
         }
         FilterCurvesRepresentation representation = (FilterCurvesRepresentation) a;
-        Spline[] spline = new Spline[4];
+        Spline[] spline = new Spline[MAX_SPLINE_NUMBER];
         for (int i = 0; i < spline.length; i++) {
             Spline sp = representation.mSplines[i];
             if (sp != null) {
@@ -52,7 +59,7 @@ public class FilterCurvesRepresentation extends FilterRepresentation {
     }
 
     public boolean isNil() {
-        for (int i = 0; i < 4; i++) {
+        for (int i = 0; i < MAX_SPLINE_NUMBER; i++) {
             if (getSpline(i) != null && !getSpline(i).isOriginal()) {
                 return false;
             }
@@ -66,7 +73,7 @@ public class FilterCurvesRepresentation extends FilterRepresentation {
         spline.addPoint(0.0f, 1.0f);
         spline.addPoint(1.0f, 0.0f);
 
-        for (int i = 0; i < 4; i++) {
+        for (int i = 0; i < MAX_SPLINE_NUMBER; i++) {
             mSplines[i] = new Spline(spline);
         }
     }
@@ -77,4 +84,58 @@ public class FilterCurvesRepresentation extends FilterRepresentation {
     public Spline getSpline(int splineIndex) {
         return mSplines[splineIndex];
     }
+
+    @Override
+    public void serializeRepresentation(JsonWriter writer) throws IOException {
+        writer.beginObject();
+        {
+            writer.name(NAME_TAG);
+            writer.value(getName());
+            for (int i = 0; i < mSplines.length; i++) {
+                writer.name(SERIALIZATION_NAME + i);
+                writer.beginArray();
+                int nop = mSplines[i].getNbPoints();
+                for (int j = 0; j < nop; j++) {
+                    ControlPoint p = mSplines[i].getPoint(j);
+                    writer.beginArray();
+                    writer.value(p.x);
+                    writer.value(p.y);
+                    writer.endArray();
+                }
+                writer.endArray();
+            }
+
+        }
+        writer.endObject();
+    }
+
+    @Override
+    public void deSerializeRepresentation(JsonReader sreader) throws IOException {
+        sreader.beginObject();
+        Spline[] spline = new Spline[MAX_SPLINE_NUMBER];
+        while (sreader.hasNext()) {
+            String name = sreader.nextName();
+            if (NAME_TAG.equals(name)) {
+                setName(sreader.nextString());
+            } else if (name.startsWith(SERIALIZATION_NAME)) {
+                int curveNo = Integer.parseInt(name.substring(SERIALIZATION_NAME.length()));
+                spline[curveNo] = new Spline();
+                sreader.beginArray();
+                while (sreader.hasNext()) {
+                    sreader.beginArray();
+                    sreader.hasNext();
+                    float x = (float) sreader.nextDouble();
+                    sreader.hasNext();
+                    float y = (float) sreader.nextDouble();
+                    sreader.endArray();
+                    spline[curveNo].addPoint(x, y);
+                }
+                sreader.endArray();
+
+            }
+        }
+        mSplines = spline;
+        sreader.endObject();
+    }
+
 }
index b192b5a..096beda 100644 (file)
 
 package com.android.gallery3d.filtershow.filters;
 
+import android.util.JsonReader;
+import android.util.JsonWriter;
 import android.util.Log;
 
 import com.android.gallery3d.filtershow.editors.BasicEditor;
 
+import java.io.IOException;
+import java.util.ArrayList;
+
 public class FilterRepresentation implements Cloneable {
     private static final String LOGTAG = "FilterRepresentation";
     private static final boolean DEBUG = false;
@@ -40,6 +45,7 @@ public class FilterRepresentation implements Cloneable {
     public static final byte TYPE_VIGNETTE = 4;
     public static final byte TYPE_NORMAL = 5;
     public static final byte TYPE_TINYPLANET = 6;
+    protected static final String NAME_TAG = "Name";
 
     private FilterRepresentation mTempRepresentation = null;
 
@@ -222,14 +228,47 @@ public class FilterRepresentation implements Cloneable {
         return "";
     }
 
+    /**
+     * Method must "beginObject()" add its info and "endObject()"
+     * @param writer
+     * @throws IOException
+     */
+    public void serializeRepresentation(JsonWriter writer) throws IOException {
+        writer.beginObject();
+        {
+            String[][] rep = serializeRepresentation();
+            for (int k = 0; k < rep.length; k++) {
+                writer.name(rep[k][0]);
+                writer.value(rep[k][1]);
+            }
+        }
+        writer.endObject();
+    }
+
+    // this is the old way of doing this and will be removed soon
     public String[][] serializeRepresentation() {
-        String[][] ret = { { "Name" , getName() }};
+        String[][] ret = {{NAME_TAG, getName()}};
         return ret;
     }
 
+    public void deSerializeRepresentation(JsonReader reader) throws IOException {
+        ArrayList<String[]> al = new ArrayList<String[]>();
+        reader.beginObject();
+        while (reader.hasNext()) {
+            String[] kv = {reader.nextName(), reader.nextString()};
+            al.add(kv);
+
+        }
+        reader.endObject();
+        String[][] oldFormat = al.toArray(new String[al.size()][]);
+
+        deSerializeRepresentation(oldFormat);
+    }
+
+    // this is the old way of doing this and will be removed soon
     public void deSerializeRepresentation(String[][] rep) {
         for (int i = 0; i < rep.length; i++) {
-            if ("Name".equals(rep[i][0])) {
+            if (NAME_TAG.equals(rep[i][0])) {
                 mName = rep[i][1];
                 break;
             }
index 11502af..80dbc7b 100644 (file)
@@ -65,7 +65,7 @@ public class ImagePreset {
 
     private boolean mPartialRendering = false;
     private Rect mPartialRenderingBounds;
-
+    private static final boolean DEBUG = false;
     private Bitmap mPreviewImage;
 
     public ImagePreset() {
@@ -643,10 +643,9 @@ public class ImagePreset {
             JsonWriter writer = new JsonWriter(swriter);
             writeJson(writer, name);
             writer.close();
-        } catch (Exception e) {
-            e.printStackTrace();
+        } catch (IOException e) {
+            return null;
         }
-
         return swriter.toString();
     }
 
@@ -673,16 +672,14 @@ public class ImagePreset {
                     continue;
                 }
                 String sname = filter.getSerializationName();
-                writer.name(sname);
-                writer.beginObject();
-                {
-                    String[][] rep = filter.serializeRepresentation();
-                    for (int k = 0; k < rep.length; k++) {
-                        writer.name(rep[k][0]);
-                        writer.value(rep[k][1]);
+                if (DEBUG) {
+                    Log.v(LOGTAG, "Serialization: " + sname);
+                    if (sname == null) {
+                        Log.v(LOGTAG, "Serialization: " + filter);
                     }
                 }
-                writer.endObject();
+                writer.name(sname);
+                filter.serializeRepresentation(writer);
             }
             writer.endObject();
 
@@ -691,21 +688,36 @@ public class ImagePreset {
         }
     }
 
+    /**
+     * populates preset from JSON string
+     * @param filterString a JSON string
+     * @return true on success if false ImagePreset is undefined
+     */
     public boolean readJsonFromString(String filterString) {
+        if (DEBUG) {
+            Log.v(LOGTAG,"reading preset: \""+filterString+"\"");
+        }
         StringReader sreader = new StringReader(filterString);
         try {
             JsonReader reader = new JsonReader(sreader);
             boolean ok = readJson(reader);
             if (!ok) {
+                reader.close();
                 return false;
             }
             reader.close();
         } catch (Exception e) {
-            e.printStackTrace();
+            Log.e(LOGTAG,"parsing the filter parameters:",e);
+            return false;
         }
         return true;
     }
 
+    /**
+     * populates preset from JSON stream
+     * @param sreader a JSON string
+     * @return true on success if false ImagePreset is undefined
+     */
     public boolean readJson(JsonReader sreader) throws IOException {
         sreader.beginObject();
         sreader.nextName();
@@ -715,6 +727,7 @@ public class ImagePreset {
             String name = sreader.nextName();
             FilterRepresentation filter = creatFilterFromName(name);
             if (filter == null) {
+                Log.w(LOGTAG,"UNKNOWN FILTER! "+name);
                 return false;
             }
             filter.deSerializeRepresentation(read(sreader));