ImageFilterDraw.java revision 4e289659f746cf659e300c9fcd2048960e115f0c
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
19db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Bitmap;
2019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport android.graphics.BitmapFactory;
21db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Canvas;
22db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Color;
23db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Matrix;
24db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Paint;
25db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport android.graphics.Paint.Style;
26915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.Path;
27915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.PathMeasure;
2819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport android.graphics.PorterDuff;
2919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport android.graphics.PorterDuffColorFilter;
30db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
31db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordimport com.android.gallery3d.R;
320c1b4c6422a4d2d9b81cc0946d1c9675440a94e2Ruben Brunkimport com.android.gallery3d.filtershow.cache.ImageLoader;
3319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport com.android.gallery3d.filtershow.filters.FilterDrawRepresentation.StrokeData;
3419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport com.android.gallery3d.filtershow.imageshow.MasterImage;
35ce9ceff5776a9b0479c30cbeb2a9388b44df1865nicolasroardimport com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
36db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
3719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport java.util.Vector;
38db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
39db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordpublic class ImageFilterDraw extends ImageFilter {
40db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    private static final String LOGTAG = "ImageFilterDraw";
4119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public final static byte SIMPLE_STYLE = 0;
42e2c4d033de164d886af9d242d8672570730580d6John Hoford    public final static byte BRUSH_STYLE_SPATTER = 1;
43e2c4d033de164d886af9d242d8672570730580d6John Hoford    public final static byte BRUSH_STYLE_MARKER = 2;
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";
53915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
54db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
5519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    DrawStyle[] mDrawingsTypes = new DrawStyle[] {
56e2c4d033de164d886af9d242d8672570730580d6John Hoford            new SimpleDraw(),
57a7836003e689af28725930ba6a2c191275876577John Hoford            new Brush(R.drawable.brush_gauss),
58e2c4d033de164d886af9d242d8672570730580d6John Hoford            new Brush(R.drawable.brush_marker),
59e2c4d033de164d886af9d242d8672570730580d6John Hoford            new Brush(R.drawable.brush_spatter)
60e2c4d033de164d886af9d242d8672570730580d6John Hoford    };
6119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    {
6219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = 0; i < mDrawingsTypes.length; i++) {
6319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mDrawingsTypes[i].setType((byte) i);
6419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
65db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
6619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
67db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
6819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
6919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public FilterRepresentation getDefaultRepresentation() {
706900cad45d240c9a54b92991538b6a33652e766cnicolasroard        return new FilterDrawRepresentation();
7119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
72db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
7319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
7419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void useRepresentation(FilterRepresentation representation) {
7519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        FilterDrawRepresentation parameters = (FilterDrawRepresentation) representation;
7619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mParameters = parameters;
7719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
78db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
7919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void setStyle(byte style) {
8019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrentStyle = style % mDrawingsTypes.length;
8119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
82915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
8319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public int getStyle() {
8419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return mCurrentStyle;
8519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
86db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
8719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public static interface DrawStyle {
8819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type);
8919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
9099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality);
9119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
92db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
9319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    class SimpleDraw implements DrawStyle {
9419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
95db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
96915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
9719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
9819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
99db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
100db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
101915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
10219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
10399baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality) {
10419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null) {
10519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                return;
106db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            }
10719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd.mPath == null) {
108db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                return;
109915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
110db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Paint paint = new Paint();
111db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
112db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setStyle(Style.STROKE);
1134e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford            paint.setAntiAlias(true);
11419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColor(sd.mColor);
11519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setStrokeWidth(toScrMatrix.mapRadius(sd.mRadius));
116db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
117e2c4d033de164d886af9d242d8672570730580d6John Hoford            // done this way because of a bug in path.transform(matrix)
118db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Path mCacheTransPath = new Path();
11919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
120db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
121db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            canvas.drawPath(mCacheTransPath, paint);
122db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
123915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
124915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
125915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    class Brush implements DrawStyle {
126e2c4d033de164d886af9d242d8672570730580d6John Hoford        int mBrushID;
12719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Bitmap mBrush;
12819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
12919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
13019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Brush(int brushID) {
13119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mBrushID = brushID;
13219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
1330c1b4c6422a4d2d9b81cc0946d1c9675440a94e2Ruben Brunk
13419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Bitmap getBrush() {
13519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (mBrush == null) {
13619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                BitmapFactory.Options opt = new BitmapFactory.Options();
13719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
13847886ac74f2874633d4c1284b91c33117f056581Ruben Brunk                mBrush = BitmapFactory.decodeResource(MasterImage.getImage().getActivity()
13947886ac74f2874633d4c1284b91c33117f056581Ruben Brunk                        .getResources(), mBrushID, opt);
14019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mBrush = mBrush.extractAlpha();
141915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
14219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            return mBrush;
143915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
144915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
145915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
14647886ac74f2874633d4c1284b91c33117f056581Ruben Brunk        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas,
14747886ac74f2874633d4c1284b91c33117f056581Ruben Brunk                Matrix toScrMatrix,
14899baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality) {
14919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null || sd.mPath == null) {
150915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                return;
151915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
152915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            Paint paint = new Paint();
153915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setStyle(Style.STROKE);
154e2c4d033de164d886af9d242d8672570730580d6John Hoford            paint.setAntiAlias(true);
15587e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford            Path mCacheTransPath = new Path();
15619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
157e2c4d033de164d886af9d242d8672570730580d6John Hoford            draw(canvas, paint, sd.mColor, toScrMatrix.mapRadius(sd.mRadius) * 2,
15887e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford                    mCacheTransPath);
159915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
160915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
161e2c4d033de164d886af9d242d8672570730580d6John Hoford        public Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
162e2c4d033de164d886af9d242d8672570730580d6John Hoford        {
163e2c4d033de164d886af9d242d8672570730580d6John Hoford            Matrix m = new Matrix();
164e2c4d033de164d886af9d242d8672570730580d6John Hoford            m.setScale(dstWidth / (float) src.getWidth(), dstHeight / (float) src.getHeight());
165e2c4d033de164d886af9d242d8672570730580d6John Hoford            Bitmap result = Bitmap.createBitmap(dstWidth, dstHeight, src.getConfig());
166e2c4d033de164d886af9d242d8672570730580d6John Hoford            Canvas canvas = new Canvas(result);
167e2c4d033de164d886af9d242d8672570730580d6John Hoford
168e2c4d033de164d886af9d242d8672570730580d6John Hoford            Paint paint = new Paint();
169e2c4d033de164d886af9d242d8672570730580d6John Hoford            paint.setFilterBitmap(filter);
170e2c4d033de164d886af9d242d8672570730580d6John Hoford            canvas.drawBitmap(src, m, paint);
171e2c4d033de164d886af9d242d8672570730580d6John Hoford
172e2c4d033de164d886af9d242d8672570730580d6John Hoford            return result;
173e2c4d033de164d886af9d242d8672570730580d6John Hoford
174e2c4d033de164d886af9d242d8672570730580d6John Hoford        }
175915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        void draw(Canvas canvas, Paint paint, int color, float size, Path path) {
17619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            PathMeasure mPathMeasure = new PathMeasure();
17719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mPosition = new float[2];
17819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mTan = new float[2];
179915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
180915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            mPathMeasure.setPath(path, false);
18119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
18219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setAntiAlias(true);
183915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setColor(color);
184e2c4d033de164d886af9d242d8672570730580d6John Hoford
18519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
186e2c4d033de164d886af9d242d8672570730580d6John Hoford            Bitmap brush;
187e2c4d033de164d886af9d242d8672570730580d6John Hoford            // done this way because of a bug in
188e2c4d033de164d886af9d242d8672570730580d6John Hoford            // Bitmap.createScaledBitmap(getBrush(),(int) size,(int) size,true);
189e2c4d033de164d886af9d242d8672570730580d6John Hoford            brush = createScaledBitmap(getBrush(), (int) size, (int) size, true);
190915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            float len = mPathMeasure.getLength();
19119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float s2 = size / 2;
192e2c4d033de164d886af9d242d8672570730580d6John Hoford            float step = s2 / 8;
19319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (float i = 0; i < len; i += step) {
19419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mPathMeasure.getPosTan(i, mPosition, mTan);
19519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                //                canvas.drawCircle(pos[0], pos[1], size, paint);
19619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                canvas.drawBitmap(brush, mPosition[0] - s2, mPosition[1] - s2, paint);
197915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
198915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
199915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
200915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
20119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
20219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
203db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
204db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
205db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
20619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
20799baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            int quality) {
20899baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        mDrawingsTypes[sd.mType].paint(sd, canvas, toScrMatrix, quality);
209db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
210db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
21199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public void drawData(Canvas canvas, Matrix originalRotateToScreen, int quality) {
212db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Paint paint = new Paint();
21336e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard        if (quality == FilterEnvironment.QUALITY_FINAL) {
214db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setAntiAlias(true);
215db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
216db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStyle(Style.STROKE);
217db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setColor(Color.RED);
218db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStrokeWidth(40);
21919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
220e2c4d033de164d886af9d242d8672570730580d6John Hoford        if (mParameters.getDrawing().isEmpty() && mParameters.getCurrentDrawing() == null) {
221db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
222db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
22336e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard        if (quality == FilterEnvironment.QUALITY_FINAL) {
22419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
22599baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                paint(strokeData, canvas, originalRotateToScreen, quality);
226915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
227db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
228db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
22919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
230db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        if (mOverlayBitmap == null ||
231db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                mOverlayBitmap.getWidth() != canvas.getWidth() ||
232573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                mOverlayBitmap.getHeight() != canvas.getHeight() ||
233573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                mParameters.getDrawing().size() < mCachedStrokes) {
234db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
235db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mOverlayBitmap = Bitmap.createBitmap(
236db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                    canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
237db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mCachedStrokes = 0;
238db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
23919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
24019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (mCachedStrokes < mParameters.getDrawing().size()) {
241db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            fillBuffer(originalRotateToScreen);
242db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
243db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        canvas.drawBitmap(mOverlayBitmap, 0, 0, paint);
24419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
24519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        StrokeData stroke = mParameters.getCurrentDrawing();
24619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (stroke != null) {
24799baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            paint(stroke, canvas, originalRotateToScreen, quality);
24819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
249db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
250db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
251db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void fillBuffer(Matrix originalRotateToScreen) {
252db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Canvas drawCache = new Canvas(mOverlayBitmap);
25319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Vector<FilterDrawRepresentation.StrokeData> v = mParameters.getDrawing();
25419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        int n = v.size();
255db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
25619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = mCachedStrokes; i < n; i++) {
25736e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard            paint(v.get(i), drawCache, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
258915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
25919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCachedStrokes = n;
260db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
261db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
262db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void draw(Canvas canvas, Matrix originalRotateToScreen) {
26319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
26436e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard            paint(strokeData, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
265915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
26699baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        mDrawingsTypes[mCurrentStyle].paint(
26736e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard                null, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
268db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
269db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
270db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
27199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
272db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int w = bitmap.getWidth();
273db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int h = bitmap.getHeight();
27412017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford
27512017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        Matrix m = getOriginalToScreenMatrix(w, h);
27699baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        drawData(new Canvas(bitmap), m, quality);
277db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        return bitmap;
278db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
27919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
280db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford}
281