Skip to content

Commit

Permalink
Issue #101 - Added possibility to set Bitmap config (colorspace) in
Browse files Browse the repository at this point in the history
DisplayImageOptions
  • Loading branch information
nostra13 committed Nov 17, 2012
1 parent a11bb0c commit f976103
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* <li>whether loaded image will be cached in memory</li>
* <li>whether loaded image will be cached on disc</li>
* <li>image scale type</li>
* <li>bitmap decoding configuration</li>
* <li>how decoded {@link Bitmap} will be displayed</li>
* </ul>
*
Expand All @@ -36,6 +37,7 @@ public final class DisplayImageOptions {
private final boolean cacheInMemory;
private final boolean cacheOnDisc;
private final ImageScaleType imageScaleType;
private final Bitmap.Config bitmapConfig;
private final BitmapDisplayer displayer;

private DisplayImageOptions(Builder builder) {
Expand All @@ -45,6 +47,7 @@ private DisplayImageOptions(Builder builder) {
cacheInMemory = builder.cacheInMemory;
cacheOnDisc = builder.cacheOnDisc;
imageScaleType = builder.imageScaleType;
bitmapConfig = builder.bitmapConfig;
displayer = builder.displayer;
}

Expand Down Expand Up @@ -80,6 +83,10 @@ ImageScaleType getImageScaleType() {
return imageScaleType;
}

Bitmap.Config getBitmapConfig() {
return bitmapConfig;
}

BitmapDisplayer getDisplayer() {
return displayer;
}
Expand All @@ -96,6 +103,7 @@ public static class Builder {
private boolean cacheInMemory = false;
private boolean cacheOnDisc = false;
private ImageScaleType imageScaleType = ImageScaleType.IN_SAMPLE_POWER_OF_2;
private Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888;
private BitmapDisplayer displayer = DefaultConfigurationFactory.createBitmapDisplayer();

/**
Expand Down Expand Up @@ -148,6 +156,12 @@ public Builder imageScaleType(ImageScaleType imageScaleType) {
return this;
}

/** Sets {@link Bitmap.Config bitmap config} for image decoding. Default value - {@link Bitmap.Config#ARGB_8888} */
public Builder bitmapConfig(Bitmap.Config bitmapConfig) {
this.bitmapConfig = bitmapConfig;
return this;
}

/**
* Sets custom {@link BitmapDisplayer displayer} for image loading task. Default value -
* {@link DefaultConfigurationFactory#createBitmapDisplayer()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @see ImageScaleType
* @see ViewScaleType
* @see ImageDownloader
* @see DisplayImageOptions
*/
class ImageDecoder {

Expand All @@ -30,6 +31,7 @@ class ImageDecoder {

private final URI imageUri;
private final ImageDownloader imageDownloader;
private final DisplayImageOptions displayOptions;

private boolean loggingEnabled;

Expand All @@ -40,9 +42,10 @@ class ImageDecoder {
* Image downloader
*
*/
ImageDecoder(URI imageUri, ImageDownloader imageDownloader) {
ImageDecoder(URI imageUri, ImageDownloader imageDownloader, DisplayImageOptions options) {
this.imageUri = imageUri;
this.imageDownloader = imageDownloader;
this.displayOptions = options;
}

/**
Expand Down Expand Up @@ -97,9 +100,10 @@ public Bitmap decode(ImageSize targetSize, ImageScaleType scaleType, ViewScaleTy
}

private Options getBitmapOptionsForImageDecoding(ImageSize targetSize, ImageScaleType scaleType, ViewScaleType viewScaleType) throws IOException {
Options options = new Options();
options.inSampleSize = computeImageScale(targetSize, scaleType, viewScaleType);
return options;
Options decodeOptions = new Options();
decodeOptions.inSampleSize = computeImageScale(targetSize, scaleType, viewScaleType);
decodeOptions.inPreferredConfig = displayOptions.getBitmapConfig();
return decodeOptions;
}

@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private Bitmap decodeImage(URI imageUri) throws IOException {
if (configuration.handleOutOfMemory) {
bmp = decodeWithOOMHandling(imageUri);
} else {
ImageDecoder decoder = new ImageDecoder(imageUri, configuration.downloader);
ImageDecoder decoder = new ImageDecoder(imageUri, configuration.downloader, imageLoadingInfo.options);
decoder.setLoggingEnabled(configuration.loggingEnabled);
ViewScaleType viewScaleType = ViewScaleType.fromImageView(imageLoadingInfo.imageView);
bmp = decoder.decode(imageLoadingInfo.targetSize, imageLoadingInfo.options.getImageScaleType(), viewScaleType);
Expand All @@ -178,7 +178,7 @@ private Bitmap decodeImage(URI imageUri) throws IOException {

private Bitmap decodeWithOOMHandling(URI imageUri) throws IOException {
Bitmap result = null;
ImageDecoder decoder = new ImageDecoder(imageUri, configuration.downloader);
ImageDecoder decoder = new ImageDecoder(imageUri, configuration.downloader, imageLoadingInfo.options);
decoder.setLoggingEnabled(configuration.loggingEnabled);
for (int attempt = 1; attempt <= ATTEMPT_COUNT_TO_DECODE_BITMAP; attempt++) {
try {
Expand Down Expand Up @@ -213,7 +213,7 @@ private void saveImageOnDisc(File targetFile) throws IOException, URISyntaxExcep
if (width > 0 || height > 0) {
// Download, decode, compress and save image
ImageSize targetImageSize = new ImageSize(width, height);
ImageDecoder decoder = new ImageDecoder(new URI(imageLoadingInfo.uri), configuration.downloader);
ImageDecoder decoder = new ImageDecoder(new URI(imageLoadingInfo.uri), configuration.downloader, imageLoadingInfo.options);
decoder.setLoggingEnabled(configuration.loggingEnabled);
Bitmap bmp = decoder.decode(targetImageSize, ImageScaleType.IN_SAMPLE_INT, ViewScaleType.FIT_INSIDE);

Expand Down

0 comments on commit f976103

Please sign in to comment.