Qt 4.8
Public Types | Public Functions | Properties | List of all members
QPNGImageWriter Class Reference

Public Types

enum  DisposalMethod { Unspecified, NoDisposal, RestoreBackground, RestoreImage }
 

Public Functions

QIODevicedevice ()
 
 QPNGImageWriter (QIODevice *)
 
void setDisposalMethod (DisposalMethod)
 
void setFrameDelay (int msecs)
 
void setGamma (float)
 
void setLooping (int loops=0)
 
bool writeImage (const QImage &img, int x, int y)
 
bool writeImage (const QImage &img, int quality, const QString &description, int x, int y)
 
bool writeImage (const QImage &img)
 
bool writeImage (const QImage &img, int quality, const QString &description)
 
 ~QPNGImageWriter ()
 

Properties

QIODevicedev
 
DisposalMethod disposal
 
int frames_written
 
float gamma
 
int looping
 
int ms_delay
 

Detailed Description

Definition at line 149 of file qpnghandler.cpp.

Enumerations

◆ DisposalMethod

Constructors and Destructors

◆ QPNGImageWriter()

QPNGImageWriter::QPNGImageWriter ( QIODevice iod)
explicit

Definition at line 613 of file qpnghandler.cpp.

613  :
614  dev(iod),
615  frames_written(0),
617  looping(-1),
618  ms_delay(-1),
619  gamma(0.0)
620 {
621 }
DisposalMethod disposal
QIODevice * dev

◆ ~QPNGImageWriter()

QPNGImageWriter::~QPNGImageWriter ( )

Definition at line 623 of file qpnghandler.cpp.

624 {
625 }

Functions

◆ device()

QIODevice* QPNGImageWriter::device ( )
inline

Definition at line 167 of file qpnghandler.cpp.

Referenced by qpiw_write_fn().

167 { return dev; }
QIODevice * dev

◆ setDisposalMethod()

void QPNGImageWriter::setDisposalMethod ( DisposalMethod  dm)

Definition at line 627 of file qpnghandler.cpp.

628 {
629  disposal = dm;
630 }
DisposalMethod disposal

◆ setFrameDelay()

void QPNGImageWriter::setFrameDelay ( int  msecs)

Definition at line 637 of file qpnghandler.cpp.

638 {
639  ms_delay = msecs;
640 }

◆ setGamma()

void QPNGImageWriter::setGamma ( float  g)

Definition at line 642 of file qpnghandler.cpp.

Referenced by write_png_image().

643 {
644  gamma = g;
645 }

◆ setLooping()

void QPNGImageWriter::setLooping ( int  loops = 0)

Definition at line 632 of file qpnghandler.cpp.

633 {
634  looping = loops;
635 }

◆ writeImage() [1/4]

bool QPNGImageWriter::writeImage ( const QImage img,
int  x,
int  y 
)

Definition at line 724 of file qpnghandler.cpp.

Referenced by write_png_image().

725 {
726  return writeImage(image, -1, QString(), off_x, off_y);
727 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool writeImage(const QImage &img, int x, int y)

◆ writeImage() [2/4]

bool Q_INTERNAL_WIN_NO_THROW QPNGImageWriter::writeImage ( const QImage img,
int  quality,
const QString description,
int  x,
int  y 
)

Definition at line 729 of file qpnghandler.cpp.

731 {
732 #ifdef QT_NO_IMAGE_TEXT
733  Q_UNUSED(description);
734 #endif
735 
736  QPoint offset = image.offset();
737  int off_x = off_x_in + offset.x();
738  int off_y = off_y_in + offset.y();
739 
740  png_structp png_ptr;
741  png_infop info_ptr;
742 
743  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0);
744  if (!png_ptr) {
745  return false;
746  }
747 
748  png_set_error_fn(png_ptr, 0, 0, qt_png_warning);
749 
750  info_ptr = png_create_info_struct(png_ptr);
751  if (!info_ptr) {
752  png_destroy_write_struct(&png_ptr, 0);
753  return false;
754  }
755 
756  if (setjmp(png_jmpbuf(png_ptr))) {
757  png_destroy_write_struct(&png_ptr, &info_ptr);
758  return false;
759  }
760 
761  int quality = quality_in;
762  if (quality >= 0) {
763  if (quality > 9) {
764  qWarning("PNG: Quality %d out of range", quality);
765  quality = 9;
766  }
767  png_set_compression_level(png_ptr, quality);
768  }
769 
770  png_set_write_fn(png_ptr, (void*)this, qpiw_write_fn, qpiw_flush_fn);
771 
772 
773  int color_type = 0;
774  if (image.colorCount())
775  color_type = PNG_COLOR_TYPE_PALETTE;
776  else if (image.hasAlphaChannel())
777  color_type = PNG_COLOR_TYPE_RGB_ALPHA;
778  else
779  color_type = PNG_COLOR_TYPE_RGB;
780 
781  png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(),
782  image.depth() == 1 ? 1 : 8, // per channel
783  color_type, 0, 0, 0); // sets #channels
784 
785  if (gamma != 0.0) {
786  png_set_gAMA(png_ptr, info_ptr, 1.0/gamma);
787  }
788 
789  png_color_8 sig_bit;
790  sig_bit.red = 8;
791  sig_bit.green = 8;
792  sig_bit.blue = 8;
793  sig_bit.alpha = image.hasAlphaChannel() ? 8 : 0;
794  png_set_sBIT(png_ptr, info_ptr, &sig_bit);
795 
796  if (image.format() == QImage::Format_MonoLSB)
797  png_set_packswap(png_ptr);
798 
799  if (image.colorCount()) {
800  // Paletted
801  int num_palette = qMin(256, image.colorCount());
802  png_color palette[256];
803  png_byte trans[256];
804  int num_trans = 0;
805  for (int i=0; i<num_palette; i++) {
806  QRgb rgba=image.color(i);
807  palette[i].red = qRed(rgba);
808  palette[i].green = qGreen(rgba);
809  palette[i].blue = qBlue(rgba);
810  trans[i] = qAlpha(rgba);
811  if (trans[i] < 255) {
812  num_trans = i+1;
813  }
814  }
815  png_set_PLTE(png_ptr, info_ptr, palette, num_palette);
816 
817  if (num_trans) {
818  png_set_tRNS(png_ptr, info_ptr, trans, num_trans, 0);
819  }
820  }
821 
822  // Swap ARGB to RGBA (normal PNG format) before saving on
823  // BigEndian machines
824  if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
825  png_set_swap_alpha(png_ptr);
826  }
827 
828  // Qt==ARGB==Big(ARGB)==Little(BGRA). But RGB888 is RGB regardless
829  if (QSysInfo::ByteOrder == QSysInfo::LittleEndian
830  && image.format() != QImage::Format_RGB888) {
831  png_set_bgr(png_ptr);
832  }
833 
834  if (off_x || off_y) {
835  png_set_oFFs(png_ptr, info_ptr, off_x, off_y, PNG_OFFSET_PIXEL);
836  }
837 
838  if (frames_written > 0)
839  png_set_sig_bytes(png_ptr, 8);
840 
841  if (image.dotsPerMeterX() > 0 || image.dotsPerMeterY() > 0) {
842  png_set_pHYs(png_ptr, info_ptr,
843  image.dotsPerMeterX(), image.dotsPerMeterY(),
844  PNG_RESOLUTION_METER);
845  }
846 
847 #ifndef QT_NO_IMAGE_TEXT
848  set_text(image, png_ptr, info_ptr, description);
849 #endif
850  png_write_info(png_ptr, info_ptr);
851 
852  if (image.depth() != 1)
853  png_set_packing(png_ptr);
854 
855  if (color_type == PNG_COLOR_TYPE_RGB && image.format() != QImage::Format_RGB888)
856  png_set_filler(png_ptr, 0,
857  QSysInfo::ByteOrder == QSysInfo::BigEndian ?
858  PNG_FILLER_BEFORE : PNG_FILLER_AFTER);
859 
860  if (looping >= 0 && frames_written == 0) {
861  uchar data[13] = "NETSCAPE2.0";
862  // 0123456789aBC
863  data[0xB] = looping%0x100;
864  data[0xC] = looping/0x100;
865  png_write_chunk(png_ptr, (png_byte*)"gIFx", data, 13);
866  }
867  if (ms_delay >= 0 || disposal!=Unspecified) {
868  uchar data[4];
869  data[0] = disposal;
870  data[1] = 0;
871  data[2] = (ms_delay/10)/0x100; // hundredths
872  data[3] = (ms_delay/10)%0x100;
873  png_write_chunk(png_ptr, (png_byte*)"gIFg", data, 4);
874  }
875 
876  int height = image.height();
877  int width = image.width();
878  switch (image.format()) {
879  case QImage::Format_Mono:
885  {
886  png_bytep* row_pointers = new png_bytep[height];
887  for (int y=0; y<height; y++)
888  row_pointers[y] = (png_bytep)image.constScanLine(y);
889  png_write_image(png_ptr, row_pointers);
890  delete [] row_pointers;
891  }
892  break;
893  default:
894  {
895  QImage::Format fmt = image.hasAlphaChannel() ? QImage::Format_ARGB32 : QImage::Format_RGB32;
896  QImage row;
897  png_bytep row_pointers[1];
898  for (int y=0; y<height; y++) {
899  row = image.copy(0, y, width, 1).convertToFormat(fmt);
900  row_pointers[0] = png_bytep(row.constScanLine(0));
901  png_write_rows(png_ptr, row_pointers, 1);
902  }
903  }
904  break;
905  }
906 
907  png_write_end(png_ptr, info_ptr);
908  frames_written++;
909 
910  png_destroy_write_struct(&png_ptr, &info_ptr);
911 
912  return true;
913 }
Format
The following image formats are available in Qt.
Definition: qimage.h:91
unsigned int QRgb
Definition: qrgb.h:53
QImage copy(const QRect &rect=QRect()) const
Returns a sub-area of the image as a new image.
Definition: qimage.cpp:1410
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
Q_GUI_EXPORT_INLINE int qAlpha(QRgb rgb)
Definition: qrgb.h:66
static void CALLBACK_CALL_TYPE qpiw_flush_fn(png_structp)
Q_GUI_EXPORT_INLINE int qRed(QRgb rgb)
Definition: qrgb.h:57
unsigned char uchar
Definition: qglobal.h:994
DisposalMethod disposal
static void CALLBACK_CALL_TYPE qpiw_write_fn(png_structp png_ptr, png_bytep data, png_size_t length)
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
static const char * data(const QByteArray &arr)
Q_GUI_EXPORT_INLINE int qBlue(QRgb rgb)
Definition: qrgb.h:63
const uchar * constScanLine(int) const
Returns a pointer to the pixel data at the scanline with index i.
Definition: qimage.cpp:1926
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
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
Q_GUI_EXPORT_INLINE int qGreen(QRgb rgb)
Definition: qrgb.h:60
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
#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
static void CALLBACK_CALL_TYPE qt_png_warning(png_structp, png_const_charp message)
static void set_text(const QImage &image, png_structp png_ptr, png_infop info_ptr, const QString &description)

◆ writeImage() [3/4]

bool QPNGImageWriter::writeImage ( const QImage img)
inline

Definition at line 162 of file qpnghandler.cpp.

163  { return writeImage(img, 0, 0); }
bool writeImage(const QImage &img, int x, int y)

◆ writeImage() [4/4]

bool QPNGImageWriter::writeImage ( const QImage img,
int  quality,
const QString description 
)
inline

Definition at line 164 of file qpnghandler.cpp.

165  { return writeImage(img, quality, description, 0, 0); }
bool writeImage(const QImage &img, int x, int y)

Properties

◆ dev

QIODevice* QPNGImageWriter::dev
private

Definition at line 170 of file qpnghandler.cpp.

◆ disposal

DisposalMethod QPNGImageWriter::disposal
private

Definition at line 172 of file qpnghandler.cpp.

Referenced by setDisposalMethod(), and writeImage().

◆ frames_written

int QPNGImageWriter::frames_written
private

Definition at line 171 of file qpnghandler.cpp.

Referenced by writeImage().

◆ gamma

float QPNGImageWriter::gamma
private

Definition at line 175 of file qpnghandler.cpp.

Referenced by setGamma(), and writeImage().

◆ looping

int QPNGImageWriter::looping
private

Definition at line 173 of file qpnghandler.cpp.

Referenced by setLooping(), and writeImage().

◆ ms_delay

int QPNGImageWriter::ms_delay
private

Definition at line 174 of file qpnghandler.cpp.

Referenced by setFrameDelay(), and writeImage().


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