RgaColorTransfer.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "RgaColorTransfer.h"
  2. // static member initialization
  3. RgaColorTransfer* RgaColorTransfer::m_instance = nullptr;
  4. RgaColorTransfer::RgaColorTransfer()
  5. {
  6. m_Src = new RGA_BUFFER;
  7. m_Dst = new RGA_BUFFER;
  8. }
  9. RgaColorTransfer::~RgaColorTransfer()
  10. {
  11. ReleaseResource();
  12. }
  13. bool RgaColorTransfer::copyBuffer(cv::Rect srcRect, cv::Rect dstRect)
  14. {
  15. if (!m_Src || !m_Dst)
  16. return false;
  17. int ret = 0;
  18. im_rect srcRectRga = { srcRect.x, srcRect.y, srcRect.width, srcRect.height };
  19. im_rect dstRectRga = { dstRect.x, dstRect.y, dstRect.width, dstRect.height };
  20. ret = imcheck(m_Src->SrcImg, m_Dst->SrcImg, srcRectRga, dstRectRga);
  21. if (ret != 0)
  22. {
  23. printf("imcheck failed, ret = %d\n", ret);
  24. return false;
  25. }
  26. // Use improcess to perform the copy operation between the source and destination buffers.
  27. ret = improcess(m_Src->SrcImg,m_Dst->SrcImg,{},srcRectRga,dstRectRga,{},IM_ASYNC);
  28. if (ret != 0)
  29. {
  30. printf("improcess failed, ret = %d\n", ret);
  31. return false;
  32. }
  33. return true;
  34. }
  35. bool RgaColorTransfer::transfer(int nType)
  36. {
  37. if (!m_Src || !m_Dst)
  38. return false;
  39. int ret = 0;
  40. ret = imcvtcolor(m_Src->SrcImg, m_Dst->SrcImg,m_Src->nType,m_Dst->nType);
  41. if (ret != 0)
  42. {
  43. printf("imcvtcolor failed, ret = %d\n", ret);
  44. return false;
  45. }
  46. return true;
  47. }
  48. bool RgaColorTransfer::resizeImage(cv::Rect srcRect, cv::Rect dstRect)
  49. {
  50. if(!m_Src || !m_Dst)
  51. return false;
  52. if(m_Src->nWidth == 0 || m_Src->nHeight == 0 || m_Dst->nWidth == 0 || m_Dst->nHeight == 0)
  53. return false;
  54. if(m_Src->nWidth == m_Dst->nWidth && m_Src->nHeight == m_Dst->nHeight)
  55. return true;
  56. int ret = 0;
  57. double fx = (double)dstRect.width / (double)srcRect.width;
  58. double fy = (double)dstRect.height / (double)srcRect.height;
  59. ret = imresize(m_Src->SrcImg, m_Dst->SrcImg,fx,fy,INTER_LINEAR,IM_SYNC);
  60. if(ret != 0)
  61. {
  62. printf("imresize failed, ret = %d\n", ret);
  63. return false;
  64. }
  65. return true;
  66. }
  67. void RgaColorTransfer::ReleaseResource()
  68. {
  69. if (m_Src)
  70. {
  71. m_Src->ReleaseResource();
  72. delete m_Src;
  73. m_Src = nullptr;
  74. }
  75. if (m_Dst)
  76. {
  77. m_Dst->ReleaseResource();
  78. delete m_Dst;
  79. m_Dst = nullptr;
  80. }
  81. }
  82. int resize_image(void * src_buf, int src_width, int src_height, void * dst_buf, int dst_width, int dst_height)
  83. {
  84. int ret;
  85. rga_info_t src_info;
  86. rga_info_t dst_info;
  87. // Initialize source image information
  88. memset(&src_info, 0, sizeof(rga_info_t));
  89. src_info.fd = -1;
  90. src_info.virAddr = src_buf;
  91. src_info.mmuFlag = 1;
  92. rga_set_rect(&src_info.rect, 0, 0, src_width, src_height, src_width, src_height, RK_FORMAT_RGB_888);
  93. // Initialize destination image information
  94. memset(&dst_info, 0, sizeof(rga_info_t));
  95. dst_info.fd = -1;
  96. dst_info.virAddr = dst_buf;
  97. dst_info.mmuFlag = 1;
  98. rga_set_rect(&dst_info.rect, 0, 0, dst_width, dst_height, dst_width, dst_height, RK_FORMAT_RGB_888);
  99. // Perform the resize operation
  100. ret = c_RkRgaBlit(&src_info, &dst_info, NULL);
  101. if (ret) {
  102. printf("RGA resize failed: %d\n", ret);
  103. return ret;
  104. }
  105. return 0;
  106. }