本文整理汇总了C++中PNG::Create方法的典型用法代码示例。如果您正苦于以下问题:C++ PNG::Create方法的具体用法?C++ PNG::Create怎么用?C++ PNG::Create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PNG
的用法示例。
在下文中一共展示了PNG::Create方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//.........这里部分代码省略.........
}
std::vector<cl::Device> devices;
platforms[platformId].getDevices(CL_DEVICE_TYPE_ALL, &devices);
cl::Device device = devices[0];
std::cout << "Using device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
std::cout << "Using platform: " << platforms[platformId].getInfo<CL_PLATFORM_NAME>() << std::endl;
cl::Context context(device);
//load our image
PNG inPng("Lenna.png");
//store width and height so we can use them for our output image later
const unsigned int w = inPng.w;
const unsigned int h = inPng.h;
//input image
const cl::ImageFormat format(CL_RGBA, CL_UNSIGNED_INT8);
cl::Image2D in(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, format, w, h, 0, &inPng.data[0]);
//we are done with the image so free up its memory
inPng.Free();
//output image
cl::Image2D out(context, CL_MEM_WRITE_ONLY, format, w, h, 0, NULL);
cl::Program::Sources sources;
std::string kernel_code = readFile("cl_tutorial_3_boxFilter.cl");
//Add your program source
sources.push_back({ kernel_code.c_str(),kernel_code.length() });
//Create your OpenCL program and build it.
cl::Program program(context, sources);
if (program.build({ device }) != CL_SUCCESS)
{
std::cout << " Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;//print the build log to find any issues with your source
exit(1);//Quit if your program doesn't compile
}
//create command queue
cl::CommandQueue queue(context, device, CL_QUEUE_PROFILING_ENABLE, NULL);
//set the kernel arguments
cl::Kernel kernelboxFilter(program, "boxFilter");
kernelboxFilter.setArg(0, in);
kernelboxFilter.setArg(1, out);
kernelboxFilter.setArg(2, w);
kernelboxFilter.setArg(3, h);
kernelboxFilter.setArg(4, filterWidth);
kernelboxFilter.setArg(5, filterHeight);
cl::Event timer;
//execute kernel
queue.enqueueNDRangeKernel(kernelboxFilter, cl::NullRange, cl::NDRange(w, h), cl::NullRange, NULL, &timer);
//wait for kernel to finish
timer.wait();
cl_ulong time_start, time_end;
double total_time;
time_start = timer.getProfilingInfo<CL_PROFILING_COMMAND_START>();
time_end = timer.getProfilingInfo<CL_PROFILING_COMMAND_END>();
total_time = time_end - time_start;
printf("\nExecution time in milliseconds = %0.3f ms\n", (total_time / 1000000.0));
//start and end coordinates for reading our image (I really do not like how the c++ wrapper does this)
cl::size_t<3> origin;
cl::size_t<3> size;
origin[0] = 0;
origin[1] = 0;
origin[2] = 0;
size[0] = w;
size[1] = h;
size[2] = 1;
//output png
PNG outPng;
//create the image with the same width and height as original
outPng.Create(w, h);
//temporary array to store the result from opencl
auto tmp = new unsigned char[w * h * 4];
//CL_TRUE means that it waits for the entire image to be copied before continuing
queue.enqueueReadImage(out, CL_TRUE, origin, size, 0, 0, tmp);
//copy the data from the temp array to the png
std::copy(&tmp[0], &tmp[w * h * 4], std::back_inserter(outPng.data));
//write the image to file
outPng.Save("cl_tutorial_3.png");
//free the iamge's resources since we are done with it
outPng.Free();
//free the temp array
delete[] tmp;
return 0;
}