📄 qimage.cpp
字号:
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() fucntion 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 \sa QImageReader, QImageWriter, QPixmap*//*! \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.) 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) || defined(Q_WS_QWS)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;#ifdef Q_WS_QWS } else if (depth == 16) { format = QImage::Format_RGB16;#endif } 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);}/*! 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, and that \a data must be 32-bit aligned. The buffer must remain valid throughout the life of the QImage. The image does not delete the buffer at destruction. If the image is in an indexed color format, set the color table for the image using setColorTable().*/QImage::QImage(uchar* data, int width, int height, Format format) : QPaintDevice(){ d = 0; if (format == Format_Invalid || width <= 0 || height <= 0 || !data) return; // invalid parameter(s) d = new QImageData; d->ref.ref(); d->own_data = false; d->data = data; d->width = width; d->height = height; d->depth = depthForFormat(format); d->format = format; d->bytes_per_line = ((width * d->depth + 31)/32) * 4; d->nbytes = d->bytes_per_line * height;}/*! Constructs an image and tries to load the image from the file with the given \a fileName. The loader attempts to read the image using the specified \a format. If the \a format is not specified (which is the default), the loader probes the file for a header to guess the file format. If the loading of the image failed, this object is a null image. The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the \l{resources.html}{Resource System} overview for details on how to embed images and other resource files in the application's executable. \sa isNull(), {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}*/QImage::QImage(const QString &fileName, const char *format) : QPaintDevice(){ d = 0; load(fileName, format);}/*! Constructs an image and tries to load the image from the file with the given \a fileName. The loader attempts to read the image using the specified \a format. If the \a format is not specified (which is the default), the loader probes the file for a header to guess the file format. If the loading of the image failed, this object is a null image. The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the \l{resources.html}{Resource System} overview for details on how to embed images and other resource files in the application's executable. You can disable this constructor by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful, for example, if you want to ensure that all user-visible strings go through QObject::tr(). \sa QString::fromAscii(), isNull(), {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}*/QImage::QImage(const char *fileName, const char *format) : QPaintDevice(){ // ### Qt 5: if you remove the QImage(const QByteArray &) QT3_SUPPORT // constructor, remove this constructor as well. The constructor here // exists so that QImage("foo.png") compiles without ambiguity. d = 0; load(QString::fromAscii(fileName), format);}#ifndef QT_NO_IMAGEFORMAT_XPMextern bool qt_read_xpm_image_or_array(QIODevice *device, const char * const *source, QImage &image);/*! Constructs an image from the given \a xpm image. Make sure that the image is a valid XPM image. Errors are silently ignored. Note that it's possible to squeeze the XPM variable a little bit by using an unusual declaration: \code static const char * const start_xpm[] = { "16 15 8 1",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -