Blender V5.3
format_targa.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_tga[] = {".tga", ".tpic", nullptr};
17
18OIIO_NAMESPACE_USING
19using namespace blender::imbuf;
20
21bool imb_is_a_tga(const uchar *mem, size_t size)
22{
23 return imb_oiio_check(mem, size, "tga");
24}
25
27 size_t size,
28 ImBufFlags flags,
29 ImFileColorSpace &r_colorspace)
30{
31 ImageSpec config, spec;
32 config.attribute("oiio:UnassociatedAlpha", 1);
33
34 ReadContext ctx{mem, size, "tga", IMB_FTYPE_TGA, flags};
35 return imb_oiio_read(ctx, config, r_colorspace, spec);
36}
37
38static std::tuple<WriteContext, ImageSpec> prepare_save_tga(ImBuf *ibuf, ImBufFlags flags)
39{
40 const int file_channels = ibuf->color_mode_channels_get();
41 const TypeDesc data_format = TypeDesc::UINT8;
42
43 WriteContext ctx = imb_create_write_context("tga", ibuf, flags, false);
44 ImageSpec file_spec = imb_create_write_spec(ctx, file_channels, data_format);
45 file_spec.attribute("oiio:UnassociatedAlpha", 1);
46 file_spec.attribute("compression", (ibuf->foptions.flag & RAWTGA) ? "none" : "rle");
47
48 return {ctx, file_spec};
49}
50
51bool imb_save_tga(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
52{
53 const auto [ctx, file_spec] = prepare_save_tga(ibuf, flags);
54 return imb_oiio_write(ctx, filepath, file_spec);
55}
56
58{
59 const auto [ctx, file_spec] = prepare_save_tga(ibuf, flags);
60 return imb_oiio_write_buffer(ctx, file_spec);
61}
62
63} // namespace blender
#define RAWTGA
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_tga[]
Vector< uint8_t > imb_save_buffer_tga(ImBuf *ibuf, ImBufFlags flags)
bool imb_save_tga(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
bool imb_is_a_tga(const uchar *mem, size_t size)
unsigned char uchar
Definition BKE_text.h:58
static std::tuple< WriteContext, ImageSpec > prepare_save_tga(ImBuf *ibuf, ImBufFlags flags)
ImBuf * imb_load_tga(const uchar *mem, size_t size, ImBufFlags flags, ImFileColorSpace &r_colorspace)
ImbFormatOptions foptions
int color_mode_channels_get() const