一、源码
官方在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_image
STATUS = 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