src/gui/image/qimage.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   447 
   447 
   448     The color of a pixel can be retrieved by passing its coordinates
   448     The color of a pixel can be retrieved by passing its coordinates
   449     to the pixel() function.  The pixel() function returns the color
   449     to the pixel() function.  The pixel() function returns the color
   450     as a QRgb value indepedent of the image's format.
   450     as a QRgb value indepedent of the image's format.
   451 
   451 
   452     In case of monochrome and 8-bit images, the numColors() and
   452     In case of monochrome and 8-bit images, the colorCount() and
   453     colorTable() functions provide information about the color
   453     colorTable() functions provide information about the color
   454     components used to store the image data: The colorTable() function
   454     components used to store the image data: The colorTable() function
   455     returns the image's entire color table. To obtain a single entry,
   455     returns the image's entire color table. To obtain a single entry,
   456     use the pixelIndex() function to retrieve the pixel index for a
   456     use the pixelIndex() function to retrieve the pixel index for a
   457     given pair of coordinates, then use the color() function to
   457     given pair of coordinates, then use the color() function to
   482     \o
   482     \o
   483     The depth() function returns the depth of the image. The supported
   483     The depth() function returns the depth of the image. The supported
   484     depths are 1 (monochrome), 8 and 32 (for more information see the
   484     depths are 1 (monochrome), 8 and 32 (for more information see the
   485     \l {QImage#Image Formats}{Image Formats} section).
   485     \l {QImage#Image Formats}{Image Formats} section).
   486 
   486 
   487     The format(), bytesPerLine(), and numBytes() functions provide
   487     The format(), bytesPerLine(), and byteCount() functions provide
   488     low-level information about the data stored in the image.
   488     low-level information about the data stored in the image.
   489 
   489 
   490     The cacheKey() function returns a number that uniquely
   490     The cacheKey() function returns a number that uniquely
   491     identifies the contents of this QImage object.
   491     identifies the contents of this QImage object.
   492     \endtable
   492     \endtable
   621     \row
   621     \row
   622     \o setColorTable()
   622     \o setColorTable()
   623     \o Sets the color table used to translate color indexes. Only
   623     \o Sets the color table used to translate color indexes. Only
   624     monochrome and 8-bit formats.
   624     monochrome and 8-bit formats.
   625     \row
   625     \row
   626     \o setNumColors()
   626     \o setColorCount()
   627     \o Resizes the color table. Only monochrome and 8-bit formats.
   627     \o Resizes the color table. Only monochrome and 8-bit formats.
   628 
   628 
   629     \endtable
   629     \endtable
   630 
   630 
   631     \section1 Legal Information
   631     \section1 Legal Information
   904     The buffer must remain valid throughout the life of the
   904     The buffer must remain valid throughout the life of the
   905     QImage. The image does not delete the buffer at destruction.
   905     QImage. The image does not delete the buffer at destruction.
   906 
   906 
   907     If \a format is an indexed color format, the image color table is
   907     If \a format is an indexed color format, the image color table is
   908     initially empty and must be sufficiently expanded with
   908     initially empty and must be sufficiently expanded with
   909     setNumColors() or setColorTable() before the image is used.
   909     setColorCount() or setColorTable() before the image is used.
   910 */
   910 */
   911 QImage::QImage(uchar* data, int width, int height, Format format)
   911 QImage::QImage(uchar* data, int width, int height, Format format)
   912     : QPaintDevice()
   912     : QPaintDevice()
   913 {
   913 {
   914     d = QImageData::create(data, width, height, 0, format, false);
   914     d = QImageData::create(data, width, height, 0, format, false);
   926     the original buffer. The image does not delete the buffer at
   926     the original buffer. The image does not delete the buffer at
   927     destruction.
   927     destruction.
   928 
   928 
   929     If \a format is an indexed color format, the image color table is
   929     If \a format is an indexed color format, the image color table is
   930     initially empty and must be sufficiently expanded with
   930     initially empty and must be sufficiently expanded with
   931     setNumColors() or setColorTable() before the image is used.
   931     setColorCount() or setColorTable() before the image is used.
   932 
   932 
   933     Unlike the similar QImage constructor that takes a non-const data buffer,
   933     Unlike the similar QImage constructor that takes a non-const data buffer,
   934     this version will never alter the contents of the buffer.  For example,
   934     this version will never alter the contents of the buffer.  For example,
   935     calling QImage::bits() will return a deep copy of the image, rather than
   935     calling QImage::bits() will return a deep copy of the image, rather than
   936     the buffer passed to the constructor.  This allows for the efficiency of
   936     the buffer passed to the constructor.  This allows for the efficiency of
   952     The buffer must remain valid throughout the life of the
   952     The buffer must remain valid throughout the life of the
   953     QImage. The image does not delete the buffer at destruction.
   953     QImage. The image does not delete the buffer at destruction.
   954 
   954 
   955     If \a format is an indexed color format, the image color table is
   955     If \a format is an indexed color format, the image color table is
   956     initially empty and must be sufficiently expanded with
   956     initially empty and must be sufficiently expanded with
   957     setNumColors() or setColorTable() before the image is used.
   957     setColorCount() or setColorTable() before the image is used.
   958 */
   958 */
   959 QImage::QImage(uchar *data, int width, int height, int bytesPerLine, Format format)
   959 QImage::QImage(uchar *data, int width, int height, int bytesPerLine, Format format)
   960     :QPaintDevice()
   960     :QPaintDevice()
   961 {
   961 {
   962     d = QImageData::create(data, width, height, bytesPerLine, format, false);
   962     d = QImageData::create(data, width, height, bytesPerLine, format, false);
   972     The buffer must remain valid throughout the life of the
   972     The buffer must remain valid throughout the life of the
   973     QImage. The image does not delete the buffer at destruction.
   973     QImage. The image does not delete the buffer at destruction.
   974 
   974 
   975     If \a format is an indexed color format, the image color table is
   975     If \a format is an indexed color format, the image color table is
   976     initially empty and must be sufficiently expanded with
   976     initially empty and must be sufficiently expanded with
   977     setNumColors() or setColorTable() before the image is used.
   977     setColorCount() or setColorTable() before the image is used.
   978 
   978 
   979     Unlike the similar QImage constructor that takes a non-const data buffer,
   979     Unlike the similar QImage constructor that takes a non-const data buffer,
   980     this version will never alter the contents of the buffer.  For example,
   980     this version will never alter the contents of the buffer.  For example,
   981     calling QImage::bits() will return a deep copy of the image, rather than
   981     calling QImage::bits() will return a deep copy of the image, rather than
   982     the buffer passed to the constructor.  This allows for the efficiency of
   982     the buffer passed to the constructor.  This allows for the efficiency of
  1124     Constructs an image with the given \a width, \a height, \a depth,
  1124     Constructs an image with the given \a width, \a height, \a depth,
  1125     \a numColors colors and \a bitOrder.
  1125     \a numColors colors and \a bitOrder.
  1126 
  1126 
  1127     Use the constructor that accepts a width, a height and a format
  1127     Use the constructor that accepts a width, a height and a format
  1128     (i.e. specifying the depth and bit order), in combination with the
  1128     (i.e. specifying the depth and bit order), in combination with the
  1129     setNumColors() function, instead.
  1129     setColorCount() function, instead.
  1130 
  1130 
  1131     \oldcode
  1131     \oldcode
  1132         QImage image(width, height, depth, numColors);
  1132         QImage image(width, height, depth, numColors);
  1133     \newcode
  1133     \newcode
  1134         QImage image(width, height, format);
  1134         QImage image(width, height, format);
  1135 
  1135 
  1136         // For 8 bit images the default number of colors is 256. If
  1136         // For 8 bit images the default number of colors is 256. If
  1137         // another number of colors is required it can be specified
  1137         // another number of colors is required it can be specified
  1138         // using the setNumColors() function.
  1138         // using the setColorCount() function.
  1139         image.setNumColors(numColors);
  1139         image.setColorCount(numColors);
  1140     \endcode
  1140     \endcode
  1141 */
  1141 */
  1142 
  1142 
  1143 QImage::QImage(int w, int h, int depth, int numColors, Endian bitOrder)
  1143 QImage::QImage(int w, int h, int depth, int colorCount, Endian bitOrder)
  1144     : QPaintDevice()
  1144     : QPaintDevice()
  1145 {
  1145 {
  1146     d = QImageData::create(QSize(w, h), formatFor(depth, bitOrder), numColors);
  1146     d = QImageData::create(QSize(w, h), formatFor(depth, bitOrder), colorCount);
  1147 }
  1147 }
  1148 
  1148 
  1149 /*!
  1149 /*!
  1150     Constructs an image with the given \a size, \a depth, \a numColors
  1150     Constructs an image with the given \a size, \a depth, \a numColors
  1151     and \a bitOrder.
  1151     and \a bitOrder.
  1152 
  1152 
  1153     Use the constructor that accepts a size and a format
  1153     Use the constructor that accepts a size and a format
  1154     (i.e. specifying the depth and bit order), in combination with the
  1154     (i.e. specifying the depth and bit order), in combination with the
  1155     setNumColors() function, instead.
  1155     setColorCount() function, instead.
  1156 
  1156 
  1157     \oldcode
  1157     \oldcode
  1158         QSize mySize(width, height);
  1158         QSize mySize(width, height);
  1159         QImage image(mySize, depth, numColors);
  1159         QImage image(mySize, depth, numColors);
  1160     \newcode
  1160     \newcode
  1161         QSize mySize(width, height);
  1161         QSize mySize(width, height);
  1162         QImage image(mySize, format);
  1162         QImage image(mySize, format);
  1163 
  1163 
  1164         // For 8 bit images the default number of colors is 256. If
  1164         // For 8 bit images the default number of colors is 256. If
  1165         // another number of colors is required it can be specified
  1165         // another number of colors is required it can be specified
  1166         // using the setNumColors() function.
  1166         // using the setColorCount() function.
  1167         image.setNumColors(numColors);
  1167         image.setColorCount(numColors);
  1168     \endcode
  1168     \endcode
  1169 */
  1169 */
  1170 QImage::QImage(const QSize& size, int depth, int numColors, Endian bitOrder)
  1170 QImage::QImage(const QSize& size, int depth, int numColors, Endian bitOrder)
  1171     : QPaintDevice()
  1171     : QPaintDevice()
  1172 {
  1172 {
  1230     if (colortable) {
  1230     if (colortable) {
  1231         d->colortable.resize(numColors);
  1231         d->colortable.resize(numColors);
  1232         for (int i = 0; i < numColors; ++i)
  1232         for (int i = 0; i < numColors; ++i)
  1233             d->colortable[i] = colortable[i];
  1233             d->colortable[i] = colortable[i];
  1234     } else if (numColors) {
  1234     } else if (numColors) {
  1235         setNumColors(numColors);
  1235         setColorCount(numColors);
  1236     }
  1236     }
  1237 }
  1237 }
  1238 
  1238 
  1239 #ifdef Q_WS_QWS
  1239 #ifdef Q_WS_QWS
  1240 
  1240 
  1281     if (colortable) {
  1281     if (colortable) {
  1282         d->colortable.resize(numColors);
  1282         d->colortable.resize(numColors);
  1283         for (int i = 0; i < numColors; ++i)
  1283         for (int i = 0; i < numColors; ++i)
  1284             d->colortable[i] = colortable[i];
  1284             d->colortable[i] = colortable[i];
  1285     } else if (numColors) {
  1285     } else if (numColors) {
  1286         setNumColors(numColors);
  1286         setColorCount(numColors);
  1287     }
  1287     }
  1288 }
  1288 }
  1289 #endif // Q_WS_QWS
  1289 #endif // Q_WS_QWS
  1290 #endif // QT3_SUPPORT
  1290 #endif // QT3_SUPPORT
  1291 
  1291 
  1590 {
  1590 {
  1591     return d ? d->depth : 0;
  1591     return d ? d->depth : 0;
  1592 }
  1592 }
  1593 
  1593 
  1594 /*!
  1594 /*!
       
  1595     \obsolete
  1595     \fn int QImage::numColors() const
  1596     \fn int QImage::numColors() const
  1596 
  1597 
  1597     Returns the size of the color table for the image.
  1598     Returns the size of the color table for the image.
  1598 
  1599 
  1599     Notice that numColors() returns 0 for 32-bpp images because these
  1600     \sa setColorCount()
       
  1601 */
       
  1602 int QImage::numColors() const
       
  1603 {
       
  1604     return d ? d->colortable.size() : 0;
       
  1605 }
       
  1606 
       
  1607 /*!
       
  1608     \since 4.6
       
  1609     \fn int QImage::colorCount() const
       
  1610 
       
  1611     Returns the size of the color table for the image.
       
  1612 
       
  1613     Notice that colorCount() returns 0 for 32-bpp images because these
  1600     images do not use color tables, but instead encode pixel values as
  1614     images do not use color tables, but instead encode pixel values as
  1601     ARGB quadruplets.
  1615     ARGB quadruplets.
  1602 
  1616 
  1603     \sa setNumColors(), {QImage#Image Information}{Image Information}
  1617     \sa setColorCount(), {QImage#Image Information}{Image Information}
  1604 */
  1618 */
  1605 int QImage::numColors() const
  1619 int QImage::colorCount() const
  1606 {
  1620 {
  1607     return d ? d->colortable.size() : 0;
  1621     return d ? d->colortable.size() : 0;
  1608 }
  1622 }
  1609 
  1623 
  1610 
  1624 
  1709 
  1723 
  1710 /*!
  1724 /*!
  1711     Returns a list of the colors contained in the image's color table,
  1725     Returns a list of the colors contained in the image's color table,
  1712     or an empty list if the image does not have a color table
  1726     or an empty list if the image does not have a color table
  1713 
  1727 
  1714     \sa setColorTable(), numColors(), color()
  1728     \sa setColorTable(), colorCount(), color()
  1715 */
  1729 */
  1716 QVector<QRgb> QImage::colorTable() const
  1730 QVector<QRgb> QImage::colorTable() const
  1717 {
  1731 {
  1718     return d ? d->colortable : QVector<QRgb>();
  1732     return d ? d->colortable : QVector<QRgb>();
  1719 }
  1733 }
  1720 
  1734 
  1721 
  1735 
  1722 /*!
  1736 /*!
       
  1737     \obsolete
       
  1738     Returns the number of bytes occupied by the image data.
       
  1739 
       
  1740     \sa byteCount()
       
  1741 */
       
  1742 int QImage::numBytes() const
       
  1743 {
       
  1744     return d ? d->nbytes : 0;
       
  1745 }
       
  1746 
       
  1747 /*!
       
  1748     \since 4.6
  1723     Returns the number of bytes occupied by the image data.
  1749     Returns the number of bytes occupied by the image data.
  1724 
  1750 
  1725     \sa bytesPerLine(), bits(), {QImage#Image Information}{Image
  1751     \sa bytesPerLine(), bits(), {QImage#Image Information}{Image
  1726     Information}
  1752     Information}
  1727 */
  1753 */
  1728 int QImage::numBytes() const
  1754 int QImage::byteCount() const
  1729 {
  1755 {
  1730     return d ? d->nbytes : 0;
  1756     return d ? d->nbytes : 0;
  1731 }
  1757 }
  1732 
  1758 
  1733 /*!
  1759 /*!
  1734     Returns the number of bytes per image scanline.
  1760     Returns the number of bytes per image scanline.
  1735 
  1761 
  1736     This is equivalent to numBytes()/ height().
  1762     This is equivalent to byteCount() / height().
  1737 
  1763 
  1738     \sa scanLine()
  1764     \sa scanLine()
  1739 */
  1765 */
  1740 int QImage::bytesPerLine() const
  1766 int QImage::bytesPerLine() const
  1741 {
  1767 {
  1754     \sa setColor(), pixelIndex(), {QImage#Pixel Manipulation}{Pixel
  1780     \sa setColor(), pixelIndex(), {QImage#Pixel Manipulation}{Pixel
  1755     Manipulation}
  1781     Manipulation}
  1756 */
  1782 */
  1757 QRgb QImage::color(int i) const
  1783 QRgb QImage::color(int i) const
  1758 {
  1784 {
  1759     Q_ASSERT(i < numColors());
  1785     Q_ASSERT(i < colorCount());
  1760     return d ? d->colortable.at(i) : QRgb(uint(-1));
  1786     return d ? d->colortable.at(i) : QRgb(uint(-1));
  1761 }
  1787 }
  1762 
  1788 
  1763 /*!
  1789 /*!
  1764     \fn void QImage::setColor(int index, QRgb colorValue)
  1790     \fn void QImage::setColor(int index, QRgb colorValue)
  1765 
  1791 
  1766     Sets the color at the given \a index in the color table, to the
  1792     Sets the color at the given \a index in the color table, to the
  1767     given to \a colorValue. The color value is an ARGB quadruplet.
  1793     given to \a colorValue. The color value is an ARGB quadruplet.
  1768 
  1794 
  1769     If \a index is outside the current size of the color table, it is
  1795     If \a index is outside the current size of the color table, it is
  1770     expanded with setNumColors().
  1796     expanded with setColorCount().
  1771 
  1797 
  1772     \sa color(), numColors(), setColorTable(), {QImage#Pixel Manipulation}{Pixel
  1798     \sa color(), colorCount(), setColorTable(), {QImage#Pixel Manipulation}{Pixel
  1773     Manipulation}
  1799     Manipulation}
  1774 */
  1800 */
  1775 void QImage::setColor(int i, QRgb c)
  1801 void QImage::setColor(int i, QRgb c)
  1776 {
  1802 {
  1777     if (!d)
  1803     if (!d)
  1785     // In case detach() run out of memory
  1811     // In case detach() run out of memory
  1786     if (!d)
  1812     if (!d)
  1787         return;
  1813         return;
  1788 
  1814 
  1789     if (i >= d->colortable.size())
  1815     if (i >= d->colortable.size())
  1790         setNumColors(i+1);
  1816         setColorCount(i+1);
  1791     d->colortable[i] = c;
  1817     d->colortable[i] = c;
  1792     d->has_alpha_clut |= (qAlpha(c) != 255);
  1818     d->has_alpha_clut |= (qAlpha(c) != 255);
  1793 }
  1819 }
  1794 
  1820 
  1795 /*!
  1821 /*!
  1842     Note that QImage uses \l{Implicit Data Sharing} {implicit data
  1868     Note that QImage uses \l{Implicit Data Sharing} {implicit data
  1843     sharing}. This function performs a deep copy of the shared pixel
  1869     sharing}. This function performs a deep copy of the shared pixel
  1844     data, thus ensuring that this QImage is the only one using the
  1870     data, thus ensuring that this QImage is the only one using the
  1845     current return value.
  1871     current return value.
  1846 
  1872 
  1847     \sa scanLine(), numBytes()
  1873     \sa scanLine(), byteCount()
  1848 */
  1874 */
  1849 uchar *QImage::bits()
  1875 uchar *QImage::bits()
  1850 {
  1876 {
  1851     if (!d)
  1877     if (!d)
  1852         return 0;
  1878         return 0;
  2023 #if defined(read)
  2049 #if defined(read)
  2024 # undef read
  2050 # undef read
  2025 #endif
  2051 #endif
  2026 
  2052 
  2027 /*!
  2053 /*!
       
  2054     \obsolete
  2028     Resizes the color table to contain \a numColors entries.
  2055     Resizes the color table to contain \a numColors entries.
       
  2056 
       
  2057     \sa setColorCount()
       
  2058 */
       
  2059 
       
  2060 void QImage::setNumColors(int numColors)
       
  2061 {
       
  2062     setColorCount(numColors);
       
  2063 }
       
  2064 
       
  2065 /*!
       
  2066     \since 4.6
       
  2067     Resizes the color table to contain \a colorCount entries.
  2029 
  2068 
  2030     If the color table is expanded, all the extra colors will be set to
  2069     If the color table is expanded, all the extra colors will be set to
  2031     transparent (i.e qRgba(0, 0, 0, 0)).
  2070     transparent (i.e qRgba(0, 0, 0, 0)).
  2032 
  2071 
  2033     When the image is used, the color table must be large enough to
  2072     When the image is used, the color table must be large enough to
  2034     have entries for all the pixel/index values present in the image,
  2073     have entries for all the pixel/index values present in the image,
  2035     otherwise the results are undefined.
  2074     otherwise the results are undefined.
  2036 
  2075 
  2037     \sa numColors(), colorTable(), setColor(), {QImage#Image
  2076     \sa colorCount(), colorTable(), setColor(), {QImage#Image
  2038     Transformations}{Image Transformations}
  2077     Transformations}{Image Transformations}
  2039 */
  2078 */
  2040 
  2079 
  2041 void QImage::setNumColors(int numColors)
  2080 void QImage::setColorCount(int colorCount)
  2042 {
  2081 {
  2043     if (!d) {
  2082     if (!d) {
  2044         qWarning("QImage::setNumColors: null image");
  2083         qWarning("QImage::setColorCount: null image");
  2045         return;
  2084         return;
  2046     }
  2085     }
  2047 
  2086 
  2048     detach();
  2087     detach();
  2049 
  2088 
  2050     // In case detach() ran out of memory
  2089     // In case detach() ran out of memory
  2051     if (!d)
  2090     if (!d)
  2052         return;
  2091         return;
  2053 
  2092 
  2054     if (numColors == d->colortable.size())
  2093     if (colorCount == d->colortable.size())
  2055         return;
  2094         return;
  2056     if (numColors <= 0) {                        // use no color table
  2095     if (colorCount <= 0) {                        // use no color table
  2057         d->colortable = QVector<QRgb>();
  2096         d->colortable = QVector<QRgb>();
  2058         return;
  2097         return;
  2059     }
  2098     }
  2060     int nc = d->colortable.size();
  2099     int nc = d->colortable.size();
  2061     d->colortable.resize(numColors);
  2100     d->colortable.resize(colorCount);
  2062     for (int i = nc; i < numColors; ++i)
  2101     for (int i = nc; i < colorCount; ++i)
  2063         d->colortable[i] = 0;
  2102         d->colortable[i] = 0;
  2064 
       
  2065 }
  2103 }
  2066 
  2104 
  2067 /*!
  2105 /*!
  2068     Returns the format of the image.
  2106     Returns the format of the image.
  2069 
  2107 
  3672     If the image's format is either monochrome or 8-bit, the given \a
  3710     If the image's format is either monochrome or 8-bit, the given \a
  3673     index_or_rgb value must be an index in the image's color table,
  3711     index_or_rgb value must be an index in the image's color table,
  3674     otherwise the parameter must be a QRgb value.
  3712     otherwise the parameter must be a QRgb value.
  3675 
  3713 
  3676     If \a position is not a valid coordinate pair in the image, or if
  3714     If \a position is not a valid coordinate pair in the image, or if
  3677     \a index_or_rgb >= numColors() in the case of monochrome and
  3715     \a index_or_rgb >= colorCount() in the case of monochrome and
  3678     8-bit images, the result is undefined.
  3716     8-bit images, the result is undefined.
  3679 
  3717 
  3680     \warning This function is expensive due to the call of the internal
  3718     \warning This function is expensive due to the call of the internal
  3681     \c{detach()} function called within; if performance is a concern, we
  3719     \c{detach()} function called within; if performance is a concern, we
  3682     recommend the use of \l{QImage::}{scanLine()} to access pixel data
  3720     recommend the use of \l{QImage::}{scanLine()} to access pixel data
  3833             if (!qIsGray(qt_colorConvert<quint32, qrgb888>(*b++, 0)))
  3871             if (!qIsGray(qt_colorConvert<quint32, qrgb888>(*b++, 0)))
  3834                 return false;
  3872                 return false;
  3835     } else {
  3873     } else {
  3836         if (d->colortable.isEmpty())
  3874         if (d->colortable.isEmpty())
  3837             return true;
  3875             return true;
  3838         for (int i = 0; i < numColors(); i++)
  3876         for (int i = 0; i < colorCount(); i++)
  3839             if (!qIsGray(d->colortable.at(i)))
  3877             if (!qIsGray(d->colortable.at(i)))
  3840                 return false;
  3878                 return false;
  3841     }
  3879     }
  3842     return true;
  3880     return true;
  3843 }
  3881 }
  3860     case 32:
  3898     case 32:
  3861     case 24:
  3899     case 24:
  3862     case 16:
  3900     case 16:
  3863         return allGray();
  3901         return allGray();
  3864     case 8: {
  3902     case 8: {
  3865         for (int i = 0; i < numColors(); i++)
  3903         for (int i = 0; i < colorCount(); i++)
  3866             if (d->colortable.at(i) != qRgb(i,i,i))
  3904             if (d->colortable.at(i) != qRgb(i,i,i))
  3867                 return false;
  3905                 return false;
  3868         return true;
  3906         return true;
  3869         }
  3907         }
  3870     }
  3908     }
  4081         // Pretty unlikely, so use less efficient solution.
  4119         // Pretty unlikely, so use less efficient solution.
  4082         return convertToFormat(Format_Indexed8, flags).createAlphaMask(flags);
  4120         return convertToFormat(Format_Indexed8, flags).createAlphaMask(flags);
  4083     }
  4121     }
  4084 
  4122 
  4085     QImage mask(d->width, d->height, Format_MonoLSB);
  4123     QImage mask(d->width, d->height, Format_MonoLSB);
  4086     dither_to_Mono(mask.d, d, flags, true);
  4124     if (!mask.isNull())
       
  4125         dither_to_Mono(mask.d, d, flags, true);
  4087     return mask;
  4126     return mask;
  4088 }
  4127 }
  4089 
  4128 
  4090 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
  4129 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
  4091 /*!
  4130 /*!
  4125 #define PIX(x,y)  (*((QRgb*)scanLine(y)+x) & 0x00ffffff)
  4164 #define PIX(x,y)  (*((QRgb*)scanLine(y)+x) & 0x00ffffff)
  4126 
  4165 
  4127     int w = width();
  4166     int w = width();
  4128     int h = height();
  4167     int h = height();
  4129     QImage m(w, h, Format_MonoLSB);
  4168     QImage m(w, h, Format_MonoLSB);
  4130     m.setNumColors(2);
  4169     m.setColorCount(2);
  4131     m.setColor(0, QColor(Qt::color0).rgba());
  4170     m.setColor(0, QColor(Qt::color0).rgba());
  4132     m.setColor(1, QColor(Qt::color1).rgba());
  4171     m.setColor(1, QColor(Qt::color1).rgba());
  4133     m.fill(0xff);
  4172     m.fill(0xff);
  4134 
  4173 
  4135     QRgb background = PIX(0,0);
  4174     QRgb background = PIX(0,0);
  5700 
  5739 
  5701     int w = d->width;
  5740     int w = d->width;
  5702     int h = d->height;
  5741     int h = d->height;
  5703 
  5742 
  5704     QImage image(w, h, Format_Indexed8);
  5743     QImage image(w, h, Format_Indexed8);
  5705     image.setNumColors(256);
  5744     image.setColorCount(256);
  5706 
  5745 
  5707     // set up gray scale table.
  5746     // set up gray scale table.
  5708     for (int i=0; i<256; ++i)
  5747     for (int i=0; i<256; ++i)
  5709         image.setColor(i, qRgb(i, i, i));
  5748         image.setColor(i, qRgb(i, i, i));
  5710 
  5749 
  5830 }
  5869 }
  5831 
  5870 
  5832 
  5871 
  5833 static QImage rotated90(const QImage &image) {
  5872 static QImage rotated90(const QImage &image) {
  5834     QImage out(image.height(), image.width(), image.format());
  5873     QImage out(image.height(), image.width(), image.format());
  5835     if (image.numColors() > 0)
  5874     if (image.colorCount() > 0)
  5836         out.setColorTable(image.colorTable());
  5875         out.setColorTable(image.colorTable());
  5837     int w = image.width();
  5876     int w = image.width();
  5838     int h = image.height();
  5877     int h = image.height();
  5839     switch (image.format()) {
  5878     switch (image.format()) {
  5840     case QImage::Format_RGB32:
  5879     case QImage::Format_RGB32:
  5869                         reinterpret_cast<quint8*>(out.bits()),
  5908                         reinterpret_cast<quint8*>(out.bits()),
  5870                         out.bytesPerLine());
  5909                         out.bytesPerLine());
  5871         break;
  5910         break;
  5872     default:
  5911     default:
  5873         for (int y=0; y<h; ++y) {
  5912         for (int y=0; y<h; ++y) {
  5874             if (image.numColors())
  5913             if (image.colorCount())
  5875                 for (int x=0; x<w; ++x)
  5914                 for (int x=0; x<w; ++x)
  5876                     out.setPixel(h-y-1, x, image.pixelIndex(x, y));
  5915                     out.setPixel(h-y-1, x, image.pixelIndex(x, y));
  5877             else
  5916             else
  5878                 for (int x=0; x<w; ++x)
  5917                 for (int x=0; x<w; ++x)
  5879                     out.setPixel(h-y-1, x, image.pixel(x, y));
  5918                     out.setPixel(h-y-1, x, image.pixel(x, y));
  5889 }
  5928 }
  5890 
  5929 
  5891 
  5930 
  5892 static QImage rotated270(const QImage &image) {
  5931 static QImage rotated270(const QImage &image) {
  5893     QImage out(image.height(), image.width(), image.format());
  5932     QImage out(image.height(), image.width(), image.format());
  5894     if (image.numColors() > 0)
  5933     if (image.colorCount() > 0)
  5895         out.setColorTable(image.colorTable());
  5934         out.setColorTable(image.colorTable());
  5896     int w = image.width();
  5935     int w = image.width();
  5897     int h = image.height();
  5936     int h = image.height();
  5898     switch (image.format()) {
  5937     switch (image.format()) {
  5899     case QImage::Format_RGB32:
  5938     case QImage::Format_RGB32:
  5928                        reinterpret_cast<quint8*>(out.bits()),
  5967                        reinterpret_cast<quint8*>(out.bits()),
  5929                        out.bytesPerLine());
  5968                        out.bytesPerLine());
  5930         break;
  5969         break;
  5931     default:
  5970     default:
  5932         for (int y=0; y<h; ++y) {
  5971         for (int y=0; y<h; ++y) {
  5933             if (image.numColors())
  5972             if (image.colorCount())
  5934                 for (int x=0; x<w; ++x)
  5973                 for (int x=0; x<w; ++x)
  5935                     out.setPixel(y, w-x-1, image.pixelIndex(x, y));
  5974                     out.setPixel(y, w-x-1, image.pixelIndex(x, y));
  5936             else
  5975             else
  5937                 for (int x=0; x<w; ++x)
  5976                 for (int x=0; x<w; ++x)
  5938                     out.setPixel(y, w-x-1, image.pixel(x, y));
  5977                     out.setPixel(y, w-x-1, image.pixel(x, y));
  6068         // initizialize the data
  6107         // initizialize the data
  6069         case 8:
  6108         case 8:
  6070             if (dImage.d->colortable.size() < 256) {
  6109             if (dImage.d->colortable.size() < 256) {
  6071                 // colors are left in the color table, so pick that one as transparent
  6110                 // colors are left in the color table, so pick that one as transparent
  6072                 dImage.d->colortable.append(0x0);
  6111                 dImage.d->colortable.append(0x0);
  6073                 memset(dImage.bits(), dImage.d->colortable.size() - 1, dImage.numBytes());
  6112                 memset(dImage.bits(), dImage.d->colortable.size() - 1, dImage.byteCount());
  6074             } else {
  6113             } else {
  6075                 memset(dImage.bits(), 0, dImage.numBytes());
  6114                 memset(dImage.bits(), 0, dImage.byteCount());
  6076             }
  6115             }
  6077             break;
  6116             break;
  6078         case 1:
  6117         case 1:
  6079         case 16:
  6118         case 16:
  6080         case 24:
  6119         case 24:
  6081         case 32:
  6120         case 32:
  6082             memset(dImage.bits(), 0x00, dImage.numBytes());
  6121             memset(dImage.bits(), 0x00, dImage.byteCount());
  6083             break;
  6122             break;
  6084     }
  6123     }
  6085 
  6124 
  6086     if (target_format >= QImage::Format_RGB32) {
  6125     if (target_format >= QImage::Format_RGB32) {
  6087         QPainter p(&dImage);
  6126         QPainter p(&dImage);