如何在Android上使用BitmapFactory或ImageDecoder将图像强制加载为ALPHA_8位图?

huangapple go评论100阅读模式
英文:

How to force an image to be loaded as ALPHA_8 bitmap on Android with BitmapFactory or ImageDecoder?

问题

我的目标是将 GL_ALPHA 纹理上传到 GPU 并在 OpenGL 中使用它。为此,我需要一个 ALPHA_8 格式的位图。

目前,我正在使用 BitmapFactory 加载一个 8 位(带灰度调色板)的 PNG,但 getConfig() 表示它是 ARGB_8888 格式。

英文:

My goal is to upload a GL_ALPHA texture to the GPU and use it with OpenGL. For that, I need a bitmap in ALPHA_8 format.

Currently, I'm using BitmapFactory to load a 8-bit (with grayscale palette) PNG but getConfig() says it's in ARGB_8888 format.

答案1

得分: 0

我最终使用了PNGJ库,如下:

import ar.com.hjg.pngj.IImageLine;
import ar.com.hjg.pngj.ImageLineHelper;
import ar.com.hjg.pngj.PngReader;

public static Bitmap loadAlpha8Bitmap(Context context, String fileName) {
    Bitmap result = null;

    try {
        PngReader reader = new PngReader(context.getAssets().open(fileName));

        if (reader.imgInfo.channels == 3 && reader.imgInfo.bitDepth == 8) {
            int size = reader.imgInfo.cols * reader.imgInfo.rows;
            ByteBuffer buffer = ByteBuffer.allocate(size);

            for (int row = 0; row < reader.imgInfo.rows; row++) {
                IImageLine line = reader.readRow();

                for (int col = 0; col < reader.imgInfo.cols; col++) {
                    int pixel = ImageLineHelper.getPixelRGB8(line, col);
                    byte gray = (byte)(pixel & 0x000000ff);
                    buffer.put(row * reader.imgInfo.cols + col, gray);
                }
            }

            reader.end();

            result = Bitmap.createBitmap(reader.imgInfo.cols, reader.imgInfo.rows, Bitmap.Config.ALPHA_8);
            result.copyPixelsFromBuffer(buffer);
        }
    } catch (IOException e) {}

    return result;
}
英文:

I ended up using the PNGJ library, as follows:

import ar.com.hjg.pngj.IImageLine;
import ar.com.hjg.pngj.ImageLineHelper;
import ar.com.hjg.pngj.PngReader;

public static Bitmap loadAlpha8Bitmap(Context context, String fileName) {
    Bitmap result = null;

    try {
        PngReader reader = new PngReader(context.getAssets().open(fileName));

        if (reader.imgInfo.channels == 3 &amp;&amp; reader.imgInfo.bitDepth == 8) {
            int size = reader.imgInfo.cols * reader.imgInfo.rows;
            ByteBuffer buffer = ByteBuffer.allocate(size);

            for (int row = 0; row &lt; reader.imgInfo.rows; row++) {
                IImageLine line = reader.readRow();

                for (int col = 0; col &lt; reader.imgInfo.cols; col++) {
                    int pixel = ImageLineHelper.getPixelRGB8(line, col);
                    byte gray = (byte)(pixel &amp; 0x000000ff);
                    buffer.put(row * reader.imgInfo.cols + col, gray);
                }
            }

            reader.end();

            result = Bitmap.createBitmap(reader.imgInfo.cols, reader.imgInfo.rows, Bitmap.Config.ALPHA_8);
            result.copyPixelsFromBuffer(buffer);
        }
    } catch (IOException e) {}

    return result;
}

huangapple
  • 本文由 发表于 2023年4月20日 02:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定