QtPainterModule.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "QtPainterModule.h"
  2. #include <libyuv.h>
  3. #include <mutex>
  4. //#include "..//360StitchingModule/SetParam.h"
  5. //数据4对齐
  6. #define ALIGN_4(x) (((x) + 3) & ~3)
  7. //数据2对齐
  8. #define ALIGN_2(x) (((x) + 1) & ~1)
  9. QtPainterModule::QtPainterModule()
  10. {
  11. DrawBoard = NULL;
  12. OrginImg = NULL;
  13. DrawPainter = NULL;
  14. EditScaleOrNot = true;
  15. m_BackGroundColor = COLORREF(64);
  16. Scale = 1;
  17. ImgRect = MfcLabel::fRect(0, 0, 0, 0);
  18. ClientRect = MfcLabel::fRect(0, 0, 0, 0);
  19. CurImageRect = MfcLabel::fRect(0, 0, 0, 0);
  20. ShowClientRect = MfcLabel::fRect(0, 0, 0, 0);
  21. ShowImageRect = MfcLabel::fRect(0, 0, 0, 0);
  22. OutputOffsetX = &Offset.x;
  23. OutputOffsetY = &Offset.y;
  24. bYUVShowOrNot = false;
  25. pYUVData = NULL;
  26. pYUVImage.reset();
  27. pClipImage.reset();
  28. }
  29. QtPainterModule::~QtPainterModule()
  30. {
  31. SAFE_DELETE(DrawPainter);
  32. }
  33. void QtPainterModule::SetLabelRect(QRect pQRect)
  34. {
  35. ClientRect = pQRect;
  36. }
  37. void QtPainterModule::SetPixmap(QPixmap* pDrawBoard)
  38. {
  39. DrawBoard = pDrawBoard;
  40. if (DrawPainter == NULL)
  41. DrawPainter = new QPainter(DrawBoard);
  42. else
  43. {
  44. SAFE_DELETE(DrawPainter);
  45. DrawPainter = new QPainter(DrawBoard);
  46. }
  47. }
  48. void QtPainterModule::SetOrginImage(QImage* pImage)
  49. {
  50. OrginImg = pImage;
  51. ImgRect = OrginImg->rect();
  52. }
  53. void QtPainterModule::SetOrginImage(QByteArray* pYuvData, int nWidth, int nHeight)
  54. {
  55. //设置初值
  56. this->pYUVData = pYuvData;
  57. this->nYUVImageWidth = nWidth;
  58. this->nYUVImageHeight = nHeight;
  59. }
  60. void QtPainterModule::SetPainter(QPainter* pPainter)
  61. {
  62. DrawPainter = pPainter;
  63. }
  64. void QtPainterModule::SetYUVShowOrNot(bool bYUVShowOrNot)
  65. {
  66. this->bYUVShowOrNot = bYUVShowOrNot;
  67. }
  68. void QtPainterModule::init()
  69. {
  70. //初始化全部的显示参数
  71. Scale = 1.f;
  72. Offset.SetPoint(0, 0);
  73. }
  74. void QtPainterModule::CalculateMouseDragOffset(QPoint ptOldMouse, QPoint ptNewMouse)
  75. {
  76. MfcLabel::fPoint ptOffset;
  77. ptOffset.x = (ptNewMouse.x() - ptOldMouse.x());
  78. ptOffset.y = (ptNewMouse.y() - ptOldMouse.y());
  79. Offset.offset(ptOffset.x, ptOffset.y);
  80. CurImageRect.offset(ptOffset.x, ptOffset.y);
  81. }
  82. void QtPainterModule::CalculateMouseDragOffset(int Offset_x, int Offset_y)
  83. {
  84. Offset.offset(Offset_x, Offset_y);
  85. CurImageRect.offset(Offset_x, Offset_y);
  86. }
  87. void QtPainterModule::CalculateWheelZoomOffsetAndScale(QPoint ptMouse, bool bEnlargeOrNarrow, double dbScale)
  88. {
  89. CurImageRect = ImgRect;
  90. CurImageRect.Scale(Scale);
  91. CurImageRect.offset(Offset.x, Offset.y);
  92. if (!bEnlargeOrNarrow)
  93. {
  94. Scale /= 2;
  95. QPoint topleft;
  96. topleft.setX(CurImageRect.Left);
  97. topleft.setY(CurImageRect.Top);
  98. int dx = ptMouse.x() - topleft.x();
  99. int dy = ptMouse.y() - topleft.y();
  100. Offset.offset(0.5 * (double)dx, 0.5 * (double)dy);
  101. }
  102. else
  103. {
  104. Scale *= 2;
  105. QPoint topleft;
  106. topleft.setX(CurImageRect.Left);
  107. topleft.setY(CurImageRect.Top);
  108. int dx = ptMouse.x() - topleft.x();
  109. int dy = ptMouse.y() - topleft.y();
  110. Offset.offset(-dx, -dy);
  111. }
  112. }
  113. bool QtPainterModule::CalculateShowRectInDC(QPoint ptOffset, double dbScale)
  114. {
  115. //计算当前预备显示的图像位置
  116. ShowClientRect = CurImageRect.IntersectRect(ClientRect);
  117. ShowImageRect.SetRectEmpty();
  118. //计算最终的图片将要复制到显示区域的Rect
  119. if (CurImageRect.Bottom > ClientRect.Bottom)
  120. {
  121. ShowImageRect.Bottom = ClientRect.Height() - CurImageRect.Top;
  122. }
  123. else if (CurImageRect.Bottom <= ClientRect.Bottom)
  124. {
  125. ShowImageRect.Bottom = CurImageRect.Height();
  126. }
  127. if (CurImageRect.Right > ClientRect.Right)
  128. {
  129. ShowImageRect.Right = ClientRect.Width() - CurImageRect.Left;
  130. }
  131. else if (CurImageRect.Right <= ClientRect.Right)
  132. {
  133. ShowImageRect.Right = CurImageRect.Width();
  134. }
  135. if (CurImageRect.Top < 0)
  136. {
  137. ShowImageRect.Top = -CurImageRect.Top;
  138. }
  139. else if (CurImageRect.Top > ClientRect.Bottom)
  140. {
  141. return false;
  142. }
  143. if (CurImageRect.Left < 0)
  144. {
  145. ShowImageRect.Left = -CurImageRect.Left;
  146. }
  147. else if (CurImageRect.Left > ClientRect.Right)
  148. {
  149. return false;
  150. }
  151. ShowImageRect.Left = ShowImageRect.Left / Scale;
  152. ShowImageRect.Top = ShowImageRect.Top / Scale;
  153. ShowImageRect.Right = ShowImageRect.Right / Scale;
  154. ShowImageRect.Bottom = ShowImageRect.Bottom / Scale;
  155. return true;
  156. }
  157. void QtPainterModule::PasteImageInDC()
  158. {
  159. if (EditScaleOrNot)
  160. {
  161. DrawPainter->setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing | QPainter::TextAntialiasing);
  162. //首先将数据转入到画板之中
  163. MfcLabel::fRect test;
  164. test = OrginImg->rect();
  165. *DrawBoard = (QPixmap::fromImage(*OrginImg));
  166. *DrawBoard = DrawBoard->copy(ShowImageRect.TrunFloat2Int());
  167. //缩放
  168. *DrawBoard = DrawBoard->scaled(ShowClientRect.TrunFloat2Int().size());
  169. EditScaleOrNot = false;
  170. }
  171. OutputPaintRect = ShowImageRect.TrunFloat2Int();
  172. }
  173. void QtPainterModule::ShowInFitMode(void* pCallBack)
  174. {
  175. //坐标需要转换位double,用于计算比例
  176. //fit模式的比例计算
  177. double nImage_width, nImage_height;
  178. nImage_height = ImgRect.Height();
  179. nImage_width = ImgRect.Width();
  180. double rect_height = 0, rect_width = 0;
  181. rect_height = ClientRect.Height();
  182. rect_width = ClientRect.Width();
  183. //fit的情况下的初始坐标为0,0
  184. Offset.x = 0;
  185. Offset.y = 0;
  186. //计算放缩倍数,保证fit形式的
  187. if ((rect_height / rect_width) != (nImage_height / nImage_width))
  188. {
  189. if (nImage_height / nImage_width > rect_height / rect_width)
  190. {
  191. Scale = rect_height / nImage_height;
  192. FitUseWidthOrHeight = false;
  193. }
  194. else
  195. {
  196. Scale = rect_width / nImage_width;
  197. FitUseWidthOrHeight = true;
  198. }
  199. }
  200. CurImageRect = ImgRect;
  201. CurImageRect.offset(Offset.x, Offset.y);
  202. CurImageRect.Scale(Scale);
  203. CalculateShowRectInDC(Offset.TurnFloatToInt(), Scale);
  204. //完成绘制
  205. if(this->bYUVShowOrNot == false)
  206. PasteImageInDC();
  207. else
  208. PasteYUVImageInDC();
  209. if (pCallBack != NULL)
  210. {
  211. ((ShowPictureInQPixMapCallBack)(pCallBack))(DrawBoard, Scale, Offset.TurnFloatToInt(), pCallBackParam);
  212. }
  213. }
  214. void QtPainterModule::ShowInOneToOneMode(void* pCallBack)
  215. {
  216. //坐标需要转换位double,用于计算比例
  217. //fit模式的比例计算
  218. double nImage_width, nImage_height;
  219. nImage_height = ImgRect.Height();
  220. nImage_width = ImgRect.Width();
  221. double rect_height = 0, rect_width = 0;
  222. rect_height = ClientRect.Height();
  223. rect_width = ClientRect.Width();
  224. //计算放缩倍数,保证fit形式的
  225. Scale = 1;
  226. CurImageRect = ImgRect;
  227. CurImageRect.offset(Offset.x, Offset.y);
  228. CurImageRect.Scale(Scale);
  229. CalculateShowRectInDC(Offset.TurnFloatToInt(), Scale);
  230. //完成绘制
  231. if (this->bYUVShowOrNot == false)
  232. PasteImageInDC();
  233. else
  234. PasteYUVImageInDC();
  235. if (pCallBack != NULL)
  236. {
  237. ((ShowPictureInQPixMapCallBack)(pCallBack))(DrawBoard, Scale, Offset.TurnFloatToInt(), pCallBackParam);
  238. }
  239. }
  240. void QtPainterModule::ShowInFreeMode(void* pCallBack)
  241. {
  242. CurImageRect = ImgRect;
  243. CurImageRect.Scale(Scale);
  244. CurImageRect.offset(Offset.x, Offset.y);
  245. CalculateShowRectInDC(Offset.TurnFloatToInt(), Scale);
  246. //完成绘制,需要配置dbscale 和 offset两个参数
  247. PasteImageInDC();
  248. if (pCallBack != NULL)
  249. {
  250. ((ShowPictureInQPixMapCallBack)(pCallBack))(DrawBoard, Scale, Offset.TurnFloatToInt(), pCallBackParam);
  251. }
  252. }
  253. void QtPainterModule::PasteYUVImageInDC()
  254. {
  255. if (EditScaleOrNot)
  256. {
  257. //需要将yuv根据指定的显示区域进行裁剪
  258. //首先根据显示区域计算出来的大小给pYUVImage分配空间
  259. //清空指针内的内容
  260. if (pYUVImage.get() != NULL)
  261. std::shared_ptr<unsigned char>().swap(pYUVImage);
  262. if (pClipImage.get() != NULL)
  263. std::shared_ptr<unsigned char>().swap(pClipImage);
  264. ShowClientRect.Right = ALIGN_2(int(ShowClientRect.Right));
  265. ShowClientRect.Bottom = ALIGN_2(int(ShowClientRect.Bottom));
  266. //设置dest区域和src区域
  267. QRect DestRc, SrcRc;
  268. //确定裁剪的区域
  269. DestRc = ShowImageRect.TrunFloat2Int();
  270. SrcRc = ShowClientRect.TrunFloat2Int();
  271. //分配裁剪的空间
  272. pClipImage.reset(new unsigned char[DestRc.width() * DestRc.height() * 3/2] {0});
  273. //分配显示的空间
  274. pYUVImage.reset(new unsigned char[SrcRc.width() * SrcRc.height() * 3/2 ] {0});
  275. //设置原图大小
  276. SrcRc.setWidth(nYUVImageWidth); SrcRc.setHeight(nYUVImageHeight);
  277. //开始裁剪
  278. ClipYUVImageByLibYUV((unsigned char*)pYUVData->data(), SrcRc, pClipImage.get(), DestRc);
  279. //重新设置原图大小
  280. SrcRc.setWidth(DestRc.width()); SrcRc.setHeight(DestRc.height());
  281. //重新设置dst区域大小
  282. DestRc.setWidth(ShowClientRect.Width()); DestRc.setHeight(ShowClientRect.Height());
  283. //开始缩放
  284. ScaleYUVImageByLibYUV((unsigned char*)pClipImage.get(), SrcRc, pYUVImage.get(), DestRc);
  285. //得到当前显示区域的长宽
  286. nYUVShowImageHeight = DestRc.height();
  287. nYUVShowImageWidth = DestRc.width();
  288. }
  289. OutputPaintRect = ShowImageRect.TrunFloat2Int();
  290. }
  291. void QtPainterModule::Draw(ShowMode Flags, void* pCallBack,void* pParam)
  292. {
  293. if ((!bYUVShowOrNot || pYUVData == NULL)
  294. && (OrginImg == NULL || DrawBoard == NULL || DrawPainter == NULL) )
  295. return;
  296. //类内的接住回调的参数
  297. pCallBackParam = pParam;
  298. switch (Flags)
  299. {
  300. case Fit:
  301. ShowInFitMode(pCallBack);
  302. break;
  303. case OneToOne:
  304. ShowInOneToOneMode(pCallBack);
  305. break;
  306. case Free:
  307. ShowInFreeMode(pCallBack);
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. bool QtPainterModule::ClipYUVImageByLibYUV(unsigned char* pSrcImage, QRect ClipSrcRc, unsigned char* pDstImage, QRect ClipDstRc)
  314. {
  315. //检查ClipRect是否在pSrcImage之内的工作在外面完成
  316. //检查ClipRect是否为偶数,yuv坐标必须为偶数,如果不为偶数无法定位uv的具体坐标
  317. if (ClipSrcRc.left() % 2 != 0 || ClipSrcRc.top() % 2 != 0)
  318. return false;
  319. //检查输入的两个地址是否为合法地址
  320. if (pSrcImage == NULL || pDstImage == NULL)
  321. return false;
  322. //计算yuv各个数据的起始地址
  323. unsigned char* pSrcY = pSrcImage;
  324. unsigned char* pSrcU = pSrcImage + ClipSrcRc.width() * ClipSrcRc.height();
  325. unsigned char* pSrcV = pSrcImage + ClipSrcRc.width() * ClipSrcRc.height() * 5 / 4;
  326. unsigned char* pDstY = pDstImage;
  327. unsigned char* pDstU = pDstImage + ClipDstRc.width() * ClipDstRc.height();
  328. unsigned char* pDstV = pDstImage + ClipDstRc.width() * ClipDstRc.height() * 5 / 4;
  329. if (ClipDstRc.width() == ClipSrcRc.width() && ClipDstRc.height() == ClipSrcRc.height())
  330. {
  331. libyuv::I420Copy(
  332. pSrcY, ClipSrcRc.width(),
  333. pSrcU, ClipSrcRc.width() >> 1,
  334. pSrcV, ClipSrcRc.width() >> 1,
  335. pDstY, ClipDstRc.width(),
  336. pDstU, ClipDstRc.width() >> 1,
  337. pDstV, ClipDstRc.width() >> 1,
  338. ClipSrcRc.width(),
  339. ClipSrcRc.height()
  340. );
  341. return 0;
  342. }
  343. else
  344. //调用libyuv的函数进行裁剪
  345. return libyuv::ConvertToI420(pSrcY, ClipSrcRc.width(),
  346. pDstY, ClipDstRc.width() ,
  347. pDstU, ClipSrcRc.width() >> 1,
  348. pDstV, ClipDstRc.width() >> 1,
  349. ClipDstRc.left(), ClipDstRc.top(),
  350. ClipSrcRc.width(), ClipSrcRc.height(),
  351. ClipDstRc.width(), ClipDstRc.height(),
  352. libyuv::kRotate0, libyuv::FOURCC_I420);
  353. }
  354. bool QtPainterModule::ScaleYUVImageByLibYUV(unsigned char* pSrcImage, QRect ClipRect, unsigned char* pDstImage, QRect ClipDstRc)
  355. {
  356. //首先判断一下目标数据的宽度是否为偶数
  357. if (ClipDstRc.width() % 2 != 0 || ClipDstRc.height() % 2 != 0)
  358. return false;
  359. //检查输入的两个地址是否为合法地址
  360. if (pSrcImage == NULL || pDstImage == NULL)
  361. return false;
  362. //计算yuv各个数据的起始地址
  363. unsigned char* pSrcY = pSrcImage;
  364. unsigned char* pSrcU = pSrcImage + ClipRect.width() * ClipRect.height();
  365. unsigned char* pSrcV = pSrcImage + ClipRect.width() * ClipRect.height() * 5 / 4;
  366. unsigned char* pDstY = pDstImage;
  367. unsigned char* pDstU = pDstImage + ClipDstRc.width() * ClipDstRc.height();
  368. unsigned char* pDstV = pDstU + ClipDstRc.width() * ClipDstRc.height() / 4;
  369. //调用libyuv的函数进行图像的缩放
  370. return libyuv::I420Scale(pSrcY, ClipRect.width()
  371. , pSrcU, ClipRect.width() >> 1
  372. , pSrcV, ClipRect.width() >> 1,
  373. ClipRect.width(), ClipRect.height()
  374. , pDstY, ClipDstRc.width()
  375. , pDstU, ClipDstRc.width() >> 1
  376. , pDstV, ClipDstRc.width() >> 1
  377. ,ClipDstRc.width(), ClipDstRc.height()
  378. ,libyuv::kFilterBilinear);
  379. }