From ca0ecc627396c69bf21f6fae5b1e6b1ac3ea36d9 Mon Sep 17 00:00:00 2001 From: John Hoford Date: Mon, 24 Jun 2013 15:13:13 -0700 Subject: support non destructive edits of Curves Change-Id: I597be6570a17072a4116bf6227a6a35796266bc3 --- .../filters/FilterCurvesRepresentation.java | 69 ++++++++++++++++++++-- .../filtershow/filters/FilterRepresentation.java | 43 +++++++++++++- .../gallery3d/filtershow/presets/ImagePreset.java | 39 ++++++++---- 3 files changed, 132 insertions(+), 19 deletions(-) diff --git a/src/com/android/gallery3d/filtershow/filters/FilterCurvesRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterCurvesRepresentation.java index 4554f9b6d..a71aa8863 100644 --- a/src/com/android/gallery3d/filtershow/filters/FilterCurvesRepresentation.java +++ b/src/com/android/gallery3d/filtershow/filters/FilterCurvesRepresentation.java @@ -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(); + } + } diff --git a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java index b192b5aaa..096bedac9 100644 --- a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java +++ b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java @@ -16,10 +16,15 @@ 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 al = new ArrayList(); + 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; } diff --git a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java index 11502afda..80dbc7ba9 100644 --- a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java +++ b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java @@ -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)); -- cgit v1.2.3