2 * Copyright (C) 2012 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.
17 package com.android.gallery3d.filtershow.presets;
19 import android.graphics.Bitmap;
20 import android.graphics.Rect;
21 import android.util.Log;
23 import com.android.gallery3d.filtershow.ImageStateAdapter;
24 import com.android.gallery3d.filtershow.cache.ImageLoader;
25 import com.android.gallery3d.filtershow.filters.BaseFiltersManager;
26 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
27 import com.android.gallery3d.filtershow.filters.FiltersManager;
28 import com.android.gallery3d.filtershow.filters.ImageFilter;
29 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
30 import com.android.gallery3d.filtershow.imageshow.MasterImage;
32 import java.util.Vector;
34 public class ImagePreset {
36 private static final String LOGTAG = "ImagePreset";
38 private FilterRepresentation mBorder = null;
39 private float mScaleFactor = 1.0f;
40 public static final int QUALITY_ICON = 0;
41 public static final int QUALITY_PREVIEW = 1;
42 public static final int QUALITY_FINAL = 2;
43 private int mQuality = QUALITY_PREVIEW;
44 private ImageLoader mImageLoader = null;
46 private Vector<FilterRepresentation> mFilters = new Vector<FilterRepresentation>();
48 protected String mName = "Original";
49 private String mHistoryName = "Original";
50 protected boolean mIsFxPreset = false;
52 private boolean mDoApplyGeometry = true;
53 private boolean mDoApplyFilters = true;
55 public final GeometryMetadata mGeoData = new GeometryMetadata();
56 private boolean mPartialRendering = false;
57 private Rect mPartialRenderingBounds;
59 public ImagePreset() {
63 public ImagePreset(String historyName) {
64 setHistoryName(historyName);
68 public ImagePreset(ImagePreset source, String historyName) {
70 if (historyName != null) {
71 setHistoryName(historyName);
75 public ImagePreset(ImagePreset source) {
77 if (source.mBorder != null) {
78 mBorder = source.mBorder.clone();
80 for (int i = 0; i < source.mFilters.size(); i++) {
81 FilterRepresentation representation = source.mFilters.elementAt(i).clone();
82 addFilter(representation);
84 } catch (java.lang.CloneNotSupportedException e) {
85 Log.v(LOGTAG, "Exception trying to clone: " + e);
87 mName = source.name();
88 mHistoryName = source.name();
89 mIsFxPreset = source.isFx();
90 mImageLoader = source.getImageLoader();
92 mGeoData.set(source.mGeoData);
95 public FilterRepresentation getFilterRepresentation(int position) {
96 FilterRepresentation representation = null;
98 representation = mFilters.elementAt(position).clone();
99 } catch (CloneNotSupportedException e) {
102 return representation;
105 public int getPositionForRepresentation(FilterRepresentation representation) {
106 for (int i = 0; i < mFilters.size(); i++) {
107 if (mFilters.elementAt(i).getFilterClass() == representation.getFilterClass()) {
114 public FilterRepresentation getFilterRepresentationCopyFrom(FilterRepresentation filterRepresentation) {
115 // TODO: add concept of position in the filters (to allow multiple instances)
116 if (filterRepresentation == null) {
119 int position = getPositionForRepresentation(filterRepresentation);
120 if (position == -1) {
123 FilterRepresentation representation = null;
125 representation = mFilters.elementAt(position).clone();
126 } catch (CloneNotSupportedException e) {
129 return representation;
132 public void updateFilterRepresentation(FilterRepresentation representation) {
133 synchronized (mFilters) {
134 int position = getPositionForRepresentation(representation);
135 FilterRepresentation old = mFilters.elementAt(position);
136 old.updateTempParametersFrom(representation);
138 MasterImage.getImage().invalidatePreview();
141 public void setDoApplyGeometry(boolean value) {
142 mDoApplyGeometry = value;
145 public void setDoApplyFilters(boolean value) {
146 mDoApplyFilters = value;
149 public boolean getDoApplyFilters() {
150 return mDoApplyFilters;
153 public synchronized GeometryMetadata getGeometry() {
157 public boolean hasModifications() {
158 if (mBorder != null && !mBorder.isNil()) {
161 if (mGeoData.hasModifications()) {
164 for (int i = 0; i < mFilters.size(); i++) {
165 FilterRepresentation filter = mFilters.elementAt(i);
166 if (!filter.isNil()) {
173 public boolean isPanoramaSafe() {
174 if (mBorder != null && !mBorder.isNil()) {
177 if (mGeoData.hasModifications()) {
180 for (FilterRepresentation representation : mFilters) {
181 if (representation.getPriority() == FilterRepresentation.TYPE_VIGNETTE
182 && !representation.isNil()) {
185 if (representation.getPriority() == FilterRepresentation.TYPE_TINYPLANET
186 && !representation.isNil()) {
193 public synchronized void setGeometry(GeometryMetadata m) {
197 private void setBorder(FilterRepresentation filter) {
201 public boolean isFx() {
205 public void setIsFx(boolean value) {
209 public void setName(String name) {
214 public void setHistoryName(String name) {
218 public ImageLoader getImageLoader() {
222 public void setImageLoader(ImageLoader mImageLoader) {
223 this.mImageLoader = mImageLoader;
226 public boolean equals(ImagePreset preset) {
230 if (mDoApplyFilters && preset.mDoApplyFilters) {
231 for (int i = 0; i < preset.mFilters.size(); i++) {
232 FilterRepresentation a = preset.mFilters.elementAt(i);
233 FilterRepresentation b = mFilters.elementAt(i);
242 public boolean same(ImagePreset preset) {
243 if (preset == null) {
247 if (preset.mFilters.size() != mFilters.size()) {
251 if (!mName.equalsIgnoreCase(preset.name())) {
255 if (mDoApplyGeometry != preset.mDoApplyGeometry) {
259 if (mDoApplyGeometry && !mGeoData.equals(preset.mGeoData)) {
263 if (mDoApplyGeometry && mBorder != preset.mBorder) {
267 if (mBorder != null && !mBorder.equals(preset.mBorder)) {
271 if (mDoApplyFilters != preset.mDoApplyFilters) {
272 if (mFilters.size() > 0 || preset.mFilters.size() > 0) {
277 if (mDoApplyFilters && preset.mDoApplyFilters) {
278 for (int i = 0; i < preset.mFilters.size(); i++) {
279 FilterRepresentation a = preset.mFilters.elementAt(i);
280 FilterRepresentation b = mFilters.elementAt(i);
290 public int similarUpTo(ImagePreset preset) {
291 if (!mGeoData.equals(preset.mGeoData)) {
295 for (int i = 0; i < preset.mFilters.size(); i++) {
296 FilterRepresentation a = preset.mFilters.elementAt(i);
297 if (i < mFilters.size()) {
298 FilterRepresentation b = mFilters.elementAt(i);
309 return preset.mFilters.size();
312 public String name() {
316 public String historyName() {
320 public void showFilters() {
321 Log.v(LOGTAG, "\\\\\\ showFilters -- " + mFilters.size() + " filters");
323 for (FilterRepresentation representation : mFilters) {
324 Log.v(LOGTAG, " filter " + n + " : " + representation.toString());
327 Log.v(LOGTAG, "/// showFilters -- " + mFilters.size() + " filters");
330 public void addFilter(FilterRepresentation representation) {
331 Log.v(LOGTAG, "*** Add Filter *** " + representation);
332 if (representation.getPriority() == FilterRepresentation.TYPE_BORDER) {
333 setHistoryName(representation.getName());
334 setBorder(representation);
335 } else if (representation.getPriority() == FilterRepresentation.TYPE_FX) {
336 boolean found = false;
337 for (int i = 0; i < mFilters.size(); i++) {
338 int type = mFilters.elementAt(i).getPriority();
340 if (type != FilterRepresentation.TYPE_VIGNETTE) {
345 if (type == FilterRepresentation.TYPE_FX) {
347 mFilters.add(i, representation);
348 setHistoryName(representation.getName());
353 mFilters.add(representation);
354 setHistoryName(representation.getName());
357 mFilters.add(representation);
358 setHistoryName(representation.getName());
362 public FilterRepresentation getRepresentation(FilterRepresentation filterRepresentation) {
363 for (int i = 0; i < mFilters.size(); i++) {
364 FilterRepresentation representation = mFilters.elementAt(i);
365 if (representation.getFilterClass() == filterRepresentation.getFilterClass()) {
366 return representation;
369 if (mBorder != null && mBorder.getFilterClass() == filterRepresentation.getFilterClass()) {
375 public void setup() {
379 public Bitmap apply(Bitmap original) {
380 Bitmap bitmap = original;
381 bitmap = applyFilters(bitmap, -1, -1);
382 return applyBorder(bitmap);
385 public Bitmap applyGeometry(Bitmap bitmap) {
386 // Apply any transform -- 90 rotate, flip, straighten, crop
387 // Returns a new bitmap.
388 return mGeoData.apply(bitmap, mScaleFactor, mQuality);
391 public Bitmap applyBorder(Bitmap bitmap) {
392 if (mBorder != null && mDoApplyGeometry) {
393 ImageFilter filter = FiltersManager.getManager().getFilterForRepresentation(mBorder);
394 mBorder.synchronizeRepresentation();
395 filter.useRepresentation(mBorder);
396 filter.setImagePreset(this);
397 bitmap = filter.apply(bitmap, mScaleFactor, mQuality);
402 public Bitmap applyFilters(Bitmap bitmap, int from, int to) {
404 if (mDoApplyFilters) {
409 to = mFilters.size();
411 for (int i = from; i < to; i++) {
412 FilterRepresentation representation = null;
413 synchronized (mFilters) {
414 representation = mFilters.elementAt(i);
415 representation.synchronizeRepresentation();
417 ImageFilter filter = FiltersManager.getManager().getFilterForRepresentation(representation);
418 filter.useRepresentation(representation);
419 filter.setImagePreset(this);
420 bitmap = filter.apply(bitmap, mScaleFactor, mQuality);
427 public boolean canDoPartialRendering() {
428 if (mGeoData.hasModifications()) {
431 for (int i = 0; i < mFilters.size(); i++) {
432 FilterRepresentation representation = null;
433 synchronized (mFilters) {
434 representation = mFilters.elementAt(i);
436 if (!representation.supportsPartialRendering()) {
443 public void fillImageStateAdapter(ImageStateAdapter imageStateAdapter) {
444 if (imageStateAdapter == null) {
447 imageStateAdapter.clear();
448 // TODO: re-enable the state panel
449 imageStateAdapter.addAll(mFilters);
450 imageStateAdapter.notifyDataSetChanged();
453 public float getScaleFactor() {
457 public int getQuality() {
461 public void setQuality(int value) {
465 public void setScaleFactor(float value) {
466 mScaleFactor = value;
469 public void setPartialRendering(boolean partialRendering, Rect bounds) {
470 mPartialRendering = partialRendering;
471 mPartialRenderingBounds = bounds;
474 public boolean isPartialRendering() {
475 return mPartialRendering;
478 public Rect getPartialRenderingBounds() {
479 return mPartialRenderingBounds;