From 15a21b125d0d131df7bc405a947e69b4d40709ae Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Tue, 30 Jul 2019 13:21:42 -0700 Subject: [PATCH] Add color space handling to ImageDecoderResourceDecoder. This should exactly match Downsampler. PiperOrigin-RevId: 260781611 --- .../resource/ImageDecoderResourceDecoder.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/src/main/java/com/bumptech/glide/load/resource/ImageDecoderResourceDecoder.java b/library/src/main/java/com/bumptech/glide/load/resource/ImageDecoderResourceDecoder.java index 11c8da20f7..69be855367 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/ImageDecoderResourceDecoder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/ImageDecoderResourceDecoder.java @@ -1,12 +1,14 @@ package com.bumptech.glide.load.resource; import android.annotation.SuppressLint; +import android.graphics.ColorSpace; import android.graphics.ImageDecoder; import android.graphics.ImageDecoder.DecodeException; import android.graphics.ImageDecoder.ImageInfo; import android.graphics.ImageDecoder.OnHeaderDecodedListener; import android.graphics.ImageDecoder.OnPartialImageListener; import android.graphics.ImageDecoder.Source; +import android.os.Build; import android.util.Log; import android.util.Size; import androidx.annotation.NonNull; @@ -14,6 +16,7 @@ import androidx.annotation.RequiresApi; import com.bumptech.glide.load.DecodeFormat; import com.bumptech.glide.load.Options; +import com.bumptech.glide.load.PreferredColorSpace; import com.bumptech.glide.load.ResourceDecoder; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy; @@ -58,6 +61,7 @@ public final Resource decode( final boolean isHardwareConfigAllowed = options.get(Downsampler.ALLOW_HARDWARE_CONFIG) != null && options.get(Downsampler.ALLOW_HARDWARE_CONFIG); + final PreferredColorSpace preferredColorSpace = options.get(Downsampler.PREFERRED_COLOR_SPACE); return decode( source, @@ -125,6 +129,18 @@ public boolean onPartialImage(@NonNull DecodeException e) { } decoder.setTargetSize(resizeWidth, resizeHeight); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + boolean isP3Eligible = + preferredColorSpace == PreferredColorSpace.DISPLAY_P3 + && info.getColorSpace() != null + && info.getColorSpace().isWideGamut(); + decoder.setTargetColorSpace( + ColorSpace.get( + isP3Eligible ? ColorSpace.Named.DISPLAY_P3 : ColorSpace.Named.SRGB)); + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + decoder.setTargetColorSpace(ColorSpace.get(ColorSpace.Named.SRGB)); + } } }); }