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

#include <qbmphandler_p.h>

Inheritance diagram for QBmpHandler:
QImageIOHandler

Public Functions

bool canRead () const
 Returns true if an image can be read from the device (i. More...
 
QByteArray name () const
 Use format() instead. More...
 
QVariant option (ImageOption option) const
 Returns the value assigned to option as a QVariant. More...
 
 QBmpHandler ()
 
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...
 
- Public Functions inherited from QImageIOHandler
virtual int currentImageNumber () const
 For image formats that support animation, this function returns the sequence number of the current image in the animation. More...
 
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 int imageCount () const
 For image formats that support animation, this function returns the number of images in the animation. 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...
 
virtual int loopCount () const
 For image formats that support animation, this function returns the number of times the animation should loop. More...
 
virtual int nextImageDelay () const
 For image formats that support animation, this function returns the number of milliseconds to wait until reading 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 Types

enum  State { Ready, ReadHeader, Error }
 

Private Functions

bool readHeader ()
 

Properties

BMP_FILEHDR fileHeader
 
BMP_INFOHDR infoHeader
 
int startpos
 
State state
 

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 84 of file qbmphandler_p.h.

Enumerations

◆ State

enum QBmpHandler::State
private
Enumerator
Ready 
ReadHeader 
Error 

Definition at line 102 of file qbmphandler_p.h.

Constructors and Destructors

◆ QBmpHandler()

QBmpHandler::QBmpHandler ( )

Definition at line 693 of file qbmphandler.cpp.

694  : state(Ready)
695 {
696 }

Functions

◆ canRead() [1/2]

bool QBmpHandler::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 721 of file qbmphandler.cpp.

Referenced by createReadHandlerHelper().

722 {
723  if (state == Ready && !canRead(device()))
724  return false;
725 
726  if (state != Error) {
727  setFormat("bmp");
728  return true;
729  }
730 
731  return false;
732 }
void setFormat(const QByteArray &format)
Sets the format of the QImageIOHandler to format.
bool canRead() const
Returns true if an image can be read from the device (i.
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.

◆ canRead() [2/2]

bool QBmpHandler::canRead ( QIODevice device)
static

Definition at line 734 of file qbmphandler.cpp.

735 {
736  if (!device) {
737  qWarning("QBmpHandler::canRead() called with 0 pointer");
738  return false;
739  }
740 
741  char head[2];
742  if (device->peek(head, sizeof(head)) != sizeof(head))
743  return false;
744 
745  return (qstrncmp(head, "BM", 2) == 0);
746 }
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

◆ name()

QByteArray QBmpHandler::name ( ) const
virtual

Use format() instead.

Reimplemented from QImageIOHandler.

Definition at line 874 of file qbmphandler.cpp.

875 {
876  return "bmp";
877 }

◆ option()

QVariant QBmpHandler::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 836 of file qbmphandler.cpp.

837 {
838  if (option == Size) {
839  if (state == Error)
840  return QVariant();
841  if (state == Ready && !const_cast<QBmpHandler*>(this)->readHeader())
842  return QVariant();
844  } else if (option == ImageFormat) {
845  if (state == Error)
846  return QVariant();
847  if (state == Ready && !const_cast<QBmpHandler*>(this)->readHeader())
848  return QVariant();
850  switch (infoHeader.biBitCount) {
851  case 32:
852  case 24:
853  case 16:
854  format = QImage::Format_RGB32;
855  break;
856  case 8:
857  case 4:
858  format = QImage::Format_Indexed8;
859  break;
860  default:
861  format = QImage::Format_Mono;
862  }
863  return format;
864  }
865  return QVariant();
866 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
Format
The following image formats are available in Qt.
Definition: qimage.h:91
qint32 biWidth
Definition: qbmphandler_p.h:72
QByteArray format() const
Returns the format that is currently assigned to QImageIOHandler.
qint16 biBitCount
Definition: qbmphandler_p.h:75
BMP_INFOHDR infoHeader
QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
bool readHeader()
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
qint32 biHeight
Definition: qbmphandler_p.h:73

◆ read()

bool QBmpHandler::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 748 of file qbmphandler.cpp.

749 {
750  if (state == Error)
751  return false;
752 
753  if (!image) {
754  qWarning("QBmpHandler::read: cannot read into null pointer");
755  return false;
756  }
757 
758  if (state == Ready && !readHeader()) {
759  state = Error;
760  return false;
761  }
762 
763  QIODevice *d = device();
764  QDataStream s(d);
765 
766  // Intel byte order
767  s.setByteOrder(QDataStream::LittleEndian);
768 
769  // read image
771  return false;
772 
773  state = Ready;
774  return true;
775 }
double d
Definition: qnumeric_p.h:62
qint32 bfOffBits
Definition: qbmphandler_p.h:67
static bool read_dib_body(QDataStream &s, const BMP_INFOHDR &bi, int offset, int startpos, QImage &image)
Q_CORE_EXPORT void qWarning(const char *,...)
BMP_INFOHDR infoHeader
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
bool readHeader()
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
BMP_FILEHDR fileHeader
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66

◆ readHeader()

bool QBmpHandler::readHeader ( )
private

Definition at line 698 of file qbmphandler.cpp.

Referenced by option(), and read().

699 {
700  state = Error;
701 
702  QIODevice *d = device();
703  QDataStream s(d);
704  startpos = d->pos();
705 
706  // Intel byte order
707  s.setByteOrder(QDataStream::LittleEndian);
708 
709  // read BMP file header
711  return false;
712 
713  // read BMP info header
715  return false;
716 
717  state = ReadHeader;
718  return true;
719 }
double d
Definition: qnumeric_p.h:62
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from...
Definition: qiodevice.cpp:624
static bool read_dib_fileheader(QDataStream &s, BMP_FILEHDR &bf)
BMP_INFOHDR infoHeader
static bool read_dib_infoheader(QDataStream &s, BMP_INFOHDR &bi)
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
BMP_FILEHDR fileHeader
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66

◆ setOption()

void QBmpHandler::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 868 of file qbmphandler.cpp.

869 {
870  Q_UNUSED(option);
871  Q_UNUSED(value);
872 }
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 QBmpHandler::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 830 of file qbmphandler.cpp.

831 {
832  return option == Size
833  || option == ImageFormat;
834 }
QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.

◆ write()

bool QBmpHandler::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 777 of file qbmphandler.cpp.

778 {
779  QImage image;
780  switch (img.format()) {
786  break;
793  break;
794  default:
795  image = img;
796  }
797 
798  QIODevice *d = device();
799  QDataStream s(d);
800  BMP_FILEHDR bf;
801  int bpl_bmp;
802  int bpl = image.bytesPerLine();
803 
804  // Code partially repeated in qt_write_dib
805  if (image.depth() == 8 && image.colorCount() <= 16) {
806  bpl_bmp = (((bpl+1)/2+3)/4)*4;
807  } else if (image.depth() == 32) {
808  bpl_bmp = ((image.width()*24+31)/32)*4;
809  } else {
810  bpl_bmp = bpl;
811  }
812 
813  // Intel byte order
814  s.setByteOrder(QDataStream::LittleEndian);
815 
816  // build file header
817  memcpy(bf.bfType, "BM", 2);
818 
819  // write file header
820  bf.bfReserved1 = 0;
821  bf.bfReserved2 = 0;
822  bf.bfOffBits = BMP_FILEHDR_SIZE + BMP_WIN + image.colorCount() * 4;
823  bf.bfSize = bf.bfOffBits + bpl_bmp*image.height();
824  s << bf;
825 
826  // write image
827  return qt_write_dib(s, image);
828 }
double d
Definition: qnumeric_p.h:62
qint32 bfOffBits
Definition: qbmphandler_p.h:67
const int BMP_WIN
char bfType[2]
Definition: qbmphandler_p.h:63
bool qt_write_dib(QDataStream &s, QImage image)
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
qint32 bfSize
Definition: qbmphandler_p.h:64
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
int depth() const
Returns the depth of the image.
Definition: qimage.cpp:1620
const int BMP_FILEHDR_SIZE
Definition: qbmphandler.cpp:82
qint16 bfReserved1
Definition: qbmphandler_p.h:65
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
Definition: qimage.cpp:3966
int colorCount() const
Returns the size of the color table for the image.
Definition: qimage.cpp:1656
qint16 bfReserved2
Definition: qbmphandler_p.h:66
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66

Properties

◆ fileHeader

BMP_FILEHDR QBmpHandler::fileHeader
private

Definition at line 108 of file qbmphandler_p.h.

Referenced by read(), and readHeader().

◆ infoHeader

BMP_INFOHDR QBmpHandler::infoHeader
private

Definition at line 109 of file qbmphandler_p.h.

Referenced by option(), read(), and readHeader().

◆ startpos

int QBmpHandler::startpos
private

Definition at line 110 of file qbmphandler_p.h.

Referenced by read(), and readHeader().

◆ state

State QBmpHandler::state
private

Definition at line 107 of file qbmphandler_p.h.

Referenced by canRead(), option(), read(), and readHeader().


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