OSDN Git Service

Merge "Revert "Give CropActivity minimal touchpad support."" into gb-ub-photos-carlsbad
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / filters / FilterDrawRepresentation.java
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.gallery3d.filtershow.filters;
18
19 import android.graphics.Color;
20 import android.graphics.Path;
21 import android.graphics.PathMeasure;
22 import android.util.JsonReader;
23 import android.util.JsonWriter;
24 import android.util.Log;
25
26 import com.android.gallery3d.R;
27 import com.android.gallery3d.filtershow.controller.BasicParameterInt;
28 import com.android.gallery3d.filtershow.controller.BasicParameterStyle;
29 import com.android.gallery3d.filtershow.controller.Parameter;
30 import com.android.gallery3d.filtershow.controller.ParameterBrightness;
31 import com.android.gallery3d.filtershow.controller.ParameterHue;
32 import com.android.gallery3d.filtershow.controller.ParameterOpacity;
33 import com.android.gallery3d.filtershow.controller.ParameterSaturation;
34 import com.android.gallery3d.filtershow.editors.EditorDraw;
35
36 import java.io.IOException;
37 import java.util.Arrays;
38 import java.util.Vector;
39
40 public class FilterDrawRepresentation extends FilterRepresentation {
41     private static final String LOGTAG = "FilterDrawRepresentation";
42
43     public static final int PARAM_SIZE = 0;
44     public static final int PARAM_HUE = 1;
45     public static final int PARAM_BRIGHTNESS = 2;
46     public static final int PARAM_SATURATION = 3;
47     public static final int PARAM_OPACITY = 4;
48     public static final int PARAM_STYLE = 5;
49     private BasicParameterInt mParamSize = new BasicParameterInt(PARAM_SIZE, 20, 2, 300);
50     private BasicParameterInt mParamHue = new ParameterHue(PARAM_HUE, 0);
51     private BasicParameterInt mParamBrightness = new ParameterBrightness(PARAM_BRIGHTNESS, 220);
52     private BasicParameterInt mParamSaturation = new ParameterSaturation(PARAM_SATURATION, 200);
53     private ParameterOpacity mParamOpacity = new ParameterOpacity(PARAM_OPACITY, 200);
54     private BasicParameterStyle mParamStyle = new BasicParameterStyle(PARAM_STYLE, 5);
55     int mParamMode;
56     Parameter mCurrentParam = mParamSize;
57     private static final String SERIAL_COLOR = "color";
58     private static final String SERIAL_RADIUS = "radius";
59     private static final String SERIAL_TYPE = "type";
60     private static final String SERIAL_POINTS_COUNT = "point_count";
61     private static final String SERIAL_POINTS = "points";
62     private static final String SERIAL_PATH =  "path";
63
64
65     private Parameter[] mAllParam = {
66             mParamSize,
67             mParamHue,
68             mParamBrightness,
69             mParamSaturation,
70             mParamOpacity,
71             mParamStyle
72     };
73
74     public void setPramMode(int mode) {
75         mParamMode = mode;
76         mCurrentParam = mAllParam[mParamMode];
77     }
78
79     public int getParamMode() {
80         return mParamMode;
81     }
82
83     public Parameter getCurrentParam() {
84         return  mAllParam[mParamMode];
85     }
86
87     public Parameter getParam(int type) {
88         return  mAllParam[type];
89     }
90
91     public static class StrokeData implements Cloneable {
92         public byte mType;
93         public Path mPath;
94         public float mRadius;
95         public int mColor;
96         public int noPoints = 0;
97         public float[] mPoints = new float[20];
98
99         @Override
100         public String toString() {
101             return "stroke(" + mType + ", path(" + (mPath) + "), " + mRadius + " , "
102                     + Integer.toHexString(mColor) + ")";
103         }
104
105         @Override
106         public StrokeData clone() throws CloneNotSupportedException {
107             return (StrokeData) super.clone();
108         }
109     }
110
111     public String getValueString() {
112
113         switch (mParamMode) {
114             case PARAM_SIZE:
115             case PARAM_HUE:
116             case PARAM_BRIGHTNESS:
117             case PARAM_SATURATION:
118             case PARAM_OPACITY:
119                 int val = ((BasicParameterInt) mAllParam[mParamMode]).getValue();
120                 return ((val > 0) ? " +" : " ") + val;
121             case PARAM_STYLE:
122                 return "";
123
124         }
125         return "";
126     }
127
128     private Vector<StrokeData> mDrawing = new Vector<StrokeData>();
129     private StrokeData mCurrent; // used in the currently drawing style
130
131     public FilterDrawRepresentation() {
132         super("Draw");
133         setFilterClass(ImageFilterDraw.class);
134         setSerializationName("DRAW");
135         setFilterType(FilterRepresentation.TYPE_VIGNETTE);
136         setTextId(R.string.imageDraw);
137         setEditorId(EditorDraw.ID);
138         setOverlayId(R.drawable.filtershow_drawing);
139         setOverlayOnly(true);
140     }
141
142     @Override
143     public String toString() {
144         return getName() + " : strokes=" + mDrawing.size()
145                 + ((mCurrent == null) ? " no current "
146                 : ("draw=" + mCurrent.mType + " " + mCurrent.noPoints));
147     }
148
149     public Vector<StrokeData> getDrawing() {
150         return mDrawing;
151     }
152
153     public StrokeData getCurrentDrawing() {
154         return mCurrent;
155     }
156
157     @Override
158     public FilterRepresentation copy() {
159         FilterDrawRepresentation representation = new FilterDrawRepresentation();
160         copyAllParameters(representation);
161         return representation;
162     }
163
164     @Override
165     protected void copyAllParameters(FilterRepresentation representation) {
166         super.copyAllParameters(representation);
167         representation.useParametersFrom(this);
168     }
169
170     @Override
171     public boolean isNil() {
172         return getDrawing().isEmpty();
173     }
174
175     @Override
176     public void useParametersFrom(FilterRepresentation a) {
177         if (a instanceof FilterDrawRepresentation) {
178             FilterDrawRepresentation representation = (FilterDrawRepresentation) a;
179             try {
180                 if (representation.mCurrent != null) {
181                     mCurrent = (StrokeData) representation.mCurrent.clone();
182                 } else {
183                     mCurrent = null;
184                 }
185                 if (representation.mDrawing != null) {
186                     mDrawing = (Vector<StrokeData>) representation.mDrawing.clone();
187                 } else {
188                     mDrawing = null;
189                 }
190
191             } catch (CloneNotSupportedException e) {
192                 e.printStackTrace();
193             }
194         } else {
195             Log.v(LOGTAG, "cannot use parameters from " + a);
196         }
197     }
198
199     @Override
200     public boolean equals(FilterRepresentation representation) {
201         if (!super.equals(representation)) {
202             return false;
203         }
204         if (representation instanceof FilterDrawRepresentation) {
205             FilterDrawRepresentation fdRep = (FilterDrawRepresentation) representation;
206             if (fdRep.mDrawing.size() != mDrawing.size())
207                 return false;
208             if (fdRep.mCurrent == null && (mCurrent == null || mCurrent.mPath == null)) {
209                 return true;
210             }
211             if (fdRep.mCurrent != null && mCurrent != null && mCurrent.mPath != null) {
212                 if (fdRep.mCurrent.noPoints == mCurrent.noPoints) {
213                     return true;
214                 }
215                 return false;
216             }
217         }
218         return false;
219     }
220
221     private int computeCurrentColor(){
222         float hue = 360 * mParamHue.getValue() / (float) mParamHue.getMaximum();
223         float sat = mParamSaturation.getValue() / (float) mParamSaturation.getMaximum();
224         float val = mParamBrightness.getValue() / (float) mParamBrightness.getMaximum();
225         int op = mParamOpacity.getValue();
226         float[] hsv = new float[]{hue, sat, val};
227         return Color.HSVToColor(op, hsv);
228     }
229
230     public void fillStrokeParameters(StrokeData sd){
231         byte type = (byte) mParamStyle.getSelected();
232         int color = computeCurrentColor();
233         float size = mParamSize.getValue();
234         sd.mColor = color;
235         sd.mRadius = size;
236         sd.mType = type;
237     }
238
239     public void startNewSection(float x, float y) {
240         mCurrent = new StrokeData();
241         fillStrokeParameters(mCurrent);
242         mCurrent.mPath = new Path();
243         mCurrent.mPath.moveTo(x, y);
244         mCurrent.mPoints[0] = x;
245         mCurrent.mPoints[1] = y;
246         mCurrent.noPoints = 1;
247     }
248
249     public void addPoint(float x, float y) {
250         int len = mCurrent.noPoints * 2;
251         mCurrent.mPath.lineTo(x, y);
252         if ((len+2) > mCurrent.mPoints.length) {
253             mCurrent.mPoints = Arrays.copyOf(mCurrent.mPoints, mCurrent.mPoints.length * 2);
254         }
255         mCurrent.mPoints[len] = x;
256         mCurrent.mPoints[len + 1] = y;
257         mCurrent.noPoints++;
258     }
259
260     public void endSection(float x, float y) {
261         addPoint(x, y);
262         mDrawing.add(mCurrent);
263         mCurrent = null;
264     }
265
266     public void clearCurrentSection() {
267         mCurrent = null;
268     }
269
270     public void clear() {
271         mCurrent = null;
272         mDrawing.clear();
273     }
274
275     @Override
276     public void serializeRepresentation(JsonWriter writer) throws IOException {
277         writer.beginObject();
278         int len = mDrawing.size();
279         int count = 0;
280         float[] mPosition = new float[2];
281         float[] mTan = new float[2];
282
283         PathMeasure mPathMeasure = new PathMeasure();
284         for (int i = 0; i < len; i++) {
285             writer.name(SERIAL_PATH + i);
286             writer.beginObject();
287             StrokeData mark = mDrawing.get(i);
288             writer.name(SERIAL_COLOR).value(mark.mColor);
289             writer.name(SERIAL_RADIUS).value(mark.mRadius);
290             writer.name(SERIAL_TYPE).value(mark.mType);
291             writer.name(SERIAL_POINTS_COUNT).value(mark.noPoints);
292             writer.name(SERIAL_POINTS);
293
294             writer.beginArray();
295             int npoints = mark.noPoints * 2;
296             for (int j = 0; j < npoints; j++) {
297                 writer.value(mark.mPoints[j]);
298             }
299             writer.endArray();
300             writer.endObject();
301         }
302         writer.endObject();
303     }
304
305     @Override
306     public void deSerializeRepresentation(JsonReader sreader) throws IOException {
307         sreader.beginObject();
308         Vector<StrokeData> strokes = new Vector<StrokeData>();
309
310         while (sreader.hasNext()) {
311             sreader.nextName();
312             sreader.beginObject();
313             StrokeData stroke = new StrokeData();
314
315             while (sreader.hasNext()) {
316                 String name = sreader.nextName();
317                 if (name.equals(SERIAL_COLOR)) {
318                     stroke.mColor = sreader.nextInt();
319                 } else if (name.equals(SERIAL_RADIUS)) {
320                     stroke.mRadius = (float) sreader.nextDouble();
321                 } else if (name.equals(SERIAL_TYPE)) {
322                     stroke.mType = (byte) sreader.nextInt();
323                 } else if (name.equals(SERIAL_POINTS_COUNT)) {
324                     stroke.noPoints = sreader.nextInt();
325                 } else if (name.equals(SERIAL_POINTS)) {
326
327                     int count = 0;
328                     sreader.beginArray();
329                     while (sreader.hasNext()) {
330                         if ((count + 1) > stroke.mPoints.length) {
331                             stroke.mPoints = Arrays.copyOf(stroke.mPoints, count * 2);
332                         }
333                         stroke.mPoints[count++] = (float) sreader.nextDouble();
334                     }
335                     stroke.mPath = new Path();
336                     stroke.mPath.moveTo(stroke.mPoints[0], stroke.mPoints[1]);
337                     for (int i = 0; i < count; i += 2) {
338                         stroke.mPath.lineTo(stroke.mPoints[i], stroke.mPoints[i + 1]);
339                     }
340                     sreader.endArray();
341                     strokes.add(stroke);
342                 } else {
343                     sreader.skipValue();
344                 }
345             }
346             sreader.endObject();
347         }
348
349         mDrawing = strokes;
350
351         sreader.endObject();
352     }
353 }