Bitmap.java revision c054966b719c8a7255f7cf4120cca5050acb68bd
1/* 2 * Copyright (C) 2006 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 android.annotation.NonNull; 20import android.os.Parcel; 21import android.os.Parcelable; 22import android.util.DisplayMetrics; 23 24import java.io.OutputStream; 25import java.nio.Buffer; 26import java.nio.ByteBuffer; 27import java.nio.IntBuffer; 28import java.nio.ShortBuffer; 29 30public final class Bitmap implements Parcelable { 31 /** 32 * Indicates that the bitmap was created for an unknown pixel density. 33 * 34 * @see Bitmap#getDensity() 35 * @see Bitmap#setDensity(int) 36 */ 37 public static final int DENSITY_NONE = 0; 38 39 /** 40 * Note: mNativeBitmap is used by FaceDetector_jni.cpp 41 * Don't change/rename without updating FaceDetector_jni.cpp 42 * 43 * @hide 44 */ 45 public final long mNativeBitmap; 46 47 /** 48 * Backing buffer for the Bitmap. 49 * Made public for quick access from drawing methods -- do NOT modify 50 * from outside this class 51 * 52 * @hide 53 */ 54 @SuppressWarnings("UnusedDeclaration") // native code only 55 public byte[] mBuffer; 56 57 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) // Keep to finalize native resources 58 private final BitmapFinalizer mFinalizer; 59 60 private final boolean mIsMutable; 61 62 /** 63 * Represents whether the Bitmap's content is requested to be pre-multiplied. 64 * Note that isPremultiplied() does not directly return this value, because 65 * isPremultiplied() may never return true for a 565 Bitmap or a bitmap 66 * without alpha. 67 * 68 * setPremultiplied() does directly set the value so that setConfig() and 69 * setPremultiplied() aren't order dependent, despite being setters. 70 * 71 * The native bitmap's premultiplication state is kept up to date by 72 * pushing down this preference for every config change. 73 */ 74 private boolean mRequestPremultiplied; 75 76 private byte[] mNinePatchChunk; // may be null 77 private int[] mOpticalInsets; // may be null 78 private int mWidth; 79 private int mHeight; 80 private boolean mRecycled; 81 82 // Package-scoped for fast access. 83 int mDensity = getDefaultDensity(); 84 85 private static volatile Matrix sScaleMatrix; 86 87 private static volatile int sDefaultDensity = -1; 88 89 /** 90 * For backwards compatibility, allows the app layer to change the default 91 * density when running old apps. 92 * @hide 93 */ 94 public static void setDefaultDensity(int density) { 95 sDefaultDensity = density; 96 } 97 98 static int getDefaultDensity() { 99 if (sDefaultDensity >= 0) { 100 return sDefaultDensity; 101 } 102 //noinspection deprecation 103 sDefaultDensity = DisplayMetrics.DENSITY_DEVICE; 104 return sDefaultDensity; 105 } 106 107 /** 108 * Private constructor that must received an already allocated native bitmap 109 * int (pointer). 110 */ 111 @SuppressWarnings({"UnusedDeclaration"}) // called from JNI 112 Bitmap(long nativeBitmap, byte[] buffer, int width, int height, int density, 113 boolean isMutable, boolean requestPremultiplied, 114 byte[] ninePatchChunk, int[] opticalInsets) { 115 if (nativeBitmap == 0) { 116 throw new RuntimeException("internal error: native bitmap is 0"); 117 } 118 119 mWidth = width; 120 mHeight = height; 121 mIsMutable = isMutable; 122 mRequestPremultiplied = requestPremultiplied; 123 mBuffer = buffer; 124 // we delete this in our finalizer 125 mNativeBitmap = nativeBitmap; 126 mFinalizer = new BitmapFinalizer(nativeBitmap); 127 128 mNinePatchChunk = ninePatchChunk; 129 mOpticalInsets = opticalInsets; 130 if (density >= 0) { 131 mDensity = density; 132 } 133 } 134 135 /** 136 * Native bitmap has been reconfigured, so set premult and cached 137 * width/height values 138 */ 139 @SuppressWarnings({"UnusedDeclaration"}) // called from JNI 140 void reinit(int width, int height, boolean requestPremultiplied) { 141 mWidth = width; 142 mHeight = height; 143 mRequestPremultiplied = requestPremultiplied; 144 } 145 146 /** 147 * <p>Returns the density for this bitmap.</p> 148 * 149 * <p>The default density is the same density as the current display, 150 * unless the current application does not support different screen 151 * densities in which case it is 152 * {@link android.util.DisplayMetrics#DENSITY_DEFAULT}. Note that 153 * compatibility mode is determined by the application that was initially 154 * loaded into a process -- applications that share the same process should 155 * all have the same compatibility, or ensure they explicitly set the 156 * density of their bitmaps appropriately.</p> 157 * 158 * @return A scaling factor of the default density or {@link #DENSITY_NONE} 159 * if the scaling factor is unknown. 160 * 161 * @see #setDensity(int) 162 * @see android.util.DisplayMetrics#DENSITY_DEFAULT 163 * @see android.util.DisplayMetrics#densityDpi 164 * @see #DENSITY_NONE 165 */ 166 public int getDensity() { 167 return mDensity; 168 } 169 170 /** 171 * <p>Specifies the density for this bitmap. When the bitmap is 172 * drawn to a Canvas that also has a density, it will be scaled 173 * appropriately.</p> 174 * 175 * @param density The density scaling factor to use with this bitmap or 176 * {@link #DENSITY_NONE} if the density is unknown. 177 * 178 * @see #getDensity() 179 * @see android.util.DisplayMetrics#DENSITY_DEFAULT 180 * @see android.util.DisplayMetrics#densityDpi 181 * @see #DENSITY_NONE 182 */ 183 public void setDensity(int density) { 184 mDensity = density; 185 } 186 187 /** 188 * <p>Modifies the bitmap to have a specified width, height, and {@link 189 * Config}, without affecting the underlying allocation backing the bitmap. 190 * Bitmap pixel data is not re-initialized for the new configuration.</p> 191 * 192 * <p>This method can be used to avoid allocating a new bitmap, instead 193 * reusing an existing bitmap's allocation for a new configuration of equal 194 * or lesser size. If the Bitmap's allocation isn't large enough to support 195 * the new configuration, an IllegalArgumentException will be thrown and the 196 * bitmap will not be modified.</p> 197 * 198 * <p>The result of {@link #getByteCount()} will reflect the new configuration, 199 * while {@link #getAllocationByteCount()} will reflect that of the initial 200 * configuration.</p> 201 * 202 * <p>Note: This may change this result of hasAlpha(). When converting to 565, 203 * the new bitmap will always be considered opaque. When converting from 565, 204 * the new bitmap will be considered non-opaque, and will respect the value 205 * set by setPremultiplied().</p> 206 * 207 * <p>WARNING: This method should NOT be called on a bitmap currently used 208 * by the view system. It does not make guarantees about how the underlying 209 * pixel buffer is remapped to the new config, just that the allocation is 210 * reused. Additionally, the view system does not account for bitmap 211 * properties being modifying during use, e.g. while attached to 212 * drawables.</p> 213 * 214 * @see #setWidth(int) 215 * @see #setHeight(int) 216 * @see #setConfig(Config) 217 */ 218 public void reconfigure(int width, int height, Config config) { 219 checkRecycled("Can't call reconfigure() on a recycled bitmap"); 220 if (width <= 0 || height <= 0) { 221 throw new IllegalArgumentException("width and height must be > 0"); 222 } 223 if (!isMutable()) { 224 throw new IllegalStateException("only mutable bitmaps may be reconfigured"); 225 } 226 if (mBuffer == null) { 227 throw new IllegalStateException("native-backed bitmaps may not be reconfigured"); 228 } 229 230 nativeReconfigure(mNativeBitmap, width, height, config.nativeInt, mBuffer.length, 231 mRequestPremultiplied); 232 mWidth = width; 233 mHeight = height; 234 } 235 236 /** 237 * <p>Convenience method for calling {@link #reconfigure(int, int, Config)} 238 * with the current height and config.</p> 239 * 240 * <p>WARNING: this method should not be used on bitmaps currently used by 241 * the view system, see {@link #reconfigure(int, int, Config)} for more 242 * details.</p> 243 * 244 * @see #reconfigure(int, int, Config) 245 * @see #setHeight(int) 246 * @see #setConfig(Config) 247 */ 248 public void setWidth(int width) { 249 reconfigure(width, getHeight(), getConfig()); 250 } 251 252 /** 253 * <p>Convenience method for calling {@link #reconfigure(int, int, Config)} 254 * with the current width and config.</p> 255 * 256 * <p>WARNING: this method should not be used on bitmaps currently used by 257 * the view system, see {@link #reconfigure(int, int, Config)} for more 258 * details.</p> 259 * 260 * @see #reconfigure(int, int, Config) 261 * @see #setWidth(int) 262 * @see #setConfig(Config) 263 */ 264 public void setHeight(int height) { 265 reconfigure(getWidth(), height, getConfig()); 266 } 267 268 /** 269 * <p>Convenience method for calling {@link #reconfigure(int, int, Config)} 270 * with the current height and width.</p> 271 * 272 * <p>WARNING: this method should not be used on bitmaps currently used by 273 * the view system, see {@link #reconfigure(int, int, Config)} for more 274 * details.</p> 275 * 276 * @see #reconfigure(int, int, Config) 277 * @see #setWidth(int) 278 * @see #setHeight(int) 279 */ 280 public void setConfig(Config config) { 281 reconfigure(getWidth(), getHeight(), config); 282 } 283 284 /** 285 * Sets the nine patch chunk. 286 * 287 * @param chunk The definition of the nine patch 288 * 289 * @hide 290 */ 291 public void setNinePatchChunk(byte[] chunk) { 292 mNinePatchChunk = chunk; 293 } 294 295 /** 296 * Free the native object associated with this bitmap, and clear the 297 * reference to the pixel data. This will not free the pixel data synchronously; 298 * it simply allows it to be garbage collected if there are no other references. 299 * The bitmap is marked as "dead", meaning it will throw an exception if 300 * getPixels() or setPixels() is called, and will draw nothing. This operation 301 * cannot be reversed, so it should only be called if you are sure there are no 302 * further uses for the bitmap. This is an advanced call, and normally need 303 * not be called, since the normal GC process will free up this memory when 304 * there are no more references to this bitmap. 305 */ 306 public void recycle() { 307 if (!mRecycled) { 308 if (nativeRecycle(mNativeBitmap)) { 309 // return value indicates whether native pixel object was actually recycled. 310 // false indicates that it is still in use at the native level and these 311 // objects should not be collected now. They will be collected later when the 312 // Bitmap itself is collected. 313 mBuffer = null; 314 mNinePatchChunk = null; 315 } 316 mRecycled = true; 317 } 318 } 319 320 /** 321 * Returns true if this bitmap has been recycled. If so, then it is an error 322 * to try to access its pixels, and the bitmap will not draw. 323 * 324 * @return true if the bitmap has been recycled 325 */ 326 public final boolean isRecycled() { 327 return mRecycled; 328 } 329 330 /** 331 * Returns the generation ID of this bitmap. The generation ID changes 332 * whenever the bitmap is modified. This can be used as an efficient way to 333 * check if a bitmap has changed. 334 * 335 * @return The current generation ID for this bitmap. 336 */ 337 public int getGenerationId() { 338 return nativeGenerationId(mNativeBitmap); 339 } 340 341 /** 342 * This is called by methods that want to throw an exception if the bitmap 343 * has already been recycled. 344 */ 345 private void checkRecycled(String errorMessage) { 346 if (mRecycled) { 347 throw new IllegalStateException(errorMessage); 348 } 349 } 350 351 /** 352 * Common code for checking that x and y are >= 0 353 * 354 * @param x x coordinate to ensure is >= 0 355 * @param y y coordinate to ensure is >= 0 356 */ 357 private static void checkXYSign(int x, int y) { 358 if (x < 0) { 359 throw new IllegalArgumentException("x must be >= 0"); 360 } 361 if (y < 0) { 362 throw new IllegalArgumentException("y must be >= 0"); 363 } 364 } 365 366 /** 367 * Common code for checking that width and height are > 0 368 * 369 * @param width width to ensure is > 0 370 * @param height height to ensure is > 0 371 */ 372 private static void checkWidthHeight(int width, int height) { 373 if (width <= 0) { 374 throw new IllegalArgumentException("width must be > 0"); 375 } 376 if (height <= 0) { 377 throw new IllegalArgumentException("height must be > 0"); 378 } 379 } 380 381 /** 382 * Possible bitmap configurations. A bitmap configuration describes 383 * how pixels are stored. This affects the quality (color depth) as 384 * well as the ability to display transparent/translucent colors. 385 */ 386 public enum Config { 387 // these native values must match up with the enum in SkBitmap.h 388 389 /** 390 * Each pixel is stored as a single translucency (alpha) channel. 391 * This is very useful to efficiently store masks for instance. 392 * No color information is stored. 393 * With this configuration, each pixel requires 1 byte of memory. 394 */ 395 ALPHA_8 (1), 396 397 /** 398 * Each pixel is stored on 2 bytes and only the RGB channels are 399 * encoded: red is stored with 5 bits of precision (32 possible 400 * values), green is stored with 6 bits of precision (64 possible 401 * values) and blue is stored with 5 bits of precision. 402 * 403 * This configuration can produce slight visual artifacts depending 404 * on the configuration of the source. For instance, without 405 * dithering, the result might show a greenish tint. To get better 406 * results dithering should be applied. 407 * 408 * This configuration may be useful when using opaque bitmaps 409 * that do not require high color fidelity. 410 */ 411 RGB_565 (3), 412 413 /** 414 * Each pixel is stored on 2 bytes. The three RGB color channels 415 * and the alpha channel (translucency) are stored with a 4 bits 416 * precision (16 possible values.) 417 * 418 * This configuration is mostly useful if the application needs 419 * to store translucency information but also needs to save 420 * memory. 421 * 422 * It is recommended to use {@link #ARGB_8888} instead of this 423 * configuration. 424 * 425 * Note: as of {@link android.os.Build.VERSION_CODES#KITKAT}, 426 * any bitmap created with this configuration will be created 427 * using {@link #ARGB_8888} instead. 428 * 429 * @deprecated Because of the poor quality of this configuration, 430 * it is advised to use {@link #ARGB_8888} instead. 431 */ 432 @Deprecated 433 ARGB_4444 (4), 434 435 /** 436 * Each pixel is stored on 4 bytes. Each channel (RGB and alpha 437 * for translucency) is stored with 8 bits of precision (256 438 * possible values.) 439 * 440 * This configuration is very flexible and offers the best 441 * quality. It should be used whenever possible. 442 */ 443 ARGB_8888 (5); 444 445 final int nativeInt; 446 447 @SuppressWarnings({"deprecation"}) 448 private static Config sConfigs[] = { 449 null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888 450 }; 451 452 Config(int ni) { 453 this.nativeInt = ni; 454 } 455 456 static Config nativeToConfig(int ni) { 457 return sConfigs[ni]; 458 } 459 } 460 461 /** 462 * <p>Copy the bitmap's pixels into the specified buffer (allocated by the 463 * caller). An exception is thrown if the buffer is not large enough to 464 * hold all of the pixels (taking into account the number of bytes per 465 * pixel) or if the Buffer subclass is not one of the support types 466 * (ByteBuffer, ShortBuffer, IntBuffer).</p> 467 * <p>The content of the bitmap is copied into the buffer as-is. This means 468 * that if this bitmap stores its pixels pre-multiplied 469 * (see {@link #isPremultiplied()}, the values in the buffer will also be 470 * pre-multiplied.</p> 471 * <p>After this method returns, the current position of the buffer is 472 * updated: the position is incremented by the number of elements written 473 * in the buffer.</p> 474 */ 475 public void copyPixelsToBuffer(Buffer dst) { 476 int elements = dst.remaining(); 477 int shift; 478 if (dst instanceof ByteBuffer) { 479 shift = 0; 480 } else if (dst instanceof ShortBuffer) { 481 shift = 1; 482 } else if (dst instanceof IntBuffer) { 483 shift = 2; 484 } else { 485 throw new RuntimeException("unsupported Buffer subclass"); 486 } 487 488 long bufferSize = (long)elements << shift; 489 long pixelSize = getByteCount(); 490 491 if (bufferSize < pixelSize) { 492 throw new RuntimeException("Buffer not large enough for pixels"); 493 } 494 495 nativeCopyPixelsToBuffer(mNativeBitmap, dst); 496 497 // now update the buffer's position 498 int position = dst.position(); 499 position += pixelSize >> shift; 500 dst.position(position); 501 } 502 503 /** 504 * <p>Copy the pixels from the buffer, beginning at the current position, 505 * overwriting the bitmap's pixels. The data in the buffer is not changed 506 * in any way (unlike setPixels(), which converts from unpremultipled 32bit 507 * to whatever the bitmap's native format is.</p> 508 * <p>After this method returns, the current position of the buffer is 509 * updated: the position is incremented by the number of elements read from 510 * the buffer. If you need to read the bitmap from the buffer again you must 511 * first rewind the buffer.</p> 512 */ 513 public void copyPixelsFromBuffer(Buffer src) { 514 checkRecycled("copyPixelsFromBuffer called on recycled bitmap"); 515 516 int elements = src.remaining(); 517 int shift; 518 if (src instanceof ByteBuffer) { 519 shift = 0; 520 } else if (src instanceof ShortBuffer) { 521 shift = 1; 522 } else if (src instanceof IntBuffer) { 523 shift = 2; 524 } else { 525 throw new RuntimeException("unsupported Buffer subclass"); 526 } 527 528 long bufferBytes = (long) elements << shift; 529 long bitmapBytes = getByteCount(); 530 531 if (bufferBytes < bitmapBytes) { 532 throw new RuntimeException("Buffer not large enough for pixels"); 533 } 534 535 nativeCopyPixelsFromBuffer(mNativeBitmap, src); 536 537 // now update the buffer's position 538 int position = src.position(); 539 position += bitmapBytes >> shift; 540 src.position(position); 541 } 542 543 /** 544 * Tries to make a new bitmap based on the dimensions of this bitmap, 545 * setting the new bitmap's config to the one specified, and then copying 546 * this bitmap's pixels into the new bitmap. If the conversion is not 547 * supported, or the allocator fails, then this returns NULL. The returned 548 * bitmap initially has the same density as the original. 549 * 550 * @param config The desired config for the resulting bitmap 551 * @param isMutable True if the resulting bitmap should be mutable (i.e. 552 * its pixels can be modified) 553 * @return the new bitmap, or null if the copy could not be made. 554 */ 555 public Bitmap copy(Config config, boolean isMutable) { 556 checkRecycled("Can't copy a recycled bitmap"); 557 Bitmap b = nativeCopy(mNativeBitmap, config.nativeInt, isMutable); 558 if (b != null) { 559 b.setPremultiplied(mRequestPremultiplied); 560 b.mDensity = mDensity; 561 } 562 return b; 563 } 564 565 /** 566 * Creates a new bitmap, scaled from an existing bitmap, when possible. If the 567 * specified width and height are the same as the current width and height of 568 * the source bitmap, the source bitmap is returned and no new bitmap is 569 * created. 570 * 571 * @param src The source bitmap. 572 * @param dstWidth The new bitmap's desired width. 573 * @param dstHeight The new bitmap's desired height. 574 * @param filter true if the source should be filtered. 575 * @return The new scaled bitmap or the source bitmap if no scaling is required. 576 * @throws IllegalArgumentException if width is <= 0, or height is <= 0 577 */ 578 public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, 579 boolean filter) { 580 Matrix m; 581 synchronized (Bitmap.class) { 582 // small pool of just 1 matrix 583 m = sScaleMatrix; 584 sScaleMatrix = null; 585 } 586 587 if (m == null) { 588 m = new Matrix(); 589 } 590 591 final int width = src.getWidth(); 592 final int height = src.getHeight(); 593 final float sx = dstWidth / (float)width; 594 final float sy = dstHeight / (float)height; 595 m.setScale(sx, sy); 596 Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter); 597 598 synchronized (Bitmap.class) { 599 // do we need to check for null? why not just assign everytime? 600 if (sScaleMatrix == null) { 601 sScaleMatrix = m; 602 } 603 } 604 605 return b; 606 } 607 608 /** 609 * Returns an immutable bitmap from the source bitmap. The new bitmap may 610 * be the same object as source, or a copy may have been made. It is 611 * initialized with the same density as the original bitmap. 612 */ 613 public static Bitmap createBitmap(Bitmap src) { 614 return createBitmap(src, 0, 0, src.getWidth(), src.getHeight()); 615 } 616 617 /** 618 * Returns an immutable bitmap from the specified subset of the source 619 * bitmap. The new bitmap may be the same object as source, or a copy may 620 * have been made. It is initialized with the same density as the original 621 * bitmap. 622 * 623 * @param source The bitmap we are subsetting 624 * @param x The x coordinate of the first pixel in source 625 * @param y The y coordinate of the first pixel in source 626 * @param width The number of pixels in each row 627 * @param height The number of rows 628 * @return A copy of a subset of the source bitmap or the source bitmap itself. 629 * @throws IllegalArgumentException if the x, y, width, height values are 630 * outside of the dimensions of the source bitmap, or width is <= 0, 631 * or height is <= 0 632 */ 633 public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) { 634 return createBitmap(source, x, y, width, height, null, false); 635 } 636 637 /** 638 * Returns an immutable bitmap from subset of the source bitmap, 639 * transformed by the optional matrix. The new bitmap may be the 640 * same object as source, or a copy may have been made. It is 641 * initialized with the same density as the original bitmap. 642 * 643 * If the source bitmap is immutable and the requested subset is the 644 * same as the source bitmap itself, then the source bitmap is 645 * returned and no new bitmap is created. 646 * 647 * @param source The bitmap we are subsetting 648 * @param x The x coordinate of the first pixel in source 649 * @param y The y coordinate of the first pixel in source 650 * @param width The number of pixels in each row 651 * @param height The number of rows 652 * @param m Optional matrix to be applied to the pixels 653 * @param filter true if the source should be filtered. 654 * Only applies if the matrix contains more than just 655 * translation. 656 * @return A bitmap that represents the specified subset of source 657 * @throws IllegalArgumentException if the x, y, width, height values are 658 * outside of the dimensions of the source bitmap, or width is <= 0, 659 * or height is <= 0 660 */ 661 public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, 662 Matrix m, boolean filter) { 663 664 checkXYSign(x, y); 665 checkWidthHeight(width, height); 666 if (x + width > source.getWidth()) { 667 throw new IllegalArgumentException("x + width must be <= bitmap.width()"); 668 } 669 if (y + height > source.getHeight()) { 670 throw new IllegalArgumentException("y + height must be <= bitmap.height()"); 671 } 672 673 // check if we can just return our argument unchanged 674 if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() && 675 height == source.getHeight() && (m == null || m.isIdentity())) { 676 return source; 677 } 678 679 int neww = width; 680 int newh = height; 681 Canvas canvas = new Canvas(); 682 Bitmap bitmap; 683 Paint paint; 684 685 Rect srcR = new Rect(x, y, x + width, y + height); 686 RectF dstR = new RectF(0, 0, width, height); 687 688 Config newConfig = Config.ARGB_8888; 689 final Config config = source.getConfig(); 690 // GIF files generate null configs, assume ARGB_8888 691 if (config != null) { 692 switch (config) { 693 case RGB_565: 694 newConfig = Config.RGB_565; 695 break; 696 case ALPHA_8: 697 newConfig = Config.ALPHA_8; 698 break; 699 //noinspection deprecation 700 case ARGB_4444: 701 case ARGB_8888: 702 default: 703 newConfig = Config.ARGB_8888; 704 break; 705 } 706 } 707 708 if (m == null || m.isIdentity()) { 709 bitmap = createBitmap(neww, newh, newConfig, source.hasAlpha()); 710 paint = null; // not needed 711 } else { 712 final boolean transformed = !m.rectStaysRect(); 713 714 RectF deviceR = new RectF(); 715 m.mapRect(deviceR, dstR); 716 717 neww = Math.round(deviceR.width()); 718 newh = Math.round(deviceR.height()); 719 720 bitmap = createBitmap(neww, newh, transformed ? Config.ARGB_8888 : newConfig, 721 transformed || source.hasAlpha()); 722 723 canvas.translate(-deviceR.left, -deviceR.top); 724 canvas.concat(m); 725 726 paint = new Paint(); 727 paint.setFilterBitmap(filter); 728 if (transformed) { 729 paint.setAntiAlias(true); 730 } 731 } 732 733 // The new bitmap was created from a known bitmap source so assume that 734 // they use the same density 735 bitmap.mDensity = source.mDensity; 736 bitmap.setHasAlpha(source.hasAlpha()); 737 bitmap.setPremultiplied(source.mRequestPremultiplied); 738 739 canvas.setBitmap(bitmap); 740 canvas.drawBitmap(source, srcR, dstR, paint); 741 canvas.setBitmap(null); 742 743 return bitmap; 744 } 745 746 /** 747 * Returns a mutable bitmap with the specified width and height. Its 748 * initial density is as per {@link #getDensity}. 749 * 750 * @param width The width of the bitmap 751 * @param height The height of the bitmap 752 * @param config The bitmap config to create. 753 * @throws IllegalArgumentException if the width or height are <= 0 754 */ 755 public static Bitmap createBitmap(int width, int height, Config config) { 756 return createBitmap(width, height, config, true); 757 } 758 759 /** 760 * Returns a mutable bitmap with the specified width and height. Its 761 * initial density is determined from the given {@link DisplayMetrics}. 762 * 763 * @param display Display metrics for the display this bitmap will be 764 * drawn on. 765 * @param width The width of the bitmap 766 * @param height The height of the bitmap 767 * @param config The bitmap config to create. 768 * @throws IllegalArgumentException if the width or height are <= 0 769 */ 770 public static Bitmap createBitmap(DisplayMetrics display, int width, 771 int height, Config config) { 772 return createBitmap(display, width, height, config, true); 773 } 774 775 /** 776 * Returns a mutable bitmap with the specified width and height. Its 777 * initial density is as per {@link #getDensity}. 778 * 779 * @param width The width of the bitmap 780 * @param height The height of the bitmap 781 * @param config The bitmap config to create. 782 * @param hasAlpha If the bitmap is ARGB_8888 this flag can be used to mark the 783 * bitmap as opaque. Doing so will clear the bitmap in black 784 * instead of transparent. 785 * 786 * @throws IllegalArgumentException if the width or height are <= 0 787 */ 788 private static Bitmap createBitmap(int width, int height, Config config, boolean hasAlpha) { 789 return createBitmap(null, width, height, config, hasAlpha); 790 } 791 792 /** 793 * Returns a mutable bitmap with the specified width and height. Its 794 * initial density is determined from the given {@link DisplayMetrics}. 795 * 796 * @param display Display metrics for the display this bitmap will be 797 * drawn on. 798 * @param width The width of the bitmap 799 * @param height The height of the bitmap 800 * @param config The bitmap config to create. 801 * @param hasAlpha If the bitmap is ARGB_8888 this flag can be used to mark the 802 * bitmap as opaque. Doing so will clear the bitmap in black 803 * instead of transparent. 804 * 805 * @throws IllegalArgumentException if the width or height are <= 0 806 */ 807 private static Bitmap createBitmap(DisplayMetrics display, int width, int height, 808 Config config, boolean hasAlpha) { 809 if (width <= 0 || height <= 0) { 810 throw new IllegalArgumentException("width and height must be > 0"); 811 } 812 Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true); 813 if (display != null) { 814 bm.mDensity = display.densityDpi; 815 } 816 bm.setHasAlpha(hasAlpha); 817 if (config == Config.ARGB_8888 && !hasAlpha) { 818 nativeErase(bm.mNativeBitmap, 0xff000000); 819 } 820 // No need to initialize the bitmap to zeroes with other configs; 821 // it is backed by a VM byte array which is by definition preinitialized 822 // to all zeroes. 823 return bm; 824 } 825 826 /** 827 * Returns a immutable bitmap with the specified width and height, with each 828 * pixel value set to the corresponding value in the colors array. Its 829 * initial density is as per {@link #getDensity}. 830 * 831 * @param colors Array of {@link Color} used to initialize the pixels. 832 * @param offset Number of values to skip before the first color in the 833 * array of colors. 834 * @param stride Number of colors in the array between rows (must be >= 835 * width or <= -width). 836 * @param width The width of the bitmap 837 * @param height The height of the bitmap 838 * @param config The bitmap config to create. If the config does not 839 * support per-pixel alpha (e.g. RGB_565), then the alpha 840 * bytes in the colors[] will be ignored (assumed to be FF) 841 * @throws IllegalArgumentException if the width or height are <= 0, or if 842 * the color array's length is less than the number of pixels. 843 */ 844 public static Bitmap createBitmap(int colors[], int offset, int stride, 845 int width, int height, Config config) { 846 return createBitmap(null, colors, offset, stride, width, height, config); 847 } 848 849 /** 850 * Returns a immutable bitmap with the specified width and height, with each 851 * pixel value set to the corresponding value in the colors array. Its 852 * initial density is determined from the given {@link DisplayMetrics}. 853 * 854 * @param display Display metrics for the display this bitmap will be 855 * drawn on. 856 * @param colors Array of {@link Color} used to initialize the pixels. 857 * @param offset Number of values to skip before the first color in the 858 * array of colors. 859 * @param stride Number of colors in the array between rows (must be >= 860 * width or <= -width). 861 * @param width The width of the bitmap 862 * @param height The height of the bitmap 863 * @param config The bitmap config to create. If the config does not 864 * support per-pixel alpha (e.g. RGB_565), then the alpha 865 * bytes in the colors[] will be ignored (assumed to be FF) 866 * @throws IllegalArgumentException if the width or height are <= 0, or if 867 * the color array's length is less than the number of pixels. 868 */ 869 public static Bitmap createBitmap(DisplayMetrics display, int colors[], 870 int offset, int stride, int width, int height, Config config) { 871 872 checkWidthHeight(width, height); 873 if (Math.abs(stride) < width) { 874 throw new IllegalArgumentException("abs(stride) must be >= width"); 875 } 876 int lastScanline = offset + (height - 1) * stride; 877 int length = colors.length; 878 if (offset < 0 || (offset + width > length) || lastScanline < 0 || 879 (lastScanline + width > length)) { 880 throw new ArrayIndexOutOfBoundsException(); 881 } 882 if (width <= 0 || height <= 0) { 883 throw new IllegalArgumentException("width and height must be > 0"); 884 } 885 Bitmap bm = nativeCreate(colors, offset, stride, width, height, 886 config.nativeInt, false); 887 if (display != null) { 888 bm.mDensity = display.densityDpi; 889 } 890 return bm; 891 } 892 893 /** 894 * Returns a immutable bitmap with the specified width and height, with each 895 * pixel value set to the corresponding value in the colors array. Its 896 * initial density is as per {@link #getDensity}. 897 * 898 * @param colors Array of {@link Color} used to initialize the pixels. 899 * This array must be at least as large as width * height. 900 * @param width The width of the bitmap 901 * @param height The height of the bitmap 902 * @param config The bitmap config to create. If the config does not 903 * support per-pixel alpha (e.g. RGB_565), then the alpha 904 * bytes in the colors[] will be ignored (assumed to be FF) 905 * @throws IllegalArgumentException if the width or height are <= 0, or if 906 * the color array's length is less than the number of pixels. 907 */ 908 public static Bitmap createBitmap(int colors[], int width, int height, Config config) { 909 return createBitmap(null, colors, 0, width, width, height, config); 910 } 911 912 /** 913 * Returns a immutable bitmap with the specified width and height, with each 914 * pixel value set to the corresponding value in the colors array. Its 915 * initial density is determined from the given {@link DisplayMetrics}. 916 * 917 * @param display Display metrics for the display this bitmap will be 918 * drawn on. 919 * @param colors Array of {@link Color} used to initialize the pixels. 920 * This array must be at least as large as width * height. 921 * @param width The width of the bitmap 922 * @param height The height of the bitmap 923 * @param config The bitmap config to create. If the config does not 924 * support per-pixel alpha (e.g. RGB_565), then the alpha 925 * bytes in the colors[] will be ignored (assumed to be FF) 926 * @throws IllegalArgumentException if the width or height are <= 0, or if 927 * the color array's length is less than the number of pixels. 928 */ 929 public static Bitmap createBitmap(DisplayMetrics display, int colors[], 930 int width, int height, Config config) { 931 return createBitmap(display, colors, 0, width, width, height, config); 932 } 933 934 /** 935 * Returns an optional array of private data, used by the UI system for 936 * some bitmaps. Not intended to be called by applications. 937 */ 938 public byte[] getNinePatchChunk() { 939 return mNinePatchChunk; 940 } 941 942 /** 943 * Populates a rectangle with the bitmap's optical insets. 944 * 945 * @param outInsets Rect to populate with optical insets 946 * @hide 947 */ 948 public void getOpticalInsets(@NonNull Rect outInsets) { 949 if (mOpticalInsets == null) { 950 outInsets.setEmpty(); 951 } else { 952 outInsets.left = mOpticalInsets[0]; 953 outInsets.top = mOpticalInsets[1]; 954 outInsets.right = mOpticalInsets[2]; 955 outInsets.bottom = mOpticalInsets[3]; 956 } 957 } 958 959 /** 960 * Specifies the known formats a bitmap can be compressed into 961 */ 962 public enum CompressFormat { 963 JPEG (0), 964 PNG (1), 965 WEBP (2); 966 967 CompressFormat(int nativeInt) { 968 this.nativeInt = nativeInt; 969 } 970 final int nativeInt; 971 } 972 973 /** 974 * Number of bytes of temp storage we use for communicating between the 975 * native compressor and the java OutputStream. 976 */ 977 private final static int WORKING_COMPRESS_STORAGE = 4096; 978 979 /** 980 * Write a compressed version of the bitmap to the specified outputstream. 981 * If this returns true, the bitmap can be reconstructed by passing a 982 * corresponding inputstream to BitmapFactory.decodeStream(). Note: not 983 * all Formats support all bitmap configs directly, so it is possible that 984 * the returned bitmap from BitmapFactory could be in a different bitdepth, 985 * and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque 986 * pixels). 987 * 988 * @param format The format of the compressed image 989 * @param quality Hint to the compressor, 0-100. 0 meaning compress for 990 * small size, 100 meaning compress for max quality. Some 991 * formats, like PNG which is lossless, will ignore the 992 * quality setting 993 * @param stream The outputstream to write the compressed data. 994 * @return true if successfully compressed to the specified stream. 995 */ 996 public boolean compress(CompressFormat format, int quality, OutputStream stream) { 997 checkRecycled("Can't compress a recycled bitmap"); 998 // do explicit check before calling the native method 999 if (stream == null) { 1000 throw new NullPointerException(); 1001 } 1002 if (quality < 0 || quality > 100) { 1003 throw new IllegalArgumentException("quality must be 0..100"); 1004 } 1005 return nativeCompress(mNativeBitmap, format.nativeInt, quality, 1006 stream, new byte[WORKING_COMPRESS_STORAGE]); 1007 } 1008 1009 /** 1010 * Returns true if the bitmap is marked as mutable (i.e. can be drawn into) 1011 */ 1012 public final boolean isMutable() { 1013 return mIsMutable; 1014 } 1015 1016 /** 1017 * <p>Indicates whether pixels stored in this bitmaps are stored pre-multiplied. 1018 * When a pixel is pre-multiplied, the RGB components have been multiplied by 1019 * the alpha component. For instance, if the original color is a 50% 1020 * translucent red <code>(128, 255, 0, 0)</code>, the pre-multiplied form is 1021 * <code>(128, 128, 0, 0)</code>.</p> 1022 * 1023 * <p>This method always returns false if {@link #getConfig()} is 1024 * {@link Bitmap.Config#RGB_565}.</p> 1025 * 1026 * <p>The return value is undefined if {@link #getConfig()} is 1027 * {@link Bitmap.Config#ALPHA_8}.</p> 1028 * 1029 * <p>This method only returns true if {@link #hasAlpha()} returns true. 1030 * A bitmap with no alpha channel can be used both as a pre-multiplied and 1031 * as a non pre-multiplied bitmap.</p> 1032 * 1033 * <p>Only pre-multiplied bitmaps may be drawn by the view system or 1034 * {@link Canvas}. If a non-pre-multiplied bitmap with an alpha channel is 1035 * drawn to a Canvas, a RuntimeException will be thrown.</p> 1036 * 1037 * @return true if the underlying pixels have been pre-multiplied, false 1038 * otherwise 1039 * 1040 * @see Bitmap#setPremultiplied(boolean) 1041 * @see BitmapFactory.Options#inPremultiplied 1042 */ 1043 public final boolean isPremultiplied() { 1044 return nativeIsPremultiplied(mNativeBitmap); 1045 } 1046 1047 /** 1048 * Sets whether the bitmap should treat its data as pre-multiplied. 1049 * 1050 * <p>Bitmaps are always treated as pre-multiplied by the view system and 1051 * {@link Canvas} for performance reasons. Storing un-pre-multiplied data in 1052 * a Bitmap (through {@link #setPixel}, {@link #setPixels}, or {@link 1053 * BitmapFactory.Options#inPremultiplied BitmapFactory.Options.inPremultiplied}) 1054 * can lead to incorrect blending if drawn by the framework.</p> 1055 * 1056 * <p>This method will not affect the behavior of a bitmap without an alpha 1057 * channel, or if {@link #hasAlpha()} returns false.</p> 1058 * 1059 * <p>Calling {@link #createBitmap} or {@link #createScaledBitmap} with a source 1060 * Bitmap whose colors are not pre-multiplied may result in a RuntimeException, 1061 * since those functions require drawing the source, which is not supported for 1062 * un-pre-multiplied Bitmaps.</p> 1063 * 1064 * @see Bitmap#isPremultiplied() 1065 * @see BitmapFactory.Options#inPremultiplied 1066 */ 1067 public final void setPremultiplied(boolean premultiplied) { 1068 mRequestPremultiplied = premultiplied; 1069 nativeSetPremultiplied(mNativeBitmap, premultiplied); 1070 } 1071 1072 /** Returns the bitmap's width */ 1073 public final int getWidth() { 1074 return mWidth; 1075 } 1076 1077 /** Returns the bitmap's height */ 1078 public final int getHeight() { 1079 return mHeight; 1080 } 1081 1082 /** 1083 * Convenience for calling {@link #getScaledWidth(int)} with the target 1084 * density of the given {@link Canvas}. 1085 */ 1086 public int getScaledWidth(Canvas canvas) { 1087 return scaleFromDensity(getWidth(), mDensity, canvas.mDensity); 1088 } 1089 1090 /** 1091 * Convenience for calling {@link #getScaledHeight(int)} with the target 1092 * density of the given {@link Canvas}. 1093 */ 1094 public int getScaledHeight(Canvas canvas) { 1095 return scaleFromDensity(getHeight(), mDensity, canvas.mDensity); 1096 } 1097 1098 /** 1099 * Convenience for calling {@link #getScaledWidth(int)} with the target 1100 * density of the given {@link DisplayMetrics}. 1101 */ 1102 public int getScaledWidth(DisplayMetrics metrics) { 1103 return scaleFromDensity(getWidth(), mDensity, metrics.densityDpi); 1104 } 1105 1106 /** 1107 * Convenience for calling {@link #getScaledHeight(int)} with the target 1108 * density of the given {@link DisplayMetrics}. 1109 */ 1110 public int getScaledHeight(DisplayMetrics metrics) { 1111 return scaleFromDensity(getHeight(), mDensity, metrics.densityDpi); 1112 } 1113 1114 /** 1115 * Convenience method that returns the width of this bitmap divided 1116 * by the density scale factor. 1117 * 1118 * @param targetDensity The density of the target canvas of the bitmap. 1119 * @return The scaled width of this bitmap, according to the density scale factor. 1120 */ 1121 public int getScaledWidth(int targetDensity) { 1122 return scaleFromDensity(getWidth(), mDensity, targetDensity); 1123 } 1124 1125 /** 1126 * Convenience method that returns the height of this bitmap divided 1127 * by the density scale factor. 1128 * 1129 * @param targetDensity The density of the target canvas of the bitmap. 1130 * @return The scaled height of this bitmap, according to the density scale factor. 1131 */ 1132 public int getScaledHeight(int targetDensity) { 1133 return scaleFromDensity(getHeight(), mDensity, targetDensity); 1134 } 1135 1136 /** 1137 * @hide 1138 */ 1139 static public int scaleFromDensity(int size, int sdensity, int tdensity) { 1140 if (sdensity == DENSITY_NONE || tdensity == DENSITY_NONE || sdensity == tdensity) { 1141 return size; 1142 } 1143 1144 // Scale by tdensity / sdensity, rounding up. 1145 return ((size * tdensity) + (sdensity >> 1)) / sdensity; 1146 } 1147 1148 /** 1149 * Return the number of bytes between rows in the bitmap's pixels. Note that 1150 * this refers to the pixels as stored natively by the bitmap. If you call 1151 * getPixels() or setPixels(), then the pixels are uniformly treated as 1152 * 32bit values, packed according to the Color class. 1153 * 1154 * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, this method 1155 * should not be used to calculate the memory usage of the bitmap. Instead, 1156 * see {@link #getAllocationByteCount()}. 1157 * 1158 * @return number of bytes between rows of the native bitmap pixels. 1159 */ 1160 public final int getRowBytes() { 1161 return nativeRowBytes(mNativeBitmap); 1162 } 1163 1164 /** 1165 * Returns the minimum number of bytes that can be used to store this bitmap's pixels. 1166 * 1167 * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, the result of this method can 1168 * no longer be used to determine memory usage of a bitmap. See {@link 1169 * #getAllocationByteCount()}.</p> 1170 */ 1171 public final int getByteCount() { 1172 // int result permits bitmaps up to 46,340 x 46,340 1173 return getRowBytes() * getHeight(); 1174 } 1175 1176 /** 1177 * Returns the size of the allocated memory used to store this bitmap's pixels. 1178 * 1179 * <p>This can be larger than the result of {@link #getByteCount()} if a bitmap is reused to 1180 * decode other bitmaps of smaller size, or by manual reconfiguration. See {@link 1181 * #reconfigure(int, int, Config)}, {@link #setWidth(int)}, {@link #setHeight(int)}, {@link 1182 * #setConfig(Bitmap.Config)}, and {@link BitmapFactory.Options#inBitmap 1183 * BitmapFactory.Options.inBitmap}. If a bitmap is not modified in this way, this value will be 1184 * the same as that returned by {@link #getByteCount()}.</p> 1185 * 1186 * <p>This value will not change over the lifetime of a Bitmap.</p> 1187 * 1188 * @see #reconfigure(int, int, Config) 1189 */ 1190 public final int getAllocationByteCount() { 1191 if (mBuffer == null) { 1192 // native backed bitmaps don't support reconfiguration, 1193 // so alloc size is always content size 1194 return getByteCount(); 1195 } 1196 return mBuffer.length; 1197 } 1198 1199 /** 1200 * If the bitmap's internal config is in one of the public formats, return 1201 * that config, otherwise return null. 1202 */ 1203 public final Config getConfig() { 1204 return Config.nativeToConfig(nativeConfig(mNativeBitmap)); 1205 } 1206 1207 /** Returns true if the bitmap's config supports per-pixel alpha, and 1208 * if the pixels may contain non-opaque alpha values. For some configs, 1209 * this is always false (e.g. RGB_565), since they do not support per-pixel 1210 * alpha. However, for configs that do, the bitmap may be flagged to be 1211 * known that all of its pixels are opaque. In this case hasAlpha() will 1212 * also return false. If a config such as ARGB_8888 is not so flagged, 1213 * it will return true by default. 1214 */ 1215 public final boolean hasAlpha() { 1216 return nativeHasAlpha(mNativeBitmap); 1217 } 1218 1219 /** 1220 * Tell the bitmap if all of the pixels are known to be opaque (false) 1221 * or if some of the pixels may contain non-opaque alpha values (true). 1222 * Note, for some configs (e.g. RGB_565) this call is ignored, since it 1223 * does not support per-pixel alpha values. 1224 * 1225 * This is meant as a drawing hint, as in some cases a bitmap that is known 1226 * to be opaque can take a faster drawing case than one that may have 1227 * non-opaque per-pixel alpha values. 1228 */ 1229 public void setHasAlpha(boolean hasAlpha) { 1230 nativeSetHasAlpha(mNativeBitmap, hasAlpha, mRequestPremultiplied); 1231 } 1232 1233 /** 1234 * Indicates whether the renderer responsible for drawing this 1235 * bitmap should attempt to use mipmaps when this bitmap is drawn 1236 * scaled down. 1237 * 1238 * If you know that you are going to draw this bitmap at less than 1239 * 50% of its original size, you may be able to obtain a higher 1240 * quality 1241 * 1242 * This property is only a suggestion that can be ignored by the 1243 * renderer. It is not guaranteed to have any effect. 1244 * 1245 * @return true if the renderer should attempt to use mipmaps, 1246 * false otherwise 1247 * 1248 * @see #setHasMipMap(boolean) 1249 */ 1250 public final boolean hasMipMap() { 1251 return nativeHasMipMap(mNativeBitmap); 1252 } 1253 1254 /** 1255 * Set a hint for the renderer responsible for drawing this bitmap 1256 * indicating that it should attempt to use mipmaps when this bitmap 1257 * is drawn scaled down. 1258 * 1259 * If you know that you are going to draw this bitmap at less than 1260 * 50% of its original size, you may be able to obtain a higher 1261 * quality by turning this property on. 1262 * 1263 * Note that if the renderer respects this hint it might have to 1264 * allocate extra memory to hold the mipmap levels for this bitmap. 1265 * 1266 * This property is only a suggestion that can be ignored by the 1267 * renderer. It is not guaranteed to have any effect. 1268 * 1269 * @param hasMipMap indicates whether the renderer should attempt 1270 * to use mipmaps 1271 * 1272 * @see #hasMipMap() 1273 */ 1274 public final void setHasMipMap(boolean hasMipMap) { 1275 nativeSetHasMipMap(mNativeBitmap, hasMipMap); 1276 } 1277 1278 /** 1279 * Fills the bitmap's pixels with the specified {@link Color}. 1280 * 1281 * @throws IllegalStateException if the bitmap is not mutable. 1282 */ 1283 public void eraseColor(int c) { 1284 checkRecycled("Can't erase a recycled bitmap"); 1285 if (!isMutable()) { 1286 throw new IllegalStateException("cannot erase immutable bitmaps"); 1287 } 1288 nativeErase(mNativeBitmap, c); 1289 } 1290 1291 /** 1292 * Returns the {@link Color} at the specified location. Throws an exception 1293 * if x or y are out of bounds (negative or >= to the width or height 1294 * respectively). The returned color is a non-premultiplied ARGB value. 1295 * 1296 * @param x The x coordinate (0...width-1) of the pixel to return 1297 * @param y The y coordinate (0...height-1) of the pixel to return 1298 * @return The argb {@link Color} at the specified coordinate 1299 * @throws IllegalArgumentException if x, y exceed the bitmap's bounds 1300 */ 1301 public int getPixel(int x, int y) { 1302 checkRecycled("Can't call getPixel() on a recycled bitmap"); 1303 checkPixelAccess(x, y); 1304 return nativeGetPixel(mNativeBitmap, x, y); 1305 } 1306 1307 /** 1308 * Returns in pixels[] a copy of the data in the bitmap. Each value is 1309 * a packed int representing a {@link Color}. The stride parameter allows 1310 * the caller to allow for gaps in the returned pixels array between 1311 * rows. For normal packed results, just pass width for the stride value. 1312 * The returned colors are non-premultiplied ARGB values. 1313 * 1314 * @param pixels The array to receive the bitmap's colors 1315 * @param offset The first index to write into pixels[] 1316 * @param stride The number of entries in pixels[] to skip between 1317 * rows (must be >= bitmap's width). Can be negative. 1318 * @param x The x coordinate of the first pixel to read from 1319 * the bitmap 1320 * @param y The y coordinate of the first pixel to read from 1321 * the bitmap 1322 * @param width The number of pixels to read from each row 1323 * @param height The number of rows to read 1324 * 1325 * @throws IllegalArgumentException if x, y, width, height exceed the 1326 * bounds of the bitmap, or if abs(stride) < width. 1327 * @throws ArrayIndexOutOfBoundsException if the pixels array is too small 1328 * to receive the specified number of pixels. 1329 */ 1330 public void getPixels(int[] pixels, int offset, int stride, 1331 int x, int y, int width, int height) { 1332 checkRecycled("Can't call getPixels() on a recycled bitmap"); 1333 if (width == 0 || height == 0) { 1334 return; // nothing to do 1335 } 1336 checkPixelsAccess(x, y, width, height, offset, stride, pixels); 1337 nativeGetPixels(mNativeBitmap, pixels, offset, stride, 1338 x, y, width, height); 1339 } 1340 1341 /** 1342 * Shared code to check for illegal arguments passed to getPixel() 1343 * or setPixel() 1344 * 1345 * @param x x coordinate of the pixel 1346 * @param y y coordinate of the pixel 1347 */ 1348 private void checkPixelAccess(int x, int y) { 1349 checkXYSign(x, y); 1350 if (x >= getWidth()) { 1351 throw new IllegalArgumentException("x must be < bitmap.width()"); 1352 } 1353 if (y >= getHeight()) { 1354 throw new IllegalArgumentException("y must be < bitmap.height()"); 1355 } 1356 } 1357 1358 /** 1359 * Shared code to check for illegal arguments passed to getPixels() 1360 * or setPixels() 1361 * 1362 * @param x left edge of the area of pixels to access 1363 * @param y top edge of the area of pixels to access 1364 * @param width width of the area of pixels to access 1365 * @param height height of the area of pixels to access 1366 * @param offset offset into pixels[] array 1367 * @param stride number of elements in pixels[] between each logical row 1368 * @param pixels array to hold the area of pixels being accessed 1369 */ 1370 private void checkPixelsAccess(int x, int y, int width, int height, 1371 int offset, int stride, int pixels[]) { 1372 checkXYSign(x, y); 1373 if (width < 0) { 1374 throw new IllegalArgumentException("width must be >= 0"); 1375 } 1376 if (height < 0) { 1377 throw new IllegalArgumentException("height must be >= 0"); 1378 } 1379 if (x + width > getWidth()) { 1380 throw new IllegalArgumentException( 1381 "x + width must be <= bitmap.width()"); 1382 } 1383 if (y + height > getHeight()) { 1384 throw new IllegalArgumentException( 1385 "y + height must be <= bitmap.height()"); 1386 } 1387 if (Math.abs(stride) < width) { 1388 throw new IllegalArgumentException("abs(stride) must be >= width"); 1389 } 1390 int lastScanline = offset + (height - 1) * stride; 1391 int length = pixels.length; 1392 if (offset < 0 || (offset + width > length) 1393 || lastScanline < 0 1394 || (lastScanline + width > length)) { 1395 throw new ArrayIndexOutOfBoundsException(); 1396 } 1397 } 1398 1399 /** 1400 * <p>Write the specified {@link Color} into the bitmap (assuming it is 1401 * mutable) at the x,y coordinate. The color must be a 1402 * non-premultiplied ARGB value.</p> 1403 * 1404 * @param x The x coordinate of the pixel to replace (0...width-1) 1405 * @param y The y coordinate of the pixel to replace (0...height-1) 1406 * @param color The ARGB color to write into the bitmap 1407 * 1408 * @throws IllegalStateException if the bitmap is not mutable 1409 * @throws IllegalArgumentException if x, y are outside of the bitmap's 1410 * bounds. 1411 */ 1412 public void setPixel(int x, int y, int color) { 1413 checkRecycled("Can't call setPixel() on a recycled bitmap"); 1414 if (!isMutable()) { 1415 throw new IllegalStateException(); 1416 } 1417 checkPixelAccess(x, y); 1418 nativeSetPixel(mNativeBitmap, x, y, color); 1419 } 1420 1421 /** 1422 * <p>Replace pixels in the bitmap with the colors in the array. Each element 1423 * in the array is a packed int prepresenting a non-premultiplied ARGB 1424 * {@link Color}.</p> 1425 * 1426 * @param pixels The colors to write to the bitmap 1427 * @param offset The index of the first color to read from pixels[] 1428 * @param stride The number of colors in pixels[] to skip between rows. 1429 * Normally this value will be the same as the width of 1430 * the bitmap, but it can be larger (or negative). 1431 * @param x The x coordinate of the first pixel to write to in 1432 * the bitmap. 1433 * @param y The y coordinate of the first pixel to write to in 1434 * the bitmap. 1435 * @param width The number of colors to copy from pixels[] per row 1436 * @param height The number of rows to write to the bitmap 1437 * 1438 * @throws IllegalStateException if the bitmap is not mutable 1439 * @throws IllegalArgumentException if x, y, width, height are outside of 1440 * the bitmap's bounds. 1441 * @throws ArrayIndexOutOfBoundsException if the pixels array is too small 1442 * to receive the specified number of pixels. 1443 */ 1444 public void setPixels(int[] pixels, int offset, int stride, 1445 int x, int y, int width, int height) { 1446 checkRecycled("Can't call setPixels() on a recycled bitmap"); 1447 if (!isMutable()) { 1448 throw new IllegalStateException(); 1449 } 1450 if (width == 0 || height == 0) { 1451 return; // nothing to do 1452 } 1453 checkPixelsAccess(x, y, width, height, offset, stride, pixels); 1454 nativeSetPixels(mNativeBitmap, pixels, offset, stride, 1455 x, y, width, height); 1456 } 1457 1458 public static final Parcelable.Creator<Bitmap> CREATOR 1459 = new Parcelable.Creator<Bitmap>() { 1460 /** 1461 * Rebuilds a bitmap previously stored with writeToParcel(). 1462 * 1463 * @param p Parcel object to read the bitmap from 1464 * @return a new bitmap created from the data in the parcel 1465 */ 1466 public Bitmap createFromParcel(Parcel p) { 1467 Bitmap bm = nativeCreateFromParcel(p); 1468 if (bm == null) { 1469 throw new RuntimeException("Failed to unparcel Bitmap"); 1470 } 1471 return bm; 1472 } 1473 public Bitmap[] newArray(int size) { 1474 return new Bitmap[size]; 1475 } 1476 }; 1477 1478 /** 1479 * No special parcel contents. 1480 */ 1481 public int describeContents() { 1482 return 0; 1483 } 1484 1485 /** 1486 * Write the bitmap and its pixels to the parcel. The bitmap can be 1487 * rebuilt from the parcel by calling CREATOR.createFromParcel(). 1488 * @param p Parcel object to write the bitmap data into 1489 */ 1490 public void writeToParcel(Parcel p, int flags) { 1491 checkRecycled("Can't parcel a recycled bitmap"); 1492 if (!nativeWriteToParcel(mNativeBitmap, mIsMutable, mDensity, p)) { 1493 throw new RuntimeException("native writeToParcel failed"); 1494 } 1495 } 1496 1497 /** 1498 * Returns a new bitmap that captures the alpha values of the original. 1499 * This may be drawn with Canvas.drawBitmap(), where the color(s) will be 1500 * taken from the paint that is passed to the draw call. 1501 * 1502 * @return new bitmap containing the alpha channel of the original bitmap. 1503 */ 1504 public Bitmap extractAlpha() { 1505 return extractAlpha(null, null); 1506 } 1507 1508 /** 1509 * Returns a new bitmap that captures the alpha values of the original. 1510 * These values may be affected by the optional Paint parameter, which 1511 * can contain its own alpha, and may also contain a MaskFilter which 1512 * could change the actual dimensions of the resulting bitmap (e.g. 1513 * a blur maskfilter might enlarge the resulting bitmap). If offsetXY 1514 * is not null, it returns the amount to offset the returned bitmap so 1515 * that it will logically align with the original. For example, if the 1516 * paint contains a blur of radius 2, then offsetXY[] would contains 1517 * -2, -2, so that drawing the alpha bitmap offset by (-2, -2) and then 1518 * drawing the original would result in the blur visually aligning with 1519 * the original. 1520 * 1521 * <p>The initial density of the returned bitmap is the same as the original's. 1522 * 1523 * @param paint Optional paint used to modify the alpha values in the 1524 * resulting bitmap. Pass null for default behavior. 1525 * @param offsetXY Optional array that returns the X (index 0) and Y 1526 * (index 1) offset needed to position the returned bitmap 1527 * so that it visually lines up with the original. 1528 * @return new bitmap containing the (optionally modified by paint) alpha 1529 * channel of the original bitmap. This may be drawn with 1530 * Canvas.drawBitmap(), where the color(s) will be taken from the 1531 * paint that is passed to the draw call. 1532 */ 1533 public Bitmap extractAlpha(Paint paint, int[] offsetXY) { 1534 checkRecycled("Can't extractAlpha on a recycled bitmap"); 1535 long nativePaint = paint != null ? paint.mNativePaint : 0; 1536 Bitmap bm = nativeExtractAlpha(mNativeBitmap, nativePaint, offsetXY); 1537 if (bm == null) { 1538 throw new RuntimeException("Failed to extractAlpha on Bitmap"); 1539 } 1540 bm.mDensity = mDensity; 1541 return bm; 1542 } 1543 1544 /** 1545 * Given another bitmap, return true if it has the same dimensions, config, 1546 * and pixel data as this bitmap. If any of those differ, return false. 1547 * If other is null, return false. 1548 */ 1549 public boolean sameAs(Bitmap other) { 1550 return this == other || (other != null && nativeSameAs(mNativeBitmap, other.mNativeBitmap)); 1551 } 1552 1553 /** 1554 * Rebuilds any caches associated with the bitmap that are used for 1555 * drawing it. In the case of purgeable bitmaps, this call will attempt to 1556 * ensure that the pixels have been decoded. 1557 * If this is called on more than one bitmap in sequence, the priority is 1558 * given in LRU order (i.e. the last bitmap called will be given highest 1559 * priority). 1560 * 1561 * For bitmaps with no associated caches, this call is effectively a no-op, 1562 * and therefore is harmless. 1563 */ 1564 public void prepareToDraw() { 1565 nativePrepareToDraw(mNativeBitmap); 1566 } 1567 1568 private static class BitmapFinalizer { 1569 private final long mNativeBitmap; 1570 1571 BitmapFinalizer(long nativeBitmap) { 1572 mNativeBitmap = nativeBitmap; 1573 } 1574 1575 @Override 1576 public void finalize() { 1577 try { 1578 super.finalize(); 1579 } catch (Throwable t) { 1580 // Ignore 1581 } finally { 1582 nativeDestructor(mNativeBitmap); 1583 } 1584 } 1585 } 1586 1587 //////////// native methods 1588 1589 private static native Bitmap nativeCreate(int[] colors, int offset, 1590 int stride, int width, int height, 1591 int nativeConfig, boolean mutable); 1592 private static native Bitmap nativeCopy(long nativeSrcBitmap, int nativeConfig, 1593 boolean isMutable); 1594 private static native void nativeDestructor(long nativeBitmap); 1595 private static native boolean nativeRecycle(long nativeBitmap); 1596 private static native void nativeReconfigure(long nativeBitmap, int width, int height, 1597 int config, int allocSize, 1598 boolean isPremultiplied); 1599 1600 private static native boolean nativeCompress(long nativeBitmap, int format, 1601 int quality, OutputStream stream, 1602 byte[] tempStorage); 1603 private static native void nativeErase(long nativeBitmap, int color); 1604 private static native int nativeRowBytes(long nativeBitmap); 1605 private static native int nativeConfig(long nativeBitmap); 1606 1607 private static native int nativeGetPixel(long nativeBitmap, int x, int y); 1608 private static native void nativeGetPixels(long nativeBitmap, int[] pixels, 1609 int offset, int stride, int x, int y, 1610 int width, int height); 1611 1612 private static native void nativeSetPixel(long nativeBitmap, int x, int y, int color); 1613 private static native void nativeSetPixels(long nativeBitmap, int[] colors, 1614 int offset, int stride, int x, int y, 1615 int width, int height); 1616 private static native void nativeCopyPixelsToBuffer(long nativeBitmap, 1617 Buffer dst); 1618 private static native void nativeCopyPixelsFromBuffer(long nativeBitmap, Buffer src); 1619 private static native int nativeGenerationId(long nativeBitmap); 1620 1621 private static native Bitmap nativeCreateFromParcel(Parcel p); 1622 // returns true on success 1623 private static native boolean nativeWriteToParcel(long nativeBitmap, 1624 boolean isMutable, 1625 int density, 1626 Parcel p); 1627 // returns a new bitmap built from the native bitmap's alpha, and the paint 1628 private static native Bitmap nativeExtractAlpha(long nativeBitmap, 1629 long nativePaint, 1630 int[] offsetXY); 1631 1632 private static native void nativePrepareToDraw(long nativeBitmap); 1633 private static native boolean nativeHasAlpha(long nativeBitmap); 1634 private static native boolean nativeIsPremultiplied(long nativeBitmap); 1635 private static native void nativeSetPremultiplied(long nativeBitmap, 1636 boolean isPremul); 1637 private static native void nativeSetHasAlpha(long nativeBitmap, 1638 boolean hasAlpha, 1639 boolean requestPremul); 1640 private static native boolean nativeHasMipMap(long nativeBitmap); 1641 private static native void nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap); 1642 private static native boolean nativeSameAs(long nativeBitmap0, long nativeBitmap1); 1643 1644 /* package */ final long ni() { 1645 return mNativeBitmap; 1646 } 1647} 1648