|
| 1 | +//**************************************************************************** |
| 2 | +// Copyright 2025 Intel Corporation |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//***************************************************************************** |
| 16 | +#include "image_conversion.hpp" |
| 17 | + |
| 18 | +#include <iostream> |
| 19 | + |
| 20 | +#define STB_IMAGE_IMPLEMENTATION |
| 21 | +#define STB_IMAGE_WRITE_IMPLEMENTATION |
| 22 | +#include "logging.hpp" |
| 23 | +#include "profiler.hpp" |
| 24 | +#pragma warning(push) |
| 25 | +#pragma warning(disable : 6262) |
| 26 | +#include "stb_image.h" // NOLINT |
| 27 | +#include "stb_image_write.h" // NOLINT |
| 28 | +#pragma warning(default : 6262) |
| 29 | +#pragma warning(disable : 6001 4324 6385 6386) |
| 30 | +#include "absl/strings/escaping.h" |
| 31 | +#include "absl/strings/str_cat.h" |
| 32 | +#pragma warning(pop) |
| 33 | + |
| 34 | +namespace ovms { |
| 35 | + |
| 36 | +void hello() { |
| 37 | + std::cout << "Hello, World!" << std::endl; |
| 38 | +} |
| 39 | + |
| 40 | +ov::Tensor load_image_stbi(const std::string& imageBytes) { |
| 41 | + int x = 0, y = 0, channelsInFile = 0; |
| 42 | + constexpr int desiredChannels = 3; |
| 43 | + unsigned char* image = stbi_load_from_memory( |
| 44 | + (const unsigned char*)imageBytes.data(), imageBytes.size(), |
| 45 | + &x, &y, &channelsInFile, desiredChannels); |
| 46 | + if (!image) { |
| 47 | + std::stringstream errorMessage; |
| 48 | + errorMessage << "Failed to load the image"; |
| 49 | + throw std::runtime_error{errorMessage.str()}; |
| 50 | + } |
| 51 | + struct SharedImageAllocator { |
| 52 | + unsigned char* image; |
| 53 | + int channels, height, width; |
| 54 | + void* allocate(size_t bytes, size_t) const { |
| 55 | + if (image && channels * height * width == bytes) { |
| 56 | + return image; |
| 57 | + } |
| 58 | + throw std::runtime_error{"Unexpected number of bytes was requested to allocate."}; |
| 59 | + } |
| 60 | + void deallocate(void*, size_t bytes, size_t) { |
| 61 | + if (channels * height * width != bytes) { |
| 62 | + throw std::runtime_error{"Unexpected number of bytes was requested to deallocate."}; |
| 63 | + } |
| 64 | + if (image != nullptr) { |
| 65 | + stbi_image_free(image); |
| 66 | + image = nullptr; |
| 67 | + } |
| 68 | + } |
| 69 | + bool is_equal(const SharedImageAllocator& other) const noexcept { return this == &other; } |
| 70 | + }; |
| 71 | + return ov::Tensor( |
| 72 | + ov::element::u8, |
| 73 | + ov::Shape{1, size_t(y), size_t(x), size_t(desiredChannels)}, |
| 74 | + SharedImageAllocator{image, desiredChannels, y, x}); |
| 75 | +} |
| 76 | + |
| 77 | +std::string save_image_stbi(ov::Tensor tensor) { |
| 78 | + // Validate tensor properties |
| 79 | + if (tensor.get_element_type() != ov::element::u8) { |
| 80 | + throw std::runtime_error{"Only U8 tensor element type is supported for image saving"}; |
| 81 | + } |
| 82 | + |
| 83 | + if (tensor.get_shape().size() != 4 || tensor.get_shape()[0] != 1) { |
| 84 | + throw std::runtime_error{"Tensor must be in NHWC format with batch size 1"}; |
| 85 | + } |
| 86 | + |
| 87 | + size_t height = tensor.get_shape()[1]; |
| 88 | + size_t width = tensor.get_shape()[2]; |
| 89 | + size_t channels = tensor.get_shape()[3]; |
| 90 | + |
| 91 | + if (channels != 3 && channels != 1) { |
| 92 | + throw std::runtime_error{"Only 1 or 3 channel images are supported for saving"}; |
| 93 | + } |
| 94 | + |
| 95 | + // Get pointer to image data |
| 96 | + unsigned char* image_data = tensor.data<unsigned char>(); |
| 97 | + |
| 98 | + // Create a memory buffer to hold the PNG data |
| 99 | + std::vector<unsigned char> png_buffer; |
| 100 | + |
| 101 | + // Define the write function that will store data in our buffer |
| 102 | + auto write_func = [](void* context, void* data, int size) { |
| 103 | + std::vector<unsigned char>* buffer = static_cast<std::vector<unsigned char>*>(context); |
| 104 | + unsigned char* bytes = static_cast<unsigned char*>(data); |
| 105 | + buffer->insert(buffer->end(), bytes, bytes + size); |
| 106 | + }; |
| 107 | + |
| 108 | + // Write PNG to memory using our buffer |
| 109 | + int success = stbi_write_png_to_func( |
| 110 | + write_func, // Our write function |
| 111 | + &png_buffer, // Context (our buffer) |
| 112 | + width, // Image width |
| 113 | + height, // Image height |
| 114 | + channels, // Number of channels |
| 115 | + image_data, // Image data |
| 116 | + width * channels); // Stride (bytes per row) |
| 117 | + |
| 118 | + if (!success) { |
| 119 | + throw std::runtime_error{"Failed to encode image to PNG format"}; |
| 120 | + } |
| 121 | + |
| 122 | + // Convert the buffer to a string |
| 123 | + return std::string(png_buffer.begin(), png_buffer.end()); |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace ovms |
0 commit comments