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) 2006-2007 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
22 /** \file
23  * \ingroup bke
24  *
25  * Resizable Icons for Blender
26  */
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 typedef void (*DrawInfoFreeFP)(void *drawinfo);
33 
34 enum {
35   /** ID preview: obj is #ID. */
36   ICON_DATA_ID = 0,
37   /** Preview: obj is #PreviewImage */
38   ICON_DATA_PREVIEW,
39   /** 2D triangles: obj is #Icon_Geom */
40   ICON_DATA_GEOM,
41   /** Studiolight */
42   ICON_DATA_STUDIOLIGHT,
43   /** GPencil Layer color preview (annotations): obj is #bGPDlayer */
44   ICON_DATA_GPLAYER,
45 };
46 
47 struct Icon {
48   void *drawinfo;
49   /**
50    * Data defined by #obj_type
51    * \note for #ICON_DATA_GEOM the memory is owned by the icon,
52    * could be made into a flag if we want that to be optional.
53    */
54   void *obj;
55   char obj_type;
56   /** Internal use only. */
57   char flag;
58   /** #ID_Type or 0 when not used for ID preview. */
59   short id_type;
60   DrawInfoFreeFP drawinfo_free;
61 };
62 
63 /** Used for #ICON_DATA_GEOM, assigned to #Icon.obj. */
64 struct Icon_Geom {
65   int icon_id;
66   int coords_len;
67   int coords_range[2];
68   unsigned char (*coords)[2];
69   unsigned char (*colors)[4];
70   /* when not NULL, the memory of coords and colors is a sub-region of this pointer. */
71   const void *mem;
72 };
73 
74 typedef struct Icon Icon;
75 
76 struct BlendDataReader;
77 struct BlendWriter;
78 struct ID;
79 struct ImBuf;
80 struct PreviewImage;
81 struct StudioLight;
82 struct bGPDlayer;
83 
84 enum eIconSizes;
85 
86 void BKE_icons_init(int first_dyn_id);
87 
88 /* return icon id for library object or create new icon if not found */
89 int BKE_icon_id_ensure(struct ID *id);
90 
91 /* return icon id for Grease Pencil layer (color preview) or create new icon if not found */
92 int BKE_icon_gplayer_color_ensure(struct bGPDlayer *gpl);
93 
94 int BKE_icon_preview_ensure(struct ID *id, struct PreviewImage *preview);
95 
96 /* retrieve icon for id */
97 struct Icon *BKE_icon_get(const int icon_id);
98 
99 /* set icon for id if not already defined */
100 /* used for inserting the internal icons */
101 void BKE_icon_set(const int icon_id, struct Icon *icon);
102 
103 /* remove icon and free data if library object becomes invalid */
104 void BKE_icon_id_delete(struct ID *id);
105 
106 bool BKE_icon_delete(const int icon_id);
107 bool BKE_icon_delete_unmanaged(const int icon_id);
108 
109 /* report changes - icon needs to be recalculated */
110 void BKE_icon_changed(const int icon_id);
111 
112 /* free all icons */
113 void BKE_icons_free(void);
114 
115 /* free all icons marked for deferred deletion */
116 void BKE_icons_deferred_free(void);
117 
118 /* free the preview image for use in list */
119 void BKE_previewimg_freefunc(void *link);
120 
121 /* free the preview image */
122 void BKE_previewimg_free(struct PreviewImage **prv);
123 
124 /* clear the preview image or icon, but does not free it */
125 void BKE_previewimg_clear(struct PreviewImage *prv);
126 
127 /* clear the preview image or icon at a specific size */
128 void BKE_previewimg_clear_single(struct PreviewImage *prv, enum eIconSizes size);
129 
130 /* get the preview from any pointer */
131 struct PreviewImage **BKE_previewimg_id_get_p(const struct ID *id);
132 
133 /* free the preview image belonging to the id */
134 void BKE_previewimg_id_free(struct ID *id);
135 
136 /* create a new preview image */
137 struct PreviewImage *BKE_previewimg_create(void);
138 
139 /* create a copy of the preview image */
140 struct PreviewImage *BKE_previewimg_copy(const struct PreviewImage *prv);
141 
142 void BKE_previewimg_id_copy(struct ID *new_id, const struct ID *old_id);
143 
144 /* retrieve existing or create new preview image */
145 struct PreviewImage *BKE_previewimg_id_ensure(struct ID *id);
146 
147 void BKE_previewimg_ensure(struct PreviewImage *prv, const int size);
148 
149 struct PreviewImage *BKE_previewimg_cached_get(const char *name);
150 
151 struct PreviewImage *BKE_previewimg_cached_ensure(const char *name);
152 
153 struct PreviewImage *BKE_previewimg_cached_thumbnail_read(const char *name,
154                                                           const char *path,
155                                                           const int source,
156                                                           bool force_update);
157 
158 void BKE_previewimg_cached_release(const char *name);
159 void BKE_previewimg_cached_release_pointer(struct PreviewImage *prv);
160 
161 void BKE_previewimg_blend_write(struct BlendWriter *writer, const struct PreviewImage *prv);
162 void BKE_previewimg_blend_read(struct BlendDataReader *reader, struct PreviewImage *prv);
163 
164 int BKE_icon_geom_ensure(struct Icon_Geom *geom);
165 struct Icon_Geom *BKE_icon_geom_from_memory(const uchar *data, size_t data_len);
166 struct Icon_Geom *BKE_icon_geom_from_file(const char *filename);
167 
168 struct ImBuf *BKE_icon_geom_rasterize(const struct Icon_Geom *geom,
169                                       const unsigned int size_x,
170                                       const unsigned int size_y);
171 void BKE_icon_geom_invert_lightness(struct Icon_Geom *geom);
172 
173 int BKE_icon_ensure_studio_light(struct StudioLight *sl, int id_type);
174 
175 #define ICON_RENDER_DEFAULT_HEIGHT 32
176 
177 #ifdef __cplusplus
178 }
179 #endif
180