Blender V5.3
writeimage.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cerrno>
10#include <cstdlib>
11
12#include "BLI_path_utils.hh" /* For assertions. */
13
15#include "IMB_filetype.hh"
16#include "IMB_imbuf.hh"
17#include "IMB_imbuf_types.hh"
18
19#include "CLG_log.h"
20
21namespace blender {
22
23static CLG_LogRef LOG = {"image.write"};
24
25static void ensure_byte_buffer(ImBuf *ibuf, const ImFileType *type)
26{
27 /* If writing byte image from float buffer, create a byte buffer for writing.
28 *
29 * For color managed image writing, IMB_colormanagement_imbuf_for_write should
30 * have already created this byte buffer. This is a basic fallback for other
31 * cases where we do not have a specific desired output colorspace. */
32 if (!(type->flag & IM_FTYPE_FLOAT)) {
33 if (ibuf->byte_data() == nullptr && ibuf->float_data()) {
36 }
37 }
38}
39
40bool IMB_save_image(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
41{
42 errno = 0;
43
44 BLI_assert(!BLI_path_is_rel(filepath));
45
46 if (ibuf == nullptr) {
47 return false;
48 }
49 ibuf->flags = flags;
50
51 const ImFileType *type = IMB_file_type_from_ibuf(ibuf);
52 if (type == nullptr || type->save == nullptr) {
53 CLOG_ERROR(&LOG, "Couldn't save image to \"%s\"", filepath);
54 return false;
55 }
56
59
60 ensure_byte_buffer(ibuf, type);
61 return type->save(ibuf, filepath, flags);
62}
63
65{
66 if (ibuf == nullptr) {
67 return {};
68 }
69 ibuf->flags = flags;
70
71 const ImFileType *type = IMB_file_type_from_ibuf(ibuf);
72 if (type == nullptr || type->save_buffer == nullptr) {
73 CLOG_ERROR(&LOG, "Couldn't save image to buffer");
74 return {};
75 }
76
79
80 ensure_byte_buffer(ibuf, type);
81 return type->save_buffer(ibuf, flags);
82}
83
84} // namespace blender
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define IM_FTYPE_FLOAT
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const ImFileType * IMB_file_type_from_ibuf(const ImBuf *ibuf)
Definition filetype.cc:338
Vector< uint8_t > IMB_save_image_to_buffer(ImBuf *ibuf, ImBufFlags flags)
Definition writeimage.cc:64
bool IMB_save_image(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
Definition writeimage.cc:40
const ColorSpace * colormanage_colorspace_get_roled(const int role)
static void ensure_byte_buffer(ImBuf *ibuf, const ImFileType *type)
Definition writeimage.cc:25
void IMB_byte_from_float(ImBuf *ibuf)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static CLG_LogRef LOG
Definition logging.cpp:12
const ColorSpace * colorspace
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
const float * float_data() const
eImFileTypeCapability capability_write
bool(*) save(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
Vector< uint8_t >(*) save_buffer(ImBuf *ibuf, ImBufFlags flags)