ImageFilterDraw.java revision 19161944e0efb1ffe23274d7cf5315ce047e9dac
1db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford/*
2db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford * Copyright (C) 2012 The Android Open Source Project
3db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford *
4db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
5db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford * you may not use this file except in compliance with the License.
6db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford * You may obtain a copy of the License at
7db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford *
8db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
9db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford *
10db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford * Unless required by applicable law or agreed to in writing, software
11db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
12db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford * See the License for the specific language governing permissions and
14db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford * limitations under the License.
15db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford */
16db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
17db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordpackage com.android.gallery3d.filtershow.filters;
18db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
1919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport android.content.Context;
20db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Bitmap;
2119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport android.graphics.BitmapFactory;
22db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Canvas;
23db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Color;
24db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Matrix;
25db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Paint;
26db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Paint.Style;
27915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.Path;
28915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.PathMeasure;
2919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport android.graphics.PorterDuff;
3019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport android.graphics.PorterDuffColorFilter;
31db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
32db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport com.android.gallery3d.R;
3319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport com.android.gallery3d.filtershow.FilterShowActivity;
34915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport com.android.gallery3d.filtershow.editors.EditorDraw;
3519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport com.android.gallery3d.filtershow.filters.FilterDrawRepresentation.StrokeData;
3619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport com.android.gallery3d.filtershow.imageshow.MasterImage;
37db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
3819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport java.util.Vector;
39db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
40db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordpublic class ImageFilterDraw extends ImageFilter {
41db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    private static final String LOGTAG = "ImageFilterDraw";
4219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public final static byte SIMPLE_STYLE = 0;
4319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public final static byte BRUSH_STYLE = 1;
4419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public final static int NUMBER_OF_STYLES = 3;
45db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    Bitmap mOverlayBitmap; // this accelerates interaction
46915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    int mCachedStrokes = -1;
47915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    int mCurrentStyle = 0;
48915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
4919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    FilterDrawRepresentation mParameters = new FilterDrawRepresentation();
5019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
5119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public ImageFilterDraw() {
5219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mName = "Image Draw";
5319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        setFilterType(TYPE_VIGNETTE);
54915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
55db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
5619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    DrawStyle[] mDrawingsTypes = new DrawStyle[] {
5719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            new SimpleDraw(), new Brush(R.drawable.brush1), new Brush(R.drawable.brush2) };
5819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    {
5919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = 0; i < mDrawingsTypes.length; i++) {
6019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mDrawingsTypes[i].setType((byte) i);
6119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
62db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
6319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
64db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
6519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
6619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public boolean hasDefaultRepresentation() {
6719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return true;
6819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
6919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
7019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public FilterRepresentation getDefaultRepresentation() {
7119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        FilterDrawRepresentation representation = new FilterDrawRepresentation();
7219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        representation.setName("Draw");
7319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        representation.setFilterClass(ImageFilterDraw.class);
7419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return representation;
7519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
76db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
7719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
7819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void useRepresentation(FilterRepresentation representation) {
7919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        FilterDrawRepresentation parameters = (FilterDrawRepresentation) representation;
8019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mParameters = parameters;
8119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
82db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
8319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void setStyle(byte style) {
8419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrentStyle = style % mDrawingsTypes.length;
8519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
86915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
8719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public int getStyle() {
8819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return mCurrentStyle;
8919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
90db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
9119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public static interface DrawStyle {
9219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type);
9319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
9419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                boolean highQuality);
9519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
96db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
9719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    class SimpleDraw implements DrawStyle {
9819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
99db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
100915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
10119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
10219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
103db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
104db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
105915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
10619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
10719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                boolean highQuality) {
10819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null) {
10919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                return;
110db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            }
11119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd.mPath == null) {
112db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                return;
113915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
114db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Paint paint = new Paint();
115db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
116db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setStyle(Style.STROKE);
11719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColor(sd.mColor);
11819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setStrokeWidth(toScrMatrix.mapRadius(sd.mRadius));
119db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
12019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            // don this way because of a bug in path.transform(matrix)
121db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Path mCacheTransPath = new Path();
12219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
123db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
124db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            canvas.drawPath(mCacheTransPath, paint);
125db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
126915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
127915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
128915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    class Brush implements DrawStyle {
12919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        int mBrushID = R.drawable.brush2;
13019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Bitmap mBrush;
13119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
13219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
13319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Brush(int brushID) {
13419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mBrushID = brushID;
13519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
13619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Bitmap getBrush() {
13719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (mBrush == null) {
13819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                BitmapFactory.Options opt = new BitmapFactory.Options();
13919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
14019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mBrush = MasterImage.getImage().getImageLoader().decodeImage(mBrushID, opt);
14119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mBrush = mBrush.extractAlpha();
142915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
14319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            return mBrush;
144915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
145915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
146915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
14719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
14819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                boolean highQuality) {
14919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null) {
15019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                return;
151915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
152915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
15319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null || sd.mPath == null) {
154915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                return;
155915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
156915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            Paint paint = new Paint();
157915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setStyle(Style.STROKE);
158915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
159915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            float scale = toScrMatrix.mapRadius(1);
16087e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford            Path mCacheTransPath = new Path();
16119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
16219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            draw(canvas, paint, sd.mColor, toScrMatrix.mapRadius(sd.mRadius),
16387e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford                    mCacheTransPath);
164915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
165915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
166915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        void draw(Canvas canvas, Paint paint, int color, float size, Path path) {
16719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            PathMeasure mPathMeasure = new PathMeasure();
16819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mPosition = new float[2];
16919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mTan = new float[2];
170915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
171915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            mPathMeasure.setPath(path, false);
17219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
17319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setAntiAlias(true);
174915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setColor(color);
17519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
17619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            Bitmap brush = Bitmap.createScaledBitmap(getBrush(), (int) size, (int) size, true);
177915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            float len = mPathMeasure.getLength();
17819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float s2 = size / 2;
17919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float step = s2 / 6;
18019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (float i = 0; i < len; i += step) {
18119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mPathMeasure.getPosTan(i, mPosition, mTan);
18219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                //                canvas.drawCircle(pos[0], pos[1], size, paint);
18319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                canvas.drawBitmap(brush, mPosition[0] - s2, mPosition[1] - s2, paint);
184915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
185915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
186915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
187915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
18819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
18919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
190db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
191db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
192db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
19319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
19419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            boolean highQuality) {
19519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mDrawingsTypes[sd.mType].paint(sd, canvas, toScrMatrix, highQuality);
196db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
197db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
198db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void drawData(Canvas canvas, Matrix originalRotateToScreen, boolean highQuality) {
199db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Paint paint = new Paint();
200db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        if (highQuality) {
201db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setAntiAlias(true);
202db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
203db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStyle(Style.STROKE);
204db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setColor(Color.RED);
205db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStrokeWidth(40);
20619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
20719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (mParameters.getDrawing().isEmpty()) {
208db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
209db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
210db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        if (highQuality) {
21119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
21219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                paint(strokeData, canvas, originalRotateToScreen, highQuality);
213915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
214db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
215db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
21619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
217db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        if (mOverlayBitmap == null ||
218db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                mOverlayBitmap.getWidth() != canvas.getWidth() ||
219db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                mOverlayBitmap.getHeight() != canvas.getHeight()) {
220db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
221db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mOverlayBitmap = Bitmap.createBitmap(
222db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                    canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
223db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mCachedStrokes = 0;
224db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
22519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
22619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (mCachedStrokes < mParameters.getDrawing().size()) {
227db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            fillBuffer(originalRotateToScreen);
228db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
229db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        canvas.drawBitmap(mOverlayBitmap, 0, 0, paint);
23019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
23119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        StrokeData stroke = mParameters.getCurrentDrawing();
23219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (stroke != null) {
23319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint(stroke, canvas, originalRotateToScreen, highQuality);
23419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
235db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
236db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
237db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void fillBuffer(Matrix originalRotateToScreen) {
238db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Canvas drawCache = new Canvas(mOverlayBitmap);
23919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Vector<FilterDrawRepresentation.StrokeData> v = mParameters.getDrawing();
24019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        int n = v.size();
241db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
24219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = mCachedStrokes; i < n; i++) {
24319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint(v.get(i), drawCache, originalRotateToScreen, false);
244915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
24519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCachedStrokes = n;
246db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
247db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
248db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
249db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public int getButtonId() {
250db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        return R.id.drawOnImageButton;
251db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
252db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
253db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
254db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public int getTextId() {
255db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        return R.string.imageDraw;
256db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
257db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
258db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
259db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public int getEditingViewId() {
260915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        return EditorDraw.ID;
261db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
262db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
263db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
264db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public ImageFilter clone() throws CloneNotSupportedException {
265db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        ImageFilterDraw filter = (ImageFilterDraw) super.clone();
266db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
26719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        filter.mDrawingsTypes = mDrawingsTypes.clone();
268db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        return filter;
269db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
270db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
271db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public boolean isNil() {
27219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return mParameters.getDrawing().isEmpty();
273db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
274db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
275db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
276db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public boolean same(ImageFilter filter) {
277db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        boolean isSuperSame = super.same(filter);
278db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        if (!isSuperSame || !(filter instanceof ImageFilterDraw)) {
279db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return false;
280db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
281db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
282db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        ImageFilterDraw dfilter = (ImageFilterDraw) filter;
28319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return mParameters.getDrawing().equals(dfilter.mParameters.getDrawing());
284db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
285db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
286db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void draw(Canvas canvas, Matrix originalRotateToScreen) {
28719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
28819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint(strokeData, canvas, originalRotateToScreen, false);
289915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
29019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mDrawingsTypes[mCurrentStyle].paint(null, canvas, originalRotateToScreen, false);
291db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
292db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
293db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
294db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
295db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int w = bitmap.getWidth();
296db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int h = bitmap.getHeight();
297db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        short[] rect = new short[4];
298db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
299db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Matrix m = new Matrix();
300db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        m.setScale(scaleFactor, scaleFactor);
301db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
302db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        drawData(new Canvas(bitmap), m, highQuality);
303db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
304db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        return bitmap;
305db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
30619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
307db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford}
308