【发布时间】:2017-05-17 20:59:16
【问题描述】:
我正在使用下面的java方法将android.graphics.Bitmap写入tga,我已经在photoshop中打开了照片,没关系。在本机中,我必须使用 opengl 加载和显示此图像,但是图像的加载不正确,并且我在屏幕上看到不正确的颜色,下面是 c++ tga 加载器。有人知道这是什么问题吗?
java写tga方法:
public static void writeTGA(Bitmap src, String path) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(src.getRowBytes() * src.getHeight());
src.copyPixelsToBuffer(buffer);
boolean alpha = src.hasAlpha();
byte[] data;
byte[] pixels = buffer.array();
if (pixels.length != src.getWidth() * src.getHeight() * (alpha ? 4 : 3))
throw new IllegalStateException();
data = new byte[pixels.length];
for(int i=0;i < pixels.length; i += 4){// rgba -> bgra
data[i] = pixels[i+2];
data[i+1] = pixels[i+1];
data[i+2] = pixels[i];
data[i+3] = pixels[i+3];
}
byte[] header = new byte[18];
header[2] = 2; // uncompressed, true-color image
header[12] = (byte) ((src.getWidth() >> 0) & 0xFF);
header[13] = (byte) ((src.getWidth() >> 8) & 0xFF);
header[14] = (byte) ((src.getHeight() >> 0) & 0xFF);
header[15] = (byte) ((src.getHeight() >> 8) & 0xFF);
header[16] = (byte) (alpha ? 32 : 24); // bits per pixel
header[17] = (byte) ((alpha ? 8 : 0) | (1 << 4));
File file = new File(path);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.write(header);
raf.write(data);
raf.setLength(raf.getFilePointer()); // trim
raf.close();
}
tga 18 位标头 c++ :
typedef struct _tgaheader {
BYTE IDLength; /* 00h Size of Image ID field */
BYTE ColorMapType; /* 01h Color map type */
BYTE ImageType; /* 02h Image type code */
BYTE CMapStart[2]; /* 03h Color map origin */
BYTE CMapLength[2]; /* 05h Color map length */
BYTE CMapDepth; /* 07h Depth of color map entries */
WORD XOffset; /* 08h X origin of image */
WORD YOffset; /* 0Ah Y origin of image */
WORD Width; /* 0Ch Width of image */
WORD Height; /* 0Eh Height of image */
BYTE PixelDepth; /* 10h Image pixel size */
BYTE ImageDescriptor; /* 11h Image descriptor byte */
} TGAHEADER;
tga 加载器方法:
void TgaFormat:: LoadImage(const char *path) {
FILE* filePtr = fopen(path, "rb");
long imageSize;
short pixel_size;
unsigned char colorSwap;
// Open the TGA file.
if( filePtr == NULL){
LOGI("cannot find Tga File!");
return;
}
fread(&file_header, 1, sizeof(TGAHEADER), filePtr);
short sz = sizeof(TGAHEADER);
// 2 (uncompressed RGB image), 3 (uncompressed black-and-white images).
if (file_header.ImageType != 2 ){
fclose(filePtr);
LOGI("this file is not a TGA!");
return;
}
// Color mode -> 3 = BGR, 4 = BGRA.
pixel_size = file_header.PixelDepth / 8;
imageSize = file_header.Width * file_header.Height * pixel_size;
m_rgba_data = (BYTE* )malloc( sizeof(BYTE) * imageSize );
if( fread(m_rgba_data, 1, imageSize, filePtr) != imageSize ) {
fclose(filePtr);
return ;
}
fclose(filePtr);
// Change from BGRA to RGBA so OpenGL can read the image data.
for (int imageIdx = 0; imageIdx < imageSize; imageIdx += pixel_size) {
colorSwap = m_rgba_data[imageIdx];
m_rgba_data[imageIdx] = m_rgba_data[imageIdx + 2];
m_rgba_data[imageIdx + 2] = colorSwap;
}
}
在 android native 中读取 tga 文件并使用 opengles 渲染后
生成的二维码放入sdcard用photoshop打开
【问题讨论】:
-
你可以试试
asserting,你的 TGA 头文件确实是 18 个字节,而且你的编译器没有填充任何东西...... -
我已经做了'int sz = sizeof(TGAHEADER); ' 然后调试,sz取值18;
-
那么图像中事物的几何形状和形状是正确的,但颜色是错误的?始终错误还是随机?
-
我将一个随机的 .bmp 文件作为纹理放在相同的形状上,它可以绑定和渲染。这个 tga 加载器很奇怪.. 我有宽度、高度、位深度、文件类型 OK,然后它分配正确大小的向量.. 我不明白为什么我有奇怪的颜色..
-
是的,几何是正确的,颜色是错误的,不是一直退出,我可以选择我的图像的 2x,左边的像素线是奇数线,右边是偶数线