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.PrimaryImage; 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( 163 "calling copyAllParameters with incompatible types!"); 164 } 165 super.copyAllParameters(representation); 166 representation.useParametersFrom(this); 167 } 168 169 @Override useParametersFrom(FilterRepresentation a)170 public void useParametersFrom(FilterRepresentation a) { 171 if (!(a instanceof FilterMirrorRepresentation)) { 172 throw new IllegalArgumentException( 173 "calling useParametersFrom with incompatible types!"); 174 } 175 setMirror(((FilterMirrorRepresentation) a).getMirror()); 176 } 177 178 @Override isNil()179 public boolean isNil() { 180 return mMirror == getNil(); 181 } 182 getNil()183 public static Mirror getNil() { 184 return Mirror.NONE; 185 } 186 187 @Override serializeRepresentation(JsonWriter writer)188 public void serializeRepresentation(JsonWriter writer) throws IOException { 189 writer.beginObject(); 190 writer.name(SERIALIZATION_MIRROR_VALUE).value(mMirror.value()); 191 writer.endObject(); 192 } 193 194 @Override deSerializeRepresentation(JsonReader reader)195 public void deSerializeRepresentation(JsonReader reader) throws IOException { 196 boolean unset = true; 197 reader.beginObject(); 198 while (reader.hasNext()) { 199 String name = reader.nextName(); 200 if (SERIALIZATION_MIRROR_VALUE.equals(name)) { 201 Mirror r = Mirror.fromValue((char) reader.nextInt()); 202 if (r != null) { 203 setMirror(r); 204 unset = false; 205 } 206 } else { 207 reader.skipValue(); 208 } 209 } 210 if (unset) { 211 Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME); 212 } 213 reader.endObject(); 214 } 215 } 216