10#include <OpenImageIO/image_span.h>
11#include <OpenImageIO/imagebuf.h>
12#include <OpenImageIO/imagebufalgo.h>
51 return "MemoryBufferWriter";
64 size_t end = offset +
size;
72 size_t size()
const override
85 if (components == 3) {
86 for (
int64_t i = 0; i < pixel_count; i++) {
87 pixels[i * 4 + 3] = alpha;
90 else if (components == 1) {
91 for (
int64_t i = 0; i < pixel_count; i++) {
92 pixels[i * 4 + 3] = alpha;
93 pixels[i * 4 + 2] = pixels[i * 4 + 0];
94 pixels[i * 4 + 1] = pixels[i * 4 + 0];
97 else if (components == 2) {
98 for (
int64_t i = 0; i < pixel_count; i++) {
99 pixels[i * 4 + 3] = pixels[i * 4 + 1];
100 pixels[i * 4 + 2] = pixels[i * 4 + 0];
101 pixels[i * 4 + 1] = pixels[i * 4 + 0];
108 ImageInput *
in,
int width,
int height,
int channels,
ImBufFlags flags,
bool use_all_planes)
111 constexpr bool is_float =
sizeof(
T) > 1;
121 else if (!use_all_planes) {
125 else if (channels == 3) {
142 const stride_t ibuf_xstride =
sizeof(
T) * 4;
143 const stride_t ibuf_ystride = ibuf_xstride * width;
144 const TypeDesc format = is_float ? TypeDesc::FLOAT : TypeDesc::UINT8;
147 void *ibuf_data = rect + ((stride_t(height) - 1) * ibuf_ystride);
149 bool ok =
in->read_image(
150 0, 0, 0, channels, format, ibuf_data, ibuf_xstride, -ibuf_ystride, AutoStride);
159 const T alpha_fill = is_float ? 1.0f : 0xFF;
167 const ImageSpec &spec,
175 string ics = spec.get_string_attribute(
"oiio:ColorSpace");
181 if (spec.getattribute(
"CICP", TypeDesc(TypeDesc::INT, 4), cicp,
true)) {
196 const ImageSpec &spec =
in->spec();
197 const int width = spec.width;
198 const int height = spec.height;
199 const bool has_alpha = spec.alpha_channel != -1;
200 const bool is_float = spec.format.basesize() > 1;
203 const int channels = spec.nchannels <= 4 ? spec.nchannels : 4;
208 if (spec.depth > 1) {
215 ImBuf *ibuf =
nullptr;
230 double x_res = spec.get_float_attribute(
"XResolution", 0.0f);
231 double y_res = spec.get_float_attribute(
"YResolution", 0.0f);
233 if (!(x_res > 0.0f && y_res > 0.0f)) {
234 x_res = spec.get_int_attribute(
"XResolution", 0);
235 y_res = spec.get_int_attribute(
"YResolution", 0);
238 if (x_res > 0.0f && y_res > 0.0f) {
240 auto unit = spec.get_string_attribute(
"ResolutionUnit",
"");
241 if (
ELEM(unit,
"in",
"inch")) {
242 scale = 100.0 / 2.54;
244 else if (unit ==
"cm") {
256 for (
const auto &attrib : spec.extra_attribs) {
257 if (attrib.name().find(
"ICCProfile") != string::npos) {
274 const ImageSpec &config,
275 Filesystem::IOMemReader &mem_reader,
276 ImageSpec &r_newspec)
279 unique_ptr<ImageInput>
in = ImageInput::create(format);
280 if (!(
in &&
in->valid_file(&mem_reader))) {
285 in->set_ioproxy(&mem_reader);
286 bool ok =
in->open(
"", r_newspec, config);
296 ImageSpec config, spec;
299 Filesystem::IOMemReader mem_reader(cspan<uchar>(mem, mem_size));
300 unique_ptr<ImageInput>
in = ImageInput::create(file_format);
301 return in &&
in->valid_file(&mem_reader);
305 const ImageSpec &config,
307 ImageSpec &r_newspec)
321#if OIIO_VERSION_MAJOR >= 3
322 const size_t original_channels_count = orig_buf.nchannels();
324 const int original_channels_count = orig_buf.nchannels();
327 if (original_channels_count > 1 && file_spec.nchannels == 1) {
330 float weights[4] = {0.0f, 0.0f, 0.0f, 0.0f};
332 ImageBufAlgo::channel_sum(final_buf, orig_buf, {weights, original_channels_count});
334 else if (original_channels_count == 1 && file_spec.nchannels > 1) {
339 const int channel_order[] = {0, 0, 0, -1};
340 const float channel_values[] = {0.0f, 0.0f, 0.0f, 1.0f};
341 const std::string channel_names[] = {
"R",
"G",
"B",
"A"};
342 ImageBufAlgo::channels(final_buf,
345 cspan<int>(channel_order, file_spec.nchannels),
346 cspan<float>(channel_values, file_spec.nchannels),
347 cspan<std::string>(channel_names, file_spec.nchannels));
349 else if (file_spec.nchannels == 2 && original_channels_count >= 2) {
352 const int channel_order[] = {0, 3};
353 const float channel_values[] = {0.0f, 1.0f};
354 const std::string channel_names[] = {
"Y",
"A"};
355 ImageBufAlgo::channels(final_buf, orig_buf, 2, channel_order, channel_values, channel_names);
357 else if (original_channels_count != file_spec.nchannels) {
359 int channel_order[4];
360 for (
int i = 0; i < 4; i++) {
364 const bool channel_exists = i + 1 <= original_channels_count;
365 channel_order[i] = channel_exists ? i : -1;
367 const float channel_values[] = {0.0f, 0.0f, 0.0f, 1.0f};
368 const std::string channel_names[] = {
"R",
"G",
"B",
"A"};
369 ImageBufAlgo::channels(final_buf,
372 cspan<int>(channel_order, file_spec.nchannels),
373 cspan<float>(channel_values, file_spec.nchannels),
374 cspan<std::string>(channel_names, file_spec.nchannels));
377 final_buf = std::move(orig_buf);
383 unique_ptr<ImageOutput>
out = ImageOutput::create(ctx.
file_format);
389 ImageBuf final_buf{};
393 bool write_ok =
false;
394 bool close_ok =
false;
395 if (
out->open(filepath, file_spec)) {
396 write_ok = final_buf.write(
out.get());
397 close_ok =
out->close();
400 const bool all_ok = write_ok && close_ok;
411 unique_ptr<ImageOutput>
out = ImageOutput::create(ctx.
file_format);
417 ImageBuf final_buf{};
422 out->set_ioproxy(&writer);
424 bool write_ok =
false;
425 bool close_ok =
false;
426 if (
out->open(
"", file_spec)) {
427 write_ok = final_buf.write(
out.get());
428 close_ok =
out->close();
430 const bool all_ok = write_ok && close_ok;
437 return std::move(writer.
buffer);
450 const int width = ibuf->
x;
451 const int height = ibuf->
y;
452 const bool use_float = prefer_float && (ibuf->
float_data() !=
nullptr);
454 auto make_image_span =
455 [](
const auto *buffer,
int width,
int height,
int channels,
size_t channel_size) {
456 const stride_t mem_xstride = channel_size * channels;
457 const stride_t mem_ystride = mem_xstride * width;
458 const std::byte *mem_start =
reinterpret_cast<const std::byte *
>(buffer);
461 const stride_t y_flip_offset = (stride_t(height) - 1) * mem_ystride;
462 return image_span<const std::byte>(mem_start + y_flip_offset,
476 ctx.
mem_span = make_image_span(ibuf->
float_data(), width, height, mem_channels,
sizeof(
float));
477 ctx.
mem_spec = ImageSpec(width, height, mem_channels, TypeDesc::FLOAT);
480 const int mem_channels = 4;
482 ctx.
mem_spec = ImageSpec(width, height, mem_channels, TypeDesc::UINT8);
490 const int width = ctx.
ibuf->
x;
491 const int height = ctx.
ibuf->
y;
492 ImageSpec file_spec(width, height, file_channels, data_format);
508 if (
char *colon = strchr(prop.name,
':')) {
509 std::string prefix(prop.name, colon);
510 Strutil::to_lower(prefix);
511 if (prefix ==
"oiio" ||
512 (!
STREQ(prefix.c_str(), ctx.
file_format) && OIIO::is_imageio_format_name(prefix)))
527 file_spec.attribute(
"ResolutionUnit",
"m");
528 file_spec.attribute(
"XResolution",
int(
round(ctx.
ibuf->
ppm[0])));
529 file_spec.attribute(
"YResolution",
int(
round(ctx.
ibuf->
ppm[1])));
533 file_spec.attribute(
"ResolutionUnit",
"in");
534 file_spec.attribute(
"XResolution",
float(ctx.
ibuf->
ppm[0] * 0.0254));
535 file_spec.attribute(
"YResolution",
float(ctx.
ibuf->
ppm[1] * 0.0254));
546 if (file_channels >= 3) {
549 file_spec.attribute(
"ICCProfile",
550 OIIO::TypeDesc(OIIO::TypeDesc::UINT8, icc_profile.
size()),
562 file_spec.attribute(
"CICP", TypeDesc(TypeDesc::INT, 4), cicp);
#define IDP_string_get(prop)
#define STRNCPY_UTF8(dst, src)
#define CLOG_ERROR(clg_ref,...)
ID and Library types, which are fundamental for SDNA.
float3x3 scale(float3x3 mat, float2 scale)
bool imb_oiio_write(const WriteContext &ctx, const char *filepath, const ImageSpec &file_spec)
static unique_ptr< ImageInput > get_oiio_reader(const char *format, const ImageSpec &config, Filesystem::IOMemReader &mem_reader, ImageSpec &r_newspec)
ImBuf * imb_oiio_read(const ReadContext &ctx, const ImageSpec &config, ImFileColorSpace &r_colorspace, ImageSpec &r_newspec)
bool imb_oiio_check(const uchar *mem, size_t mem_size, const char *file_format)
ImageSpec imb_create_write_spec(const WriteContext &ctx, int file_channels, TypeDesc data_format)
static ImBuf * load_pixels(ImageInput *in, int width, int height, int channels, ImBufFlags flags, bool use_all_planes)
WriteContext imb_create_write_context(const char *file_format, const ImBuf *ibuf, ImBufFlags flags, bool prefer_float)
static ImBuf * get_oiio_ibuf(ImageInput *in, const ReadContext &ctx, ImFileColorSpace &r_colorspace)
static void fill_all_channels(T *pixels, int width, int height, int components, T alpha)
static void oiio_write_prepare(const ImageSpec &file_spec, ImageBuf &orig_buf, ImageBuf &final_buf)
Vector< uint8_t > imb_oiio_write_buffer(const WriteContext &ctx, const ImageSpec &file_spec)
static void set_file_colorspace(ImFileColorSpace &r_colorspace, const ReadContext &ctx, const ImageSpec &spec, bool is_float)
const ColorSpace * IMB_colormanagement_space_from_cicp(const int cicp[4], ColorManagedFileOutput output)
Vector< char > IMB_colormanagement_space_to_icc_profile(const ColorSpace *colorspace)
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
static CLG_LogRef LOG_READ
bool IMB_colormanagement_space_to_cicp(const ColorSpace *colorspace, ColorManagedFileOutput output, bool rgb_matrix, int cicp[4])
void IMB_freeImBuf(ImBuf *ibuf)
const char * IMB_colormanagement_colorspace_get_name(const ColorSpace *colorspace)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static CLG_LogRef LOG_WRITE
void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
ocio::ColorSpace ColorSpace
ListBaseT< struct IDProperty > group
const ColorSpace * colorspace
const ColorSpace * colorspace
ImbFormatOptions foptions
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
IDProperty * metadata_for_write()
const IDProperty * metadata() const
const float * float_data() const
float * float_data_for_write()
char metadata_colorspace[IM_MAX_SPACE]
size_t write(const void *buf, size_t size) override
const char * proxytype() const override
size_t pwrite(const void *buf, size_t size, int64_t offset) override
size_t size() const override
bool use_metadata_colorspace
const eImbFileType file_type
OIIO::image_span< const std::byte > mem_span
void reserve(int new_size)
void resize(int new_size)