Blender V5.3
format_tiff.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_tiff[] = {".tif", ".tiff", ".tx", nullptr};
18
19OIIO_NAMESPACE_USING
20using namespace blender::imbuf;
21
22bool imb_is_a_tiff(const uchar *mem, size_t size)
23{
24 return imb_oiio_check(mem, size, "tif");
25}
26
28 size_t size,
29 ImBufFlags flags,
30 ImFileColorSpace &r_colorspace)
31{
32 ImageSpec config, spec;
33 config.attribute("oiio:UnassociatedAlpha", 1);
34
35 ReadContext ctx{mem, size, "tif", IMB_FTYPE_TIF, flags};
36
37 ImBuf *ibuf = imb_oiio_read(ctx, config, r_colorspace, spec);
38 if (ibuf) {
39 if (flag_is_set(flags, ImBufFlags::AlphaDetect)) {
40 if (spec.nchannels == 4 && spec.format == TypeDesc::UINT16) {
42 }
43 }
44 }
45
46 /* All TIFFs should be in default byte colorspace. */
47 r_colorspace.is_hdr_float = false;
48
49 return ibuf;
50}
51
52static std::tuple<WriteContext, ImageSpec> prepare_save_tiff(ImBuf *ibuf, ImBufFlags flags)
53{
54 const bool is_16bit = ((ibuf->foptions.flag & TIF_16BIT) && ibuf->float_data());
55 const int file_channels = ibuf->color_mode_channels_get();
56 const TypeDesc data_format = is_16bit ? TypeDesc::UINT16 : TypeDesc::UINT8;
57
58 WriteContext ctx = imb_create_write_context("tif", ibuf, flags, is_16bit);
59 ImageSpec file_spec = imb_create_write_spec(ctx, file_channels, data_format);
60
61 if (is_16bit && file_channels == 4) {
62 file_spec.attribute("oiio:UnassociatedAlpha", 0);
63 }
64 else {
65 file_spec.attribute("oiio:UnassociatedAlpha", 1);
66 }
67
69 file_spec.attribute("compression", "zip");
70 }
71 else if (ibuf->foptions.flag & TIF_COMPRESS_LZW) {
72 file_spec.attribute("compression", "lzw");
73 }
74 else if (ibuf->foptions.flag & TIF_COMPRESS_PACKBITS) {
75 file_spec.attribute("compression", "packbits");
76 }
77 else if (ibuf->foptions.flag & TIF_COMPRESS_NONE) {
78 file_spec.attribute("compression", "none");
79 }
80
81 return {ctx, file_spec};
82}
83
84bool imb_save_tiff(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
85{
86 const auto [ctx, file_spec] = prepare_save_tiff(ibuf, flags);
87 return imb_oiio_write(ctx, filepath, file_spec);
88}
89
91{
92 const auto [ctx, file_spec] = prepare_save_tiff(ibuf, flags);
93 return imb_oiio_write_buffer(ctx, file_spec);
94}
95
96} // namespace blender
#define TIF_COMPRESS_PACKBITS
#define TIF_COMPRESS_NONE
#define TIF_COMPRESS_DEFLATE
#define TIF_COMPRESS_LZW
#define TIF_16BIT
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_tiff[]
bool imb_save_tiff(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
unsigned char uchar
Definition BKE_text.h:58
ImBuf * imb_load_tiff(const uchar *mem, size_t size, ImBufFlags flags, ImFileColorSpace &r_colorspace)
Vector< uint8_t > imb_save_buffer_tiff(ImBuf *ibuf, ImBufFlags flags)
bool imb_is_a_tiff(const uchar *mem, size_t size)
static std::tuple< WriteContext, ImageSpec > prepare_save_tiff(ImBuf *ibuf, ImBufFlags flags)
ImbFormatOptions foptions
const float * float_data() const
int color_mode_channels_get() const