1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 Blender Foundation
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup imbuf
22  */
23 
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "BLI_string.h"
28 #include "BLI_utildefines.h"
29 
30 #include "BKE_idprop.h"
31 
32 #include "MEM_guardedalloc.h"
33 
34 #include "IMB_imbuf.h"
35 #include "IMB_imbuf_types.h"
36 
37 #include "IMB_metadata.h"
38 
39 #define METADATA_MAX_VALUE_LENGTH 1024
40 
IMB_metadata_ensure(struct IDProperty ** metadata)41 void IMB_metadata_ensure(struct IDProperty **metadata)
42 {
43   if (*metadata != NULL) {
44     return;
45   }
46 
47   IDPropertyTemplate val;
48   *metadata = IDP_New(IDP_GROUP, &val, "metadata");
49 }
50 
IMB_metadata_free(struct IDProperty * metadata)51 void IMB_metadata_free(struct IDProperty *metadata)
52 {
53   if (metadata == NULL) {
54     return;
55   }
56 
57   IDP_FreeProperty(metadata);
58 }
59 
IMB_metadata_get_field(struct IDProperty * metadata,const char * key,char * field,const size_t len)60 bool IMB_metadata_get_field(struct IDProperty *metadata,
61                             const char *key,
62                             char *field,
63                             const size_t len)
64 {
65   IDProperty *prop;
66 
67   if (metadata == NULL) {
68     return false;
69   }
70 
71   prop = IDP_GetPropertyFromGroup(metadata, key);
72 
73   if (prop && prop->type == IDP_STRING) {
74     BLI_strncpy(field, IDP_String(prop), len);
75     return true;
76   }
77   return false;
78 }
79 
IMB_metadata_copy(struct ImBuf * dimb,struct ImBuf * simb)80 void IMB_metadata_copy(struct ImBuf *dimb, struct ImBuf *simb)
81 {
82   BLI_assert(dimb != simb);
83   if (simb->metadata) {
84     IMB_metadata_free(dimb->metadata);
85     dimb->metadata = IDP_CopyProperty(simb->metadata);
86   }
87 }
88 
IMB_metadata_set_field(struct IDProperty * metadata,const char * key,const char * value)89 void IMB_metadata_set_field(struct IDProperty *metadata, const char *key, const char *value)
90 {
91   BLI_assert(metadata);
92   IDProperty *prop = IDP_GetPropertyFromGroup(metadata, key);
93 
94   if (prop != NULL && prop->type != IDP_STRING) {
95     IDP_FreeFromGroup(metadata, prop);
96     prop = NULL;
97   }
98 
99   if (prop == NULL) {
100     prop = IDP_NewString(value, key, METADATA_MAX_VALUE_LENGTH);
101     IDP_AddToGroup(metadata, prop);
102   }
103 
104   IDP_AssignString(prop, value, METADATA_MAX_VALUE_LENGTH);
105 }
106 
IMB_metadata_foreach(struct ImBuf * ibuf,IMBMetadataForeachCb callback,void * userdata)107 void IMB_metadata_foreach(struct ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
108 {
109   if (ibuf->metadata == NULL) {
110     return;
111   }
112   for (IDProperty *prop = ibuf->metadata->data.group.first; prop != NULL; prop = prop->next) {
113     callback(prop->name, IDP_String(prop), userdata);
114   }
115 }
116