QImage 类提供独立于硬件的图像表示 (允许直接访问像素数据,且可以用作描绘设备)。

QImage 是为 I/O 和直接像素访问和操作而设计和优化的。

因为 QImage 是 QPaintDevice 的子类,所以 QPainter 可用于直接在图像上绘图。在 QImage 上使用 QPainter 时,可以在当前 GUI 线程之外的另一个线程中执行绘画。

QImage 类支持由 Format 枚举描述的几种图像格式。这些包括单色、8 位、32 位和 alpha 混合图像。

QImage 提供了一组函数,可用于获取有关图像的各种信息。还有几个函数可以转换图像。

QImage 对象可以通过值传递,因为 QImage 类使用隐式数据共享。 QImage 对象也可以流式传输和比较。

1.QImage(const QSize &size, Format format);

QImage::QImage(const QSize &size, Format format): QPaintDevice()
{d = QImageData::create(size, format);
}QImageData * QImageData::create(const QSize &size, QImage::Format format)
{if (size.isEmpty() || format <= QImage::Format_Invalid || format >= QImage::NImageFormats)return nullptr;                             // invalid parameter(s)Q_TRACE_SCOPE(QImageData_create, size, format);int width = size.width();int height = size.height();int depth = qt_depthForFormat(format);auto params = calculateImageParameters(width, height, depth);if (!params.isValid())return nullptr;QScopedPointer<QImageData> d(new QImageData);switch (format) {case QImage::Format_Mono:case QImage::Format_MonoLSB:d->colortable.resize(2);d->colortable[0] = QColor(Qt::black).rgba();d->colortable[1] = QColor(Qt::white).rgba();break;default:break;}d->width = width;d->height = height;d->depth = depth;d->format = format;d->has_alpha_clut = false;d->is_cached = false;d->bytes_per_line = params.bytesPerLine;d->nbytes = params.totalSize;d->data  = (uchar *)malloc(d->nbytes);if (!d->data)return nullptr;d->ref.ref();return d.take();
}QImageData::calculateImageParameters(qsizetype width, qsizetype height, qsizetype depth)
{ImageSizeParameters invalid = { -1, -1 };if (height <= 0)return invalid;// calculate the size, taking care of overflowsqsizetype bytes_per_line;if (mul_overflow(width, depth, &bytes_per_line))return invalid;if (add_overflow(bytes_per_line, qsizetype(31), &bytes_per_line))return invalid;// bytes per scanline (must be multiple of 4)bytes_per_line = (bytes_per_line >> 5) << 2;    // can't overflowqsizetype total_size;if (mul_overflow(height, bytes_per_line, &total_size))return invalid;qsizetype dummy;if (mul_overflow(height, qsizetype(sizeof(uchar *)), &dummy))return invalid;                                 // why is this here?
#if 1 || QT_VERSION < QT_VERSION_CHECK(6,0,0) // ### can only fix this if QImage dimensions are not int anymore// Disallow images where width * depth calculations might overflowif (width > (INT_MAX - 31) / depth)return invalid;
#endifreturn { bytes_per_line, total_size };
}

根据Size和Format初始化私有变量d.

调用calculateImageParameters函数计算出所需的内存大小,用malloc函数申请内存空间。

2.QSize size() const;

QSize QImage::size() const
{return d ? QSize(d->width, d->height) : QSize(0, 0);
}

3.int width() const;

int QImage::width() const
{return d ? d->width : 0;
}

4.int height() const;

int QImage::height() const
{return d ? d->height : 0;
}

5.int dotsPerMeterX() const;

int QImage::dotsPerMeterX() const
{return d ? qRound(d->dpmx) : 0;
}

6.int dotsPerMeterY() const;

int QImage::dotsPerMeterY() const
{return d ? qRound(d->dpmy) : 0;
}

7.QRect rect() const;

QRect QImage::rect() const
{return d ? QRect(0, 0, d->width, d->height) : QRect();
}

8.bool valid(int x, int y) const;

/*!\overloadReturns \c true if QPoint(\a x, \a y) is a valid coordinate pairwithin the image; otherwise returns \c false.
*/
bool QImage::valid(int x, int y) const
{return d&& x >= 0 && x < d->width&& y >= 0 && y < d->height;
}

9.QPoint offset() const;

/*!\fn QPoint QImage::offset() constReturns the number of pixels by which the image is intended to beoffset by when positioning relative to other images.\sa setOffset(), {QImage#Image Information}{Image Information}
*/
QPoint QImage::offset() const
{return d ? d->offset : QPoint();
}

10.void setOffset(const QPoint&);

void QImage::setOffset(const QPoint& p)
{if (!d)return;detach();if (d)d->offset = p;
}

void QImage::detach()
{if (d) {if (d->is_cached && d->ref.loadRelaxed() == 1)QImagePixmapCleanupHooks::executeImageHooks(cacheKey());if (d->ref.loadRelaxed() != 1 || d->ro_data)*this = copy();if (d)++d->detach_no;}
}

先解除绑定,再设置新值。

Qt源码分析--QImage(1)相关推荐

  1. Qt源码分析--QImage(8)

    1.void setDotsPerMeterX(int); 设置每米有多少个像素 /*!Sets the number of pixels that fit horizontally in a phy ...

  2. Qt源码分析之QObject

    Qt的QObject 1.试验代码: #include <QApplication> #include <QtCore> #include <QtGui> int ...

  3. Qt源码分析之信号和槽机制

    Qt的信号和槽机制是Qt的一大特点,实际上这是和MFC中的消息映射机制相似的东西,要完成的事情也差不多,就是发送一个消息然后让其它窗口响应,当然,这里的消息是广义的 说法,简单点说就是如何在一个类的一 ...

  4. Qt源码分析--QSettings

    定义: QSettings 类提供与平台无关的持久应用程序设置. 官方例子: QSettings settings("MySoft", "Star Runner" ...

  5. 【Qt】Log4Qt(三)源码分析

    Log4Qt(三)源码分析 1.分层架构 1.1 核心对象 1.2 支持对象 2.源码分析 2.1 宏 2.1.1 LOG4QT_DECLARE_QCLASS_LOGGER 2.1.2 LOG4QT_ ...

  6. Qt 事件机制源码分析 QApplication exec 源码分析 多图超级详细

    前言: 不熟悉qt 源码结构的 可以先看这一篇 点我点我点我 写qt 的都知道 以下代码, 这段代码究竟的运行机制是怎么样的 咱们一步一步的看 QApplication a(argc, argv);Q ...

  7. Solr初始化源码分析-Solr初始化与启动

    用solr做项目已经有一年有余,但都是使用层面,只是利用solr现有机制,修改参数,然后监控调优,从没有对solr进行源码级别的研究.但是,最近手头的一个项目,让我感觉必须把solrn内部原理和扩展机 ...

  8. 14.QueuedConnection和BlockingQueuedConnection连接方式源码分析

    QT信号槽直连时的时序和信号槽的连接方式已经在前面的文章中分析过了,见https://blog.csdn.net/Master_Cui/article/details/109011425和https: ...

  9. sigslot库源码分析

    言归正传,sigslot是一个用标准C++语法实现的信号与槽机制的函数库,类型和线程安全.提到信号与槽机制,恐怕最容易想到的就是大名鼎鼎的Qt所支持的对象之间通信的模式吧.不过这里的信号与槽虽然在概念 ...

最新文章

  1. sublime php 乱码,sublime打开TXT文件乱码的问题
  2. python测试代码怎么写_Python 单元测试
  3. Tomcat学习过程
  4. XenServer中Windows 7与XP多vCPU支持配置
  5. 【PAT乙级】1039 到底买不买 (20 分)
  6. AtCoder Regular Contest 082
  7. vue 日期格式化返回指定个数月份_12、vue中日期格式化转换的函数
  8. 整合 centos安装python的介绍
  9. 四种浏览器对 clientHeight、offsetHeight、scrollHeight、clientWidth、offsetWidth 和 scrollWidth 的解释差异...
  10. 1.3计算机硬件的主要指标
  11. Web前端 — Bootstrap(2)
  12. 将txt格式的地图导入到ArcMap
  13. png能转换成html吗,png转化成pdf
  14. 用计算机实测技术研究声波和拍内容,基础物理实验/面向21世纪课程教材
  15. 如何实现一个安全的Web登陆
  16. flask web开发 Set it to True to suppress this warning问题
  17. 20200413 jzoj 普及c组 Loan Repayment
  18. Python自动发邮件
  19. 卡尔曼滤波(KF)无迹卡尔曼滤波(UKF)
  20. 哈希表、红黑树、B树、B+树基础

热门文章

  1. Navicat-数据库的连接以及使用
  2. 搜索框输入内容进行查找匹配
  3. python中------decode解码出现的0xca问题解决方法
  4. 字符型指针,数组,字符串赋值
  5. k图着色 局部搜索算法与模拟退火算法的python实现
  6. 【PTA】【C语言】N个数求和
  7. GeoHash算法获取附近店铺和距离
  8. c++头文件iomanip.h中的setw、setprecision、setfill和setbase函数的使用
  9. Java-append()方法
  10. 华为手机体验鸿蒙系统,再过40天,你就能在华为手机上,体验到鸿蒙系统了? - 区块网...