Skip to content

Commit

Permalink
Add color space handling to ImageDecoderResourceDecoder.
Browse files Browse the repository at this point in the history
This should exactly match Downsampler.

PiperOrigin-RevId: 260781611
  • Loading branch information
sjudd authored and glide-copybara-robot committed Jul 30, 2019
1 parent 6dc03d8 commit 15a21b1
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
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;
import androidx.annotation.Nullable;
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;
Expand Down Expand Up @@ -58,6 +61,7 @@ public final Resource<T> 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,
Expand Down Expand Up @@ -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));
}
}
});
}
Expand Down

0 comments on commit 15a21b1

Please sign in to comment.