C++ TGA 读取失败

C++ TGA reading fails

我正在使用下面的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 原生读取 tga 文件并使用 opengles 渲染后

生成的二维码存入sdcard用photoshop打开

第二张照片是在java中写的,然后用photoshop打开。我发现了错误。正如我一直在想的那样,我得到了一个错误的偏移量,但不是在 writing/reading 过程中。

进入上传到gpu:

我有

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB.....);

而不是

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA....);

因为我的像素大小是 4 (RGBA) 而不是 3 (RGB)。