|
| 1 | +#include "image_c.h" |
| 2 | + |
| 3 | +#include <cstring> |
| 4 | +#include "../image.h" |
| 5 | +#include "string_utils_c.h" |
| 6 | + |
| 7 | +using namespace nativeapi; |
| 8 | + |
| 9 | +// Create an image from a file path |
| 10 | +native_image_t native_image_from_file(const char* file_path) { |
| 11 | + if (!file_path) { |
| 12 | + return nullptr; |
| 13 | + } |
| 14 | + |
| 15 | + try { |
| 16 | + auto image = Image::FromFile(file_path); |
| 17 | + if (image) { |
| 18 | + auto size = image->GetSize(); |
| 19 | + if (size.width > 0 && size.height > 0) { |
| 20 | + return new std::shared_ptr<Image>(image); |
| 21 | + } |
| 22 | + } |
| 23 | + } catch (...) { |
| 24 | + // Handle exceptions |
| 25 | + } |
| 26 | + |
| 27 | + return nullptr; |
| 28 | +} |
| 29 | + |
| 30 | +// Create an image from base64-encoded data |
| 31 | +native_image_t native_image_from_base64(const char* base64_data) { |
| 32 | + if (!base64_data) { |
| 33 | + return nullptr; |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + auto image = Image::FromBase64(base64_data); |
| 38 | + if (image) { |
| 39 | + auto size = image->GetSize(); |
| 40 | + if (size.width > 0 && size.height > 0) { |
| 41 | + return new std::shared_ptr<Image>(image); |
| 42 | + } |
| 43 | + } |
| 44 | + } catch (...) { |
| 45 | + // Handle exceptions |
| 46 | + } |
| 47 | + |
| 48 | + return nullptr; |
| 49 | +} |
| 50 | + |
| 51 | +// Create an image from a platform-specific system icon |
| 52 | +native_image_t native_image_from_system_icon(const char* icon_name) { |
| 53 | + if (!icon_name) { |
| 54 | + return nullptr; |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + auto image = Image::FromSystemIcon(icon_name); |
| 59 | + if (image) { |
| 60 | + auto size = image->GetSize(); |
| 61 | + if (size.width > 0 && size.height > 0) { |
| 62 | + return new std::shared_ptr<Image>(image); |
| 63 | + } |
| 64 | + } |
| 65 | + } catch (...) { |
| 66 | + // Handle exceptions |
| 67 | + } |
| 68 | + |
| 69 | + return nullptr; |
| 70 | +} |
| 71 | + |
| 72 | +// Destroy an image and release its resources |
| 73 | +void native_image_destroy(native_image_t image) { |
| 74 | + if (image) { |
| 75 | + delete static_cast<std::shared_ptr<Image>*>(image); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Get the size of an image in pixels |
| 80 | +void native_image_get_size(native_image_t image, |
| 81 | + double* width, |
| 82 | + double* height) { |
| 83 | + if (!image || !width || !height) { |
| 84 | + if (width) |
| 85 | + *width = 0.0; |
| 86 | + if (height) |
| 87 | + *height = 0.0; |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + auto img = static_cast<std::shared_ptr<Image>*>(image); |
| 93 | + auto size = (*img)->GetSize(); |
| 94 | + *width = size.width; |
| 95 | + *height = size.height; |
| 96 | + } catch (...) { |
| 97 | + *width = 0.0; |
| 98 | + *height = 0.0; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Get the image format string for debugging purposes |
| 103 | +char* native_image_get_format(native_image_t image) { |
| 104 | + if (!image) { |
| 105 | + return nullptr; |
| 106 | + } |
| 107 | + |
| 108 | + try { |
| 109 | + auto img = static_cast<std::shared_ptr<Image>*>(image); |
| 110 | + std::string format = (*img)->GetFormat(); |
| 111 | + |
| 112 | + return to_c_str(format); |
| 113 | + } catch (...) { |
| 114 | + // Handle exceptions |
| 115 | + } |
| 116 | + |
| 117 | + return nullptr; |
| 118 | +} |
| 119 | + |
| 120 | +// Convert an image to base64-encoded PNG data |
| 121 | +char* native_image_to_base64(native_image_t image) { |
| 122 | + if (!image) { |
| 123 | + return nullptr; |
| 124 | + } |
| 125 | + |
| 126 | + try { |
| 127 | + auto img = static_cast<std::shared_ptr<Image>*>(image); |
| 128 | + std::string base64 = (*img)->ToBase64(); |
| 129 | + |
| 130 | + return to_c_str(base64); |
| 131 | + } catch (...) { |
| 132 | + // Handle exceptions |
| 133 | + } |
| 134 | + |
| 135 | + return nullptr; |
| 136 | +} |
| 137 | + |
| 138 | +// Save an image to a file |
| 139 | +bool native_image_save_to_file(native_image_t image, const char* file_path) { |
| 140 | + if (!image || !file_path) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + auto img = static_cast<std::shared_ptr<Image>*>(image); |
| 146 | + return (*img)->SaveToFile(file_path); |
| 147 | + } catch (...) { |
| 148 | + return false; |
| 149 | + } |
| 150 | +} |
0 commit comments