#include "RgaColorTransfer.h" #include "../LogRecorder/LogOutput.h" // static member initialization RgaColorTransfer* RgaColorTransfer::m_instance = nullptr; RgaColorTransfer::RgaColorTransfer() { m_Src = new RGA_BUFFER; m_Dst = new RGA_BUFFER; } RgaColorTransfer::~RgaColorTransfer() { ReleaseResource(); } bool RgaColorTransfer::copyBuffer(cv::Rect srcRect, cv::Rect dstRect) { if (!m_Src || !m_Dst) return false; int ret = 0; im_rect srcRectRga = { srcRect.x, srcRect.y, srcRect.width, srcRect.height }; im_rect dstRectRga = { dstRect.x, dstRect.y, dstRect.width, dstRect.height }; ret = imcheck(m_Src->SrcImg, m_Dst->SrcImg, srcRectRga, dstRectRga); if (ret != 0) { printf("imcheck failed, ret = %d\n", ret); return false; } // Use improcess to perform the copy operation between the source and destination buffers. ret = improcess(m_Src->SrcImg,m_Dst->SrcImg,{},srcRectRga,dstRectRga,{},IM_ASYNC); if (ret != 0) { printf("improcess failed, ret = %d\n", ret); return false; } return true; } bool RgaColorTransfer::transfer(int nType) { if (!m_Src || !m_Dst) return false; int ret = 0; ret = imcvtcolor(m_Src->SrcImg, m_Dst->SrcImg,m_Src->nType,m_Dst->nType); if (ret != 0) { printf("imcvtcolor failed, ret = %d\n", ret); return false; } return true; } bool RgaColorTransfer::resizeImage(cv::Rect srcRect, cv::Rect dstRect) { if(!m_Src || !m_Dst) return false; if(m_Src->nWidth == 0 || m_Src->nHeight == 0 || m_Dst->nWidth == 0 || m_Dst->nHeight == 0) return false; if(m_Src->nWidth == m_Dst->nWidth && m_Src->nHeight == m_Dst->nHeight) return true; int ret = 0; double fx = (double)dstRect.width / (double)srcRect.width; double fy = (double)dstRect.height / (double)srcRect.height; ret = imresize(m_Src->SrcImg, m_Dst->SrcImg,fx,fy,INTER_LINEAR,IM_SYNC); if(ret != 0) { printf("imresize failed, ret = %d\n", ret); return false; } return true; } void RgaColorTransfer::ReleaseResource() { if (m_Src) { m_Src->ReleaseResource(); delete m_Src; m_Src = nullptr; } if (m_Dst) { m_Dst->ReleaseResource(); delete m_Dst; m_Dst = nullptr; } } int resize_image(void * src_buf, int src_width, int src_height, void * dst_buf, int dst_width, int dst_height) { // 参数验证 if (!src_buf || !dst_buf) { LOG_ERROR("resize_image: 无效的缓冲区指针"); return -1; } if (src_width <= 0 || src_height <= 0 || dst_width <= 0 || dst_height <= 0) { LOG_ERROR("resize_image: 无效的图像尺寸 - src:{}x{}, dst:{}x{}", src_width, src_height, dst_width, dst_height); return -2; } int ret; rga_info_t src_info; rga_info_t dst_info; // Initialize source image information memset(&src_info, 0, sizeof(rga_info_t)); src_info.fd = -1; src_info.virAddr = src_buf; src_info.mmuFlag = 1; rga_set_rect(&src_info.rect, 0, 0, src_width, src_height, src_width, src_height, RK_FORMAT_RGB_888); // Initialize destination image information memset(&dst_info, 0, sizeof(rga_info_t)); dst_info.fd = -1; dst_info.virAddr = dst_buf; dst_info.mmuFlag = 1; rga_set_rect(&dst_info.rect, 0, 0, dst_width, dst_height, dst_width, dst_height, RK_FORMAT_RGB_888); try{ // Perform the resize operation // 执行缩放操作 ret = c_RkRgaBlit(&src_info, &dst_info, NULL); if (ret) { LOG_ERROR("resize_image: RGA缩放失败, ret = {}", ret); // 尝试重试一次 LOG_INFO("resize_image: 正在尝试重新执行RGA缩放"); ret = c_RkRgaBlit(&src_info, &dst_info, NULL); if (ret) { LOG_ERROR("resize_image: RGA缩放重试仍然失败, ret = {}", ret); return ret; } else { LOG_INFO("resize_image: RGA缩放重试成功"); } } }catch (const std::exception& e) { LOG_ERROR("resize_image: 异常: {}", e.what()); return -100; } catch (...) { LOG_ERROR("resize_image: 发生未知异常"); return -101; } return 0; }