Home | History | Annotate | Download | only in filters
      1 package com.android.gallery3d.filtershow.filters;
      2 
      3 import android.util.JsonReader;
      4 import android.util.JsonWriter;
      5 import android.util.Log;
      6 
      7 import com.android.gallery3d.R;
      8 import com.android.gallery3d.filtershow.imageshow.ControlPoint;
      9 import com.android.gallery3d.filtershow.imageshow.Spline;
     10 
     11 import java.io.IOException;
     12 
     13 /**
     14  * TODO: Insert description here. (generated by hoford)
     15  */
     16 public class FilterCurvesRepresentation extends FilterRepresentation {
     17     private static final String LOGTAG = "FilterCurvesRepresentation";
     18     public static final String SERIALIZATION_NAME = "Curve";
     19     private static final int MAX_SPLINE_NUMBER = 4;
     20 
     21     private Spline[] mSplines = new Spline[MAX_SPLINE_NUMBER];
     22 
     23     public FilterCurvesRepresentation() {
     24         super("Curves");
     25         setSerializationName("CURVES");
     26         setFilterClass(ImageFilterCurves.class);
     27         setTextId(R.string.curvesRGB);
     28         setOverlayId(R.drawable.filtershow_button_colors_curve);
     29         setEditorId(R.id.imageCurves);
     30         setShowParameterValue(false);
     31         setSupportsPartialRendering(true);
     32         reset();
     33     }
     34 
     35     @Override
     36     public FilterRepresentation copy() {
     37         FilterCurvesRepresentation representation = new FilterCurvesRepresentation();
     38         copyAllParameters(representation);
     39         return representation;
     40     }
     41 
     42     @Override
     43     protected void copyAllParameters(FilterRepresentation representation) {
     44         super.copyAllParameters(representation);
     45         representation.useParametersFrom(this);
     46     }
     47 
     48     @Override
     49     public void useParametersFrom(FilterRepresentation a) {
     50         if (!(a instanceof FilterCurvesRepresentation)) {
     51             Log.v(LOGTAG, "cannot use parameters from " + a);
     52             return;
     53         }
     54         FilterCurvesRepresentation representation = (FilterCurvesRepresentation) a;
     55         Spline[] spline = new Spline[MAX_SPLINE_NUMBER];
     56         for (int i = 0; i < spline.length; i++) {
     57             Spline sp = representation.mSplines[i];
     58             if (sp != null) {
     59                 spline[i] = new Spline(sp);
     60             } else {
     61                 spline[i] = new Spline();
     62             }
     63         }
     64         mSplines = spline;
     65     }
     66 
     67     @Override
     68     public boolean isNil() {
     69         for (int i = 0; i < MAX_SPLINE_NUMBER; i++) {
     70             if (getSpline(i) != null && !getSpline(i).isOriginal()) {
     71                 return false;
     72             }
     73         }
     74         return true;
     75     }
     76 
     77     @Override
     78     public boolean equals(FilterRepresentation representation) {
     79         if (!super.equals(representation)) {
     80             return false;
     81         }
     82 
     83         if (!(representation instanceof FilterCurvesRepresentation)) {
     84             return false;
     85         } else {
     86             FilterCurvesRepresentation curve =
     87                     (FilterCurvesRepresentation) representation;
     88             for (int i = 0; i < MAX_SPLINE_NUMBER; i++) {
     89                 if (!getSpline(i).sameValues(curve.getSpline(i))) {
     90                     return false;
     91                 }
     92             }
     93         }
     94         // Every spline matches, therefore they are the same.
     95         return true;
     96     }
     97 
     98     public void reset() {
     99         Spline spline = new Spline();
    100 
    101         spline.addPoint(0.0f, 1.0f);
    102         spline.addPoint(1.0f, 0.0f);
    103 
    104         for (int i = 0; i < MAX_SPLINE_NUMBER; i++) {
    105             mSplines[i] = new Spline(spline);
    106         }
    107     }
    108 
    109     public void setSpline(int splineIndex, Spline s) {
    110         mSplines[splineIndex] = s;
    111     }
    112 
    113     public Spline getSpline(int splineIndex) {
    114         return mSplines[splineIndex];
    115     }
    116 
    117     @Override
    118     public void serializeRepresentation(JsonWriter writer) throws IOException {
    119         writer.beginObject();
    120         {
    121             writer.name(NAME_TAG);
    122             writer.value(getName());
    123             for (int i = 0; i < mSplines.length; i++) {
    124                 writer.name(SERIALIZATION_NAME + i);
    125                 writer.beginArray();
    126                 int nop = mSplines[i].getNbPoints();
    127                 for (int j = 0; j < nop; j++) {
    128                     ControlPoint p = mSplines[i].getPoint(j);
    129                     writer.beginArray();
    130                     writer.value(p.x);
    131                     writer.value(p.y);
    132                     writer.endArray();
    133                 }
    134                 writer.endArray();
    135             }
    136 
    137         }
    138         writer.endObject();
    139     }
    140 
    141     @Override
    142     public void deSerializeRepresentation(JsonReader sreader) throws IOException {
    143         sreader.beginObject();
    144         Spline[] spline = new Spline[MAX_SPLINE_NUMBER];
    145         while (sreader.hasNext()) {
    146             String name = sreader.nextName();
    147             if (NAME_TAG.equals(name)) {
    148                 setName(sreader.nextString());
    149             } else if (name.startsWith(SERIALIZATION_NAME)) {
    150                 int curveNo = Integer.parseInt(name.substring(SERIALIZATION_NAME.length()));
    151                 spline[curveNo] = new Spline();
    152                 sreader.beginArray();
    153                 while (sreader.hasNext()) {
    154                     sreader.beginArray();
    155                     sreader.hasNext();
    156                     float x = (float) sreader.nextDouble();
    157                     sreader.hasNext();
    158                     float y = (float) sreader.nextDouble();
    159                     sreader.endArray();
    160                     spline[curveNo].addPoint(x, y);
    161                 }
    162                 sreader.endArray();
    163 
    164             }
    165         }
    166         mSplines = spline;
    167         sreader.endObject();
    168     }
    169 
    170 }
    171