|
| 1 | +// Copyright Contributors to the OpenImageIO project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// https://github.com/AcademySoftwareFoundation/OpenImageIO |
| 4 | + |
| 5 | +#include "imiv_loaded_image.h" |
| 6 | + |
| 7 | +#include <cstddef> |
| 8 | +#include <string> |
| 9 | + |
| 10 | +using namespace OIIO; |
| 11 | + |
| 12 | +namespace Imiv { |
| 13 | + |
| 14 | +bool |
| 15 | +describe_loaded_image_layout(const LoadedImage& image, |
| 16 | + LoadedImageLayout& layout, |
| 17 | + std::string& error_message) |
| 18 | +{ |
| 19 | + error_message.clear(); |
| 20 | + layout = LoadedImageLayout(); |
| 21 | + |
| 22 | + if (image.width <= 0 || image.height <= 0 || image.nchannels <= 0 |
| 23 | + || image.channel_bytes == 0) { |
| 24 | + error_message = "invalid source image dimensions"; |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + const size_t width = static_cast<size_t>(image.width); |
| 29 | + const size_t height = static_cast<size_t>(image.height); |
| 30 | + const size_t channels = static_cast<size_t>(image.nchannels); |
| 31 | + const size_t pixel_stride = channels * image.channel_bytes; |
| 32 | + const size_t min_row_pitch = width * pixel_stride; |
| 33 | + if (pixel_stride == 0 || image.row_pitch_bytes < min_row_pitch) { |
| 34 | + error_message = "invalid source row pitch"; |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + const size_t required_bytes = image.row_pitch_bytes * height; |
| 39 | + if (image.pixels.size() < required_bytes) { |
| 40 | + error_message = "source pixel buffer is smaller than declared stride"; |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + layout.pixel_stride_bytes = pixel_stride; |
| 45 | + layout.min_row_pitch_bytes = min_row_pitch; |
| 46 | + layout.required_bytes = required_bytes; |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +bool |
| 51 | +loaded_image_pixel_pointer(const LoadedImage& image, int x, int y, |
| 52 | + const unsigned char*& out_pixel, |
| 53 | + LoadedImageLayout* out_layout, |
| 54 | + std::string* error_message) |
| 55 | +{ |
| 56 | + out_pixel = nullptr; |
| 57 | + LoadedImageLayout layout; |
| 58 | + std::string local_error; |
| 59 | + if (!describe_loaded_image_layout(image, layout, local_error)) { |
| 60 | + if (error_message != nullptr) |
| 61 | + *error_message = local_error; |
| 62 | + return false; |
| 63 | + } |
| 64 | + if (x < 0 || y < 0 || x >= image.width || y >= image.height) { |
| 65 | + if (error_message != nullptr) |
| 66 | + *error_message = "requested pixel is outside the loaded image"; |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + const size_t row_start = static_cast<size_t>(y) * image.row_pitch_bytes; |
| 71 | + const size_t pixel_start = static_cast<size_t>(x) * layout.pixel_stride_bytes; |
| 72 | + const size_t offset = row_start + pixel_start; |
| 73 | + if (offset + layout.pixel_stride_bytes > image.pixels.size()) { |
| 74 | + if (error_message != nullptr) |
| 75 | + *error_message = "requested pixel lies outside the loaded buffer"; |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + out_pixel = image.pixels.data() + offset; |
| 80 | + if (out_layout != nullptr) |
| 81 | + *out_layout = layout; |
| 82 | + if (error_message != nullptr) |
| 83 | + error_message->clear(); |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +bool |
| 88 | +imagebuf_from_loaded_image(const LoadedImage& image, ImageBuf& out, |
| 89 | + std::string& error_message) |
| 90 | +{ |
| 91 | + error_message.clear(); |
| 92 | + const TypeDesc format = upload_data_type_to_typedesc(image.type); |
| 93 | + if (format == TypeUnknown) { |
| 94 | + error_message = "unsupported source pixel type"; |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + LoadedImageLayout layout; |
| 99 | + if (!describe_loaded_image_layout(image, layout, error_message)) |
| 100 | + return false; |
| 101 | + |
| 102 | + ImageSpec spec(image.width, image.height, image.nchannels, format); |
| 103 | + if (image.channel_names.size() == static_cast<size_t>(image.nchannels)) |
| 104 | + spec.channelnames = image.channel_names; |
| 105 | + spec.attribute("Orientation", image.orientation); |
| 106 | + if (!image.metadata_color_space.empty()) |
| 107 | + spec.attribute("oiio:ColorSpace", image.metadata_color_space); |
| 108 | + |
| 109 | + out.reset(spec); |
| 110 | + const std::byte* begin = reinterpret_cast<const std::byte*>(image.pixels.data()); |
| 111 | + const cspan<std::byte> byte_span(begin, layout.required_bytes); |
| 112 | + const stride_t xstride = static_cast<stride_t>(layout.pixel_stride_bytes); |
| 113 | + const stride_t ystride = static_cast<stride_t>(image.row_pitch_bytes); |
| 114 | + if (!out.set_pixels(ROI::All(), format, byte_span, begin, xstride, ystride, |
| 115 | + AutoStride)) { |
| 116 | + error_message = out.geterror(); |
| 117 | + if (error_message.empty()) |
| 118 | + error_message = "failed to copy source pixels into ImageBuf"; |
| 119 | + return false; |
| 120 | + } |
| 121 | + return true; |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace Imiv |
0 commit comments