@Before public void setUp() { MockitoAnnotations.initMocks(this); gifHeader = Mockito.spy(new GifHeader()); when(parser.parseHeader()).thenReturn(gifHeader); when(parserPool.obtain(isA(ByteBuffer.class))).thenReturn(parser); when(decoderFactory.build(isA(GifDecoder.BitmapProvider.class), eq(gifHeader), isA(ByteBuffer.class), anyInt())) .thenReturn(gifDecoder); List<ImageHeaderParser> parsers = new ArrayList<ImageHeaderParser>(); parsers.add(new DefaultImageHeaderParser()); options = new Options(); decoder = new ByteBufferGifDecoder( RuntimeEnvironment.application, parsers, bitmapPool, new LruArrayPool(ARRAY_POOL_SIZE_BYTES), parserPool, decoderFactory); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); gifHeader = Mockito.spy(new GifHeader()); when(parser.parseHeader()).thenReturn(gifHeader); when(parserPool.obtain(isA(ByteBuffer.class))).thenReturn(parser); when(decoderFactory.build(isA(GifDecoder.BitmapProvider.class), eq(gifHeader), isA(ByteBuffer.class), anyInt())) .thenReturn(gifDecoder); List<ImageHeaderParser> parsers = new ArrayList<>(); parsers.add(new DefaultImageHeaderParser()); options = new Options(); decoder = new ByteBufferGifDecoder( RuntimeEnvironment.application, parsers, bitmapPool, new LruArrayPool(ARRAY_POOL_SIZE_BYTES), parserPool, decoderFactory); }
public GifState(GifHeader header, byte[] data, Context context, Transformation<Bitmap> frameTransformation, int targetWidth, int targetHeight, GifDecoder.BitmapProvider provider, BitmapPool bitmapPool, Bitmap firstFrame) { if (firstFrame == null) { throw new NullPointerException("The first frame of the GIF must not be null"); } gifHeader = header; this.data = data; this.bitmapPool = bitmapPool; this.firstFrame = firstFrame; this.context = context.getApplicationContext(); this.frameTransformation = frameTransformation; this.targetWidth = targetWidth; this.targetHeight = targetHeight; bitmapProvider = provider; }
private GifTextureResource decode(byte[] data, int width, int height, GifHeaderParser parser, GifDecoder decoder) { final GifHeader header = parser.parseHeader(); if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) { // If we couldn't decode the GIF, we will end up with a frame count of 0. return null; } Bitmap firstFrame = decodeFirstFrame(decoder, header, data); if (firstFrame == null) { return null; } Transformation<Bitmap> unitTransformation = UnitTransformation.get(); GifTexture gifDrawable = new GifTexture(context, provider, bitmapPool, unitTransformation, width, height, header, data, firstFrame); return new GifTextureResource(gifDrawable); }
private GifDecoder decodeHeaders(ByteBuffer data) { GifHeaderParser parser = factory.buildParser(); parser.setData(data); GifHeader header = parser.parseHeader(); GifDecoder decoder = factory.buildDecoder(provider); decoder.setData(header, data); decoder.advance(); return decoder; }
@Test public void testSetsDataOnParserBeforeParsingHeader() { ByteBuffer data = ByteBuffer.allocate(1); when(gifDrawable.getBuffer()).thenReturn(data); GifHeader header = mock(GifHeader.class); when(parser.parseHeader()).thenReturn(header); encoder.encode(resource, file, options); InOrder order = inOrder(parser, decoder); order.verify(parser).setData(eq(data)); order.verify(parser).parseHeader(); order.verify(decoder).setData(header, data); }
private GifDrawableResource decode(ByteBuffer byteBuffer, int width, int height, GifHeaderParser parser) { long startTime = LogTime.getLogTime(); final GifHeader header = parser.parseHeader(); if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) { // If we couldn't decode the GIF, we will end up with a frame count of 0. return null; } int sampleSize = getSampleSize(header, width, height); GifDecoder gifDecoder = gifDecoderFactory.build(provider, header, byteBuffer, sampleSize); gifDecoder.advance(); Bitmap firstFrame = gifDecoder.getNextFrame(); if (firstFrame == null) { return null; } Transformation<Bitmap> unitTransformation = UnitTransformation.get(); GifDrawable gifDrawable = new GifDrawable(context, gifDecoder, bitmapPool, unitTransformation, width, height, firstFrame); if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Decoded GIF from stream in " + LogTime.getElapsedMillis(startTime)); } return new GifDrawableResource(gifDrawable); }
private static int getSampleSize(GifHeader gifHeader, int targetWidth, int targetHeight) { int exactSampleSize = Math.min(gifHeader.getHeight() / targetHeight, gifHeader.getWidth() / targetWidth); int powerOfTwoSampleSize = exactSampleSize == 0 ? 0 : Integer.highestOneBit(exactSampleSize); // Although functionally equivalent to 0 for BitmapFactory, 1 is a safer default for our code // than 0. int sampleSize = Math.max(1, powerOfTwoSampleSize); if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Downsampling GIF" + ", sampleSize: " + sampleSize + ", target dimens: [" + targetWidth + "x" + targetHeight + "]" + ", actual dimens: [" + gifHeader.getWidth() + "x" + gifHeader.getHeight() + "]"); } return sampleSize; }
private GifDrawableResource decode( ByteBuffer byteBuffer, int width, int height, GifHeaderParser parser, Options options) { long startTime = LogTime.getLogTime(); final GifHeader header = parser.parseHeader(); if (header.getNumFrames() <= 0 || header.getStatus() != GifDecoder.STATUS_OK) { // If we couldn't decode the GIF, we will end up with a frame count of 0. return null; } Bitmap.Config config = options.get(GifOptions.DECODE_FORMAT) == DecodeFormat.PREFER_RGB_565 ? Bitmap.Config.RGB_565 : Bitmap.Config.ARGB_8888; int sampleSize = getSampleSize(header, width, height); GifDecoder gifDecoder = gifDecoderFactory.build(provider, header, byteBuffer, sampleSize); gifDecoder.setDefaultBitmapConfig(config); gifDecoder.advance(); Bitmap firstFrame = gifDecoder.getNextFrame(); if (firstFrame == null) { return null; } Transformation<Bitmap> unitTransformation = UnitTransformation.get(); GifDrawable gifDrawable = new GifDrawable(context, gifDecoder, unitTransformation, width, height, firstFrame); if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Decoded GIF from stream in " + LogTime.getElapsedMillis(startTime)); } return new GifDrawableResource(gifDrawable); }
private static int getSampleSize(GifHeader gifHeader, int targetWidth, int targetHeight) { int exactSampleSize = Math.min(gifHeader.getHeight() / targetHeight, gifHeader.getWidth() / targetWidth); int powerOfTwoSampleSize = exactSampleSize == 0 ? 0 : Integer.highestOneBit(exactSampleSize); // Although functionally equivalent to 0 for BitmapFactory, 1 is a safer default for our code // than 0. int sampleSize = Math.max(1, powerOfTwoSampleSize); if (Log.isLoggable(TAG, Log.VERBOSE) && sampleSize > 1) { Log.v(TAG, "Downsampling GIF" + ", sampleSize: " + sampleSize + ", target dimens: [" + targetWidth + "x" + targetHeight + "]" + ", actual dimens: [" + gifHeader.getWidth() + "x" + gifHeader.getHeight() + "]"); } return sampleSize; }
public GifData(Context context, BitmapPool bitmapPool, String gifId, GifHeader header, byte[] data) { this.context = context; this.bitmapPool = bitmapPool; this.header = header; this.data = data; this.gifId = gifId; }
@Override public GifDataResource decode(InputStream source, int width, int height) throws IOException { byte[] data = inputStreamToBytes(source); GifHeader header = new GifHeaderParser(data).parseHeader(); String id = getGifId(data); return new GifDataResource(new GifData(context, bitmapPool, id, header, data)); }
public GifDecoder build(GifDecoder.BitmapProvider provider, GifHeader header, ByteBuffer data, int sampleSize) { return new StandardGifDecoder(provider, header, data, sampleSize); }
GifDecoder build(GifDecoder.BitmapProvider provider, GifHeader header, ByteBuffer data, int sampleSize) { return new StandardGifDecoder(provider, header, data, sampleSize); }
private Bitmap decodeFirstFrame(GifDecoder decoder, GifHeader header, byte[] data) { decoder.setData(header, data); decoder.advance(); return decoder.getNextFrame(); }
/** * Constructor for GifDrawable. * * @see #setFrameTransformation(com.bumptech.glide.load.Transformation, android.graphics.Bitmap) * * @param context A context. * @param bitmapProvider An {@link com.bumptech.glide.gifdecoder.GifDecoder.BitmapProvider} that can be used to * retrieve re-usable {@link android.graphics.Bitmap}s. * @param bitmapPool A {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool} that can be used to return * the first frame when this gifTexture is recycled. * @param frameTransformation An {@link com.bumptech.glide.load.Transformation} that can be applied to each frame. * @param targetFrameWidth The desired width of the frames displayed by this gifTexture (the width of the view or * {@link com.bumptech.glide.request.target.Target} this gifTexture is being loaded into). * @param targetFrameHeight The desired height of the frames displayed by this gifTexture (the height of the view or * {@link com.bumptech.glide.request.target.Target} this gifTexture is being loaded into). * @param gifHeader The header data for this gif. * @param data The full bytes of the gif. * @param firstFrame The decoded and transformed first frame of this gif. */ public GifTexture(Context context, GifDecoder.BitmapProvider bitmapProvider, BitmapPool bitmapPool, Transformation<Bitmap> frameTransformation, int targetFrameWidth, int targetFrameHeight, GifHeader gifHeader, byte[] data, Bitmap firstFrame) { this(new GifState(gifHeader, data, context, frameTransformation, targetFrameWidth, targetFrameHeight, bitmapProvider, bitmapPool, firstFrame)); }