Blender V5.3
format_hdr.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "IMB_filetype.hh"
12#include "IMB_imbuf_types.hh"
13
14namespace blender {
15
16const char *imb_file_extensions_hdr[] = {".hdr", nullptr};
17
18OIIO_NAMESPACE_USING
19using namespace blender::imbuf;
20
21bool imb_is_a_hdr(const uchar *mem, size_t size)
22{
23 return imb_oiio_check(mem, size, "hdr");
24}
25
27 size_t size,
28 ImBufFlags flags,
29 ImFileColorSpace &r_colorspace)
30{
31 ImageSpec config, spec;
32
33 ReadContext ctx{mem, size, "hdr", IMB_FTYPE_RADHDR, flags};
34
35 /* Always create ImBufs with a 4th alpha channel despite the format only supporting 3. */
36 ctx.use_all_planes = true;
37
38 ImBuf *ibuf = imb_oiio_read(ctx, config, r_colorspace, spec);
39 if (ibuf) {
40 if (flag_is_set(flags, ImBufFlags::AlphaDetect)) {
42 }
43 if (flag_is_set(flags, ImBufFlags::ByteData)) {
45 }
46 }
47
48 return ibuf;
49}
50
51static std::tuple<WriteContext, ImageSpec> prepare_save_hdr(ImBuf *ibuf, ImBufFlags flags)
52{
53 const int file_channels = 3;
54 const TypeDesc data_format = TypeDesc::FLOAT;
55 WriteContext ctx = imb_create_write_context("hdr", ibuf, flags);
56 ImageSpec file_spec = imb_create_write_spec(ctx, file_channels, data_format);
57 return {ctx, file_spec};
58}
59
60bool imb_save_hdr(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
61{
62 const auto [ctx, file_spec] = prepare_save_hdr(ibuf, flags);
63 return imb_oiio_write(ctx, filepath, file_spec);
64}
65
67{
68 const auto [ctx, file_spec] = prepare_save_hdr(ibuf, flags);
69 return imb_oiio_write_buffer(ctx, file_spec);
70}
71
72} // namespace blender
bool imb_oiio_write(const WriteContext &ctx, const char *filepath, const ImageSpec &file_spec)
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)
WriteContext imb_create_write_context(const char *file_format, const ImBuf *ibuf, ImBufFlags flags, bool prefer_float)
Vector< uint8_t > imb_oiio_write_buffer(const WriteContext &ctx, const ImageSpec &file_spec)
BMesh Iterators.
const char * imb_file_extensions_hdr[]
Definition format_hdr.cc:16
Vector< uint8_t > imb_save_buffer_hdr(ImBuf *ibuf, ImBufFlags flags)
Definition format_hdr.cc:66
static std::tuple< WriteContext, ImageSpec > prepare_save_hdr(ImBuf *ibuf, ImBufFlags flags)
Definition format_hdr.cc:51
unsigned char uchar
Definition BKE_text.h:58
bool imb_is_a_hdr(const uchar *mem, size_t size)
Definition format_hdr.cc:21
ImBuf * imb_load_hdr(const uchar *mem, size_t size, ImBufFlags flags, ImFileColorSpace &r_colorspace)
Definition format_hdr.cc:26
void IMB_byte_from_float(ImBuf *ibuf)
bool imb_save_hdr(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
Definition format_hdr.cc:60