summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/filters/FilterVignetteRepresentation.java
blob: d316adeed14ee0aa247b19702c7fb46fdcd3093d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.filters;

import android.util.JsonReader;
import android.util.JsonWriter;

import com.android.gallery3d.R;
import com.android.gallery3d.filtershow.controller.BasicParameterInt;
import com.android.gallery3d.filtershow.controller.Parameter;
import com.android.gallery3d.filtershow.editors.EditorVignette;
import com.android.gallery3d.filtershow.imageshow.Oval;

import java.io.IOException;

public class FilterVignetteRepresentation extends FilterRepresentation implements Oval {
    private static final String LOGTAG = "FilterVignetteRepresentation";

    private float mCenterX = .5f;
    private float mCenterY = .5f;
    private float mRadiusX = .5f;
    private float mRadiusY = .5f;
    public static final int MODE_VIGNETTE = 0;
    public static final int MODE_EXPOSURE = 1;
    public static final int MODE_SATURATION = 2;
    public static final int MODE_CONTRAST = 3;
    public static final int MODE_FALLOFF = 4;
    private static int MIN = -100;
    private static int MAX = 100;
    private static int MAXFALLOF = 200;

    private BasicParameterInt mParamVignette = new BasicParameterInt(MODE_VIGNETTE, 50, MIN, MAX);
    private BasicParameterInt mParamExposure = new BasicParameterInt(MODE_EXPOSURE, 0, MIN, MAX);
    private BasicParameterInt mParamSaturation = new BasicParameterInt(MODE_SATURATION, 0, MIN, MAX);
    private BasicParameterInt mParamContrast = new BasicParameterInt(MODE_CONTRAST, 0, MIN, MAX);
    private BasicParameterInt mParamFalloff = new BasicParameterInt(MODE_FALLOFF, 40, 0, MAXFALLOF);
    private BasicParameterInt[] mAllParam = {
            mParamVignette,
            mParamExposure,
            mParamSaturation,
            mParamContrast,
            mParamFalloff};
    private int mParameterMode;

    public FilterVignetteRepresentation() {
        super("Vignette");
        setSerializationName("VIGNETTE");
        setShowParameterValue(true);
        setFilterType(FilterRepresentation.TYPE_VIGNETTE);
        setTextId(R.string.vignette);
        setEditorId(EditorVignette.ID);
        setName("Vignette");
        setFilterClass(ImageFilterVignette.class);
    }

    @Override
    public void useParametersFrom(FilterRepresentation a) {
        super.useParametersFrom(a);
        FilterVignetteRepresentation rep = (FilterVignetteRepresentation) a;
        mCenterX = rep.mCenterX;
        mCenterY = rep.mCenterY;
        mRadiusX = rep.mRadiusX;
        mRadiusY = rep.mRadiusY;
        mParamVignette.setValue(rep.mParamVignette.getValue());
        mParamExposure.setValue(rep.mParamExposure.getValue());
        mParamSaturation.setValue(rep.mParamSaturation.getValue());
        mParamContrast.setValue(rep.mParamContrast.getValue());
        mParamFalloff.setValue(rep.mParamFalloff.getValue());
    }

    public int getValue(int mode) {
        return mAllParam[mode].getValue();
    }

    public void setValue(int mode, int value) {
        mAllParam[mode].setValue(value);
    }

    @Override
    public String toString() {
        return getName() + " : " + mCenterX + ", " + mCenterY + " radius: " + mRadiusX;
    }

    @Override
    public FilterRepresentation copy() {
        FilterVignetteRepresentation representation = new FilterVignetteRepresentation();
        copyAllParameters(representation);
        return representation;
    }

    @Override
    protected void copyAllParameters(FilterRepresentation representation) {
        super.copyAllParameters(representation);
        representation.useParametersFrom(this);
    }

    @Override
    public void setCenter(float centerX, float centerY) {
        mCenterX = centerX;
        mCenterY = centerY;
    }

    @Override
    public float getCenterX() {
        return mCenterX;
    }

    @Override
    public float getCenterY() {
        return mCenterY;
    }

    @Override
    public void setRadius(float radiusX, float radiusY) {
        mRadiusX = radiusX;
        mRadiusY = radiusY;
    }

    @Override
    public void setRadiusX(float radiusX) {
        mRadiusX = radiusX;
    }

    @Override
    public void setRadiusY(float radiusY) {
        mRadiusY = radiusY;
    }

    @Override
    public float getRadiusX() {
        return mRadiusX;
    }

    @Override
    public float getRadiusY() {
        return mRadiusY;
    }

    public boolean isCenterSet() {
        return mCenterX != Float.NaN;
    }

    @Override
    public boolean isNil() {
        return false;
    }

    @Override
    public boolean equals(FilterRepresentation representation) {
        if (!super.equals(representation)) {
            return false;
        }
        if (representation instanceof FilterVignetteRepresentation) {
            FilterVignetteRepresentation rep = (FilterVignetteRepresentation) representation;
            for (int i = 0; i < mAllParam.length; i++) {
                if (mAllParam[i].getValue() != rep.mAllParam[i].getValue())
                    return false;
            }
            if (rep.getCenterX() == getCenterX()
                    && rep.getCenterY() == getCenterY()
                    && rep.getRadiusX() == getRadiusX()
                    && rep.getRadiusY() == getRadiusY()) {
                return true;
            }
        }
        return false;
    }

    private static final String ELLIPSE = "ellipse";
    private static final String ARGS = "adjust";
    @Override
    public void serializeRepresentation(JsonWriter writer) throws IOException {
        writer.beginObject();
        writer.name(ELLIPSE);
        writer.beginArray();
        writer.value(mCenterX);
        writer.value(mCenterY);
        writer.value(mRadiusX);
        writer.value(mRadiusY);
        writer.endArray();

        writer.name(ARGS);
        writer.beginArray();
        writer.value(mParamVignette.getValue());
        writer.value(mParamExposure.getValue());
        writer.value(mParamSaturation.getValue());
        writer.value(mParamContrast.getValue());
        writer.value(mParamFalloff.getValue());
        writer.endArray();
        writer.endObject();
    }


    @Override
    public void deSerializeRepresentation(JsonReader sreader) throws IOException {
        sreader.beginObject();

        while (sreader.hasNext()) {
            String name = sreader.nextName();
            if (name.startsWith(ELLIPSE)) {
                sreader.beginArray();
                sreader.hasNext();
                mCenterX = (float) sreader.nextDouble();
                sreader.hasNext();
                mCenterY = (float) sreader.nextDouble();
                sreader.hasNext();
                mRadiusX = (float) sreader.nextDouble();
                sreader.hasNext();
                mRadiusY = (float) sreader.nextDouble();
                sreader.hasNext();
                sreader.endArray();
            } else if (name.startsWith(ARGS)) {
                sreader.beginArray();
                sreader.hasNext();
                mParamVignette.setValue(sreader.nextInt());
                sreader.hasNext();
                mParamExposure.setValue(sreader.nextInt());
                sreader.hasNext();
                mParamSaturation.setValue(sreader.nextInt());
                sreader.hasNext();
                mParamContrast.setValue(sreader.nextInt());
                sreader.hasNext();
                mParamFalloff.setValue(sreader.nextInt());
                sreader.hasNext();
                sreader.endArray();
            } else  {
                sreader.skipValue();
            }
        }
        sreader.endObject();
    }
    public int getParameterMode() {
        return mParameterMode;
    }

    public void setParameterMode(int parameterMode) {
        mParameterMode = parameterMode;
    }

    public int getCurrentParameter() {
        return getValue(mParameterMode);
    }

    public void setCurrentParameter(int value) {
        setValue(mParameterMode, value);
    }

    public BasicParameterInt getFilterParameter(int index) {
        return mAllParam[index];
    }

}