src/gui/image/qimage.cpp
changeset 30 5dc02b23752f
parent 29 b72c6db6890b
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
   270 {
   270 {
   271     bool has_alpha_pixels = false;
   271     bool has_alpha_pixels = false;
   272 
   272 
   273     switch (format) {
   273     switch (format) {
   274 
   274 
   275     case QImage::Format_Mono:
       
   276     case QImage::Format_MonoLSB:
       
   277     case QImage::Format_Indexed8:
   275     case QImage::Format_Indexed8:
   278         has_alpha_pixels = has_alpha_clut;
   276         has_alpha_pixels = has_alpha_clut;
   279         break;
   277         break;
   280 
   278 
   281     case QImage::Format_ARGB32:
   279     case QImage::Format_ARGB32:
   480     image's text.
   478     image's text.
   481 
   479 
   482     \row
   480     \row
   483     \o Low-level information
   481     \o Low-level information
   484     \o
   482     \o
       
   483 
   485     The depth() function returns the depth of the image. The supported
   484     The depth() function returns the depth of the image. The supported
   486     depths are 1 (monochrome), 8 and 32 (for more information see the
   485     depths are 1 (monochrome), 8, 16, 24 and 32 bits. The
   487     \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.
   488 
   489 
   489     The format(), bytesPerLine(), and byteCount() functions provide
   490     The format(), bytesPerLine(), and byteCount() functions provide
   490     low-level information about the data stored in the image.
   491     low-level information about the data stored in the image.
   491 
   492 
   492     The cacheKey() function returns a number that uniquely
   493     The cacheKey() function returns a number that uniquely
   707                             packed with the most significant bit (MSB) first.
   708                             packed with the most significant bit (MSB) first.
   708     \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
   709                             packed with the less significant bit (LSB) first.
   710                             packed with the less significant bit (LSB) first.
   710 
   711 
   711     \value Format_Indexed8  The image is stored using 8-bit indexes
   712     \value Format_Indexed8  The image is stored using 8-bit indexes
   712                             into a colormap. 
   713                             into a colormap.
   713 
   714 
   714     \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).
   715 
   716 
   716     \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
   717                             format (0xAARRGGBB).
   718                             format (0xAARRGGBB).
  1580 }
  1581 }
  1581 
  1582 
  1582 /*!
  1583 /*!
  1583     Returns the depth of the image.
  1584     Returns the depth of the image.
  1584 
  1585 
  1585     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
  1586     pixel, also called bits per pixel (bpp).
  1587     pixel, also called bits per pixel (bpp).
  1587 
  1588 
  1588     The supported depths are 1, 8, 16, 24 and 32.
  1589     The supported depths are 1, 8, 16, 24 and 32.
  1589 
  1590 
  1590     \sa convertToFormat(), {QImage#Image Formats}{Image Formats},
  1591     \sa bitPlaneCount(), convertToFormat(), {QImage#Image Formats}{Image Formats},
  1591     {QImage#Image Information}{Image Information}
  1592     {QImage#Image Information}{Image Information}
  1592 
  1593 
  1593 */
  1594 */
  1594 int QImage::depth() const
  1595 int QImage::depth() const
  1595 {
  1596 {
  1835     directly, because the pixel format depends on the byte order on
  1836     directly, because the pixel format depends on the byte order on
  1836     the underlying platform. Use qRed(), qGreen(), qBlue(), and
  1837     the underlying platform. Use qRed(), qGreen(), qBlue(), and
  1837     qAlpha() to access the pixels.
  1838     qAlpha() to access the pixels.
  1838 
  1839 
  1839     \sa bytesPerLine(), bits(), {QImage#Pixel Manipulation}{Pixel
  1840     \sa bytesPerLine(), bits(), {QImage#Pixel Manipulation}{Pixel
  1840     Manipulation}
  1841     Manipulation}, constScanLine()
  1841 */
  1842 */
  1842 uchar *QImage::scanLine(int i)
  1843 uchar *QImage::scanLine(int i)
  1843 {
  1844 {
  1844     if (!d)
  1845     if (!d)
  1845         return 0;
  1846         return 0;
  1863 
  1864 
  1864     Q_ASSERT(i >= 0 && i < height());
  1865     Q_ASSERT(i >= 0 && i < height());
  1865     return d->data + i * d->bytes_per_line;
  1866     return d->data + i * d->bytes_per_line;
  1866 }
  1867 }
  1867 
  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 }
  1868 
  1891 
  1869 /*!
  1892 /*!
  1870     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
  1871     scanLine(0).
  1894     scanLine(0).
  1872 
  1895 
  1873     Note that QImage uses \l{Implicit Data Sharing} {implicit data
  1896     Note that QImage uses \l{Implicit Data Sharing} {implicit data
  1874     sharing}. This function performs a deep copy of the shared pixel
  1897     sharing}. This function performs a deep copy of the shared pixel
  1875     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
  1876     current return value.
  1899     current return value.
  1877 
  1900 
  1878     \sa scanLine(), byteCount()
  1901     \sa scanLine(), byteCount(), constBits()
  1879 */
  1902 */
  1880 uchar *QImage::bits()
  1903 uchar *QImage::bits()
  1881 {
  1904 {
  1882     if (!d)
  1905     if (!d)
  1883         return 0;
  1906         return 0;
  1901 {
  1924 {
  1902     return d ? d->data : 0;
  1925     return d ? d->data : 0;
  1903 }
  1926 }
  1904 
  1927 
  1905 
  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 }
  1906 
  1943 
  1907 /*!
  1944 /*!
  1908     \fn void QImage::reset()
  1945     \fn void QImage::reset()
  1909 
  1946 
  1910     Resets all image parameters and deallocates the image data.
  1947     Resets all image parameters and deallocates the image data.
  2949     QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
  2986     QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
  2950     if (colorTable.size() == 0) {
  2987     if (colorTable.size() == 0) {
  2951         colorTable.resize(256);
  2988         colorTable.resize(256);
  2952         for (int i=0; i<256; ++i)
  2989         for (int i=0; i<256; ++i)
  2953             colorTable[i] = qRgb(i, i, i);
  2990             colorTable[i] = qRgb(i, i, i);
  2954 
       
  2955     }
  2991     }
  2956 
  2992 
  2957     int w = src->width;
  2993     int w = src->width;
  2958     const uchar *src_data = src->data;
  2994     const uchar *src_data = src->data;
  2959     uchar *dest_data = dest->data;
  2995     uchar *dest_data = dest->data;
       
  2996     int tableSize = colorTable.size() - 1;
  2960     for (int y = 0; y < src->height; y++) {
  2997     for (int y = 0; y < src->height; y++) {
  2961         uint *p = (uint *)dest_data;
  2998         uint *p = (uint *)dest_data;
  2962         const uchar *b = src_data;
  2999         const uchar *b = src_data;
  2963         uint *end = p + w;
  3000         uint *end = p + w;
  2964 
  3001 
  2965         while (p < end)
  3002         while (p < end)
  2966             *p++ = colorTable.at(*b++);
  3003             *p++ = colorTable.at(qMin<int>(tableSize, *b++));
  2967 
  3004 
  2968         src_data += src->bytes_per_line;
  3005         src_data += src->bytes_per_line;
  2969         dest_data += dest->bytes_per_line;
  3006         dest_data += dest->bytes_per_line;
  2970     }
  3007     }
  2971 }
  3008 }
  5665         qWarning("QImage::setAlphaChannel: "
  5702         qWarning("QImage::setAlphaChannel: "
  5666                  "Unable to set alpha channel while image is being painted on");
  5703                  "Unable to set alpha channel while image is being painted on");
  5667         return;
  5704         return;
  5668     }
  5705     }
  5669 
  5706 
  5670     detach();
  5707     if (d->format == QImage::Format_ARGB32_Premultiplied)
  5671 
  5708         detach();
  5672     QImage converted = convertToFormat(QImage::Format_ARGB32_Premultiplied);
       
  5673     if (!converted.isNull())
       
  5674         *this = converted;
       
  5675     else
  5709     else
       
  5710         *this = convertToFormat(QImage::Format_ARGB32_Premultiplied);
       
  5711 
       
  5712     if (isNull())
  5676         return;
  5713         return;
  5677 
  5714 
  5678     // Slight optimization since alphachannels are returned as 8-bit grays.
  5715     // Slight optimization since alphachannels are returned as 8-bit grays.
  5679     if (alphaChannel.d->depth == 8 && alphaChannel.isGrayscale()) {
  5716     if (alphaChannel.d->depth == 8 && alphaChannel.isGrayscale()) {
  5680         const uchar *src_data = alphaChannel.d->data;
  5717         const uchar *src_data = alphaChannel.d->data;
  5813                  || d->format == Format_ARGB6666_Premultiplied
  5850                  || d->format == Format_ARGB6666_Premultiplied
  5814                  || d->format == Format_ARGB4444_Premultiplied
  5851                  || d->format == Format_ARGB4444_Premultiplied
  5815                  || (d->has_alpha_clut && (d->format == Format_Indexed8
  5852                  || (d->has_alpha_clut && (d->format == Format_Indexed8
  5816                                            || d->format == Format_Mono
  5853                                            || d->format == Format_Mono
  5817                                            || 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;
  5818 }
  5897 }
  5819 
  5898 
  5820 
  5899 
  5821 #ifdef QT3_SUPPORT
  5900 #ifdef QT3_SUPPORT
  5822 #if defined(Q_WS_X11)
  5901 #if defined(Q_WS_X11)