#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <cf4ocl2.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define IMAGE_WIDTH 128
#define IMAGE_HEIGHT 128
#define IMAGE_FILE "out.png"
#define ERROR_MSG_AND_EXIT(msg) \
do { fprintf(stderr, "\n%s\n", msg); exit(EXIT_FAILURE); } while(0)
#define HANDLE_ERROR(err) \
if (err != NULL) { ERROR_MSG_AND_EXIT(err->message); }
#ifdef CL_VERSION_1_2
int main(
int argc,
char* argv[]) {
int file_write_status;
int dev_idx = -1;
cl_bool image_ok;
cl_uchar4 base_color = {{255, 255, 255, 255}};
cl_uchar4 img_host[IMAGE_WIDTH * IMAGE_HEIGHT];
for (int i = 0; i < IMAGE_WIDTH * IMAGE_HEIGHT; ++i)
img_host[i] = base_color;
cl_image_format image_format = { CL_RGBA, CL_UNSIGNED_INT8 };
size_t origin[3] = { 0, 0, 0 };
size_t region[3] = { IMAGE_WIDTH, IMAGE_HEIGHT, 1};
size_t c1_origin[3] = { 0, 0, 0 };
size_t c1_region[3] = { IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2, 1 };
cl_uint4 c1_color = {{ 255, 0, 0, 255 }};
size_t c2_origin[3] = { IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2, 0 };
size_t c2_region[3] = { IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2, 1};
cl_uint4 c2_color = {{ 0, 255, 0, 255 }};
if (argc >= 2) {
dev_idx = atoi(argv[1]);
}
HANDLE_ERROR(err);
HANDLE_ERROR(err);
dev, CL_DEVICE_IMAGE_SUPPORT, cl_bool, &err);
HANDLE_ERROR(err);
if (!image_ok)
ERROR_MSG_AND_EXIT("Selected device doesn't support images.");
HANDLE_ERROR(err);
img =
ccl_image_new(ctx, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
&image_format, img_host, &err,
"image_type", (cl_mem_object_type) CL_MEM_OBJECT_IMAGE2D,
"image_width", (size_t) IMAGE_WIDTH,
"image_height", (size_t) IMAGE_HEIGHT,
NULL);
HANDLE_ERROR(err);
NULL, &err);
HANDLE_ERROR(err);
NULL, &err);
HANDLE_ERROR(err);
0, 0, img_host, NULL, &err);
HANDLE_ERROR(err);
file_write_status = stbi_write_png(IMAGE_FILE, IMAGE_WIDTH,
IMAGE_HEIGHT, 4, img_host, IMAGE_WIDTH * 4);
if (file_write_status) {
fprintf(stdout, "\nImage saved in file '" IMAGE_FILE "'\n");
} else {
ERROR_MSG_AND_EXIT("Unable to save image in file.");
}
return EXIT_SUCCESS;
}
#else
fprintf(stderr, "This sample requires OpenCL 1.2\n");
return EXIT_FAILURE;
}
#endif