diff options
3 files changed, 246 insertions, 1 deletions
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java index c95a15a99..9d103d590 100644 --- a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java +++ b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java @@ -17,6 +17,7 @@ package com.android.gallery3d.filtershow.filters; import android.util.Log; + import com.android.gallery3d.filtershow.editors.BasicEditor; public class FilterRepresentation implements Cloneable { @@ -229,7 +230,7 @@ public class FilterRepresentation implements Cloneable { public void deSerializeRepresentation(String[][] rep) { for (int i = 0; i < rep.length; i++) { if ("Name".equals(rep[i][0])) { - mName = rep[i][0]; + mName = rep[i][1]; break; } } diff --git a/src/com/android/gallery3d/filtershow/tools/MatrixFit.java b/src/com/android/gallery3d/filtershow/tools/MatrixFit.java new file mode 100644 index 000000000..3b815673c --- /dev/null +++ b/src/com/android/gallery3d/filtershow/tools/MatrixFit.java @@ -0,0 +1,200 @@ +/* + * 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.tools; + +import android.util.Log; + +public class MatrixFit { + // Simple implementation of a matrix fit in N dimensions. + + private static final String LOGTAG = "MatrixFit"; + + private double[][] mMatrix; + private int mDimension; + private boolean mValid = false; + private static double sEPS = 1.0f/10000000000.0f; + + public MatrixFit(double[][] from, double[][] to) { + mValid = fit(from, to); + } + + public int getDimension() { + return mDimension; + } + + public boolean isValid() { + return mValid; + } + + public double[][] getMatrix() { + return mMatrix; + } + + public boolean fit(double[][] from, double[][] to) { + if ((from.length != to.length) || (from.length < 1)) { + Log.e(LOGTAG, "from and to must be of same size"); + return false; + } + + mDimension = from[0].length; + mMatrix = new double[mDimension +1][mDimension + mDimension +1]; + + if (from.length < mDimension) { + Log.e(LOGTAG, "Too few points => under-determined system"); + return false; + } + + double[][] q = new double[from.length][mDimension]; + for (int i = 0; i < from.length; i++) { + for (int j = 0; j < mDimension; j++) { + q[i][j] = from[i][j]; + } + } + + double[][] p = new double[to.length][mDimension]; + for (int i = 0; i < to.length; i++) { + for (int j = 0; j < mDimension; j++) { + p[i][j] = to[i][j]; + } + } + + // Make an empty (dim) x (dim + 1) matrix and fill it + double[][] c = new double[mDimension+1][mDimension]; + for (int j = 0; j < mDimension; j++) { + for (int k = 0; k < mDimension + 1; k++) { + for (int i = 0; i < q.length; i++) { + double qt = 1; + if (k < mDimension) { + qt = q[i][k]; + } + c[k][j] += qt * p[i][j]; + } + } + } + + // Make an empty (dim+1) x (dim+1) matrix and fill it + double[][] Q = new double[mDimension+1][mDimension+1]; + for (int qi = 0; qi < q.length; qi++) { + double[] qt = new double[mDimension + 1]; + for (int i = 0; i < mDimension; i++) { + qt[i] = q[qi][i]; + } + qt[mDimension] = 1; + for (int i = 0; i < mDimension + 1; i++) { + for (int j = 0; j < mDimension + 1; j++) { + Q[i][j] += qt[i] * qt[j]; + } + } + } + + // Use a gaussian elimination to solve the linear system + for (int i = 0; i < mDimension + 1; i++) { + for (int j = 0; j < mDimension + 1; j++) { + mMatrix[i][j] = Q[i][j]; + } + for (int j = 0; j < mDimension; j++) { + mMatrix[i][mDimension + 1 + j] = c[i][j]; + } + } + if (!gaussianElimination(mMatrix)) { + return false; + } + return true; + } + + public double[] apply(double[] point) { + if (mDimension != point.length) { + return null; + } + double[] res = new double[mDimension]; + for (int j = 0; j < mDimension; j++) { + for (int i = 0; i < mDimension; i++) { + res[j] += point[i] * mMatrix[i][j+ mDimension +1]; + } + res[j] += mMatrix[mDimension][j+ mDimension +1]; + } + return res; + } + + public void printEquation() { + for (int j = 0; j < mDimension; j++) { + String str = "x" + j + "' = "; + for (int i = 0; i < mDimension; i++) { + str += "x" + i + " * " + mMatrix[i][j+mDimension+1] + " + "; + } + str += mMatrix[mDimension][j+mDimension+1]; + Log.v(LOGTAG, str); + } + } + + private void printMatrix(String name, double[][] matrix) { + Log.v(LOGTAG, "name: " + name); + for (int i = 0; i < matrix.length; i++) { + String str = ""; + for (int j = 0; j < matrix[0].length; j++) { + str += "" + matrix[i][j] + " "; + } + Log.v(LOGTAG, str); + } + } + + /* + * Transforms the given matrix into a row echelon matrix + */ + private boolean gaussianElimination(double[][] m) { + int h = m.length; + int w = m[0].length; + + for (int y = 0; y < h; y++) { + int maxrow = y; + for (int y2 = y + 1; y2 < h; y2++) { // Find max pivot + if (Math.abs(m[y2][y]) > Math.abs(m[maxrow][y])) { + maxrow = y2; + } + } + // swap + for (int i = 0; i < mDimension; i++) { + double t = m[y][i]; + m[y][i] = m[maxrow][i]; + m[maxrow][i] = t; + } + + if (Math.abs(m[y][y]) <= sEPS) { // Singular Matrix + return false; + } + for (int y2 = y + 1; y2 < h; y2++) { // Eliminate column y + double c = m[y2][y] / m[y][y]; + for (int x = y; x < w; x++) { + m[y2][x] -= m[y][x] * c; + } + } + } + for (int y = h -1; y > -1; y--) { // Back substitution + double c = m[y][y]; + for (int y2 = 0; y2 < y; y2++) { + for (int x = w - 1; x > y - 1; x--) { + m[y2][x] -= m[y][x] * m[y2][y] / c; + } + } + m[y][y] /= c; + for (int x = h; x < w; x++) { // Normalize row y + m[y][x] /= c; + } + } + return true; + } +} diff --git a/src/com/android/gallery3d/ingest/ui/MtpImageView.java b/src/com/android/gallery3d/ingest/ui/MtpImageView.java index a773f4485..80c105126 100644 --- a/src/com/android/gallery3d/ingest/ui/MtpImageView.java +++ b/src/com/android/gallery3d/ingest/ui/MtpImageView.java @@ -17,7 +17,9 @@ package com.android.gallery3d.ingest.ui; import android.content.Context; +import android.graphics.Canvas; import android.graphics.Matrix; +import android.graphics.drawable.Drawable; import android.mtp.MtpDevice; import android.mtp.MtpObjectInfo; import android.os.Handler; @@ -27,6 +29,7 @@ import android.os.Message; import android.util.AttributeSet; import android.widget.ImageView; +import com.android.gallery3d.R; import com.android.gallery3d.ingest.MtpDeviceIndex; import com.android.gallery3d.ingest.data.BitmapWithMetadata; import com.android.gallery3d.ingest.data.MtpBitmapFetch; @@ -46,6 +49,8 @@ public class MtpImageView extends ImageView { private MtpObjectInfo mFetchObjectInfo; private MtpDevice mFetchDevice; private Object mFetchResult; + private Drawable mOverlayIcon; + private boolean mShowOverlayIcon; private static final FetchImageHandler sFetchHandler = FetchImageHandler.createOnNewThread(); private static final ShowImageHandler sFetchCompleteHandler = new ShowImageHandler(); @@ -82,6 +87,11 @@ public class MtpImageView extends ImageView { showPlaceholder(); mGeneration = gen; mObjectHandle = handle; + mShowOverlayIcon = MtpDeviceIndex.SUPPORTED_VIDEO_FORMATS.contains(object.getFormat()); + if (mShowOverlayIcon && mOverlayIcon == null) { + mOverlayIcon = getResources().getDrawable(R.drawable.ic_control_play); + updateOverlayIconBounds(); + } synchronized (mFetchLock) { mFetchObjectInfo = object; mFetchDevice = device; @@ -143,12 +153,46 @@ public class MtpImageView extends ImageView { setImageMatrix(mDrawMatrix); } + private static final int OVERLAY_ICON_SIZE_DENOMINATOR = 4; + + private void updateOverlayIconBounds() { + int iheight = mOverlayIcon.getIntrinsicHeight(); + int iwidth = mOverlayIcon.getIntrinsicWidth(); + int vheight = getHeight(); + int vwidth = getWidth(); + float scale_height = ((float) vheight) / (iheight * OVERLAY_ICON_SIZE_DENOMINATOR); + float scale_width = ((float) vwidth) / (iwidth * OVERLAY_ICON_SIZE_DENOMINATOR); + if (scale_height >= 1f && scale_width >= 1f) { + mOverlayIcon.setBounds((vwidth - iwidth) / 2, + (vheight - iheight) / 2, + (vwidth + iwidth) / 2, + (vheight + iheight) / 2); + } else { + float scale = Math.min(scale_height, scale_width); + mOverlayIcon.setBounds((int) (vwidth - scale * iwidth) / 2, + (int) (vheight - scale * iheight) / 2, + (int) (vwidth + scale * iwidth) / 2, + (int) (vheight + scale * iheight) / 2); + } + } + @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (changed && getScaleType() == ScaleType.MATRIX) { updateDrawMatrix(); } + if (mShowOverlayIcon && changed && mOverlayIcon != null) { + updateOverlayIconBounds(); + } + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + if (mShowOverlayIcon && mOverlayIcon != null) { + mOverlayIcon.draw(canvas); + } } protected void onMtpImageDataFetchedFromDevice(Object result) { |