summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/filters/ImageFilterDraw.java
blob: d83a521bda145ff7ec2a111fee89cfb85d2c2813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.filters;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathMeasure;

import com.android.gallery3d.R;
import com.android.gallery3d.filtershow.editors.EditorDraw;

import java.util.Arrays;

public class ImageFilterDraw extends ImageFilter {
    private static final String LOGTAG = "ImageFilterDraw";
    public final static char SIMPLE_STYLE = 0;
    public final static char BRUSH_STYLE = 1;
    Bitmap mOverlayBitmap; // this accelerates interaction
    int mCachedStrokes = -1;
    int mCurrentStyle = 0;
    DrawStyle[] mDrawings = new DrawStyle[] {
            new SimpleDraw(), new Brush() };

    public void setStyle(char style) {
        mCurrentStyle = style;
    }

    public static interface DrawStyle {
        public DrawStyle clone();
        public void setSize(float radius);
        public void setColor(int color);
        public void startStroke(float x, float y);
        public void stroke(float x, float y);
        public void endStroke(float x, float y);
        public int getNumberOfStrokes();
        public void clearCurren();
        public void paintCurrentStroke(Canvas canvas, Matrix toScrMatrix, boolean highQuality);
        public int paintLast(int from, Canvas canvas, Matrix toScrMatrix, boolean highQuality);
        public boolean same(DrawStyle o);
        public boolean empty();
    };

    class SimpleDraw implements DrawStyle {
        private Path[] mPaths = new Path[0];
        private int[] mColors = new int[0];
        private float[] mRadius = new float[0];
        private int mStrokeCnt = 0;

        private Path mCurrentPath;
        private float mCurrentRadius;
        private int mCurrentColor;

        @Override
        public DrawStyle clone() {
            SimpleDraw ret = new SimpleDraw();
            ret.mPaths = new Path[mPaths.length];
            for (int i = 0; i < ret.mPaths.length; i++) {
                ret.mPaths[i] = new Path(mPaths[i]);
            }
            ret.mColors = Arrays.copyOf(mColors, mColors.length);
            ret.mRadius = Arrays.copyOf(mRadius, mRadius.length);
            ret.mStrokeCnt = mStrokeCnt;
            return ret;
        }

        @Override
        public boolean empty() {
            return mStrokeCnt == -1;
        }

        @Override
        public void setSize(float radius) {
            mCurrentRadius = radius;
        }

        @Override
        public void setColor(int color) {
            mCurrentColor = color;
        }

        @Override
        public void startStroke(float x, float y) {
            mCurrentPath = new Path();
            mCurrentPath.moveTo(x, y);
        }

        @Override
        public void stroke(float x, float y) {
            if (mCurrentPath != null) {
                mCurrentPath.lineTo(x, y);
            }
        }

        @Override
        public void endStroke(float x, float y) {
            if (mCurrentPath != null) {
                mCurrentPath.lineTo(x, y);
                Path[] np = new Path[mStrokeCnt + 1];
                for (int i = 0; i < mStrokeCnt; i++) {
                    np[i] = mPaths[i];
                }
                np[mStrokeCnt] = mCurrentPath;
                mColors = Arrays.copyOf(mColors, mColors.length + 1);
                mRadius = Arrays.copyOf(mRadius, mRadius.length + 1);
                mRadius[mStrokeCnt] = mCurrentRadius;
                mColors[mStrokeCnt] = mCurrentColor;
                mPaths = np;
                mStrokeCnt++;
            }
        }

        @Override
        public void clearCurren(){
            mCurrentPath = null;
        }

        @Override
        public void paintCurrentStroke(Canvas canvas, Matrix toScrMatrix, boolean highQuality) {
            Path path = mCurrentPath;
            if (path == null) {
                return;
            }
            Paint paint = new Paint();

            paint.setStyle(Style.STROKE);
            paint.setColor(mCurrentColor);
            paint.setStrokeWidth(toScrMatrix.mapRadius(mCurrentRadius));

            // don this way because a bug in path.transform(matrix)
            Path mCacheTransPath = new Path();
            mCacheTransPath.addPath(path, toScrMatrix);

            canvas.drawPath(mCacheTransPath, paint);
        }

        @Override
        public int paintLast(int from, Canvas canvas, Matrix toScrMatrix, boolean highQuality) {
            Paint paint = new Paint();
            Matrix m = new Matrix();
            canvas.save();
            canvas.concat(toScrMatrix);
            paint.setStyle(Style.STROKE);
            for (int i = from; i < mStrokeCnt; i++) {
                paint.setColor(mColors[i]);
                paint.setStrokeWidth(mRadius[i]);
                canvas.drawPath(mPaths[i], paint);
            }
            canvas.restore();
            return mStrokeCnt;
        }

        @Override
        public boolean same(DrawStyle o) {
            if (!(o instanceof SimpleDraw)) {
                return false;
            }
            SimpleDraw sd = (SimpleDraw) o;
            boolean same;
            same = Arrays.equals(mRadius, sd.mRadius);
            if (!same) {
                return false;
            }
            same = Arrays.equals(mColors, sd.mColors);
            if (!same) {
                return false;
            }
            for (int i = 0; i < mPaths.length; i++) {
                if (!mPaths[i].equals(sd.mPaths)) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public int getNumberOfStrokes() {
            return mStrokeCnt;
        }
    }

    class Brush implements DrawStyle {
        private Path[] mPaths = new Path[0];
        private int[] mColors = new int[0];
        private float[] mRadius = new float[0];
        private int mStrokeCnt = 0;

        private Path mCurrentPath;
        private float mCurrentRadius;
        private int mCurrentColor;

        @Override
        public DrawStyle clone() {
            Brush ret = new Brush();
            ret.mPaths = new Path[mPaths.length];
            for (int i = 0; i < ret.mPaths.length; i++) {
                ret.mPaths[i] = new Path(mPaths[i]);
            }
            ret.mColors = Arrays.copyOf(mColors, mColors.length);
            ret.mRadius = Arrays.copyOf(mRadius, mRadius.length);
            ret.mStrokeCnt = mStrokeCnt;
            return ret;
        }

        @Override
        public boolean empty() {
            return mStrokeCnt == -1;
        }

        @Override
        public void setSize(float radius) {
            mCurrentRadius = radius;
        }

        @Override
        public void setColor(int color) {
            mCurrentColor = color;
        }

        @Override
        public void startStroke(float x, float y) {
            mCurrentPath = new Path();
            mCurrentPath.moveTo(x, y);
        }

        @Override
        public void stroke(float x, float y) {
            if (mCurrentPath != null) {
                mCurrentPath.lineTo(x, y);
            }
        }

        @Override
        public void endStroke(float x, float y) {
            if (mCurrentPath != null) {
                mCurrentPath.lineTo(x, y);
                Path[] np = new Path[mStrokeCnt + 1];
                for (int i = 0; i < mStrokeCnt; i++) {
                    np[i] = mPaths[i];
                }
                np[mStrokeCnt] = mCurrentPath;
                mColors = Arrays.copyOf(mColors, mColors.length + 1);
                mRadius = Arrays.copyOf(mRadius, mRadius.length + 1);
                mRadius[mStrokeCnt] = mCurrentRadius;
                mColors[mStrokeCnt] = mCurrentColor;
                mPaths = np;
                mStrokeCnt++;
                clearCurren();
            }
        }

        @Override
        public void clearCurren() {
            mCurrentPath = null;
        }

        @Override
        public void paintCurrentStroke(Canvas canvas, Matrix toScrMatrix, boolean highQuality) {
            Path path = mCurrentPath;
            if (path == null) {
                return;
            }
            Paint paint = new Paint();
            paint.setStyle(Style.STROKE);

            float scale = toScrMatrix.mapRadius(1);
            Path mCacheTransPath = new Path();
            mCacheTransPath.addPath(path, toScrMatrix);
            draw(canvas, paint, mCurrentColor, toScrMatrix.mapRadius(mCurrentRadius),
                    mCacheTransPath);
        }

        @Override
        public int paintLast(int from, Canvas canvas, Matrix toScrMatrix, boolean highQuality) {
            Paint paint = new Paint();

            Matrix m = new Matrix();
            canvas.save();
            canvas.concat(toScrMatrix);
            paint.setStyle(Style.STROKE);
            for (int i = from; i < mStrokeCnt; i++) {

                draw(canvas, paint, mColors[i], mRadius[i], mPaths[i]);
            }
            canvas.restore();
            return mStrokeCnt;
        }

        PathMeasure mPathMeasure = new PathMeasure();

        void draw(Canvas canvas, Paint paint, int color, float size, Path path) {

            mPathMeasure.setPath(path, false);
            float[] pos = new float[2];
            float[] tan = new float[2];
            paint.setColor(color);
            float len = mPathMeasure.getLength();
            for (float i = 0; i < len; i += (size) / 2) {
                mPathMeasure.getPosTan(i, pos, tan);
                canvas.drawCircle(pos[0], pos[1], size, paint);
            }

        }

        @Override
        public boolean same(DrawStyle o) {
            if (!(o instanceof Brush)) {
                return false;
            }
            Brush sd = (Brush) o;
            boolean same;
            same = Arrays.equals(mRadius, sd.mRadius);
            if (!same) {
                return false;
            }
            same = Arrays.equals(mColors, sd.mColors);
            if (!same) {
                return false;
            }
            for (int i = 0; i < mPaths.length; i++) {
                if (!mPaths[i].equals(sd.mPaths)) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public int getNumberOfStrokes() {
            return mStrokeCnt;
        }
    }

    public void startSection(int color, float size, float x, float y) {
        mDrawings[mCurrentStyle].setColor(color);
        mDrawings[mCurrentStyle].setSize(size);
        mDrawings[mCurrentStyle].startStroke(x, y);
    }

    public void addPoint(float x, float y) {
        mDrawings[mCurrentStyle].stroke(x, y);
    }

    public void endSection(float x, float y) {
        mDrawings[mCurrentStyle].endStroke(x, y);
    }

    public ImageFilterDraw() {
        mName = "Image Draw";
    }

    public void drawData(Canvas canvas, Matrix originalRotateToScreen, boolean highQuality) {
        Paint paint = new Paint();
        if (highQuality) {
            paint.setAntiAlias(true);
        }
        paint.setStyle(Style.STROKE);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(40);
        boolean empty = true;
        for (int i = 0; i < mDrawings.length; i++) {
            empty &= mDrawings[i].empty();
        }
        if (empty) {
            return;
        }
        if (highQuality) {
            for (int i = 0; i < mDrawings.length; i++) {
                mDrawings[i].paintLast(0, canvas, originalRotateToScreen, highQuality);
            }

            return;
        }
        if (mOverlayBitmap == null ||
                mOverlayBitmap.getWidth() != canvas.getWidth() ||
                mOverlayBitmap.getHeight() != canvas.getHeight()) {

            mOverlayBitmap = Bitmap.createBitmap(
                    canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
            mCachedStrokes = 0;
        }
        if (mCachedStrokes < mDrawings[mCurrentStyle].getNumberOfStrokes()) {
            fillBuffer(originalRotateToScreen);
        }
        canvas.drawBitmap(mOverlayBitmap, 0, 0, paint);
    }

    public void fillBuffer(Matrix originalRotateToScreen) {
        Canvas drawCache = new Canvas(mOverlayBitmap);
        for (int i = 0; i < mDrawings.length; i++) {

            mCachedStrokes = mDrawings[i].paintLast(
                mCachedStrokes, drawCache, originalRotateToScreen, false);
        }
    }

    @Override
    public int getButtonId() {
        return R.id.drawOnImageButton;
    }

    @Override
    public int getTextId() {
        return R.string.imageDraw;
    }

    @Override
    public int getEditingViewId() {
        return EditorDraw.ID;
    }

    @Override
    public ImageFilter clone() throws CloneNotSupportedException {
        ImageFilterDraw filter = (ImageFilterDraw) super.clone();

        filter.mDrawings = mDrawings.clone();
        return filter;
    }

    public boolean isNil() {
        for (int i = 0; i < mDrawings.length; i++) {
            if (mDrawings[i].getNumberOfStrokes() != 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean same(ImageFilter filter) {
        boolean isSuperSame = super.same(filter);
        if (!isSuperSame || !(filter instanceof ImageFilterDraw)) {
            return false;
        }

        ImageFilterDraw dfilter = (ImageFilterDraw) filter;
        boolean same = true;
        for (int i = 0; i < mDrawings.length; i++) {
            same &= mDrawings[i].same(dfilter.mDrawings[i]);
        }
        return same;
    }

    public void clear() {
        mDrawings[mCurrentStyle].clearCurren();
    }

    public void draw(Canvas canvas, Matrix originalRotateToScreen) {
        for (int i = 0; i < mDrawings.length; i++) {
            mDrawings[i].paintCurrentStroke(canvas, originalRotateToScreen, false);
        }
    }

    @Override
    public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        short[] rect = new short[4];

        Matrix m = new Matrix();
        m.setScale(scaleFactor, scaleFactor);

        drawData(new Canvas(bitmap), m, highQuality);

        return bitmap;
    }
}