ImageFilterDraw.java revision 36e567afff815bc821c2859ebdeec86b1fca1ef6
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;
3436e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroardimport com.android.gallery3d.filtershow.presets.FilterEnvironment;
3599baf61387ab1ef15bb9db5fa3b2b55591e87059John Hofordimport com.android.gallery3d.filtershow.presets.ImagePreset;
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(),
57e2c4d033de164d886af9d242d8672570730580d6John Hoford            new Brush(R.drawable.brush_marker),
58e2c4d033de164d886af9d242d8672570730580d6John Hoford            new Brush(R.drawable.brush_spatter)
59e2c4d033de164d886af9d242d8672570730580d6John Hoford    };
6019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    {
6119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = 0; i < mDrawingsTypes.length; i++) {
6219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mDrawingsTypes[i].setType((byte) i);
6319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
64db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
6519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
66db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
6719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
6819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public FilterRepresentation getDefaultRepresentation() {
696900cad45d240c9a54b92991538b6a33652e766cnicolasroard        return new FilterDrawRepresentation();
7019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
71db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
7219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
7319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void useRepresentation(FilterRepresentation representation) {
7419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        FilterDrawRepresentation parameters = (FilterDrawRepresentation) representation;
7519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mParameters = parameters;
7619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
77db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
7819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void setStyle(byte style) {
7919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrentStyle = style % mDrawingsTypes.length;
8019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
81915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
8219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public int getStyle() {
8319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return mCurrentStyle;
8419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
85db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
8619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public static interface DrawStyle {
8719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type);
8819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
8999baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality);
9019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
91db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
9219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    class SimpleDraw implements DrawStyle {
9319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
94db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
95915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
9619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
9719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
98db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
99db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
100915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
10119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
10299baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality) {
10319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null) {
10419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                return;
105db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            }
10619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd.mPath == null) {
107db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                return;
108915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
109db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Paint paint = new Paint();
110db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
111db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setStyle(Style.STROKE);
11219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColor(sd.mColor);
11319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setStrokeWidth(toScrMatrix.mapRadius(sd.mRadius));
114db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
115e2c4d033de164d886af9d242d8672570730580d6John Hoford            // done this way because of a bug in path.transform(matrix)
116db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Path mCacheTransPath = new Path();
11719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
118db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
119db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            canvas.drawPath(mCacheTransPath, paint);
120db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
121915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
122915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
123915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    class Brush implements DrawStyle {
124e2c4d033de164d886af9d242d8672570730580d6John Hoford        int mBrushID;
12519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Bitmap mBrush;
12619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
12719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
12819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Brush(int brushID) {
12919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mBrushID = brushID;
13019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
13119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Bitmap getBrush() {
13219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (mBrush == null) {
13319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                BitmapFactory.Options opt = new BitmapFactory.Options();
13419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
13519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mBrush = MasterImage.getImage().getImageLoader().decodeImage(mBrushID, opt);
13619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mBrush = mBrush.extractAlpha();
137915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
13819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            return mBrush;
139915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
140915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
141915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
14219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
14399baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality) {
14419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null || sd.mPath == null) {
145915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                return;
146915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
147915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            Paint paint = new Paint();
148915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setStyle(Style.STROKE);
149e2c4d033de164d886af9d242d8672570730580d6John Hoford            paint.setAntiAlias(true);
15087e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford            Path mCacheTransPath = new Path();
15119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
152e2c4d033de164d886af9d242d8672570730580d6John Hoford            draw(canvas, paint, sd.mColor, toScrMatrix.mapRadius(sd.mRadius) * 2,
15387e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford                    mCacheTransPath);
154915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
155915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
156e2c4d033de164d886af9d242d8672570730580d6John Hoford        public Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
157e2c4d033de164d886af9d242d8672570730580d6John Hoford        {
158e2c4d033de164d886af9d242d8672570730580d6John Hoford            Matrix m = new Matrix();
159e2c4d033de164d886af9d242d8672570730580d6John Hoford            m.setScale(dstWidth / (float) src.getWidth(), dstHeight / (float) src.getHeight());
160e2c4d033de164d886af9d242d8672570730580d6John Hoford            Bitmap result = Bitmap.createBitmap(dstWidth, dstHeight, src.getConfig());
161e2c4d033de164d886af9d242d8672570730580d6John Hoford            Canvas canvas = new Canvas(result);
162e2c4d033de164d886af9d242d8672570730580d6John Hoford
163e2c4d033de164d886af9d242d8672570730580d6John Hoford            Paint paint = new Paint();
164e2c4d033de164d886af9d242d8672570730580d6John Hoford            paint.setFilterBitmap(filter);
165e2c4d033de164d886af9d242d8672570730580d6John Hoford            canvas.drawBitmap(src, m, paint);
166e2c4d033de164d886af9d242d8672570730580d6John Hoford
167e2c4d033de164d886af9d242d8672570730580d6John Hoford            return result;
168e2c4d033de164d886af9d242d8672570730580d6John Hoford
169e2c4d033de164d886af9d242d8672570730580d6John Hoford        }
170915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        void draw(Canvas canvas, Paint paint, int color, float size, Path path) {
17119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            PathMeasure mPathMeasure = new PathMeasure();
17219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mPosition = new float[2];
17319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mTan = new float[2];
174915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
175915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            mPathMeasure.setPath(path, false);
17619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
17719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setAntiAlias(true);
178915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setColor(color);
179e2c4d033de164d886af9d242d8672570730580d6John Hoford
18019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
181e2c4d033de164d886af9d242d8672570730580d6John Hoford            Bitmap brush;
182e2c4d033de164d886af9d242d8672570730580d6John Hoford            // done this way because of a bug in
183e2c4d033de164d886af9d242d8672570730580d6John Hoford            // Bitmap.createScaledBitmap(getBrush(),(int) size,(int) size,true);
184e2c4d033de164d886af9d242d8672570730580d6John Hoford            brush = createScaledBitmap(getBrush(), (int) size, (int) size, true);
185915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            float len = mPathMeasure.getLength();
18619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float s2 = size / 2;
187e2c4d033de164d886af9d242d8672570730580d6John Hoford            float step = s2 / 8;
18819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (float i = 0; i < len; i += step) {
18919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mPathMeasure.getPosTan(i, mPosition, mTan);
19019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                //                canvas.drawCircle(pos[0], pos[1], size, paint);
19119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                canvas.drawBitmap(brush, mPosition[0] - s2, mPosition[1] - s2, paint);
192915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
193915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
194915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
195915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
19619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
19719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
198db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
199db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
200db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
20119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
20299baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            int quality) {
20399baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        mDrawingsTypes[sd.mType].paint(sd, canvas, toScrMatrix, quality);
204db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
205db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
20699baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public void drawData(Canvas canvas, Matrix originalRotateToScreen, int quality) {
207db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Paint paint = new Paint();
20836e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard        if (quality == FilterEnvironment.QUALITY_FINAL) {
209db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setAntiAlias(true);
210db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
211db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStyle(Style.STROKE);
212db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setColor(Color.RED);
213db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStrokeWidth(40);
21419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
215e2c4d033de164d886af9d242d8672570730580d6John Hoford        if (mParameters.getDrawing().isEmpty() && mParameters.getCurrentDrawing() == null) {
216db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
217db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
21836e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard        if (quality == FilterEnvironment.QUALITY_FINAL) {
21919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
22099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                paint(strokeData, canvas, originalRotateToScreen, quality);
221915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
222db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
223db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
22419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
225db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        if (mOverlayBitmap == null ||
226db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                mOverlayBitmap.getWidth() != canvas.getWidth() ||
227573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                mOverlayBitmap.getHeight() != canvas.getHeight() ||
228573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                mParameters.getDrawing().size() < mCachedStrokes) {
229db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
230db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mOverlayBitmap = Bitmap.createBitmap(
231db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                    canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
232db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mCachedStrokes = 0;
233db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
23419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
23519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (mCachedStrokes < mParameters.getDrawing().size()) {
236db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            fillBuffer(originalRotateToScreen);
237db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
238db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        canvas.drawBitmap(mOverlayBitmap, 0, 0, paint);
23919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
24019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        StrokeData stroke = mParameters.getCurrentDrawing();
24119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (stroke != null) {
24299baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            paint(stroke, canvas, originalRotateToScreen, quality);
24319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
244db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
245db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
246db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void fillBuffer(Matrix originalRotateToScreen) {
247db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Canvas drawCache = new Canvas(mOverlayBitmap);
24819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Vector<FilterDrawRepresentation.StrokeData> v = mParameters.getDrawing();
24919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        int n = v.size();
250db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
25119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = mCachedStrokes; i < n; i++) {
25236e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard            paint(v.get(i), drawCache, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
253915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
25419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCachedStrokes = n;
255db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
256db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
257db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void draw(Canvas canvas, Matrix originalRotateToScreen) {
25819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
25936e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard            paint(strokeData, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
260915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
26199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        mDrawingsTypes[mCurrentStyle].paint(
26236e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard                null, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
263db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
264db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
265db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
26699baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
267db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int w = bitmap.getWidth();
268db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int h = bitmap.getHeight();
26912017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford
27012017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        Matrix m = getOriginalToScreenMatrix(w, h);
27199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        drawData(new Canvas(bitmap), m, quality);
272db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        return bitmap;
273db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
27419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
275db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford}
276