C ++ TGA读取失败

时间:2017-05-17 20:59:16

标签: c++ image bitmap tga

我使用下面的java方法将一个android.graphics.Bitmap写入tga,我已经在photoshop中打开了这张照片,而且它很好。在本机我必须使用opengl加载和显示此图像,但图像的加载不正确,我看到屏幕上的颜色不正确,c ++ tga加载器在下面。任何人都有什么想法是什么问题?

java write 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 bit header 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 loader方法:

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渲染之后

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

1 个答案:

答案 0 :(得分:0)

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

上传到gpu:

我有

    <?= $form->field($model, 'page_content')->widget(TinyMce::className(), [
    'options' => ['rows' => 50],
    'language' => 'en_CA',
    'clientOptions' => [
        //'inline' => true,
        'content_css' => $content_css,
        'plugins' => [
            "advlist autolink lists link charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste",
            "image imagetools spellchecker visualchars textcolor",
            "autosave colorpicker hr nonbreaking template"
        ],
        'toolbar1' => "undo redo | styleselect fontselect fontsizeselect forecolor backcolor | bold italic",
        'toolbar2' => "alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        'image_advtab' => true,
        'templates' => [
            [ 'title'=>'Test template 1', 'content'=>'Test 1' ],
            [ 'title'=>'Test template 2', 'content'=>'Test 2' ]
        ],
        'visualblocks_default_state'=>true,

        'images_upload_url'=>'postAcceptor.php',
        // here we add custom filepicker only to Image dialog
        'file_picker_types'=>'image',
        // and here's our custom image picker
        'file_picker_callback'=>"function(callback, value, meta) {
            var input = document.createElement('input');
            input.setAttribute('type', 'file');
            input.setAttribute('accept', 'image/*');

            input.onchange = function() {
                var file = this.files[0];

                var reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = function () {
                    var id = 'blobid' + (new Date()).getTime();
                    var blobCache =  tinymce.activeEditor.editorUpload.blobCache;
                    var blobInfo = blobCache.create(id, file, reader.result);
                    blobCache.add(blobInfo);

                    // call the callback and populate the Title field with the file name
                    callback(blobInfo.blobUri(), { title: file.name });
                };
            };
            input.click();
        }"
    ]
]);?>

而不是

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

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