I’m stuck for 3 days on this. I got a Dicom files that i need to parse with a buffered reader which returns some informations from the header of the document and the raw data for the image. After that, I apply a LUT on the raw to convert the byte into grayscale and then throw it into a Bitmap.create . It was perfect for little image but now, I have to load 13Mo image and, not only it take ages to open it (about 20 seconds), but also, while applying the LUT int the bitmap method, Android throws an error About Bitmap 29052480-byte external allocation too large for this process. java.lang.OutOfMemoryError: bitmap size exceeds VM budget . I know there are a lot of threads about this error, but in my case, it’s a little bit original as I only want to open one image (so it’s not about stacking much bitmap). I could show you some code :
RefreshBmp :
private void refreshBmp(int windowWidth, int windowCentre) {
int[] colorArray = process.transformBuffer(myDicomObject.getRawData(),
myDicomObject.isInverted(), windowWidth, windowCentre,
myDicomObject.getnBits());
Bitmap bmp = Bitmap.createBitmap(colorArray,
myDicomObject.getColumns(), myDicomObject.getRows(),
Bitmap.Config.ARGB_8888);
dicomImageView.setImageBitmap(bmp);
}
Which call my LUT :
public int[] transformBuffer(int[] rawData, boolean inverted,
int windowWidth, int windowCenter, int nBits) {
System.gc();
int min = windowCenter - (windowWidth/2);
int max = windowCenter + (windowWidth/2);
int intGrayscale = (int) Math.pow(2, nBits);
int intDivisionFactor = nBits-8;
double dmin = (double) min;
double dmax = (double) max;
double doubleGrayScale = (double) intGrayscale;
int rawDataLength = rawData.length;
int[] resultBuffer = new int[rawDataLength];
lutBuffer = new int[intGrayscale];
if(inverted){
for(int i = 0 ; i < min ; i++){
lutBuffer[i] = 255;
}
for(int i = min ; i < max ; i++){
double value = doubleGrayScale * ((i - dmin + 1) / (dmax - dmin + 1));
lutBuffer[i] = (int) (doubleGrayScale - value) >> intDivisionFactor;
}
for(int i = max ; i < intGrayscale ; i++){
lutBuffer[i] = 0;
}
}else{
for(int i = 0 ; i < min ; i++){
lutBuffer[i] = 0;
}
for(int i = min ; i < max ; i++){
double value = ((i - dmin + 1) / (dmax - dmin + 1));
lutBuffer[i] = (int) (value) << intDivisionFactor;
}
for(int i = max ; i < intGrayscale ; i++){
lutBuffer[i] = 255;
}
}
for(int i = 0 ; i < rawDataLength ; i++){
int colorValue = lutBuffer[rawData[i]];
resultBuffer[i] = Color.argb(255, colorValue, colorValue, colorValue);
}
System.out.println(resultBuffer.length);
return resultBuffer;
}
Hopefully, someone would know a way to save some memory allocation, especialy onto the LUT method.
I decided to answer myself as I finaly found a solution. The only way you can avoid this
OutOfMemoryerror with those big Bitmap data is post-binning (I’m not sur if it is the actual name). It consists in taking every alternate pixels. You do this by reading one pixel, skip one, read another, skip one and keep doing this until you reach the end of the line, then skip a line and continue this process to the end of the data buffer.I hope it could help someone else.