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