2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.gallery3d.filtershow.filters;
18 import android.graphics.Rect;
19 import android.util.JsonReader;
20 import android.util.JsonWriter;
22 import com.android.gallery3d.R;
23 import com.android.gallery3d.filtershow.editors.EditorGrad;
24 import com.android.gallery3d.filtershow.imageshow.MasterImage;
25 import com.android.gallery3d.filtershow.imageshow.Line;
27 import java.io.IOException;
28 import java.util.Vector;
30 public class FilterGradRepresentation extends FilterRepresentation
32 private static final String LOGTAG = "FilterGradRepresentation";
33 public static final int MAX_POINTS = 16;
34 public static final int PARAM_BRIGHTNESS = 0;
35 public static final int PARAM_SATURATION = 1;
36 public static final int PARAM_CONTRAST = 2;
37 private static final double ADD_MIN_DIST = .05;
38 private static String LINE_NAME = "Point";
39 private static final String SERIALIZATION_NAME = "grad";
41 public FilterGradRepresentation() {
43 setSerializationName(SERIALIZATION_NAME);
45 setOverlayId(R.drawable.filtershow_button_grad);
46 setFilterClass(ImageFilterGrad.class);
47 setTextId(R.string.grad);
48 setEditorId(EditorGrad.ID);
51 public void trimVector(){
52 int n = mBands.size();
53 for (int i = n; i < MAX_POINTS; i++) {
54 mBands.add(new Band());
56 for (int i = MAX_POINTS; i < n; i++) {
61 Vector<Band> mBands = new Vector<Band>();
65 private boolean mask = true;
67 private int xPos1 = -1;
68 private int yPos1 = 100;
69 private int xPos2 = -1;
70 private int yPos2 = 100;
71 private int brightness = -40;
72 private int contrast = 0;
73 private int saturation = 0;
79 public Band(int x, int y) {
86 public Band(Band copy) {
92 brightness = copy.brightness;
93 contrast = copy.contrast;
94 saturation = copy.saturation;
100 public String toString() {
102 for (Band point : mBands) {
107 return "c=" + mBands.indexOf(mCurrentBand) + "[" + mBands.size() + "]" + count;
110 private void creatExample() {
126 public void useParametersFrom(FilterRepresentation a) {
127 FilterGradRepresentation rep = (FilterGradRepresentation) a;
128 Vector<Band> tmpBands = new Vector<Band>();
129 int n = (rep.mCurrentBand == null) ? 0 : rep.mBands.indexOf(rep.mCurrentBand);
130 for (Band band : rep.mBands) {
131 tmpBands.add(new Band(band));
135 mCurrentBand = mBands.elementAt(n);
139 public FilterRepresentation copy() {
140 FilterGradRepresentation representation = new FilterGradRepresentation();
141 copyAllParameters(representation);
142 return representation;
146 protected void copyAllParameters(FilterRepresentation representation) {
147 super.copyAllParameters(representation);
148 representation.useParametersFrom(this);
152 public boolean equals(FilterRepresentation representation) {
153 if (representation instanceof FilterGradRepresentation) {
154 FilterGradRepresentation rep = (FilterGradRepresentation) representation;
155 int n = getNumberOfBands();
156 if (rep.getNumberOfBands() != n) {
159 for (int i = 0; i < mBands.size(); i++) {
160 Band b1 = mBands.get(i);
161 Band b2 = rep.mBands.get(i);
162 if (b1.mask != b2.mask
163 || b1.brightness != b2.brightness
164 || b1.contrast != b2.contrast
165 || b1.saturation != b2.saturation
166 || b1.xPos1 != b2.xPos1
167 || b1.xPos2 != b2.xPos2
168 || b1.yPos1 != b2.yPos1
169 || b1.yPos2 != b2.yPos2) {
178 public int getNumberOfBands() {
180 for (Band point : mBands) {
188 public int addBand(Rect rect) {
189 mBands.add(0, mCurrentBand = new Band(rect.centerX(), rect.centerY()));
190 mCurrentBand.mask = false;
191 int x = (mCurrentBand.xPos1 + mCurrentBand.xPos2)/2;
192 int y = (mCurrentBand.yPos1 + mCurrentBand.yPos2)/2;
193 double addDelta = ADD_MIN_DIST * Math.max(rect.width(), rect.height());
194 boolean moved = true;
196 int toMove = mBands.indexOf(mCurrentBand);
205 for (Band point : mBands) {
211 for (Band point : mBands) {
215 int index = mBands.indexOf(point);
217 if (toMove != index) {
218 double dist = Math.hypot(point.xPos1 - x, point.yPos1 - y);
219 if (dist < addDelta) {
221 mCurrentBand.xPos1 += addDelta;
222 mCurrentBand.yPos1 += addDelta;
223 mCurrentBand.xPos2 += addDelta;
224 mCurrentBand.yPos2 += addDelta;
225 x = (mCurrentBand.xPos1 + mCurrentBand.xPos2)/2;
226 y = (mCurrentBand.yPos1 + mCurrentBand.yPos2)/2;
228 if (mCurrentBand.yPos1 > rect.bottom) {
229 mCurrentBand.yPos1 = (int) (rect.top + addDelta);
231 if (mCurrentBand.xPos1 > rect.right) {
232 mCurrentBand.xPos1 = (int) (rect.left + addDelta);
242 public void deleteCurrentBand() {
243 int index = mBands.indexOf(mCurrentBand);
244 mBands.remove(mCurrentBand);
246 if (getNumberOfBands() == 0) {
247 addBand(MasterImage.getImage().getOriginalBounds());
249 mCurrentBand = mBands.get(0);
252 public void nextPoint(){
253 int index = mBands.indexOf(mCurrentBand);
258 index = (index+1)% mBands.size();
259 point = mBands.get(index);
260 if (k++ >= mBands.size()) {
264 while (point.mask == true);
265 mCurrentBand = mBands.get(index);
268 public void setSelectedPoint(int pos) {
269 mCurrentBand = mBands.get(pos);
272 public int getSelectedPoint() {
273 return mBands.indexOf(mCurrentBand);
276 public boolean[] getMask() {
277 boolean[] ret = new boolean[mBands.size()];
279 for (Band point : mBands) {
280 ret[i++] = !point.mask;
285 public int[] getXPos1() {
286 int[] ret = new int[mBands.size()];
288 for (Band point : mBands) {
289 ret[i++] = point.xPos1;
294 public int[] getYPos1() {
295 int[] ret = new int[mBands.size()];
297 for (Band point : mBands) {
298 ret[i++] = point.yPos1;
303 public int[] getXPos2() {
304 int[] ret = new int[mBands.size()];
306 for (Band point : mBands) {
307 ret[i++] = point.xPos2;
312 public int[] getYPos2() {
313 int[] ret = new int[mBands.size()];
315 for (Band point : mBands) {
316 ret[i++] = point.yPos2;
321 public int[] getBrightness() {
322 int[] ret = new int[mBands.size()];
324 for (Band point : mBands) {
325 ret[i++] = point.brightness;
330 public int[] getContrast() {
331 int[] ret = new int[mBands.size()];
333 for (Band point : mBands) {
334 ret[i++] = point.contrast;
339 public int[] getSaturation() {
340 int[] ret = new int[mBands.size()];
342 for (Band point : mBands) {
343 ret[i++] = point.saturation;
348 public int getParameter(int type) {
350 case PARAM_BRIGHTNESS:
351 return mCurrentBand.brightness;
352 case PARAM_SATURATION:
353 return mCurrentBand.saturation;
355 return mCurrentBand.contrast;
357 throw new IllegalArgumentException("no such type " + type);
360 public int getParameterMax(int type) {
362 case PARAM_BRIGHTNESS:
364 case PARAM_SATURATION:
369 throw new IllegalArgumentException("no such type " + type);
372 public int getParameterMin(int type) {
374 case PARAM_BRIGHTNESS:
376 case PARAM_SATURATION:
381 throw new IllegalArgumentException("no such type " + type);
384 public void setParameter(int type, int value) {
385 mCurrentBand.mask = false;
387 case PARAM_BRIGHTNESS:
388 mCurrentBand.brightness = value;
390 case PARAM_SATURATION:
391 mCurrentBand.saturation = value;
394 mCurrentBand.contrast = value;
397 throw new IllegalArgumentException("no such type " + type);
402 public void setPoint1(float x, float y) {
403 mCurrentBand.xPos1 = (int)x;
404 mCurrentBand.yPos1 = (int)y;
408 public void setPoint2(float x, float y) {
409 mCurrentBand.xPos2 = (int)x;
410 mCurrentBand.yPos2 = (int)y;
414 public float getPoint1X() {
415 return mCurrentBand.xPos1;
419 public float getPoint1Y() {
420 return mCurrentBand.yPos1;
423 public float getPoint2X() {
424 return mCurrentBand.xPos2;
428 public float getPoint2Y() {
429 return mCurrentBand.yPos2;
433 public void serializeRepresentation(JsonWriter writer) throws IOException {
434 writer.beginObject();
435 int len = mBands.size();
438 for (int i = 0; i < len; i++) {
439 Band point = mBands.get(i);
443 writer.name(LINE_NAME + count);
446 writer.value(point.xPos1);
447 writer.value(point.yPos1);
448 writer.value(point.xPos2);
449 writer.value(point.yPos2);
450 writer.value(point.brightness);
451 writer.value(point.contrast);
452 writer.value(point.saturation);
459 public void deSerializeRepresentation(JsonReader sreader) throws IOException {
460 sreader.beginObject();
461 Vector<Band> points = new Vector<Band>();
463 while (sreader.hasNext()) {
464 String name = sreader.nextName();
465 if (name.startsWith(LINE_NAME)) {
466 int pointNo = Integer.parseInt(name.substring(LINE_NAME.length()));
467 sreader.beginArray();
471 p.xPos1 = sreader.nextInt();
473 p.yPos1 = sreader.nextInt();
475 p.xPos2 = sreader.nextInt();
477 p.yPos2 = sreader.nextInt();
479 p.brightness = sreader.nextInt();
481 p.contrast = sreader.nextInt();
483 p.saturation = sreader.nextInt();
494 mCurrentBand = mBands.get(0);