QImage类详解(QImage类型转换、QImage类函数及QImage像素操作)

QImage类详解(QImage类型转换、QImage类函数及QImage像素操作)QImage类(QImage类型转换、QImage类函数及QImage像素操作)
打开Qt帮助⽂档,会看到有关于QImage的描述如下:The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device。即QImage类是设备⽆关的图像,可以进⾏像素级操作,也可以被⽤作绘图设备,因为QImage继承于QPaintDevice。
Format:
打开enum QImage::Format,会看到如下信息:
Constant Value Description
QImage::Format_Invalid0The image is invalid.
QImage::Format_Mono1The image is stored using 1-bit per pixel. Bytes are packed with the most significant
bit (MSB) first.
QImage::Format_MonoLSB2The image is stored using 1-bit per pixel. Bytes are packed with the less significant
bit (LSB) first.
QImage::Format_Indexed83The image is stored using 8-bit indexes into a colormap. QImage::Format_RGB324The image is stored using a 32-bit RGB format (0xffRRGGBB). QImage::Format_ARGB325The image is stored using a 32-bit ARGB format (0xAARRGGBB).
QImage::Format_ARGB32_Premultiplied6
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.
QImage::Format_RGB167The image is stored using a 16-bit RGB format (5-6-5). QImage::Format_ARGB8565_Premultiplied8The image is stored using a premultiplied 24-bit ARGB format (8-5-6-5).独裁国家
QImage::Format_RGB6669The image is stored using a 24-bit RGB format (6-6-6). The unused most significant
bits is always zero.
QImage::Format_ARGB6666_Premultiplied10The image is stored using a premultiplied 24-bit ARGB format (6-6-6-6).
QImage::Format_RGB55511The image is stored using a 16-bit RGB format (5-5-5). The unused most significant
bit is always zero.
QImage::Format_ARGB8555_Premultiplied12The image is stored using a premultiplied 24-bit ARGB format (8-5-5-5).
QImage::Format_RGB88813The image is stored using a 24-bit RGB format (8-8-8).
QImage::Format_RGB44414The image is stored using a 16-bit RGB format (4-4-4). The unused bits are always
zero.
QImage::Format_ARGB4444_Premultiplied15The image is stored using a premultiplied 16-bit ARGB format (4-4-4-4).
QImage::Format_RGBX888816The image is stored using a 32-bit byte-ordered RGB(x) format (8-8-8-8). This is the same as the Format_RGBA8888 except alpha must always be 255. (added in Qt 5.2)
QImage::Format_RGBA888817
The image is stored using a 32-bit byte-ordered RGBA format (8-8-8-8). Unlike ARGB32 this is a byte-ordered format, which means the 32bit encoding differs between big endian and little endian architectures, being respectively (0xRRGGBBAA) and (0xAABBGGRR). The order of the colors is the same on any architecture if read as bytes 0xRR,0xGG,0xBB,0xAA. (added in Qt 5.2)
QImage::Format_RGBA8888_Premultiplied18The image is stored using a premultiplied 32-bit byte-ordered RGBA format (8-8-8-8).
(added in Qt 5.2)
QImage::Format_BGR3019The image is stored using a 32-bit BGR format (x-10-10-10). (added in Qt 5.4)
官⽅也不推荐以上函数来访问QImage像素,因为效率极低,开销较⼤。
2、pixel()函数
QRgb QImage::pixel(const QPoint &position) const
返回指定位置的像素颜⾊
同样有Warning: This function is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read.
3、scanLine()函数
uchar *QImage::scanLine(int i)
返回索引为i的扫描线处的像素数据指针。第⼀条扫描线位于索引0处。
4、bits()函数
uchar *QImage::bits()
返回指向第⼀个像素数据的指针。这相当于scanLine(0)。
Note that QImage uses implicit data sharing. This function performs a deep copy of the shared pixel data, thus ensuring that this QImage is the only one using the current return value.
Implicit Sharing:
Qt中的许多C++类使⽤隐式数据共享来最⼤化资源使⽤和最⼩化复制。隐式共享类在作为参数传递时既安全⼜⾼效,因为只传递指向数据的指针,并且仅当函数写⼊数据时(即,写⼊时复制)才会复制数据。在使⽤=操作符的时候浅复制。
5、constScanLine()函数
const uchar *QImage::constScanLine(int i) const
机辅翻译返回索引为i的扫描线处的像素数据指针。第⼀条扫描线位于索引0处。
6、constBit()函数
const uchar *QImage::constBits() const
返回指向第⼀个像素数据的指针。
智能卡阅读器
请注意,QImage使⽤隐式数据共享,但此函数不执⾏共享像素数据的深度复制,因为返回的数据是常量。
7、setColor()函数
void QImage::setColor(int index, QRgb colorValue)
将颜⾊表中给定索引处的颜⾊设置为给定的colorValue,colorValue是⼀个ARGB四元组。如果索引超出颜⾊表的当前⼤⼩,则会使⽤setColorCount()函数扩展。
8、setColorCount()函数
void QImage::setColorCount(int colorCount)
调整颜⾊表的⼤⼩以包含colorCount个条⽬,如果颜⾊表是可扩展的,所有额外颜⾊将设置为透明(即qRgba(0,0,0,0))。
使⽤图像时,颜⾊表必须⾜够⼤,以包含图像中所有像素/索引值的条⽬,否则结果将⽆法定义。
9、colorCount()函数
int QImage::colorCount() const
返回图像颜⾊表的⼤⼩。注意,对于32 bpp图像,colorCount()返回0,因为这些图像不使⽤颜⾊表,⽽是将像素值编码为ARGB四元组。
10、color()函数
QRgb QImage::color(int i) const
返回索引i处颜⾊表中的颜⾊。第⼀种颜⾊位于索引0处。
图像颜⾊表中的颜⾊指定为ARGB四元组(QRgb)。使⽤qAlpha()、qRed()、qGreen()和qBlue()函数获取颜⾊值组件。
11、setColorTable()函数
人民日报国庆社论void QImage::setColorTable(const QVector<QRgb> colors)
将⽤于将颜⾊索引转换为QRgb值的颜⾊表设置为指定的颜⾊。
使⽤图像时,颜⾊表必须⾜够⼤,以包含图像中所有像素/索引值的条⽬,否则结果将⽆法定义。
12、colorTable()函数
QVector<QRgb> QImage::colorTable() const
返回图像颜⾊表中包含的颜⾊列表,如果图像没有颜⾊表,则返回空列表河南地震网
有的时候只看注释也不能完全搞懂函数的意思,举⼀些实例吧
以上部分函数的使⽤举例(主要代码): 直接建⽴⼀个Indexed8格式的QImage,并读⼊数据
QImage Qimg(imgWidth,imgHeight,QImage::Format_Indexed8);
//QImage(int width, int height, QImage::Format format)
Qimg.setColorCount(256);// 灰度级数256
for(int i =0; i <256; i++)
{
Qimg.setColor(i,qRgb(i, i, i));
}
uchar tempData[imgWidth];
for(int i =0; i < imgHeight; i++)
{
for(int j =0; j < imgWidth; j++)
{
tempData[j]= Hidata[i*imgWidth + j]/16;//将第i+1⾏的数据复制给⼀维数组tempdata
//Hidata[]为图像数据,事先将图像数据读⼊该⼀维数组中,该图像的⾼为imgWidth、宽为imgHeight
}
uchar *pDest = Qimg.scanLine(i);
memcpy(pDest, tempData, imgWidth);
}
*qimgHi = Qimg;
int pixelindexValue = qimgHi->pixelIndex(304,236);//分析图像中点(304,236)处的坐标
qDebug()<<"像素值索引pixelIndex()为:"<< pixelindexValue << Qt::endl;
QRgb mRgb = qimgHi->pixel(304,236);
QColor mColor =QColor(mRgb);
qDebug()<<"QColor,即pixel()为:"<< mColor << Qt::endl;
黄冈师范学院图书馆
qDebug()<< d()<< ()<< mColor.blue()<< Qt::endl;
qDebug()<<"QRgb为:"<< mRgb << Qt::endl;
运⾏结果如下:
像素值索引pixelIndex()为: 13
QColor,即pixel()为: QColor(ARGB 1, 0.0509804, 0.0509804, 0.0509804)  //  13/255=0.0509804
13 13 13
QRgb为: 4279045389

本文发布于:2024-09-21 08:16:23,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/155454.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:像素   数据   函数   图像   返回   指针   复制   指定
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议