diff options
author | Ruben Brunk <rubenbrunk@google.com> | 2013-07-15 18:32:50 -0700 |
---|---|---|
committer | Ruben Brunk <rubenbrunk@google.com> | 2013-07-17 14:13:05 -0700 |
commit | c0d712e074fc38463464516befb7ea145654d21b (patch) | |
tree | 39dd0e858e40703c1984934baf4a13b79bebbef8 /src/com/android/gallery3d/filtershow/filters | |
parent | 4264520a6a231d42f65168b07e7e793cda7b1a8d (diff) | |
download | android_packages_apps_Snap-c0d712e074fc38463464516befb7ea145654d21b.tar.gz android_packages_apps_Snap-c0d712e074fc38463464516befb7ea145654d21b.tar.bz2 android_packages_apps_Snap-c0d712e074fc38463464516befb7ea145654d21b.zip |
Refactoring GeometryMetadata.
Bug: 9170644
- First part of multi-step refactor, splits
GeometryMetadata functionality into 4
FilterRepresentation subclasses as requested.
Change-Id: I212193db8c154f64c550adb9735c0502bcaa7468
Diffstat (limited to 'src/com/android/gallery3d/filtershow/filters')
6 files changed, 620 insertions, 25 deletions
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterCropRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterCropRepresentation.java new file mode 100644 index 000000000..345c2f1bf --- /dev/null +++ b/src/com/android/gallery3d/filtershow/filters/FilterCropRepresentation.java @@ -0,0 +1,167 @@ +/* + * 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.RectF; +import android.util.JsonReader; +import android.util.JsonWriter; + +import com.android.gallery3d.R; +import com.android.gallery3d.filtershow.editors.EditorCrop; + +import java.io.IOException; + +public class FilterCropRepresentation extends FilterRepresentation { + public static final String SERIALIZATION_NAME = "CROP"; + public static final String[] BOUNDS = { + "C0", "C1", "C2", "C3", "I0", "I1", "I2", "I3" + }; + private static final String TAG = FilterCropRepresentation.class.getSimpleName(); + + RectF mCrop = new RectF(); + RectF mImage = new RectF(); + + public FilterCropRepresentation(RectF crop, RectF image) { + super(FilterCropRepresentation.class.getSimpleName()); + setSerializationName(SERIALIZATION_NAME); + setShowParameterValue(true); + setFilterClass(FilterCropRepresentation.class); + setFilterType(FilterRepresentation.TYPE_GEOMETRY); + setTextId(R.string.crop); + setEditorId(EditorCrop.ID); + setCrop(crop); + setImage(image); + } + + public FilterCropRepresentation(FilterCropRepresentation m) { + this(m.getCrop(), m.getImage()); + } + + public FilterCropRepresentation() { + this(new RectF(), new RectF()); + } + + public void set(FilterCropRepresentation r) { + mCrop.set(r.mCrop); + mImage.set(r.mImage); + } + + public RectF getCrop() { + return new RectF(mCrop); + } + + public void getCrop(RectF r) { + r.set(mCrop); + } + + public void setCrop(RectF crop) { + if (crop == null) { + throw new IllegalArgumentException("Argument to setCrop is null"); + } + mCrop.set(crop); + } + + public RectF getImage() { + return new RectF(mImage); + } + + public void getImage(RectF r) { + r.set(mImage); + } + + public void setImage(RectF image) { + if (image == null) { + throw new IllegalArgumentException("Argument to setImage is null"); + } + mImage.set(image); + } + + @Override + public boolean allowsSingleInstanceOnly() { + return true; + } + + @Override + public FilterRepresentation copy(){ + return new FilterCropRepresentation(this); + } + + @Override + protected void copyAllParameters(FilterRepresentation representation) { + if (!(representation instanceof FilterCropRepresentation)) { + throw new IllegalArgumentException("calling copyAllParameters with incompatible types!"); + } + super.copyAllParameters(representation); + representation.useParametersFrom(this); + } + + @Override + public void useParametersFrom(FilterRepresentation a) { + if (!(a instanceof FilterCropRepresentation)) { + throw new IllegalArgumentException("calling useParametersFrom with incompatible types!"); + } + setCrop(((FilterCropRepresentation) a).mCrop); + setImage(((FilterCropRepresentation) a).mImage); + } + + @Override + public boolean isNil() { + return mCrop.equals(mImage); + } + + @Override + public void serializeRepresentation(JsonWriter writer) throws IOException { + writer.beginObject(); + writer.name(BOUNDS[0]).value(mCrop.left); + writer.name(BOUNDS[1]).value(mCrop.top); + writer.name(BOUNDS[2]).value(mCrop.right); + writer.name(BOUNDS[3]).value(mCrop.bottom); + writer.name(BOUNDS[4]).value(mImage.left); + writer.name(BOUNDS[5]).value(mImage.top); + writer.name(BOUNDS[6]).value(mImage.right); + writer.name(BOUNDS[7]).value(mImage.bottom); + writer.endObject(); + } + + @Override + public void deSerializeRepresentation(JsonReader reader) throws IOException { + reader.beginObject(); + while (reader.hasNext()) { + String name = reader.nextName(); + if (BOUNDS[0].equals(name)) { + mCrop.left = (float) reader.nextDouble(); + } else if (BOUNDS[1].equals(name)) { + mCrop.top = (float) reader.nextDouble(); + } else if (BOUNDS[2].equals(name)) { + mCrop.right = (float) reader.nextDouble(); + } else if (BOUNDS[3].equals(name)) { + mCrop.bottom = (float) reader.nextDouble(); + } else if (BOUNDS[4].equals(name)) { + mImage.left = (float) reader.nextDouble(); + } else if (BOUNDS[5].equals(name)) { + mImage.top = (float) reader.nextDouble(); + } else if (BOUNDS[6].equals(name)) { + mImage.right = (float) reader.nextDouble(); + } else if (BOUNDS[7].equals(name)) { + mImage.bottom = (float) reader.nextDouble(); + } else { + reader.skipValue(); + } + } + reader.endObject(); + } +} diff --git a/src/com/android/gallery3d/filtershow/filters/FilterMirrorRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterMirrorRepresentation.java new file mode 100644 index 000000000..0acf70e71 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/filters/FilterMirrorRepresentation.java @@ -0,0 +1,157 @@ +/* + * 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.util.JsonReader; +import android.util.JsonWriter; +import android.util.Log; + +import com.android.gallery3d.R; +import com.android.gallery3d.filtershow.editors.EditorFlip; + +import java.io.IOException; + +public class FilterMirrorRepresentation extends FilterRepresentation { + public static final String SERIALIZATION_NAME = "MIRROR"; + private static final String SERIALIZATION_MIRROR_VALUE = "value"; + private static final String TAG = FilterMirrorRepresentation.class.getSimpleName(); + + Mirror mMirror = Mirror.NONE; + + public enum Mirror { + NONE('N'), VERTICAL('V'), HORIZONTAL('H'), BOTH('B'); + char mValue; + + private Mirror(char value) { + mValue = value; + } + + public char value() { + return mValue; + } + + public static Mirror fromValue(char value) { + switch (value) { + case 'N': + return NONE; + case 'V': + return VERTICAL; + case 'H': + return HORIZONTAL; + case 'B': + return BOTH; + default: + return null; + } + } + } + + public FilterMirrorRepresentation(Mirror mirror) { + super(FilterMirrorRepresentation.class.getSimpleName()); + setSerializationName(SERIALIZATION_NAME); + setShowParameterValue(true); + setFilterClass(FilterMirrorRepresentation.class); + setFilterType(FilterRepresentation.TYPE_GEOMETRY); + setTextId(R.string.mirror); + setEditorId(EditorFlip.ID); + setMirror(mirror); + } + + public FilterMirrorRepresentation(FilterMirrorRepresentation m) { + this(m.getMirror()); + } + + public FilterMirrorRepresentation() { + this(Mirror.NONE); + } + + public Mirror getMirror() { + return mMirror; + } + + public void set(FilterMirrorRepresentation r) { + mMirror = r.mMirror; + } + + public void setMirror(Mirror mirror) { + if (mirror == null) { + throw new IllegalArgumentException("Argument to setMirror is null"); + } + mMirror = mirror; + } + + @Override + public boolean allowsSingleInstanceOnly() { + return true; + } + + @Override + public FilterRepresentation copy() { + return new FilterMirrorRepresentation(this); + } + + @Override + protected void copyAllParameters(FilterRepresentation representation) { + if (!(representation instanceof FilterMirrorRepresentation)) { + throw new IllegalArgumentException("calling copyAllParameters with incompatible types!"); + } + super.copyAllParameters(representation); + representation.useParametersFrom(this); + } + + @Override + public void useParametersFrom(FilterRepresentation a) { + if (!(a instanceof FilterMirrorRepresentation)) { + throw new IllegalArgumentException("calling useParametersFrom with incompatible types!"); + } + setMirror(((FilterMirrorRepresentation) a).getMirror()); + } + + @Override + public boolean isNil() { + return mMirror == Mirror.NONE; + } + + @Override + public void serializeRepresentation(JsonWriter writer) throws IOException { + writer.beginObject(); + writer.name(SERIALIZATION_MIRROR_VALUE).value(mMirror.value()); + writer.endObject(); + } + + @Override + public void deSerializeRepresentation(JsonReader reader) throws IOException { + boolean unset = true; + reader.beginObject(); + while (reader.hasNext()) { + String name = reader.nextName(); + if (SERIALIZATION_MIRROR_VALUE.equals(name)) { + Mirror r = Mirror.fromValue((char) reader.nextInt()); + if (r != null) { + setMirror(r); + unset = false; + } + } else { + reader.skipValue(); + } + } + if (unset) { + Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME); + } + reader.endObject(); + } +} diff --git a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java index ca9587c28..5b33ffba5 100644 --- a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java +++ b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java @@ -45,6 +45,7 @@ public class FilterRepresentation { public static final byte TYPE_VIGNETTE = 4; public static final byte TYPE_NORMAL = 5; public static final byte TYPE_TINYPLANET = 6; + public static final byte TYPE_GEOMETRY = 7; protected static final String NAME_TAG = "Name"; public FilterRepresentation(String name) { diff --git a/src/com/android/gallery3d/filtershow/filters/FilterRotateRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterRotateRepresentation.java new file mode 100644 index 000000000..8f7d8ebef --- /dev/null +++ b/src/com/android/gallery3d/filtershow/filters/FilterRotateRepresentation.java @@ -0,0 +1,157 @@ +/* + * 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.util.JsonReader; +import android.util.JsonWriter; +import android.util.Log; + +import com.android.gallery3d.R; +import com.android.gallery3d.filtershow.editors.EditorRotate; + +import java.io.IOException; + +public class FilterRotateRepresentation extends FilterRepresentation { + public static final String SERIALIZATION_NAME = "ROTATION"; + public static final String SERIALIZATION_ROTATE_VALUE = "value"; + private static final String TAG = FilterRotateRepresentation.class.getSimpleName(); + + Rotation mRotation = Rotation.ZERO; + + public enum Rotation { + ZERO(0), NINETY(90), ONE_EIGHTY(180), TWO_SEVENTY(270); + private final int mValue; + + private Rotation(int value) { + mValue = value; + } + + public int value() { + return mValue; + } + + public static Rotation fromValue(int value) { + switch (value) { + case 0: + return ZERO; + case 90: + return NINETY; + case 180: + return ONE_EIGHTY; + case 270: + return TWO_SEVENTY; + default: + return null; + } + } + } + + public FilterRotateRepresentation(Rotation rotation) { + super(FilterRotateRepresentation.class.getSimpleName()); + setSerializationName(SERIALIZATION_NAME); + setShowParameterValue(true); + setFilterClass(FilterRotateRepresentation.class); + setFilterType(FilterRepresentation.TYPE_GEOMETRY); + setTextId(R.string.rotate); + setEditorId(EditorRotate.ID); + setRotation(rotation); + } + + public FilterRotateRepresentation(FilterRotateRepresentation r) { + this(r.getRotation()); + } + + public FilterRotateRepresentation() { + this(Rotation.ZERO); + } + + public Rotation getRotation() { + return mRotation; + } + + public void set(FilterRotateRepresentation r) { + mRotation = r.mRotation; + } + + public void setRotation(Rotation rotation) { + if (rotation == null) { + throw new IllegalArgumentException("Argument to setRotation is null"); + } + mRotation = rotation; + } + + @Override + public boolean allowsSingleInstanceOnly() { + return true; + } + + @Override + public FilterRepresentation copy() { + return new FilterRotateRepresentation(this); + } + + @Override + protected void copyAllParameters(FilterRepresentation representation) { + if (!(representation instanceof FilterRotateRepresentation)) { + throw new IllegalArgumentException("calling copyAllParameters with incompatible types!"); + } + super.copyAllParameters(representation); + representation.useParametersFrom(this); + } + + @Override + public void useParametersFrom(FilterRepresentation a) { + if (!(a instanceof FilterRotateRepresentation)) { + throw new IllegalArgumentException("calling useParametersFrom with incompatible types!"); + } + setRotation(((FilterRotateRepresentation) a).getRotation()); + } + + @Override + public boolean isNil() { + return mRotation == Rotation.ZERO; + } + + @Override + public void serializeRepresentation(JsonWriter writer) throws IOException { + writer.beginObject(); + writer.name(SERIALIZATION_ROTATE_VALUE).value(mRotation.value()); + writer.endObject(); + } + + @Override + public void deSerializeRepresentation(JsonReader reader) throws IOException { + boolean unset = true; + reader.beginObject(); + while (reader.hasNext()) { + String name = reader.nextName(); + if (SERIALIZATION_ROTATE_VALUE.equals(name)) { + Rotation r = Rotation.fromValue(reader.nextInt()); + if (r != null) { + setRotation(r); + unset = false; + } + } else { + reader.skipValue(); + } + } + if (unset) { + Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME); + } + reader.endObject(); + } +} diff --git a/src/com/android/gallery3d/filtershow/filters/FilterStraightenRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterStraightenRepresentation.java new file mode 100644 index 000000000..a06216ef3 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/filters/FilterStraightenRepresentation.java @@ -0,0 +1,136 @@ +/* + * 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.util.JsonReader; +import android.util.JsonWriter; +import android.util.Log; + +import com.android.gallery3d.R; +import com.android.gallery3d.filtershow.editors.EditorStraighten; + +import java.io.IOException; + +public class FilterStraightenRepresentation extends FilterRepresentation { + public static final String SERIALIZATION_NAME = "STRAIGHTEN"; + public static final String SERIALIZATION_STRAIGHTEN_VALUE = "value"; + private static final String TAG = FilterStraightenRepresentation.class.getSimpleName(); + + float mStraighten; + + public FilterStraightenRepresentation(float straighten) { + super(FilterStraightenRepresentation.class.getSimpleName()); + setSerializationName(SERIALIZATION_NAME); + setShowParameterValue(true); + setFilterClass(FilterStraightenRepresentation.class); + setFilterType(FilterRepresentation.TYPE_GEOMETRY); + setTextId(R.string.straighten); + setEditorId(EditorStraighten.ID); + setStraighten(straighten); + } + + public FilterStraightenRepresentation(FilterStraightenRepresentation s) { + this(s.getStraighten()); + } + + public FilterStraightenRepresentation() { + this(0); + } + + public void set(FilterStraightenRepresentation r) { + mStraighten = r.mStraighten; + } + + public float getStraighten() { + return mStraighten; + } + + public void setStraighten(float straighten) { + if (!rangeCheck(straighten)) { + straighten = Math.min(Math.max(straighten, -45), 45); + } + mStraighten = straighten; + } + + @Override + public boolean allowsSingleInstanceOnly() { + return true; + } + + @Override + public FilterRepresentation copy() { + return new FilterStraightenRepresentation(this); + } + + @Override + protected void copyAllParameters(FilterRepresentation representation) { + if (!(representation instanceof FilterStraightenRepresentation)) { + throw new IllegalArgumentException("calling copyAllParameters with incompatible types!"); + } + super.copyAllParameters(representation); + representation.useParametersFrom(this); + } + + @Override + public void useParametersFrom(FilterRepresentation a) { + if (!(a instanceof FilterStraightenRepresentation)) { + throw new IllegalArgumentException("calling useParametersFrom with incompatible types!"); + } + setStraighten(((FilterStraightenRepresentation) a).getStraighten()); + } + + @Override + public boolean isNil() { + return mStraighten == 0; + } + + @Override + public void serializeRepresentation(JsonWriter writer) throws IOException { + writer.beginObject(); + writer.name(SERIALIZATION_STRAIGHTEN_VALUE).value(mStraighten); + writer.endObject(); + } + + @Override + public void deSerializeRepresentation(JsonReader reader) throws IOException { + boolean unset = true; + reader.beginObject(); + while (reader.hasNext()) { + String name = reader.nextName(); + if (SERIALIZATION_STRAIGHTEN_VALUE.equals(name)) { + int s = reader.nextInt(); + if (rangeCheck(s)) { + setStraighten(s); + unset = false; + } + } else { + reader.skipValue(); + } + } + if (unset) { + Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME); + } + reader.endObject(); + } + + private boolean rangeCheck(float s) { + if (s < -45 || s > 45) { + return false; + } + return true; + } +} diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterGeometry.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterGeometry.java index 4f46eed82..3c323e182 100644 --- a/src/com/android/gallery3d/filtershow/filters/ImageFilterGeometry.java +++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterGeometry.java @@ -79,17 +79,6 @@ public class ImageFilterGeometry extends ImageFilter { Log.w(LOGTAG, "Cannot apply geometry: geometry metadata has not been initialized"); return bitmap; } - CropExtras extras = mGeometry.getCropExtras(); - boolean useExtras = mGeometry.getUseCropExtrasFlag(); - int outputX = 0; - int outputY = 0; - boolean s = false; - if (extras != null && useExtras){ - outputX = extras.getOutputX(); - outputY = extras.getOutputY(); - s = extras.getScaleUp(); - } - Rect cropBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); RectF crop = mGeometry.getCropBounds(bitmap); @@ -105,20 +94,8 @@ public class ImageFilterGeometry extends ImageFilter { height = temp; } - if(outputX <= 0 || outputY <= 0){ - outputX = width; - outputY = height; - } - - float scaleX = 1; - float scaleY = 1; - if (s){ - scaleX = (float) outputX / width; - scaleY = (float) outputY / height; - } - Bitmap temp = null; - temp = Bitmap.createBitmap(outputX, outputY, mConfig); + temp = Bitmap.createBitmap(width, height, mConfig); float[] displayCenter = { temp.getWidth() / 2f, temp.getHeight() / 2f @@ -126,7 +103,7 @@ public class ImageFilterGeometry extends ImageFilter { Matrix m1 = mGeometry.buildTotalXform(bitmap.getWidth(), bitmap.getHeight(), displayCenter); - m1.postScale(scaleX, scaleY, displayCenter[0], displayCenter[1]); + m1.postScale(1, 1, displayCenter[0], displayCenter[1]); Canvas canvas = new Canvas(temp); Paint paint = new Paint(); |