ToB企服应用市场:ToB评测及商务社交产业平台

标题: Qt 的QImage 像素操作 [打印本页]

作者: 农民    时间: 2025-2-16 16:38
标题: Qt 的QImage 像素操作
QRgb QImage::pixel(const QPoint &position) const
Returns the color of the pixel at the given position.
If the position is not valid, the results are undefined.
Warning: This function is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read.
See also setPixel(), valid(), constBits(), constScanLine(), and Pixel Manipulation.
    ⚠️ 告诫:性能问题

  "Warning: This function is expensive when used for massive pixel manipulations."
  ⚠️ (告诫: 该函数在大规模像素操作时代价昂贵)
  为什么 pixel(x, y) 慢?

  
  
   如何优化?

  
  1. const uchar *data = image.constBits();
  2. int bytesPerLine = image.bytesPerLine();
  3. for (int y = 0; y < image.height(); ++y) {
  4.     const QRgb *line = reinterpret_cast<const QRgb *>(data + y * bytesPerLine);
  5.     for (int x = 0; x < image.width(); ++x) {
  6.         QRgb pixelColor = line[x];  // 直接访问像素
  7.         int r = qRed(pixelColor);
  8.         int g = qGreen(pixelColor);
  9.         int b = qBlue(pixelColor);
  10.     }
  11. }
复制代码
 这样访问像素比 pixel(x, y) 快许多,因为它直接操作数据缓冲区,避免了函数调用开销。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4