我的目标是上传一个GL_ALPHA纹理到GPU并与OpenGL一起使用。为此,我需要一个ALPHA_8格式的位图。目前,我正在使用BitmapFactory加载一个8位(带有灰度调色板)PNG,但getConfig()说它是ARGB_8888格式的。
c0vxltue1#
我最终使用了PNGJ library,如下所示:
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; }
1条答案
按热度按时间c0vxltue1#
我最终使用了PNGJ library,如下所示: