1 /* 2 * Copyright (C) 2010 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 17 package com.android.photoeditor; 18 19 import android.graphics.Bitmap; 20 import android.graphics.Matrix; 21 22 /** 23 * Photo that is used for editing/display and should be synchronized for concurrent access. 24 */ 25 public class Photo { 26 27 private Bitmap bitmap; 28 29 /** 30 * Factory method to ensure every Photo instance holds a non-null bitmap. 31 */ create(Bitmap bitmap)32 public static Photo create(Bitmap bitmap) { 33 return (bitmap != null) ? new Photo(bitmap) : null; 34 } 35 Photo(Bitmap bitmap)36 private Photo(Bitmap bitmap) { 37 this.bitmap = bitmap; 38 } 39 bitmap()40 public Bitmap bitmap() { 41 return bitmap; 42 } 43 copy(Bitmap.Config config)44 public Photo copy(Bitmap.Config config) { 45 Bitmap copy = bitmap.copy(config, true); 46 return (copy != null) ? new Photo(copy) : null; 47 } 48 matchDimension(Photo photo)49 public boolean matchDimension(Photo photo) { 50 return ((photo.width() == width()) && (photo.height() == height())); 51 } 52 width()53 public int width() { 54 return bitmap.getWidth(); 55 } 56 height()57 public int height() { 58 return bitmap.getHeight(); 59 } 60 transform(Matrix matrix)61 public void transform(Matrix matrix) { 62 // Copy immutable transformed photo; no-op if it fails to ensure bitmap isn't assigned null. 63 Bitmap transformed = BitmapUtils.createBitmap(bitmap, matrix).copy( 64 bitmap.getConfig(), true); 65 if (transformed != null) { 66 bitmap.recycle(); 67 bitmap = transformed; 68 } 69 } 70 crop(int left, int top, int width, int height)71 public void crop(int left, int top, int width, int height) { 72 // Copy immutable cropped photo; no-op if it fails to ensure bitmap isn't assigned null. 73 Bitmap cropped = Bitmap.createBitmap(bitmap, left, top, width, height).copy( 74 bitmap.getConfig(), true); 75 if (cropped != null) { 76 bitmap.recycle(); 77 bitmap = cropped; 78 } 79 } 80 81 /** 82 * Recycles bitmaps; this instance should not be used after its clear() is called. 83 */ clear()84 public void clear() { 85 bitmap.recycle(); 86 } 87 } 88