|
| 1 | +#include "load_image.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#ifdef _WIN32 |
| 7 | + #define strcasecmp _stricmp |
| 8 | +#else |
| 9 | + #include <strings.h> |
| 10 | +#endif |
| 11 | + |
| 12 | +#ifdef _WIN32 |
| 13 | + #include <io.h> |
| 14 | + #define stat _stat |
| 15 | +#else |
| 16 | + #include <sys/stat.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +#include "openvino/c/openvino.h" |
| 20 | + |
| 21 | +#define STB_IMAGE_IMPLEMENTATION |
| 22 | +#include "stb_image.h" |
| 23 | + |
| 24 | +static const char* supported_extensions[] = { |
| 25 | + ".jpg", ".jpeg", ".png", ".bmp", ".tga", ".psd", ".gif", ".hdr", ".pic", ".pnm" |
| 26 | +}; |
| 27 | +static const size_t num_extensions = sizeof(supported_extensions) / sizeof(supported_extensions[0]); |
| 28 | + |
| 29 | +static int is_supported_image(const char* filename) { |
| 30 | + if (!filename) return 0; |
| 31 | + |
| 32 | + size_t len = strlen(filename); |
| 33 | + for (size_t i = 0; i < num_extensions; i++) { |
| 34 | + size_t ext_len = strlen(supported_extensions[i]); |
| 35 | + if (len >= ext_len) { |
| 36 | + const char* ext = filename + len - ext_len; |
| 37 | + if (strcasecmp(ext, supported_extensions[i]) == 0) { |
| 38 | + return 1; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + return 0; |
| 43 | +} |
| 44 | + |
| 45 | +typedef struct { |
| 46 | + unsigned char* image_data; |
| 47 | + int channels; |
| 48 | + int height; |
| 49 | + int width; |
| 50 | +} image_allocator_t; |
| 51 | + |
| 52 | +static void* image_allocate(size_t bytes, size_t alignment, void* user_data) { |
| 53 | + image_allocator_t* allocator = (image_allocator_t*)user_data; |
| 54 | + if (allocator && allocator->image_data && |
| 55 | + allocator->channels * allocator->height * allocator->width == (int)bytes) { |
| 56 | + return allocator->image_data; |
| 57 | + } |
| 58 | + return NULL; |
| 59 | +} |
| 60 | + |
| 61 | +static void image_deallocate(void* ptr, size_t bytes, size_t alignment, void* user_data) { |
| 62 | + image_allocator_t* allocator = (image_allocator_t*)user_data; |
| 63 | + if (allocator && allocator->image_data && |
| 64 | + allocator->channels * allocator->height * allocator->width == (int)bytes) { |
| 65 | + stbi_image_free(allocator->image_data); |
| 66 | + allocator->image_data = NULL; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#define CHECK_STATUS(return_status) \ |
| 71 | + if (return_status != OK) { \ |
| 72 | + fprintf(stderr, "[ERROR] return status %d, line %d\n", return_status, __LINE__); \ |
| 73 | + goto err; \ |
| 74 | + } |
| 75 | + |
| 76 | +ov_tensor_t* load_image(const char* image_path) { |
| 77 | + if (!image_path) { |
| 78 | + fprintf(stderr, "Error: image_path is NULL\n"); |
| 79 | + return NULL; |
| 80 | + } |
| 81 | + |
| 82 | + if (!file_exists(image_path)) { |
| 83 | + fprintf(stderr, "Error: Image file '%s' does not exist\n", image_path); |
| 84 | + return NULL; |
| 85 | + } |
| 86 | + |
| 87 | + int width, height, channels; |
| 88 | + const int desired_channels = 3; |
| 89 | + |
| 90 | + unsigned char* data = stbi_load(image_path, &width, &height, &channels, desired_channels); |
| 91 | + if (!data) { |
| 92 | + fprintf(stderr, "Error: Failed to load image '%s': %s\n", image_path, stbi_failure_reason()); |
| 93 | + return NULL; |
| 94 | + } |
| 95 | + |
| 96 | + image_allocator_t* allocator = (image_allocator_t*)malloc(sizeof(image_allocator_t)); |
| 97 | + if (!allocator) { |
| 98 | + fprintf(stderr, "Error: Failed to allocate memory for allocator\n"); |
| 99 | + stbi_image_free(data); |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + |
| 103 | + allocator->image_data = data; |
| 104 | + allocator->channels = desired_channels; |
| 105 | + allocator->height = height; |
| 106 | + allocator->width = width; |
| 107 | + |
| 108 | + ov_tensor_t* tensor = NULL; |
| 109 | + ov_element_type_e input_type = U8; |
| 110 | + int64_t dims[4] = {1, height, width, desired_channels}; |
| 111 | + |
| 112 | + ov_shape_t input_shape = {.rank = 0, .dims = NULL}; |
| 113 | + ov_shape_create(4, dims, &input_shape); |
| 114 | + |
| 115 | + ov_tensor_create_from_host_ptr( |
| 116 | + input_type, |
| 117 | + input_shape, // shape: [1, H, W, C] |
| 118 | + data, |
| 119 | + &tensor |
| 120 | + ); |
| 121 | + |
| 122 | + free(allocator); |
| 123 | + |
| 124 | + return tensor; |
| 125 | +} |
| 126 | + |
| 127 | +const ov_tensor_t** load_images(const char* image_path, size_t* tensor_count) { |
| 128 | + if (!image_path || !tensor_count) { |
| 129 | + fprintf(stderr, "Error: image_path or tensor_count is NULL\n"); |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + |
| 133 | + if (!file_exists(image_path)) { |
| 134 | + fprintf(stderr, "Error: Image file '%s' does not exist\n", image_path); |
| 135 | + return NULL; |
| 136 | + } |
| 137 | + |
| 138 | + ov_tensor_t* tensor = load_image(image_path); |
| 139 | + if (!tensor) { |
| 140 | + return NULL; |
| 141 | + } |
| 142 | + |
| 143 | + const ov_tensor_t** tensors = (const ov_tensor_t**)malloc(sizeof(ov_tensor_t*)); |
| 144 | + if (!tensors) { |
| 145 | + fprintf(stderr, "Error: Failed to allocate memory for single tensor\n"); |
| 146 | + free_tensor(tensor); |
| 147 | + return NULL; |
| 148 | + } |
| 149 | + |
| 150 | + tensors[0] = tensor; |
| 151 | + *tensor_count = 1; |
| 152 | + |
| 153 | + return tensors; |
| 154 | +} |
| 155 | + |
| 156 | +void free_tensor(ov_tensor_t* tensor) { |
| 157 | + if (tensor) { |
| 158 | + ov_tensor_free(tensor); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +void free_tensor_array(ov_tensor_t** tensors, size_t count) { |
| 163 | + if (tensors) { |
| 164 | + for (size_t i = 0; i < count; i++) { |
| 165 | + if (tensors[i]) { |
| 166 | + ov_tensor_free(tensors[i]); |
| 167 | + } |
| 168 | + } |
| 169 | + free(tensors); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +int file_exists(const char* path) { |
| 174 | + if (!path) return 0; |
| 175 | + |
| 176 | + struct stat buffer; |
| 177 | + return (stat(path, &buffer) == 0); |
| 178 | +} |
| 179 | + |
0 commit comments