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