[Qt] A comprehensive summary of Qt's operation on image data

1. Introduction

Qt provides four classes to handle image data: QImage, QPixmap, QBitmap, and QPicture.

(1) QImage: A class designed and optimized for I/O and direct pixel access and manipulation.

(2) QPixmap: A class designed and optimized for images displayed on the screen.

(3) QBitmap: A convenient class inherited from QPixmap that ensures a depth of 1.

(4) QPicture: This class is a painting device that can record and playback QPainter commands.

Two, QImage class

(2-1) Introduction to QImage

The QImage class provides a hardware-independent image representation that allows developers to directly access pixel data, and this class can be used as a drawing device.

Because QImage is a subclass of QPaintDevice, you can use QPainter to draw directly on QImage. When using QPainter on a QImage, drawing operations can be performed in a thread other than the current GUI thread.

The QImage class supports several image formats described by the Format enum. Includes: monochrome, 8-bit, 32-bit and alphanumeric images.

QImage provides a set of functions that can be used to obtain various information about an image. There are also several functions that implement image transformations.

QImage objects can be passed by value because the QImage class uses implicit data sharing. QImage objects can also be streamed and compared.

Note: Drawing on QImage in QImage::Format_Indexed8 format is not supported.

(2-2) Use QImage to read image files

QImage provides three methods of loading image files:

Method 1: You can load the image file when constructing the QImage object.

Method 2: Use the load() or loadFromData() function to load the image file.

Method 3: QImage provides a static fromData() function to construct a QImage object from the given data.

Note: When loading an image file, the name of the image file can be an actual file path on disk, or it can refer to an application's resource file.

(2-3) Image writing using QImage

​ Use the save() function to save the QImage object.

QImageSupported file formats can be obtained with QImageReader::supportedImageFormats()and QImageWriter::supportedImageFormats()functions. New file formats can be added as plugins. By default Qt supports the following image formats:

serial number Format describe Operations supported by Qt
1 BMP Windows bitmap read/write
2 GIF Graphic Interchange Format read
3 JPG Joint Photographic Experts Group read/write
4 JPEG Joint Photographic Experts Group read/write
5 PNG removable web images read
6 PBM movable bitmap read
7 PGM Portable Graymap read
8 PPM Portable Pixmap read/write
9 XBM X11 bitmap read/write
10 XPM X11Pixmap read/write
(2-4) Get image information
(2-4-1) Obtaining Geometryinformation
  • The size(), width(), height(), dotsPerMeterX() and dotsPerMeterY() functions provide information about the image size and aspect ratio.
  • The rect() function returns the surrounding rectangle of the image.
  • The valid() function checks whether a given coordinate pair is within this rectangle.
  • The offset() function returns the number of pixels by which the image is positioned relative to other images, which can also be manipulated using the setOffset() function.
(2-4-2) Get color information
  • The color of a pixel can be retrieved by passing the pixel coordinates to the pixel() function. The role of the pixel() function is to return a color that has nothing to do with the image format in the form of a QRgb value.
  • For monochrome and 8-bit images, the colorCount() and colorTable() functions provide information about the color components used to store the image data: the colorTable() function returns the image's color table. To get a single entry, use the pixelIndex() function to retrieve the pixel index for a given coordinate pair, then use the color() function to retrieve the color. (Note that if you manually create an 8-bit image, you must also set a valid colormap on the image).
  • The hasalphchannel() function checks whether the image format respects the alpha channel.
  • The allGray() and isGrayscale() functions check if the image colors are all gray.
(2-4-3) Get text information
  • text()What the function does is: Return the image text associated with the given text key. An image's text keys can be retrieved using the textKeys() function. Use the setText() function to modify the text of the image.
(2-4-4) Obtain the underlying information of the image
  • depth()The role of the function is to return the depth of the image, and the supported depths are 1 (monochrome), 8, 16, 24 and 32 bits. Call the bitPlaneCount() function to know how many bits the image uses.
  • The format(), bytesPerLine(), and sizeInBytes() functions provide low-level information about the data stored in the image. The bytesPerLine() function returns the bytes per image scan line. The sizeInBytes() function returns the image data size in bytes. The cacheKey() function returns a number that uniquely identifies the contents of a QImage object.
(2-5) Manipulating image pixels

Functions for manipulating image pixels depend on the image format. The reason is that monochrome and 8-bit images are index based, using a color lookup table. Whereas 32-bit images store ARGB values ​​directly.

(1) For 32-bit images, you can use the setPixel() function to change the color of a pixel at a given coordinate to any color specified by the ARGB quadruple. To generate a suitable QRgb value, use the QRgb() (adds a default alpha component to a given RGB value, i.e. creates an opaque color) or qRgba() functions. For example:

QImage image(3,3,QImage::Format_RGB32);
QRgb value;

value = qRgb(189,149,39);
image.setPixmel(1,1,value);

(2) For 8-bit and monochrome images, the pixel value is just an index into the image's color table. Therefore, the setPixel() function can only change the pixel color at a given coordinate from the image's color table to a predefined color, that is, only the index value of the pixel can be changed. To change or add colors to an image's color table, use the setColor() function. For example:

QImage image(3,3,QImage::Format_Indexed8);
QRgb value;

value = qRgb(122,163,39);
image.setColor(0,value);

​ For images with more than 8 bits per color channel. The QColor value can be set and retrieved using the setPixelColor() and pixelColor() methods.

(2-6) Image format

Each pixel stored in a QImage is represented by an integer. The size of the integer depends on the format. QImage supports several image formats described by enum QImage::Format.

Monochrome images are stored using 1-bit indices into a colormap containing up to two colors. There are two different types of monochrome images: big endian (MSB first) and little endian (LSB first) bit ordering.

8-bit images are stored into the color table using an 8-bit index, i.e. one byte per pixel. The color table is a QVector, and the QRgb type definition is equivalent to an unsigned int, including the ARGB quadruplet in the format 0xAARRGGBB.

32-bit images do not have a color table; instead, each pixel contains a QRgb value. There are three different types of 32-bit images that store RGB (ie 0xffRRGGBB), ARGB, and premultiplied ARGB values. In premultiplied format, the red, green, and blue channels are multiplied by the alpha component and divided by 255.

The format of an image can be retrieved using the format() function. Use the convertToFormat() function to convert an image to another format. The allGray() and isGrayscale() functions detect whether it is safe to convert a color image to grayscale.

serial number Format Remark
1 QImage::Format_Invalid None
2 QImage::Format_Mono None
3 QImage::Format_MonoLSB None
4 QImage::Format_Indexed8 None
5 QImage::Format_RGB32 None
6 QImage::Format_ARGB32 None
7 QImage::Format_ARGB32_Premultiplied None
8 QImage::Format_RGB16 None
9 QImage::Format_ARGB8565_Premultiplied None
10 QImage::Format_RGB666 None
11 QImage::Format_ARGB6666_Premultiplied None
12 QImage::Format_RGB555 None
13 QImage::Format_ARGB8555_Premultiplied None
14 QImage::Format_RGB888 None
15 QImage::Format_RGB444 None
16 QImage::Format_ARGB4444_Premultiplied None
17 QImage::Format_RGBX8888 Qt5.2
18 QImage::Format_RGBA8888 Qt5.2
19 QImage::Format_RGBA8888_Premultiplied Qt5.2
20 QImage::Format_BGR30 Qt5.4
21 QImage::Format_A2BGR30_Premultiplied Qt5.4
22 QImage::Format_RGB30 Qt5.4
23 QImage::Format_A2RGB30_Premultiplied Qt5.4
24 QImage::Format_Alpha8 Qt5.5
25 QImage::Format_Grayscale8 Qt5.5
26 QImage::Format_Grayscale16 Qt5.13
27 QImage::Format_RGBX64 Qt5.12
28 QImage::Format_BGR888 Qt5.14

Three, QPixmap class

(3-1) Introduction to QPixmap

The QPixmap class is an off-screen image representation that can be used as a drawing device.

Displaying a QPixmap on the screen is easy using QLabel or a subclass of QAbstractButton such as QPushButton and QToolButton. QLabel has a pixmap property, and QAbstractButton has an icon property.

​ QPixmap objects can be passed by value because the QPixmap class uses implicit data sharing.

​ 注意,pixmap中的像素数据是内部的,由底层窗口系统管理。因为QPixmapQPaintDevice子类,所以可以使用QPainter直接在pixmap上绘图。像素只能通过QPainter函数或将QPixmap转换为QImage来访问。不过,fill()函数可用来初始化给定颜色的整个像素映射。

​ 可以在QImage和QPixmap之间转换。通常,在将QImage对象转换为要显示在屏幕上的QPixmap之前,QImage类用于加载图像文件,也可以操作图像数据。另外,如果不需要操作,可以将图像文件直接加载到QPixmap中。

​ QPixmap提供了一组函数,可用于获取有关pixmap的各种信息。此外,还有几个函数支持pixmap的转换。

(3-2)使用QPixmap读取图像文件

​ QPixmap提供了几种读取图像文件的方法:可以在构建QPixmap对象时加载文件,或者使用load()或loadFromData()函数加载文件。加载图像时,文件名可以引用磁盘上的实际文件,也可以引用应用程序的图像资源。

(3-3)使用QPixmap进行图像数据写入

​ 与QImage一样,调用save()函数来保存QPixmap对象。

(3-4)获取图像信息
(3-4-1)获取Geometry信息
  • size(), width()和height()函数提供了关于位图大小的信息。
  • rect()函数的作用是:返回图像的外围矩形。
(3-4-2)获取Alpha信息
  • 如果位图的格式支持alpha通道,则hasAlphaChannel()函数返回true,否则返回false。
  • hasAlpha(), setMask()和mask()函数是低版本Qt遗留下来的,不应该使用。因为它们可能比较慢。高版本的Qt中有高性能的替代函数:createHeuristicMask()函数为该位图创建并返回1-bpp启发式掩码(即QBitmap)。它的工作原理是:从一个角落选择一种颜色,然后从所有的边缘开始去除该颜色的像素。createMaskFromColor()函数创建并返回基于给定颜色的位图的遮罩(即QBitmap)。
(3-4-3)获取底层信息
  • depth()函数的作用是:返回位图的深度。
  • defaultDepth()函数返回默认的深度,即应用程序在给定屏幕上使用的深度。
  • cacheKey()函数返回一个唯一标识QPixmap对象内容的数字。
(3-5)QPixmap转换

​ 可以使用toImage()函数将QPixmap对象转换为QImage。可以使用fromImage()将QImage转换为QPixmap。如果这个操作比较占用资源,可以使用QBitmap::fromImage()。

(3-6)QPixmap变换

​ scale()、scaledToWidth()和scaledToHeight()函数返回按比例缩放的像素映射副本,而copy()函数创建的QPixmap是原始像素映射的普通副本。

​ transform()函数返回,用给定的变换矩阵和变换模式变换后的像素图的副本:调整变换矩阵以补偿不需要的平移,即transform()返回包含原像素图所有变换点的最小像素图。函数的作用是:返回用于转换位图的实际矩阵。

四、QBitmap类

​ QBitmap类是一个单色的屏幕外绘制设备,主要用于创建自定义QCursor和QBrush对象,构造QRegion对象,以及设置pixmap和QWidget的遮罩。

​ 除了深度为0的空对象外,QBitmap是一个确保深度为1的QPixmap子类。如果将深度大于1的位图分配给位图,位图将自动抖动。

​ QBitmap类提供了transform()函数,该函数返回位图的转换副本;使用QTransform参数来平移、缩放、剪切和旋转位图。此外,QBitmap提供静态fromData()函数,该函数返回由给定uchar数据构造的位图,而静态fromImage()函数返回QImage对象的转换副本。

五、QPicture类

​ QPicture可以在不同的设备上显示 (例如svg、pdf、ps、打印机和屏幕),这些看起来都是一样的。

(1)示例:使用QPicture记录一张picture:
在这里插入图片描述
注:每次对QPainter:begin()函数调用都会重置painter命令列表。

(2)示例:如何重放一张picture
在这里插入图片描述

注:也可以使用paly()绘制图片。

六、总结

​ Qt中对图像操作的类主要有:QImage、QPixmap、QBitmap、QPicture。每个类都有其自身的特点和功能。不同类对图像的读取、写入各有特点。可以在不同图像格式之间进行转换,也可以获取关于图像的相关信息(例如:位置(geometry)、颜色(color)、底层信息)等,同时可以对图像进行变换。在对图像进行操作时,合理配合使用这几个类。可以对图像有着很好的处理和显示效果。


搜索关注【嵌入式小生】wx公众号获取更多精彩内容>>>>
请添加图片描述

Guess you like

Origin blog.csdn.net/iriczhao/article/details/123265538