Blender V5.0
metadata.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11
12#include "BLI_listbase.h"
13#include "BLI_string.h"
14
15#include "BKE_idprop.hh"
16
17#include "DNA_ID.h" /* ID property definitions. */
18
19#include "IMB_imbuf_types.hh"
20
21#include "IMB_metadata.hh"
22
24{
25 if (*metadata != nullptr) {
26 return;
27 }
28
29 *metadata = blender::bke::idprop::create_group("metadata").release();
30}
31
33{
34 if (metadata == nullptr) {
35 return;
36 }
37
38 IDP_FreeProperty(metadata);
39}
40
42 const char *key,
43 char *value,
44 const size_t value_maxncpy)
45{
46 if (metadata == nullptr) {
47 return false;
48 }
49
50 IDProperty *prop = IDP_GetPropertyFromGroup(metadata, key);
51
52 if (prop && prop->type == IDP_STRING) {
53 BLI_strncpy(value, IDP_string_get(prop), value_maxncpy);
54 return true;
55 }
56 return false;
57}
58
59void IMB_metadata_copy(ImBuf *ibuf_dst, const ImBuf *ibuf_src)
60{
61 BLI_assert(ibuf_dst != ibuf_src);
62 if (ibuf_src->metadata) {
63 IMB_metadata_free(ibuf_dst->metadata);
64 ibuf_dst->metadata = IDP_CopyProperty(ibuf_src->metadata);
65 }
66}
67
68void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
69{
70 BLI_assert(metadata);
71 IDProperty *prop = IDP_GetPropertyFromGroup(metadata, key);
72
73 if (prop != nullptr && prop->type != IDP_STRING) {
74 IDP_FreeFromGroup(metadata, prop);
75 prop = nullptr;
76 }
77
78 if (prop) {
79 IDP_AssignString(prop, value);
80 }
81 else {
82 prop = blender::bke::idprop::create(key, value).release();
83 IDP_AddToGroup(metadata, prop);
84 }
85}
86
87void IMB_metadata_foreach(const ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
88{
89 if (ibuf->metadata == nullptr) {
90 return;
91 }
92 LISTBASE_FOREACH (IDProperty *, prop, &ibuf->metadata->data.group) {
93 callback(prop->name, IDP_string_get(prop), userdata);
94 }
95}
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, blender::StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:747
void IDP_FreeFromGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:741
void IDP_AssignString(IDProperty *prop, const char *st) ATTR_NONNULL()
Definition idprop.cc:439
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1251
#define IDP_string_get(prop)
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:717
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:863
#define BLI_assert(a)
Definition BLI_assert.h:46
#define LISTBASE_FOREACH(type, var, list)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
ID and Library types, which are fundamental for SDNA.
@ IDP_STRING
void(*)(const char *field, const char *value, void *userdata) IMBMetadataForeachCb
void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
Definition metadata.cc:68
void IMB_metadata_ensure(IDProperty **metadata)
Definition metadata.cc:23
void IMB_metadata_free(IDProperty *metadata)
Definition metadata.cc:32
void IMB_metadata_foreach(const ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
Definition metadata.cc:87
bool IMB_metadata_get_field(const IDProperty *metadata, const char *key, char *value, const size_t value_maxncpy)
Definition metadata.cc:41
void IMB_metadata_copy(ImBuf *ibuf_dst, const ImBuf *ibuf_src)
Definition metadata.cc:59
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
ListBase group
Definition DNA_ID.h:143
IDPropertyData data
Definition DNA_ID.h:169
char type
Definition DNA_ID.h:156
IDProperty * metadata