Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fallback to imprecise scaling if low memory #1892

Merged
merged 1 commit into from
Sep 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 55 additions & 21 deletions src/org/thoughtcrime/securesms/util/BitmapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import android.net.Uri;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class BitmapUtil {
private static final String TAG = BitmapUtil.class.getSimpleName();

private static final int MAX_COMPRESSION_QUALITY = 95;
private static final int MIN_COMPRESSION_QUALITY = 50;
Expand All @@ -26,9 +26,17 @@ public static byte[] createScaledBytes(Context context, Uri uri, int maxWidth,
int maxHeight, int maxSize)
throws IOException, BitmapDecodingException
{
InputStream measure = context.getContentResolver().openInputStream(uri);
InputStream data = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = createScaledBitmap(measure, data, maxWidth, maxHeight);
Bitmap bitmap;
try {
bitmap = createScaledBitmap(context.getContentResolver().openInputStream(uri),
context.getContentResolver().openInputStream(uri),
maxWidth, maxHeight, false);
} catch(OutOfMemoryError oome) {
Log.w(TAG, "OutOfMemoryError when scaling precisely, doing rough scale to save memory instead");
bitmap = createScaledBitmap(context.getContentResolver().openInputStream(uri),
context.getContentResolver().openInputStream(uri),
maxWidth, maxHeight, true);
}
int quality = MAX_COMPRESSION_QUALITY;
int attempts = 0;

Expand All @@ -53,39 +61,56 @@ public static Bitmap createScaledBitmap(InputStream measure, InputStream data, f
final BitmapFactory.Options options = getImageDimensions(measure);
final int outWidth = (int)(options.outWidth * scale);
final int outHeight = (int)(options.outHeight * scale);
Log.w("BitmapUtil", "creating scaled bitmap with scale " + scale + " => " + outWidth + "x" + outHeight);
return createScaledBitmap(data, outWidth, outHeight, options);
Log.w(TAG, "creating scaled bitmap with scale " + scale + " => " + outWidth + "x" + outHeight);
return createScaledBitmap(data, outWidth, outHeight, options, false);
}

public static Bitmap createScaledBitmap(InputStream measure, InputStream data,
int maxWidth, int maxHeight)
int maxWidth, int maxHeight)
throws BitmapDecodingException
{
return createScaledBitmap(measure, data, maxWidth, maxHeight, false);
}

public static Bitmap createScaledBitmap(InputStream measure, InputStream data,
int maxWidth, int maxHeight, boolean constrainedMemory)
throws BitmapDecodingException
{
final BitmapFactory.Options options = getImageDimensions(measure);
return createScaledBitmap(data, maxWidth, maxHeight, options);
return createScaledBitmap(data, maxWidth, maxHeight, options, constrainedMemory);
}

private static Bitmap createScaledBitmap(InputStream data,
int maxWidth, int maxHeight, BitmapFactory.Options options)
private static Bitmap createScaledBitmap(InputStream data, int maxWidth, int maxHeight,
BitmapFactory.Options options, boolean constrainedMemory)
throws BitmapDecodingException
{
final int imageWidth = options.outWidth;
final int imageHeight = options.outHeight;

int scaler = 1;

while ((imageWidth / scaler / 2 >= maxWidth) && (imageHeight / scaler / 2 >= maxHeight))
int scaleFactor = (constrainedMemory ? 1 : 2);
while ((imageWidth / scaler / scaleFactor >= maxWidth) && (imageHeight / scaler / scaleFactor >= maxHeight)) {
scaler *= 2;
}

options.inSampleSize = scaler;
options.inJustDecodeBounds = false;

Bitmap roughThumbnail = BitmapFactory.decodeStream(new BufferedInputStream(data), null, options);
Log.w("BitmapUtil", "rough scale " + (imageWidth) + "x" + (imageHeight) +
" => " + (options.outWidth) + "x" + (options.outHeight));
FlushedInputStream is = new FlushedInputStream(data);
Bitmap roughThumbnail = BitmapFactory.decodeStream(is, null, options);
try {
is.close();
} catch (IOException ioe) {
Log.w(TAG, "IOException thrown when closing an images InputStream", ioe);
}
Log.w(TAG, "rough scale " + (imageWidth) + "x" + (imageHeight) +
" => " + (options.outWidth) + "x" + (options.outHeight));
if (roughThumbnail == null) {
throw new BitmapDecodingException("Decoded stream was null.");
}
if (constrainedMemory) {
return roughThumbnail;
}

if (options.outWidth > maxWidth || options.outHeight > maxHeight) {
final float aspectWidth, aspectHeight;
Expand All @@ -101,10 +126,14 @@ private static Bitmap createScaledBitmap(InputStream data,
aspectWidth = (aspectHeight / options.outHeight) * options.outWidth;
}

Log.w("BitmapUtil", "fine scale " + options.outWidth + "x" + options.outHeight +
" => " + aspectWidth + "x" + aspectHeight);
Bitmap scaledThumbnail = Bitmap.createScaledBitmap(roughThumbnail, (int)aspectWidth, (int)aspectHeight, true);
if (roughThumbnail != scaledThumbnail) roughThumbnail.recycle();
Log.w(TAG, "fine scale " + options.outWidth + "x" + options.outHeight +
" => " + aspectWidth + "x" + aspectHeight);
Bitmap scaledThumbnail = null;
try {
scaledThumbnail = Bitmap.createScaledBitmap(roughThumbnail, (int) aspectWidth, (int) aspectHeight, true);
} finally {
if (roughThumbnail != scaledThumbnail) roughThumbnail.recycle();
}
return scaledThumbnail;
} else {
return roughThumbnail;
Expand All @@ -114,8 +143,13 @@ private static Bitmap createScaledBitmap(InputStream data,
private static BitmapFactory.Options getImageDimensions(InputStream inputStream) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);

FlushedInputStream fis = new FlushedInputStream(inputStream);
BitmapFactory.decodeStream(fis, null, options);
try {
fis.close();
} catch (IOException ioe) {
Log.w(TAG, "failed to close the InputStream after reading image dimensions");
}
return options;
}

Expand Down
33 changes: 33 additions & 0 deletions src/org/thoughtcrime/securesms/util/FlushedInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.thoughtcrime.securesms.util;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* Memory-friendly workaround found from https://code.google.com/p/android/issues/detail?id=6066#c23
* to solve decoding problems in bitmaps from InputStreams that don't skip if no more stream is available.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this happening? This link is talking about blocking network streams, where as read() from disk should always read the number of bytes requested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, wasn't aware. The Javadoc didn't say that read() always returns the number of bytes requested. Seemed like a good idea to make sure it worked with all InputStream objects anyway. Would you prefer I instead have the method signature only take in FileInputStreams?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contract is not that strong, but in practice read() will only return short of the requested value if less than the requested value is available. That should only happen when reading from the network. I'm mostly curious how this came up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a while ago, but I think originally I was experiencing those "decoder false" errors when doing Bitmap work a while back, and wrapping it in a BufferedInputStream seemed to resolve the problem. This is supposedly a more memory-efficient way to do the same thing, and it feels safer to include it.

class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}

@Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int inByte = read();
if (inByte < 0) {
break;
} else {
bytesSkipped = 1;
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}