Skip to content

Commit 26fd174

Browse files
Merge pull request #349 from dhabensky/master
Support for other bitmap formats (not just default RGB565)
2 parents 64038a1 + c95f6e2 commit 26fd174

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
package com.davemorrissey.labs.subscaleview.decoder;
22

3+
import android.graphics.Bitmap;
34
import android.support.annotation.NonNull;
45

6+
import java.lang.reflect.Constructor;
7+
import java.lang.reflect.InvocationTargetException;
8+
59
/**
610
* Compatibility factory to instantiate decoders with empty public constructors.
711
* @param <T> The base type of the decoder this factory will produce.
812
*/
9-
public class CompatDecoderFactory <T> implements DecoderFactory<T> {
13+
public class CompatDecoderFactory<T> implements DecoderFactory<T> {
1014
private Class<? extends T> clazz;
15+
private Bitmap.Config bitmapConfig;
1116

1217
public CompatDecoderFactory(@NonNull Class<? extends T> clazz) {
18+
this(clazz, null);
19+
}
20+
21+
public CompatDecoderFactory(@NonNull Class<? extends T> clazz, Bitmap.Config bitmapConfig) {
1322
this.clazz = clazz;
23+
this.bitmapConfig = bitmapConfig;
1424
}
1525

1626
@Override
17-
public T make() throws IllegalAccessException, InstantiationException {
18-
return clazz.newInstance();
27+
public T make() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
28+
if (bitmapConfig == null) {
29+
return clazz.newInstance();
30+
}
31+
else {
32+
Constructor<? extends T> ctor = clazz.getConstructor(Bitmap.Config.class);
33+
return ctor.newInstance(bitmapConfig);
34+
}
1935
}
2036
}

library/src/com/davemorrissey/labs/subscaleview/decoder/DecoderFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.davemorrissey.labs.subscaleview.decoder;
22

3+
import java.lang.reflect.InvocationTargetException;
4+
35
/**
46
* Interface for decoder (and region decoder) factories.
57
* @param <T> the class of decoder that will be produced.
@@ -9,5 +11,5 @@ public interface DecoderFactory<T> {
911
* Produce a new instance of a decoder with type {@link T}.
1012
* @return a new instance of your decoder.
1113
*/
12-
T make() throws IllegalAccessException, InstantiationException;
14+
T make() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException;
1315
}

library/src/com/davemorrissey/labs/subscaleview/decoder/SkiaImageDecoder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,25 @@ public class SkiaImageDecoder implements ImageDecoder {
2424
private static final String ASSET_PREFIX = FILE_PREFIX + "/android_asset/";
2525
private static final String RESOURCE_PREFIX = ContentResolver.SCHEME_ANDROID_RESOURCE + "://";
2626

27+
private final Bitmap.Config bitmapConfig;
28+
29+
public SkiaImageDecoder() {
30+
this(null);
31+
}
32+
33+
public SkiaImageDecoder(Bitmap.Config bitmapConfig) {
34+
if (bitmapConfig == null)
35+
this.bitmapConfig = Bitmap.Config.RGB_565;
36+
else
37+
this.bitmapConfig = bitmapConfig;
38+
}
39+
2740
@Override
2841
public Bitmap decode(Context context, Uri uri) throws Exception {
2942
String uriString = uri.toString();
3043
BitmapFactory.Options options = new BitmapFactory.Options();
3144
Bitmap bitmap;
32-
options.inPreferredConfig = Bitmap.Config.RGB_565;
45+
options.inPreferredConfig = bitmapConfig;
3346
if (uriString.startsWith(RESOURCE_PREFIX)) {
3447
Resources res;
3548
String packageName = uri.getAuthority();

library/src/com/davemorrissey/labs/subscaleview/decoder/SkiaImageRegionDecoder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ public class SkiaImageRegionDecoder implements ImageRegionDecoder {
2828
private static final String ASSET_PREFIX = FILE_PREFIX + "/android_asset/";
2929
private static final String RESOURCE_PREFIX = ContentResolver.SCHEME_ANDROID_RESOURCE + "://";
3030

31+
private final Bitmap.Config bitmapConfig;
32+
33+
public SkiaImageRegionDecoder() {
34+
this(null);
35+
}
36+
37+
public SkiaImageRegionDecoder(Bitmap.Config bitmapConfig) {
38+
if (bitmapConfig == null)
39+
this.bitmapConfig = Bitmap.Config.RGB_565;
40+
else
41+
this.bitmapConfig = bitmapConfig;
42+
}
43+
3144
@Override
3245
public Point init(Context context, Uri uri) throws Exception {
3346
String uriString = uri.toString();
@@ -80,7 +93,7 @@ public Bitmap decodeRegion(Rect sRect, int sampleSize) {
8093
synchronized (decoderLock) {
8194
BitmapFactory.Options options = new BitmapFactory.Options();
8295
options.inSampleSize = sampleSize;
83-
options.inPreferredConfig = Config.RGB_565;
96+
options.inPreferredConfig = bitmapConfig;
8497
Bitmap bitmap = decoder.decodeRegion(sRect, options);
8598
if (bitmap == null) {
8699
throw new RuntimeException("Skia image decoder returned null bitmap - image format may not be supported");

0 commit comments

Comments
 (0)