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

#include <qgifhandler_p.h>

Inheritance diagram for QGifHandler:
QImageIOHandler

Public Functions

bool canRead () const
 Returns true if an image can be read from the device (i. More...
 
int currentImageNumber () const
 For image formats that support animation, this function returns the sequence number of the current image in the animation. More...
 
int imageCount () const
 For image formats that support animation, this function returns the number of images in the animation. More...
 
int loopCount () const
 For image formats that support animation, this function returns the number of times the animation should loop. More...
 
QByteArray name () const
 Use format() instead. More...
 
int nextImageDelay () const
 For image formats that support animation, this function returns the number of milliseconds to wait until reading the next image. More...
 
QVariant option (ImageOption option) const
 Returns the value assigned to option as a QVariant. More...
 
 QGifHandler ()
 
bool read (QImage *image)
 Read an image from the device, and stores it in image. More...
 
void setOption (ImageOption option, const QVariant &value)
 Sets the option option with the value value. More...
 
bool supportsOption (ImageOption option) const
 Returns true if the QImageIOHandler supports the option option; otherwise returns false. More...
 
bool write (const QImage &image)
 Writes the image image to the assigned device. More...
 
 ~QGifHandler ()
 
- Public Functions inherited from QImageIOHandler
virtual QRect currentImageRect () const
 Returns the rect of the current image. More...
 
QIODevicedevice () const
 Returns the device currently assigned to the QImageIOHandler. More...
 
QByteArray format () const
 Returns the format that is currently assigned to QImageIOHandler. More...
 
virtual bool jumpToImage (int imageNumber)
 For image formats that support animation, this function jumps to the image whose sequence number is imageNumber. More...
 
virtual bool jumpToNextImage ()
 For image formats that support animation, this function jumps to the next image. More...
 
 QImageIOHandler ()
 Constructs a QImageIOHandler object. More...
 
void setDevice (QIODevice *device)
 Sets the device of the QImageIOHandler to device. More...
 
void setFormat (const QByteArray &format)
 Sets the format of the QImageIOHandler to format. More...
 
void setFormat (const QByteArray &format) const
 Sets the format of the QImageIOHandler to format. More...
 
virtual ~QImageIOHandler ()
 Destructs the QImageIOHandler object. More...
 

Static Public Functions

static bool canRead (QIODevice *device)
 

Private Functions

bool imageIsComing () const
 

Properties

QByteArray buffer
 
QString fileName
 
int frameNumber
 
QGIFFormatgifFormat
 
QVector< QSizeimageSizes
 
QImage lastImage
 
int loopCnt
 
int nextDelay
 
bool scanIsCached
 

Additional Inherited Members

- Public Types inherited from QImageIOHandler
enum  ImageOption {
  Size, ClipRect, Description, ScaledClipRect,
  ScaledSize, CompressionRatio, Gamma, Quality,
  Name, SubType, IncrementalReading, Endianness,
  Animation, BackgroundColor, ImageFormat
}
 This enum describes the different options supported by QImageIOHandler. More...
 
- Protected Functions inherited from QImageIOHandler
 QImageIOHandler (QImageIOHandlerPrivate &dd)
 Constructs a QImageIOHandler object, using the private member dd. More...
 
- Protected Variables inherited from QImageIOHandler
QScopedPointer< QImageIOHandlerPrivated_ptr
 

Detailed Description

Definition at line 57 of file qgifhandler_p.h.

Constructors and Destructors

◆ QGifHandler()

QGifHandler::QGifHandler ( )

Definition at line 1042 of file qgifhandler.cpp.

1043 {
1044  gifFormat = new QGIFFormat;
1045  nextDelay = 100;
1046  loopCnt = -1;
1047  frameNumber = -1;
1048  scanIsCached = false;
1049 }
bool scanIsCached
Definition: qgifhandler_p.h:91
QGIFFormat * gifFormat
Definition: qgifhandler_p.h:82

◆ ~QGifHandler()

QGifHandler::~QGifHandler ( )

Definition at line 1051 of file qgifhandler.cpp.

1052 {
1053  delete gifFormat;
1054 }
QGIFFormat * gifFormat
Definition: qgifhandler_p.h:82

Functions

◆ canRead() [1/2]

bool QGifHandler::canRead ( ) const
virtual

Returns true if an image can be read from the device (i.

e., the image format is supported, the device can be read from and the initial header information suggests that the image can be read); otherwise returns false.

When reimplementing canRead(), make sure that the I/O device (device()) is left in its original state (e.g., by using peek() rather than read()).

See also
read(), QIODevice::peek()

Implements QImageIOHandler.

Definition at line 1078 of file qgifhandler.cpp.

Referenced by QGifPlugin::capabilities(), and createReadHandlerHelper().

1079 {
1080  if (canRead(device()) || imageIsComing()) {
1081  setFormat("gif");
1082  return true;
1083  }
1084 
1085  return false;
1086 }
bool canRead() const
Returns true if an image can be read from the device (i.
void setFormat(const QByteArray &format)
Sets the format of the QImageIOHandler to format.
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
bool imageIsComing() const

◆ canRead() [2/2]

bool QGifHandler::canRead ( QIODevice device)
static

Definition at line 1088 of file qgifhandler.cpp.

1089 {
1090  if (!device) {
1091  qWarning("QGifHandler::canRead() called with no device");
1092  return false;
1093  }
1094 
1095  char head[6];
1096  if (device->peek(head, sizeof(head)) == sizeof(head))
1097  return qstrncmp(head, "GIF87a", 6) == 0
1098  || qstrncmp(head, "GIF89a", 6) == 0;
1099  return false;
1100 }
qint64 peek(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, without side effects (i.
Definition: qiodevice.cpp:1563
Q_CORE_EXPORT void qWarning(const char *,...)
int qstrncmp(const char *str1, const char *str2, uint len)
Definition: qbytearray.h:101

◆ currentImageNumber()

int QGifHandler::currentImageNumber ( ) const
virtual

For image formats that support animation, this function returns the sequence number of the current image in the animation.

If this function is called before any image is read(), -1 is returned. The number of the first image in the sequence is 0.

If the image format does not support animation, 0 is returned.

See also
read()

Reimplemented from QImageIOHandler.

Definition at line 1201 of file qgifhandler.cpp.

1202 {
1203  return frameNumber;
1204 }

◆ imageCount()

int QGifHandler::imageCount ( ) const
virtual

For image formats that support animation, this function returns the number of images in the animation.

If the image format does not support animation, or if it is unable to determine the number of images, 0 is returned.

The default implementation returns 1 if canRead() returns true; otherwise 0 is returned.

Reimplemented from QImageIOHandler.

Definition at line 1177 of file qgifhandler.cpp.

1178 {
1179  if (!scanIsCached) {
1181  scanIsCached = true;
1182  }
1183  return imageSizes.count();
1184 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
static void scan(QIODevice *device, QVector< QSize > *imageSizes, int *loopCount)
Scans through the data stream defined by device and returns the image sizes found in the stream in th...
QVector< QSize > imageSizes
Definition: qgifhandler_p.h:90
bool scanIsCached
Definition: qgifhandler_p.h:91
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.

◆ imageIsComing()

bool QGifHandler::imageIsComing ( ) const
private

Definition at line 1058 of file qgifhandler.cpp.

1059 {
1060  const int GifChunkSize = 4096;
1061 
1062  while (!gifFormat->partialNewFrame) {
1063  if (buffer.isEmpty()) {
1064  buffer += device()->read(GifChunkSize);
1065  if (buffer.isEmpty())
1066  break;
1067  }
1068 
1069  int decoded = gifFormat->decode(&lastImage, (const uchar *)buffer.constData(), buffer.size(),
1070  &nextDelay, &loopCnt);
1071  if (decoded == -1)
1072  break;
1073  buffer.remove(0, decoded);
1074  }
1075  return gifFormat->partialNewFrame;
1076 }
QImage lastImage
Definition: qgifhandler_p.h:85
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
unsigned char uchar
Definition: qglobal.h:994
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int decode(QImage *image, const uchar *buffer, int length, int *nextFrameDelay, int *loopCount)
This function decodes some data into image changes.
bool partialNewFrame
Definition: qgifhandler.cpp:78
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QByteArray & remove(int index, int len)
Removes len bytes from the array, starting at index position pos, and returns a reference to the arra...
QGIFFormat * gifFormat
Definition: qgifhandler_p.h:82
QByteArray buffer
Definition: qgifhandler_p.h:84

◆ loopCount()

int QGifHandler::loopCount ( ) const
virtual

For image formats that support animation, this function returns the number of times the animation should loop.

If the image format does not support animation, 0 is returned.

Reimplemented from QImageIOHandler.

Definition at line 1186 of file qgifhandler.cpp.

1187 {
1188  if (!scanIsCached) {
1190  scanIsCached = true;
1191  }
1192 
1193  if (loopCnt == 0)
1194  return -1;
1195  else if (loopCnt == -1)
1196  return 0;
1197  else
1198  return loopCnt;
1199 }
static void scan(QIODevice *device, QVector< QSize > *imageSizes, int *loopCount)
Scans through the data stream defined by device and returns the image sizes found in the stream in th...
QVector< QSize > imageSizes
Definition: qgifhandler_p.h:90
bool scanIsCached
Definition: qgifhandler_p.h:91
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.

◆ name()

QByteArray QGifHandler::name ( ) const
virtual

Use format() instead.

Reimplemented from QImageIOHandler.

Definition at line 1206 of file qgifhandler.cpp.

1207 {
1208  return "gif";
1209 }

◆ nextImageDelay()

int QGifHandler::nextImageDelay ( ) const
virtual

For image formats that support animation, this function returns the number of milliseconds to wait until reading the next image.

If the image format does not support animation, 0 is returned.

Reimplemented from QImageIOHandler.

Definition at line 1172 of file qgifhandler.cpp.

1173 {
1174  return nextDelay;
1175 }

◆ option()

QVariant QGifHandler::option ( ImageOption  option) const
virtual

Returns the value assigned to option as a QVariant.

The type of the value depends on the option. For example, option(Size) returns a QSize variant.

See also
setOption(), supportsOption()

Reimplemented from QImageIOHandler.

Definition at line 1145 of file qgifhandler.cpp.

1146 {
1147  if (option == Size) {
1148  if (!scanIsCached) {
1150  scanIsCached = true;
1151  }
1152  // before the first frame is read, or we have an empty data stream
1153  if (frameNumber == -1)
1154  return (imageSizes.count() > 0) ? QVariant(imageSizes.at(0)) : QVariant();
1155  // after the last frame has been read, the next size is undefined
1156  if (frameNumber >= imageSizes.count() - 1)
1157  return QVariant();
1158  // and the last case: the size of the next frame
1159  return imageSizes.at(frameNumber + 1);
1160  } else if (option == Animation) {
1161  return true;
1162  }
1163  return QVariant();
1164 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
static void scan(QIODevice *device, QVector< QSize > *imageSizes, int *loopCount)
Scans through the data stream defined by device and returns the image sizes found in the stream in th...
QVector< QSize > imageSizes
Definition: qgifhandler_p.h:90
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
bool scanIsCached
Definition: qgifhandler_p.h:91
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
The Animation element is the base of all QML animations.

◆ read()

bool QGifHandler::read ( QImage image)
virtual

Read an image from the device, and stores it in image.

Returns true if the image is successfully read; otherwise returns false.

For image formats that support incremental loading, and for animation formats, the image handler can assume that image points to the previous frame.

See also
canRead()

Implements QImageIOHandler.

Definition at line 1102 of file qgifhandler.cpp.

1103 {
1104  const int GifChunkSize = 4096;
1105 
1106  while (!gifFormat->newFrame) {
1107  if (buffer.isEmpty()) {
1108  buffer += device()->read(GifChunkSize);
1109  if (buffer.isEmpty())
1110  break;
1111  }
1112 
1113  int decoded = gifFormat->decode(&lastImage, (const uchar *)buffer.constData(), buffer.size(),
1114  &nextDelay, &loopCnt);
1115  if (decoded == -1)
1116  break;
1117  buffer.remove(0, decoded);
1118  }
1119  if (gifFormat->newFrame || (gifFormat->partialNewFrame && device()->atEnd())) {
1120  *image = lastImage;
1121  ++frameNumber;
1122  gifFormat->newFrame = false;
1123  gifFormat->partialNewFrame = false;
1124  return true;
1125  }
1126 
1127  return false;
1128 }
QImage lastImage
Definition: qgifhandler_p.h:85
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
unsigned char uchar
Definition: qglobal.h:994
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int decode(QImage *image, const uchar *buffer, int length, int *nextFrameDelay, int *loopCount)
This function decodes some data into image changes.
bool partialNewFrame
Definition: qgifhandler.cpp:78
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QByteArray & remove(int index, int len)
Removes len bytes from the array, starting at index position pos, and returns a reference to the arra...
QGIFFormat * gifFormat
Definition: qgifhandler_p.h:82
QByteArray buffer
Definition: qgifhandler_p.h:84

◆ setOption()

void QGifHandler::setOption ( ImageOption  option,
const QVariant value 
)
virtual

Sets the option option with the value value.

See also
option(), ImageOption

Reimplemented from QImageIOHandler.

Definition at line 1166 of file qgifhandler.cpp.

1167 {
1168  Q_UNUSED(option);
1169  Q_UNUSED(value);
1170 }
QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ supportsOption()

bool QGifHandler::supportsOption ( ImageOption  option) const
virtual

Returns true if the QImageIOHandler supports the option option; otherwise returns false.

For example, if the QImageIOHandler supports the Size option, supportsOption(Size) must return true.

See also
setOption(), option()

Reimplemented from QImageIOHandler.

Definition at line 1136 of file qgifhandler.cpp.

1137 {
1138  if (!device() || device()->isSequential())
1139  return option == Animation;
1140  else
1141  return option == Size
1142  || option == Animation;
1143 }
QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.

◆ write()

bool QGifHandler::write ( const QImage image)
virtual

Writes the image image to the assigned device.

Returns true on success; otherwise returns false.

The default implementation does nothing, and simply returns false.

Reimplemented from QImageIOHandler.

Definition at line 1130 of file qgifhandler.cpp.

1131 {
1132  Q_UNUSED(image);
1133  return false;
1134 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

Properties

◆ buffer

QByteArray QGifHandler::buffer
mutableprivate

Definition at line 84 of file qgifhandler_p.h.

◆ fileName

QString QGifHandler::fileName
private

Definition at line 83 of file qgifhandler_p.h.

◆ frameNumber

int QGifHandler::frameNumber
private

Definition at line 89 of file qgifhandler_p.h.

◆ gifFormat

QGIFFormat* QGifHandler::gifFormat
private

Definition at line 82 of file qgifhandler_p.h.

◆ imageSizes

QVector<QSize> QGifHandler::imageSizes
mutableprivate

Definition at line 90 of file qgifhandler_p.h.

◆ lastImage

QImage QGifHandler::lastImage
mutableprivate

Definition at line 85 of file qgifhandler_p.h.

◆ loopCnt

int QGifHandler::loopCnt
mutableprivate

Definition at line 88 of file qgifhandler_p.h.

◆ nextDelay

int QGifHandler::nextDelay
mutableprivate

Definition at line 87 of file qgifhandler_p.h.

◆ scanIsCached

bool QGifHandler::scanIsCached
mutableprivate

Definition at line 91 of file qgifhandler_p.h.


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