📄 qimage.cpp
字号:
\l {QImage#Image Formats}{Image Formats} section). The format(), bytesPerLine(), and numBytes() functions provide low-level information about the data stored in the image. The cacheKey() function returns a number that uniquely identifies the contents of this QImage object. \endtable \section1 Pixel Manipulation The functions used to manipulate an image's pixels depend on the image format. The reason is that monochrome and 8-bit images are index-based and use a color lookup table, while 32-bit images store ARGB values directly. For more information on image formats, see the \l {Image Formats} section. In case of a 32-bit image, the setPixel() function can be used to alter the color of the pixel at the given coordinates to any other color specified as an ARGB quadruplet. To make a suitable QRgb value, use the qRgb() (adding a default alpha component to the given RGB values, i.e. creating an opaque color) or qRgba() function. For example: \table \row \o \inlineimage qimage-32bit_scaled.png \o \code QImage image(3, 3, QImage::Format_RGB32); QRgb value; value = qRgb(189, 149, 39); // 0xffbd9527 image.setPixel(1, 1, value); value = qRgb(122, 163, 39); // 0xff7aa327 image.setPixel(0, 1, value); image.setPixel(1, 0, value); value = qRgb(237, 187, 51); // 0xffedba31 image.setPixel(2, 1, value); \endcode \header \o {2,1}32-bit \endtable In case of a 8-bit and monchrome images , the pixel value is only an index from the image's color table. So the setPixel() function can only be used to alter the color of the pixel at the given coordinates to a predefined color from the image's color table, i.e. it can only change the pixel's index value. To alter or add a color to an image's color table, use the setColor() function. An entry in the color table is an ARGB quadruplet encoded as an QRgb value. Use the qRgb() and qRgba() functions to make a suitable QRgb value for use with the setColor() function. For example: \table \row \o \inlineimage qimage-8bit_scaled.png \o \code QImage image(3, 3, QImage::Format_Indexed8); QRgb value; value = qRgb(122, 163, 39); // 0xff7aa327 image.setColor(0, value); value = qRgb(237, 187, 51); // 0xffedba31 image.setColor(1, value); value = qRgb(189, 149, 39); // 0xffbd9527 image.setColor(2, value); image.setPixel(0, 1, 0); image.setPixel(1, 0, 0); image.setPixel(1, 1, 2); image.setPixel(2, 1, 1); \endcode \header \o {2,1} 8-bit \endtable QImage also provide the scanLine() function which returns a pointer to the pixel data at the scanline with the given index, and the bits() function which returns a pointer to the first pixel data (this is equivalent to \c scanLine(0)). \section1 Image Formats Each pixel stored in a QImage is represented by an integer. The size of the integer varies depending on the format. QImage supports several image formats described by the \l Format enum. The monochrome (1-bit), 8-bit and 32-bit images are available in all versions of Qt. In addition Qtopia Core also supports 2-bit, 4-bit, and 16-bit images. For more information about the Qtopia specific formats, see the documentation of the \l Format enum. Monochrome images are stored using 1-bit indexes into a color table with at most two colors. There are two different types of monochrome images: big endian (MSB first) or little endian (LSB first) bit order. 8-bit images are stored using 8-bit indexes into a color table, i.e. they have a single byte per pixel. The color table is a QVector<QRgb>, and the QRgb typedef is equivalent to an unsigned int containing an ARGB quadruplet on the format 0xAARRGGBB. 32-bit images have no color table; instead, each pixel contains an QRgb value. There are three different types of 32-bit images storing RGB (i.e. 0xffRRGGBB), ARGB and premultiplied ARGB values respectively. In the premultiplied format the red, green, and blue channels are multiplied by the alpha component divided by 255. An image's format can be retrieved using the format() function. Use the convertToFormat() functions to convert an image into another format. The allGray() and isGrayscale() functions tell whether a color image can safely be converted to a grayscale image. \section1 Image Transformations QImage supports a number of functions for creating a new image that is a transformed version of the original: The createAlphaMask() function builds and returns a 1-bpp mask from the alpha buffer in this image, and the createHeuristicMask() function creates and returns a 1-bpp heuristic mask for this image. The latter function works by selecting a color from one of the corners, then chipping away pixels of that color starting at all the edges. The mirrored() function returns a mirror of the image in the desired direction, the scaled() returns a copy of the image scaled to a rectangle of the desired measures, the rgbSwapped() function constructs a BGR image from a RGB image, and the alphaChannel() function constructs an image from this image's alpha channel. The scaledToWidth() and scaledToHeight() functions return scaled copies of the image. The transformed() function returns a copy of the image that is transformed with the given transformation matrix and transformation mode: Internally, the transformation matrix is adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest image containing all transformed points of the original image. The static trueMatrix() function returns the actual matrix used for transforming the image. There are also functions for changing attributes of an image in-place: \table \header \o Function \o Description \row \o setAlphaChannel() \o Sets the alpha channel of the image. \row \o setDotsPerMeterX() \o Defines the aspect ratio by setting the number of pixels that fit horizontally in a physical meter. \row \o setDotsPerMeterY() \o Defines the aspect ratio by setting the number of pixels that fit vertically in a physical meter. \row \o fill() \o Fills the entire image with the given pixel value. \row \o invertPixels() \o Inverts all pixel values in the image using the given InvertMode value. \row \o setColorTable() \o Sets the color table used to translate color indexes. Only monochrome and 8-bit formats. \row \o setNumColors() \o Resizes the color table. Only monochrome and 8-bit formats. \endtable \section1 Legal Information For smooth scaling, the transformed() functions use code based on smooth scaling algorithm by Daniel M. Duley. \legalese Copyright (C) 2004, 2005 Daniel M. Duley Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \endlegalese \sa QImageReader, QImageWriter, QPixmap, QSvgRenderer, {Image Composition Example}, {Image Viewer Example}, {Scribble Example}, {Pixelator Example}*//*! \enum QImage::Endian \compat This enum type is used to describe the endianness of the CPU and graphics hardware. It is provided here for compatibility with earlier versions of Qt. Use the \l Format enum instead. The \l Format enum specify the endianess for monchrome formats, but for other formats the endianess is not relevant. \value IgnoreEndian Endianness does not matter. Useful for some operations that are independent of endianness. \value BigEndian Most significant bit first or network byte order, as on SPARC, PowerPC, and Motorola CPUs. \value LittleEndian Least significant bit first or little endian byte order, as on Intel x86.*//*! \enum QImage::InvertMode This enum type is used to describe how pixel values should be inverted in the invertPixels() function. \value InvertRgb Invert only the RGB values and leave the alpha channel unchanged. \value InvertRgba Invert all channels, including the alpha channel. \sa invertPixels()*//*! \enum QImage::Format The following image formats are available in all versions of Qt: \value Format_Invalid The image is invalid. \value Format_Mono The image is stored using 1-bit per pixel. Bytes are packed with the most significant bit (MSB) first. \value Format_MonoLSB The image is stored using 1-bit per pixel. Bytes are packed with the less significant bit (LSB) first. \value Format_Indexed8 The image is stored using 8-bit indexes into a colormap. \value Format_RGB32 The image is stored using a 32-bit RGB format (0xffRRGGBB). \value Format_ARGB32 The image is stored using a 32-bit ARGB format (0xAARRGGBB). \value Format_ARGB32_Premultiplied The image is stored using a premultiplied 32-bit ARGB format (0xAARRGGBB), i.e. the red, green, and blue channels are multiplied by the alpha component divided by 255. (If RR, GG, or BB has a higher value than the alpha channel, the results are undefined.) Certain operations (such as image composition using alpha blending) are faster using premultiplied ARGB32 than with plain ARGB32. The following image format is specific to \l{Qtopia Core}: \value Format_RGB16 The image is stored using a 16-bit RGB format (5-6-5). \sa format(), convertToFormat()*//***************************************************************************** QImage member functions *****************************************************************************/// table to flip bitsstatic const uchar bitflip[256] = { /* open OUT, "| fmt"; for $i (0..255) { print OUT (($i >> 7) & 0x01) | (($i >> 5) & 0x02) | (($i >> 3) & 0x04) | (($i >> 1) & 0x08) | (($i << 7) & 0x80) | (($i << 5) & 0x40) | (($i << 3) & 0x20) | (($i << 1) & 0x10), ", "; } close OUT; */ 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255};const uchar *qt_get_bitflip_array() // called from QPixmap code{ return bitflip;}#if defined(QT3_SUPPORT)static QImage::Format formatFor(int depth, QImage::Endian bitOrder){ QImage::Format format; if (depth == 1) { format = bitOrder == QImage::BigEndian ? QImage::Format_Mono : QImage::Format_MonoLSB; } else if (depth == 8) { format = QImage::Format_Indexed8; } else if (depth == 32) { format = QImage::Format_RGB32; } else if (depth == 16) { format = QImage::Format_RGB16; } else { qWarning("QImage: Depth %d not supported", depth); format = QImage::Format_Invalid; } return format;}#endif/*! Constructs a null image. \sa isNull()*/QImage::QImage() : QPaintDevice(){ d = 0;}/*! Constructs an image with the given \a width, \a height and \a format.*/QImage::QImage(int width, int height, Format format) : QPaintDevice(){ d = QImageData::create(QSize(width, height), format, 0);}/*! Constructs an image with the given \a size and \a format.*/QImage::QImage(const QSize &size, Format format) : QPaintDevice(){ d = QImageData::create(size, format, 0);}QImageData *QImageData::create(uchar *data, int width, int height, int bpl, QImage::Format format, bool readOnly){ QImageData *d = 0; if (format == QImage::Format_Invalid) return d; const int depth = depthForFormat(format); const int calc_bytes_per_line = ((width * depth + 31)/32) * 4; const int min_bytes_per_line = (width * depth + 7)/8; if (bpl <= 0) bpl = calc_bytes_per_line; if (width <= 0 || height <= 0 || !data || INT_MAX/sizeof(uchar *) < uint(height) || INT_MAX/uint(depth) < uint(width) || bpl <= 0 || bpl < min_bytes_per_line || INT_MAX/uint(bpl) < uint(height)) return d; // invalid parameter(s) d = new QImageData; d->ref.ref(); d->own_data = false; d->ro_data = readOnly; d->data = data; d->width = width; d->height = height; d->depth = depth; d->format = format; d->bytes_per_line = bpl; d->nbytes = d->bytes_per_line * height; return d;}/*! Constructs an image with the given \a width, \a height and \a format, that uses an existing memory buffer, \a data. The \a width and \a height must be specified in pixels, \a data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned. The buffer must remain valid throughout the life of the QImage. The image does not delete the buffer at destruction.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -