2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.gallery3d.filtershow.filters;
19 import android.graphics.Bitmap;
20 import android.graphics.BitmapFactory;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Matrix;
24 import android.graphics.Paint;
25 import android.graphics.Paint.Style;
26 import android.graphics.Path;
27 import android.graphics.PathMeasure;
28 import android.graphics.PorterDuff;
29 import android.graphics.PorterDuffColorFilter;
31 import com.android.gallery3d.R;
32 import com.android.gallery3d.app.Log;
33 import com.android.gallery3d.filtershow.cache.ImageLoader;
34 import com.android.gallery3d.filtershow.filters.FilterDrawRepresentation.StrokeData;
35 import com.android.gallery3d.filtershow.imageshow.MasterImage;
36 import com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
38 import java.util.Vector;
40 public class ImageFilterDraw extends ImageFilter {
41 private static final String LOGTAG = "ImageFilterDraw";
42 public final static byte SIMPLE_STYLE = 0;
43 public final static byte BRUSH_STYLE_SPATTER = 1;
44 public final static byte BRUSH_STYLE_MARKER = 2;
45 public final static int NUMBER_OF_STYLES = 3;
46 Bitmap mOverlayBitmap; // this accelerates interaction
47 int mCachedStrokes = -1;
48 int mCurrentStyle = 0;
50 FilterDrawRepresentation mParameters = new FilterDrawRepresentation();
52 public ImageFilterDraw() {
56 DrawStyle[] mDrawingsTypes = new DrawStyle[] {
59 new Brush(R.drawable.brush_gauss),
60 new Brush(R.drawable.brush_marker),
61 new Brush(R.drawable.brush_spatter)
64 for (int i = 0; i < mDrawingsTypes.length; i++) {
65 mDrawingsTypes[i].setType((byte) i);
71 public FilterRepresentation getDefaultRepresentation() {
72 return new FilterDrawRepresentation();
76 public void useRepresentation(FilterRepresentation representation) {
77 FilterDrawRepresentation parameters = (FilterDrawRepresentation) representation;
78 mParameters = parameters;
81 public void setStyle(byte style) {
82 mCurrentStyle = style % mDrawingsTypes.length;
85 public int getStyle() {
89 public static interface DrawStyle {
90 public void setType(byte type);
91 public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
95 class SimpleDraw implements DrawStyle {
99 public SimpleDraw(int mode) {
104 public void setType(byte type) {
109 public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
114 if (sd.mPath == null) {
117 Paint paint = new Paint();
119 paint.setStyle(Style.STROKE);
121 paint.setStrokeCap(Paint.Cap.SQUARE);
123 paint.setStrokeCap(Paint.Cap.ROUND);
125 paint.setAntiAlias(true);
126 paint.setColor(sd.mColor);
127 paint.setStrokeWidth(toScrMatrix.mapRadius(sd.mRadius));
129 // done this way because of a bug in path.transform(matrix)
130 Path mCacheTransPath = new Path();
131 mCacheTransPath.addPath(sd.mPath, toScrMatrix);
133 canvas.drawPath(mCacheTransPath, paint);
137 class Brush implements DrawStyle {
142 public Brush(int brushID) {
146 public Bitmap getBrush() {
147 if (mBrush == null) {
148 BitmapFactory.Options opt = new BitmapFactory.Options();
149 opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
150 mBrush = BitmapFactory.decodeResource(MasterImage.getImage().getActivity()
151 .getResources(), mBrushID, opt);
152 mBrush = mBrush.extractAlpha();
158 public void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas,
161 if (sd == null || sd.mPath == null) {
164 Paint paint = new Paint();
165 paint.setStyle(Style.STROKE);
166 paint.setAntiAlias(true);
167 Path mCacheTransPath = new Path();
168 mCacheTransPath.addPath(sd.mPath, toScrMatrix);
169 draw(canvas, paint, sd.mColor, toScrMatrix.mapRadius(sd.mRadius) * 2,
173 public Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
175 Matrix m = new Matrix();
176 m.setScale(dstWidth / (float) src.getWidth(), dstHeight / (float) src.getHeight());
177 Bitmap result = Bitmap.createBitmap(dstWidth, dstHeight, src.getConfig());
178 Canvas canvas = new Canvas(result);
180 Paint paint = new Paint();
181 paint.setFilterBitmap(filter);
182 canvas.drawBitmap(src, m, paint);
187 void draw(Canvas canvas, Paint paint, int color, float size, Path path) {
188 PathMeasure mPathMeasure = new PathMeasure();
189 float[] mPosition = new float[2];
190 float[] mTan = new float[2];
192 mPathMeasure.setPath(path, false);
194 paint.setAntiAlias(true);
195 paint.setColor(color);
197 paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
199 // done this way because of a bug in
200 // Bitmap.createScaledBitmap(getBrush(),(int) size,(int) size,true);
201 brush = createScaledBitmap(getBrush(), (int) size, (int) size, true);
202 float len = mPathMeasure.getLength();
205 for (float i = 0; i < len; i += step) {
206 mPathMeasure.getPosTan(i, mPosition, mTan);
207 // canvas.drawCircle(pos[0], pos[1], size, paint);
208 canvas.drawBitmap(brush, mPosition[0] - s2, mPosition[1] - s2, paint);
213 public void setType(byte type) {
218 void paint(FilterDrawRepresentation.StrokeData sd, Canvas canvas, Matrix toScrMatrix,
220 mDrawingsTypes[sd.mType].paint(sd, canvas, toScrMatrix, quality);
223 public void drawData(Canvas canvas, Matrix originalRotateToScreen, int quality) {
224 Paint paint = new Paint();
225 if (quality == FilterEnvironment.QUALITY_FINAL) {
226 paint.setAntiAlias(true);
228 paint.setStyle(Style.STROKE);
229 paint.setColor(Color.RED);
230 paint.setStrokeWidth(40);
232 if (mParameters.getDrawing().isEmpty() && mParameters.getCurrentDrawing() == null) {
233 mOverlayBitmap = null;
237 if (quality == FilterEnvironment.QUALITY_FINAL) {
239 for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
240 paint(strokeData, canvas, originalRotateToScreen, quality);
245 if (mOverlayBitmap == null ||
246 mOverlayBitmap.getWidth() != canvas.getWidth() ||
247 mOverlayBitmap.getHeight() != canvas.getHeight() ||
248 mParameters.getDrawing().size() < mCachedStrokes) {
250 mOverlayBitmap = Bitmap.createBitmap(
251 canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
255 if (mCachedStrokes < mParameters.getDrawing().size()) {
256 fillBuffer(originalRotateToScreen);
258 canvas.drawBitmap(mOverlayBitmap, 0, 0, paint);
260 StrokeData stroke = mParameters.getCurrentDrawing();
261 if (stroke != null) {
262 paint(stroke, canvas, originalRotateToScreen, quality);
266 public void fillBuffer(Matrix originalRotateToScreen) {
267 Canvas drawCache = new Canvas(mOverlayBitmap);
268 Vector<FilterDrawRepresentation.StrokeData> v = mParameters.getDrawing();
271 for (int i = mCachedStrokes; i < n; i++) {
272 paint(v.get(i), drawCache, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
277 public void draw(Canvas canvas, Matrix originalRotateToScreen) {
278 for (FilterDrawRepresentation.StrokeData strokeData : mParameters.getDrawing()) {
279 paint(strokeData, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
281 mDrawingsTypes[mCurrentStyle].paint(
282 null, canvas, originalRotateToScreen, FilterEnvironment.QUALITY_PREVIEW);
286 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
287 int w = bitmap.getWidth();
288 int h = bitmap.getHeight();
290 Matrix m = getOriginalToScreenMatrix(w, h);
291 drawData(new Canvas(bitmap), m, quality);