src/gui/image/qimage.cpp
changeset 30 5dc02b23752f
parent 29 b72c6db6890b
child 33 3e2da88830cd
--- a/src/gui/image/qimage.cpp	Wed Jun 23 19:07:03 2010 +0300
+++ b/src/gui/image/qimage.cpp	Tue Jul 06 15:10:48 2010 +0300
@@ -272,8 +272,6 @@
 
     switch (format) {
 
-    case QImage::Format_Mono:
-    case QImage::Format_MonoLSB:
     case QImage::Format_Indexed8:
         has_alpha_pixels = has_alpha_clut;
         break;
@@ -482,9 +480,12 @@
     \row
     \o Low-level information
     \o
+
     The depth() function returns the depth of the image. The supported
-    depths are 1 (monochrome), 8 and 32 (for more information see the
-    \l {QImage#Image Formats}{Image Formats} section).
+    depths are 1 (monochrome), 8, 16, 24 and 32 bits. The
+    bitPlaneCount() function tells how many of those bits that are
+    used. For more information see the
+    \l {QImage#Image Formats}{Image Formats} section.
 
     The format(), bytesPerLine(), and byteCount() functions provide
     low-level information about the data stored in the image.
@@ -709,7 +710,7 @@
                             packed with the less significant bit (LSB) first.
 
     \value Format_Indexed8  The image is stored using 8-bit indexes
-                            into a colormap. 
+                            into a colormap.
 
     \value Format_RGB32     The image is stored using a 32-bit RGB format (0xffRRGGBB).
 
@@ -1582,12 +1583,12 @@
 /*!
     Returns the depth of the image.
 
-    The image depth is the number of bits used to encode a single
+    The image depth is the number of bits used to store a single
     pixel, also called bits per pixel (bpp).
 
     The supported depths are 1, 8, 16, 24 and 32.
 
-    \sa convertToFormat(), {QImage#Image Formats}{Image Formats},
+    \sa bitPlaneCount(), convertToFormat(), {QImage#Image Formats}{Image Formats},
     {QImage#Image Information}{Image Information}
 
 */
@@ -1837,7 +1838,7 @@
     qAlpha() to access the pixels.
 
     \sa bytesPerLine(), bits(), {QImage#Pixel Manipulation}{Pixel
-    Manipulation}
+    Manipulation}, constScanLine()
 */
 uchar *QImage::scanLine(int i)
 {
@@ -1867,6 +1868,28 @@
 
 
 /*!
+    Returns a pointer to the pixel data at the scanline with index \a
+    i. The first scanline is at index 0.
+
+    The scanline data is aligned on a 32-bit boundary.
+
+    Note that QImage uses \l{Implicit Data Sharing} {implicit data
+    sharing}, but this function does \e not perform a deep copy of the
+    shared pixel data, because the returned data is const.
+
+    \sa scanLine(), constBits()
+    \since 4.7
+*/
+const uchar *QImage::constScanLine(int i) const
+{
+    if (!d)
+        return 0;
+
+    Q_ASSERT(i >= 0 && i < height());
+    return d->data + i * d->bytes_per_line;
+}
+
+/*!
     Returns a pointer to the first pixel data. This is equivalent to
     scanLine(0).
 
@@ -1875,7 +1898,7 @@
     data, thus ensuring that this QImage is the only one using the
     current return value.
 
-    \sa scanLine(), byteCount()
+    \sa scanLine(), byteCount(), constBits()
 */
 uchar *QImage::bits()
 {
@@ -1903,6 +1926,20 @@
 }
 
 
+/*!
+    Returns a pointer to the first pixel data.
+
+    Note that QImage uses \l{Implicit Data Sharing} {implicit data
+    sharing}, but this function does \e not perform a deep copy of the
+    shared pixel data, because the returned data is const.
+
+    \sa bits(), constScanLine()
+    \since 4.7
+*/
+const uchar *QImage::constBits() const
+{
+    return d ? d->data : 0;
+}
 
 /*!
     \fn void QImage::reset()
@@ -2951,19 +2988,19 @@
         colorTable.resize(256);
         for (int i=0; i<256; ++i)
             colorTable[i] = qRgb(i, i, i);
-
     }
 
     int w = src->width;
     const uchar *src_data = src->data;
     uchar *dest_data = dest->data;
+    int tableSize = colorTable.size() - 1;
     for (int y = 0; y < src->height; y++) {
         uint *p = (uint *)dest_data;
         const uchar *b = src_data;
         uint *end = p + w;
 
         while (p < end)
-            *p++ = colorTable.at(*b++);
+            *p++ = colorTable.at(qMin<int>(tableSize, *b++));
 
         src_data += src->bytes_per_line;
         dest_data += dest->bytes_per_line;
@@ -5667,12 +5704,12 @@
         return;
     }
 
-    detach();
-
-    QImage converted = convertToFormat(QImage::Format_ARGB32_Premultiplied);
-    if (!converted.isNull())
-        *this = converted;
+    if (d->format == QImage::Format_ARGB32_Premultiplied)
+        detach();
     else
+        *this = convertToFormat(QImage::Format_ARGB32_Premultiplied);
+
+    if (isNull())
         return;
 
     // Slight optimization since alphachannels are returned as 8-bit grays.
@@ -5818,6 +5855,48 @@
 }
 
 
+/*!
+    \since 4.7
+    Returns the number of bit planes in the image.
+
+    The number of bit planes is the number of bits of color and
+    transparency information for each pixel. This is different from
+    (i.e. smaller than) the depth when the image format contains
+    unused bits.
+
+    \sa depth(), format(), {QImage#Image Formats}{Image Formats}
+*/
+int QImage::bitPlaneCount() const
+{
+    if (!d)
+        return 0;
+    int bpc = 0;
+    switch (d->format) {
+    case QImage::Format_Invalid:
+        break;
+    case QImage::Format_RGB32:
+        bpc = 24;
+        break;
+    case QImage::Format_RGB666:
+        bpc = 18;
+        break;
+    case QImage::Format_RGB555:
+        bpc = 15;
+        break;
+    case QImage::Format_ARGB8555_Premultiplied:
+        bpc = 23;
+        break;
+    case QImage::Format_RGB444:
+        bpc = 12;
+        break;
+    default:
+        bpc = depthForFormat(d->format);
+        break;
+    }
+    return bpc;
+}
+
+
 #ifdef QT3_SUPPORT
 #if defined(Q_WS_X11)
 QT_BEGIN_INCLUDE_NAMESPACE