Blender V5.3
format_dpx.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
12#include "IMB_filetype.hh"
13#include "IMB_imbuf_types.hh"
14
15namespace blender {
16
17const char *imb_file_extensions_dpx[] = {".dpx", nullptr};
18const char *imb_file_extensions_cineon[] = {".cin", nullptr};
19
20OIIO_NAMESPACE_USING
21using namespace blender::imbuf;
22
23bool imb_is_a_dpx(const uchar *mem, size_t size)
24{
25 return imb_oiio_check(mem, size, "dpx");
26}
27
29 size_t size,
30 ImBufFlags flags,
31 ImFileColorSpace &r_colorspace)
32{
33 ImageSpec config, spec;
34
35 ReadContext ctx{mem, size, "dpx", IMB_FTYPE_DPX, flags};
36
37 ImBuf *ibuf = imb_oiio_read(ctx, config, r_colorspace, spec);
38 if (ibuf) {
39 if (flag_is_set(flags, ImBufFlags::AlphaDetect)) {
41 }
42 }
43
44 r_colorspace.is_hdr_float = true;
45
46 return ibuf;
47}
48
49static std::tuple<WriteContext, ImageSpec> prepare_save_dpx(ImBuf *ibuf, ImBufFlags flags)
50{
51 int bits_per_sample = 8;
52 if (ibuf->foptions.flag & CINEON_10BIT) {
53 bits_per_sample = 10;
54 }
55 else if (ibuf->foptions.flag & CINEON_12BIT) {
56 bits_per_sample = 12;
57 }
58 else if (ibuf->foptions.flag & CINEON_16BIT) {
59 bits_per_sample = 16;
60 }
61
62 const int file_channels = ibuf->color_mode_channels_get();
63 const TypeDesc data_format = bits_per_sample == 8 ? TypeDesc::UINT8 : TypeDesc::UINT16;
64
65 WriteContext ctx = imb_create_write_context("dpx", ibuf, flags);
66 ImageSpec file_spec = imb_create_write_spec(ctx, file_channels, data_format);
67
68 const float max_value = powf(2, bits_per_sample) - 1.0f;
69 file_spec.attribute("oiio:BitsPerSample", bits_per_sample);
70 file_spec.attribute("dpx:WhiteLevel", 685.0f / 1023.0f * max_value);
71 file_spec.attribute("dpx:BlackLevel", 95.0f / 1023.0f * max_value);
72 file_spec.attribute("dpx:HighData", max_value);
73 file_spec.attribute("dpx:LowData", 0);
74 file_spec.attribute("dpx:LowQuantity", 0.0f);
75
76 if (ibuf->foptions.flag & CINEON_LOG) {
77 /* VERIFY: This matches previous code but seems odd. Needs a comment if confirmed. */
78 file_spec.attribute("dpx:Transfer", "Printing density");
79 file_spec.attribute("dpx:HighQuantity", 2.048f);
80 }
81 else {
82 file_spec.attribute("dpx:Transfer", "Linear");
83 file_spec.attribute("dpx:HighQuantity", max_value);
84 }
85
86 if (ELEM(bits_per_sample, 8, 16)) {
87 file_spec.attribute("dpx:Packing", "Packed");
88 }
89 else {
90 file_spec.attribute("dpx:Packing", "Filled, method A");
91 }
92 return {ctx, file_spec};
93}
94
95bool imb_save_dpx(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
96{
97 const auto [ctx, file_spec] = prepare_save_dpx(ibuf, flags);
98 return imb_oiio_write(ctx, filepath, file_spec);
99}
100
102{
103 const auto [ctx, file_spec] = prepare_save_dpx(ibuf, flags);
104 return imb_oiio_write_buffer(ctx, file_spec);
105}
106
107} // namespace blender
#define ELEM(...)
#define powf(x, y)
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.
bool imb_is_a_dpx(const uchar *mem, size_t size)
Definition format_dpx.cc:23
const char * imb_file_extensions_dpx[]
Definition format_dpx.cc:17
unsigned char uchar
Definition BKE_text.h:58
Vector< uint8_t > imb_save_buffer_dpx(ImBuf *ibuf, ImBufFlags flags)
static std::tuple< WriteContext, ImageSpec > prepare_save_dpx(ImBuf *ibuf, ImBufFlags flags)
Definition format_dpx.cc:49
ImBuf * imb_load_dpx(const uchar *mem, size_t size, ImBufFlags flags, ImFileColorSpace &r_colorspace)
Definition format_dpx.cc:28
bool imb_save_dpx(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
Definition format_dpx.cc:95
const char * imb_file_extensions_cineon[]
Definition format_dpx.cc:18
ImbFormatOptions foptions
int color_mode_channels_get() const