OSDN Git Service

322a34d7b9e43601a4d345b15043a3550e00c024
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / pipeline / ImagePreset.java
1 /*
2  * Copyright (C) 2012 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
17 package com.android.gallery3d.filtershow.pipeline;
18
19 import android.graphics.Bitmap;
20 import android.graphics.Rect;
21 import android.support.v8.renderscript.Allocation;
22 import android.util.JsonReader;
23 import android.util.JsonWriter;
24 import android.util.Log;
25
26 import com.android.gallery3d.R;
27 import com.android.gallery3d.filtershow.cache.BitmapCache;
28 import com.android.gallery3d.filtershow.cache.ImageLoader;
29 import com.android.gallery3d.filtershow.filters.BaseFiltersManager;
30 import com.android.gallery3d.filtershow.filters.FilterCropRepresentation;
31 import com.android.gallery3d.filtershow.filters.FilterFxRepresentation;
32 import com.android.gallery3d.filtershow.filters.FilterImageBorderRepresentation;
33 import com.android.gallery3d.filtershow.filters.FilterMirrorRepresentation;
34 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
35 import com.android.gallery3d.filtershow.filters.FilterRotateRepresentation;
36 import com.android.gallery3d.filtershow.filters.FilterStraightenRepresentation;
37 import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
38 import com.android.gallery3d.filtershow.filters.FiltersManager;
39 import com.android.gallery3d.filtershow.filters.ImageFilter;
40 import com.android.gallery3d.filtershow.imageshow.GeometryMathUtils;
41 import com.android.gallery3d.filtershow.imageshow.MasterImage;
42 import com.android.gallery3d.filtershow.state.State;
43 import com.android.gallery3d.filtershow.state.StateAdapter;
44 import com.android.gallery3d.util.UsageStatistics;
45
46 import java.io.IOException;
47 import java.io.StringReader;
48 import java.io.StringWriter;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Vector;
52
53 public class ImagePreset {
54
55     private static final String LOGTAG = "ImagePreset";
56
57     private Vector<FilterRepresentation> mFilters = new Vector<FilterRepresentation>();
58
59     private boolean mDoApplyGeometry = true;
60     private boolean mDoApplyFilters = true;
61
62     private boolean mPartialRendering = false;
63     private Rect mPartialRenderingBounds;
64     private static final boolean DEBUG = false;
65
66     public ImagePreset() {
67     }
68
69     public ImagePreset(ImagePreset source) {
70         for (int i = 0; i < source.mFilters.size(); i++) {
71             FilterRepresentation sourceRepresentation = source.mFilters.elementAt(i);
72             mFilters.add(sourceRepresentation.copy());
73         }
74     }
75
76     public Vector<FilterRepresentation> getFilters() {
77         return mFilters;
78     }
79
80     public FilterRepresentation getFilterRepresentation(int position) {
81         FilterRepresentation representation = null;
82
83         representation = mFilters.elementAt(position).copy();
84
85         return representation;
86     }
87
88     private static boolean sameSerializationName(String a, String b) {
89         if (a != null && b != null) {
90             return a.equals(b);
91         } else {
92             return a == null && b == null;
93         }
94     }
95
96     public static boolean sameSerializationName(FilterRepresentation a, FilterRepresentation b) {
97         if (a == null || b == null) {
98             return false;
99         }
100         return sameSerializationName(a.getSerializationName(), b.getSerializationName());
101     }
102
103     public int getPositionForRepresentation(FilterRepresentation representation) {
104         for (int i = 0; i < mFilters.size(); i++) {
105             if (sameSerializationName(mFilters.elementAt(i), representation)) {
106                 return i;
107             }
108         }
109         return -1;
110     }
111
112     private FilterRepresentation getFilterRepresentationForType(int type) {
113         for (int i = 0; i < mFilters.size(); i++) {
114             if (mFilters.elementAt(i).getFilterType() == type) {
115                 return mFilters.elementAt(i);
116             }
117         }
118         return null;
119     }
120
121     public int getPositionForType(int type) {
122         for (int i = 0; i < mFilters.size(); i++) {
123             if (mFilters.elementAt(i).getFilterType() == type) {
124                 return i;
125             }
126         }
127         return -1;
128     }
129
130     public FilterRepresentation getFilterRepresentationCopyFrom(
131             FilterRepresentation filterRepresentation) {
132         // TODO: add concept of position in the filters (to allow multiple instances)
133         if (filterRepresentation == null) {
134             return null;
135         }
136         int position = getPositionForRepresentation(filterRepresentation);
137         if (position == -1) {
138             return null;
139         }
140         FilterRepresentation representation = mFilters.elementAt(position);
141         if (representation != null) {
142             representation = representation.copy();
143         }
144         return representation;
145     }
146
147     public void updateFilterRepresentations(Collection<FilterRepresentation> reps) {
148         for (FilterRepresentation r : reps) {
149             updateOrAddFilterRepresentation(r);
150         }
151     }
152
153     public void updateOrAddFilterRepresentation(FilterRepresentation rep) {
154         int pos = getPositionForRepresentation(rep);
155         if (pos != -1) {
156             mFilters.elementAt(pos).useParametersFrom(rep);
157         } else {
158             addFilter(rep.copy());
159         }
160     }
161
162     public void setDoApplyGeometry(boolean value) {
163         mDoApplyGeometry = value;
164     }
165
166     public void setDoApplyFilters(boolean value) {
167         mDoApplyFilters = value;
168     }
169
170     public boolean getDoApplyFilters() {
171         return mDoApplyFilters;
172     }
173
174     public boolean hasModifications() {
175         for (int i = 0; i < mFilters.size(); i++) {
176             FilterRepresentation filter = mFilters.elementAt(i);
177             if (!filter.isNil()) {
178                 return true;
179             }
180         }
181         return false;
182     }
183
184     public boolean contains(byte type) {
185         for (FilterRepresentation representation : mFilters) {
186             if (representation.getFilterType() == type
187                     && !representation.isNil()) {
188                 return true;
189             }
190         }
191         return false;
192     }
193
194     public boolean isPanoramaSafe() {
195         for (FilterRepresentation representation : mFilters) {
196             if (representation.getFilterType() == FilterRepresentation.TYPE_GEOMETRY
197                     && !representation.isNil()) {
198                 return false;
199             }
200             if (representation.getFilterType() == FilterRepresentation.TYPE_BORDER
201                     && !representation.isNil()) {
202                 return false;
203             }
204             if (representation.getFilterType() == FilterRepresentation.TYPE_VIGNETTE
205                     && !representation.isNil()) {
206                 return false;
207             }
208             if (representation.getFilterType() == FilterRepresentation.TYPE_TINYPLANET
209                     && !representation.isNil()) {
210                 return false;
211             }
212         }
213         return true;
214     }
215
216     public boolean same(ImagePreset preset) {
217         if (preset == null) {
218             return false;
219         }
220
221         if (preset.mFilters.size() != mFilters.size()) {
222             return false;
223         }
224
225         if (mDoApplyGeometry != preset.mDoApplyGeometry) {
226             return false;
227         }
228
229         if (mDoApplyFilters != preset.mDoApplyFilters) {
230             if (mFilters.size() > 0 || preset.mFilters.size() > 0) {
231                 return false;
232             }
233         }
234
235         if (mDoApplyFilters && preset.mDoApplyFilters) {
236             for (int i = 0; i < preset.mFilters.size(); i++) {
237                 FilterRepresentation a = preset.mFilters.elementAt(i);
238                 FilterRepresentation b = mFilters.elementAt(i);
239
240                 if (!a.same(b)) {
241                     return false;
242                 }
243             }
244         }
245
246         return true;
247     }
248
249     public boolean equals(ImagePreset preset) {
250         if (preset == null) {
251             return false;
252         }
253
254         if (preset.mFilters.size() != mFilters.size()) {
255             return false;
256         }
257
258         if (mDoApplyGeometry != preset.mDoApplyGeometry) {
259             return false;
260         }
261
262         if (mDoApplyFilters != preset.mDoApplyFilters) {
263             if (mFilters.size() > 0 || preset.mFilters.size() > 0) {
264                 return false;
265             }
266         }
267
268         for (int i = 0; i < preset.mFilters.size(); i++) {
269             FilterRepresentation a = preset.mFilters.elementAt(i);
270             FilterRepresentation b = mFilters.elementAt(i);
271             boolean isGeometry = false;
272             if (a instanceof FilterRotateRepresentation
273                     || a instanceof FilterMirrorRepresentation
274                     || a instanceof FilterCropRepresentation
275                     || a instanceof FilterStraightenRepresentation) {
276                 isGeometry = true;
277             }
278             boolean evaluate = true;
279             if (!isGeometry && mDoApplyGeometry && !mDoApplyFilters) {
280                 evaluate = false;
281             } else if (isGeometry && !mDoApplyGeometry && mDoApplyFilters) {
282                 evaluate = false;
283             }
284             if (evaluate && !a.equals(b)) {
285                 return false;
286             }
287         }
288
289         return true;
290     }
291
292     public int similarUpTo(ImagePreset preset) {
293         for (int i = 0; i < preset.mFilters.size(); i++) {
294             FilterRepresentation a = preset.mFilters.elementAt(i);
295             if (i < mFilters.size()) {
296                 FilterRepresentation b = mFilters.elementAt(i);
297                 if (!a.same(b)) {
298                     return i;
299                 }
300                 if (!a.equals(b)) {
301                     return i;
302                 }
303             } else {
304                 return i;
305             }
306         }
307         return preset.mFilters.size();
308     }
309
310     public void showFilters() {
311         Log.v(LOGTAG, "\\\\\\ showFilters -- " + mFilters.size() + " filters");
312         int n = 0;
313         for (FilterRepresentation representation : mFilters) {
314             Log.v(LOGTAG, " filter " + n + " : " + representation.toString());
315             n++;
316         }
317         Log.v(LOGTAG, "/// showFilters -- " + mFilters.size() + " filters");
318     }
319
320     public FilterRepresentation getLastRepresentation() {
321         if (mFilters.size() > 0) {
322             return mFilters.lastElement();
323         }
324         return null;
325     }
326
327     public void removeFilter(FilterRepresentation filterRepresentation) {
328         if (filterRepresentation.getFilterType() == FilterRepresentation.TYPE_BORDER) {
329             for (int i = 0; i < mFilters.size(); i++) {
330                 if (mFilters.elementAt(i).getFilterType()
331                 == filterRepresentation.getFilterType()) {
332                     mFilters.remove(i);
333                     break;
334                 }
335             }
336         } else {
337             for (int i = 0; i < mFilters.size(); i++) {
338                 if (sameSerializationName(mFilters.elementAt(i), filterRepresentation)) {
339                     mFilters.remove(i);
340                     break;
341                 }
342             }
343         }
344     }
345
346     // If the filter is an "None" effect or border, then just don't add this filter.
347     public void addFilter(FilterRepresentation representation) {
348         if (representation instanceof FilterUserPresetRepresentation) {
349             ImagePreset preset = ((FilterUserPresetRepresentation) representation).getImagePreset();
350             // user preset replace everything but geometry
351             mFilters.clear();
352             for (int i = 0; i < preset.nbFilters(); i++) {
353                 addFilter(preset.getFilterRepresentation(i));
354             }
355             mFilters.add(representation);
356         } else if (representation.getFilterType() == FilterRepresentation.TYPE_GEOMETRY) {
357             // Add geometry filter, removing duplicates and do-nothing operations.
358             for (int i = 0; i < mFilters.size(); i++) {
359                 if (sameSerializationName(representation, mFilters.elementAt(i))) {
360                     mFilters.remove(i);
361                 }
362             }
363             int index = 0;
364             for (; index < mFilters.size(); index++) {
365                 FilterRepresentation rep = mFilters.elementAt(index);
366                 if (rep.getFilterType() != FilterRepresentation.TYPE_GEOMETRY) {
367                     break;
368                 }
369             }
370             if (!representation.isNil()) {
371                 mFilters.insertElementAt(representation, index);
372             }
373         } else if (representation.getFilterType() == FilterRepresentation.TYPE_BORDER) {
374             removeFilter(representation);
375             if (!isNoneBorderFilter(representation)) {
376                 mFilters.add(representation);
377             }
378         } else if (representation.getFilterType() == FilterRepresentation.TYPE_FX) {
379             boolean found = false;
380             for (int i = 0; i < mFilters.size(); i++) {
381                 FilterRepresentation current = mFilters.elementAt(i);
382                 int type = current.getFilterType();
383                 if (found) {
384                     if (type != FilterRepresentation.TYPE_VIGNETTE) {
385                         mFilters.remove(i);
386                         continue;
387                     }
388                 }
389                 if (type == FilterRepresentation.TYPE_FX) {
390                     if (current instanceof FilterUserPresetRepresentation) {
391                         ImagePreset preset = ((FilterUserPresetRepresentation) current)
392                                 .getImagePreset();
393                         // If we had an existing user preset, let's remove all the presets that
394                         // were added by it
395                         for (int j = 0; j < preset.nbFilters(); j++) {
396                             FilterRepresentation rep = preset.getFilterRepresentation(j);
397                             int pos = getPositionForRepresentation(rep);
398                             if (pos != -1) {
399                                 mFilters.remove(pos);
400                             }
401                         }
402                         int pos = getPositionForRepresentation(current);
403                         if (pos != -1) {
404                             mFilters.remove(pos);
405                         } else {
406                             pos = 0;
407                         }
408                         if (!isNoneFxFilter(representation)) {
409                             mFilters.add(pos, representation);
410                         }
411
412                     } else {
413                         mFilters.remove(i);
414                         if (!isNoneFxFilter(representation)) {
415                             mFilters.add(i, representation);
416                         }
417                     }
418                     found = true;
419                 }
420             }
421             if (!found) {
422                 if (!isNoneFxFilter(representation)) {
423                     mFilters.add(representation);
424                 }
425             }
426         } else {
427             mFilters.add(representation);
428         }
429     }
430
431     private boolean isNoneBorderFilter(FilterRepresentation representation) {
432         return representation instanceof FilterImageBorderRepresentation &&
433                 ((FilterImageBorderRepresentation) representation).getDrawableResource() == 0;
434     }
435
436     private boolean isNoneFxFilter(FilterRepresentation representation) {
437         return representation instanceof FilterFxRepresentation &&
438                 ((FilterFxRepresentation) representation).getNameResource() == R.string.none;
439     }
440
441     public FilterRepresentation getRepresentation(FilterRepresentation filterRepresentation) {
442         for (int i = 0; i < mFilters.size(); i++) {
443             FilterRepresentation representation = mFilters.elementAt(i);
444             if (sameSerializationName(representation, filterRepresentation)) {
445                 return representation;
446             }
447         }
448         return null;
449     }
450
451     public Bitmap apply(Bitmap original, FilterEnvironment environment) {
452         Bitmap bitmap = original;
453         bitmap = applyFilters(bitmap, -1, -1, environment);
454         return applyBorder(bitmap, environment);
455     }
456
457     public Collection<FilterRepresentation> getGeometryFilters() {
458         ArrayList<FilterRepresentation> geometry = new ArrayList<FilterRepresentation>();
459         for (FilterRepresentation r : mFilters) {
460             if (r.getFilterType() == FilterRepresentation.TYPE_GEOMETRY) {
461                 geometry.add(r);
462             }
463         }
464         return geometry;
465     }
466
467     public FilterRepresentation getFilterWithSerializationName(String serializationName) {
468         for (FilterRepresentation r : mFilters) {
469             if (r != null) {
470                 if (sameSerializationName(r.getSerializationName(), serializationName)) {
471                     return r.copy();
472                 }
473             }
474         }
475         return null;
476     }
477
478     public Rect finalGeometryRect(int width, int height) {
479         return GeometryMathUtils.finalGeometryRect(width, height, getGeometryFilters());
480     }
481
482     public Bitmap applyGeometry(Bitmap bitmap, FilterEnvironment environment) {
483         // Apply any transform -- 90 rotate, flip, straighten, crop
484         // Returns a new bitmap.
485         if (mDoApplyGeometry) {
486             Bitmap bmp = GeometryMathUtils.applyGeometryRepresentations(
487                     getGeometryFilters(), bitmap);
488             if (bmp != bitmap) {
489                 environment.cache(bitmap);
490             }
491             return bmp;
492         }
493         return bitmap;
494     }
495
496     public Bitmap applyBorder(Bitmap bitmap, FilterEnvironment environment) {
497         // get the border from the list of filters.
498         FilterRepresentation border = getFilterRepresentationForType(
499                 FilterRepresentation.TYPE_BORDER);
500         if (border != null && mDoApplyGeometry) {
501             bitmap = environment.applyRepresentation(border, bitmap);
502             if (environment.getQuality() == FilterEnvironment.QUALITY_FINAL) {
503                 UsageStatistics.onEvent(UsageStatistics.COMPONENT_EDITOR,
504                         "SaveBorder", border.getSerializationName(), 1);
505             }
506         }
507         return bitmap;
508     }
509
510     public int nbFilters() {
511         return mFilters.size();
512     }
513
514     public Bitmap applyFilters(Bitmap bitmap, int from, int to, FilterEnvironment environment) {
515         if (mDoApplyFilters) {
516             if (from < 0) {
517                 from = 0;
518             }
519             if (to == -1) {
520                 to = mFilters.size();
521             }
522             if (environment.getQuality() == FilterEnvironment.QUALITY_FINAL) {
523                 UsageStatistics.onEvent(UsageStatistics.COMPONENT_EDITOR,
524                         "SaveFilters", "Total", to - from + 1);
525             }
526             for (int i = from; i < to; i++) {
527                 FilterRepresentation representation = mFilters.elementAt(i);
528                 if (representation.getFilterType() == FilterRepresentation.TYPE_GEOMETRY) {
529                     // skip the geometry as it's already applied.
530                     continue;
531                 }
532                 if (representation.getFilterType() == FilterRepresentation.TYPE_BORDER) {
533                     // for now, let's skip the border as it will be applied in
534                     // applyBorder()
535                     // TODO: might be worth getting rid of applyBorder.
536                     continue;
537                 }
538                 Bitmap tmp = bitmap;
539                 bitmap = environment.applyRepresentation(representation, bitmap);
540                 if (tmp != bitmap) {
541                     environment.cache(tmp);
542                 }
543                 if (environment.getQuality() == FilterEnvironment.QUALITY_FINAL) {
544                     UsageStatistics.onEvent(UsageStatistics.COMPONENT_EDITOR,
545                             "SaveFilter", representation.getSerializationName(), 1);
546                 }
547                 if (environment.needsStop()) {
548                     return bitmap;
549                 }
550             }
551         }
552
553         return bitmap;
554     }
555
556     public void applyBorder(Allocation in, Allocation out,
557             boolean copyOut, FilterEnvironment environment) {
558         FilterRepresentation border = getFilterRepresentationForType(
559                 FilterRepresentation.TYPE_BORDER);
560         if (border != null && mDoApplyGeometry) {
561             // TODO: should keep the bitmap around
562             Allocation bitmapIn = in;
563             if (copyOut) {
564                 bitmapIn = Allocation.createTyped(
565                         CachingPipeline.getRenderScriptContext(), in.getType());
566                 bitmapIn.copyFrom(out);
567             }
568             environment.applyRepresentation(border, bitmapIn, out);
569         }
570     }
571
572     public void applyFilters(int from, int to, Allocation in, Allocation out,
573             FilterEnvironment environment) {
574         if (mDoApplyFilters) {
575             if (from < 0) {
576                 from = 0;
577             }
578             if (to == -1) {
579                 to = mFilters.size();
580             }
581             for (int i = from; i < to; i++) {
582                 FilterRepresentation representation = mFilters.elementAt(i);
583                 if (representation.getFilterType() == FilterRepresentation.TYPE_GEOMETRY
584                         || representation.getFilterType() == FilterRepresentation.TYPE_BORDER) {
585                     continue;
586                 }
587                 if (i > from) {
588                     in.copyFrom(out);
589                 }
590                 environment.applyRepresentation(representation, in, out);
591             }
592         }
593     }
594
595     public boolean canDoPartialRendering() {
596         if (MasterImage.getImage().getZoomOrientation() != ImageLoader.ORI_NORMAL) {
597             return false;
598         }
599         for (int i = 0; i < mFilters.size(); i++) {
600             FilterRepresentation representation = mFilters.elementAt(i);
601             if (!representation.supportsPartialRendering()) {
602                 return false;
603             }
604         }
605         return true;
606     }
607
608     public void fillImageStateAdapter(StateAdapter imageStateAdapter) {
609         if (imageStateAdapter == null) {
610             return;
611         }
612         Vector<State> states = new Vector<State>();
613         for (FilterRepresentation filter : mFilters) {
614             if (filter instanceof FilterUserPresetRepresentation) {
615                 // do not show the user preset itself in the state panel
616                 continue;
617             }
618             State state = new State(filter.getName());
619             state.setFilterRepresentation(filter);
620             states.add(state);
621         }
622         imageStateAdapter.fill(states);
623     }
624
625     public void setPartialRendering(boolean partialRendering, Rect bounds) {
626         mPartialRendering = partialRendering;
627         mPartialRenderingBounds = bounds;
628     }
629
630     public boolean isPartialRendering() {
631         return mPartialRendering;
632     }
633
634     public Rect getPartialRenderingBounds() {
635         return mPartialRenderingBounds;
636     }
637
638     public Vector<ImageFilter> getUsedFilters(BaseFiltersManager filtersManager) {
639         Vector<ImageFilter> usedFilters = new Vector<ImageFilter>();
640         for (int i = 0; i < mFilters.size(); i++) {
641             FilterRepresentation representation = mFilters.elementAt(i);
642             ImageFilter filter = filtersManager.getFilterForRepresentation(representation);
643             usedFilters.add(filter);
644         }
645         return usedFilters;
646     }
647
648     public String getJsonString(String name) {
649         StringWriter swriter = new StringWriter();
650         try {
651             JsonWriter writer = new JsonWriter(swriter);
652             writeJson(writer, name);
653             writer.close();
654         } catch (IOException e) {
655             return null;
656         }
657         return swriter.toString();
658     }
659
660     public void writeJson(JsonWriter writer, String name) {
661         int numFilters = mFilters.size();
662         try {
663             writer.beginObject();
664             for (int i = 0; i < numFilters; i++) {
665                 FilterRepresentation filter = mFilters.get(i);
666                 if (filter instanceof FilterUserPresetRepresentation) {
667                     continue;
668                 }
669                 String sname = filter.getSerializationName();
670                 if (DEBUG) {
671                     Log.v(LOGTAG, "Serialization: " + sname);
672                     if (sname == null) {
673                         Log.v(LOGTAG, "Serialization name null for filter: " + filter);
674                     }
675                 }
676                 writer.name(sname);
677                 filter.serializeRepresentation(writer);
678             }
679             writer.endObject();
680
681         } catch (IOException e) {
682            Log.e(LOGTAG,"Error encoding JASON",e);
683         }
684     }
685
686     /**
687      * populates preset from JSON string
688      *
689      * @param filterString a JSON string
690      * @return true on success if false ImagePreset is undefined
691      */
692     public boolean readJsonFromString(String filterString) {
693         if (DEBUG) {
694             Log.v(LOGTAG, "reading preset: \"" + filterString + "\"");
695         }
696         StringReader sreader = new StringReader(filterString);
697         try {
698             JsonReader reader = new JsonReader(sreader);
699             boolean ok = readJson(reader);
700             if (!ok) {
701                 reader.close();
702                 return false;
703             }
704             reader.close();
705         } catch (Exception e) {
706             Log.e(LOGTAG, "parsing the filter parameters:", e);
707             return false;
708         }
709         return true;
710     }
711
712     /**
713      * populates preset from JSON stream
714      *
715      * @param sreader a JSON string
716      * @return true on success if false ImagePreset is undefined
717      */
718     public boolean readJson(JsonReader sreader) throws IOException {
719         sreader.beginObject();
720
721         while (sreader.hasNext()) {
722             String name = sreader.nextName();
723             FilterRepresentation filter = creatFilterFromName(name);
724             if (filter == null) {
725                 Log.w(LOGTAG, "UNKNOWN FILTER! " + name);
726                 return false;
727             }
728             filter.deSerializeRepresentation(sreader);
729             addFilter(filter);
730         }
731         sreader.endObject();
732         return true;
733     }
734
735     FilterRepresentation creatFilterFromName(String name) {
736         if (FilterRotateRepresentation.SERIALIZATION_NAME.equals(name)) {
737             return new FilterRotateRepresentation();
738         } else if (FilterMirrorRepresentation.SERIALIZATION_NAME.equals(name)) {
739             return new FilterMirrorRepresentation();
740         } else if (FilterStraightenRepresentation.SERIALIZATION_NAME.equals(name)) {
741             return new FilterStraightenRepresentation();
742         } else if (FilterCropRepresentation.SERIALIZATION_NAME.equals(name)) {
743             return new FilterCropRepresentation();
744         }
745         FiltersManager filtersManager = FiltersManager.getManager();
746         return filtersManager.createFilterFromName(name);
747     }
748
749     public void updateWith(ImagePreset preset) {
750         if (preset.mFilters.size() != mFilters.size()) {
751             Log.e(LOGTAG, "Updating a preset with an incompatible one");
752             return;
753         }
754         for (int i = 0; i < mFilters.size(); i++) {
755             FilterRepresentation destRepresentation = mFilters.elementAt(i);
756             FilterRepresentation sourceRepresentation = preset.mFilters.elementAt(i);
757             destRepresentation.useParametersFrom(sourceRepresentation);
758         }
759     }
760 }