2017-07-23 96 views
0

我想讀Qt QDataStream的TGA文件標題。我已經從規格如下結構與固定類型:TGA閱讀標題與QDataStream

#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。如果我進制打印文件獲得:

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)); 
+0

'stream >> header.id_lenght; ......這是一個錯字嗎?我相信它應該是'stream >> header.id_lenght >> ...'。 – DimChtz

+0

是的,這是一個錯字(更正) –

回答

0

的數據確定。你的電腦將16位單詞解釋爲little-endian。在文件中,它們被存儲爲大端序列。

對於所有16位類型,您應該交換低/高字節。你也可以使用Qt的幫助函數:http://doc.qt.io/qt-4.8/qtendian.html