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.EditorStraighten;
25 
26 import java.io.IOException;
27 
28 public class FilterStraightenRepresentation extends FilterRepresentation {
29     public static final String SERIALIZATION_NAME = "STRAIGHTEN";
30     public static final String SERIALIZATION_STRAIGHTEN_VALUE = "value";
31     private static final String TAG = FilterStraightenRepresentation.class.getSimpleName();
32     public static final int MAX_STRAIGHTEN_ANGLE = 45;
33     public static final int MIN_STRAIGHTEN_ANGLE = -45;
34 
35     float mStraighten;
36 
FilterStraightenRepresentation(float straighten)37     public FilterStraightenRepresentation(float straighten) {
38         super(SERIALIZATION_NAME);
39         setSerializationName(SERIALIZATION_NAME);
40         setShowParameterValue(true);
41         setFilterClass(FilterStraightenRepresentation.class);
42         setFilterType(FilterRepresentation.TYPE_GEOMETRY);
43         setSupportsPartialRendering(true);
44         setTextId(R.string.straighten);
45         setEditorId(EditorStraighten.ID);
46         setStraighten(straighten);
47     }
48 
FilterStraightenRepresentation(FilterStraightenRepresentation s)49     public FilterStraightenRepresentation(FilterStraightenRepresentation s) {
50         this(s.getStraighten());
51         setName(s.getName());
52     }
53 
FilterStraightenRepresentation()54     public FilterStraightenRepresentation() {
55         this(getNil());
56     }
57 
set(FilterStraightenRepresentation r)58     public void set(FilterStraightenRepresentation r) {
59         mStraighten = r.mStraighten;
60     }
61 
62     @Override
equals(FilterRepresentation rep)63     public boolean equals(FilterRepresentation rep) {
64         if (!(rep instanceof FilterStraightenRepresentation)) {
65             return false;
66         }
67         FilterStraightenRepresentation straighten = (FilterStraightenRepresentation) rep;
68         if (straighten.mStraighten != mStraighten) {
69             return false;
70         }
71         return true;
72     }
73 
getStraighten()74     public float getStraighten() {
75         return mStraighten;
76     }
77 
setStraighten(float straighten)78     public void setStraighten(float straighten) {
79         if (!rangeCheck(straighten)) {
80             straighten = Math.min(Math.max(straighten, MIN_STRAIGHTEN_ANGLE), MAX_STRAIGHTEN_ANGLE);
81         }
82         mStraighten = straighten;
83     }
84 
85     @Override
allowsSingleInstanceOnly()86     public boolean allowsSingleInstanceOnly() {
87         return true;
88     }
89 
90     @Override
copy()91     public FilterRepresentation copy() {
92         return new FilterStraightenRepresentation(this);
93     }
94 
95     @Override
copyAllParameters(FilterRepresentation representation)96     protected void copyAllParameters(FilterRepresentation representation) {
97         if (!(representation instanceof FilterStraightenRepresentation)) {
98             throw new IllegalArgumentException("calling copyAllParameters with incompatible types!");
99         }
100         super.copyAllParameters(representation);
101         representation.useParametersFrom(this);
102     }
103 
104     @Override
useParametersFrom(FilterRepresentation a)105     public void useParametersFrom(FilterRepresentation a) {
106         if (!(a instanceof FilterStraightenRepresentation)) {
107             throw new IllegalArgumentException("calling useParametersFrom with incompatible types!");
108         }
109         setStraighten(((FilterStraightenRepresentation) a).getStraighten());
110     }
111 
112     @Override
isNil()113     public boolean isNil() {
114         return mStraighten == getNil();
115     }
116 
getNil()117     public static float getNil() {
118         return 0;
119     }
120 
121     @Override
serializeRepresentation(JsonWriter writer)122     public void serializeRepresentation(JsonWriter writer) throws IOException {
123         writer.beginObject();
124         writer.name(SERIALIZATION_STRAIGHTEN_VALUE).value(mStraighten);
125         writer.endObject();
126     }
127 
128     @Override
deSerializeRepresentation(JsonReader reader)129     public void deSerializeRepresentation(JsonReader reader) throws IOException {
130         boolean unset = true;
131         reader.beginObject();
132         while (reader.hasNext()) {
133             String name = reader.nextName();
134             if (SERIALIZATION_STRAIGHTEN_VALUE.equals(name)) {
135                 float s = (float) reader.nextDouble();
136                 if (rangeCheck(s)) {
137                     setStraighten(s);
138                     unset = false;
139                 }
140             } else {
141                 reader.skipValue();
142             }
143         }
144         if (unset) {
145             Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME);
146         }
147         reader.endObject();
148     }
149 
rangeCheck(double s)150     private boolean rangeCheck(double s) {
151         if (s < -45 || s > 45) {
152             return false;
153         }
154         return true;
155     }
156 }
157