4
4
import android .graphics .Bitmap ;
5
5
import android .graphics .Bitmap .Config ;
6
6
import android .graphics .BitmapFactory ;
7
+ import android .graphics .ColorSpace ;
7
8
import android .os .Build ;
8
9
import android .util .DisplayMetrics ;
9
10
import android .util .Log ;
14
15
import com .bumptech .glide .load .ImageHeaderParserUtils ;
15
16
import com .bumptech .glide .load .Option ;
16
17
import com .bumptech .glide .load .Options ;
18
+ import com .bumptech .glide .load .PreferredColorSpace ;
17
19
import com .bumptech .glide .load .engine .Resource ;
18
20
import com .bumptech .glide .load .engine .bitmap_recycle .ArrayPool ;
19
21
import com .bumptech .glide .load .engine .bitmap_recycle .BitmapPool ;
@@ -48,6 +50,18 @@ public final class Downsampler {
48
50
public static final Option <DecodeFormat > DECODE_FORMAT =
49
51
Option .memory (
50
52
"com.bumptech.glide.load.resource.bitmap.Downsampler.DecodeFormat" , DecodeFormat .DEFAULT );
53
+
54
+ /**
55
+ * Sets the {@link PreferredColorSpace} that will be used along with the version of Android and
56
+ * color space of the requested image to determine the final color space used to decode the image.
57
+ *
58
+ * <p>Refer to {@link PreferredColorSpace} for details on how this option works and its various
59
+ * limitations.
60
+ */
61
+ public static final Option <PreferredColorSpace > PREFERRED_COLOR_SPACE =
62
+ Option .memory (
63
+ "com.bumptech.glide.load.resource.bitmap.Downsampler.PreferredColorSpace" ,
64
+ PreferredColorSpace .SRGB );
51
65
/**
52
66
* Indicates the {@link com.bumptech.glide.load.resource.bitmap.DownsampleStrategy} option that
53
67
* will be used to calculate the sample size to use to downsample an image given the original and
@@ -199,6 +213,7 @@ public Resource<Bitmap> decode(
199
213
bitmapFactoryOptions .inTempStorage = bytesForOptions ;
200
214
201
215
DecodeFormat decodeFormat = options .get (DECODE_FORMAT );
216
+ PreferredColorSpace preferredColorSpace = options .get (PREFERRED_COLOR_SPACE );
202
217
DownsampleStrategy downsampleStrategy = options .get (DownsampleStrategy .OPTION );
203
218
boolean fixBitmapToRequestedDimensions = options .get (FIX_BITMAP_SIZE_TO_REQUESTED_DIMENSIONS );
204
219
boolean isHardwareConfigAllowed =
@@ -211,6 +226,7 @@ public Resource<Bitmap> decode(
211
226
bitmapFactoryOptions ,
212
227
downsampleStrategy ,
213
228
decodeFormat ,
229
+ preferredColorSpace ,
214
230
isHardwareConfigAllowed ,
215
231
requestedWidth ,
216
232
requestedHeight ,
@@ -228,6 +244,7 @@ private Bitmap decodeFromWrappedStreams(
228
244
BitmapFactory .Options options ,
229
245
DownsampleStrategy downsampleStrategy ,
230
246
DecodeFormat decodeFormat ,
247
+ PreferredColorSpace preferredColorSpace ,
231
248
boolean isHardwareConfigAllowed ,
232
249
int requestedWidth ,
233
250
int requestedHeight ,
@@ -328,6 +345,18 @@ private Bitmap decodeFromWrappedStreams(
328
345
setInBitmap (options , bitmapPool , expectedWidth , expectedHeight );
329
346
}
330
347
}
348
+
349
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
350
+ boolean isP3Eligible =
351
+ preferredColorSpace == PreferredColorSpace .DISPLAY_P3
352
+ && options .outColorSpace != null
353
+ && options .outColorSpace .isWideGamut ();
354
+ options .inPreferredColorSpace =
355
+ ColorSpace .get (isP3Eligible ? ColorSpace .Named .DISPLAY_P3 : ColorSpace .Named .SRGB );
356
+ } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
357
+ options .inPreferredColorSpace = ColorSpace .get (ColorSpace .Named .SRGB );
358
+ }
359
+
331
360
Bitmap downsampled = decodeStream (is , options , callbacks , bitmapPool );
332
361
callbacks .onDecodeComplete (bitmapPool , downsampled );
333
362
@@ -860,6 +889,7 @@ private static void resetOptions(BitmapFactory.Options decodeBitmapOptions) {
860
889
decodeBitmapOptions .inDensity = 0 ;
861
890
decodeBitmapOptions .inTargetDensity = 0 ;
862
891
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
892
+ decodeBitmapOptions .outColorSpace = null ;
863
893
decodeBitmapOptions .outConfig = null ;
864
894
}
865
895
decodeBitmapOptions .outWidth = 0 ;
0 commit comments