Blender V4.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
10
12#include "IMB_filetype.hh"
13#include "IMB_imbuf_types.hh"
14
15OIIO_NAMESPACE_USING
16using namespace blender::imbuf;
17
18bool imb_is_a_tiff(const uchar *mem, size_t size)
19{
20 return imb_oiio_check(mem, size, "tif");
21}
22
23ImBuf *imb_load_tiff(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
24{
25 ImageSpec config, spec;
26 config.attribute("oiio:UnassociatedAlpha", 1);
27
28 ReadContext ctx{mem, size, "tif", IMB_FTYPE_TIF, flags};
29
30 /* All TIFFs should be in default byte colorspace. */
32
33 ImBuf *ibuf = imb_oiio_read(ctx, config, colorspace, spec);
34 if (ibuf) {
35 if (flags & IB_alphamode_detect) {
36 if (spec.nchannels == 4 && spec.format == TypeDesc::UINT16) {
38 }
39 }
40 }
41
42 return ibuf;
43}
44
45bool imb_save_tiff(ImBuf *ibuf, const char *filepath, int flags)
46{
47 const bool is_16bit = ((ibuf->foptions.flag & TIF_16BIT) && ibuf->float_buffer.data);
48 const int file_channels = ibuf->planes >> 3;
49 const TypeDesc data_format = is_16bit ? TypeDesc::UINT16 : TypeDesc::UINT8;
50
51 WriteContext ctx = imb_create_write_context("tif", ibuf, flags, is_16bit);
52 ImageSpec file_spec = imb_create_write_spec(ctx, file_channels, data_format);
53
54 if (is_16bit && file_channels == 4) {
55 file_spec.attribute("oiio:UnassociatedAlpha", 0);
56 }
57 else {
58 file_spec.attribute("oiio:UnassociatedAlpha", 1);
59 }
60
62 file_spec.attribute("compression", "zip");
63 }
64 else if (ibuf->foptions.flag & TIF_COMPRESS_LZW) {
65 file_spec.attribute("compression", "lzw");
66 }
67 else if (ibuf->foptions.flag & TIF_COMPRESS_PACKBITS) {
68 file_spec.attribute("compression", "packbits");
69 }
70 else if (ibuf->foptions.flag & TIF_COMPRESS_NONE) {
71 file_spec.attribute("compression", "none");
72 }
73
74 return imb_oiio_write(ctx, filepath, file_spec);
75}
unsigned char uchar
@ COLOR_ROLE_DEFAULT_BYTE
#define IM_MAX_SPACE
Definition IMB_imbuf.hh:49
@ IMB_FTYPE_TIF
Contains defines and structs used throughout the imbuf module.
#define TIF_COMPRESS_PACKBITS
#define TIF_COMPRESS_NONE
#define TIF_COMPRESS_DEFLATE
#define TIF_COMPRESS_LZW
#define TIF_16BIT
@ IB_alphamode_premul
@ IB_alphamode_detect
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
bool imb_is_a_tiff(const uchar *mem, size_t size)
ImBuf * imb_load_tiff(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
bool imb_save_tiff(ImBuf *ibuf, const char *filepath, int flags)
bool imb_oiio_write(const WriteContext &ctx, const char *filepath, const ImageSpec &file_spec)
WriteContext imb_create_write_context(const char *file_format, ImBuf *ibuf, int flags, bool prefer_float)
ImBuf * imb_oiio_read(const ReadContext &ctx, const ImageSpec &config, char colorspace[IM_MAX_SPACE], 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)
long long TypeDesc
ImBufFloatBuffer float_buffer
ImbFormatOptions foptions
unsigned char planes