一、源碼
? ? ? ? 官方在librga中給了很多 demo 以供參考,例如 imresize 操作:
/** Copyright (C) 2022 Rockchip Electronics Co., Ltd.* Authors:* YuQiaowei <cerf.yu@rock-chips.com>** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/#define LOG_NDEBUG 0
#undef LOG_TAG
#define LOG_TAG "rga_resize_demo"#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <linux/stddef.h>#include "RgaUtils.h"
#include "im2d.hpp"#include "utils.h"#define LOCAL_FILE_PATH "/data"int main() {int ret = 0;int src_width, src_height, src_format;int dst_width, dst_height, dst_format;char *src_buf, *dst_buf;int src_buf_size, dst_buf_size;rga_buffer_t src_img, dst_img;rga_buffer_handle_t src_handle, dst_handle;memset(&src_img, 0, sizeof(src_img));memset(&dst_img, 0, sizeof(dst_img));src_width = 1280;src_height = 720;src_format = RK_FORMAT_RGBA_8888;dst_width = 1920;dst_height = 1080;dst_format = RK_FORMAT_RGBA_8888;src_buf_size = src_width * src_height * get_bpp_from_format(src_format);dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);src_buf = (char *)malloc(src_buf_size);dst_buf = (char *)malloc(dst_buf_size);/* fill image data */if (0 != read_image_from_file(src_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0)) {printf("src image read err\n");draw_rgba(src_buf, src_width, src_height);}memset(dst_buf, 0x80, dst_buf_size);src_handle = importbuffer_virtualaddr(src_buf, src_buf_size);dst_handle = importbuffer_virtualaddr(dst_buf, dst_buf_size);if (src_handle == 0 || dst_handle == 0) {printf("importbuffer failed!\n");goto release_buffer;}src_img = wrapbuffer_handle(src_handle, src_width, src_height, src_format);dst_img = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);/** Scale up the src image to 1920*1080.-------------- ---------------------| | | || src_img | | dst_img || | => | |-------------- | || |---------------------*/ret = imcheck(src_img, dst_img, {}, {});if (IM_STATUS_NOERROR != ret) {printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));return -1;}ret = imresize(src_img, dst_img);if (ret == IM_STATUS_SUCCESS) {printf("%s running success!\n", LOG_TAG);} else {printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));goto release_buffer;}write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);release_buffer:if (src_handle)releasebuffer_handle(src_handle);if (dst_handle)releasebuffer_handle(dst_handle);if (src_buf)free(src_buf);if (dst_buf)free(dst_buf);return ret;
}
二、移植使用
? ? ? ? 下面是我在移植進項目時的代碼:
/*** @Description: 直接內存映射圖像數據,圖像的生命周期由用戶管理* @param {Mat} &image: 輸入的源圖像,使用 OpenCV 的 Mat 結構表示* @param {Mat} &resized_image: 輸出的目標圖像,經過縮放處理后的圖像* @return {*} 返回 0 表示成功*/
int RGA_resize(const cv::Mat &image, cv::Mat &resized_image)
{if (image.type() != CV_8UC3) // 8位無符號三通道彩色圖像{printf("source image type is %d!\n", image.type());return -1;}rga_buffer_t src_img;rga_buffer_t dst_img;size_t img_width = image.cols;size_t img_height = image.rows;size_t target_width = resized_image.cols;size_t target_height = resized_image.rows;memset(&src_img, 0, sizeof(src_img));memset(&dst_img, 0, sizeof(dst_img));// 將源圖像和目標圖像的數據填充至 rga_buffer_t 結構體(函數內部就是填充 rga_buffer_t)// 該函數用于用戶自己管理的圖像內存// OpenCV 的 MAT 格式為 RGB888,沒有 A 通道src_img = wrapbuffer_virtualaddr((void *)image.data, img_width, img_height, RK_FORMAT_RGB_888);// 創建的 RGA 緩沖區,它直接引用了 resized_image.data 的內存地址,這意味著 dst_img 和 resized_image 共享同一塊內存dst_img = wrapbuffer_virtualaddr((void *)resized_image.data, target_width, target_height, RK_FORMAT_RGB_888);// 在配置完畢RGA任務參數后,可以通過該接口校驗當前參數是否合法,并根據當前硬件情況判斷硬件是否支持// src_img [required] input imageA// dst_img [required] output image// srect [required] src_img crop region// drect [required] dst_img crop region// 建議該接口僅在開發調試階段使用,避免多次校驗導致性能損耗IM_STATUS STATUS;/*STATUS = imcheck(src_img, dst_img, src_rect, dst_rect);if (IM_STATUS_NOERROR != STATUS){fprintf(stderr, "rga check error! %s", imStrError(STATUS));return -1;}*/// 調?RGA實現快速圖像縮放操作,將 rga_buffer_t 格式的結構體src、dst傳?imresize()// dst_img 是 resized_imageSTATUS = imresize(src_img, dst_img);if (IM_STATUS_SUCCESS != STATUS) {fprintf(stderr, "rga resize error! %s", imStrError(STATUS));return -1;}// printf("resizing .... %s\n", imStrError(STATUS));return 0;
}
? ? ? ? 與官方 demo 不同的是,我這里使用?wrapbuffer_virtualaddr 接口來映射?rga_buffer_t 接口體,輸入輸出圖像的緩沖區需要用戶提前申請,而官方的 demo 則是將圖像放入?RGA 內部統一管理內存。
? ? ? ? 我在傳入 cv::Mat 對象進行圖像操作時,發現 resize 后的接收對象,需要提前定義好緩沖區和大小,否則 RGA 在執行時會報錯,也就是接收緩沖區不能是只申明的 cv::Mat 對象:
cv::Mat img;
可以根據需要在創建時就設置大小:
// 創建 rgb 空圖像
cv::Mat rgb_img(orig_img.rows, orig_img.cols, CV_8UC3);
也可以在使用前通過 create 設置大小:
// 創建需要 resize 的空圖像,這里只申明,不申請內存
cv::Mat resized_img;// 如果需要縮放,再對 resized_img 申請大小,節約內存開銷
resized_img.create(height, width, CV_8UC3);
三、其他
? ? ? ? 有關其他接口的筆記,移步:
瑞芯微RKRGA(librga)Buffer API 分析-CSDN博客https://blog.csdn.net/plmm__/article/details/146571080?spm=1001.2014.3001.5501