@SuppressWarnings("unchecked") <Z> Transformation<Z> getTransformation(Class<Z> resourceClass) { Transformation<Z> result = (Transformation<Z>) transformations.get(resourceClass); if (result == null) { for (Entry<Class<?>, Transformation<?>> entry : transformations.entrySet()) { if (entry.getKey().isAssignableFrom(resourceClass)) { result = (Transformation<Z>) entry.getValue(); break; } } } if (result == null) { if (transformations.isEmpty() && isTransformationRequired) { throw new IllegalArgumentException( "Missing transformation for " + resourceClass + ". If you wish to" + " ignore unknown resource types, use the optional transformation methods."); } else { return UnitTransformation.get(); } } return result; }
@Test @SuppressWarnings("unchecked") public void testSetsTransformationAsFrameTransformation() { Resource<GifDrawable> resource = mockResource(); GifDrawable gifDrawable = mock(GifDrawable.class); Transformation<Bitmap> unitTransformation = UnitTransformation.get(); when(gifDrawable.getFrameTransformation()).thenReturn(unitTransformation); when(gifDrawable.getIntrinsicWidth()).thenReturn(500); when(gifDrawable.getIntrinsicHeight()).thenReturn(500); when(resource.get()).thenReturn(gifDrawable); Bitmap firstFrame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(gifDrawable.getFirstFrame()).thenReturn(firstFrame); final int width = 123; final int height = 456; Bitmap expectedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Resource<Bitmap> expectedResource = mockResource(); when(expectedResource.get()).thenReturn(expectedBitmap); when(wrapped.transform(any(Context.class), Util.<Bitmap>anyResource(), anyInt(), anyInt())) .thenReturn(expectedResource); transformation.transform(context, resource, width, height); verify(gifDrawable).setFrameTransformation(isA(Transformation.class), eq(expectedBitmap)); }
/** * @see GlideOptions#transform(Class<T>, Transformation<T>) */ @CheckResult public <T> GlideRequest<TranscodeType> transform(@NonNull Class<T> arg0, @NonNull Transformation<T> arg1) { if (getMutableOptions() instanceof GlideOptions) { this.requestOptions = ((GlideOptions) getMutableOptions()).transform(arg0, arg1); } else { this.requestOptions = new GlideOptions().apply(this.requestOptions).transform(arg0, arg1); } return this; }
@Test public void testEquals() throws NoSuchAlgorithmException { doAnswer(new Util.WriteDigest("first")).when(wrapped) .updateDiskCacheKey(isA(MessageDigest.class)); @SuppressWarnings("unchecked") Transformation<Bitmap> other = mock(Transformation.class); doAnswer(new Util.WriteDigest("other")).when(other) .updateDiskCacheKey(isA(MessageDigest.class)); keyTester .addEquivalenceGroup( transformation, new GifDrawableTransformation(wrapped), new GifDrawableTransformation(wrapped)) .addEquivalenceGroup(wrapped) .addEquivalenceGroup(new GifDrawableTransformation(other)) .addRegressionTest( new GifDrawableTransformation(wrapped), "a7937b64b8caa58f03721bb6bacf5c78cb235febe0e70b1b84cd99541461a08e") .test(); }
private RequestOptions transform( @NonNull Transformation<Bitmap> transformation, boolean isRequired) { if (isAutoCloneEnabled) { return clone().transform(transformation, isRequired); } DrawableTransformation drawableTransformation = new DrawableTransformation(transformation, isRequired); transform(Bitmap.class, transformation, isRequired); transform(Drawable.class, drawableTransformation, isRequired); // TODO: remove BitmapDrawable decoder and this transformation. // Registering as BitmapDrawable is simply an optimization to avoid some iteration and // isAssignableFrom checks when obtaining the transformation later on. It can be removed without // affecting the functionality. transform(BitmapDrawable.class, drawableTransformation.asBitmapDrawable(), isRequired); transform(GifDrawable.class, new GifDrawableTransformation(transformation), isRequired); return selfOrThrowIfLocked(); }
ResourceCacheKey( ArrayPool arrayPool, Key sourceKey, Key signature, int width, int height, Transformation<?> appliedTransformation, Class<?> decodedResourceClass, Options options) { this.arrayPool = arrayPool; this.sourceKey = sourceKey; this.signature = signature; this.width = width; this.height = height; this.transformation = appliedTransformation; this.decodedResourceClass = decodedResourceClass; this.options = options; }
private boolean encodeTransformedToStream(GifDrawable drawable, OutputStream os) { Transformation<Bitmap> transformation = drawable.getFrameTransformation(); GifDecoder decoder = decodeHeaders(drawable.getBuffer()); AnimatedGifEncoder encoder = factory.buildEncoder(); if (!encoder.start(os)) { return false; } for (int i = 0; i < decoder.getFrameCount(); i++) { Bitmap currentFrame = decoder.getNextFrame(); Resource<Bitmap> transformedResource = getTransformedFrame(currentFrame, transformation, drawable); try { if (!encoder.addFrame(transformedResource.get())) { return false; } int currentFrameIndex = decoder.getCurrentFrameIndex(); int delay = decoder.getDelay(currentFrameIndex); encoder.setDelay(delay); decoder.advance(); } finally { transformedResource.recycle(); } } return encoder.finish(); }
private Resource<Bitmap> getTransformedFrame(Bitmap currentFrame, Transformation<Bitmap> transformation, GifDrawable drawable) { // TODO: what if current frame is null? Resource<Bitmap> bitmapResource = factory.buildFrameResource(currentFrame, bitmapPool); Resource<Bitmap> transformedResource = transformation.transform( context, bitmapResource, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); if (!bitmapResource.equals(transformedResource)) { bitmapResource.recycle(); } return transformedResource; }
@Override @SafeVarargs @SuppressWarnings("varargs") @CheckResult public final GlideOptions transforms(@NonNull Transformation<Bitmap>... arg0) { return (GlideOptions) super.transforms(arg0); }
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); }
@SuppressWarnings("WeakerAccess") final RequestOptions transform(DownsampleStrategy downsampleStrategy, Transformation<Bitmap> transformation) { if (isAutoCloneEnabled) { return clone().transform(downsampleStrategy, transformation); } downsample(downsampleStrategy); return transform(transformation); }
/** * @see GlideOptions#optionalTransform(Class<T>, Transformation<T>) */ @CheckResult public <T> GlideRequest<TranscodeType> optionalTransform(@NonNull Class<T> arg0, @NonNull Transformation<T> arg1) { if (getMutableOptions() instanceof GlideOptions) { this.requestOptions = ((GlideOptions) getMutableOptions()).optionalTransform(arg0, arg1); } else { this.requestOptions = new GlideOptions().apply(this.requestOptions).optionalTransform(arg0, arg1); } return this; }
/** * @see GlideOptions#optionalTransform(Transformation<Bitmap>) */ @CheckResult public GlideRequest<TranscodeType> optionalTransform(@NonNull Transformation<Bitmap> arg0) { if (getMutableOptions() instanceof GlideOptions) { this.requestOptions = ((GlideOptions) getMutableOptions()).optionalTransform(arg0); } else { this.requestOptions = new GlideOptions().apply(this.requestOptions).optionalTransform(arg0); } return this; }
@Test public void updateDiskCacheKey_throwsException() throws NoSuchAlgorithmException { // If this test fails, update testEqualsAndHashcode to use KeyTester including regression tests. EngineKey key = new EngineKey( "id", new ObjectKey("signature"), 100, 100, Collections.<Class<?>, Transformation<?>>emptyMap(), Object.class, Object.class, new Options()); expectedException.expect(UnsupportedOperationException.class); key.updateDiskCacheKey(MessageDigest.getInstance("SHA-1")); }
@Test public void testReturnsCurrentTransformationInGetFrameTransformation() { @SuppressWarnings("unchecked") Transformation<Bitmap> newTransformation = mock(Transformation.class); Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); drawable.setFrameTransformation(newTransformation, bitmap); verify(frameLoader).setFrameTransformation(eq(newTransformation), eq(bitmap)); }
/** * @deprecated use {@link #BitmapDrawableTransformation(Transformation)}} */ @Deprecated public BitmapDrawableTransformation( @SuppressWarnings("unused") Context context, @SuppressWarnings("unused") BitmapPool bitmapPool, Transformation<Bitmap> wrapped) { this(wrapped); }
@SuppressWarnings("unchecked") <Z> Transformation<Z> getTransformation(Class<Z> resourceClass) { Transformation<Z> result = (Transformation<Z>) transformations.get(resourceClass); if (result == null) { if (transformations.isEmpty() && isTransformationRequired) { throw new IllegalArgumentException( "Missing transformation for " + resourceClass + ". If you wish to" + " ignore unknown resource types, use the optional transformation methods."); } else { return UnitTransformation.get(); } } return result; }
EngineKey(Object model, Key signature, int width, int height, Map<Class<?>, Transformation<?>> transformations, Class<?> resourceClass, Class<?> transcodeClass, Options options) { this.model = Preconditions.checkNotNull(model); this.signature = Preconditions.checkNotNull(signature, "Signature must not be null"); this.width = width; this.height = height; this.transformations = Preconditions.checkNotNull(transformations); this.resourceClass = Preconditions.checkNotNull(resourceClass, "Resource class must not be null"); this.transcodeClass = Preconditions.checkNotNull(transcodeClass, "Transcode class must not be null"); this.options = Preconditions.checkNotNull(options); }
@SuppressWarnings("unchecked") <R> DecodeJob<R> build(GlideContext glideContext, Object model, EngineKey loadKey, Key signature, int width, int height, Class<?> resourceClass, Class<R> transcodeClass, Priority priority, DiskCacheStrategy diskCacheStrategy, Map<Class<?>, Transformation<?>> transformations, boolean isTransformationRequired, boolean onlyRetrieveFromCache, Options options, DecodeJob.Callback<R> callback) { DecodeJob<R> result = (DecodeJob<R>) pool.acquire(); return result.init( glideContext, model, loadKey, signature, width, height, resourceClass, transcodeClass, priority, diskCacheStrategy, transformations, isTransformationRequired, onlyRetrieveFromCache, options, callback, creationOrder++); }
@SuppressWarnings("rawtypes") public EngineKey buildKey(Object model, Key signature, int width, int height, Map<Class<?>, Transformation<?>> transformations, Class<?> resourceClass, Class<?> transcodeClass, Options options) { return new EngineKey(model, signature, width, height, transformations, resourceClass, transcodeClass, options); }
/** * @see GlideOptions#transform(Transformation<Bitmap>) */ @CheckResult public GlideRequest<TranscodeType> transform(@NonNull Transformation<Bitmap> arg0) { if (getMutableOptions() instanceof GlideOptions) { this.requestOptions = ((GlideOptions) getMutableOptions()).transform(arg0); } else { this.requestOptions = new GlideOptions().apply(this.requestOptions).transform(arg0); } return this; }
/** * @see GlideOptions#transforms(Transformation<Bitmap>[]) */ @CheckResult @SuppressWarnings({ "unchecked", "varargs" }) public GlideRequest<TranscodeType> transforms(@NonNull Transformation<Bitmap>... arg0) { if (getMutableOptions() instanceof GlideOptions) { this.requestOptions = ((GlideOptions) getMutableOptions()).transforms(arg0); } else { this.requestOptions = new GlideOptions().apply(this.requestOptions).transforms(arg0); } return this; }