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.util.JsonReader;
20import android.util.JsonWriter;
21
22import com.android.gallery3d.R;
23import com.android.gallery3d.filtershow.controller.BasicParameterInt;
24import com.android.gallery3d.filtershow.controller.Parameter;
25import com.android.gallery3d.filtershow.controller.ParameterSet;
26import com.android.gallery3d.filtershow.editors.EditorChanSat;
27import com.android.gallery3d.filtershow.imageshow.ControlPoint;
28import com.android.gallery3d.filtershow.imageshow.Spline;
29
30import java.io.IOException;
31import java.util.Vector;
32
33/**
34 * Representation for a filter that has per channel & Master saturation
35 */
36public class FilterChanSatRepresentation extends FilterRepresentation implements ParameterSet {
37    private static final String LOGTAG = "FilterChanSatRepresentation";
38    private static final String ARGS = "ARGS";
39    private static final String SERIALIZATION_NAME = "channelsaturation";
40
41    public static final int MODE_MASTER = 0;
42    public static final int MODE_RED = 1;
43    public static final int MODE_YELLOW = 2;
44    public static final int MODE_GREEN = 3;
45    public static final int MODE_CYAN = 4;
46    public static final int MODE_BLUE = 5;
47    public static final int MODE_MAGENTA = 6;
48    private int mParameterMode = MODE_MASTER;
49
50    private static int MINSAT = -100;
51    private static int MAXSAT = 100;
52    private BasicParameterInt mParamMaster = new BasicParameterInt(MODE_MASTER, 0, MINSAT, MAXSAT);
53    private BasicParameterInt mParamRed = new BasicParameterInt(MODE_RED, 0, MINSAT, MAXSAT);
54    private BasicParameterInt mParamYellow = new BasicParameterInt(MODE_YELLOW, 0, MINSAT, MAXSAT);
55    private BasicParameterInt mParamGreen = new BasicParameterInt(MODE_GREEN, 0, MINSAT, MAXSAT);
56    private BasicParameterInt mParamCyan = new BasicParameterInt(MODE_CYAN, 0, MINSAT, MAXSAT);
57    private BasicParameterInt mParamBlue = new BasicParameterInt(MODE_BLUE, 0, MINSAT, MAXSAT);
58    private BasicParameterInt mParamMagenta = new BasicParameterInt(MODE_MAGENTA, 0, MINSAT, MAXSAT);
59
60    private BasicParameterInt[] mAllParam = {
61            mParamMaster,
62            mParamRed,
63            mParamYellow,
64            mParamGreen,
65            mParamCyan,
66            mParamBlue,
67            mParamMagenta};
68
69    public FilterChanSatRepresentation() {
70        super("ChannelSaturation");
71        setTextId(R.string.saturation);
72        setFilterType(FilterRepresentation.TYPE_NORMAL);
73        setSerializationName(SERIALIZATION_NAME);
74        setFilterClass(ImageFilterChanSat.class);
75        setEditorId(EditorChanSat.ID);
76        setSupportsPartialRendering(true);
77    }
78
79    public String toString() {
80        return getName() + " : " + mParamRed + ", " + mParamCyan + ", " + mParamRed
81                + ", " + mParamGreen + ", " + mParamMaster + ", " + mParamYellow;
82    }
83
84    @Override
85    public FilterRepresentation copy() {
86        FilterChanSatRepresentation representation = new FilterChanSatRepresentation();
87        copyAllParameters(representation);
88        return representation;
89    }
90
91    @Override
92    protected void copyAllParameters(FilterRepresentation representation) {
93        super.copyAllParameters(representation);
94        representation.useParametersFrom(this);
95    }
96
97    public void useParametersFrom(FilterRepresentation a) {
98        if (a instanceof FilterChanSatRepresentation) {
99            FilterChanSatRepresentation representation = (FilterChanSatRepresentation) a;
100
101            for (int i = 0; i < mAllParam.length; i++) {
102                mAllParam[i].copyFrom(representation.mAllParam[i]);
103            }
104        }
105    }
106
107    @Override
108    public boolean equals(FilterRepresentation representation) {
109        if (!super.equals(representation)) {
110            return false;
111        }
112        if (representation instanceof FilterChanSatRepresentation) {
113            FilterChanSatRepresentation rep = (FilterChanSatRepresentation) representation;
114            for (int i = 0; i < mAllParam.length; i++) {
115                if (rep.getValue(i) != getValue(i))
116                    return false;
117            }
118            return true;
119        }
120        return false;
121    }
122
123    public int getValue(int mode) {
124        return mAllParam[mode].getValue();
125    }
126
127    public void setValue(int mode, int value) {
128        mAllParam[mode].setValue(value);
129    }
130
131    public int getMinimum() {
132        return mParamMaster.getMinimum();
133    }
134
135    public int getMaximum() {
136        return mParamMaster.getMaximum();
137    }
138
139    public int getParameterMode() {
140        return mParameterMode;
141    }
142
143    public void setParameterMode(int parameterMode) {
144        mParameterMode = parameterMode;
145    }
146
147    public int getCurrentParameter() {
148        return getValue(mParameterMode);
149    }
150
151    public void setCurrentParameter(int value) {
152        setValue(mParameterMode, value);
153    }
154
155    @Override
156    public int getNumberOfParameters() {
157        return 6;
158    }
159
160    @Override
161    public Parameter getFilterParameter(int index) {
162        return mAllParam[index];
163    }
164
165    @Override
166    public void serializeRepresentation(JsonWriter writer) throws IOException {
167        writer.beginObject();
168
169        writer.name(ARGS);
170        writer.beginArray();
171        writer.value(getValue(MODE_MASTER));
172        writer.value(getValue(MODE_RED));
173        writer.value(getValue(MODE_YELLOW));
174        writer.value(getValue(MODE_GREEN));
175        writer.value(getValue(MODE_CYAN));
176        writer.value(getValue(MODE_BLUE));
177        writer.value(getValue(MODE_MAGENTA));
178        writer.endArray();
179        writer.endObject();
180    }
181
182    @Override
183    public void deSerializeRepresentation(JsonReader sreader) throws IOException {
184        sreader.beginObject();
185
186        while (sreader.hasNext()) {
187            String name = sreader.nextName();
188            if (name.startsWith(ARGS)) {
189                sreader.beginArray();
190                sreader.hasNext();
191                setValue(MODE_MASTER, sreader.nextInt());
192                sreader.hasNext();
193                setValue(MODE_RED, sreader.nextInt());
194                sreader.hasNext();
195                setValue(MODE_YELLOW, sreader.nextInt());
196                sreader.hasNext();
197                setValue(MODE_GREEN, sreader.nextInt());
198                sreader.hasNext();
199                setValue(MODE_CYAN, sreader.nextInt());
200                sreader.hasNext();
201                setValue(MODE_BLUE, sreader.nextInt());
202                sreader.hasNext();
203                setValue(MODE_MAGENTA, sreader.nextInt());
204                sreader.hasNext();
205                sreader.endArray();
206            } else {
207                sreader.skipValue();
208            }
209        }
210        sreader.endObject();
211    }
212}