0

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.

10
  • What are the values of rawDataLength and intGrayScale when it crashes? Jun 13, 2012 at 9:33
  • @David intGrayscale is a constant, it value is 2^16 = 65536. rawDataLength = resultBuffer.length = 7263120
    – Purfakt
    Jun 13, 2012 at 11:28
  • OK. Do you realize that allocating an int[7263120] needs 28MB of memory? Most Android phones have a maximum process heap size of 16MB, 24MB or 32MB. You can check your device's memory max with ActivityManager.getMemoryClass(). In any case, you can't do this in memory. It isn't possible. Jun 13, 2012 at 14:31
  • Do you need to allocate an int array? Each int needs 4 bytes. If the values you are putting in there are only values from 0-255 then you could use a byte array. That would reduce your memory requirement by 75% Jun 13, 2012 at 14:33
  • @david Thanks for your answer, I've tried to make a byte[] but the method create bitmap need an int array.
    – Purfakt
    Jun 14, 2012 at 7:51

2 Answers 2

0

You should try modifying directly rawData if you don't need it as it is any further. Something like this:

public void transformBuffer(int[] rawData, boolean inverted,
        int windowWidth, int windowCenter, int nBits) {
/*
change rowData instead resultBuffer
*/
}

public void methodThatCallsTransformBuffer(...) {
    transformBuffer(data, inverted, ...);
    //now data is transformed
}

Also, instead using lutBuffer, you could compute colorValue for every pixel. It would be a bit slower bu you'll save some memory:

int colorValue = 0;
if (inverted) {
   if (rawData[i] < min) {
      colorValue = 255;
   } else {
      if (rawData[i] < max) {
         double value = doubleGrayScale * ((i - dmin + 1) / (dmax - dmin + 1));
         colorValue  = (int) (doubleGrayScale - value) >> intDivisionFactor;         
      }
   }
} else {
   if (rawData[i] >= min && rowData[i] < max) {
      double value = doubleGrayScale * ((i - dmin + 1) / (dmax - dmin + 1));
      colorValue  = (int) (doubleGrayScale - value) >> intDivisionFactor;      
   } else if (rowData[i] > max) {
      colorValue = 255;
   }
}
1
  • Thanks for your answer! But I actualy need it later, so it wont work in my case.
    – Purfakt
    Jun 13, 2012 at 8:56
0

I decided to answer myself as I finaly found a solution. The only way you can avoid this OutOfMemory error 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.