对于raw文件而言,读取其中的NV12格式的数据,需要利用二进制进行读取,再经过GPU处理后,保存输出的数据为jpg文件。
首先读取和保存图像接口:
1.data_io.h
#ifdef APPLE //平台相关代码
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
cl_int Load_image_from_NV12(const std::string &filename,cl_mem *imageObjects,cl_context context,int &img_h,int &img_w);
void Save_pic_by_iostream(const std::string &filename,unsigned char *rd,int &img_h,int &img_w);
data_io.cpp
#include "data_io.h"
#include <iostream>
#include <fstream>
#include <sstream>
cv::Mat src_Image,dst_Image;
u_char * get_NV12_buffer(const std::string &filePath, int width, int height)
{
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::cerr << "Error: Unable to open NV12 file " << filePath << std::endl;
return NULL;
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
size=width*height*3/2;
char *buffer=new char [size];
if (!file.read(buffer, size)) {
std::cerr << "Error: Unable to read NV12 data from file " << filePath << std::endl;
return NULL;
}
return (u_char *)buffer;
}
cl_int Load_image_from_NV12(const std::string &filename,cl_mem *imageObjects,cl_context context,int &img_h,int &img_w)
{
cl_int errNum;
img_w=3840;
img_h=2176;
u_char * nv12data= get_NV12_buffer(filename, img_w, img_h);
if(NULL==nv12data)
return 1;
int nv12_size=sizeof(u_char)*img_h*img_w*3/2;
imageObjects[0] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
nv12_size, nv12data, &errNum); //CL_MEM_COPY_HOST_PTR指定创建缓存对象后拷贝数据
return errNum;
}
void Save_pic_by_iostream(const std::string &filename,unsigned char *rd,int &img_h,int &img_w)
{
std::ofstream out; // 读取图像
out.open(filename, std::ios::out | std::ios::binary);
//从buffer中写数据到out指向的文件中
out.write((const char*)rd, img_w*3*img_h* sizeof(char));
//关闭文件指针,释放buffer内存
out.close();
delete[]rd;
std::cout <<"if:"<< filename <<std::endl;
std::ifstream is(filename, std::ifstream::in | std::ios::binary);
// 2. 计算图片长度
is.seekg(0, is.end);
int length = is.tellg();
std::cout << length <<std::endl;
is.seekg(0, is.beg);
// 3. 创建内存缓存区
char * buffer = new char[length];
// 4. 读取图片
is.read(buffer, length);
// 到此,图片已经成功的被读取到内存(buffer)中
//二、另存为 223.jpg
cv::Mat dst_Image =cv::Mat(img_h, img_w, CV_8UC3, (void*)buffer);
cv::imwrite("../data/dst_rgb_if.jpg", dst_Image);
}
其次,主函数:
3.main.cp
#include <iostream>
#include <fstream>
#include <sstream>
#ifdef APPLE //平台相关代码
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include "data_io.h"
extern cv::Mat src_Image,dst_Image;
//在第一个平台中创建只包括GPU的上下文
cl_context CreateContext()
{
cl_int errNum;
cl_uint numPlatforms;
cl_platform_id firstPlatformId;
cl_context context = NULL;
// 选择第一个平台
errNum = clGetPlatformIDs(1, &firstPlatformId, &numPlatforms);
if (errNum != CL_SUCCESS || numPlatforms <= 0)
{
std::cerr << "Failed to find any OpenCL platforms." << std::endl;
return NULL;
}
//cout<<"numPlatforms="<<numPlatforms<<endl; 1
// 接下来尝试通过GPU设备建立上下文
cl_context_properties contextProperties[] =
{
CL_CONTEXT_PLATFORM,
(cl_context_properties)firstPlatformId,
0
};
context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_GPU,
NULL, NULL, &errNum);
if (errNum != CL_SUCCESS)
{
std::cout << "Could not create GPU context, trying CPU..." << std::endl;
context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_CPU,
NULL, NULL, &errNum);
if (errNum != CL_SUCCESS)
{
std::cerr << "Failed to create an OpenCL GPU or CPU context." << std::endl;
return NULL;
}
}
return context;
}
//在第一个设备上创建命令队列
cl_command_queue CreateCommandQueue(cl_context context, cl_device_id *device)
{
cl_int errNum;
cl_device_id *devices;
cl_command_queue commandQueue = NULL;
size_t deviceBufferSize = -1;
// 首先获得设备的信息
errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceBufferSize);
if (errNum != CL_SUCCESS)
{
std::cerr << "Failed call to clGetContextInfo(...,GL_CONTEXT_DEVICES,...)";
return NULL;
}
if (deviceBufferSize <= 0)
{
std::cerr << "No devices available.";
return NULL;
}
//为设备分配内存
devices = new cl_device_id[deviceBufferSize / sizeof(cl_device_id)];
errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES, deviceBufferSize, devices, NULL);
if (errNum != CL_SUCCESS)
{
std::cerr << "Failed to get device IDs";
return NULL;
}
// 选择第一个设备并为其创建命令队列
commandQueue = clCreateCommandQueue(context, devices[0], 0, NULL);
if (commandQueue == NULL)
{
std::cerr << "Failed to create commandQueue for device 0";
return NULL;
}
//释放信息
*device = devices[0];
delete [] devices;
return commandQueue;
}
// 创建OpenCL程序对象
cl_program CreateProgram(cl_context context, cl_device_id device, const char* fileName)
{
cl_int errNum;
cl_program program;
std::ifstream kernelFile(fileName, std::ios::in);
if (!kernelFile.is_open())
{
std::cerr << "Failed to open file for reading: " << fileName << std::endl;
return NULL;
}
std::ostringstream oss;
oss << kernelFile.rdbuf();
std::string srcStdStr = oss.str();
const char *srcStr = srcStdStr.c_str();
program = clCreateProgramWithSource(context, 1,
(const char**)&srcStr,
NULL, NULL);
if (program == NULL)
{
std::cerr << "Failed to create CL program from source." << std::endl;
return NULL;
}
errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (errNum != CL_SUCCESS)
{
// 输出错误信息
char buildLog[16384];
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
sizeof(buildLog), buildLog, NULL);
std::cerr << "Error in kernel: " << std::endl;
std::cerr << buildLog;
clReleaseProgram(program);
return NULL;
}
return program;
}
//获取最接近的倍数
size_t RoundUp(int groupSize, int globalSize)
{
int r = globalSize % groupSize;
if(r == 0)
{
return globalSize;
}
else
{
return globalSize + groupSize - r;
}
}
//清除资源
void Cleanup(cl_context context, cl_command_queue commandQueue,
cl_program program, cl_kernel kernel, cl_mem imageObjects[2])
{
for (int i = 0; i < 2; i++)
{
if (imageObjects[i] != 0)
clReleaseMemObject(imageObjects[i]);
}
if (commandQueue != 0)
clReleaseCommandQueue(commandQueue);
if (kernel != 0)
clReleaseKernel(kernel);
if (program != 0)
clReleaseProgram(program);
if (context != 0)
clReleaseContext(context);
}
int main()
{
//initial varibles
cl_int errNum=0;
cl_device_id device = 0;
cl_context context = 0;
cl_program program = 0;
cl_kernel kernel = 0;
cl_command_queue commandQueue = 0;
cl_sampler sampler = 0;
cl_mem imageObjects[2] = { 0, 0 };
int img_w = 3840;
int img_h = 2176;
int new_w=3840;
int new_h=2176;
size_t read_size=img_w*img_h*3;
size_t g_w=3840;
size_t g_h=2176;
std::string input_file,output_jpgfile;
char *kernel_file=new char [100];
char *kernel_name=new char [100];
input_file="../data/test.raw";
output_jpgfile="../data/dst_rgb_of.jpg";
strcpy(kernel_file,"nv12_to_rgb.cl");
strcpy(kernel_name,"kernel_nv12_to_rgb");
// 1.选择platform,创建contex上下文
context = CreateContext();
if (context == NULL)
{
std::cerr << "Failed to create OpenCL context." << std::endl;
return 1;
}
// 2.创建命令队列
commandQueue = CreateCommandQueue(context, &device);
if (commandQueue == NULL)
{
std::cerr <<"CreateCommandQueue failed"<<std::endl;
Cleanup(context, commandQueue, program, kernel, imageObjects);
return 1;
}
// 3. 确保设备支持这种图像格式
cl_bool imageSupport = CL_FALSE;
clGetDeviceInfo(device, CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool),
&imageSupport, NULL);
if (imageSupport != CL_TRUE)
{
std::cerr << "OpenCL device does not support images." << std::endl;
Cleanup(context, commandQueue, program, kernel, imageObjects);
return 1;
}
//4.准备输入数据
errNum=Load_image_from_NV12(input_file,imageObjects,context,img_h,img_w);
if (errNum != CL_SUCCESS)
{
std::cerr << "Load_image failed." << std::endl;
Cleanup(context, commandQueue, program, kernel, imageObjects);
return 1;
}
// 5.创建输出的图像对象
new_w=img_w;
new_h=img_h;
read_size=sizeof(u_char)*img_h*img_w*3;
imageObjects[1] = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
read_size, NULL, &errNum);
if (errNum != CL_SUCCESS)
{
std::cerr << "create output imageobject failed." << std::endl;
Cleanup(context, commandQueue, program, kernel, imageObjects);
return 1;
}
//6.创建OpenCL-program对象
program = CreateProgram(context, device, kernel_file);
if (program == NULL)
{
std::cerr <<"CreateProgram failed"<<std::endl;
Cleanup(context, commandQueue, program, kernel, imageObjects);
return 1;
}
// 7.创建OpenCL核
kernel = clCreateKernel(program, kernel_name, NULL);
if (kernel == NULL)
{
std::cerr << "Failed to create kernel" << std::endl;
Cleanup(context, commandQueue, program, kernel, imageObjects);
return 1;
}
//8. 设定参数
errNum = clSetKernelArg(kernel, 0, sizeof(cl_mem), &imageObjects[0]);
errNum |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &imageObjects[1]);
errNum |= clSetKernelArg(kernel, 2, sizeof(cl_int), &img_w);
errNum |= clSetKernelArg(kernel, 3, sizeof(cl_int), &img_h);
if (errNum != CL_SUCCESS)
{
std::cerr << "Error setting kernel arguments." << std::endl;
Cleanup(context, commandQueue, program, kernel, imageObjects);
return 1;
}
g_w=new_w;
g_h=new_h;
size_t localWorkSize[2] = { 32, 32 };
size_t globalWorkSize[2] = {g_w,g_h};
//9.启动内核,内核执行完成后,会将evt置为CL_SUCCESS/CL_COMPLETE
cl_event evt;
errNum = clEnqueueNDRangeKernel(commandQueue, kernel,
2, 0, globalWorkSize, localWorkSize,
0, NULL, &evt);
clWaitForEvents(1, &evt); //等待命令事件发生
clReleaseEvent(evt);
//10.读回数据
u_char *read_data = new u_char [read_size];
errNum =clEnqueueReadBuffer(commandQueue, imageObjects[1],
CL_TRUE,
0,
read_size,
read_data, 0, NULL, NULL);
if (errNum != CL_SUCCESS)
{
std::cerr << "Error reading result buffer enume." <<errNum<< std::endl;
Cleanup(context, commandQueue, program, kernel, imageObjects);
return 1;
}
if (read_data == NULL)
{
std::cerr << "Error reading result buffer null." <<errNum<< std::endl;
Cleanup(context, commandQueue, program, kernel, imageObjects);
return 1;
}
//显示图像
Save_pic_by_iostream(output_jpgfile,read_data,img_h,img_w);
Cleanup(context, commandQueue, program, kernel, imageObjects);
std::cout<<"NV12 convert to rgb via opencl success!"<<std::endl;
return 0;
}
最后,实际灰度图像的kernel函数
4.nv12_to_rgb.cl
__kernel void kernel_nv12_to_rgb(__global unsigned char * nv12data,
__global unsigned char * rgbdata,
int width, int height){
int x = get_global_id(0); //width
int y = get_global_id(1); //height
unsigned char *ybase = nv12data;
unsigned char *ubase = &nv12data[width * height];
if(x < width && y < height)
{
int index = y * width + x;
unsigned char Y = ybase[x + y * width];
unsigned char U = ubase[y / 2 * width + (x / 2) * 2];
unsigned char V = ubase[y / 2 * width + (x / 2) * 2 + 1];
rgbdata[index*3] = Y + 1.402 * (V - 128);//R
rgbdata[index*3+1] = Y - 0.34413 * (U - 128) - 0.71414 * (V - 128);//G
rgbdata[index*3+2] =Y + 1.772 * (U - 128);//B
}
}
5.Makefile
demo : main.cpp data_io.cpp
g++ `pkg-config opencv4 --cflags` main.cpp data_io.cpp -o demo `pkg-config opencv4 --libs` -D CL_TARGET_OPENCL_VERSION=100 -lOpenCL
验证是否转换正确,可以分别用opencv和自己生成的结果做对比。