summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/filters/FilterDrawRepresentation.java
blob: 8a4799c1cc4c964d510794681cc0eda4881ef114 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.filters;

import android.graphics.Color;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.JsonReader;
import android.util.JsonWriter;
import android.util.Log;

import com.android.gallery3d.R;
import com.android.gallery3d.filtershow.controller.BasicParameterInt;
import com.android.gallery3d.filtershow.controller.BasicParameterStyle;
import com.android.gallery3d.filtershow.controller.Parameter;
import com.android.gallery3d.filtershow.controller.ParameterBrightness;
import com.android.gallery3d.filtershow.controller.ParameterHue;
import com.android.gallery3d.filtershow.controller.ParameterOpacity;
import com.android.gallery3d.filtershow.controller.ParameterSaturation;
import com.android.gallery3d.filtershow.editors.EditorDraw;

import java.io.IOException;
import java.util.Arrays;
import java.util.Vector;

public class FilterDrawRepresentation extends FilterRepresentation {
    private static final String LOGTAG = "FilterDrawRepresentation";

    public static final int PARAM_SIZE = 0;
    public static final int PARAM_HUE = 1;
    public static final int PARAM_BRIGHTNESS = 2;
    public static final int PARAM_SATURATION = 3;
    public static final int PARAM_OPACITY = 4;
    public static final int PARAM_STYLE = 5;
    private BasicParameterInt mParamSize = new BasicParameterInt(PARAM_SIZE, 20, 2, 300);
    private BasicParameterInt mParamHue = new ParameterHue(PARAM_HUE, 0);
    private BasicParameterInt mParamBrightness = new ParameterBrightness(PARAM_BRIGHTNESS, 220);
    private BasicParameterInt mParamSaturation = new ParameterSaturation(PARAM_SATURATION, 200);
    private ParameterOpacity mParamOpacity = new ParameterOpacity(PARAM_OPACITY, 200);
    private BasicParameterStyle mParamStyle = new BasicParameterStyle(PARAM_STYLE, 3);
    int mParamMode;
    Parameter mCurrentParam = mParamSize;

    private Parameter[] mAllParam = {
            mParamSize,
            mParamHue,
            mParamBrightness,
            mParamSaturation,
            mParamOpacity,
            mParamStyle
    };

    public void setPramMode(int mode) {
        mParamMode = mode;
        mCurrentParam = mAllParam[mParamMode];
    }

    public Parameter getCurrentParam() {
        return  mAllParam[mParamMode];
    }

    public Parameter getParam(int type) {
        return  mAllParam[type];
    }

    public static class StrokeData implements Cloneable {
        public byte mType;
        public Path mPath;
        public float mRadius;
        public int mColor;
        public int noPoints = 0;
        public float []mPoints = new float [20];

        @Override
        public String toString() {
            return "stroke(" + mType + ", path(" + (mPath) + "), " + mRadius + " , "
                    + Integer.toHexString(mColor) + ")";
        }

        @Override
        public StrokeData clone() throws CloneNotSupportedException {
            return (StrokeData) super.clone();
        }
    }

    public String getValueString() {

        switch (mParamMode) {
            case PARAM_SIZE:
            case PARAM_HUE:
            case PARAM_BRIGHTNESS:
            case PARAM_SATURATION:
            case PARAM_OPACITY:
                int val = ((BasicParameterInt) mAllParam[mParamMode]).getValue();
                return ((val > 0) ? " +" : " ") + val;
            case PARAM_STYLE:
                return "";

        }
        return "";
    }

    private Vector<StrokeData> mDrawing = new Vector<StrokeData>();
    private StrokeData mCurrent; // used in the currently drawing style

    public FilterDrawRepresentation() {
        super("Draw");
        setFilterClass(ImageFilterDraw.class);
        setSerializationName("DRAW");
        setFilterType(FilterRepresentation.TYPE_VIGNETTE);
        setTextId(R.string.imageDraw);
        setEditorId(EditorDraw.ID);
        setOverlayId(R.drawable.filtershow_drawing);
        setOverlayOnly(true);
    }

    @Override
    public String toString() {
        return getName() + " : strokes=" + mDrawing.size()
                + ((mCurrent == null) ? " no current "
                : ("draw=" + mCurrent.mType + " " + mCurrent.noPoints));
    }

    public Vector<StrokeData> getDrawing() {
        return mDrawing;
    }

    public StrokeData getCurrentDrawing() {
        return mCurrent;
    }

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

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

    @Override
    public boolean isNil() {
        return getDrawing().isEmpty();
    }

    @Override
    public void useParametersFrom(FilterRepresentation a) {
        if (a instanceof FilterDrawRepresentation) {
            FilterDrawRepresentation representation = (FilterDrawRepresentation) a;
            try {
                if (representation.mCurrent != null) {
                    mCurrent = (StrokeData) representation.mCurrent.clone();
                } else {
                    mCurrent = null;
                }
                if (representation.mDrawing != null) {
                    mDrawing = (Vector<StrokeData>) representation.mDrawing.clone();
                } else {
                    mDrawing = null;
                }

            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
        } else {
            Log.v(LOGTAG, "cannot use parameters from " + a);
        }
    }

    @Override
    public boolean equals(FilterRepresentation representation) {
        if (!super.equals(representation)) {
            return false;
        }
        if (representation instanceof FilterDrawRepresentation) {
            FilterDrawRepresentation fdRep = (FilterDrawRepresentation) representation;
            if (fdRep.mDrawing.size() != mDrawing.size())
                return false;
            if (fdRep.mCurrent == null && (mCurrent == null || mCurrent.mPath == null)) {
                return true;
            }
            if (fdRep.mCurrent != null && mCurrent != null && mCurrent.mPath != null) {
                if (fdRep.mCurrent.noPoints == mCurrent.noPoints) {
                    return true;
                }
                return false;
            }
        }
        return false;
    }

    private int computeCurrentColor(){
        float hue = 360 * mParamHue.getValue() / (float) mParamHue.getMaximum();
        float sat = mParamSaturation.getValue() / (float) mParamSaturation.getMaximum();
        float val = mParamBrightness.getValue() / (float) mParamBrightness.getMaximum();
        int op = mParamOpacity.getValue();
        float[] hsv = new float[]{hue, sat, val};
        return Color.HSVToColor(op, hsv);
    }

    public void startNewSection(float x, float y) {
        byte type = (byte) mParamStyle.getSelected();
        int color = computeCurrentColor();
        float size = mParamSize.getValue();
        mCurrent = new StrokeData();
        mCurrent.mColor = color;
        mCurrent.mRadius = size;
        mCurrent.mType = type;
        mCurrent.mPath = new Path();
        mCurrent.mPath.moveTo(x, y);
        mCurrent.mPoints[0] = x;
        mCurrent.mPoints[1] = y;
        mCurrent.noPoints = 1;
    }

    public void addPoint(float x, float y) {
        int len = mCurrent.noPoints * 2;
        mCurrent.mPath.lineTo(x, y);
        if ((len+2) > mCurrent.mPoints.length) {
            mCurrent.mPoints = Arrays.copyOf(mCurrent.mPoints, mCurrent.mPoints.length * 2);
        }
        mCurrent.mPoints[len] = x;
        mCurrent.mPoints[len + 1] = y;
        mCurrent.noPoints++;
    }

    public void endSection(float x, float y) {
        addPoint(x, y);
        mDrawing.add(mCurrent);
        mCurrent = null;
    }

    public void clearCurrentSection() {
        mCurrent = null;
    }

    public void clear() {
        mCurrent = null;
        mDrawing.clear();
    }
    private static final String SERIAL_COLOR = "color";
    private static final String SERIAL_RADIUS = "radius";
    private static final String SERIAL_TYPE = "type";
    private static final String SERIAL_POINTS_COUNT = "point_count";
    private static final String SERIAL_POINTS = "points";

    @Override
    public void serializeRepresentation(JsonWriter writer) throws IOException {
        writer.beginObject();
        int len = mDrawing.size();
        int count = 0;
        float[] mPosition = new float[2];
        float[] mTan = new float[2];

        PathMeasure mPathMeasure = new PathMeasure();
        for (int i = 0; i < len; i++) {
            writer.name("path" + i);
            writer.beginObject();
            StrokeData mark = mDrawing.get(i);
            writer.name(SERIAL_COLOR).value(mark.mColor);
            writer.name(SERIAL_RADIUS).value(mark.mRadius);
            writer.name(SERIAL_TYPE).value(mark.mType);
            writer.name(SERIAL_POINTS_COUNT).value(mark.noPoints);
            writer.name(SERIAL_POINTS);

            writer.beginArray();
            int npoints = mark.noPoints * 2;
            for (int j = 0; j < npoints; j++) {
                writer.value(mark.mPoints[j]);
            }
            writer.endArray();
            writer.endObject();
        }
        writer.endObject();
    }

    @Override
    public void deSerializeRepresentation(JsonReader sreader) throws IOException {
        sreader.beginObject();
        Vector<StrokeData> strokes = new Vector<StrokeData>();

        while (sreader.hasNext()) {
            sreader.nextName();
            sreader.beginObject();
            StrokeData stroke = new StrokeData();

            while (sreader.hasNext()) {
                String name = sreader.nextName();
                if (name.equals(SERIAL_COLOR)) {
                    stroke.mColor = sreader.nextInt();
                } else if (name.equals(SERIAL_RADIUS)) {
                    stroke.mRadius = (float) sreader.nextDouble();
                } else if (name.equals(SERIAL_TYPE)) {
                    stroke.mType = (byte) sreader.nextInt();
                } else if (name.equals(SERIAL_POINTS_COUNT)) {
                    stroke.noPoints = sreader.nextInt();
                } else if (name.equals(SERIAL_POINTS)) {

                    int count = 0;
                    sreader.beginArray();
                    while (sreader.hasNext()) {
                        if ((count + 1) > stroke.mPoints.length) {
                            stroke.mPoints = Arrays.copyOf(stroke.mPoints, count * 2);
                        }
                        stroke.mPoints[count++] = (float) sreader.nextDouble();
                    }
                    stroke.mPath = new Path();
                    stroke.mPath.moveTo(stroke.mPoints[0], stroke.mPoints[1]);
                    for (int i = 0; i < count; i += 2) {
                        stroke.mPath.lineTo(stroke.mPoints[i], stroke.mPoints[i + 1]);
                    }
                    sreader.endArray();
                    strokes.add(stroke);
                } else {
                    sreader.skipValue();
                }
            }
            sreader.endObject();
        }

        mDrawing = strokes;

        sreader.endObject();
    }
}