Ollie C
Ollie C

Reputation: 28509

How to use high-quality rendering for an Android resource bitmap loaded into an OpenGL texture?

I know very little about OpenGL so please be gentle.

The app needs to load a bitmap (from resources), resize it, and use it in an OpenGL texture. I have an implementation that works, but there was a bad banding issue on the Wildfire S. So I changed the implementation and fixed the banding issue (by switching to ARGB_8888) but that then broke the functionality on the Galaxy Nexus and the Nexus One.

I am seeing three visual presentations:

  1. The bitmap (a smooth 24-bit gradient) shows correctly, with no banding.

  2. The gradient shows, but with obvious banding

  3. The texture shows as flat white, no bitmap (or issues in logcat)

Here are two versions of the method to load the bitmap, and notes on the results seen with each:

    // White on Galaxy Nexus. White on Nexus One. Renders correct image (no banding) on Wildfire S
    private Bitmap getBitmap1() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.outWidth = getTextureSize();
        options.outHeight = getTextureSize();
        final Bitmap bmp;
        bmp = BitmapFactory.decodeResource(getResources(), bitmapResourceId, options);
        return bmp;
    }

    // Renders correctly (no banding) on Galaxy Nexus. Renders on Nexus One and Wildfire S but with obvious banding.
    private Bitmap getBitmap2() {
        int textureSize = getTextureSize();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.outWidth = getTextureSize();
        options.outHeight = getTextureSize();
        final Bitmap bmp;
        bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), bitmapResourceId, options), textureSize, textureSize, true);
        return bmp;
    }

getTextureSize() returns 1024.

How do I build a single method that shows the bitmap without banding on all devices, and without any devices show a big white box?

Upvotes: 1

Views: 2566

Answers (3)

Diljeet
Diljeet

Reputation: 1976

Color Banding Solved ooooooooooyyyyyyyeaaaaaaaaaa

I solved color banding in two phases

1) * when we use the BitmapFactory to decode resources it decodes the resource in RGB565 which shows color banding, instead of using ARGB_8888, so i used BitmapFactory.Options for setting the decode options to ARGB_8888

second problem was whenever i scaled the bitmap it again got banded

2) This was the tough part and took a lot of searching and finally worked * the method Bitmap.createScaledBitmap for scaling bitmaps also reduced the images to RGB565 format after scaling i got banded images(the old method for solving this was using at least one transparent pixel in a png but no other format like jpg or bmp worked)so here i created a method CreateScaledBitmap to scale the bitmap with the original bitmaps configurations in the resulting scale bitmap(actually i copied the method from a post by logicnet.dk and translated in java)

    BitmapFactory.Options myOptions = new BitmapFactory.Options();
    myOptions.inDither = true;
    myOptions.inScaled = false;
    myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//important
    //myOptions.inDither = false;
    myOptions.inPurgeable = true;
    Bitmap tempImage =  
    BitmapFactory.decodeResource(getResources(),R.drawable.defaultart, myOptions);//important

    //this is important part new scale method created by someone else
    tempImage = CreateScaledBitmap(tempImage,300,300,false);

    ImageView v = (ImageView)findViewById(R.id.imageView1);
    v.setImageBitmap(tempImage);

// the function

public static Bitmap CreateScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
{
    Matrix m = new Matrix();
    m.setScale(dstWidth  / (float)src.getWidth(), dstHeight / (float)src.getHeight());
    Bitmap result = Bitmap.createBitmap(dstWidth, dstHeight, src.getConfig());
    Canvas canvas = new Canvas(result);
    //using (var canvas = new Canvas(result))
    {
        Paint paint = new Paint();
        paint.setFilterBitmap(filter);
        canvas.drawBitmap(src, m, paint);
    }
    return result;

}

Please correct me if i am wrong. Also comment if it worked for you.

I am so happy i solved it, Hope it works for you.

Upvotes: 1

Renard
Renard

Reputation: 6929

getBitmap1

outHeight and outWidth are used in conjunction with inJustDecodeBounds. You cannot use them to load a scaled bitmap. So the reason you are seeing a white texture is that the bitmap is not a power of two.

getBitmap2

you should keep a reference to the bitmap returned by decodeResource so that you can recycle it later. also use options.inScaled = false;to load an unscaled version of the bitmap. Also take note that createScaledBitmap may change the depth of the bitmap to RGB_565 if the original bitmap contains no alpha channel (Source);

Questions: is the original Bitmap Resource square? If not your scaling code will change the aspect ratio which could result in artifacts.

EDIT: so how do you scale a bitmap and preserve the bit depths? Easiest solution is to pass a bitmap with alpha channel into createScaledBitmap. You can also scale yourself like so:

                    Bitmap newBitmap = Bitmap.createBitmap(1024, 1024, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(newBitmap);
                    final int width = src.getWidth();
                    final int height = src.getHeight();
                    final float sx = 1024  / (float)width;
                    final float sy = 1024 / (float)height;
                    Matrix m = new Matrix();
                    m.setScale(sx, sy);
                    canvas.drawBitmap(src,m,null );
                    src.recycle();

ANOTHER EDIT: take a look at this Question for pointers on how to deal with that.

Upvotes: 1

Tim
Tim

Reputation: 35933

OpenGL.org has this to say about that error:

GL_INVALID_VALUE​, 0x0501: Given when a value parameter is not a leval value for that function. This is only given for local problems; if the spec allows the value in certain circumstances, and other parameters or state dictate those circumstances, then GL_INVALID_OPERATION is the result instead.

Step one is to find the exact opengl call that is causing the problem. You'll have to do trial and error to see which line is throwing that error. If you set up the program flow like this:

glSomeCallA()
glGetError() //returns 0
glSomeCallB()
glGetError() //returns 0
glSomeCallC()  
glGetError() //returns 0x501

Then you'll know that glSomeCallC was the operation that caused the error. If you look at the man page for that particular call, it will enumerate everything that could cause a specific error to occur.

In your case I'll guess that the error will be after glTexImage call just to save you some time, though I'm not positive.

If you look at the glTexImage man page, at the bottom it will list everything that can cause an invalid value error. My guess will be that your texture is larger than the GL_MAX_TEXTURE_SIZE. You can confirm this by checking glGetIntegerv(GL_MAX_TEXTURE_SIZE);

Upvotes: 1

Related Questions