Qt 4.8
Public Types | Public Functions | Public Variables | List of all members
QPngHandlerPrivate Class Reference

Public Types

enum  State { Ready, ReadHeader, ReadingEnd, Error }
 

Public Functions

 QPngHandlerPrivate (QPngHandler *qq)
 
QImage::Format readImageFormat ()
 
bool readPngHeader ()
 
bool readPngImage (QImage *image)
 
void readPngTexts (png_info *info)
 

Public Variables

QString description
 
png_info * end_info
 
float gamma
 
png_info * info_ptr
 
png_struct * png_ptr
 
QPngHandlerq
 
int quality
 
QStringList readTexts
 
png_byte ** row_pointers
 
State state
 

Detailed Description

Definition at line 108 of file qpnghandler.cpp.

Enumerations

◆ State

Enumerator
Ready 
ReadHeader 
ReadingEnd 
Error 

Definition at line 111 of file qpnghandler.cpp.

Constructors and Destructors

◆ QPngHandlerPrivate()

QPngHandlerPrivate::QPngHandlerPrivate ( QPngHandler qq)
inline

Definition at line 118 of file qpnghandler.cpp.

119  : gamma(0.0), quality(2), png_ptr(0), info_ptr(0),
120  end_info(0), row_pointers(0), state(Ready), q(qq)
121  { }
QPngHandler * q
png_struct * png_ptr
png_byte ** row_pointers

Functions

◆ readImageFormat()

QImage::Format QPngHandlerPrivate::readImageFormat ( )

Definition at line 569 of file qpnghandler.cpp.

Referenced by QPngHandler::option().

570 {
572  png_uint_32 width, height;
573  int bit_depth, color_type;
574  png_colorp palette;
575  int num_palette;
576  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
577  if (color_type == PNG_COLOR_TYPE_GRAY) {
578  // Black & White or 8-bit grayscale
579  if (bit_depth == 1 && png_get_channels(png_ptr, info_ptr) == 1) {
580  format = QImage::Format_Mono;
581  } else if (bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
582  format = QImage::Format_ARGB32;
583  } else {
584  format = QImage::Format_Indexed8;
585  }
586  } else if (color_type == PNG_COLOR_TYPE_PALETTE
587  && png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)
588  && num_palette <= 256)
589  {
590  // 1-bit and 8-bit color
591  if (bit_depth != 1)
592  png_set_packing(png_ptr);
593  png_read_update_info(png_ptr, info_ptr);
594  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
595  format = bit_depth == 1 ? QImage::Format_Mono : QImage::Format_Indexed8;
596  } else {
597  // 32-bit
598  if (bit_depth == 16)
599  png_set_strip_16(png_ptr);
600 
601  format = QImage::Format_ARGB32;
602  // Only add filler if no alpha, or we can get 5 channel data.
603  if (!(color_type & PNG_COLOR_MASK_ALPHA)
604  && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
605  // We want 4 bytes, but it isn't an alpha channel
606  format = QImage::Format_RGB32;
607  }
608  }
609 
610  return format;
611 }
Format
The following image formats are available in Qt.
Definition: qimage.h:91
png_struct * png_ptr

◆ readPngHeader()

bool Q_INTERNAL_WIN_NO_THROW QPngHandlerPrivate::readPngHeader ( )
Warning
This function is not part of the public interface.

Definition at line 423 of file qpnghandler.cpp.

Referenced by QPngHandler::option(), and readPngImage().

424 {
425  state = Error;
426  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
427  if (!png_ptr)
428  return false;
429 
430  png_set_error_fn(png_ptr, 0, 0, qt_png_warning);
431 
432  info_ptr = png_create_info_struct(png_ptr);
433  if (!info_ptr) {
434  png_destroy_read_struct(&png_ptr, 0, 0);
435  png_ptr = 0;
436  return false;
437  }
438 
439  end_info = png_create_info_struct(png_ptr);
440  if (!end_info) {
441  png_destroy_read_struct(&png_ptr, &info_ptr, 0);
442  png_ptr = 0;
443  return false;
444  }
445 
446  if (setjmp(png_jmpbuf(png_ptr))) {
447  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
448  png_ptr = 0;
449  return false;
450  }
451 
452  png_set_read_fn(png_ptr, this, iod_read_fn);
453  png_read_info(png_ptr, info_ptr);
454 
456 
457  state = ReadHeader;
458  return true;
459 }
void readPngTexts(png_info *info)
png_struct * png_ptr
static void CALLBACK_CALL_TYPE iod_read_fn(png_structp png_ptr, png_bytep data, png_size_t length)
static void CALLBACK_CALL_TYPE qt_png_warning(png_structp, png_const_charp message)

◆ readPngImage()

bool Q_INTERNAL_WIN_NO_THROW QPngHandlerPrivate::readPngImage ( QImage outImage)
Warning
This function is not part of the public interface.

Definition at line 464 of file qpnghandler.cpp.

Referenced by QPngHandler::read().

465 {
466  if (state == Error)
467  return false;
468 
469  if (state == Ready && !readPngHeader()) {
470  state = Error;
471  return false;
472  }
473 
474  row_pointers = 0;
475  if (setjmp(png_jmpbuf(png_ptr))) {
476  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
477  delete [] row_pointers;
478  png_ptr = 0;
479  state = Error;
480  return false;
481  }
482 
483  setup_qt(*outImage, png_ptr, info_ptr, gamma);
484 
485  if (outImage->isNull()) {
486  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
487  delete [] row_pointers;
488  png_ptr = 0;
489  state = Error;
490  return false;
491  }
492 
493  png_uint_32 width = 0;
494  png_uint_32 height = 0;
495  png_int_32 offset_x = 0;
496  png_int_32 offset_y = 0;
497  int bit_depth = 0;
498  int color_type = 0;
499  int unit_type = PNG_OFFSET_PIXEL;
500  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
501  0, 0, 0);
502  png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y, &unit_type);
503 
504  uchar *data = outImage->bits();
505  int bpl = outImage->bytesPerLine();
506  row_pointers = new png_bytep[height];
507 
508  for (uint y = 0; y < height; y++)
509  row_pointers[y] = data + y * bpl;
510 
511  png_read_image(png_ptr, row_pointers);
512 
513 #if 0 // libpng takes care of this.
514  png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)
515  if (outImage->depth()==32 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
516  QRgb trans = 0xFF000000 | qRgb(
517  (info_ptr->trans_values.red << 8 >> bit_depth)&0xff,
518  (info_ptr->trans_values.green << 8 >> bit_depth)&0xff,
519  (info_ptr->trans_values.blue << 8 >> bit_depth)&0xff);
520  for (uint y=0; y<height; y++) {
521  for (uint x=0; x<info_ptr->width; x++) {
522  if (((uint**)jt)[y][x] == trans) {
523  ((uint**)jt)[y][x] &= 0x00FFFFFF;
524  } else {
525  }
526  }
527  }
528  }
529 #endif
530 
531  outImage->setDotsPerMeterX(png_get_x_pixels_per_meter(png_ptr,info_ptr));
532  outImage->setDotsPerMeterY(png_get_y_pixels_per_meter(png_ptr,info_ptr));
533 
534  if (unit_type == PNG_OFFSET_PIXEL)
535  outImage->setOffset(QPoint(offset_x, offset_y));
536 
537  state = ReadingEnd;
538  png_read_end(png_ptr, end_info);
539 
540 #ifndef QT_NO_IMAGE_TEXT
542  for (int i = 0; i < readTexts.size()-1; i+=2)
543  outImage->setText(readTexts.at(i), readTexts.at(i+1));
544 #endif
545 
546  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
547  delete [] row_pointers;
548  png_ptr = 0;
549  state = Ready;
550 
551  // sanity check palette entries
552  if (color_type == PNG_COLOR_TYPE_PALETTE
553  && outImage->format() == QImage::Format_Indexed8) {
554  int color_table_size = outImage->colorCount();
555  for (int y=0; y<(int)height; ++y) {
556  uchar *p = FAST_SCAN_LINE(data, bpl, y);
557  uchar *end = p + width;
558  while (p < end) {
559  if (*p >= color_table_size)
560  *p = 0;
561  ++p;
562  }
563  }
564  }
565 
566  return true;
567 }
unsigned int QRgb
Definition: qrgb.h:53
void readPngTexts(png_info *info)
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition: qimage.cpp:1542
#define FAST_SCAN_LINE(data, bpl, y)
Definition: qpnghandler.cpp:99
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
Format format() const
Returns the format of the image.
Definition: qimage.cpp:2305
unsigned char uchar
Definition: qglobal.h:994
void setOffset(const QPoint &)
Sets the number of pixels by which the image is intended to be offset by when positioning relative to...
Definition: qimage.cpp:5705
void setText(const QString &key, const QString &value)
Sets the image text to the given text and associate it with the given key.
Definition: qimage.cpp:5780
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
void setDotsPerMeterY(int)
Sets the number of pixels that fit vertically in a physical meter, to y.
Definition: qimage.cpp:5667
int depth() const
Returns the depth of the image.
Definition: qimage.cpp:1620
uchar * bits()
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1946
png_struct * png_ptr
void setDotsPerMeterX(int)
Sets the number of pixels that fit horizontally in a physical meter, to x.
Definition: qimage.cpp:5645
int colorCount() const
Returns the size of the color table for the image.
Definition: qimage.cpp:1656
png_byte ** row_pointers
Q_GUI_EXPORT_INLINE QRgb qRgb(int r, int g, int b)
Definition: qrgb.h:69
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static void setup_qt(QImage &image, png_structp png_ptr, png_infop info_ptr, float screen_gamma=0.0)
static const KeyPair *const end
QStringList readTexts

◆ readPngTexts()

void Q_INTERNAL_WIN_NO_THROW QPngHandlerPrivate::readPngTexts ( png_info *  info)
Warning
This function is not part of the public interface.

Definition at line 391 of file qpnghandler.cpp.

Referenced by readPngHeader(), and readPngImage().

392 {
393 #ifndef QT_NO_IMAGE_TEXT
394  png_textp text_ptr;
395  int num_text=0;
396  png_get_text(png_ptr, info, &text_ptr, &num_text);
397 
398  while (num_text--) {
399  QString key, value;
400  key = QString::fromLatin1(text_ptr->key);
401 #if defined(PNG_iTXt_SUPPORTED)
402  if (text_ptr->itxt_length) {
403  value = QString::fromUtf8(text_ptr->text, int(text_ptr->itxt_length));
404  } else
405 #endif
406  {
407  value = QString::fromLatin1(text_ptr->text, int(text_ptr->text_length));
408  }
409  if (!description.isEmpty())
410  description += QLatin1String("\n\n");
411  description += key + QLatin1String(": ") + value.simplified();
412  readTexts.append(key);
413  readTexts.append(value);
414  text_ptr++;
415  }
416 #endif
417 }
static mach_timebase_info_data_t info
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
png_struct * png_ptr
QString simplified() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end, and that has each sequence o...
Definition: qstring.cpp:4415
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int key
QStringList readTexts

Properties

◆ description

QString QPngHandlerPrivate::description

◆ end_info

png_info* QPngHandlerPrivate::end_info

Definition at line 130 of file qpnghandler.cpp.

Referenced by readPngHeader(), readPngImage(), and QPngHandler::~QPngHandler().

◆ gamma

float QPngHandlerPrivate::gamma

◆ info_ptr

png_info* QPngHandlerPrivate::info_ptr

◆ png_ptr

png_struct* QPngHandlerPrivate::png_ptr

◆ q

QPngHandler* QPngHandlerPrivate::q

Definition at line 141 of file qpnghandler.cpp.

Referenced by iod_read_fn().

◆ quality

int QPngHandlerPrivate::quality

◆ readTexts

QStringList QPngHandlerPrivate::readTexts

Definition at line 126 of file qpnghandler.cpp.

Referenced by readPngImage(), and readPngTexts().

◆ row_pointers

png_byte** QPngHandlerPrivate::row_pointers

Definition at line 131 of file qpnghandler.cpp.

Referenced by readPngImage().

◆ state

State QPngHandlerPrivate::state

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