OSDN Git Service

fix draw and curves to use FilterRepresentation
authorJohn Hoford <hoford@google.com>
Tue, 5 Feb 2013 17:09:04 +0000 (09:09 -0800)
committerJohn Hoford <hoford@google.com>
Thu, 7 Feb 2013 22:43:26 +0000 (14:43 -0800)
Change-Id: I500d9c04ea0e55cbc9b1927dee2f6a19a67d5349

13 files changed:
res/drawable-nodpi/brush1.png [new file with mode: 0644]
res/drawable-nodpi/brush2.png [new file with mode: 0644]
src/com/android/gallery3d/filtershow/cache/ImageLoader.java
src/com/android/gallery3d/filtershow/editors/EditorCurves.java
src/com/android/gallery3d/filtershow/editors/EditorDraw.java
src/com/android/gallery3d/filtershow/filters/FilterCurvesRepresentation.java [new file with mode: 0644]
src/com/android/gallery3d/filtershow/filters/FilterDrawRepresentation.java [new file with mode: 0644]
src/com/android/gallery3d/filtershow/filters/ImageFilterCurves.java
src/com/android/gallery3d/filtershow/filters/ImageFilterDraw.java
src/com/android/gallery3d/filtershow/imageshow/ImageDraw.java
src/com/android/gallery3d/filtershow/imageshow/MasterImage.java
src/com/android/gallery3d/filtershow/ui/ImageCurves.java
src/com/android/gallery3d/filtershow/ui/Spline.java

diff --git a/res/drawable-nodpi/brush1.png b/res/drawable-nodpi/brush1.png
new file mode 100644 (file)
index 0000000..6030f25
Binary files /dev/null and b/res/drawable-nodpi/brush1.png differ
diff --git a/res/drawable-nodpi/brush2.png b/res/drawable-nodpi/brush2.png
new file mode 100644 (file)
index 0000000..ae15c22
Binary files /dev/null and b/res/drawable-nodpi/brush2.png differ
index da8e528..b47d13b 100644 (file)
@@ -215,6 +215,10 @@ public class ImageLoader {
         warnListeners();
     }
 
+    public Bitmap decodeImage(int id, BitmapFactory.Options options) {
+        return BitmapFactory.decodeResource(mContext.getResources(), id, options);
+    }
+
     public static Bitmap rotateToPortrait(Bitmap bitmap, int ori) {
         Matrix matrix = new Matrix();
         int w = bitmap.getWidth();
index a336048..b6e7b2b 100644 (file)
@@ -20,11 +20,13 @@ import android.content.Context;
 import android.widget.FrameLayout;
 
 import com.android.gallery3d.R;
+import com.android.gallery3d.filtershow.filters.FilterCurvesRepresentation;
+import com.android.gallery3d.filtershow.filters.FilterRepresentation;
 import com.android.gallery3d.filtershow.ui.ImageCurves;
 
 public class EditorCurves extends Editor {
     public static final int ID = R.id.imageCurves;
-
+    ImageCurves mImageCurves;
     public EditorCurves() {
         super(ID);
     }
@@ -32,6 +34,17 @@ public class EditorCurves extends Editor {
     @Override
     public void createEditor(Context context, FrameLayout frameLayout) {
         super.createEditor(context, frameLayout);
-        mView = mImageShow = new ImageCurves(context);
+        mView = mImageShow = mImageCurves = new ImageCurves(context);
+        mImageCurves.setEditor(this);
+    }
+
+    @Override
+    public void reflectCurrentFilter() {
+        super.reflectCurrentFilter();
+        FilterRepresentation rep = getLocalRepresentation();
+        if (rep != null && getLocalRepresentation() instanceof FilterCurvesRepresentation) {
+            FilterCurvesRepresentation drawRep = (FilterCurvesRepresentation) rep;
+            mImageCurves.setFilterDrawRepresentation(drawRep);
+        }
     }
 }
index a31c1d3..ff40185 100644 (file)
@@ -18,12 +18,10 @@ package com.android.gallery3d.filtershow.editors;
 
 import android.app.Dialog;
 import android.content.Context;
-import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
-import android.view.WindowManager;
 import android.view.WindowManager.LayoutParams;
 import android.widget.Button;
 import android.widget.FrameLayout;
@@ -35,16 +33,16 @@ import com.android.gallery3d.R;
 import com.android.gallery3d.filtershow.FilterShowActivity;
 import com.android.gallery3d.filtershow.colorpicker.ColorGridDialog;
 import com.android.gallery3d.filtershow.colorpicker.RGBListener;
+import com.android.gallery3d.filtershow.filters.FilterDrawRepresentation;
+import com.android.gallery3d.filtershow.filters.FilterRepresentation;
 import com.android.gallery3d.filtershow.filters.ImageFilterDraw;
 import com.android.gallery3d.filtershow.imageshow.ImageDraw;
 import com.android.gallery3d.filtershow.ui.FramedTextButton;
 
-/**
- * TODO: Insert description here. (generated by hoford)
- */
 public class EditorDraw extends Editor {
     private static final String LOGTAG = "EditorDraw";
     public static final int ID = R.id.editorDraw;
+    public ImageDraw mImageDraw;
 
     public EditorDraw() {
         super(ID);
@@ -53,7 +51,9 @@ public class EditorDraw extends Editor {
     @Override
     public void createEditor(Context context, FrameLayout frameLayout) {
         super.createEditor(context, frameLayout);
-        mView = mImageShow = new ImageDraw(context);
+        mView = mImageShow = mImageDraw = new ImageDraw(context);
+        mImageDraw.setEditor(this);
+
     }
 
     @Override
@@ -62,6 +62,17 @@ public class EditorDraw extends Editor {
     }
 
     @Override
+    public void reflectCurrentFilter() {
+        super.reflectCurrentFilter();
+        FilterRepresentation rep = getLocalRepresentation();
+
+        if (rep != null && getLocalRepresentation() instanceof FilterDrawRepresentation) {
+            FilterDrawRepresentation drawRep = (FilterDrawRepresentation) getLocalRepresentation();
+            mImageDraw.setFilterDrawRepresentation(drawRep);
+        }
+    }
+
+    @Override
     public void openUtilityPanel(final LinearLayout accessoryViewList) {
         View view = accessoryViewList.findViewById(R.id.drawUtilityButton);
         if (view == null) {
@@ -70,7 +81,8 @@ public class EditorDraw extends Editor {
             view = inflater.inflate(R.layout.filtershow_draw_button, accessoryViewList, false);
             accessoryViewList.addView(view, view.getLayoutParams());
             view.setOnClickListener(new OnClickListener() {
-                    @Override
+
+                @Override
                 public void onClick(View arg0) {
                     showPopupMenu(accessoryViewList);
                 }
@@ -88,9 +100,10 @@ public class EditorDraw extends Editor {
         if (button == null) {
             return;
         }
-        PopupMenu popupMenu = new PopupMenu(mImageShow.getActivity(), button);
+        final PopupMenu popupMenu = new PopupMenu(mImageShow.getActivity(), button);
         popupMenu.getMenuInflater().inflate(R.menu.filtershow_menu_draw, popupMenu.getMenu());
         popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
+
             @Override
             public boolean onMenuItemClick(MenuItem item) {
                 ImageFilterDraw filter = (ImageFilterDraw) mImageShow.getCurrentFilter();
@@ -100,11 +113,12 @@ public class EditorDraw extends Editor {
                     showSizeDialog(item);
                 } else if (item.getItemId() == R.id.draw_menu_style_brush) {
                     ImageDraw idraw = (ImageDraw) mImageShow;
-                    idraw.setStyle(ImageFilterDraw.BRUSH_STYLE);
+                    idraw.setStyle((byte) (idraw.getStyle() + 1));
+                    item.setIcon(idraw.getIcon(mContext));
+                    popupMenu.show();
                 } else if (item.getItemId() == R.id.draw_menu_style_line) {
                     ImageDraw idraw = (ImageDraw) mImageShow;
                     idraw.setStyle(ImageFilterDraw.SIMPLE_STYLE);
-
                 }
                 mView.invalidate();
                 return true;
@@ -124,6 +138,7 @@ public class EditorDraw extends Editor {
         Button button = (Button) dialog.findViewById(R.id.sizeAcceptButton);
         button.setOnClickListener(new OnClickListener() {
 
+            @Override
             public void onClick(View arg0) {
                 int p = bar.getProgress();
                 ImageDraw idraw = (ImageDraw) mImageShow;
@@ -133,9 +148,10 @@ public class EditorDraw extends Editor {
         });
         dialog.show();
     }
+
     public void showColorGrid(final MenuItem item) {
         RGBListener cl = new RGBListener() {
-                @Override
+            @Override
             public void setColor(int rgb) {
                 ImageDraw idraw = (ImageDraw) mImageShow;
                 idraw.setColor(rgb);
@@ -145,5 +161,4 @@ public class EditorDraw extends Editor {
         cpd.show();
         LayoutParams params = cpd.getWindow().getAttributes();
     }
-
 }
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterCurvesRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterCurvesRepresentation.java
new file mode 100644 (file)
index 0000000..d90f558
--- /dev/null
@@ -0,0 +1,35 @@
+package com.android.gallery3d.filtershow.filters;
+
+import com.android.gallery3d.filtershow.ui.Spline;
+
+/**
+ * TODO: Insert description here. (generated by hoford)
+ */
+public class FilterCurvesRepresentation extends FilterRepresentation {
+    private Spline[] mSplines = new Spline[4];
+    public FilterCurvesRepresentation() {
+        super("Curves");
+        for (int i = 0; i < mSplines.length; i++) {
+            mSplines[i] = new Spline();
+            mSplines[i].reset();
+        }
+    }
+
+    public void reset() {
+        Spline spline = new Spline();
+
+        spline.addPoint(0.0f, 1.0f);
+        spline.addPoint(1.0f, 0.0f);
+
+        for (int i = 0; i < 4; i++) {
+            mSplines[i] = new Spline(spline);
+        }
+    }
+
+    public void setSpline(int splineIndex, Spline s) {
+        mSplines[splineIndex] = s;
+    }
+    public Spline getSpline(int splineIndex) {
+        return mSplines[splineIndex];
+    }
+}
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterDrawRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterDrawRepresentation.java
new file mode 100644 (file)
index 0000000..84e04c7
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * 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.Path;
+
+import java.util.Vector;
+
+public class FilterDrawRepresentation extends FilterRepresentation {
+    private static final String LOGTAG = "FilterDrawRepresentation";
+
+    public static class StrokeData implements Cloneable {
+        public byte mType;
+        public Path mPath;
+        public float mRadius;
+        public int mColor;
+
+        @Override
+        public String toString() {
+            return "stroke(" + mType + ", path(" + (mPath) + "), " + mRadius + " , "
+                    + Integer.toHexString(mColor) + ")";
+        }
+        @Override
+        public StrokeData clone() throws CloneNotSupportedException {
+            return (StrokeData) super.clone();
+        }
+    }
+
+    private Vector<StrokeData> mDrawing = new Vector<StrokeData>();
+    private StrokeData mCurrent; // used in the currently drawing style
+
+    public FilterDrawRepresentation() {
+        super("Draw");
+    }
+
+    @Override
+    public String toString() {
+        return getName() + " : strokes=" + mDrawing.size()
+                + ((mCurrent == null) ? " no current " : ("current=" + mCurrent.mType));
+    }
+
+    public Vector<StrokeData> getDrawing() {
+        return mDrawing;
+    }
+
+    public StrokeData getCurrentDrawing() {
+        return mCurrent;
+    }
+
+    @Override
+    public FilterRepresentation clone() throws CloneNotSupportedException {
+        FilterDrawRepresentation representation = (FilterDrawRepresentation) super.clone();
+        return representation;
+    }
+
+    @Override
+    public void useParametersFrom(FilterRepresentation a) {
+        if (a instanceof FilterDrawRepresentation) {
+            FilterDrawRepresentation representation = (FilterDrawRepresentation) a;
+            try {
+                if (representation.mCurrent != null) {
+                    mCurrent = (StrokeData) representation.mCurrent.clone();
+                } else {
+                    mCurrent = null;
+                }
+                if (representation.mDrawing != null) {
+                    mDrawing = (Vector<StrokeData>) representation.mDrawing.clone();
+                }
+            } catch (CloneNotSupportedException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+    @Override
+    public boolean equals(FilterRepresentation representation) {
+        if (!super.equals(representation)) {
+            return false;
+        }
+        if (representation instanceof FilterDrawRepresentation) {
+            FilterDrawRepresentation grunge = (FilterDrawRepresentation) representation;
+                return true;
+        }
+        return false;
+    }
+
+    public void startNewSection(byte type, int color, float size, float x, float y) {
+        mCurrent = new StrokeData();
+        mCurrent.mColor = color;
+        mCurrent.mRadius = size;
+        mCurrent.mType = type;
+        mCurrent.mPath = new Path();
+        mCurrent.mPath.moveTo(x, y);
+    }
+
+    public void addPoint(float x, float y) {
+        mCurrent.mPath.lineTo(x, y);
+    }
+
+    public void endSection(float x, float y) {
+        mCurrent.mPath.lineTo(x, y);
+        mDrawing.add(mCurrent);
+        mCurrent = null;
+    }
+
+    public void clear() {
+        mCurrent = null;
+        mDrawing.clear();
+    }
+
+}
index 15cf046..7919052 100644 (file)
@@ -25,7 +25,26 @@ import com.android.gallery3d.filtershow.ui.Spline;
 public class ImageFilterCurves extends ImageFilter {
 
     private static final String LOGTAG = "ImageFilterCurves";
-    private Spline[] mSplines = new Spline[4];
+    FilterCurvesRepresentation mParameters = new FilterCurvesRepresentation();
+
+    @Override
+    public boolean hasDefaultRepresentation() {
+        return true;
+    }
+
+    @Override
+    public FilterRepresentation getDefaultRepresentation() {
+        FilterCurvesRepresentation representation = new FilterCurvesRepresentation();
+        representation.setName("Draw");
+        representation.setFilterClass(ImageFilterCurves.class);
+        return representation;
+    }
+
+    @Override
+    public void useRepresentation(FilterRepresentation representation) {
+        FilterCurvesRepresentation parameters = (FilterCurvesRepresentation) representation;
+        mParameters = parameters;
+    }
 
     public ImageFilterCurves() {
         mName = "Curves";
@@ -65,18 +84,14 @@ public class ImageFilterCurves extends ImageFilter {
     @Override
     public ImageFilter clone() throws CloneNotSupportedException {
         ImageFilterCurves filter = (ImageFilterCurves) super.clone();
-        filter.mSplines = new Spline[4];
-        for (int i = 0; i < 4; i++) {
-            if (mSplines[i] != null) {
-                filter.setSpline(mSplines[i], i);
-            }
-        }
+        filter.mParameters = (FilterCurvesRepresentation) mParameters.clone();
+
         return filter;
     }
 
     public boolean isNil() {
         for (int i = 0; i < 4; i++) {
-            if (mSplines[i] != null && !mSplines[i].isOriginal()) {
+            if (mParameters.getSpline(i) != null && !mParameters.getSpline(i).isOriginal()) {
                 return false;
             }
         }
@@ -91,7 +106,7 @@ public class ImageFilterCurves extends ImageFilter {
         }
         ImageFilterCurves curve = (ImageFilterCurves) filter;
         for (int i = 0; i < 4; i++) {
-            if (mSplines[i] != curve.mSplines[i]) {
+            if (mParameters.getSpline(i) != curve.mParameters.getSpline(i)) {
                 return false;
             }
         }
@@ -99,7 +114,7 @@ public class ImageFilterCurves extends ImageFilter {
     }
 
     public void populateArray(int[] array, int curveIndex) {
-        Spline spline = mSplines[curveIndex];
+        Spline spline = mParameters.getSpline(curveIndex);
         if (spline == null) {
             return;
         }
@@ -111,7 +126,7 @@ public class ImageFilterCurves extends ImageFilter {
 
     @Override
     public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
-        if (!mSplines[Spline.RGB].isOriginal()) {
+        if (!mParameters.getSpline(Spline.RGB).isOriginal()) {
             int[] rgbGradient = new int[256];
             populateArray(rgbGradient, Spline.RGB);
             nativeApplyGradientFilter(bitmap, bitmap.getWidth(), bitmap.getHeight(),
@@ -119,17 +134,17 @@ public class ImageFilterCurves extends ImageFilter {
         }
 
         int[] redGradient = null;
-        if (!mSplines[Spline.RED].isOriginal()) {
+        if (!mParameters.getSpline(Spline.RED).isOriginal()) {
             redGradient = new int[256];
             populateArray(redGradient, Spline.RED);
         }
         int[] greenGradient = null;
-        if (!mSplines[Spline.GREEN].isOriginal()) {
+        if (!mParameters.getSpline(Spline.GREEN).isOriginal()) {
             greenGradient = new int[256];
             populateArray(greenGradient, Spline.GREEN);
         }
         int[] blueGradient = null;
-        if (!mSplines[Spline.BLUE].isOriginal()) {
+        if (!mParameters.getSpline(Spline.BLUE).isOriginal()) {
             blueGradient = new int[256];
             populateArray(blueGradient, Spline.BLUE);
         }
@@ -140,11 +155,11 @@ public class ImageFilterCurves extends ImageFilter {
     }
 
     public void setSpline(Spline spline, int splineIndex) {
-        mSplines[splineIndex] = new Spline(spline);
+        mParameters.setSpline(splineIndex, new Spline(spline));
     }
 
     public Spline getSpline(int splineIndex) {
-        return mSplines[splineIndex];
+        return mParameters.getSpline(splineIndex);
     }
 
     public void reset() {
@@ -154,16 +169,15 @@ public class ImageFilterCurves extends ImageFilter {
         spline.addPoint(1.0f, 0.0f);
 
         for (int i = 0; i < 4; i++) {
-            mSplines[i] = new Spline(spline);
+            mParameters.setSpline(i, new Spline(spline));
         }
     }
 
-    // TODO: fix useFilter
     public void useFilter(ImageFilter a) {
         ImageFilterCurves c = (ImageFilterCurves) a;
         for (int i = 0; i < 4; i++) {
-            if (c.mSplines[i] != null) {
-                setSpline(c.mSplines[i], i);
+            if (c.mParameters.getSpline(i) != null) {
+                setSpline(c.mParameters.getSpline(i), i);
             }
         }
     }
index d83a521..f1ed90c 100644 (file)
@@ -16,7 +16,9 @@
 
 package com.android.gallery3d.filtershow.filters;
 
+import android.content.Context;
 import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Matrix;
@@ -24,258 +26,131 @@ import android.graphics.Paint;
 import android.graphics.Paint.Style;
 import android.graphics.Path;
 import android.graphics.PathMeasure;
+import android.graphics.PorterDuff;
+import android.graphics.PorterDuffColorFilter;
 
 import com.android.gallery3d.R;
+import com.android.gallery3d.filtershow.FilterShowActivity;
 import com.android.gallery3d.filtershow.editors.EditorDraw;
+import com.android.gallery3d.filtershow.filters.FilterDrawRepresentation.StrokeData;
+import com.android.gallery3d.filtershow.imageshow.MasterImage;
 
-import java.util.Arrays;
+import java.util.Vector;
 
 public class ImageFilterDraw extends ImageFilter {
     private static final String LOGTAG = "ImageFilterDraw";
-    public final static char SIMPLE_STYLE = 0;
-    public final static char BRUSH_STYLE = 1;
+    public final static byte SIMPLE_STYLE = 0;
+    public final static byte BRUSH_STYLE = 1;
+    public final static int NUMBER_OF_STYLES = 3;
     Bitmap mOverlayBitmap; // this accelerates interaction
     int mCachedStrokes = -1;
     int mCurrentStyle = 0;
-    DrawStyle[] mDrawings = new DrawStyle[] {
-            new SimpleDraw(), new Brush() };
 
-    public void setStyle(char style) {
-        mCurrentStyle = style;
+    FilterDrawRepresentation mParameters = new FilterDrawRepresentation();
+
+    public ImageFilterDraw() {
+        mName = "Image Draw";
+        setFilterType(TYPE_VIGNETTE);
     }
 
-    public static interface DrawStyle {
-        public DrawStyle clone();
-        public void setSize(float radius);
-        public void setColor(int color);
-        public void startStroke(float x, float y);
-        public void stroke(float x, float y);
-        public void endStroke(float x, float y);
-        public int getNumberOfStrokes();
-        public void clearCurren();
-        public void paintCurrentStroke(Canvas canvas, Matrix toScrMatrix, boolean highQuality);
-        public int paintLast(int from, Canvas canvas, Matrix toScrMatrix, boolean highQuality);
-        public boolean same(DrawStyle o);
-        public boolean empty();
-    };
+    DrawStyle[] mDrawingsTypes = new DrawStyle[] {
+            new SimpleDraw(), new Brush(R.drawable.brush1), new Brush(R.drawable.brush2) };
+    {
+        for (int i = 0; i < mDrawingsTypes.length; i++) {
+            mDrawingsTypes[i].setType((byte) i);
+        }
 
-    class SimpleDraw implements DrawStyle {
-        private Path[] mPaths = new Path[0];
-        private int[] mColors = new int[0];
-        private float[] mRadius = new float[0];
-        private int mStrokeCnt = 0;
+    }
 
-        private Path mCurrentPath;
-        private float mCurrentRadius;
-        private int mCurrentColor;
+    @Override
+    public boolean hasDefaultRepresentation() {
+        return true;
+    }
+    @Override
+    public FilterRepresentation getDefaultRepresentation() {
+        FilterDrawRepresentation representation = new FilterDrawRepresentation();
+        representation.setName("Draw");
+        representation.setFilterClass(ImageFilterDraw.class);
+        return representation;
+    }
 
-        @Override
-        public DrawStyle clone() {
-            SimpleDraw ret = new SimpleDraw();
-            ret.mPaths = new Path[mPaths.length];
-            for (int i = 0; i < ret.mPaths.length; i++) {
-                ret.mPaths[i] = new Path(mPaths[i]);
-            }
-            ret.mColors = Arrays.copyOf(mColors, mColors.length);
-            ret.mRadius = Arrays.copyOf(mRadius, mRadius.length);
-            ret.mStrokeCnt = mStrokeCnt;
-            return ret;
-        }
+    @Override
+    public void useRepresentation(FilterRepresentation representation) {
+        FilterDrawRepresentation parameters = (FilterDrawRepresentation) representation;
+        mParameters = parameters;
+    }
 
-        @Override
-        public boolean empty() {
-            return mStrokeCnt == -1;
-        }
+    public void setStyle(byte style) {
+        mCurrentStyle = style % mDrawingsTypes.length;
+    }
 
-        @Override
-        public void setSize(float radius) {
-            mCurrentRadius = radius;
-        }
+    public int getStyle() {
+        return mCurrentStyle;
+    }
 
-        @Override
-        public void setColor(int color) {
-            mCurrentColor = color;
-        }
+    public static interface DrawStyle {
+        public void setType(byte type);
+        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
+                boolean highQuality);
+    }
 
-        @Override
-        public void startStroke(float x, float y) {
-            mCurrentPath = new Path();
-            mCurrentPath.moveTo(x, y);
-        }
+    class SimpleDraw implements DrawStyle {
+        byte mType;
 
         @Override
-        public void stroke(float x, float y) {
-            if (mCurrentPath != null) {
-                mCurrentPath.lineTo(x, y);
-            }
+        public void setType(byte type) {
+            mType = type;
         }
 
         @Override
-        public void endStroke(float x, float y) {
-            if (mCurrentPath != null) {
-                mCurrentPath.lineTo(x, y);
-                Path[] np = new Path[mStrokeCnt + 1];
-                for (int i = 0; i < mStrokeCnt; i++) {
-                    np[i] = mPaths[i];
-                }
-                np[mStrokeCnt] = mCurrentPath;
-                mColors = Arrays.copyOf(mColors, mColors.length + 1);
-                mRadius = Arrays.copyOf(mRadius, mRadius.length + 1);
-                mRadius[mStrokeCnt] = mCurrentRadius;
-                mColors[mStrokeCnt] = mCurrentColor;
-                mPaths = np;
-                mStrokeCnt++;
+        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
+                boolean highQuality) {
+            if (sd == null) {
+                return;
             }
-        }
-
-        @Override
-        public void clearCurren(){
-            mCurrentPath = null;
-        }
-
-        @Override
-        public void paintCurrentStroke(Canvas canvas, Matrix toScrMatrix, boolean highQuality) {
-            Path path = mCurrentPath;
-            if (path == null) {
+            if (sd.mPath == null) {
                 return;
             }
             Paint paint = new Paint();
 
             paint.setStyle(Style.STROKE);
-            paint.setColor(mCurrentColor);
-            paint.setStrokeWidth(toScrMatrix.mapRadius(mCurrentRadius));
+            paint.setColor(sd.mColor);
+            paint.setStrokeWidth(toScrMatrix.mapRadius(sd.mRadius));
 
-            // don this way because a bug in path.transform(matrix)
+            // don this way because of a bug in path.transform(matrix)
             Path mCacheTransPath = new Path();
-            mCacheTransPath.addPath(path, toScrMatrix);
+            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
 
             canvas.drawPath(mCacheTransPath, paint);
         }
-
-        @Override
-        public int paintLast(int from, Canvas canvas, Matrix toScrMatrix, boolean highQuality) {
-            Paint paint = new Paint();
-            Matrix m = new Matrix();
-            canvas.save();
-            canvas.concat(toScrMatrix);
-            paint.setStyle(Style.STROKE);
-            for (int i = from; i < mStrokeCnt; i++) {
-                paint.setColor(mColors[i]);
-                paint.setStrokeWidth(mRadius[i]);
-                canvas.drawPath(mPaths[i], paint);
-            }
-            canvas.restore();
-            return mStrokeCnt;
-        }
-
-        @Override
-        public boolean same(DrawStyle o) {
-            if (!(o instanceof SimpleDraw)) {
-                return false;
-            }
-            SimpleDraw sd = (SimpleDraw) o;
-            boolean same;
-            same = Arrays.equals(mRadius, sd.mRadius);
-            if (!same) {
-                return false;
-            }
-            same = Arrays.equals(mColors, sd.mColors);
-            if (!same) {
-                return false;
-            }
-            for (int i = 0; i < mPaths.length; i++) {
-                if (!mPaths[i].equals(sd.mPaths)) {
-                    return false;
-                }
-            }
-            return true;
-        }
-
-        @Override
-        public int getNumberOfStrokes() {
-            return mStrokeCnt;
-        }
     }
 
     class Brush implements DrawStyle {
-        private Path[] mPaths = new Path[0];
-        private int[] mColors = new int[0];
-        private float[] mRadius = new float[0];
-        private int mStrokeCnt = 0;
-
-        private Path mCurrentPath;
-        private float mCurrentRadius;
-        private int mCurrentColor;
-
-        @Override
-        public DrawStyle clone() {
-            Brush ret = new Brush();
-            ret.mPaths = new Path[mPaths.length];
-            for (int i = 0; i < ret.mPaths.length; i++) {
-                ret.mPaths[i] = new Path(mPaths[i]);
-            }
-            ret.mColors = Arrays.copyOf(mColors, mColors.length);
-            ret.mRadius = Arrays.copyOf(mRadius, mRadius.length);
-            ret.mStrokeCnt = mStrokeCnt;
-            return ret;
-        }
-
-        @Override
-        public boolean empty() {
-            return mStrokeCnt == -1;
-        }
-
-        @Override
-        public void setSize(float radius) {
-            mCurrentRadius = radius;
-        }
-
-        @Override
-        public void setColor(int color) {
-            mCurrentColor = color;
-        }
-
-        @Override
-        public void startStroke(float x, float y) {
-            mCurrentPath = new Path();
-            mCurrentPath.moveTo(x, y);
-        }
-
-        @Override
-        public void stroke(float x, float y) {
-            if (mCurrentPath != null) {
-                mCurrentPath.lineTo(x, y);
+        int mBrushID = R.drawable.brush2;
+        Bitmap mBrush;
+        byte mType;
+
+        public Brush(int brushID) {
+            mBrushID = brushID;
+        }
+        public Bitmap getBrush() {
+            if (mBrush == null) {
+                BitmapFactory.Options opt = new BitmapFactory.Options();
+                opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
+                mBrush = MasterImage.getImage().getImageLoader().decodeImage(mBrushID, opt);
+                mBrush = mBrush.extractAlpha();
             }
+            return mBrush;
         }
 
         @Override
-        public void endStroke(float x, float y) {
-            if (mCurrentPath != null) {
-                mCurrentPath.lineTo(x, y);
-                Path[] np = new Path[mStrokeCnt + 1];
-                for (int i = 0; i < mStrokeCnt; i++) {
-                    np[i] = mPaths[i];
-                }
-                np[mStrokeCnt] = mCurrentPath;
-                mColors = Arrays.copyOf(mColors, mColors.length + 1);
-                mRadius = Arrays.copyOf(mRadius, mRadius.length + 1);
-                mRadius[mStrokeCnt] = mCurrentRadius;
-                mColors[mStrokeCnt] = mCurrentColor;
-                mPaths = np;
-                mStrokeCnt++;
-                clearCurren();
+        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
+                boolean highQuality) {
+            if (sd == null) {
+                return;
             }
-        }
 
-        @Override
-        public void clearCurren() {
-            mCurrentPath = null;
-        }
-
-        @Override
-        public void paintCurrentStroke(Canvas canvas, Matrix toScrMatrix, boolean highQuality) {
-            Path path = mCurrentPath;
-            if (path == null) {
+            if (sd == null || sd.mPath == null) {
                 return;
             }
             Paint paint = new Paint();
@@ -283,88 +158,41 @@ public class ImageFilterDraw extends ImageFilter {
 
             float scale = toScrMatrix.mapRadius(1);
             Path mCacheTransPath = new Path();
-            mCacheTransPath.addPath(path, toScrMatrix);
-            draw(canvas, paint, mCurrentColor, toScrMatrix.mapRadius(mCurrentRadius),
+            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
+            draw(canvas, paint, sd.mColor, toScrMatrix.mapRadius(sd.mRadius),
                     mCacheTransPath);
         }
 
-        @Override
-        public int paintLast(int from, Canvas canvas, Matrix toScrMatrix, boolean highQuality) {
-            Paint paint = new Paint();
-
-            Matrix m = new Matrix();
-            canvas.save();
-            canvas.concat(toScrMatrix);
-            paint.setStyle(Style.STROKE);
-            for (int i = from; i < mStrokeCnt; i++) {
-
-                draw(canvas, paint, mColors[i], mRadius[i], mPaths[i]);
-            }
-            canvas.restore();
-            return mStrokeCnt;
-        }
-
-        PathMeasure mPathMeasure = new PathMeasure();
-
         void draw(Canvas canvas, Paint paint, int color, float size, Path path) {
+            PathMeasure mPathMeasure = new PathMeasure();
+            float[] mPosition = new float[2];
+            float[] mTan = new float[2];
 
             mPathMeasure.setPath(path, false);
-            float[] pos = new float[2];
-            float[] tan = new float[2];
+
+            paint.setAntiAlias(true);
             paint.setColor(color);
+            paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
+            Bitmap brush = Bitmap.createScaledBitmap(getBrush(), (int) size, (int) size, true);
             float len = mPathMeasure.getLength();
-            for (float i = 0; i < len; i += (size) / 2) {
-                mPathMeasure.getPosTan(i, pos, tan);
-                canvas.drawCircle(pos[0], pos[1], size, paint);
+            float s2 = size / 2;
+            float step = s2 / 6;
+            for (float i = 0; i < len; i += step) {
+                mPathMeasure.getPosTan(i, mPosition, mTan);
+                //                canvas.drawCircle(pos[0], pos[1], size, paint);
+                canvas.drawBitmap(brush, mPosition[0] - s2, mPosition[1] - s2, paint);
             }
-
-        }
-
-        @Override
-        public boolean same(DrawStyle o) {
-            if (!(o instanceof Brush)) {
-                return false;
-            }
-            Brush sd = (Brush) o;
-            boolean same;
-            same = Arrays.equals(mRadius, sd.mRadius);
-            if (!same) {
-                return false;
-            }
-            same = Arrays.equals(mColors, sd.mColors);
-            if (!same) {
-                return false;
-            }
-            for (int i = 0; i < mPaths.length; i++) {
-                if (!mPaths[i].equals(sd.mPaths)) {
-                    return false;
-                }
-            }
-            return true;
         }
 
         @Override
-        public int getNumberOfStrokes() {
-            return mStrokeCnt;
+        public void setType(byte type) {
+            mType = type;
         }
     }
 
-    public void startSection(int color, float size, float x, float y) {
-        mDrawings[mCurrentStyle].setColor(color);
-        mDrawings[mCurrentStyle].setSize(size);
-        mDrawings[mCurrentStyle].startStroke(x, y);
-    }
-
-    public void addPoint(float x, float y) {
-        mDrawings[mCurrentStyle].stroke(x, y);
-    }
-
-    public void endSection(float x, float y) {
-        mDrawings[mCurrentStyle].endStroke(x, y);
-    }
-
-    public ImageFilterDraw() {
-        mName = "Image Draw";
+    void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
+            boolean highQuality) {
+        mDrawingsTypes[sd.mType].paint(sd, canvas, toScrMatrix, highQuality);
     }
 
     public void drawData(Canvas canvas, Matrix originalRotateToScreen, boolean highQuality) {
@@ -375,20 +203,17 @@ public class ImageFilterDraw extends ImageFilter {
         paint.setStyle(Style.STROKE);
         paint.setColor(Color.RED);
         paint.setStrokeWidth(40);
-        boolean empty = true;
-        for (int i = 0; i < mDrawings.length; i++) {
-            empty &= mDrawings[i].empty();
-        }
-        if (empty) {
+
+        if (mParameters.getDrawing().isEmpty()) {
             return;
         }
         if (highQuality) {
-            for (int i = 0; i < mDrawings.length; i++) {
-                mDrawings[i].paintLast(0, canvas, originalRotateToScreen, highQuality);
+            for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
+                paint(strokeData, canvas, originalRotateToScreen, highQuality);
             }
-
             return;
         }
+
         if (mOverlayBitmap == null ||
                 mOverlayBitmap.getWidth() != canvas.getWidth() ||
                 mOverlayBitmap.getHeight() != canvas.getHeight()) {
@@ -397,19 +222,27 @@ public class ImageFilterDraw extends ImageFilter {
                     canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
             mCachedStrokes = 0;
         }
-        if (mCachedStrokes < mDrawings[mCurrentStyle].getNumberOfStrokes()) {
+
+        if (mCachedStrokes < mParameters.getDrawing().size()) {
             fillBuffer(originalRotateToScreen);
         }
         canvas.drawBitmap(mOverlayBitmap, 0, 0, paint);
+
+        StrokeData stroke = mParameters.getCurrentDrawing();
+        if (stroke != null) {
+            paint(stroke, canvas, originalRotateToScreen, highQuality);
+        }
     }
 
     public void fillBuffer(Matrix originalRotateToScreen) {
         Canvas drawCache = new Canvas(mOverlayBitmap);
-        for (int i = 0; i < mDrawings.length; i++) {
+        Vector<FilterDrawRepresentation.StrokeData> v = mParameters.getDrawing();
+        int n = v.size();
 
-            mCachedStrokes = mDrawings[i].paintLast(
-                mCachedStrokes, drawCache, originalRotateToScreen, false);
+        for (int i = mCachedStrokes; i < n; i++) {
+            paint(v.get(i), drawCache, originalRotateToScreen, false);
         }
+        mCachedStrokes = n;
     }
 
     @Override
@@ -431,17 +264,12 @@ public class ImageFilterDraw extends ImageFilter {
     public ImageFilter clone() throws CloneNotSupportedException {
         ImageFilterDraw filter = (ImageFilterDraw) super.clone();
 
-        filter.mDrawings = mDrawings.clone();
+        filter.mDrawingsTypes = mDrawingsTypes.clone();
         return filter;
     }
 
     public boolean isNil() {
-        for (int i = 0; i < mDrawings.length; i++) {
-            if (mDrawings[i].getNumberOfStrokes() != 0) {
-                return false;
-            }
-        }
-        return true;
+        return mParameters.getDrawing().isEmpty();
     }
 
     @Override
@@ -452,21 +280,14 @@ public class ImageFilterDraw extends ImageFilter {
         }
 
         ImageFilterDraw dfilter = (ImageFilterDraw) filter;
-        boolean same = true;
-        for (int i = 0; i < mDrawings.length; i++) {
-            same &= mDrawings[i].same(dfilter.mDrawings[i]);
-        }
-        return same;
-    }
-
-    public void clear() {
-        mDrawings[mCurrentStyle].clearCurren();
+        return mParameters.getDrawing().equals(dfilter.mParameters.getDrawing());
     }
 
     public void draw(Canvas canvas, Matrix originalRotateToScreen) {
-        for (int i = 0; i < mDrawings.length; i++) {
-            mDrawings[i].paintCurrentStroke(canvas, originalRotateToScreen, false);
+        for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
+            paint(strokeData, canvas, originalRotateToScreen, false);
         }
+        mDrawingsTypes[mCurrentStyle].paint(null, canvas, originalRotateToScreen, false);
     }
 
     @Override
@@ -482,4 +303,5 @@ public class ImageFilterDraw extends ImageFilter {
 
         return bitmap;
     }
+
 }
index 16076fd..65ccf1f 100644 (file)
@@ -5,15 +5,13 @@ import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Matrix;
-import android.graphics.Paint;
-import android.graphics.Paint.Style;
-import android.graphics.RectF;
+import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
-import android.util.Log;
 import android.view.MotionEvent;
 
+import com.android.gallery3d.filtershow.editors.EditorDraw;
+import com.android.gallery3d.filtershow.filters.FilterDrawRepresentation;
 import com.android.gallery3d.filtershow.filters.ImageFilterDraw;
-import com.android.gallery3d.filtershow.filters.RedEyeCandidate;
 
 public class ImageDraw extends ImageShow {
 
@@ -21,20 +19,36 @@ public class ImageDraw extends ImageShow {
     private int mCurrentColor = Color.RED;
     final static float INITAL_STROKE_RADIUS = 40;
     private float mCurrentSize = INITAL_STROKE_RADIUS;
+    private byte mType = 0;
+    private FilterDrawRepresentation mFRep;
+    private EditorDraw mEditorDraw;
 
     public ImageDraw(Context context, AttributeSet attrs) {
         super(context, attrs);
+        resetParameter();
     }
 
     public ImageDraw(Context context) {
         super(context);
+        resetParameter();
+    }
+
+    public void setEditor(EditorDraw editorDraw) {
+        mEditorDraw = editorDraw;
+    }
+    public void setFilterDrawRepresentation(FilterDrawRepresentation fr) {
+        mFRep = fr;
+    }
+
+    public Drawable getIcon(Context context) {
+
+        return null;
     }
 
     @Override
     public void resetParameter() {
-        ImageFilterDraw filter = (ImageFilterDraw) getCurrentFilter();
-        if (filter != null) {
-            filter.clear();
+        if (mFRep != null) {
+            mFRep.clear();
         }
     }
 
@@ -46,9 +60,12 @@ public class ImageDraw extends ImageShow {
         mCurrentSize = size;
     }
 
-    public void setStyle(char style) {
-        ImageFilterDraw filter = (ImageFilterDraw) getCurrentFilter();
-        filter.setStyle(style);
+    public void setStyle(byte style) {
+        mType = (byte) (style % ImageFilterDraw.NUMBER_OF_STYLES);
+    }
+
+    public int getStyle() {
+        return mType;
     }
 
     public int getSize() {
@@ -75,7 +92,7 @@ public class ImageDraw extends ImageShow {
             mTmpPoint[0] = event.getX();
             mTmpPoint[1] = event.getY();
             mToOrig.mapPoints(mTmpPoint);
-            filter.startSection(mCurrentColor, mCurrentSize, mTmpPoint[0], mTmpPoint[1]);
+            mFRep.startNewSection(mType, mCurrentColor, mCurrentSize, mTmpPoint[0], mTmpPoint[1]);
 
         }
         if (event.getAction() == MotionEvent.ACTION_MOVE) {
@@ -87,7 +104,7 @@ public class ImageDraw extends ImageShow {
                     mTmpPoint[0] = event.getHistoricalX(p, h);
                     mTmpPoint[1] = event.getHistoricalY(p, h);
                     mToOrig.mapPoints(mTmpPoint);
-                    filter.addPoint(mTmpPoint[0], mTmpPoint[1]);
+                    mFRep.addPoint(mTmpPoint[0], mTmpPoint[1]);
                 }
             }
         }
@@ -95,10 +112,11 @@ public class ImageDraw extends ImageShow {
             mTmpPoint[0] = event.getX();
             mTmpPoint[1] = event.getY();
             mToOrig.mapPoints(mTmpPoint);
-            filter.endSection(mTmpPoint[0], mTmpPoint[1]);
+            mFRep.endSection(mTmpPoint[0], mTmpPoint[1]);
             this.resetImageCaches(this);
 
         }
+        mEditorDraw.commitLocalRepresentation();
         invalidate();
         return true;
     }
@@ -122,11 +140,8 @@ public class ImageDraw extends ImageShow {
     @Override
     public void onDraw(Canvas canvas) {
         super.onDraw(canvas);
-        ImageFilterDraw filter = (ImageFilterDraw) getCurrentFilter();
-        if (filter != null) {
-            calcScreenMapping();
-            filter.draw(canvas, mRotateToScreen);
-        }
+        calcScreenMapping();
+
     }
 
 }
index cd7763e..7c45ee2 100644 (file)
@@ -134,6 +134,10 @@ public class MasterImage implements RenderingRequestCaller {
         mLoader = loader;
     }
 
+    public ImageLoader getImageLoader() {
+        return mLoader;
+    }
+
     public void setCurrentFilter(ImageFilter filter) {
         mCurrentFilter = filter;
     }
index 88e756a..ec2df6c 100644 (file)
@@ -35,6 +35,8 @@ import android.widget.LinearLayout;
 import android.widget.PopupMenu;
 
 import com.android.gallery3d.R;
+import com.android.gallery3d.filtershow.editors.EditorCurves;
+import com.android.gallery3d.filtershow.filters.FilterCurvesRepresentation;
 import com.android.gallery3d.filtershow.filters.FiltersManager;
 import com.android.gallery3d.filtershow.filters.ImageFilterCurves;
 import com.android.gallery3d.filtershow.imageshow.ImageShow;
@@ -58,6 +60,8 @@ public class ImageCurves extends ImageShow {
     Path gHistoPath = new Path();
 
     boolean mDoingTouchMove = false;
+    private EditorCurves mEditorCurves;
+    private FilterCurvesRepresentation mFilterCurvesRepresentation;
 
     public ImageCurves(Context context) {
         super(context);
@@ -285,9 +289,11 @@ public class ImageCurves extends ImageShow {
     }
 
     public synchronized void updateCachedImage() {
-        // update image
         if (getImagePreset() != null) {
             resetImageCaches(this);
+            if (mEditorCurves != null) {
+                mEditorCurves.commitLocalRepresentation();
+            }
             invalidate();
         }
     }
@@ -392,6 +398,15 @@ public class ImageCurves extends ImageShow {
                 break;
             }
         }
+        mEditorCurves.commitLocalRepresentation();
         invalidate();
     }
+
+    public void setEditor(EditorCurves editorCurves) {
+        mEditorCurves = editorCurves;
+    }
+
+    public void setFilterDrawRepresentation(FilterCurvesRepresentation drawRep) {
+        mFilterCurvesRepresentation = drawRep;
+    }
 }
index 8334177..2cfbd80 100644 (file)
@@ -110,6 +110,12 @@ public class Spline {
         return true;
     }
 
+    public void reset() {
+        mPoints.clear();
+        addPoint(0.0f, 1.0f);
+        addPoint(1.0f, 0.0f);
+    }
+
     private void drawHandles(Canvas canvas, Drawable indicator, float centerX, float centerY) {
         int left = (int) centerX - mCurveHandleSize / 2;
         int top = (int) centerY - mCurveHandleSize / 2;