QRgba64 https://doc.qt.io/qt-5/qrgba64.html是 16 位(每个组件)颜色的正确选择。
另一种选择是检索颜色QImage::pixelColor() https://doc.qt.io/qt-5/qimage.html#pixelColor(并设置QImage::setPixelColor() https://doc.qt.io/qt-5/qimage.html#setPixelColor)这或多或少应该与深度无关。
功能qRgb() https://doc.qt.io/qt-5/qcolor.html#qRgb is a 错误的选择因为它有意处理 8 位(每个组件)颜色。
来自 Qt 文档:
QRgb QColor::qRgb(int r, int g, int b) https://doc.qt.io/qt-5/qcolor.html#qRgb
返回 ARGB 四元组 (255, r, g, b)。
alpha 值 255 给出了第一个提示,但检查结果类型QRgb https://doc.qt.io/qt-5/qcolor.html#QRgb-typedef使这一点显而易见:
typedef QColor::QRgb https://doc.qt.io/qt-5/qcolor.html#QRgb-typedef
格式为 #AARRGGBB 的 ARGB 四元组,相当于无符号整数。
看看woboq.org 上的源代码 https://code.woboq.org/qt5/qtbase/src/gui/painting/qrgb.h.html#_Z4qRgbiii支持这一点:
inline Q_DECL_CONSTEXPR QRgb qRgb(int r, int g, int b)// set RGB value
{ return (0xffu << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu); }
一个小样本来说明这一点:
#include <QtWidgets>
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
qDebug() << "qRgb(1 << 12, 1 << 12, 1 << 12):"
<< hex << qRgb(1 << 12, 1 << 12, 1 << 12);
qDebug() << "QColor(qRgb(1 << 12, 1 << 12, 1 << 12)):"
<< QColor(qRgb(1 << 12, 1 << 12, 1 << 12));
qDebug() << "QColor().fromRgba64(1 << 12, 1 << 12, 1 << 12):"
<< QColor().fromRgba64(1 << 12, 1 << 12, 1 << 12);
// done
return 0;
}
Output:
Qt Version: 5.11.2
qRgb(1 << 12, 1 << 12, 1 << 12): ff000000
QColor(qRgb(1 << 12, 1 << 12, 1 << 12)): QColor(ARGB 1, 0, 0, 0)
QColor().fromRgba64(1 << 12, 1 << 12, 1 << 12): QColor(ARGB 1, 0.062501, 0.062501, 0.062501)
使用的替代方法QColor
和同伴是写data
值直接转化为图像:
QImage image = Qimage(imagewidth, imageheight, QImage::Format_Grayscale16);
for (int j = 0; j < imageheight; ++j) {
quint16 *dst = (quint16*)(image.bits() + j * image.bytesPerLine());
for (int i = 0; i < imagewidth; ++i) {
dst[i] = data[i + j * imagewidth];
}
}
这肯定比转换更快、更准确data
值重新转换为灰度级的颜色。
请注意,我交换了行和列的循环。处理源中的连续字节(data
)和目的地(dst
)将提高缓存局部性并提高速度。
在撰写本文时,16 位深度的图像在 Qt 中还是相当新的。
Qt 5.12 中添加了每个组件 16 位深度的新颜色格式:
-
QImage::Format_RGBX64 = 25
图像使用 64 位半字顺序 RGB(x) 格式 (16-16-16-16) 存储。这与 Format_RGBX64 相同,只是 alpha 必须始终为 65535。(在 Qt 5.12 中添加)
-
QImage::Format_RGBA64 = 26
图像使用 64 位半字顺序 RGBA 格式 (16-16-16-16) 存储。 (Qt 5.12 中添加)
-
QImage::Format_RGBA64_Premultiplied = 27
图像使用预乘 64 位半字顺序 RGBA 格式 (16-16-16-16) 存储。 (Qt 5.12 中添加)
Qt 5.13中添加了16位深度的灰度:
-
QImage::Format_Grayscale16 = 28
图像使用 16 位灰度格式存储。 (Qt 5.13 中添加)
(文档复制自enum QImage::Format https://doc.qt.io/qt-5/qimage.html#Format-enum)
为了解决这个问题,我制作了一个示例应用程序,用于将每个组件 16 位 RGB 图像转换为 16 位灰度图像。
testQImageGray16.cc
:
#include <QtWidgets>
QImage imageToGray16(const QImage &qImg)
{
QImage qImgGray(qImg.width(), qImg.height(), QImage::Format_Grayscale16);
for (int y = 0; y < qImg.height(); ++y) {
for (int x = 0; x < qImg.width(); ++x) {
qImgGray.setPixelColor(x, y, qImg.pixelColor(x, y));
}
}
return qImgGray;
}
class Canvas: public QWidget {
private:
QImage _qImg;
public:
std::function<void(QPoint)> sigMouseMove;
public:
Canvas(QWidget *pQParent = nullptr):
QWidget(pQParent)
{
setMouseTracking(true);
}
Canvas(const QImage &qImg, QWidget *pQParent = nullptr):
QWidget(pQParent), _qImg(qImg)
{
setMouseTracking(true);
}
virtual ~Canvas() = default;
Canvas(const Canvas&) = delete;
Canvas& operator=(const Canvas&) = delete;
public:
virtual QSize sizeHint() const { return _qImg.size(); }
const QImage& image() const { return _qImg; }
void setImage(const QImage &qImg) { _qImg = qImg; update(); }
protected:
virtual void paintEvent(QPaintEvent *pQEvent) override;
virtual void mouseMoveEvent(QMouseEvent *pQEvent) override;
};
void Canvas::paintEvent(QPaintEvent *pQEvent)
{
QWidget::paintEvent(pQEvent);
QPainter qPainter(this);
qPainter.drawImage(0, 0, _qImg);
}
void Canvas::mouseMoveEvent(QMouseEvent *pQEvent)
{
if (sigMouseMove) sigMouseMove(pQEvent->pos());
}
QString getInfo(const QImage &qImg)
{
QString qStr;
QDebug(&qStr) << "Image Info:\n" << qImg;
for (int i = 0, len = qStr.length(); i < qStr.length(); ++i) {
if (qStr[i] == ',' && i + 1 < len && qStr[i + 1] != ' ') qStr[i] = '\n';
}
return qStr;
}
QString getPixelInfo(const QImage &qImg, QPoint pos)
{
if (!QRect(QPoint(0, 0), qImg.size()).contains(pos)) return QString();
const int bytes = (qImg.depth() + 7) / 8; assert(bytes > 0);
const QByteArray data(
(const char*)(qImg.bits()
+ pos.y() * qImg.bytesPerLine()
+ (pos.x() * qImg.depth() + 7) / 8),
bytes);
QString qStr;
QDebug(&qStr) << pos << ":" << qImg.pixelColor(pos)
<< "raw:" << QString("#") + data.toHex();
return qStr;
}
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// load sample data
const QImage qImgRGB16("pnggrad16rgb.png"/*, QImage::Format_RGBX64*/);
// setup GUI
QWidget winMain;
QGridLayout qGrid;
int col = 0, row = 0;
QLabel qLblRGBInfo(getInfo(qImgRGB16));
qGrid.addWidget(&qLblRGBInfo, row++, col);
Canvas qCanvasRGB(qImgRGB16);
qGrid.addWidget(&qCanvasRGB, row++, col);
QLabel qLblRGB;
qGrid.addWidget(&qLblRGB, row++, col);
row = 0; ++col;
Canvas qCanvasGray(qImgRGB16.convertToFormat(QImage::Format_Grayscale16));
QLabel qLblGrayInfo;
qGrid.addWidget(&qLblGrayInfo, row++, col);
qGrid.addWidget(&qCanvasGray, row++, col);
QLabel qLblGray;
qGrid.addWidget(&qLblGray, row++, col);
QHBoxLayout qHBoxQImageConvert;
QButtonGroup qBtnGrpQImageConvert;
QRadioButton qTglQImageConvertBuiltIn("Use QImage::convertToFormat()");
qBtnGrpQImageConvert.addButton(&qTglQImageConvertBuiltIn);
qTglQImageConvertBuiltIn.setChecked(true);
qHBoxQImageConvert.addWidget(&qTglQImageConvertBuiltIn);
QRadioButton qTglQImageConvertCustom("Use imageToGray16()");
qBtnGrpQImageConvert.addButton(&qTglQImageConvertCustom);
qHBoxQImageConvert.addWidget(&qTglQImageConvertCustom);
qGrid.addLayout(&qHBoxQImageConvert, row++, col);
winMain.setLayout(&qGrid);
winMain.show();
// install signal handlers
auto updatePixelInfo = [&](QPoint pos)
{
qLblRGB.setText (getPixelInfo(qCanvasRGB.image(), pos));
qLblGray.setText(getPixelInfo(qCanvasGray.image(), pos));
};
qCanvasRGB.sigMouseMove = updatePixelInfo;
qCanvasGray.sigMouseMove = updatePixelInfo;
auto updateGrayImage = [&](bool customConvert)
{
qCanvasGray.setImage(customConvert
? qImgRGB16.convertToFormat(QImage::Format_Grayscale16)
: imageToGray16(qImgRGB16));
qLblGrayInfo.setText(getInfo(qCanvasGray.image()));
qLblGray.setText(QString());
};
QObject::connect(&qTglQImageConvertBuiltIn, &QRadioButton::toggled,
[&](bool checked) { if (checked) updateGrayImage(false); });
QObject::connect(&qTglQImageConvertCustom, &QRadioButton::toggled,
[&](bool checked) { if (checked) updateGrayImage(true); });
// runtime loop
updateGrayImage(false);
return app.exec();
}
和 CMake 的构建脚本CMakeLists.txt
:
project(QImageGray16)
cmake_minimum_required(VERSION 3.10.0)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Qt5Widgets CONFIG REQUIRED)
include_directories("${CMAKE_SOURCE_DIR}")
add_executable(testQImageGray16
testQImageGray16.cc)
target_link_libraries(testQImageGray16
Qt5::Widgets)
# define QT_NO_KEYWORDS to prevent confusion between of Qt signal-slots and
# other signal-slot APIs
target_compile_definitions(testQImageGray16 PUBLIC QT_NO_KEYWORDS)
我下载了示例图片pnggrad16rgb.png https://www.fnordware.com/superpng/pnggrad16rgb.png from www.fnordware.com/superpng/samples.html https://www.fnordware.com/superpng/samples.html。 (其他示例图像可以在PNG“官方”测试套件 http://www.schaik.com/pngsuite/#basic.)
在VS2017中构建并运行后,我得到了以下快照:
当鼠标移动到显示的图像上时,底部标签显示图像中的当前位置以及相应的像素,如下所示QColor
和原始十六进制值。
出于好奇,我用两个嵌套循环实现了OP的方法(修复了qRgb()
issue):
QImage imageToGray16(const QImage &qImg)
{
QImage qImgGray(qImg.width(), qImg.height(), QImage::Format_Grayscale16);
for (int y = 0; y < qImg.height(); ++y) {
for (int x = 0; x < qImg.width(); ++x) {
qImgGray.setPixelColor(x, y, qImg.pixelColor(x, y));
}
}
return qImgGray;
}
将结果与QImage::convertToFormat() https://doc.qt.io/qt-5/qimage.html#convertToFormat我在 Qt 文档中找到了它。
返回给定格式的图像副本。
指定的图像转换标志控制在转换过程中如何处理图像数据。
那很有意思:
There is no visible difference. Though, this might not be a surprise considering that the display may reduce color depth to 8 or 10 (beside of the fact that humans are probably not able to differentiate 216 shades of gray nor 2163 RGB values.
然而擦拭图像后,我意识到QImage::convertToFormat() https://doc.qt.io/qt-5/qimage.html#convertToFormat,每个像素的第一个和第二个字节始终相同(例如b2b2
),自定义转换时情况并非如此imageToGray16()
用来。
我没有深入挖掘,但可能值得进一步研究这些方法中哪种方法实际上更准确。