Java Code Examples for com.facebook.common.references.CloseableReference#get()
The following examples show how to use
com.facebook.common.references.CloseableReference#get() .
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: ScalingBlurPostprocessor.java From fresco with MIT License | 6 votes |
@Override public CloseableReference<Bitmap> process( Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) { final CloseableReference<Bitmap> bitmapRef = bitmapFactory.createBitmap( sourceBitmap.getWidth() / mScaleRatio, sourceBitmap.getHeight() / mScaleRatio); try { final Bitmap destBitmap = bitmapRef.get(); final Canvas canvas = new Canvas(destBitmap); canvas.drawBitmap( sourceBitmap, null, new Rect(0, 0, destBitmap.getWidth(), destBitmap.getHeight()), mPaint); NativeBlurFilter.iterativeBoxBlur( destBitmap, mIterations, Math.max(1, mBlurRadius / mScaleRatio)); return CloseableReference.cloneOrNull(bitmapRef); } finally { CloseableReference.closeSafely(bitmapRef); } }
Example 2
Source File: AnimatedImageFactoryImpl.java From fresco with MIT License | 6 votes |
/** * Decodes a GIF into a CloseableImage. * * @param encodedImage encoded image (native byte array holding the encoded bytes and meta data) * @param options the options for the decode * @param bitmapConfig the Bitmap.Config used to generate the output bitmaps * @return a {@link CloseableImage} for the GIF image */ public CloseableImage decodeGif( final EncodedImage encodedImage, final ImageDecodeOptions options, final Bitmap.Config bitmapConfig) { if (sGifAnimatedImageDecoder == null) { throw new UnsupportedOperationException( "To encode animated gif please add the dependency " + "to the animated-gif module"); } final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef(); Preconditions.checkNotNull(bytesRef); try { final PooledByteBuffer input = bytesRef.get(); AnimatedImage gifImage; if (input.getByteBuffer() != null) { gifImage = sGifAnimatedImageDecoder.decodeFromByteBuffer(input.getByteBuffer(), options); } else { gifImage = sGifAnimatedImageDecoder.decodeFromNativeMemory( input.getNativePtr(), input.size(), options); } return getCloseableImage(options, gifImage, bitmapConfig); } finally { CloseableReference.closeSafely(bytesRef); } }
Example 3
Source File: AnimatedImageFactoryImpl.java From fresco with MIT License | 6 votes |
/** * Decode a WebP into a CloseableImage. * * @param encodedImage encoded image (native byte array holding the encoded bytes and meta data) * @param options the options for the decode * @param bitmapConfig the Bitmap.Config used to generate the output bitmaps * @return a {@link CloseableImage} for the WebP image */ public CloseableImage decodeWebP( final EncodedImage encodedImage, final ImageDecodeOptions options, final Bitmap.Config bitmapConfig) { if (sWebpAnimatedImageDecoder == null) { throw new UnsupportedOperationException( "To encode animated webp please add the dependency " + "to the animated-webp module"); } final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef(); Preconditions.checkNotNull(bytesRef); try { final PooledByteBuffer input = bytesRef.get(); AnimatedImage webPImage; if (input.getByteBuffer() != null) { webPImage = sWebpAnimatedImageDecoder.decodeFromByteBuffer(input.getByteBuffer(), options); } else { webPImage = sWebpAnimatedImageDecoder.decodeFromNativeMemory( input.getNativePtr(), input.size(), options); } return getCloseableImage(options, webPImage, bitmapConfig); } finally { CloseableReference.closeSafely(bytesRef); } }
Example 4
Source File: PipelineDraweeController.java From fresco with MIT License | 5 votes |
@Override protected Drawable createDrawable(CloseableReference<CloseableImage> image) { try { if (FrescoSystrace.isTracing()) { FrescoSystrace.beginSection("PipelineDraweeController#createDrawable"); } Preconditions.checkState(CloseableReference.isValid(image)); CloseableImage closeableImage = image.get(); maybeUpdateDebugOverlay(closeableImage); Drawable drawable = maybeCreateDrawableFromFactories(mCustomDrawableFactories, closeableImage); if (drawable != null) { return drawable; } drawable = maybeCreateDrawableFromFactories(mGlobalDrawableFactories, closeableImage); if (drawable != null) { return drawable; } drawable = mDefaultDrawableFactory.createDrawable(closeableImage); if (drawable != null) { return drawable; } throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage); } finally { if (FrescoSystrace.isTracing()) { FrescoSystrace.endSection(); } } }
Example 5
Source File: PipelineDraweeController.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
@Override protected Drawable createDrawable(CloseableReference<CloseableImage> image) { Preconditions.checkState(CloseableReference.isValid(image)); CloseableImage closeableImage = image.get(); if (closeableImage instanceof CloseableStaticBitmap) { CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage; return new BitmapDrawable(mResources, closeableStaticBitmap.getUnderlyingBitmap()); } else if (closeableImage instanceof CloseableAnimatedImage) { return mAnimatedDrawableFactory.create( ((CloseableAnimatedImage) closeableImage).getImageResult()); } else { throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage); } }
Example 6
Source File: AnimatedDrawableCachingBackendImpl.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
/** * Copies the source bitmap for the specified frame and caches it. * * @param frameNumber the frame number * @param sourceBitmap the rendered bitmap to be cached (after copying) */ private void copyAndCacheBitmapDuringRendering(int frameNumber, Bitmap sourceBitmap) { CloseableReference<Bitmap> destBitmapReference = obtainBitmapInternal(); try { Canvas copyCanvas = new Canvas(destBitmapReference.get()); copyCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC); copyCanvas.drawBitmap(sourceBitmap, 0, 0, null); maybeCacheRenderedBitmap(frameNumber, destBitmapReference); } finally { destBitmapReference.close(); } }
Example 7
Source File: FrescoVitoRegionDecoder.java From fresco with MIT License | 5 votes |
private void maybeApplyTransformation( @Nullable BitmapTransformation transformation, CloseableReference<Bitmap> bitmapReference) { if (transformation == null) { return; } Bitmap bitmap = bitmapReference.get(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1 && transformation.modifiesTransparency()) { bitmap.setHasAlpha(true); } transformation.transform(bitmap); }
Example 8
Source File: ProgressiveJpegParser.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
/** * If this is the first time calling this method, the buffer will be checked to make sure it * starts with SOI marker (0xffd8). If the image has been identified as a non-JPEG, data will be * ignored and false will be returned immediately on all subsequent calls. * * This object maintains state of the position of the last read byte. On repeated calls to this * method, it will continue from where it left off. * * @param dataBufferRef Next set of bytes received by the caller * @return true if a new full scan has been found */ public boolean parseMoreData(final CloseableReference<PooledByteBuffer> dataBufferRef) { if (mParserState == NOT_A_JPEG) { return false; } final PooledByteBuffer dataBuffer = dataBufferRef.get(); final int dataBufferSize = dataBuffer.size(); // Is there any new data to parse? // mBytesParsed might be greater than size of dataBuffer - that happens when // we skip more data than is available to read inside doParseMoreData method if (dataBufferSize <= mBytesParsed) { return false; } final InputStream bufferedDataStream = new PooledByteArrayBufferedInputStream( dataBuffer.getStream(), mByteArrayPool.get(BUFFER_SIZE), mByteArrayPool); try { StreamUtil.skip(bufferedDataStream, mBytesParsed); return doParseMoreData(bufferedDataStream); } catch (IOException ioe) { // Does not happen - streams returned by PooledByteBuffers do not throw IOExceptions Throwables.propagate(ioe); return false; } finally { Closeables.closeQuietly(bufferedDataStream); } }
Example 9
Source File: PorterDuffXfermodePostProcessor.java From react-native-image-filter-kit with MIT License | 5 votes |
@Override protected CloseableReference<Bitmap> processComposition( Bitmap dstImage, Bitmap srcImage, PlatformBitmapFactory bitmapFactory ) { final CloseableReference<Bitmap> outRef = bitmapFactory.createBitmap( canvasExtent(dstImage.getWidth(), srcImage.getWidth(), mWidth), canvasExtent(dstImage.getHeight(), srcImage.getHeight(), mHeight) ); try { final Canvas canvas = new Canvas(outRef.get()); final int flags = Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG; final Paint paint = new Paint(flags); if (mSwapImages) { drawSrc(canvas, srcImage, paint); } else { drawDst(canvas, dstImage, paint); } paint.setXfermode(new PorterDuffXfermode(mMode)); if (mSwapImages) { drawDst(canvas, dstImage, paint); } else { drawSrc(canvas, srcImage, paint); } return CloseableReference.cloneOrNull(outRef); } finally { CloseableReference.closeSafely(outRef); } }
Example 10
Source File: ArtBitmapFactory.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
/** * Creates a bitmap from encoded JPEG bytes. Supports a partial JPEG image. * * @param pooledByteBufferRef the reference to the encoded bytes * @param length the number of encoded bytes in the buffer * @return the bitmap * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated */ CloseableReference<Bitmap> decodeJPEGFromPooledByteBuffer( CloseableReference<PooledByteBuffer> pooledByteBufferRef, int length) { final PooledByteBuffer pooledByteBuffer = pooledByteBufferRef.get(); final InputStream jpegBufferInputStream = pooledByteBuffer.getStream(); jpegBufferInputStream.mark(Integer.MAX_VALUE); boolean isJpegComplete; try { jpegBufferInputStream.skip(length - 2); isJpegComplete = (jpegBufferInputStream.read() == JfifUtil.MARKER_FIRST_BYTE) && (jpegBufferInputStream.read() == JfifUtil.MARKER_EOI); jpegBufferInputStream.reset(); } catch (IOException ioe) { throw new RuntimeException(ioe); } InputStream jpegDataStream = jpegBufferInputStream; if (pooledByteBuffer.size() > length) { jpegDataStream = new LimitedInputStream(jpegDataStream, length); } if (!isJpegComplete) { jpegDataStream = new TailAppendingInputStream(jpegDataStream, EOI_TAIL); } return doDecodeStaticImage(jpegDataStream); }
Example 11
Source File: SharedByteArrayTest.java From fresco with MIT License | 5 votes |
@Test public void testGet_Realloc() { CloseableReference<byte[]> arrayRef = mArray.get(1); final byte[] smallArray = arrayRef.get(); arrayRef.close(); arrayRef = mArray.get(7); assertEquals(8, arrayRef.get().length); assertSame(mArray.mByteArraySoftRef.get(), arrayRef.get()); assertNotSame(smallArray, arrayRef.get()); }
Example 12
Source File: BitmapPrepareProducer.java From fresco with MIT License | 5 votes |
private void internalPrepareBitmap(CloseableReference<CloseableImage> newResult) { if (newResult == null || !newResult.isValid()) { return; } final CloseableImage closeableImage = newResult.get(); if (closeableImage == null || closeableImage.isClosed()) { return; } if (closeableImage instanceof CloseableStaticBitmap) { final CloseableStaticBitmap staticBitmap = (CloseableStaticBitmap) closeableImage; final Bitmap bitmap = staticBitmap.getUnderlyingBitmap(); if (bitmap == null) { return; } final int bitmapByteCount = bitmap.getRowBytes() * bitmap.getHeight(); if (bitmapByteCount < mMinBitmapSizeBytes) { return; } if (bitmapByteCount > mMaxBitmapSizeBytes) { return; } bitmap.prepareToDraw(); } }
Example 13
Source File: DalvikPurgeableDecoder.java From fresco with MIT License | 5 votes |
@VisibleForTesting public static boolean endsWithEOI(CloseableReference<PooledByteBuffer> bytesRef, int length) { PooledByteBuffer buffer = bytesRef.get(); return length >= 2 && buffer.read(length - 2) == (byte) JfifUtil.MARKER_FIRST_BYTE && buffer.read(length - 1) == (byte) JfifUtil.MARKER_EOI; }
Example 14
Source File: FrescoControllerImpl.java From fresco with MIT License | 4 votes |
@VisibleForTesting void displayResultOrError( FrescoState frescoState, @Nullable CloseableReference<CloseableImage> result, boolean wasImmediate, @Nullable DataSource<CloseableReference<CloseableImage>> dataSource) { if (FrescoSystrace.isTracing()) { FrescoSystrace.beginSection("FrescoControllerImpl#displayResultOrError"); } try { if (!frescoState.isAttached()) { return; } CloseableImage closeableImage; if (result == null || (closeableImage = result.get()) == null) { Drawable errorDrawable = mFrescoContext .getHierarcher() .buildErrorDrawable(frescoState.getResources(), frescoState.getImageOptions()); displayErrorImage(frescoState, errorDrawable); if (mControllerListener2 != null) { mControllerListener2.onFailure( VitoUtils.getStringId(frescoState.getId()), null, obtainExtras( dataSource, null, frescoState.getFrescoDrawable(), frescoState.getCallerContext(), null)); } frescoState.onFailure(frescoState.getId(), errorDrawable, null); return; } frescoState.getFrescoDrawable().setProgressDrawable(null); InstrumentedDrawable.Listener instrumentedListener = maybeGetInstrumentedListener(frescoState, result); Drawable actualDrawable = mFrescoContext .getHierarcher() .setupActualImageDrawable( frescoState.getFrescoDrawable(), frescoState.getResources(), frescoState.getImageOptions(), result, frescoState.getActualImageWrapper(), wasImmediate, instrumentedListener); if (mControllerListener2 != null) { mControllerListener2.onFinalImageSet( VitoUtils.getStringId(frescoState.getId()), closeableImage, obtainExtras( dataSource, closeableImage, frescoState.getFrescoDrawable(), frescoState.getCallerContext(), null)); } frescoState.onFinalImageSet( frescoState.getId(), frescoState.getImageOrigin(), closeableImage, actualDrawable); mFrescoContext .getHierarcher() .setupDebugOverlayDrawable( frescoState.getFrescoDrawable(), frescoState.getFrescoDrawable().getOverlayDrawable(), mDebugOverlayFactory.create(frescoState)); } finally { if (FrescoSystrace.isTracing()) { FrescoSystrace.endSection(); } } }
Example 15
Source File: PipelineDraweeController.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 4 votes |
@Override protected ImageInfo getImageInfo(CloseableReference<CloseableImage> image) { Preconditions.checkState(CloseableReference.isValid(image)); return image.get(); }
Example 16
Source File: MyBaseBitmapDataSubscriber.java From ImageLoader with Apache License 2.0 | 4 votes |
@Override public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) { if (!dataSource.isFinished()) { return; } CloseableReference<CloseableImage> closeableImageRef = dataSource.getResult(); Bitmap bitmap = null; if (closeableImageRef != null && closeableImageRef.get() instanceof CloseableBitmap) { bitmap = ((CloseableBitmap) closeableImageRef.get()).getUnderlyingBitmap(); } if(bitmap!=null ){ if(bitmap.isRecycled()){ onFail(new Throwable("bitmap.isRecycled")); }else { onNewResultImpl(bitmap,dataSource); } return; } //如果bitmap为空 Log.e("onNewResultImpl","finalUrl :"+finalUrl); File cacheFile = ImageLoader.getActualLoader().getFileFromDiskCache(finalUrl); if(cacheFile ==null){ onFail(new Throwable("file cache is null:"+finalUrl)); return; } //还要判断文件是不是gif格式的 if (!"gif".equalsIgnoreCase(MyUtil.getRealType(cacheFile))){ onFail(new Throwable("file cache is not gif:"+finalUrl)); return; } Bitmap bitmapGif = GifUtils.getBitmapFromGifFile(cacheFile);//拿到gif第一帧的bitmap if(width>0 && height >0) { bitmapGif = MyUtil.compressBitmap(bitmapGif, false, width, height);//将bitmap压缩到指定宽高。 } if (bitmapGif != null) { onNewResultImpl(bitmapGif,dataSource); } else { onFail(new Throwable("can not create bitmap from gif file:"+cacheFile.getAbsolutePath())); } /* try { onNewResultImpl(bitmap); } finally { //CloseableReference.closeSafely(closeableImageRef); }*/ }
Example 17
Source File: CloseableImageCopier.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 4 votes |
public boolean isCloseableImageCopyable(CloseableReference<CloseableImage> closeableImageRef) { return closeableImageRef != null && closeableImageRef.get() instanceof CloseableStaticBitmap; }
Example 18
Source File: PipelineDraweeController.java From fresco with MIT License | 4 votes |
@Override protected ImageInfo getImageInfo(CloseableReference<CloseableImage> image) { Preconditions.checkState(CloseableReference.isValid(image)); return image.get(); }
Example 19
Source File: RenderscriptCompositionPostProcessor.java From react-native-image-filter-kit with MIT License | 4 votes |
@Override protected CloseableReference<Bitmap> processComposition( Bitmap dstImage, Bitmap srcImage, PlatformBitmapFactory bitmapFactory ) { final int width = canvasExtent(dstImage.getWidth(), srcImage.getWidth(), mWidth); final int height = canvasExtent(dstImage.getHeight(), srcImage.getHeight(), mHeight); final CloseableReference<Bitmap> tmpDstRef = bitmapFactory.createBitmap(width, height); final CloseableReference<Bitmap> tmpSrcRef = bitmapFactory.createBitmap(width, height); final CloseableReference<Bitmap> outRef = bitmapFactory.createBitmap(width, height); try { final Bitmap out = outRef.get(); final Bitmap tmpDst = tmpDstRef.get(); final Bitmap tmpSrc = tmpSrcRef.get(); final Canvas dstCanvas = new Canvas(tmpDst); final Canvas srcCanvas = new Canvas(tmpSrc); final int flags = Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG; final Paint paint = new Paint(flags); dstCanvas.drawBitmap( dstImage, bitmapTransform(width, height, dstImage.getWidth(), dstImage.getHeight(), mDstTransform), paint ); srcCanvas.drawBitmap( srcImage, bitmapTransform(width, height, srcImage.getWidth(), srcImage.getHeight(), mSrcTransform), paint ); processRenderscriptComposition( mSwapImages ? tmpSrc : tmpDst, mSwapImages ? tmpDst : tmpSrc, out ); return CloseableReference.cloneOrNull(outRef); } finally { CloseableReference.closeSafely(tmpDstRef); CloseableReference.closeSafely(tmpSrcRef); CloseableReference.closeSafely(outRef); } }
Example 20
Source File: PlatformBitmapFactory.java From fresco with MIT License | 3 votes |
/** * Creates a bitmap with the specified width and height. Its initial density is determined from * the given DisplayMetrics. * * @param colors The colors to write to the bitmap * @param width The width of the bitmap * @param height The height of the bitmap * @param config The bitmap config to create * @param callerContext the Tag to track who create the Bitmap * @return a reference to the bitmap * @throws TooManyBitmapsException if the pool is full * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated */ public CloseableReference<Bitmap> createBitmap( int[] colors, int width, int height, Bitmap.Config config, @Nullable Object callerContext) { CloseableReference<Bitmap> bitmapRef = createBitmapInternal(width, height, config); Bitmap bitmap = bitmapRef.get(); bitmap.setPixels(colors, 0, width, 0, 0, width, height); return bitmapRef; }