com.facebook.imagepipeline.nativecode.Bitmaps Java Examples

The following examples show how to use com.facebook.imagepipeline.nativecode.Bitmaps. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArtBitmapFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
   * Options returned by this method are configured with mDecodeBuffer which is GuardedBy("this")
   */
  @SuppressLint("NewApi")
private BitmapFactory.Options getDecodeOptionsForStream(InputStream inputStream) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inTempStorage = mDecodeBuffer;

    options.inJustDecodeBounds = true;
    // fill outWidth and outHeight
    BitmapFactory.decodeStream(inputStream, null, options);
    if (options.outWidth == -1 || options.outHeight == -1) {
      throw new IllegalArgumentException();
    }

    options.inJustDecodeBounds = false;
    options.inDither = true;
    options.inPreferredConfig = Bitmaps.BITMAP_CONFIG;
    options.inMutable = true;

    return options;
  }
 
Example #2
Source File: DalvikBitmapFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Performs the actual decoding.
 */
private CloseableReference<Bitmap> doDecodeBitmap(
    final byte[] encodedBytes,
    final int length) {
  final Bitmap bitmap = decodeAsPurgeableBitmap(encodedBytes, length);

  try {
    // Real decoding happens here - if the image was corrupted, this will throw an exception
    Bitmaps.pinBitmap(bitmap);
  } catch (Exception e) {
    bitmap.recycle();
    throw Throwables.propagate(e);
  }

  if (!mUnpooledBitmapsCounter.increase(bitmap)) {
    bitmap.recycle();
    throw new TooManyBitmapsException();
  }

  return CloseableReference.of(bitmap, mUnpooledBitmapsReleaser);
}
 
Example #3
Source File: DalvikBitmapFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Decode the input into a purgeable bitmap
 *
 * @param encodedBytes the input encoded image
 * @return a purgeable bitmap
 */
@SuppressLint("NewApi")
private static synchronized Bitmap decodeAsPurgeableBitmap(byte[] encodedBytes, int size) {
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inDither = true; // known to improve picture quality at low cost
  options.inPreferredConfig = Bitmaps.BITMAP_CONFIG;
  // Decode the image into a 'purgeable' bitmap that lives on the ashmem heap
  options.inPurgeable = true;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    options.inMutable = true;  // no known perf difference; allows postprocessing to work
  }
  Bitmap bitmap = BitmapFactory.decodeByteArray(
      encodedBytes,
      0,
      size,
      options);
  return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
}
 
Example #4
Source File: CloseableImageCopier.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
private CloseableReference<CloseableImage> copyCloseableStaticBitmap(
    final CloseableReference<CloseableImage> closeableStaticBitmapRef) {
  Bitmap sourceBitmap = ((CloseableStaticBitmap) closeableStaticBitmapRef.get())
      .getUnderlyingBitmap();
  CloseableReference<Bitmap> bitmapRef = mPlatformBitmapFactory.createBitmap(
      sourceBitmap.getWidth(),
      sourceBitmap.getHeight());
  try {
    Bitmap destinationBitmap = bitmapRef.get();
    Preconditions.checkState(!destinationBitmap.isRecycled());
    Preconditions.checkState(destinationBitmap.isMutable());
    Bitmaps.copyBitmap(destinationBitmap, sourceBitmap);
    return CloseableReference.<CloseableImage>of(
        new CloseableStaticBitmap(bitmapRef, ImmutableQualityInfo.FULL_QUALITY));
  } finally {
    bitmapRef.close();
  }
}
 
Example #5
Source File: DokitFrescoPostprocessor.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
/**
 * Copies the content of {@code sourceBitmap} to {@code destBitmap}. Both bitmaps must have the
 * same width and height. If their {@link Bitmap.Config} are identical, the memory is directly
 * copied. Otherwise, the {@code sourceBitmap} is drawn into {@code destBitmap}.
 */
private static void internalCopyBitmap(Bitmap destBitmap, Bitmap sourceBitmap) {
    if (destBitmap.getConfig() == sourceBitmap.getConfig()) {
        Bitmaps.copyBitmap(destBitmap, sourceBitmap);
    } else {
        // The bitmap configurations might be different when the source bitmap's configuration is
        // null, because it uses an internal configuration and the destination bitmap's configuration
        // is the FALLBACK_BITMAP_CONFIGURATION. This is the case for static images for animated GIFs.
        Canvas canvas = new Canvas(destBitmap);
        canvas.drawBitmap(sourceBitmap, 0, 0, null);
    }
}
 
Example #6
Source File: GingerbreadPurgeableDecoderTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {

  mBitmap = MockBitmapFactory.create();
  mBitmapCounter = new BitmapCounter(MAX_BITMAP_COUNT, MAX_BITMAP_SIZE);

  mockStatic(DalvikPurgeableDecoder.class);
  when(DalvikPurgeableDecoder.getBitmapFactoryOptions(anyInt(), any(Bitmap.Config.class)))
      .thenCallRealMethod();
  when(DalvikPurgeableDecoder.endsWithEOI(any(CloseableReference.class), anyInt()))
      .thenCallRealMethod();
  mockStatic(BitmapCounterProvider.class);
  when(BitmapCounterProvider.get()).thenReturn(mBitmapCounter);

  mockStatic(BitmapFactory.class);
  when(BitmapFactory.decodeFileDescriptor(
          any(FileDescriptor.class), any(Rect.class), any(BitmapFactory.Options.class)))
      .thenReturn(mBitmap);

  mInputBuf = new byte[LENGTH];
  PooledByteBuffer input = new TrivialPooledByteBuffer(mInputBuf, POINTER);
  mByteBufferRef = CloseableReference.of(input);
  mEncodedImage = new EncodedImage(mByteBufferRef);

  mDecodeBuf = new byte[LENGTH + 2];
  mDecodeBufRef = CloseableReference.of(mDecodeBuf, mock(ResourceReleaser.class));

  mockStatic(Bitmaps.class);
  mGingerbreadPurgeableDecoder = new GingerbreadPurgeableDecoder();
}
 
Example #7
Source File: KitKatPurgeableDecoderTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {
  mFlexByteArrayPool = mock(FlexByteArrayPool.class);

  mBitmap = MockBitmapFactory.create();
  mBitmapCounter = new BitmapCounter(MAX_BITMAP_COUNT, MAX_BITMAP_SIZE);

  mockStatic(DalvikPurgeableDecoder.class);
  when(DalvikPurgeableDecoder.getBitmapFactoryOptions(anyInt(), any(Bitmap.Config.class)))
      .thenCallRealMethod();
  when(DalvikPurgeableDecoder.endsWithEOI(any(CloseableReference.class), anyInt()))
      .thenCallRealMethod();
  mockStatic(BitmapCounterProvider.class);
  when(BitmapCounterProvider.get()).thenReturn(mBitmapCounter);

  mockStatic(BitmapFactory.class);
  when(BitmapFactory.decodeByteArray(
          any(byte[].class), anyInt(), anyInt(), any(BitmapFactory.Options.class)))
      .thenReturn(mBitmap);

  mInputBuf = new byte[LENGTH];
  PooledByteBuffer input = new TrivialPooledByteBuffer(mInputBuf, POINTER);
  mByteBufferRef = CloseableReference.of(input);
  mEncodedImage = new EncodedImage(mByteBufferRef);

  mDecodeBuf = new byte[LENGTH + 2];
  mDecodeBufRef = CloseableReference.of(mDecodeBuf, mock(ResourceReleaser.class));
  when(mFlexByteArrayPool.get(Integer.valueOf(LENGTH))).thenReturn(mDecodeBufRef);

  mockStatic(Bitmaps.class);
  mKitKatPurgeableDecoder = new KitKatPurgeableDecoder(mFlexByteArrayPool);
}
 
Example #8
Source File: BitmapPool.java    From FanXin-based-HuanXin with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the bucketed size of the value.
 * We don't check the 'validity' of the value (beyond the not-null check). That's handled
 * in {@link #isReusable(Bitmap)}
 * @param value the value
 * @return bucketed size of the value
 */
@Override
protected int getBucketedSizeForValue(Bitmap value) {
  Preconditions.checkNotNull(value);
  final int allocationByteCount = value.getAllocationByteCount();
  return allocationByteCount / Bitmaps.BYTES_PER_PIXEL;
}
 
Example #9
Source File: BitmapPool.java    From FanXin-based-HuanXin with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Determine if this bitmap is reusable (i.e.) if subsequent {@link #get(int)} requests can
 * use this value.
 * The bitmap is reusable if
 *  - it has not already been recycled AND
 *  - it is mutable AND
 *  - it has the desired bitmap-config
 * @param value the value to test for reusability
 * @return true, if the bitmap can be reused
 */
@Override
protected boolean isReusable(Bitmap value) {
  Preconditions.checkNotNull(value);
  return !value.isRecycled() &&
      value.isMutable() &&
      Bitmaps.BITMAP_CONFIG.equals(value.getConfig());
}
 
Example #10
Source File: ArtBitmapFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Creates a bitmap of the specified width and height.
 *
 * @param width the width of the bitmap
 * @param height the height of the bitmap
 * @return a reference to the bitmap
 * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
CloseableReference<Bitmap> createBitmap(int width, int height) {
  Bitmap bitmap = mBitmapPool.get(width * height);
  Bitmaps.reconfigureBitmap(bitmap, width, height);
  return CloseableReference.of(bitmap, mBitmapPool);
}
 
Example #11
Source File: BitmapPool.java    From FanXin-based-HuanXin with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Allocate a bitmap with the specified width and height.
 * The bitmap's config is controlled by the BITMAP_CONFIG we've defined above.
 * @param size the 'size' of the bitmap
 * @return a new bitmap with the specified dimensions
 */
@Override
protected Bitmap alloc(int size) {
  return Bitmap.createBitmap(1, size, Bitmaps.BITMAP_CONFIG);
}
 
Example #12
Source File: BitmapPool.java    From FanXin-based-HuanXin with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets the size in bytes for the given bucketed size
 * This will use the BYTES_PER_PIXEL constant defined above (which is dependent on the specific
 * BITMAP_CONFIG above)
 * @param bucketedSize the bucketed size
 * @return size in bytes
 */
@Override
protected int getSizeInBytes(int bucketedSize) {
  return Bitmaps.BYTES_PER_PIXEL * bucketedSize;
}