Bitmap_Delegate.java revision 5a09488a158b669577cd8eb557ce4feb62929e75
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 17package android.graphics; 18 19import com.android.layoutlib.api.IDensityBasedResourceValue.Density; 20import com.android.layoutlib.bridge.impl.DelegateManager; 21 22import android.graphics.Bitmap.Config; 23import android.os.Parcel; 24 25import java.awt.Graphics2D; 26import java.awt.image.BufferedImage; 27import java.io.File; 28import java.io.IOException; 29import java.io.InputStream; 30import java.io.OutputStream; 31import java.nio.Buffer; 32 33import javax.imageio.ImageIO; 34 35/** 36 * Delegate implementing the native methods of android.graphics.Bitmap 37 * 38 * Through the layoutlib_create tool, the original native methods of Bitmap have been replaced 39 * by calls to methods of the same name in this delegate class. 40 * 41 * This class behaves like the original native implementation, but in Java, keeping previously 42 * native data into its own objects and mapping them to int that are sent back and forth between 43 * it and the original Bitmap class. 44 * 45 * @see DelegateManager 46 * 47 */ 48public class Bitmap_Delegate { 49 50 // ---- delegate manager ---- 51 private static final DelegateManager<Bitmap_Delegate> sManager = 52 new DelegateManager<Bitmap_Delegate>(); 53 54 // ---- delegate helper data ---- 55 56 // ---- delegate data ---- 57 private BufferedImage mImage; 58 private boolean mHasAlpha = true; 59 60 // ---- Public Helper methods ---- 61 62 /** 63 * Returns the native delegate associated to a given {@link Bitmap_Delegate} object. 64 */ 65 public static Bitmap_Delegate getDelegate(Bitmap bitmap) { 66 return sManager.getDelegate(bitmap.mNativeBitmap); 67 } 68 69 /** 70 * Returns the native delegate associated to a given an int referencing a {@link Bitmap} object. 71 */ 72 public static Bitmap_Delegate getDelegate(int native_bitmap) { 73 return sManager.getDelegate(native_bitmap); 74 } 75 76 /** 77 * Creates and returns a {@link Bitmap} initialized with the given file content. 78 */ 79 public static Bitmap createBitmap(File input, Density density) throws IOException { 80 // create a delegate with the content of the file. 81 Bitmap_Delegate delegate = new Bitmap_Delegate(ImageIO.read(input)); 82 83 return createBitmap(delegate, density.getValue()); 84 } 85 86 /** 87 * Creates and returns a {@link Bitmap} initialized with the given stream content. 88 */ 89 public static Bitmap createBitmap(InputStream input, Density density) throws IOException { 90 // create a delegate with the content of the stream. 91 Bitmap_Delegate delegate = new Bitmap_Delegate(ImageIO.read(input)); 92 93 return createBitmap(delegate, density.getValue()); 94 } 95 96 /** 97 * Creates and returns a {@link Bitmap} initialized with the given {@link BufferedImage} 98 */ 99 public static Bitmap createBitmap(BufferedImage image, Density density) throws IOException { 100 // create a delegate with the given image. 101 Bitmap_Delegate delegate = new Bitmap_Delegate(image); 102 103 return createBitmap(delegate, density.getValue()); 104 } 105 106 /** 107 * Returns the {@link BufferedImage} used by the delegate of the given {@link Bitmap}. 108 */ 109 public static BufferedImage getImage(Bitmap bitmap) { 110 // get the delegate from the native int. 111 Bitmap_Delegate delegate = sManager.getDelegate(bitmap.mNativeBitmap); 112 if (delegate == null) { 113 assert false; 114 return null; 115 } 116 117 return delegate.mImage; 118 } 119 120 public static int getBufferedImageType(int nativeBitmapConfig) { 121 switch (Config.sConfigs[nativeBitmapConfig]) { 122 case ALPHA_8: 123 return BufferedImage.TYPE_INT_ARGB; 124 case RGB_565: 125 return BufferedImage.TYPE_INT_ARGB; 126 case ARGB_4444: 127 return BufferedImage.TYPE_INT_ARGB; 128 case ARGB_8888: 129 return BufferedImage.TYPE_INT_ARGB; 130 } 131 132 return BufferedImage.TYPE_INT_ARGB; 133 } 134 135 /** 136 * Returns the {@link BufferedImage} used by the delegate of the given {@link Bitmap}. 137 */ 138 public BufferedImage getImage() { 139 return mImage; 140 } 141 142 // ---- native methods ---- 143 144 /*package*/ static Bitmap nativeCreate(int[] colors, int offset, int stride, int width, 145 int height, int nativeConfig, boolean mutable) { 146 int imageType = getBufferedImageType(nativeConfig); 147 148 // create the image 149 BufferedImage image = new BufferedImage(width, height, imageType); 150 151 // FIXME fill the bitmap! 152 153 // create a delegate with the content of the stream. 154 Bitmap_Delegate delegate = new Bitmap_Delegate(image); 155 156 return createBitmap(delegate, Bitmap.getDefaultDensity()); 157 } 158 159 /*package*/ static Bitmap nativeCopy(int srcBitmap, int nativeConfig, boolean isMutable) { 160 // FIXME implement native delegate 161 throw new UnsupportedOperationException("Native delegate needed for Bitmap"); 162 } 163 164 /*package*/ static void nativeDestructor(int nativeBitmap) { 165 sManager.removeDelegate(nativeBitmap); 166 } 167 168 /*package*/ static void nativeRecycle(int nativeBitmap) { 169 // FIXME implement native delegate 170 throw new UnsupportedOperationException("Native delegate needed for Bitmap"); 171 } 172 173 /*package*/ static boolean nativeCompress(int nativeBitmap, int format, int quality, 174 OutputStream stream, byte[] tempStorage) { 175 // FIXME implement native delegate 176 throw new UnsupportedOperationException("Native delegate needed for Bitmap"); 177 } 178 179 /*package*/ static void nativeErase(int nativeBitmap, int color) { 180 // get the delegate from the native int. 181 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 182 if (delegate == null) { 183 assert false; 184 return; 185 } 186 187 BufferedImage image = delegate.mImage; 188 189 Graphics2D g = image.createGraphics(); 190 try { 191 if (delegate.mHasAlpha == false) { 192 color |= color & 0xFF000000; 193 } 194 g.setColor(new java.awt.Color(color)); 195 196 g.fillRect(0, 0, image.getWidth(), image.getHeight()); 197 } finally { 198 g.dispose(); 199 } 200 } 201 202 /*package*/ static int nativeWidth(int nativeBitmap) { 203 // get the delegate from the native int. 204 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 205 if (delegate == null) { 206 assert false; 207 return 0; 208 } 209 210 return delegate.mImage.getWidth(); 211 } 212 213 /*package*/ static int nativeHeight(int nativeBitmap) { 214 // get the delegate from the native int. 215 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 216 if (delegate == null) { 217 assert false; 218 return 0; 219 } 220 221 return delegate.mImage.getHeight(); 222 } 223 224 /*package*/ static int nativeRowBytes(int nativeBitmap) { 225 // get the delegate from the native int. 226 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 227 if (delegate == null) { 228 assert false; 229 return 0; 230 } 231 232 return delegate.mImage.getWidth(); 233 } 234 235 /*package*/ static int nativeConfig(int nativeBitmap) { 236 return Config.ARGB_8888.nativeInt; 237 } 238 239 /*package*/ static boolean nativeHasAlpha(int nativeBitmap) { 240 // get the delegate from the native int. 241 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 242 if (delegate == null) { 243 assert false; 244 return true; 245 } 246 247 return delegate.mHasAlpha; 248 } 249 250 /*package*/ static int nativeGetPixel(int nativeBitmap, int x, int y) { 251 // get the delegate from the native int. 252 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 253 if (delegate == null) { 254 assert false; 255 return 0; 256 } 257 258 return delegate.mImage.getRGB(x, y); 259 } 260 261 /*package*/ static void nativeGetPixels(int nativeBitmap, int[] pixels, int offset, 262 int stride, int x, int y, int width, int height) { 263 // FIXME implement native delegate 264 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeGetPixels"); 265 } 266 267 268 /*package*/ static void nativeSetPixel(int nativeBitmap, int x, int y, int color) { 269 // FIXME implement native delegate 270 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeSetPixel"); 271 } 272 273 /*package*/ static void nativeSetPixels(int nativeBitmap, int[] colors, int offset, 274 int stride, int x, int y, int width, int height) { 275 // FIXME implement native delegate 276 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeSetPixels"); 277 } 278 279 /*package*/ static void nativeCopyPixelsToBuffer(int nativeBitmap, Buffer dst) { 280 // FIXME implement native delegate 281 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeCopyPixelsToBuffer"); 282 } 283 284 /*package*/ static void nativeCopyPixelsFromBuffer(int nb, Buffer src) { 285 // FIXME implement native delegate 286 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeCopyPixelsFromBuffer"); 287 } 288 289 /*package*/ static int nativeGenerationId(int nativeBitmap) { 290 // FIXME implement native delegate 291 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeGenerationId"); 292 } 293 294 /*package*/ static Bitmap nativeCreateFromParcel(Parcel p) { 295 // FIXME implement native delegate 296 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeCreateFromParcel"); 297 } 298 299 /*package*/ static boolean nativeWriteToParcel(int nativeBitmap, boolean isMutable, 300 int density, Parcel p) { 301 // FIXME implement native delegate 302 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeWriteToParcel"); 303 } 304 305 /*package*/ static Bitmap nativeExtractAlpha(int nativeBitmap, int nativePaint, 306 int[] offsetXY) { 307 // FIXME implement native delegate 308 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeExtractAlpha"); 309 } 310 311 312 /*package*/ static void nativePrepareToDraw(int nativeBitmap) { 313 // FIXME implement native delegate 314 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativePrepareToDraw"); 315 } 316 317 /*package*/ static void nativeSetHasAlpha(int nativeBitmap, boolean hasAlpha) { 318 // get the delegate from the native int. 319 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 320 if (delegate == null) { 321 assert false; 322 return; 323 } 324 325 delegate.mHasAlpha = hasAlpha; 326 } 327 328 /*package*/ static boolean nativeSameAs(int nb0, int nb1) { 329 // FIXME implement native delegate 330 throw new UnsupportedOperationException("Native delegate needed for Bitmap.nativeSameAs"); 331 } 332 333 // ---- Private delegate/helper methods ---- 334 335 private Bitmap_Delegate(BufferedImage image) { 336 mImage = image; 337 } 338 339 private static Bitmap createBitmap(Bitmap_Delegate delegate, int density) { 340 // get its native_int 341 int nativeInt = sManager.addDelegate(delegate); 342 343 // and create/return a new Bitmap with it 344 return new Bitmap(nativeInt, true /*isMutable*/, null /*ninePatchChunk*/, density); 345 } 346} 347