summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/filters/FilterCurvesRepresentation.java
blob: edab2a08d5bee0672b98f8b3bf88a861016f7a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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.imageshow.ControlPoint;
import com.android.gallery3d.filtershow.imageshow.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[MAX_SPLINE_NUMBER];

    public FilterCurvesRepresentation() {
        super("Curves");
        setSerializationName("CURVES");
        setFilterClass(ImageFilterCurves.class);
        setTextId(R.string.curvesRGB);
        setOverlayId(R.drawable.filtershow_button_colors_curve);
        setEditorId(R.id.imageCurves);
        setShowParameterValue(false);
        setSupportsPartialRendering(true);
        reset();
    }

    @Override
    public FilterRepresentation copy() {
        FilterCurvesRepresentation representation = new FilterCurvesRepresentation();
        copyAllParameters(representation);
        return representation;
    }

    @Override
    protected void copyAllParameters(FilterRepresentation representation) {
        super.copyAllParameters(representation);
        representation.useParametersFrom(this);
    }

    @Override
    public void useParametersFrom(FilterRepresentation a) {
        if (!(a instanceof FilterCurvesRepresentation)) {
            Log.v(LOGTAG, "cannot use parameters from " + a);
            return;
        }
        FilterCurvesRepresentation representation = (FilterCurvesRepresentation) a;
        Spline[] spline = new Spline[MAX_SPLINE_NUMBER];
        for (int i = 0; i < spline.length; i++) {
            Spline sp = representation.mSplines[i];
            if (sp != null) {
                spline[i] = new Spline(sp);
            } else {
                spline[i] = new Spline();
            }
        }
        mSplines = spline;
    }

    @Override
    public boolean isNil() {
        for (int i = 0; i < MAX_SPLINE_NUMBER; i++) {
            if (getSpline(i) != null && !getSpline(i).isOriginal()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean equals(FilterRepresentation representation) {
        if (!super.equals(representation)) {
            return false;
        }

        if (!(representation instanceof FilterCurvesRepresentation)) {
            return false;
        } else {
            FilterCurvesRepresentation curve =
                    (FilterCurvesRepresentation) representation;
            for (int i = 0; i < MAX_SPLINE_NUMBER; i++) {
                if (!getSpline(i).sameValues(curve.getSpline(i))) {
                    return false;
                }
            }
        }
        // Every spline matches, therefore they are the same.
        return true;
    }

    public void reset() {
        Spline spline = new Spline();

        spline.addPoint(0.0f, 1.0f);
        spline.addPoint(1.0f, 0.0f);

        for (int i = 0; i < MAX_SPLINE_NUMBER; i++) {
            mSplines[i] = new Spline(spline);
        }
    }

    public void setSpline(int splineIndex, Spline s) {
        mSplines[splineIndex] = s;
    }

    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();
    }

}