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

Bitmap dimensions exceeded max android texture size fix #23 #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.HashMap;
import java.util.Map;

import static com.steelkiwi.cropiwa.util.CropIwaUtils.*;
import static com.steelkiwi.cropiwa.util.CropIwaUtils.closeSilently;

/**
* @author Yaroslav Polyakov https://github.com/polyak01
Expand Down Expand Up @@ -181,39 +181,44 @@ private File cacheLocally(Context context, Uri input) throws IOException {
return local;
}

private BitmapFactory.Options getBitmapFactoryOptions(Context c, Uri uri, int width, int height) throws FileNotFoundException {
if (width != SIZE_UNSPECIFIED && height != SIZE_UNSPECIFIED) {
return getOptimalSizeOptions(c, uri, width, height);
} else {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
return options;
}
}

private static BitmapFactory.Options getOptimalSizeOptions(
Context context, Uri bitmapUri,
int reqWidth, int reqHeight) throws FileNotFoundException {
private BitmapFactory.Options getBitmapFactoryOptions(Context context, Uri bitmapUri, int width, int height) throws FileNotFoundException {
InputStream is = context.getContentResolver().openInputStream(bitmapUri);
BitmapFactory.Options result = new BitmapFactory.Options();
result.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, result);
result.inJustDecodeBounds = false;
result.inSampleSize = calculateInSampleSize(result, reqWidth, reqHeight);
result.inSampleSize = calculateInSampleSize(result, width, height);
return result;
}

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
int inSampleSize = calculateMinimalInSampleSize(options);

if (reqHeight != SIZE_UNSPECIFIED && reqWidth != SIZE_UNSPECIFIED) {
inSampleSize = calculateInSampleSizeForDimensions(reqWidth, reqHeight, height, width, inSampleSize);
}
return inSampleSize;
}

private static int calculateInSampleSizeForDimensions(int reqWidth, int reqHeight, int height, int width, int baseInSampleSize) {
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
while ((halfHeight / baseInSampleSize) >= reqHeight && (halfWidth / baseInSampleSize) >= reqWidth) {
baseInSampleSize *= 2;
}
}
return baseInSampleSize;
}

private static int calculateMinimalInSampleSize(BitmapFactory.Options options) {
int inSampleSize = 1;
int maximumTextureSize = TextureUtils.getMaxTextureSize();
while (options.outWidth / inSampleSize > maximumTextureSize || options.outHeight / inSampleSize > maximumTextureSize) {
inSampleSize *= 2;
}
return inSampleSize;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.steelkiwi.cropiwa.image;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;


/**
* @author Kamil Kalisz https://github.com/polyak01
* on 15.07.2017.
*/
final class TextureUtils
{
private static final int DEFAULT_IMAGE_MAX_BITMAP_DIMENSION = 2048;
/**
* Maxium bitmap size supported by OpenGl
*/
static int getMaxTextureSize() {

// Get EGL Display
EGL10 egl = (EGL10) EGLContext.getEGL();
EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

// Initialise
int[] version = new int[2];
egl.eglInitialize(display, version);

// Query total number of configurations
int[] totalConfigurations = new int[1];
egl.eglGetConfigs(display, null, 0, totalConfigurations);

// Query actual list configurations
EGLConfig[] configurationsList = new EGLConfig[totalConfigurations[0]];
egl.eglGetConfigs(display, configurationsList, totalConfigurations[0], totalConfigurations);

int[] textureSize = new int[1];
int maximumTextureSize = 0;

// Iterate through all the configurations to located the maximum texture size
for (int i = 0; i < totalConfigurations[0]; i++) {
// Only need to check for width since opengl textures are always squared
egl.eglGetConfigAttrib(display, configurationsList[i], EGL10.EGL_MAX_PBUFFER_WIDTH, textureSize);

// Keep track of the maximum texture size
if (maximumTextureSize < textureSize[0])
maximumTextureSize = textureSize[0];
}

// Release
egl.eglTerminate(display);

// Return largest texture size found, or default
return Math.max(maximumTextureSize, DEFAULT_IMAGE_MAX_BITMAP_DIMENSION);
}
}