ImageFilterDraw.java revision ce9ceff5776a9b0479c30cbeb2a9388b44df1865
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;
3219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport com.android.gallery3d.filtershow.filters.FilterDrawRepresentation.StrokeData;
3319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport com.android.gallery3d.filtershow.imageshow.MasterImage;
34ce9ceff5776a9b0479c30cbeb2a9388b44df1865nicolasroardimport com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
35db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
3619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport java.util.Vector;
37db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
38db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hofordpublic class ImageFilterDraw extends ImageFilter {
39db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    private static final String LOGTAG = "ImageFilterDraw";
4019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public final static byte SIMPLE_STYLE = 0;
41e2c4d033de164d886af9d242d8672570730580d6John Hoford    public final static byte BRUSH_STYLE_SPATTER = 1;
42e2c4d033de164d886af9d242d8672570730580d6John Hoford    public final static byte BRUSH_STYLE_MARKER = 2;
4319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public final static int NUMBER_OF_STYLES = 3;
44db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    Bitmap mOverlayBitmap; // this accelerates interaction
45915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    int mCachedStrokes = -1;
46915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    int mCurrentStyle = 0;
47915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
4819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    FilterDrawRepresentation mParameters = new FilterDrawRepresentation();
4919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
5019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public ImageFilterDraw() {
5119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mName = "Image Draw";
52915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
53db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
5419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    DrawStyle[] mDrawingsTypes = new DrawStyle[] {
55e2c4d033de164d886af9d242d8672570730580d6John Hoford            new SimpleDraw(),
56e2c4d033de164d886af9d242d8672570730580d6John Hoford            new Brush(R.drawable.brush_marker),
57e2c4d033de164d886af9d242d8672570730580d6John Hoford            new Brush(R.drawable.brush_spatter)
58e2c4d033de164d886af9d242d8672570730580d6John Hoford    };
5919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    {
6019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = 0; i < mDrawingsTypes.length; i++) {
6119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mDrawingsTypes[i].setType((byte) i);
6219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
63db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
6419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
65db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
6619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
6719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public FilterRepresentation getDefaultRepresentation() {
686900cad45d240c9a54b92991538b6a33652e766cnicolasroard        return new FilterDrawRepresentation();
6919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
70db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
7119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
7219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void useRepresentation(FilterRepresentation representation) {
7319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        FilterDrawRepresentation parameters = (FilterDrawRepresentation) representation;
7419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mParameters = parameters;
7519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
76db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
7719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void setStyle(byte style) {
7819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrentStyle = style % mDrawingsTypes.length;
7919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
80915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
8119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public int getStyle() {
8219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return mCurrentStyle;
8319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
84db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
8519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public static interface DrawStyle {
8619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type);
8719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
8899baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality);
8919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
90db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
9119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    class SimpleDraw implements DrawStyle {
9219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
93db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
94915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
9519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
9619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
97db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
98db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
99915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
10019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
10199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality) {
10219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null) {
10319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                return;
104db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            }
10519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd.mPath == null) {
106db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                return;
107915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
108db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Paint paint = new Paint();
109db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
110db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setStyle(Style.STROKE);
11119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColor(sd.mColor);
11219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setStrokeWidth(toScrMatrix.mapRadius(sd.mRadius));
113db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
114e2c4d033de164d886af9d242d8672570730580d6John Hoford            // done this way because of a bug in path.transform(matrix)
115db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Path mCacheTransPath = new Path();
11619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
117db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
118db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            canvas.drawPath(mCacheTransPath, paint);
119db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
120915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
121915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
122915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    class Brush implements DrawStyle {
123e2c4d033de164d886af9d242d8672570730580d6John Hoford        int mBrushID;
12419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Bitmap mBrush;
12519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
12619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
12719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Brush(int brushID) {
12819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mBrushID = brushID;
12919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
13019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Bitmap getBrush() {
13119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (mBrush == null) {
13219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                BitmapFactory.Options opt = new BitmapFactory.Options();
13319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
13419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mBrush = MasterImage.getImage().getImageLoader().decodeImage(mBrushID, opt);
13519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mBrush = mBrush.extractAlpha();
136915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
13719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            return mBrush;
138915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
139915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
140915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
14119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
14299baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality) {
14319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null || sd.mPath == null) {
144915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                return;
145915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
146915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            Paint paint = new Paint();
147915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setStyle(Style.STROKE);
148e2c4d033de164d886af9d242d8672570730580d6John Hoford            paint.setAntiAlias(true);
14987e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford            Path mCacheTransPath = new Path();
15019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
151e2c4d033de164d886af9d242d8672570730580d6John Hoford            draw(canvas, paint, sd.mColor, toScrMatrix.mapRadius(sd.mRadius) * 2,
15287e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford                    mCacheTransPath);
153915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
154915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
155e2c4d033de164d886af9d242d8672570730580d6John Hoford        public Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
156e2c4d033de164d886af9d242d8672570730580d6John Hoford        {
157e2c4d033de164d886af9d242d8672570730580d6John Hoford            Matrix m = new Matrix();
158e2c4d033de164d886af9d242d8672570730580d6John Hoford            m.setScale(dstWidth / (float) src.getWidth(), dstHeight / (float) src.getHeight());
159e2c4d033de164d886af9d242d8672570730580d6John Hoford            Bitmap result = Bitmap.createBitmap(dstWidth, dstHeight, src.getConfig());
160e2c4d033de164d886af9d242d8672570730580d6John Hoford            Canvas canvas = new Canvas(result);
161e2c4d033de164d886af9d242d8672570730580d6John Hoford
162e2c4d033de164d886af9d242d8672570730580d6John Hoford            Paint paint = new Paint();
163e2c4d033de164d886af9d242d8672570730580d6John Hoford            paint.setFilterBitmap(filter);
164e2c4d033de164d886af9d242d8672570730580d6John Hoford            canvas.drawBitmap(src, m, paint);
165e2c4d033de164d886af9d242d8672570730580d6John Hoford
166e2c4d033de164d886af9d242d8672570730580d6John Hoford            return result;
167e2c4d033de164d886af9d242d8672570730580d6John Hoford
168e2c4d033de164d886af9d242d8672570730580d6John Hoford        }
169915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        void draw(Canvas canvas, Paint paint, int color, float size, Path path) {
17019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            PathMeasure mPathMeasure = new PathMeasure();
17119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mPosition = new float[2];
17219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mTan = new float[2];
173915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
174915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            mPathMeasure.setPath(path, false);
17519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
17619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setAntiAlias(true);
177915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setColor(color);
178e2c4d033de164d886af9d242d8672570730580d6John Hoford
17919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
180e2c4d033de164d886af9d242d8672570730580d6John Hoford            Bitmap brush;
181e2c4d033de164d886af9d242d8672570730580d6John Hoford            // done this way because of a bug in
182e2c4d033de164d886af9d242d8672570730580d6John Hoford            // Bitmap.createScaledBitmap(getBrush(),(int) size,(int) size,true);
183e2c4d033de164d886af9d242d8672570730580d6John Hoford            brush = createScaledBitmap(getBrush(), (int) size, (int) size, true);
184915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            float len = mPathMeasure.getLength();
18519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float s2 = size / 2;
186e2c4d033de164d886af9d242d8672570730580d6John Hoford            float step = s2 / 8;
18719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (float i = 0; i < len; i += step) {
18819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mPathMeasure.getPosTan(i, mPosition, mTan);
18919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                //                canvas.drawCircle(pos[0], pos[1], size, paint);
19019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                canvas.drawBitmap(brush, mPosition[0] - s2, mPosition[1] - s2, paint);
191915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
192915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
193915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
194915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
19519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
19619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
197db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
198db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
199db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
20019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
20199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            int quality) {
20299baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        mDrawingsTypes[sd.mType].paint(sd, canvas, toScrMatrix, quality);
203db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
204db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
20599baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public void drawData(Canvas canvas, Matrix originalRotateToScreen, int quality) {
206db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Paint paint = new Paint();
20736e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard        if (quality == FilterEnvironment.QUALITY_FINAL) {
208db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setAntiAlias(true);
209db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
210db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStyle(Style.STROKE);
211db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setColor(Color.RED);
212db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStrokeWidth(40);
21319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
214e2c4d033de164d886af9d242d8672570730580d6John Hoford        if (mParameters.getDrawing().isEmpty() && mParameters.getCurrentDrawing() == null) {
215db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
216db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
21736e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard        if (quality == FilterEnvironment.QUALITY_FINAL) {
21819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
21999baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                paint(strokeData, canvas, originalRotateToScreen, quality);
220915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
221db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
222db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
22319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
224db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        if (mOverlayBitmap == null ||
225db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                mOverlayBitmap.getWidth() != canvas.getWidth() ||
226573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                mOverlayBitmap.getHeight() != canvas.getHeight() ||
227573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                mParameters.getDrawing().size() < mCachedStrokes) {
228db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
229db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mOverlayBitmap = Bitmap.createBitmap(
230db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                    canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
231db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mCachedStrokes = 0;
232db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
23319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
23419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (mCachedStrokes < mParameters.getDrawing().size()) {
235db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            fillBuffer(originalRotateToScreen);
236db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
237db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        canvas.drawBitmap(mOverlayBitmap, 0, 0, paint);
23819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
23919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        StrokeData stroke = mParameters.getCurrentDrawing();
24019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (stroke != null) {
24199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            paint(stroke, canvas, originalRotateToScreen, quality);
24219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
243db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
244db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
245db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void fillBuffer(Matrix originalRotateToScreen) {
246db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Canvas drawCache = new Canvas(mOverlayBitmap);
24719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Vector<FilterDrawRepresentation.StrokeData> v = mParameters.getDrawing();
24819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        int n = v.size();
249db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
25019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = mCachedStrokes; i < n; i++) {
25136e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard            paint(v.get(i), drawCache, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
252915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
25319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCachedStrokes = n;
254db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
255db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
256db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void draw(Canvas canvas, Matrix originalRotateToScreen) {
25719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
25836e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard            paint(strokeData, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
259915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
26099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        mDrawingsTypes[mCurrentStyle].paint(
26136e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard                null, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
262db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
263db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
264db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
26599baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
266db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int w = bitmap.getWidth();
267db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int h = bitmap.getHeight();
26812017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford
26912017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        Matrix m = getOriginalToScreenMatrix(w, h);
27099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        drawData(new Canvas(bitmap), m, quality);
271db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        return bitmap;
272db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
27319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
274db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford}
275