RgaColorTransfer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "RgaColorTransfer.h"
  2. #include "../LogRecorder/LogOutput.h"
  3. // static member initialization
  4. RgaColorTransfer* RgaColorTransfer::m_instance = nullptr;
  5. RgaColorTransfer::RgaColorTransfer()
  6. {
  7. m_Src = new RGA_BUFFER;
  8. m_Dst = new RGA_BUFFER;
  9. }
  10. RgaColorTransfer::~RgaColorTransfer()
  11. {
  12. ReleaseResource();
  13. }
  14. bool RgaColorTransfer::copyBuffer(cv::Rect srcRect, cv::Rect dstRect)
  15. {
  16. if (!m_Src || !m_Dst)
  17. return false;
  18. int ret = 0;
  19. im_rect srcRectRga = { srcRect.x, srcRect.y, srcRect.width, srcRect.height };
  20. im_rect dstRectRga = { dstRect.x, dstRect.y, dstRect.width, dstRect.height };
  21. ret = imcheck(m_Src->SrcImg, m_Dst->SrcImg, srcRectRga, dstRectRga);
  22. if (ret != 0)
  23. {
  24. printf("imcheck failed, ret = %d\n", ret);
  25. return false;
  26. }
  27. // Use improcess to perform the copy operation between the source and destination buffers.
  28. ret = improcess(m_Src->SrcImg,m_Dst->SrcImg,{},srcRectRga,dstRectRga,{},IM_ASYNC);
  29. if (ret != 0)
  30. {
  31. printf("improcess failed, ret = %d\n", ret);
  32. return false;
  33. }
  34. return true;
  35. }
  36. bool RgaColorTransfer::transfer(int nType)
  37. {
  38. if (!m_Src || !m_Dst)
  39. return false;
  40. int ret = 0;
  41. ret = imcvtcolor(m_Src->SrcImg, m_Dst->SrcImg,m_Src->nType,m_Dst->nType);
  42. if (ret != 0)
  43. {
  44. printf("imcvtcolor failed, ret = %d\n", ret);
  45. return false;
  46. }
  47. return true;
  48. }
  49. bool RgaColorTransfer::resizeImage(cv::Rect srcRect, cv::Rect dstRect)
  50. {
  51. if(!m_Src || !m_Dst)
  52. return false;
  53. if(m_Src->nWidth == 0 || m_Src->nHeight == 0 || m_Dst->nWidth == 0 || m_Dst->nHeight == 0)
  54. return false;
  55. if(m_Src->nWidth == m_Dst->nWidth && m_Src->nHeight == m_Dst->nHeight)
  56. return true;
  57. int ret = 0;
  58. double fx = (double)dstRect.width / (double)srcRect.width;
  59. double fy = (double)dstRect.height / (double)srcRect.height;
  60. ret = imresize(m_Src->SrcImg, m_Dst->SrcImg,fx,fy,INTER_LINEAR,IM_SYNC);
  61. if(ret != 0)
  62. {
  63. printf("imresize failed, ret = %d\n", ret);
  64. return false;
  65. }
  66. return true;
  67. }
  68. void RgaColorTransfer::ReleaseResource()
  69. {
  70. if (m_Src)
  71. {
  72. m_Src->ReleaseResource();
  73. delete m_Src;
  74. m_Src = nullptr;
  75. }
  76. if (m_Dst)
  77. {
  78. m_Dst->ReleaseResource();
  79. delete m_Dst;
  80. m_Dst = nullptr;
  81. }
  82. }
  83. int resize_image(void * src_buf, int src_width, int src_height, void * dst_buf, int dst_width, int dst_height)
  84. {
  85. // 参数验证
  86. if (!src_buf || !dst_buf) {
  87. LOG_ERROR("resize_image: 无效的缓冲区指针");
  88. return -1;
  89. }
  90. if (src_width <= 0 || src_height <= 0 || dst_width <= 0 || dst_height <= 0) {
  91. LOG_ERROR("resize_image: 无效的图像尺寸 - src:{}x{}, dst:{}x{}",
  92. src_width, src_height, dst_width, dst_height);
  93. return -2;
  94. }
  95. int ret;
  96. rga_info_t src_info;
  97. rga_info_t dst_info;
  98. // Initialize source image information
  99. memset(&src_info, 0, sizeof(rga_info_t));
  100. src_info.fd = -1;
  101. src_info.virAddr = src_buf;
  102. src_info.mmuFlag = 1;
  103. rga_set_rect(&src_info.rect, 0, 0, src_width, src_height, src_width, src_height, RK_FORMAT_RGB_888);
  104. // Initialize destination image information
  105. memset(&dst_info, 0, sizeof(rga_info_t));
  106. dst_info.fd = -1;
  107. dst_info.virAddr = dst_buf;
  108. dst_info.mmuFlag = 1;
  109. rga_set_rect(&dst_info.rect, 0, 0, dst_width, dst_height, dst_width, dst_height, RK_FORMAT_RGB_888);
  110. try{
  111. // Perform the resize operation
  112. // 执行缩放操作
  113. ret = c_RkRgaBlit(&src_info, &dst_info, NULL);
  114. if (ret) {
  115. LOG_ERROR("resize_image: RGA缩放失败, ret = {}", ret);
  116. // 尝试重试一次
  117. LOG_INFO("resize_image: 正在尝试重新执行RGA缩放");
  118. ret = c_RkRgaBlit(&src_info, &dst_info, NULL);
  119. if (ret) {
  120. LOG_ERROR("resize_image: RGA缩放重试仍然失败, ret = {}", ret);
  121. return ret;
  122. } else {
  123. LOG_INFO("resize_image: RGA缩放重试成功");
  124. }
  125. }
  126. }catch (const std::exception& e) {
  127. LOG_ERROR("resize_image: 异常: {}", e.what());
  128. return -100;
  129. } catch (...) {
  130. LOG_ERROR("resize_image: 发生未知异常");
  131. return -101;
  132. }
  133. return 0;
  134. }