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;
32a4f38c0f8f70c998391780b672eea5d5e49277c7John Hofordimport com.android.gallery3d.app.Log;
330c1b4c6422a4d2d9b81cc0946d1c9675440a94e2Ruben Brunkimport com.android.gallery3d.filtershow.cache.ImageLoader;
3419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport com.android.gallery3d.filtershow.filters.FilterDrawRepresentation.StrokeData;
3519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport com.android.gallery3d.filtershow.imageshow.MasterImage;
36ce9ceff5776a9b0479c30cbeb2a9388b44df1865nicolasroardimport com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
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;
43e2c4d033de164d886af9d242d8672570730580d6John Hoford    public final static byte BRUSH_STYLE_SPATTER = 1;
44e2c4d033de164d886af9d242d8672570730580d6John Hoford    public final static byte BRUSH_STYLE_MARKER = 2;
4519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public final static int NUMBER_OF_STYLES = 3;
46db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    Bitmap mOverlayBitmap; // this accelerates interaction
47915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    int mCachedStrokes = -1;
48915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    int mCurrentStyle = 0;
49915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
5019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    FilterDrawRepresentation mParameters = new FilterDrawRepresentation();
5119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
5219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public ImageFilterDraw() {
5319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mName = "Image Draw";
54915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
55db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
5619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    DrawStyle[] mDrawingsTypes = new DrawStyle[] {
57d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford            new SimpleDraw(0),
58d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford            new SimpleDraw(1),
59a7836003e689af28725930ba6a2c191275876577John Hoford            new Brush(R.drawable.brush_gauss),
60e2c4d033de164d886af9d242d8672570730580d6John Hoford            new Brush(R.drawable.brush_marker),
61e2c4d033de164d886af9d242d8672570730580d6John Hoford            new Brush(R.drawable.brush_spatter)
62e2c4d033de164d886af9d242d8672570730580d6John Hoford    };
6319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    {
6419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = 0; i < mDrawingsTypes.length; i++) {
6519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mDrawingsTypes[i].setType((byte) i);
6619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
67db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
6819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
69db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
7019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
7119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public FilterRepresentation getDefaultRepresentation() {
726900cad45d240c9a54b92991538b6a33652e766cnicolasroard        return new FilterDrawRepresentation();
7319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
74db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
7519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
7619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void useRepresentation(FilterRepresentation representation) {
7719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        FilterDrawRepresentation parameters = (FilterDrawRepresentation) representation;
7819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mParameters = parameters;
7919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
80db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
8119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void setStyle(byte style) {
8219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrentStyle = style % mDrawingsTypes.length;
8319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
84915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
8519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public int getStyle() {
8619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return mCurrentStyle;
8719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
88db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
8919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public static interface DrawStyle {
9019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type);
9119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
9299baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality);
9319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
94db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
9519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    class SimpleDraw implements DrawStyle {
9619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
97d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford        int mMode;
98d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford
99d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford        public SimpleDraw(int mode) {
100d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford            mMode = mode;
101d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford        }
102db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
103915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
10419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
10519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
106db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
107db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
108915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
10919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
11099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality) {
11119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null) {
11219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                return;
113db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            }
11419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd.mPath == null) {
115db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                return;
116915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
117db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Paint paint = new Paint();
118db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
119db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setStyle(Style.STROKE);
120d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford            if (mMode == 0) {
121d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford                paint.setStrokeCap(Paint.Cap.SQUARE);
122d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford            } else {
123d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford                paint.setStrokeCap(Paint.Cap.ROUND);
124d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford            }
1254e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford            paint.setAntiAlias(true);
12619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColor(sd.mColor);
12719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setStrokeWidth(toScrMatrix.mapRadius(sd.mRadius));
128db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
129e2c4d033de164d886af9d242d8672570730580d6John Hoford            // done this way because of a bug in path.transform(matrix)
130db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            Path mCacheTransPath = new Path();
13119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
132db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
133db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            canvas.drawPath(mCacheTransPath, paint);
134db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
135915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
136915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
137915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    class Brush implements DrawStyle {
138e2c4d033de164d886af9d242d8672570730580d6John Hoford        int mBrushID;
13919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Bitmap mBrush;
14019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        byte mType;
14119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
14219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Brush(int brushID) {
14319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mBrushID = brushID;
14419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
1450c1b4c6422a4d2d9b81cc0946d1c9675440a94e2Ruben Brunk
14619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Bitmap getBrush() {
14719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (mBrush == null) {
14819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                BitmapFactory.Options opt = new BitmapFactory.Options();
14919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
15047886ac74f2874633d4c1284b91c33117f056581Ruben Brunk                mBrush = BitmapFactory.decodeResource(MasterImage.getImage().getActivity()
15147886ac74f2874633d4c1284b91c33117f056581Ruben Brunk                        .getResources(), mBrushID, opt);
15219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mBrush = mBrush.extractAlpha();
153915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
15419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            return mBrush;
155915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
156915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
157915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
15847886ac74f2874633d4c1284b91c33117f056581Ruben Brunk        public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas,
15947886ac74f2874633d4c1284b91c33117f056581Ruben Brunk                Matrix toScrMatrix,
16099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                int quality) {
16119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            if (sd == null || sd.mPath == null) {
162915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                return;
163915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
164915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            Paint paint = new Paint();
165915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setStyle(Style.STROKE);
166e2c4d033de164d886af9d242d8672570730580d6John Hoford            paint.setAntiAlias(true);
16787e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford            Path mCacheTransPath = new Path();
16819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mCacheTransPath.addPath(sd.mPath, toScrMatrix);
169e2c4d033de164d886af9d242d8672570730580d6John Hoford            draw(canvas, paint, sd.mColor, toScrMatrix.mapRadius(sd.mRadius) * 2,
17087e889b195de98334048bb70bb1263a2ab76c76aJohn Hoford                    mCacheTransPath);
171915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
172915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
173e2c4d033de164d886af9d242d8672570730580d6John Hoford        public Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
174e2c4d033de164d886af9d242d8672570730580d6John Hoford        {
175e2c4d033de164d886af9d242d8672570730580d6John Hoford            Matrix m = new Matrix();
176e2c4d033de164d886af9d242d8672570730580d6John Hoford            m.setScale(dstWidth / (float) src.getWidth(), dstHeight / (float) src.getHeight());
177e2c4d033de164d886af9d242d8672570730580d6John Hoford            Bitmap result = Bitmap.createBitmap(dstWidth, dstHeight, src.getConfig());
178e2c4d033de164d886af9d242d8672570730580d6John Hoford            Canvas canvas = new Canvas(result);
179e2c4d033de164d886af9d242d8672570730580d6John Hoford
180e2c4d033de164d886af9d242d8672570730580d6John Hoford            Paint paint = new Paint();
181e2c4d033de164d886af9d242d8672570730580d6John Hoford            paint.setFilterBitmap(filter);
182e2c4d033de164d886af9d242d8672570730580d6John Hoford            canvas.drawBitmap(src, m, paint);
183e2c4d033de164d886af9d242d8672570730580d6John Hoford
184e2c4d033de164d886af9d242d8672570730580d6John Hoford            return result;
185e2c4d033de164d886af9d242d8672570730580d6John Hoford
186e2c4d033de164d886af9d242d8672570730580d6John Hoford        }
187915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        void draw(Canvas canvas, Paint paint, int color, float size, Path path) {
18819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            PathMeasure mPathMeasure = new PathMeasure();
18919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mPosition = new float[2];
19019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float[] mTan = new float[2];
191915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
192915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            mPathMeasure.setPath(path, false);
19319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
19419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setAntiAlias(true);
195915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            paint.setColor(color);
196e2c4d033de164d886af9d242d8672570730580d6John Hoford
19719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
198e2c4d033de164d886af9d242d8672570730580d6John Hoford            Bitmap brush;
199e2c4d033de164d886af9d242d8672570730580d6John Hoford            // done this way because of a bug in
200e2c4d033de164d886af9d242d8672570730580d6John Hoford            // Bitmap.createScaledBitmap(getBrush(),(int) size,(int) size,true);
201e2c4d033de164d886af9d242d8672570730580d6John Hoford            brush = createScaledBitmap(getBrush(), (int) size, (int) size, true);
202915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            float len = mPathMeasure.getLength();
20319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            float s2 = size / 2;
204e2c4d033de164d886af9d242d8672570730580d6John Hoford            float step = s2 / 8;
20519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (float i = 0; i < len; i += step) {
20619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                mPathMeasure.getPosTan(i, mPosition, mTan);
20719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                //                canvas.drawCircle(pos[0], pos[1], size, paint);
20819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                canvas.drawBitmap(brush, mPosition[0] - s2, mPosition[1] - s2, paint);
209915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
210915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
211915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
212915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        @Override
21319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public void setType(byte type) {
21419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            mType = type;
215db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
216db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
217db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
21819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
21999baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            int quality) {
22099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        mDrawingsTypes[sd.mType].paint(sd, canvas, toScrMatrix, quality);
221db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
222db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
22399baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public void drawData(Canvas canvas, Matrix originalRotateToScreen, int quality) {
224db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Paint paint = new Paint();
22536e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard        if (quality == FilterEnvironment.QUALITY_FINAL) {
226db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            paint.setAntiAlias(true);
227db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
228db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStyle(Style.STROKE);
229db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setColor(Color.RED);
230db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        paint.setStrokeWidth(40);
23119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
232e2c4d033de164d886af9d242d8672570730580d6John Hoford        if (mParameters.getDrawing().isEmpty() && mParameters.getCurrentDrawing() == null) {
23397d76abecd25656bcd622c56d9cf4e2c9b236ea3John Hoford            mOverlayBitmap = null;
23497d76abecd25656bcd622c56d9cf4e2c9b236ea3John Hoford            mCachedStrokes = -1;
235db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
236db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
23736e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard        if (quality == FilterEnvironment.QUALITY_FINAL) {
238a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
23919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
24099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                paint(strokeData, canvas, originalRotateToScreen, quality);
241915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
242db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            return;
243db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
24419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
245db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        if (mOverlayBitmap == null ||
246db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                mOverlayBitmap.getWidth() != canvas.getWidth() ||
247573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                mOverlayBitmap.getHeight() != canvas.getHeight() ||
248573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                mParameters.getDrawing().size() < mCachedStrokes) {
249db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
250db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mOverlayBitmap = Bitmap.createBitmap(
251db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford                    canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
252db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            mCachedStrokes = 0;
253db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
25419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
25519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (mCachedStrokes < mParameters.getDrawing().size()) {
256db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford            fillBuffer(originalRotateToScreen);
257db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        }
258db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        canvas.drawBitmap(mOverlayBitmap, 0, 0, paint);
25919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
26019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        StrokeData stroke = mParameters.getCurrentDrawing();
26119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (stroke != null) {
26299baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            paint(stroke, canvas, originalRotateToScreen, quality);
26319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
264db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
265db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
266db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void fillBuffer(Matrix originalRotateToScreen) {
267db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        Canvas drawCache = new Canvas(mOverlayBitmap);
26819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        Vector<FilterDrawRepresentation.StrokeData> v = mParameters.getDrawing();
26919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        int n = v.size();
270db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
27119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (int i = mCachedStrokes; i < n; i++) {
27236e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard            paint(v.get(i), drawCache, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
273915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
27419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCachedStrokes = n;
275db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
276db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
277db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    public void draw(Canvas canvas, Matrix originalRotateToScreen) {
27819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
27936e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard            paint(strokeData, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
280915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
28199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        mDrawingsTypes[mCurrentStyle].paint(
28236e567afff815bc821c2859ebdeec86b1fca1ef6nicolasroard                null, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
283db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
284db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford
285db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    @Override
28699baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
287db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int w = bitmap.getWidth();
288db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        int h = bitmap.getHeight();
28912017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford
29012017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        Matrix m = getOriginalToScreenMatrix(w, h);
29199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        drawData(new Canvas(bitmap), m, quality);
292db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford        return bitmap;
293db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford    }
29419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
295db94725c415a9e16abfce05c0315bd7c1c3ecce2John Hoford}
296