Blender V4.3
format_png.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_png(const uchar *mem, size_t size)
19{
20 return imb_oiio_check(mem, size, "png");
21}
22
23ImBuf *imb_load_png(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, "png", IMB_FTYPE_PNG, flags};
29
30 /* Both 8 and 16 bit PNGs should be in default byte colorspace. */
32
33 ImBuf *ibuf = imb_oiio_read(ctx, config, colorspace, spec);
34 if (ibuf) {
35 if (spec.format == TypeDesc::UINT16) {
36 ibuf->flags |= PNG_16BIT;
37 }
38 }
39
40 return ibuf;
41}
42
43bool imb_save_png(ImBuf *ibuf, const char *filepath, int flags)
44{
45 const bool is_16bit = (ibuf->foptions.flag & PNG_16BIT);
46 const int file_channels = ibuf->planes >> 3;
47 const TypeDesc data_format = is_16bit ? TypeDesc::UINT16 : TypeDesc::UINT8;
48
49 WriteContext ctx = imb_create_write_context("png", ibuf, flags, is_16bit);
50 ImageSpec file_spec = imb_create_write_spec(ctx, file_channels, data_format);
51
52 /* Skip if the float buffer was managed already. */
53 if (is_16bit &&
55 {
56 file_spec.attribute("oiio:UnassociatedAlpha", 0);
57 }
58 else {
59 file_spec.attribute("oiio:UnassociatedAlpha", 1);
60 }
61
62 int compression = int(float(ibuf->foptions.quality) / 11.1111f);
63 compression = compression < 0 ? 0 : (compression > 9 ? 9 : compression);
64 file_spec.attribute("png:compressionLevel", compression);
65
66 return imb_oiio_write(ctx, filepath, file_spec);
67}
unsigned char uchar
@ COLOR_ROLE_DEFAULT_BYTE
#define IM_MAX_SPACE
Definition IMB_imbuf.hh:49
@ IMB_FTYPE_PNG
Contains defines and structs used throughout the imbuf module.
#define PNG_16BIT
@ IMB_COLORMANAGE_IS_DATA
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
ImBuf * imb_load_png(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
Definition format_png.cc:23
bool imb_is_a_png(const uchar *mem, size_t size)
Definition format_png.cc:18
bool imb_save_png(ImBuf *ibuf, const char *filepath, int flags)
Definition format_png.cc:43
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
ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImbFormatOptions foptions
int colormanage_flag
unsigned char planes