首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++ TGA读取失败

C++ TGA读取失败
EN

Stack Overflow用户
提问于 2017-05-17 20:59:16
回答 1查看 1.2K关注 0票数 0

我正在使用下面的java方法为tga编写一个android.graphics.Bitmap,我已经在photoshop中打开了照片,它没有问题。在本机中,我必须使用opengl加载和显示此图像,但是加载图像是不正确的,并且我在屏幕上看到不正确的颜色,c++ tga加载程序如下所示。有人有什么问题吗?

java写tga方法:

代码语言:javascript
运行
复制
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++:

代码语言:javascript
运行
复制
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加载器方法:

代码语言:javascript
运行
复制
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呈现后

生成的qr码进入sdcard,用photoshop打开。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-18 19:34:15

第二张照片用java写成,然后在photoshop中打开。我发现了错误。正如我一直在想的,我得到了一个错误的偏移,但没有在写作/阅读过程中。

上传到gpu:

我有

代码语言:javascript
运行
复制
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB.....);

而不是

代码语言:javascript
运行
复制
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA....);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44034678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档