summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/filters/FilterDrawRepresentation.java
blob: 5c5e561ca7dbf6688ec741f6c4f789eba1e10639 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * 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.ParameterColor;
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.Iterator;
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_STYLE = 1;
    public static final int PARAM_COLOR = 2;
    private BasicParameterInt mParamSize = new BasicParameterInt(PARAM_SIZE, 30, 2, 300);
    private BasicParameterStyle mParamStyle = new BasicParameterStyle(PARAM_STYLE, 5);
    public static int DEFAULT_MENU_COLOR1 = Color.RED & 0x80FFFFFF;
    public static int DEFAULT_MENU_COLOR2 = Color.GREEN & 0x80FFFFFF;
    public static int DEFAULT_MENU_COLOR3 = Color.BLUE & 0x80FFFFFF;
    public static int DEFAULT_MENU_COLOR4 = Color.BLACK & 0x80FFFFFF;
    public static int DEFAULT_MENU_COLOR5 = Color.WHITE & 0x80FFFFFF;
    ParameterColor mParamColor = new ParameterColor(PARAM_COLOR,DEFAULT_MENU_COLOR1);
    int mParamMode;
    Parameter mCurrentParam = mParamSize;
    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";
    private static final String SERIAL_PATH =  "path";


    private Parameter[] mAllParam = {
            mParamSize,
            mParamStyle,
            mParamColor
    };

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

    public int getParamMode() {
        return 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];

        public StrokeData() {
        }

        public StrokeData(StrokeData copy) {
            mType = copy.mType;
            mPath = new Path(copy.mPath);
            mRadius = copy.mRadius;
            mColor = copy.mColor;
            noPoints = copy.noPoints;
            mPoints = Arrays.copyOf(copy.mPoints, copy.mPoints.length);
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof StrokeData)) {
                return false;
            }
            StrokeData sd = (StrokeData) o;
            if (mType != sd.mType
                    || mRadius != sd.mRadius
                    || noPoints != sd.noPoints
                    || mColor != sd.mColor) {
                return false;
            }
            return mPath.equals(sd.mPath);
        }

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

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

    static String colorHexString(int val) {
        String str = "00000000" + Integer.toHexString(val);
        str = "0x" + str.substring(str.length() - 8);
        return str;
    }

    public String getValueString() {
        int val;
        switch (mParamMode) {
            case PARAM_COLOR:
                val = ((ParameterColor) mAllParam[mParamMode]).getValue();
                return "";
            case PARAM_SIZE:
                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(String name) {
        super(name);
        setFilterClass(ImageFilterDraw.class);
        setSerializationName(name);
        setFilterType(FilterRepresentation.TYPE_VIGNETTE);
        setTextId(R.string.imageDraw);
        setEditorId(EditorDraw.ID);
        setOverlayId(R.drawable.filtershow_drawing);
        setOverlayOnly(true);
        setDefaultColor();
    }

    private void setDefaultColor() {
        mParamColor.setValue(DEFAULT_MENU_COLOR1);
    }

    @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(getName());
        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;
            mParamColor.copyPalletFrom(representation.mParamColor);
            try {
                if (representation.mCurrent != null) {
                    mCurrent = (StrokeData) representation.mCurrent.clone();
                } else {
                    mCurrent = null;
                }
                if (representation.mDrawing != null) {
                    mDrawing = new Vector<StrokeData>();
                    for (Iterator<StrokeData> elem = representation.mDrawing.iterator(); elem.hasNext(); ) {
                        StrokeData next =  elem.next();
                        mDrawing.add(new StrokeData(next));
                    }
                } 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 false;
            }


            if (fdRep.mCurrent != null && mCurrent != null && mCurrent.mPath != null) {
                if (fdRep.mCurrent.noPoints == mCurrent.noPoints) {
                    return true;
                }
                return false;
            }

        int n = mDrawing.size();
        for (int i = 0; i < n; i++) {
            StrokeData a = mDrawing.get(i);
            StrokeData b = mDrawing.get(i);
            if (!a.equals(b)){
                return false;
            }
        }
        return true;
        }
        return false;
    }

    private int computeCurrentColor(){
        return mParamColor.getValue();
    }

    public void fillStrokeParameters(StrokeData sd){
        byte type = (byte) mParamStyle.getSelected();
        int color = computeCurrentColor();
        float size = mParamSize.getValue();
        sd.mColor = color;
        sd.mRadius = size;
        sd.mType = type;
    }

    public void startNewSection(float x, float y) {
        mCurrent = new StrokeData();
        fillStrokeParameters(mCurrent);
        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();
    }

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