summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/presets/ImagePreset.java
blob: fb58d1ff94708c3f36f5abc2340839fd98ddc010 (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
/*
 * 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.presets;

import android.graphics.Bitmap;
import android.util.Log;

import com.android.gallery3d.filtershow.ImageStateAdapter;
import com.android.gallery3d.filtershow.cache.ImageLoader;
import com.android.gallery3d.filtershow.filters.FilterRepresentation;
import com.android.gallery3d.filtershow.filters.FiltersManager;
import com.android.gallery3d.filtershow.filters.ImageFilter;
import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
import com.android.gallery3d.filtershow.imageshow.MasterImage;

import java.util.Vector;

public class ImagePreset {

    private static final String LOGTAG = "ImagePreset";

    private FilterRepresentation mBorder = null;
    private float mScaleFactor = 1.0f;
    private boolean mIsHighQuality = false;
    private ImageLoader mImageLoader = null;

    private Vector<FilterRepresentation> mFilters = new Vector<FilterRepresentation>();

    protected String mName = "Original";
    private String mHistoryName = "Original";
    protected boolean mIsFxPreset = false;

    private boolean mDoApplyGeometry = true;
    private boolean mDoApplyFilters = true;

    public final GeometryMetadata mGeoData = new GeometryMetadata();

    public ImagePreset() {
        setup();
    }

    public ImagePreset(String historyName) {
        setHistoryName(historyName);
        setup();
    }

    public ImagePreset(ImagePreset source, String historyName) {
        this(source);
        if (historyName != null) {
            setHistoryName(historyName);
        }
    }

    public ImagePreset(ImagePreset source) {
        try {
            if (source.mBorder != null) {
                mBorder = source.mBorder.clone();
            }
            for (int i = 0; i < source.mFilters.size(); i++) {
                FilterRepresentation representation = source.mFilters.elementAt(i).clone();
                representation.setImagePreset(this);
                addFilter(representation);
            }
        } catch (java.lang.CloneNotSupportedException e) {
            Log.v(LOGTAG, "Exception trying to clone: " + e);
        }
        mName = source.name();
        mHistoryName = source.name();
        mIsFxPreset = source.isFx();
        mImageLoader = source.getImageLoader();

        mGeoData.set(source.mGeoData);
    }

    public FilterRepresentation getFilterRepresentation(int position) {
        FilterRepresentation representation = null;
        try {
            representation = mFilters.elementAt(position).clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return representation;
    }

    public int getPositionForRepresentation(FilterRepresentation representation) {
        for (int i = 0; i < mFilters.size(); i++) {
            if (mFilters.elementAt(i).getFilterClass() == representation.getFilterClass()) {
                return i;
            }
        }
        return -1;
    }

    public FilterRepresentation getFilterRepresentationCopyFrom(FilterRepresentation filterRepresentation) {
        // TODO: add concept of position in the filters (to allow multiple instances)
        if (filterRepresentation == null) {
            return null;
        }
        int position = getPositionForRepresentation(filterRepresentation);
        if (position == -1) {
            return null;
        }
        FilterRepresentation representation = null;
        try {
            representation = mFilters.elementAt(position).clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return representation;
    }

    public void updateFilterRepresentation(FilterRepresentation representation) {
        synchronized (mFilters) {
            int position = getPositionForRepresentation(representation);
            FilterRepresentation old = mFilters.elementAt(position);
            old.useParametersFrom(representation);
        }
        MasterImage.getImage().invalidatePreview();
    }

    public void setDoApplyGeometry(boolean value) {
        mDoApplyGeometry = value;
    }

    public void setDoApplyFilters(boolean value) {
        mDoApplyFilters = value;
    }

    public boolean getDoApplyFilters() {
        return mDoApplyFilters;
    }

    public synchronized GeometryMetadata getGeometry() {
        return mGeoData;
    }

    public boolean hasModifications() {
        if (mBorder != null && !mBorder.isNil()) {
            return true;
        }
        if (mGeoData.hasModifications()) {
            return true;
        }
        for (int i = 0; i < mFilters.size(); i++) {
            FilterRepresentation filter = mFilters.elementAt(i);
            if (!filter.isNil()) {
                return true;
            }
        }
        return false;
    }

    public boolean isPanoramaSafe() {
        if (mBorder != null && !mBorder.isNil()) {
            return false;
        }
        if (mGeoData.hasModifications()) {
            return false;
        }
        for (FilterRepresentation representation : mFilters) {
            if (representation.getPriority() == ImageFilter.TYPE_VIGNETTE
                && !representation.isNil()) {
                return false;
            }
            if (representation.getPriority() == ImageFilter.TYPE_TINYPLANET
                && !representation.isNil()) {
                return false;
            }
        }
        return true;
    }

    public synchronized void setGeometry(GeometryMetadata m) {
        mGeoData.set(m);
    }

    private void setBorder(FilterRepresentation filter) {
        mBorder = filter;
    }

    public boolean isFx() {
        return mIsFxPreset;
    }

    public void setIsFx(boolean value) {
        mIsFxPreset = value;
    }

    public void setName(String name) {
        mName = name;
        mHistoryName = name;
    }

    public void setHistoryName(String name) {
        mHistoryName = name;
    }

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }

    public void setImageLoader(ImageLoader mImageLoader) {
        this.mImageLoader = mImageLoader;
    }

    public boolean equals(ImagePreset preset) {
        if (!same(preset)) {
            return false;
        }
        if (mDoApplyFilters && preset.mDoApplyFilters) {
            for (int i = 0; i < preset.mFilters.size(); i++) {
                FilterRepresentation a = preset.mFilters.elementAt(i);
                FilterRepresentation b = mFilters.elementAt(i);
                if (!a.equals(b)) {
                    return false;
                }
            }
        }
        return true;
    }

    public boolean same(ImagePreset preset) {
        if (preset == null) {
            return false;
        }

        if (preset.mFilters.size() != mFilters.size()) {
            return false;
        }

        if (!mName.equalsIgnoreCase(preset.name())) {
            return false;
        }

        if (mDoApplyGeometry != preset.mDoApplyGeometry) {
            return false;
        }

        if (mDoApplyGeometry && !mGeoData.equals(preset.mGeoData)) {
            return false;
        }

        if (mDoApplyGeometry && mBorder != preset.mBorder) {
            return false;
        }

        if (mBorder != null && !mBorder.equals(preset.mBorder)) {
            return false;
        }

        if (mDoApplyFilters != preset.mDoApplyFilters) {
            if (mFilters.size() > 0 || preset.mFilters.size() > 0) {
                return false;
            }
        }

        if (mDoApplyFilters && preset.mDoApplyFilters) {
            for (int i = 0; i < preset.mFilters.size(); i++) {
                FilterRepresentation a = preset.mFilters.elementAt(i);
                FilterRepresentation b = mFilters.elementAt(i);
                if (!a.same(b)) {
                    return false;
                }
            }
        }

        return true;
    }

    public int similarUpTo(ImagePreset preset) {
        if (!mGeoData.equals(preset.mGeoData)) {
            return -1;
        }

        for (int i = 0; i < preset.mFilters.size(); i++) {
            FilterRepresentation a = preset.mFilters.elementAt(i);
            if (i < mFilters.size()) {
                FilterRepresentation b = mFilters.elementAt(i);
                if (!a.same(b)) {
                    return i;
                }
                if (!a.equals(b)) {
                    return i;
                }
            } else {
                return i;
            }
        }
        return preset.mFilters.size();
    }

    public String name() {
        return mName;
    }

    public String historyName() {
        return mHistoryName;
    }

    public void showFilters() {
        Log.v(LOGTAG, "\\\\\\ showFilters -- " + mFilters.size() + " filters");
        int n = 0;
        for (FilterRepresentation representation : mFilters) {
            Log.v(LOGTAG, " filter " + n + " : " + representation.toString());
            n++;
        }
        Log.v(LOGTAG, "/// showFilters -- " + mFilters.size() + " filters");
    }

    public void addFilter(FilterRepresentation representation) {
        Log.v(LOGTAG, "*** Add Filter *** " + representation);
        if (representation.getPriority() == ImageFilter.TYPE_BORDER) {
            setHistoryName(representation.getName());
            setBorder(representation);
        } else if (representation.getPriority() == ImageFilter.TYPE_FX) {
            boolean found = false;
            for (int i = 0; i < mFilters.size(); i++) {
                int type = mFilters.elementAt(i).getPriority();
                if (found) {
                    if (type != ImageFilter.TYPE_VIGNETTE) {
                        mFilters.remove(i);
                        continue;
                    }
                }
                if (type == ImageFilter.TYPE_FX) {
                    mFilters.remove(i);
                    mFilters.add(i, representation);
                    setHistoryName(representation.getName());
                    found = true;
                }
            }
            if (!found) {
                mFilters.add(representation);
                setHistoryName(representation.getName());
            }
        } else {
            mFilters.add(representation);
            setHistoryName(representation.getName());
        }
        representation.setImagePreset(this);
    }

    public FilterRepresentation getRepresentation(FilterRepresentation filterRepresentation) {
        for (int i = 0; i < mFilters.size(); i++) {
            FilterRepresentation representation = mFilters.elementAt(i);
            if (representation.getFilterClass() == filterRepresentation.getFilterClass()) {
                return representation;
            }
        }
        if (mBorder != null && mBorder.getFilterClass() == filterRepresentation.getFilterClass()) {
            return mBorder;
        }
        return null;
    }

    public void setup() {
        // do nothing here
    }

    public Bitmap apply(Bitmap original) {
        Bitmap bitmap = original;
        bitmap = applyFilters(bitmap, -1, -1);
        return applyBorder(bitmap);
    }

    public Bitmap applyGeometry(Bitmap bitmap) {
        // Apply any transform -- 90 rotate, flip, straighten, crop
        // Returns a new bitmap.
        return mGeoData.apply(bitmap, mScaleFactor, mIsHighQuality);
    }

    public Bitmap applyBorder(Bitmap bitmap) {
        if (mBorder != null && mDoApplyGeometry) {
            ImageFilter filter = FiltersManager.getManager().getFilterForRepresentation(mBorder);
            filter.useRepresentation(mBorder);
            bitmap = filter.apply(bitmap, mScaleFactor, mIsHighQuality);
        }
        return bitmap;
    }

    public Bitmap applyFilters(Bitmap bitmap, int from, int to) {

        if (mDoApplyFilters) {
            if (from < 0) {
                from = 0;
            }
            if (to == -1) {
                to = mFilters.size();
            }
            for (int i = from; i < to; i++) {
                FilterRepresentation representation = null;
                synchronized (mFilters) {
                    representation = mFilters.elementAt(i);
                }
                ImageFilter filter = FiltersManager.getManager().getFilterForRepresentation(representation);
                filter.useRepresentation(representation);
                bitmap = filter.apply(bitmap, mScaleFactor, mIsHighQuality);
            }
        }

        return bitmap;
    }

    public void fillImageStateAdapter(ImageStateAdapter imageStateAdapter) {
        if (imageStateAdapter == null) {
            return;
        }
        imageStateAdapter.clear();
        // TODO: re-enable the state panel
        // imageStateAdapter.addAll(mFilters);
        imageStateAdapter.notifyDataSetChanged();
    }

    public float getScaleFactor() {
        return mScaleFactor;
    }

    public boolean isHighQuality() {
        return mIsHighQuality;
    }

    public void setIsHighQuality(boolean value) {
        mIsHighQuality = value;
    }

    public void setScaleFactor(float value) {
        mScaleFactor = value;
    }
}