带有 QDataStream 的 TGA 读取头

2A-66-42

我正在尝试使用 Qt QDataStream 读取 TGA 文件头。我从具有固定类型的规范中具有以下结构:

#pragma pack(push, 1)
/* TGA header */
struct tga_header_t
{
    quint8   id_lenght;            /* size of image id */
    quint8   colormap_type;        /* 1 is has a colormap */
    quint8   image_type;           /* compression type */

    /* Color Map Specification */
    quint16  cm_origin;            /* colormap origin */
    quint16  cm_length;            /* colormap length */
    quint8   cm_size;              /* colormap size */

    /* Image Specification */
    quint16  x_origin;           /* bottom left x coord origin */
    quint16  y_origin;           /* bottom left y coord origin */
    quint16  width;              /* picture width (in pixels) */
    quint16  height;             /* picture height (in pixels) */
    quint8   pixel_depth;        /* bits per pixel: 8, 16, 24 or 32 */
    quint8   image_descriptor;   /* 24 bits = 0x00; 32 bits = 0x80 */
};
#pragma pack(pop)

我用 QFile 打开一个文件,然后用它构造一个 QDataStream,如下所示:

QFile file(path);
tga_header_t header;

file.open(QIODevice::ReadOnly);
QDataStream stream(&file);
stream >> header.id_lenght >>
          header.colormap_type >>
          header.image_type >>
          header.cm_origin >>
          header.cm_length >>
          header.cm_size >>
          header.x_origin >>
          header.y_origin >>
          header.width >>
          header.height >>
          header.pixel_depth >>
          header.image_descriptor;
qDebug() << header.id_lenght << "id_lenght" <<
            header.colormap_type << "colormap_type" <<
            header.image_type << "image_type" <<
            header.cm_origin << "cm_origin" <<
            header.cm_length << "cm_length" <<
            header.cm_size << "cm_size" <<
            header.x_origin << "x_origin" <<
            header.y_origin << "y_origin" <<
            header.width << "width" <<
            header.height << "height" <<
            header.pixel_depth << "pixel_depth" <<
            header.image_descriptor << "image_descriptor" <<
            "SIZE:" << sizeof(header);

问题在于我得到的宽度和高度:

0 id_lenght 0 colormap_type 2 image_type 0 cm_origin 0 cm_length 0 cm_size 0 x_origin 0 y_origin 22021 width 3 height 24 pixel_depth 0 image_descriptor SIZE: 18

我应该得到 1366 和 768。如果我 hexdump 文件我得到:

0000000 0000 0002 0000 0000 0000 0000 0556 0300
0000010 0018 0000 0000 0000 0000 0000 0000 0000

这是奇怪的,因为它在开始一个额外的字节之间,另一个失踪的地方0x00020x0556

更新:
使用 fstream 有效,所以如果我错了,请纠正我,但我认为该<<运算符没有按预期工作(仅读取填充结构变量所需的大小)。

    stream.read((char*)&header.id_lenght, sizeof(header.id_lenght));
    stream.read((char*)&header.colormap_type, sizeof(header.colormap_type));
    stream.read((char*)&header.image_type, sizeof(header.image_type));
    stream.read((char*)&header.cm_origin, sizeof(header.cm_origin));
    stream.read((char*)&header.cm_length, sizeof(header.cm_length));
    stream.read((char*)&header.cm_size, sizeof(header.cm_size));
    stream.read((char*)&header.x_origin, sizeof(header.x_origin));
    stream.read((char*)&header.y_origin, sizeof(header.y_origin));
    stream.read((char*)&header.width, sizeof(header.width));
    stream.read((char*)&header.height, sizeof(header.height));
    stream.read((char*)&header.pixel_depth, sizeof(header.pixel_depth));
    stream.read((char*)&header.image_descriptor, sizeof(header.image_descriptor));

使用 QDataStream::readRawData 也有效:

stream.readRawData((char*)&header.id_lenght, sizeof(header.id_lenght));
    stream.readRawData((char*)&header.colormap_type, sizeof(header.colormap_type));
    stream.readRawData((char*)&header.image_type, sizeof(header.image_type));
    stream.readRawData((char*)&header.cm_origin, sizeof(header.cm_origin));
    stream.readRawData((char*)&header.cm_length, sizeof(header.cm_length));
    stream.readRawData((char*)&header.cm_size, sizeof(header.cm_size));
    stream.readRawData((char*)&header.x_origin, sizeof(header.x_origin));
    stream.readRawData((char*)&header.y_origin, sizeof(header.y_origin));
    stream.readRawData((char*)&header.width, sizeof(header.width));
    stream.readRawData((char*)&header.height, sizeof(header.height));
    stream.readRawData((char*)&header.pixel_depth, sizeof(header.pixel_depth));
    stream.readRawData((char*)&header.image_descriptor, sizeof(header.image_descriptor));
瓦伦丁·海尼茨

数据正常。您的 PC 将 16 位字解释为小端。在文件中,它们存储为大端。

对于所有 16 位类型,您应该交换低/高字节。你也可以使用 Qt 的辅助函数:http : //doc.qt.io/qt-4.8/qtendian.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章