diff options
author | Jorge Ruesga <jorge@ruesga.com> | 2013-08-13 17:11:48 +0200 |
---|---|---|
committer | Jorge Ruesga <jorge@ruesga.com> | 2013-08-13 17:11:48 +0200 |
commit | 9ec2c4522d62b26e3034314e361f255fe044016c (patch) | |
tree | fc74d411ea9b35119b153dd59cd28a04520eab96 /src/org | |
parent | 675f7c51443e11d6ad89662d9f8124473cc0c148 (diff) | |
download | android_packages_wallpapers_PhotoPhase-9ec2c4522d62b26e3034314e361f255fe044016c.tar.gz android_packages_wallpapers_PhotoPhase-9ec2c4522d62b26e3034314e361f255fe044016c.tar.bz2 android_packages_wallpapers_PhotoPhase-9ec2c4522d62b26e3034314e361f255fe044016c.zip |
Aspect ratio correction (implementation) (#14)
Signed-off-by: Jorge Ruesga <jorge@ruesga.com>
Diffstat (limited to 'src/org')
7 files changed, 96 insertions, 92 deletions
diff --git a/src/org/cyanogenmod/wallpapers/photophase/PhotoFrame.java b/src/org/cyanogenmod/wallpapers/photophase/PhotoFrame.java index 29af55f..e1ddea7 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/PhotoFrame.java +++ b/src/org/cyanogenmod/wallpapers/photophase/PhotoFrame.java @@ -17,6 +17,7 @@ package org.cyanogenmod.wallpapers.photophase; import android.content.Context; +import android.graphics.RectF; import android.opengl.GLES20; import org.cyanogenmod.wallpapers.photophase.utils.GLESUtil; @@ -87,7 +88,7 @@ public class PhotoFrame implements TextureRequestor { mFrameHeight = frameVertex[1] - frameVertex[5]; mPhotoVertex = photoVertex; mPhotoWidth = photoVertex[6] - photoVertex[4]; - mPhotoHeight = photoVertex[1] - photoVertex[5]; + mPhotoHeight = photoVertex[5] - photoVertex[1]; // Initialize vertex byte buffer for shape coordinates ByteBuffer bb = ByteBuffer.allocateDirect(photoVertex.length * 4); // (# of coordinate values * 4 bytes per float) @@ -107,6 +108,14 @@ public class PhotoFrame implements TextureRequestor { * {@inheritDoc} */ @Override + public RectF getRequestorDimensions() { + return new RectF(0, 0, mPhotoWidth, mPhotoHeight); + } + + /** + * {@inheritDoc} + */ + @Override public void setTextureHandle(GLESTextureInfo ti) { // If the picture is invalid request a new texture if (ti == null || ti.handle <= 0) { @@ -114,8 +123,6 @@ public class PhotoFrame implements TextureRequestor { return; } - // TODO Apply the texture correction - // Full frame picture setTextureHandle(ti, DEFAULT_TEXTURE_COORDS); mLoaded = true; diff --git a/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java b/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java index b8e881c..d15632b 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java +++ b/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java @@ -17,13 +17,18 @@ package org.cyanogenmod.wallpapers.photophase; import android.content.Context; +import android.graphics.Bitmap; import android.graphics.Rect; +import android.graphics.RectF; +import android.media.ThumbnailUtils; +import android.media.effect.Effect; import android.media.effect.EffectContext; import android.opengl.GLES20; import android.util.Log; import android.widget.Toast; import org.cyanogenmod.wallpapers.photophase.FixedQueue.EmptyQueueException; +import org.cyanogenmod.wallpapers.photophase.preferences.PreferencesProvider.Preferences; import org.cyanogenmod.wallpapers.photophase.utils.GLESUtil; import org.cyanogenmod.wallpapers.photophase.utils.GLESUtil.GLESTextureInfo; import org.cyanogenmod.wallpapers.photophase.MediaPictureDiscoverer.OnMediaPictureDiscoveredListener; @@ -79,24 +84,30 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { public void run() { try { // If we have bitmap to reused then pick up from the recycled list + Effect effect = Effects.getNextEffect(mEffectContext); if (sRecycledBitmaps.size() > 0) { // Bind to the GLES context GLESTextureInfo oldTextureInfo = sRecycledBitmaps.remove(0); - ti = GLESUtil.loadTexture(oldTextureInfo.bitmap, - Effects.getNextEffect(mEffectContext), mScreenDimensions); + ti = GLESUtil.loadTexture(oldTextureInfo.bitmap, effect, mScreenDimensions); ti.path = oldTextureInfo.path; oldTextureInfo.bitmap = null; + if (effect != null) { + effect.release(); + } } else { - // Load and bind to the GLES context - ti = GLESUtil.loadTexture(mImage, mDimensions, - Effects.getNextEffect(mEffectContext), mScreenDimensions, false); + // Load and bind to the GLES context. The effect is applied when the image + // is associated to the destination target + ti = GLESUtil.loadTexture(mImage, mDimensions, null, null, false); + ti.effect = effect; } synchronized (mSync) { // Notify the new images to all pending frames if (mPendingRequests.size() > 0) { // Invalid textures are also reported, so requestor can handle it - mPendingRequests.remove(0).setTextureHandle(ti); + TextureRequestor requestor = mPendingRequests.remove(0); + fixAspectRatio(requestor, ti); + requestor.setTextureHandle(ti); } else { // Add to the queue (only valid textures) if (ti.handle > 0) { @@ -216,7 +227,9 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { public void request(TextureRequestor requestor) { synchronized (mSync) { try { - requestor.setTextureHandle(mQueue.remove()); + GLESTextureInfo ti = mQueue.remove(); + fixAspectRatio(requestor, ti); + requestor.setTextureHandle(ti); } catch (EmptyQueueException eqex) { // Add to queue of pending request to be notified when // we have a new bitmap in the queue @@ -386,6 +399,48 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { } /** + * Methdo that fix the aspect ratio of a image to fit the destination target + * + * @param request The requestor target + * @param ti The original texture information + * @param effect The effect to apply to the destination picture + */ + /*package*/ void fixAspectRatio(TextureRequestor requestor, GLESTextureInfo ti) { + // Check if we have to apply any correction to the image + if (Preferences.General.isFixAspectRatio()) { + // Transform requestor dimensions to screen dimensions + RectF dimens = requestor.getRequestorDimensions(); + Rect pixels = new Rect( + 0, + 0, + (int)(mScreenDimensions.width() * dimens.width() / 2), + (int)(mScreenDimensions.height() * dimens.height() / 2)); + + // Create a thumbnail of the image + Bitmap thumb = ThumbnailUtils.extractThumbnail( + ti.bitmap, + (int)pixels.width(), + (int)pixels.height(), + ThumbnailUtils.OPTIONS_RECYCLE_INPUT); + GLESTextureInfo dst = GLESUtil.loadTexture( + thumb, ti == null ? null : ti.effect, mScreenDimensions); + + // Destroy references + int[] textures = new int[]{ti.handle}; + GLES20.glDeleteTextures(1, textures, 0); + GLESUtil.glesCheckError("glDeleteTextures"); + if (ti != null && ti.effect != null) { + ti.effect.release(); + } + + // Swap references + ti.bitmap = dst.bitmap; + ti.handle = dst.handle; + ti.effect = null; + } + } + + /** * An internal thread to load pictures in background */ private class BackgroundPictureLoaderThread extends Thread { diff --git a/src/org/cyanogenmod/wallpapers/photophase/TextureRequestor.java b/src/org/cyanogenmod/wallpapers/photophase/TextureRequestor.java index a60d10d..9c62eb2 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/TextureRequestor.java +++ b/src/org/cyanogenmod/wallpapers/photophase/TextureRequestor.java @@ -16,6 +16,8 @@ package org.cyanogenmod.wallpapers.photophase; +import android.graphics.RectF; + import org.cyanogenmod.wallpapers.photophase.utils.GLESUtil.GLESTextureInfo; /** @@ -29,4 +31,11 @@ public interface TextureRequestor { * @param ti The texture information */ void setTextureHandle(GLESTextureInfo ti); + + /** + * Method that returns the dimension of the requestor + * + * @return RectF The dimensions of the requestor + */ + RectF getRequestorDimensions(); } diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/AspectRatioCorrection.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/AspectRatioCorrection.java deleted file mode 100644 index e3732f8..0000000 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/AspectRatioCorrection.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2013 The CyanogenMod 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 org.cyanogenmod.wallpapers.photophase.preferences; - -/** - * An enumeration with all the touch actions supported - */ -public enum AspectRatioCorrection { - /** - * Do not apply aspect ratio correction - */ - NONE(0), - /** - * Center the image and crop the image using the shortest size of the image - */ - CROP(1), - /** - * Center using the largest size of the image - */ - CENTER(2); - - private final int mValue; - - /** - * Constructor of <code>AspectRatioCorrection</code> - * - * @param id The unique identifier - */ - private AspectRatioCorrection(int value) { - mValue = value; - } - - /** - * Method that returns the value - * - * @return int The value - */ - public int getValue() { - return mValue; - } - - /** - * Method that gets the reference of a AspectRatioCorrection from its value - * - * @param value The value - * @return AspectRatioCorrection The reference - */ - public static final AspectRatioCorrection fromValue(int value) { - if (value == CROP.mValue) return CROP; - if (value == CENTER.mValue) return CENTER; - return NONE; - } -} diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/GeneralPreferenceFragment.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/GeneralPreferenceFragment.java index 08b8fd6..1d04152 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/GeneralPreferenceFragment.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/GeneralPreferenceFragment.java @@ -19,11 +19,11 @@ package org.cyanogenmod.wallpapers.photophase.preferences; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.preference.CheckBoxPreference; import android.preference.ListPreference; import android.preference.MultiSelectListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; -import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.util.Log; @@ -48,7 +48,7 @@ public class GeneralPreferenceFragment extends PreferenceFragment { private SeekBarProgressPreference mWallpaperDim; private ColorPickerPreference mBackgroundColor; private ListPreference mTouchActions; - private ListPreference mAspectRatioCorrection; + private CheckBoxPreference mFixAspectRatio; private MultiSelectListPreference mTransitionsTypes; private SeekBarProgressPreference mTransitionsInterval; private MultiSelectListPreference mEffectsTypes; @@ -67,7 +67,7 @@ public class GeneralPreferenceFragment extends PreferenceFragment { mRedrawFlag = true; int color = ((Integer)newValue).intValue(); Colors.setBackground(new GLColor(color)); - } else if (key.compareTo("ui_aspect_ratio_correction") == 0) { + } else if (key.compareTo("ui_fix_aspect_ratio") == 0) { mRedrawFlag = true; } else if (key.compareTo("ui_transition_types") == 0) { mRedrawFlag = true; @@ -130,10 +130,8 @@ public class GeneralPreferenceFragment extends PreferenceFragment { mTouchActions = (ListPreference)findPreference("ui_touch_action"); mTouchActions.setOnPreferenceChangeListener(mOnChangeListener); - mAspectRatioCorrection = (ListPreference)findPreference("ui_aspect_ratio_correction"); - mAspectRatioCorrection.setOnPreferenceChangeListener(mOnChangeListener); - // TODO Hide for now, because no functionality was developed yet - ((PreferenceCategory)findPreference("category_settings")).removePreference(mAspectRatioCorrection); + mFixAspectRatio = (CheckBoxPreference)findPreference("ui_fix_aspect_ratio"); + mFixAspectRatio.setOnPreferenceChangeListener(mOnChangeListener); mTransitionsTypes = (MultiSelectListPreference)findPreference("ui_transition_types"); mTransitionsTypes.setOnPreferenceChangeListener(mOnChangeListener); diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java index 615c3df..f8aa5f4 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java @@ -201,18 +201,17 @@ public final class PreferencesProvider { * @return TouchAction The action (default NONE) */ public static TouchAction getTouchAction() { - return TouchAction.fromValue(Integer.valueOf(getString("ui_touch_action", "0"))) ; + return TouchAction.fromValue(Integer.valueOf(getString("ui_touch_action", "0"))); } /** - * Return the current user preference about how to fix the aspect ratio of - * the current image of the frame. + * Return the current user preference about fix or not fix the aspect ratio + * of the image by cropping the image. * - * @return AspectRatioCorrection The correction mode (default NONE) + * @return boolean Indicates if the image should be cropped */ - public static AspectRatioCorrection getAspectRatioCorrection() { - return AspectRatioCorrection.fromValue( - Integer.valueOf(getString("ui_aspect_ratio_correction", "0"))) ; + public static boolean isFixAspectRatio() { + return getBoolean("ui_fix_aspect_ratio", false); } /** diff --git a/src/org/cyanogenmod/wallpapers/photophase/utils/GLESUtil.java b/src/org/cyanogenmod/wallpapers/photophase/utils/GLESUtil.java index 7f0cbbc..d2bd46e 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/utils/GLESUtil.java +++ b/src/org/cyanogenmod/wallpapers/photophase/utils/GLESUtil.java @@ -166,6 +166,10 @@ public final class GLESUtil { * The path to the texture */ public File path; + /** + * The effect to apply + */ + public Effect effect; } /** @@ -436,7 +440,6 @@ public final class GLESUtil { if (effect != null) { // Apply the effect effect.apply(textureNames[0], screenDim.width(), screenDim.height(), textureNames[1]); - effect.release(); handle = textureNames[1]; // Delete the unused texture |