src/gui/image/qimage.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
   478     image's text.
   478     image's text.
   479 
   479 
   480     \row
   480     \row
   481     \o Low-level information
   481     \o Low-level information
   482     \o
   482     \o
       
   483 
   483     The depth() function returns the depth of the image. The supported
   484     The depth() function returns the depth of the image. The supported
   484     depths are 1 (monochrome), 8 and 32 (for more information see the
   485     depths are 1 (monochrome), 8, 16, 24 and 32 bits. The
   485     \l {QImage#Image Formats}{Image Formats} section).
   486     bitPlaneCount() function tells how many of those bits that are
       
   487     used. For more information see the
       
   488     \l {QImage#Image Formats}{Image Formats} section.
   486 
   489 
   487     The format(), bytesPerLine(), and byteCount() functions provide
   490     The format(), bytesPerLine(), and byteCount() functions provide
   488     low-level information about the data stored in the image.
   491     low-level information about the data stored in the image.
   489 
   492 
   490     The cacheKey() function returns a number that uniquely
   493     The cacheKey() function returns a number that uniquely
   705                             packed with the most significant bit (MSB) first.
   708                             packed with the most significant bit (MSB) first.
   706     \value Format_MonoLSB   The image is stored using 1-bit per pixel. Bytes are
   709     \value Format_MonoLSB   The image is stored using 1-bit per pixel. Bytes are
   707                             packed with the less significant bit (LSB) first.
   710                             packed with the less significant bit (LSB) first.
   708 
   711 
   709     \value Format_Indexed8  The image is stored using 8-bit indexes
   712     \value Format_Indexed8  The image is stored using 8-bit indexes
   710                             into a colormap. 
   713                             into a colormap.
   711 
   714 
   712     \value Format_RGB32     The image is stored using a 32-bit RGB format (0xffRRGGBB).
   715     \value Format_RGB32     The image is stored using a 32-bit RGB format (0xffRRGGBB).
   713 
   716 
   714     \value Format_ARGB32    The image is stored using a 32-bit ARGB
   717     \value Format_ARGB32    The image is stored using a 32-bit ARGB
   715                             format (0xAARRGGBB).
   718                             format (0xAARRGGBB).
  1578 }
  1581 }
  1579 
  1582 
  1580 /*!
  1583 /*!
  1581     Returns the depth of the image.
  1584     Returns the depth of the image.
  1582 
  1585 
  1583     The image depth is the number of bits used to encode a single
  1586     The image depth is the number of bits used to store a single
  1584     pixel, also called bits per pixel (bpp).
  1587     pixel, also called bits per pixel (bpp).
  1585 
  1588 
  1586     The supported depths are 1, 8, 16, 24 and 32.
  1589     The supported depths are 1, 8, 16, 24 and 32.
  1587 
  1590 
  1588     \sa convertToFormat(), {QImage#Image Formats}{Image Formats},
  1591     \sa bitPlaneCount(), convertToFormat(), {QImage#Image Formats}{Image Formats},
  1589     {QImage#Image Information}{Image Information}
  1592     {QImage#Image Information}{Image Information}
  1590 
  1593 
  1591 */
  1594 */
  1592 int QImage::depth() const
  1595 int QImage::depth() const
  1593 {
  1596 {
  1833     directly, because the pixel format depends on the byte order on
  1836     directly, because the pixel format depends on the byte order on
  1834     the underlying platform. Use qRed(), qGreen(), qBlue(), and
  1837     the underlying platform. Use qRed(), qGreen(), qBlue(), and
  1835     qAlpha() to access the pixels.
  1838     qAlpha() to access the pixels.
  1836 
  1839 
  1837     \sa bytesPerLine(), bits(), {QImage#Pixel Manipulation}{Pixel
  1840     \sa bytesPerLine(), bits(), {QImage#Pixel Manipulation}{Pixel
  1838     Manipulation}
  1841     Manipulation}, constScanLine()
  1839 */
  1842 */
  1840 uchar *QImage::scanLine(int i)
  1843 uchar *QImage::scanLine(int i)
  1841 {
  1844 {
  1842     if (!d)
  1845     if (!d)
  1843         return 0;
  1846         return 0;
  1861 
  1864 
  1862     Q_ASSERT(i >= 0 && i < height());
  1865     Q_ASSERT(i >= 0 && i < height());
  1863     return d->data + i * d->bytes_per_line;
  1866     return d->data + i * d->bytes_per_line;
  1864 }
  1867 }
  1865 
  1868 
       
  1869 
       
  1870 /*!
       
  1871     Returns a pointer to the pixel data at the scanline with index \a
       
  1872     i. The first scanline is at index 0.
       
  1873 
       
  1874     The scanline data is aligned on a 32-bit boundary.
       
  1875 
       
  1876     Note that QImage uses \l{Implicit Data Sharing} {implicit data
       
  1877     sharing}, but this function does \e not perform a deep copy of the
       
  1878     shared pixel data, because the returned data is const.
       
  1879 
       
  1880     \sa scanLine(), constBits()
       
  1881     \since 4.7
       
  1882 */
       
  1883 const uchar *QImage::constScanLine(int i) const
       
  1884 {
       
  1885     if (!d)
       
  1886         return 0;
       
  1887 
       
  1888     Q_ASSERT(i >= 0 && i < height());
       
  1889     return d->data + i * d->bytes_per_line;
       
  1890 }
  1866 
  1891 
  1867 /*!
  1892 /*!
  1868     Returns a pointer to the first pixel data. This is equivalent to
  1893     Returns a pointer to the first pixel data. This is equivalent to
  1869     scanLine(0).
  1894     scanLine(0).
  1870 
  1895 
  1871     Note that QImage uses \l{Implicit Data Sharing} {implicit data
  1896     Note that QImage uses \l{Implicit Data Sharing} {implicit data
  1872     sharing}. This function performs a deep copy of the shared pixel
  1897     sharing}. This function performs a deep copy of the shared pixel
  1873     data, thus ensuring that this QImage is the only one using the
  1898     data, thus ensuring that this QImage is the only one using the
  1874     current return value.
  1899     current return value.
  1875 
  1900 
  1876     \sa scanLine(), byteCount()
  1901     \sa scanLine(), byteCount(), constBits()
  1877 */
  1902 */
  1878 uchar *QImage::bits()
  1903 uchar *QImage::bits()
  1879 {
  1904 {
  1880     if (!d)
  1905     if (!d)
  1881         return 0;
  1906         return 0;
  1899 {
  1924 {
  1900     return d ? d->data : 0;
  1925     return d ? d->data : 0;
  1901 }
  1926 }
  1902 
  1927 
  1903 
  1928 
       
  1929 /*!
       
  1930     Returns a pointer to the first pixel data.
       
  1931 
       
  1932     Note that QImage uses \l{Implicit Data Sharing} {implicit data
       
  1933     sharing}, but this function does \e not perform a deep copy of the
       
  1934     shared pixel data, because the returned data is const.
       
  1935 
       
  1936     \sa bits(), constScanLine()
       
  1937     \since 4.7
       
  1938 */
       
  1939 const uchar *QImage::constBits() const
       
  1940 {
       
  1941     return d ? d->data : 0;
       
  1942 }
  1904 
  1943 
  1905 /*!
  1944 /*!
  1906     \fn void QImage::reset()
  1945     \fn void QImage::reset()
  1907 
  1946 
  1908     Resets all image parameters and deallocates the image data.
  1947     Resets all image parameters and deallocates the image data.
  2947     QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
  2986     QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
  2948     if (colorTable.size() == 0) {
  2987     if (colorTable.size() == 0) {
  2949         colorTable.resize(256);
  2988         colorTable.resize(256);
  2950         for (int i=0; i<256; ++i)
  2989         for (int i=0; i<256; ++i)
  2951             colorTable[i] = qRgb(i, i, i);
  2990             colorTable[i] = qRgb(i, i, i);
  2952 
       
  2953     }
  2991     }
  2954 
  2992 
  2955     int w = src->width;
  2993     int w = src->width;
  2956     const uchar *src_data = src->data;
  2994     const uchar *src_data = src->data;
  2957     uchar *dest_data = dest->data;
  2995     uchar *dest_data = dest->data;
       
  2996     int tableSize = colorTable.size() - 1;
  2958     for (int y = 0; y < src->height; y++) {
  2997     for (int y = 0; y < src->height; y++) {
  2959         uint *p = (uint *)dest_data;
  2998         uint *p = (uint *)dest_data;
  2960         const uchar *b = src_data;
  2999         const uchar *b = src_data;
  2961         uint *end = p + w;
  3000         uint *end = p + w;
  2962 
  3001 
  2963         while (p < end)
  3002         while (p < end)
  2964             *p++ = colorTable.at(*b++);
  3003             *p++ = colorTable.at(qMin<int>(tableSize, *b++));
  2965 
  3004 
  2966         src_data += src->bytes_per_line;
  3005         src_data += src->bytes_per_line;
  2967         dest_data += dest->bytes_per_line;
  3006         dest_data += dest->bytes_per_line;
  2968     }
  3007     }
  2969 }
  3008 }
  5663         qWarning("QImage::setAlphaChannel: "
  5702         qWarning("QImage::setAlphaChannel: "
  5664                  "Unable to set alpha channel while image is being painted on");
  5703                  "Unable to set alpha channel while image is being painted on");
  5665         return;
  5704         return;
  5666     }
  5705     }
  5667 
  5706 
  5668     detach();
  5707     if (d->format == QImage::Format_ARGB32_Premultiplied)
  5669 
  5708         detach();
  5670     QImage converted = convertToFormat(QImage::Format_ARGB32_Premultiplied);
       
  5671     if (!converted.isNull())
       
  5672         *this = converted;
       
  5673     else
  5709     else
       
  5710         *this = convertToFormat(QImage::Format_ARGB32_Premultiplied);
       
  5711 
       
  5712     if (isNull())
  5674         return;
  5713         return;
  5675 
  5714 
  5676     // Slight optimization since alphachannels are returned as 8-bit grays.
  5715     // Slight optimization since alphachannels are returned as 8-bit grays.
  5677     if (alphaChannel.d->depth == 8 && alphaChannel.isGrayscale()) {
  5716     if (alphaChannel.d->depth == 8 && alphaChannel.isGrayscale()) {
  5678         const uchar *src_data = alphaChannel.d->data;
  5717         const uchar *src_data = alphaChannel.d->data;
  5811                  || d->format == Format_ARGB6666_Premultiplied
  5850                  || d->format == Format_ARGB6666_Premultiplied
  5812                  || d->format == Format_ARGB4444_Premultiplied
  5851                  || d->format == Format_ARGB4444_Premultiplied
  5813                  || (d->has_alpha_clut && (d->format == Format_Indexed8
  5852                  || (d->has_alpha_clut && (d->format == Format_Indexed8
  5814                                            || d->format == Format_Mono
  5853                                            || d->format == Format_Mono
  5815                                            || d->format == Format_MonoLSB)));
  5854                                            || d->format == Format_MonoLSB)));
       
  5855 }
       
  5856 
       
  5857 
       
  5858 /*!
       
  5859     \since 4.7
       
  5860     Returns the number of bit planes in the image.
       
  5861 
       
  5862     The number of bit planes is the number of bits of color and
       
  5863     transparency information for each pixel. This is different from
       
  5864     (i.e. smaller than) the depth when the image format contains
       
  5865     unused bits.
       
  5866 
       
  5867     \sa depth(), format(), {QImage#Image Formats}{Image Formats}
       
  5868 */
       
  5869 int QImage::bitPlaneCount() const
       
  5870 {
       
  5871     if (!d)
       
  5872         return 0;
       
  5873     int bpc = 0;
       
  5874     switch (d->format) {
       
  5875     case QImage::Format_Invalid:
       
  5876         break;
       
  5877     case QImage::Format_RGB32:
       
  5878         bpc = 24;
       
  5879         break;
       
  5880     case QImage::Format_RGB666:
       
  5881         bpc = 18;
       
  5882         break;
       
  5883     case QImage::Format_RGB555:
       
  5884         bpc = 15;
       
  5885         break;
       
  5886     case QImage::Format_ARGB8555_Premultiplied:
       
  5887         bpc = 23;
       
  5888         break;
       
  5889     case QImage::Format_RGB444:
       
  5890         bpc = 12;
       
  5891         break;
       
  5892     default:
       
  5893         bpc = depthForFormat(d->format);
       
  5894         break;
       
  5895     }
       
  5896     return bpc;
  5816 }
  5897 }
  5817 
  5898 
  5818 
  5899 
  5819 #ifdef QT3_SUPPORT
  5900 #ifdef QT3_SUPPORT
  5820 #if defined(Q_WS_X11)
  5901 #if defined(Q_WS_X11)