#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <cf4ocl2.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define FILTER_KERNEL \
@image_filter_KERNEL_SRC@
#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); }
int main(
int argc,
char* argv[]) {
int dev_idx = -1;
cl_bool image_ok;
unsigned char* input_image;
unsigned char* output_image;
int width, height, n_channels;
int file_write_status;
cl_image_format image_format = { CL_RGBA, CL_UNSIGNED_INT8 };
size_t origin[3] = { 0, 0, 0 };
size_t region[3];
size_t real_ws[2];
size_t gws[2];
size_t lws[2];
if (argc < 2) {
ERROR_MSG_AND_EXIT("Usage: image_filter <image_file> [device_index]");
} else if (argc >= 3) {
dev_idx = atoi(argv[2]);
}
input_image = stbi_load(argv[1], &width, &height, &n_channels, 4);
if (!input_image) ERROR_MSG_AND_EXIT(stbi_failure_reason());
real_ws[0] = width; real_ws[1] = height;
region[0] = width; region[1] = height; region[2] = 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_in =
ccl_image_new(ctx, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
&image_format, input_image, &err,
"image_type", (cl_mem_object_type) CL_MEM_OBJECT_IMAGE2D,
"image_width", (size_t) width,
"image_height", (size_t) height,
NULL);
HANDLE_ERROR(err);
&image_format, NULL, &err,
"image_type", (cl_mem_object_type) CL_MEM_OBJECT_IMAGE2D,
"image_width", (size_t) width,
"image_height", (size_t) height,
NULL);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
printf("\n * Image size: %d x %d, %d channels\n",
width, height, n_channels);
printf(" * Global work-size: (%d, %d)\n", (int) gws[0], (int) gws[1]);
printf(" * Local work-size: (%d, %d)\n", (int) lws[0], (int) lws[1]);
CL_FILTER_NEAREST, &err);
HANDLE_ERROR(err);
krnl, queue, 2, NULL, gws, lws, NULL, &err,
img_in, img_out, smplr, NULL);
HANDLE_ERROR(err);
output_image = (unsigned char*)
malloc(width * height * 4 * sizeof(unsigned char));
0, 0, output_image, NULL, &err);
HANDLE_ERROR(err);
file_write_status = stbi_write_png(IMAGE_FILE, width, height, 4,
output_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.");
}
free(output_image);
stbi_image_free(input_image);
return EXIT_SUCCESS;
}