Qt 4.8
Public Types | Public Functions | Static Private Functions | Properties | List of all members
QTgaFile Class Reference

#include <qtgafile.h>

Public Types

enum  Compression { NoCompression = 0, RleCompression = 1 }
 
enum  FooterOffset { ExtensionOffset = 0, DeveloperOffset = 4, SignatureOffset = 8, FooterSize = 26 }
 
enum  HeaderOffset {
  IdLength = 0, ColorMapType = 1, ImageType = 2, CMapStart = 3,
  CMapLength = 5, CMapDepth = 7, XOffset = 8, YOffset = 10,
  Width = 12, Height = 14, PixelDepth = 16, ImageDescriptor = 17,
  HeaderSize = 18
}
 

Public Functions

Compression compression () const
 
QString errorMessage () const
 
int height () const
 
bool isValid () const
 
 QTgaFile (QIODevice *)
 Construct a new QTgaFile object getting data from device. More...
 
QImage readImage ()
 Reads an image file from the QTgaFile's device, and returns it. More...
 
QSize size () const
 
int width () const
 
int xOffset () const
 
int yOffset () const
 
 ~QTgaFile ()
 Destroy the device, recovering any resources. More...
 

Static Private Functions

static quint16 littleEndianInt (const unsigned char *d)
 Returns the integer encoded in the two little endian bytes at d. More...
 

Properties

QIODevicemDevice
 
QString mErrorMessage
 
unsigned char mHeader [HeaderSize]
 

Detailed Description

Since
4.8
Warning
This function is not part of the public interface.

File data container for a TrueVision Graphics format file.

Format is as described here: http://local.wasp.uwa.edu.au/~pbourke/dataformats/tga/ http://netghost.narod.ru/gff2/graphics/summary/tga.htm

Usage is:

QTgaFile tga(myFile);
QImage tgaImage;
if (tga.isValid())
tgaImage = tga.readImage();

The class is designed to handle sequential and non-sequential sources, so during construction the mHeader is read. Then during the readImage() call the rest of the data is read.

After passing myFile to the constructor, if the QIODevice *myFile is read, or has seek() called, the results are undefined - so don't do that.

Definition at line 52 of file qtgafile.h.

Enumerations

◆ Compression

Enumerator
NoCompression 
RleCompression 

Definition at line 55 of file qtgafile.h.

◆ FooterOffset

Enumerator
ExtensionOffset 
DeveloperOffset 
SignatureOffset 
FooterSize 

Definition at line 76 of file qtgafile.h.

◆ HeaderOffset

Enumerator
IdLength 
ColorMapType 
ImageType 
CMapStart 
CMapLength 
CMapDepth 
XOffset 
YOffset 
Width 
Height 
PixelDepth 
ImageDescriptor 
HeaderSize 

Definition at line 60 of file qtgafile.h.

60  {
61  IdLength = 0, /* 00h Size of Image ID field */
62  ColorMapType = 1, /* 01h Color map type */
63  ImageType = 2, /* 02h Image type code */
64  CMapStart = 3, /* 03h Color map origin */
65  CMapLength = 5, /* 05h Color map length */
66  CMapDepth = 7, /* 07h Depth of color map entries */
67  XOffset = 8, /* 08h X origin of image */
68  YOffset = 10, /* 0Ah Y origin of image */
69  Width = 12, /* 0Ch Width of image */
70  Height = 14, /* 0Eh Height of image */
71  PixelDepth = 16, /* 10h Image pixel size */
72  ImageDescriptor = 17, /* 11h Image descriptor byte */
73  HeaderSize = 18
74  };

Constructors and Destructors

◆ QTgaFile()

QTgaFile::QTgaFile ( QIODevice device)

Construct a new QTgaFile object getting data from device.

The object does not take ownership of the device, but until the object is destroyed do not do any non-const operations, eg seek or read on the device.

Definition at line 130 of file qtgafile.cpp.

131  : mDevice(device)
132 {
133  ::memset(mHeader, 0, HeaderSize);
134  if (!mDevice->isReadable())
135  {
136  mErrorMessage = QObject::tr("Could not read image data");
137  return;
138  }
139  if (mDevice->isSequential())
140  {
141  mErrorMessage = QObject::tr("Sequential device (eg socket) for image read not supported");
142  return;
143  }
144  if (!mDevice->seek(0))
145  {
146  mErrorMessage = QObject::tr("Seek file/device for image read failed");
147  return;
148  }
149  int bytes = device->read((char*)mHeader, HeaderSize);
150  if (bytes != HeaderSize)
151  {
152  mErrorMessage = QObject::tr("Image mHeader read failed");
153  device->seek(0);
154  return;
155  }
156  if (mHeader[ImageType] != 2)
157  {
158  // TODO: should support other image types
159  mErrorMessage = QObject::tr("Image type not supported");
160  device->seek(0);
161  return;
162  }
163  int bitsPerPixel = mHeader[PixelDepth];
164  bool validDepth = (bitsPerPixel == 16 || bitsPerPixel == 24 || bitsPerPixel == 32);
165  if (!validDepth)
166  {
167  mErrorMessage = QObject::tr("Image depth not valid");
168  }
169  int fileBytes = mDevice->size();
170  if (!mDevice->seek(fileBytes - FooterSize))
171  {
172  mErrorMessage = QObject::tr("Could not seek to image read footer");
173  device->seek(0);
174  return;
175  }
176  char footer[FooterSize];
177  bytes = mDevice->read((char*)footer, FooterSize);
178  if (bytes != FooterSize)
179  {
180  mErrorMessage = QObject::tr("Could not read footer");
181  }
182  if (qstrncmp(&footer[SignatureOffset], "TRUEVISION-XFILE", 16) != 0)
183  {
184  mErrorMessage = QObject::tr("Image type (non-TrueVision 2.0) not supported");
185  }
186  if (!mDevice->seek(0))
187  {
188  mErrorMessage = QObject::tr("Could not reset to start position");
189  }
190 }
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
Definition: qiodevice.cpp:642
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
Definition: qiodevice.cpp:544
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QString mErrorMessage
Definition: qtgafile.h:99
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
QIODevice * mDevice
Definition: qtgafile.h:101
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
Definition: qiodevice.cpp:454
unsigned char mHeader[HeaderSize]
Definition: qtgafile.h:100
int qstrncmp(const char *str1, const char *str2, uint len)
Definition: qbytearray.h:101
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success...
Definition: qiodevice.cpp:659

◆ ~QTgaFile()

QTgaFile::~QTgaFile ( )

Destroy the device, recovering any resources.

Warning
This function is not part of the public interface.

Definition at line 199 of file qtgafile.cpp.

200 {
201 }

Functions

◆ compression()

QTgaFile::Compression QTgaFile::compression ( ) const
inline

Definition at line 151 of file qtgafile.h.

Referenced by QTgaHandler::option().

152 {
153  // TODO: for now, only handle type 2 files, with no color table
154  // and no compression
155  return NoCompression;
156 }

◆ errorMessage()

QString QTgaFile::errorMessage ( ) const
inline

Definition at line 109 of file qtgafile.h.

110 {
111  return mErrorMessage;
112 }
QString mErrorMessage
Definition: qtgafile.h:99

◆ height()

int QTgaFile::height ( ) const
inline

Definition at line 141 of file qtgafile.h.

Referenced by readImage(), and size().

142 {
143  return littleEndianInt(&mHeader[Height]);
144 }
unsigned char mHeader[HeaderSize]
Definition: qtgafile.h:100
static quint16 littleEndianInt(const unsigned char *d)
Returns the integer encoded in the two little endian bytes at d.
Definition: qtgafile.h:121

◆ isValid()

bool QTgaFile::isValid ( ) const
inline

Definition at line 104 of file qtgafile.h.

Referenced by QTgaHandler::canRead(), and readImage().

105 {
106  return mErrorMessage.isEmpty();
107 }
QString mErrorMessage
Definition: qtgafile.h:99
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704

◆ littleEndianInt()

quint16 QTgaFile::littleEndianInt ( const unsigned char *  d)
inlinestaticprivate

Returns the integer encoded in the two little endian bytes at d.

Warning
This function is not part of the public interface.

Definition at line 121 of file qtgafile.h.

Referenced by height(), readImage(), width(), xOffset(), and yOffset().

122 {
123  return d[0] + d[1] * 256;
124 }
double d
Definition: qnumeric_p.h:62

◆ readImage()

QImage QTgaFile::readImage ( )

Reads an image file from the QTgaFile's device, and returns it.

Warning
This function is not part of the public interface.

This method seeks to the absolute position of the image data in the file, so no assumptions are made about where the devices read pointer is when this method is called. For this reason only random access devices are supported.

If the constructor completed successfully, such that isValid() returns true, then this method is likely to succeed, unless the file is somehow corrupted.

In the case that the read fails, the QImage returned will be null, such that QImage::isNull() will be true.

Definition at line 220 of file qtgafile.cpp.

Referenced by QTgaHandler::read().

221 {
222  if (!isValid())
223  return QImage();
224 
225  int offset = mHeader[IdLength]; // Mostly always zero
226 
227  // Even in TrueColor files a color pallette may be present
228  if (mHeader[ColorMapType] == 1)
230 
231  mDevice->seek(HeaderSize + offset);
232 
233  char dummy;
234  for (int i = 0; i < offset; ++i)
235  mDevice->getChar(&dummy);
236 
237  int bitsPerPixel = mHeader[PixelDepth];
238  int imageWidth = width();
239  int imageHeight = height();
240 
241  unsigned char desc = mHeader[ImageDescriptor];
242  //unsigned char xCorner = desc & 0x10; // 0 = left, 1 = right
243  unsigned char yCorner = desc & 0x20; // 0 = lower, 1 = upper
244 
245  QImage im(imageWidth, imageHeight, QImage::Format_ARGB32);
246  TgaReader *reader = 0;
247  if (bitsPerPixel == 16)
248  reader = new Tga16Reader();
249  else if (bitsPerPixel == 24)
250  reader = new Tga24Reader();
251  else if (bitsPerPixel == 32)
252  reader = new Tga32Reader();
253  TgaReader &read = *reader;
254 
255  // For now only deal with yCorner, since no one uses xCorner == 1
256  // Also this is upside down, since Qt has the origin flipped
257  if (yCorner)
258  {
259  for (int y = 0; y < imageHeight; ++y)
260  for (int x = 0; x < imageWidth; ++x)
261  im.setPixel(x, y, read(mDevice));
262  }
263  else
264  {
265  for (int y = imageHeight - 1; y >= 0; --y)
266  for (int x = 0; x < imageWidth; ++x)
267  im.setPixel(x, y, read(mDevice));
268  }
269 
270  delete reader;
271 
272  // TODO: add processing of TGA extension information - ie TGA 2.0 files
273  return im;
274 }
bool getChar(char *c)
Reads one character from the device and stores it in c.
Definition: qiodevice.cpp:1536
QIODevice * mDevice
Definition: qtgafile.h:101
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
unsigned char mHeader[HeaderSize]
Definition: qtgafile.h:100
bool isValid() const
Definition: qtgafile.h:104
static quint16 littleEndianInt(const unsigned char *d)
Returns the integer encoded in the two little endian bytes at d.
Definition: qtgafile.h:121
int width() const
Definition: qtgafile.h:136
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success...
Definition: qiodevice.cpp:659
int height() const
Definition: qtgafile.h:141

◆ size()

QSize QTgaFile::size ( ) const
inline

Definition at line 146 of file qtgafile.h.

Referenced by QTgaHandler::option().

147 {
148  return QSize(width(), height());
149 }
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
int width() const
Definition: qtgafile.h:136
int height() const
Definition: qtgafile.h:141

◆ width()

int QTgaFile::width ( ) const
inline

Definition at line 136 of file qtgafile.h.

Referenced by readImage(), and size().

137 {
138  return littleEndianInt(&mHeader[Width]);
139 }
unsigned char mHeader[HeaderSize]
Definition: qtgafile.h:100
static quint16 littleEndianInt(const unsigned char *d)
Returns the integer encoded in the two little endian bytes at d.
Definition: qtgafile.h:121

◆ xOffset()

int QTgaFile::xOffset ( ) const
inline

Definition at line 126 of file qtgafile.h.

127 {
128  return littleEndianInt(&mHeader[XOffset]);
129 }
unsigned char mHeader[HeaderSize]
Definition: qtgafile.h:100
static quint16 littleEndianInt(const unsigned char *d)
Returns the integer encoded in the two little endian bytes at d.
Definition: qtgafile.h:121

◆ yOffset()

int QTgaFile::yOffset ( ) const
inline

Definition at line 131 of file qtgafile.h.

132 {
133  return littleEndianInt(&mHeader[YOffset]);
134 }
unsigned char mHeader[HeaderSize]
Definition: qtgafile.h:100
static quint16 littleEndianInt(const unsigned char *d)
Returns the integer encoded in the two little endian bytes at d.
Definition: qtgafile.h:121

Properties

◆ mDevice

QIODevice* QTgaFile::mDevice
private

Definition at line 101 of file qtgafile.h.

Referenced by QTgaFile(), and readImage().

◆ mErrorMessage

QString QTgaFile::mErrorMessage
private

Definition at line 99 of file qtgafile.h.

Referenced by errorMessage(), isValid(), and QTgaFile().

◆ mHeader

unsigned char QTgaFile::mHeader[HeaderSize]
private

Definition at line 100 of file qtgafile.h.

Referenced by height(), QTgaFile(), readImage(), width(), xOffset(), and yOffset().


The documentation for this class was generated from the following files: