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.util.JsonReader;
20 import android.util.JsonWriter;
21 import android.util.Log;
22 
23 import com.android.gallery3d.R;
24 import com.android.gallery3d.filtershow.editors.EditorMirror;
25 import com.android.gallery3d.filtershow.editors.ImageOnlyEditor;
26 import com.android.gallery3d.filtershow.imageshow.GeometryMathUtils;
27 import com.android.gallery3d.filtershow.imageshow.MasterImage;
28 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
29 
30 import java.io.IOException;
31 import java.util.ArrayList;
32 
33 public class FilterMirrorRepresentation extends FilterRepresentation {
34     public static final String SERIALIZATION_NAME = "MIRROR";
35     private static final String SERIALIZATION_MIRROR_VALUE = "value";
36     private static final String TAG = FilterMirrorRepresentation.class.getSimpleName();
37 
38     Mirror mMirror;
39 
40     public enum Mirror {
41         NONE('N'), VERTICAL('V'), HORIZONTAL('H'), BOTH('B');
42         char mValue;
43 
Mirror(char value)44         private Mirror(char value) {
45             mValue = value;
46         }
47 
value()48         public char value() {
49             return mValue;
50         }
51 
fromValue(char value)52         public static Mirror fromValue(char value) {
53             switch (value) {
54                 case 'N':
55                     return NONE;
56                 case 'V':
57                     return VERTICAL;
58                 case 'H':
59                     return HORIZONTAL;
60                 case 'B':
61                     return BOTH;
62                 default:
63                     return null;
64             }
65         }
66     }
67 
FilterMirrorRepresentation(Mirror mirror)68     public FilterMirrorRepresentation(Mirror mirror) {
69         super(SERIALIZATION_NAME);
70         setSerializationName(SERIALIZATION_NAME);
71         setShowParameterValue(false);
72         setFilterClass(FilterMirrorRepresentation.class);
73         setFilterType(FilterRepresentation.TYPE_GEOMETRY);
74         setSupportsPartialRendering(true);
75         setTextId(R.string.mirror);
76         setEditorId(ImageOnlyEditor.ID);
77         setMirror(mirror);
78     }
79 
FilterMirrorRepresentation(FilterMirrorRepresentation m)80     public FilterMirrorRepresentation(FilterMirrorRepresentation m) {
81         this(m.getMirror());
82         setName(m.getName());
83     }
84 
FilterMirrorRepresentation()85     public FilterMirrorRepresentation() {
86         this(getNil());
87     }
88 
89     @Override
equals(FilterRepresentation rep)90     public boolean equals(FilterRepresentation rep) {
91         if (!(rep instanceof FilterMirrorRepresentation)) {
92             return false;
93         }
94         FilterMirrorRepresentation mirror = (FilterMirrorRepresentation) rep;
95         if (mMirror != mirror.mMirror) {
96             return false;
97         }
98         return true;
99     }
100 
getMirror()101     public Mirror getMirror() {
102         return mMirror;
103     }
104 
set(FilterMirrorRepresentation r)105     public void set(FilterMirrorRepresentation r) {
106         mMirror = r.mMirror;
107     }
108 
setMirror(Mirror mirror)109     public void setMirror(Mirror mirror) {
110         if (mirror == null) {
111             throw new IllegalArgumentException("Argument to setMirror is null");
112         }
113         mMirror = mirror;
114     }
115 
isHorizontal()116     public boolean isHorizontal() {
117         if (mMirror == Mirror.BOTH
118                 || mMirror == Mirror.HORIZONTAL) {
119             return true;
120         }
121         return false;
122     }
123 
isVertical()124     public boolean isVertical() {
125         if (mMirror == Mirror.BOTH
126                 || mMirror == Mirror.VERTICAL) {
127             return true;
128         }
129         return false;
130     }
131 
cycle()132     public void cycle() {
133         switch (mMirror) {
134             case NONE:
135                 mMirror = Mirror.HORIZONTAL;
136                 break;
137             case HORIZONTAL:
138                 mMirror = Mirror.BOTH;
139                 break;
140             case BOTH:
141                 mMirror = Mirror.VERTICAL;
142                 break;
143             case VERTICAL:
144                 mMirror = Mirror.NONE;
145                 break;
146         }
147     }
148 
149     @Override
allowsSingleInstanceOnly()150     public boolean allowsSingleInstanceOnly() {
151         return true;
152     }
153 
154     @Override
copy()155     public FilterRepresentation copy() {
156         return new FilterMirrorRepresentation(this);
157     }
158 
159     @Override
copyAllParameters(FilterRepresentation representation)160     protected void copyAllParameters(FilterRepresentation representation) {
161         if (!(representation instanceof FilterMirrorRepresentation)) {
162             throw new IllegalArgumentException("calling copyAllParameters with incompatible types!");
163         }
164         super.copyAllParameters(representation);
165         representation.useParametersFrom(this);
166     }
167 
168     @Override
useParametersFrom(FilterRepresentation a)169     public void useParametersFrom(FilterRepresentation a) {
170         if (!(a instanceof FilterMirrorRepresentation)) {
171             throw new IllegalArgumentException("calling useParametersFrom with incompatible types!");
172         }
173         setMirror(((FilterMirrorRepresentation) a).getMirror());
174     }
175 
176     @Override
isNil()177     public boolean isNil() {
178         return mMirror == getNil();
179     }
180 
getNil()181     public static Mirror getNil() {
182         return Mirror.NONE;
183     }
184 
185     @Override
serializeRepresentation(JsonWriter writer)186     public void serializeRepresentation(JsonWriter writer) throws IOException {
187         writer.beginObject();
188         writer.name(SERIALIZATION_MIRROR_VALUE).value(mMirror.value());
189         writer.endObject();
190     }
191 
192     @Override
deSerializeRepresentation(JsonReader reader)193     public void deSerializeRepresentation(JsonReader reader) throws IOException {
194         boolean unset = true;
195         reader.beginObject();
196         while (reader.hasNext()) {
197             String name = reader.nextName();
198             if (SERIALIZATION_MIRROR_VALUE.equals(name)) {
199                 Mirror r = Mirror.fromValue((char) reader.nextInt());
200                 if (r != null) {
201                     setMirror(r);
202                     unset = false;
203                 }
204             } else {
205                 reader.skipValue();
206             }
207         }
208         if (unset) {
209             Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME);
210         }
211         reader.endObject();
212     }
213 }
214