summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/filters/FilterGradRepresentation.java
blob: 0c272d48ab73560bc3fa1fed4b1a01ffef6de3b4 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * Copyright (C) 2013 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.graphics.Rect;
import android.util.JsonReader;
import android.util.JsonWriter;

import com.android.gallery3d.R;
import com.android.gallery3d.filtershow.editors.EditorGrad;
import com.android.gallery3d.filtershow.imageshow.MasterImage;
import com.android.gallery3d.filtershow.imageshow.Line;

import java.io.IOException;
import java.util.Vector;

public class FilterGradRepresentation extends FilterRepresentation
        implements Line {
    private static final String LOGTAG = "FilterGradRepresentation";
    public static final int MAX_POINTS = 16;
    public static final int PARAM_BRIGHTNESS = 0;
    public static final int PARAM_SATURATION = 1;
    public static final int PARAM_CONTRAST = 2;
    private static final double ADD_MIN_DIST = .05;
    private static String LINE_NAME = "Point";
    private static final  String SERIALIZATION_NAME = "grad";

    public FilterGradRepresentation() {
        super("Grad");
        setSerializationName(SERIALIZATION_NAME);
        creatExample();
        setOverlayId(R.drawable.filtershow_button_grad);
        setFilterClass(ImageFilterGrad.class);
        setTextId(R.string.grad);
        setEditorId(EditorGrad.ID);
    }

    public void trimVector(){
        int n = mBands.size();
        for (int i = n; i < MAX_POINTS; i++) {
            mBands.add(new Band());
        }
        for (int i = MAX_POINTS; i <  n; i++) {
            mBands.remove(i);
        }
    }

    Vector<Band> mBands = new Vector<Band>();
    Band mCurrentBand;

    static class Band {
        private boolean mask = true;

        private int xPos1 = -1;
        private int yPos1 = 100;
        private int xPos2 = -1;
        private int yPos2 = 100;
        private int brightness = 40;
        private int contrast = 0;
        private int saturation = 0;


        public Band() {
        }

        public Band(int x, int y) {
            xPos1 = x;
            yPos1 = y+30;
            xPos2 = x;
            yPos2 = y-30;
        }

        public Band(Band copy) {
            mask = copy.mask;
            xPos1 = copy.xPos1;
            yPos1 = copy.yPos1;
            xPos2 = copy.xPos2;
            yPos2 = copy.yPos2;
            brightness = copy.brightness;
            contrast = copy.contrast;
            saturation = copy.saturation;
        }

    }

    @Override
    public String toString() {
        int count = 0;
        for (Band point : mBands) {
            if (!point.mask) {
                count++;
            }
        }
        return "c=" + mBands.indexOf(mBands) + "[" + mBands.size() + "]" + count;
    }

    private void creatExample() {
        Band p = new Band();
        p.mask = false;
        p.xPos1 = -1;
        p.yPos1 = 100;
        p.xPos2 = -1;
        p.yPos2 = 100;
        p.brightness = 40;
        p.contrast = 0;
        p.saturation = 0;
        mBands.add(0, p);
        mCurrentBand = p;
        trimVector();
    }

    @Override
    public void useParametersFrom(FilterRepresentation a) {
            FilterGradRepresentation rep = (FilterGradRepresentation) a;
            Vector<Band> tmpBands = new Vector<Band>();
            int n = (rep.mCurrentBand == null) ? 0 : rep.mBands.indexOf(rep.mCurrentBand);
            for (Band band : rep.mBands) {
                tmpBands.add(new Band(band));
            }
            mCurrentBand = null;
            mBands = tmpBands;
            mCurrentBand = mBands.elementAt(n);
    }

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

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

    @Override
    public boolean equals(FilterRepresentation representation) {
        if (representation instanceof FilterGradRepresentation) {
            FilterGradRepresentation rep = (FilterGradRepresentation) representation;
            int n = getNumberOfBands();
            if (rep.getNumberOfBands() != n) {
                return false;
            }
            for (int i = 0; i < mBands.size(); i++) {
                Band b1 = mBands.get(i);
                Band b2 = rep.mBands.get(i);
                if (b1.mask != b2.mask
                        || b1.brightness != b2.brightness
                        || b1.contrast != b2.contrast
                        || b1.saturation != b2.saturation
                        || b1.xPos1 != b2.xPos1
                        || b1.xPos2 != b2.xPos2
                        || b1.yPos1 != b2.yPos1
                        || b1.yPos2 != b2.yPos2) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public int getNumberOfBands() {
        int count = 0;
        for (Band point : mBands) {
            if (!point.mask) {
                count++;
            }
        }
        return count;
    }

    public int addBand(Rect rect) {
        mBands.add(0, mCurrentBand = new Band(rect.centerX(), rect.centerY()));
        mCurrentBand.mask = false;
        int x = (mCurrentBand.xPos1 + mCurrentBand.xPos2)/2;
        int y = (mCurrentBand.yPos1 + mCurrentBand.yPos2)/2;
        double addDelta = ADD_MIN_DIST * Math.max(rect.width(), rect.height());
        boolean moved = true;
        int count = 0;
        int toMove =  mBands.indexOf(mCurrentBand);

        while (moved) {
            moved = false;
            count++;
            if (count > 14) {
                break;
            }

            for (Band point : mBands) {
                if (point.mask) {
                    break;
                }
            }

            for (Band point : mBands) {
                if (point.mask) {
                    break;
                }
                int index = mBands.indexOf(point);

                if (toMove != index) {
                    double dist = Math.hypot(point.xPos1 - x, point.yPos1 - y);
                    if (dist < addDelta) {
                        moved = true;
                        mCurrentBand.xPos1 += addDelta;
                        mCurrentBand.yPos1 += addDelta;
                        mCurrentBand.xPos2 += addDelta;
                        mCurrentBand.yPos2 += addDelta;
                        x = (mCurrentBand.xPos1 + mCurrentBand.xPos2)/2;
                        y = (mCurrentBand.yPos1 + mCurrentBand.yPos2)/2;

                        if (mCurrentBand.yPos1 > rect.bottom) {
                            mCurrentBand.yPos1 = (int) (rect.top + addDelta);
                        }
                        if (mCurrentBand.xPos1 > rect.right) {
                            mCurrentBand.xPos1 = (int) (rect.left + addDelta);
                        }
                    }
                }
            }
        }
        trimVector();
        return 0;
    }

    public void deleteCurrentBand() {
        int index = mBands.indexOf(mCurrentBand);
        mBands.remove(mCurrentBand);
        trimVector();
        if (getNumberOfBands() == 0) {
            addBand(MasterImage.getImage().getOriginalBounds());
        }
        mCurrentBand = mBands.get(0);
    }

    public void  nextPoint(){
        int index = mBands.indexOf(mCurrentBand);
        int tmp = index;
        Band point;
        int k = 0;
        do  {
            index =   (index+1)% mBands.size();
            point = mBands.get(index);
            if (k++ >= mBands.size()) {
                break;
            }
        }
        while (point.mask == true);
        mCurrentBand = mBands.get(index);
    }

    public void setSelectedPoint(int pos) {
        mCurrentBand = mBands.get(pos);
    }

    public int getSelectedPoint() {
        return mBands.indexOf(mCurrentBand);
    }

    public boolean[] getMask() {
        boolean[] ret = new boolean[mBands.size()];
        int i = 0;
        for (Band point : mBands) {
            ret[i++] = !point.mask;
        }
        return ret;
    }

    public int[] getXPos1() {
        int[] ret = new int[mBands.size()];
        int i = 0;
        for (Band point : mBands) {
            ret[i++] = point.xPos1;
        }
        return ret;
    }

    public int[] getYPos1() {
        int[] ret = new int[mBands.size()];
        int i = 0;
        for (Band point : mBands) {
            ret[i++] = point.yPos1;
        }
        return ret;
    }

    public int[] getXPos2() {
        int[] ret = new int[mBands.size()];
        int i = 0;
        for (Band point : mBands) {
            ret[i++] = point.xPos2;
        }
        return ret;
    }

    public int[] getYPos2() {
        int[] ret = new int[mBands.size()];
        int i = 0;
        for (Band point : mBands) {
            ret[i++] = point.yPos2;
        }
        return ret;
    }

    public int[] getBrightness() {
        int[] ret = new int[mBands.size()];
        int i = 0;
        for (Band point : mBands) {
            ret[i++] = point.brightness;
        }
        return ret;
    }

    public int[] getContrast() {
        int[] ret = new int[mBands.size()];
        int i = 0;
        for (Band point : mBands) {
            ret[i++] = point.contrast;
        }
        return ret;
    }

    public int[] getSaturation() {
        int[] ret = new int[mBands.size()];
        int i = 0;
        for (Band point : mBands) {
            ret[i++] = point.saturation;
        }
        return ret;
    }

    public int getParameter(int type) {
        switch (type){
            case PARAM_BRIGHTNESS:
                return mCurrentBand.brightness;
            case PARAM_SATURATION:
                return mCurrentBand.saturation;
            case PARAM_CONTRAST:
                return mCurrentBand.contrast;
        }
        throw new IllegalArgumentException("no such type " + type);
    }

    public int getParameterMax(int type) {
        switch (type) {
            case PARAM_BRIGHTNESS:
                return 100;
            case PARAM_SATURATION:
                return 100;
            case PARAM_CONTRAST:
                return 100;
        }
        throw new IllegalArgumentException("no such type " + type);
    }

    public int getParameterMin(int type) {
        switch (type) {
            case PARAM_BRIGHTNESS:
                return -100;
            case PARAM_SATURATION:
                return -100;
            case PARAM_CONTRAST:
                return -100;
        }
        throw new IllegalArgumentException("no such type " + type);
    }

    public void setParameter(int type, int value) {
        mCurrentBand.mask = false;
        switch (type) {
            case PARAM_BRIGHTNESS:
                mCurrentBand.brightness = value;
                break;
            case PARAM_SATURATION:
                mCurrentBand.saturation = value;
                break;
            case PARAM_CONTRAST:
                mCurrentBand.contrast = value;
                break;
            default:
                throw new IllegalArgumentException("no such type " + type);
        }
    }

    @Override
    public void setPoint1(float x, float y) {
        mCurrentBand.xPos1 = (int)x;
        mCurrentBand.yPos1 = (int)y;
    }

    @Override
    public void setPoint2(float x, float y) {
        mCurrentBand.xPos2 = (int)x;
        mCurrentBand.yPos2 = (int)y;
    }

    @Override
    public float getPoint1X() {
        return mCurrentBand.xPos1;
    }

    @Override
    public float getPoint1Y() {
        return mCurrentBand.yPos1;
    }
    @Override
    public float getPoint2X() {
        return mCurrentBand.xPos2;
    }

    @Override
    public float getPoint2Y() {
        return mCurrentBand.yPos2;
    }

    @Override
    public void serializeRepresentation(JsonWriter writer) throws IOException {
        writer.beginObject();
        int len = mBands.size();
        int count = 0;

        for (int i = 0; i < len; i++) {
            Band point = mBands.get(i);
            if (point.mask) {
                continue;
            }
            writer.name(LINE_NAME + count);
            count++;
            writer.beginArray();
            writer.value(point.xPos1);
            writer.value(point.yPos1);
            writer.value(point.xPos2);
            writer.value(point.yPos2);
            writer.value(point.brightness);
            writer.value(point.contrast);
            writer.value(point.saturation);
            writer.endArray();
        }
        writer.endObject();
    }

    @Override
    public void deSerializeRepresentation(JsonReader sreader) throws IOException {
        sreader.beginObject();
        Vector<Band> points = new Vector<Band>();

        while (sreader.hasNext()) {
            String name = sreader.nextName();
            if (name.startsWith(LINE_NAME)) {
                int pointNo = Integer.parseInt(name.substring(LINE_NAME.length()));
                sreader.beginArray();
                Band p = new Band();
                p.mask = false;
                sreader.hasNext();
                p.xPos1 = sreader.nextInt();
                sreader.hasNext();
                p.yPos1 = sreader.nextInt();
                sreader.hasNext();
                p.xPos2 = sreader.nextInt();
                sreader.hasNext();
                p.yPos2 = sreader.nextInt();
                sreader.hasNext();
                p.brightness = sreader.nextInt();
                sreader.hasNext();
                p.contrast = sreader.nextInt();
                sreader.hasNext();
                p.saturation = sreader.nextInt();
                sreader.hasNext();
                sreader.endArray();
                points.add(p);

            } else {
                sreader.skipValue();
            }
        }
        mBands = points;
        trimVector();
        mCurrentBand = mBands.get(0);
        sreader.endObject();
    }
}