1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package rs.example.android.com.healingbrush;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.ColorFilter;
23import android.graphics.Paint;
24import android.graphics.Path;
25import android.graphics.PathMeasure;
26import android.graphics.PorterDuff;
27import android.graphics.PorterDuffXfermode;
28import android.graphics.Rect;
29import android.graphics.RectF;
30import android.graphics.drawable.Drawable;
31import android.support.v8.renderscript.Allocation;
32import android.support.v8.renderscript.Element;
33import android.support.v8.renderscript.RenderScript;
34import android.support.v8.renderscript.Script;
35import android.support.v8.renderscript.Type;
36import android.util.Log;
37
38import com.example.android.rs.sample.ScriptC_find_region;
39import com.example.android.rs.sample.ScriptC_healing;
40
41public class Region {
42    private static final String TAG = "Region";
43    int mCutOffsetX; // image coords of the cut  (mPointsXY - mPasteOffX + mCutOffsetX)
44    int mCutOffsetY; // image coords of the cut (mPointsXY - mPasteOffY + mCutOffsetY)
45
46    int[] mPaste; // contains a copy where to paste
47
48    float[] mPointsXY; // polygon point in original image coordnates
49    int numberOfPoints;
50
51    FindRegion mFindRegion;
52    private Bitmap mUndoBitmap;
53
54    /**
55     * @param xy  A list of xy points that represents the polygon
56     * @param img The original polygon
57     */
58    public Region(float[] xy, Bitmap img) {
59        mPointsXY = xy;
60        mFindRegion = new FindRegion(mPointsXY, img);
61    }
62
63    Drawable getSourceLocation() {
64        final Path path = new Path();
65        Rect bounds = mFindRegion.getRoiBounds();
66        for (int i = 0; i < mPointsXY.length; i += 2) {
67            if (i == 0) {
68                path.moveTo(mPointsXY[i] - bounds.left + mCutOffsetX,
69                        mPointsXY[i + 1] - bounds.top + mCutOffsetY);
70            } else {
71                path.lineTo(mPointsXY[i] - bounds.left + mCutOffsetX,
72                        mPointsXY[i + 1] - bounds.top + mCutOffsetY);
73            }
74        }
75        path.close();
76        Drawable d = new Drawable() {
77            Paint paint1 = new Paint();
78            Paint paint2 = new Paint();
79
80            {
81                paint1.setStyle(Paint.Style.STROKE);
82                paint2.setStyle(Paint.Style.STROKE);
83                paint1.setColor(Color.BLACK);
84                paint1.setStrokeWidth(2);
85                paint2.setColor(Color.BLUE);
86            }
87
88            @Override
89            public void draw(Canvas canvas) {
90                canvas.drawPath(path, paint1);
91                canvas.drawPath(path, paint2);
92            }
93
94            @Override
95            public void setAlpha(int alpha) {
96            }
97
98            @Override
99            public void setColorFilter(ColorFilter cf) {
100            }
101
102            @Override
103            public int getOpacity() {
104                return 0;
105            }
106        };
107        return d;
108    }
109
110    public Drawable findMatch(ScriptC_find_region findRegion, RenderScript mRs, Bitmap image) {
111        Rect mRoiBounds = mFindRegion.findMatch(findRegion, mRs, image);
112        int cutOffsetX = mFindRegion.getCutOffsetX();
113        int cutOffsetY = mFindRegion.getCutOffsetY();
114        final Path path = new Path();
115        for (int i = 0; i < mPointsXY.length; i += 2) {
116            if (i == 0) {
117                path.moveTo(mPointsXY[i] - mRoiBounds.left + cutOffsetX,
118                        mPointsXY[i + 1] - mRoiBounds.top + cutOffsetY);
119            } else {
120                path.lineTo(mPointsXY[i] - mRoiBounds.left + cutOffsetX,
121                        mPointsXY[i + 1] - mRoiBounds.top + cutOffsetY);
122            }
123        }
124
125        path.close();
126
127
128        Drawable d = new Drawable() {
129            Paint paint = new Paint();
130
131            {
132                paint.setStyle(Paint.Style.STROKE);
133            }
134
135            @Override
136            public void draw(Canvas canvas) {
137                canvas.drawPath(path, paint);
138            }
139
140            @Override
141            public void setAlpha(int alpha) {
142            }
143
144            @Override
145            public void setColorFilter(ColorFilter cf) {
146            }
147
148            @Override
149            public int getOpacity() {
150                return 0;
151            }
152        };
153        return d;
154    }
155
156    Bitmap createMutableBitmap(Bitmap image, int x, int y, int width, int height) {
157        Bitmap ret = Bitmap.createBitmap(image, x, y, width, height);
158        return ret.copy(Bitmap.Config.ARGB_8888, true);
159    }
160
161    /**
162     * This function only assumes mPointsXY, mPasteOffX, mPasteOffY
163     *
164     * @param healing
165     * @param rs
166     * @param image
167     */
168    public void heal(ScriptC_healing healing, RenderScript rs, Bitmap image, Bitmap output) {
169        Healing h = new Healing(
170                mFindRegion.getRoiBounds(),
171                mPointsXY,
172                mFindRegion.getCutOffsetX(),
173                mFindRegion.getCutOffsetY());
174        h.heal(healing, rs, image, output);
175        mUndoBitmap = h.getmUndoBitmap();
176    }
177
178    public void undo(Bitmap output) {
179        Canvas c = new Canvas(output);
180        Rect roi;
181        roi = mFindRegion.getRoiBounds();
182        c.drawBitmap(mUndoBitmap, roi.left, roi.top, null);
183    }
184}
185