1

我正在尝试读取我在 Paint.net 中创建的文件的 TGA 标头。它似乎有什么问题。如果我使用规范中的标头结构,例如:

typedef struct {
    CHAR  idlength;
    CHAR  colourmaptype;
    CHAR  datatypecode;
    WORD colourmaporigin;
    WORD colourmaplength;
    CHAR  colourmapdepth;
    WORD x_origin;
    WORD y_origin;
    WORD width;
    WORD height;
    CHAR  bitsperpixel;
    CHAR  imagedescriptor;
} TGAHEADER;

我明白了:

Data size: 0
Color Map type: 0
Data Type code: 2
Bits per-pixel: 0
Size: 501 x 2080

这是错误的,因为我的图像是 501x501,每像素 32 位。但是,如果我从结构中注释掉两个字节,fe this one colourmaporigin,我会得到:

Data size: 0
Color Map type: 0
Data Type code: 2
Bits per-pixel: 32
Size: 501 x 501

哪个是对的。我正在阅读我在这种文件格式上找到的所有内容。它从来没有说这些字段中的任何一个都是可选的。

我怎么会得到这样的结果?

下面是读取数据的代码:

void Image::readTGA()
{
    TGAHEADER fileHeader;

    std::ifstream fileHandle(fileName, std::ios::binary);
    if (fileHandle.is_open())
    {
        fileHandle.read((char*)(&fileHeader), sizeof(TGAHEADER));
        fileHandle.close();
    }
    else
    {
        std::cout << "An error occured when opening a file." << std::endl;
    }
}

我正在使用 VS2015,针对 x86 平台。

4

1 回答 1

2

这是一个填充问题。使用 Visual Studio,您可以使用#pragma pack(1)编译器指令禁用任何结构填充。

示范

#include<stdio.h>
#include<windows.h>

// Default packing of structure with padding

typedef struct {
  CHAR  idlength;
  CHAR  colourmaptype;
  CHAR  datatypecode;
  WORD colourmaporigin;
  WORD colourmaplength;
  CHAR  colourmapdepth;
  WORD x_origin;
  WORD y_origin;
  WORD width;
  WORD height;
  CHAR  bitsperpixel;
  CHAR  imagedescriptor;
} TGAHEADER;


#pragma pack(1) // structure fields are aligned to byte boundary (no padding)

typedef struct {
  CHAR  idlength;
  CHAR  colourmaptype;
  CHAR  datatypecode;
  WORD colourmaporigin;
  WORD colourmaplength;
  CHAR  colourmapdepth;
  WORD x_origin;
  WORD y_origin;
  WORD width;
  WORD height;
  CHAR  bitsperpixel;
  CHAR  imagedescriptor;
} TGAHEADER_PACKED;

int main()
{
  printf("Offset of field bitsperpixel in TGAHEADER structure %d\n", offsetof(TGAHEADER, bitsperpixel));
  printf("Offset of field bitsperpixel in packed TGAHEADER structure %d\n", offsetof(TGAHEADER_PACKED, bitsperpixel));
}

输出:

Offset of field bitsperpixel in TGAHEADER structure 18
Offset of field bitsperpixel in packed TGAHEADER structure 16
于 2017-08-23T15:21:18.773 回答