FilterCropRepresentation.java revision 8f442fae60e0154867d2a6927eb9a35bcb7014e6
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 17package com.android.gallery3d.filtershow.filters; 18 19import android.graphics.RectF; 20import android.util.JsonReader; 21import android.util.JsonWriter; 22 23import com.android.gallery3d.R; 24import com.android.gallery3d.filtershow.editors.EditorCrop; 25 26import java.io.IOException; 27 28public class FilterCropRepresentation extends FilterRepresentation { 29 public static final String SERIALIZATION_NAME = "CROP"; 30 public static final String[] BOUNDS = { 31 "C0", "C1", "C2", "C3", "I0", "I1", "I2", "I3" 32 }; 33 private static final String TAG = FilterCropRepresentation.class.getSimpleName(); 34 35 RectF mCrop = new RectF(); 36 RectF mImage = new RectF(); 37 38 public FilterCropRepresentation(RectF crop, RectF image) { 39 super(FilterCropRepresentation.class.getSimpleName()); 40 setSerializationName(SERIALIZATION_NAME); 41 setShowParameterValue(true); 42 setFilterClass(FilterCropRepresentation.class); 43 setFilterType(FilterRepresentation.TYPE_GEOMETRY); 44 setTextId(R.string.crop); 45 setEditorId(EditorCrop.ID); 46 setCrop(crop); 47 setImage(image); 48 } 49 50 public FilterCropRepresentation(FilterCropRepresentation m) { 51 this(m.getCrop(), m.getImage()); 52 } 53 54 public FilterCropRepresentation() { 55 this(new RectF(), new RectF()); 56 } 57 58 public void set(FilterCropRepresentation r) { 59 mCrop.set(r.mCrop); 60 mImage.set(r.mImage); 61 } 62 63 @Override 64 public boolean equals(FilterRepresentation rep) { 65 if (!(rep instanceof FilterCropRepresentation)) { 66 return false; 67 } 68 FilterCropRepresentation crop = (FilterCropRepresentation) rep; 69 if (mCrop.bottom != crop.mCrop.bottom 70 || mCrop.left != crop.mCrop.left 71 || mCrop.right != crop.mCrop.right 72 || mCrop.top != crop.mCrop.top 73 || mImage.bottom != crop.mImage.bottom 74 || mImage.left != crop.mImage.left 75 || mImage.right != crop.mImage.right 76 || mImage.top != crop.mImage.top) { 77 return false; 78 } 79 return true; 80 } 81 82 public RectF getCrop() { 83 return new RectF(mCrop); 84 } 85 86 public void getCrop(RectF r) { 87 r.set(mCrop); 88 } 89 90 public void setCrop(RectF crop) { 91 if (crop == null) { 92 throw new IllegalArgumentException("Argument to setCrop is null"); 93 } 94 mCrop.set(crop); 95 } 96 97 public RectF getImage() { 98 return new RectF(mImage); 99 } 100 101 public void getImage(RectF r) { 102 r.set(mImage); 103 } 104 105 public void setImage(RectF image) { 106 if (image == null) { 107 throw new IllegalArgumentException("Argument to setImage is null"); 108 } 109 mImage.set(image); 110 } 111 112 @Override 113 public boolean allowsSingleInstanceOnly() { 114 return true; 115 } 116 117 @Override 118 public FilterRepresentation copy(){ 119 return new FilterCropRepresentation(this); 120 } 121 122 @Override 123 protected void copyAllParameters(FilterRepresentation representation) { 124 if (!(representation instanceof FilterCropRepresentation)) { 125 throw new IllegalArgumentException("calling copyAllParameters with incompatible types!"); 126 } 127 super.copyAllParameters(representation); 128 representation.useParametersFrom(this); 129 } 130 131 @Override 132 public void useParametersFrom(FilterRepresentation a) { 133 if (!(a instanceof FilterCropRepresentation)) { 134 throw new IllegalArgumentException("calling useParametersFrom with incompatible types!"); 135 } 136 setCrop(((FilterCropRepresentation) a).mCrop); 137 setImage(((FilterCropRepresentation) a).mImage); 138 } 139 140 @Override 141 public boolean isNil() { 142 return mCrop.equals(mImage); 143 } 144 145 @Override 146 public void serializeRepresentation(JsonWriter writer) throws IOException { 147 writer.beginObject(); 148 writer.name(BOUNDS[0]).value(mCrop.left); 149 writer.name(BOUNDS[1]).value(mCrop.top); 150 writer.name(BOUNDS[2]).value(mCrop.right); 151 writer.name(BOUNDS[3]).value(mCrop.bottom); 152 writer.name(BOUNDS[4]).value(mImage.left); 153 writer.name(BOUNDS[5]).value(mImage.top); 154 writer.name(BOUNDS[6]).value(mImage.right); 155 writer.name(BOUNDS[7]).value(mImage.bottom); 156 writer.endObject(); 157 } 158 159 @Override 160 public void deSerializeRepresentation(JsonReader reader) throws IOException { 161 reader.beginObject(); 162 while (reader.hasNext()) { 163 String name = reader.nextName(); 164 if (BOUNDS[0].equals(name)) { 165 mCrop.left = (float) reader.nextDouble(); 166 } else if (BOUNDS[1].equals(name)) { 167 mCrop.top = (float) reader.nextDouble(); 168 } else if (BOUNDS[2].equals(name)) { 169 mCrop.right = (float) reader.nextDouble(); 170 } else if (BOUNDS[3].equals(name)) { 171 mCrop.bottom = (float) reader.nextDouble(); 172 } else if (BOUNDS[4].equals(name)) { 173 mImage.left = (float) reader.nextDouble(); 174 } else if (BOUNDS[5].equals(name)) { 175 mImage.top = (float) reader.nextDouble(); 176 } else if (BOUNDS[6].equals(name)) { 177 mImage.right = (float) reader.nextDouble(); 178 } else if (BOUNDS[7].equals(name)) { 179 mImage.bottom = (float) reader.nextDouble(); 180 } else { 181 reader.skipValue(); 182 } 183 } 184 reader.endObject(); 185 } 186} 187