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) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 #pragma once
20 
21 /** \file
22  * \ingroup bke
23  */
24 
25 #include "BLI_utildefines.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 struct Depsgraph;
32 struct ID;
33 struct ImBuf;
34 struct Image;
35 struct ImageFormatData;
36 struct ImagePool;
37 struct ImageTile;
38 struct ImbFormatOptions;
39 struct Main;
40 struct Object;
41 struct RenderResult;
42 struct ReportList;
43 struct Scene;
44 struct StampData;
45 struct anim;
46 
47 #define IMA_MAX_SPACE 64
48 #define IMA_UDIM_MAX 1999
49 
50 void BKE_images_init(void);
51 void BKE_images_exit(void);
52 
53 void BKE_image_free_packedfiles(struct Image *image);
54 void BKE_image_free_views(struct Image *image);
55 void BKE_image_free_buffers(struct Image *image);
56 void BKE_image_free_buffers_ex(struct Image *image, bool do_lock);
57 void BKE_image_free_gputextures(struct Image *ima);
58 /* call from library */
59 void BKE_image_free(struct Image *image);
60 
61 typedef void(StampCallback)(void *data, const char *propname, char *propvalue, int len);
62 
63 void BKE_render_result_stamp_info(struct Scene *scene,
64                                   struct Object *camera,
65                                   struct RenderResult *rr,
66                                   bool allocate_only);
67 /**
68  * Fills in the static stamp data (i.e. everything except things that can change per frame).
69  * The caller is responsible for freeing the allocated memory.
70  */
71 struct StampData *BKE_stamp_info_from_scene_static(const struct Scene *scene);
72 bool BKE_stamp_is_known_field(const char *field_name);
73 void BKE_imbuf_stamp_info(struct RenderResult *rr, struct ImBuf *ibuf);
74 void BKE_stamp_info_from_imbuf(struct RenderResult *rr, struct ImBuf *ibuf);
75 void BKE_stamp_info_callback(void *data,
76                              struct StampData *stamp_data,
77                              StampCallback callback,
78                              bool noskip);
79 void BKE_render_result_stamp_data(struct RenderResult *rr, const char *key, const char *value);
80 struct StampData *BKE_stamp_data_copy(const struct StampData *stamp_data);
81 void BKE_stamp_data_free(struct StampData *stamp_data);
82 void BKE_image_stamp_buf(struct Scene *scene,
83                          struct Object *camera,
84                          const struct StampData *stamp_data_template,
85                          unsigned char *rect,
86                          float *rectf,
87                          int width,
88                          int height,
89                          int channels);
90 bool BKE_imbuf_alpha_test(struct ImBuf *ibuf);
91 int BKE_imbuf_write_stamp(struct Scene *scene,
92                           struct RenderResult *rr,
93                           struct ImBuf *ibuf,
94                           const char *name,
95                           const struct ImageFormatData *imf);
96 void BKE_imbuf_write_prepare(struct ImBuf *ibuf, const struct ImageFormatData *imf);
97 int BKE_imbuf_write(struct ImBuf *ibuf, const char *name, const struct ImageFormatData *imf);
98 int BKE_imbuf_write_as(struct ImBuf *ibuf,
99                        const char *name,
100                        struct ImageFormatData *imf,
101                        const bool save_copy);
102 void BKE_image_path_from_imformat(char *string,
103                                   const char *base,
104                                   const char *relbase,
105                                   int frame,
106                                   const struct ImageFormatData *im_format,
107                                   const bool use_ext,
108                                   const bool use_frames,
109                                   const char *suffix);
110 void BKE_image_path_from_imtype(char *string,
111                                 const char *base,
112                                 const char *relbase,
113                                 int frame,
114                                 const char imtype,
115                                 const bool use_ext,
116                                 const bool use_frames,
117                                 const char *suffix);
118 int BKE_image_path_ensure_ext_from_imformat(char *string, const struct ImageFormatData *im_format);
119 int BKE_image_path_ensure_ext_from_imtype(char *string, const char imtype);
120 char BKE_image_ftype_to_imtype(const int ftype, const struct ImbFormatOptions *options);
121 int BKE_image_imtype_to_ftype(const char imtype, struct ImbFormatOptions *r_options);
122 
123 bool BKE_imtype_is_movie(const char imtype);
124 bool BKE_imtype_supports_zbuf(const char imtype);
125 bool BKE_imtype_supports_compress(const char imtype);
126 bool BKE_imtype_supports_quality(const char imtype);
127 bool BKE_imtype_requires_linear_float(const char imtype);
128 char BKE_imtype_valid_channels(const char imtype, bool write_file);
129 char BKE_imtype_valid_depths(const char imtype);
130 
131 char BKE_imtype_from_arg(const char *arg);
132 
133 void BKE_imformat_defaults(struct ImageFormatData *im_format);
134 void BKE_imbuf_to_image_format(struct ImageFormatData *im_format, const struct ImBuf *imbuf);
135 
136 struct anim *openanim(const char *name,
137                       int flags,
138                       int streamindex,
139                       char colorspace[IMA_MAX_SPACE]);
140 struct anim *openanim_noload(const char *name,
141                              int flags,
142                              int streamindex,
143                              char colorspace[IMA_MAX_SPACE]);
144 
145 void BKE_image_tag_time(struct Image *ima);
146 
147 /* ********************************** NEW IMAGE API *********************** */
148 
149 /* ImageUser is in Texture, in Nodes, Background Image, Image Window, .... */
150 /* should be used in conjunction with an ID * to Image. */
151 struct ImageUser;
152 struct RenderData;
153 struct RenderPass;
154 struct RenderResult;
155 
156 /* ima->ok */
157 #define IMA_OK 1
158 #define IMA_OK_LOADED 2
159 
160 /* signals */
161 /* reload only frees, doesn't read until image_get_ibuf() called */
162 #define IMA_SIGNAL_RELOAD 0
163 #define IMA_SIGNAL_FREE 1
164 /* source changes, from image to sequence or movie, etc */
165 #define IMA_SIGNAL_SRC_CHANGE 5
166 /* image-user gets a new image, check settings */
167 #define IMA_SIGNAL_USER_NEW_IMAGE 6
168 #define IMA_SIGNAL_COLORMANAGE 7
169 
170 #define IMA_CHAN_FLAG_BW 1
171 #define IMA_CHAN_FLAG_RGB 2
172 #define IMA_CHAN_FLAG_ALPHA 4
173 
174 /* checks whether there's an image buffer for given image and user */
175 bool BKE_image_has_ibuf(struct Image *ima, struct ImageUser *iuser);
176 
177 /* same as above, but can be used to retrieve images being rendered in
178  * a thread safe way, always call both acquire and release */
179 struct ImBuf *BKE_image_acquire_ibuf(struct Image *ima, struct ImageUser *iuser, void **r_lock);
180 void BKE_image_release_ibuf(struct Image *ima, struct ImBuf *ibuf, void *lock);
181 
182 struct ImagePool *BKE_image_pool_new(void);
183 void BKE_image_pool_free(struct ImagePool *pool);
184 struct ImBuf *BKE_image_pool_acquire_ibuf(struct Image *ima,
185                                           struct ImageUser *iuser,
186                                           struct ImagePool *pool);
187 void BKE_image_pool_release_ibuf(struct Image *ima, struct ImBuf *ibuf, struct ImagePool *pool);
188 
189 /* set an alpha mode based on file extension */
190 char BKE_image_alpha_mode_from_extension_ex(const char *filepath);
191 void BKE_image_alpha_mode_from_extension(struct Image *image);
192 
193 /* returns a new image or NULL if it can't load */
194 struct Image *BKE_image_load(struct Main *bmain, const char *filepath);
195 /* returns existing Image when filename/type is same (frame optional) */
196 struct Image *BKE_image_load_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists);
197 struct Image *BKE_image_load_exists(struct Main *bmain, const char *filepath);
198 
199 /* adds image, adds ibuf, generates color or pattern */
200 struct Image *BKE_image_add_generated(struct Main *bmain,
201                                       unsigned int width,
202                                       unsigned int height,
203                                       const char *name,
204                                       int depth,
205                                       int floatbuf,
206                                       short gen_type,
207                                       const float color[4],
208                                       const bool stereo3d,
209                                       const bool is_data,
210                                       const bool tiled);
211 /* adds image from imbuf, owns imbuf */
212 struct Image *BKE_image_add_from_imbuf(struct Main *bmain, struct ImBuf *ibuf, const char *name);
213 
214 /* for reload, refresh, pack */
215 void BKE_imageuser_default(struct ImageUser *iuser);
216 void BKE_image_init_imageuser(struct Image *ima, struct ImageUser *iuser);
217 void BKE_image_signal(struct Main *bmain, struct Image *ima, struct ImageUser *iuser, int signal);
218 
219 void BKE_image_walk_all_users(const struct Main *mainp,
220                               void *customdata,
221                               void callback(struct Image *ima,
222                                             struct ID *iuser_id,
223                                             struct ImageUser *iuser,
224                                             void *customdata));
225 
226 /* ensures an Image exists for viewing nodes or render */
227 struct Image *BKE_image_ensure_viewer(struct Main *bmain, int type, const char *name);
228 /* ensures the view node cache is compatible with the scene views */
229 void BKE_image_ensure_viewer_views(const struct RenderData *rd,
230                                    struct Image *ima,
231                                    struct ImageUser *iuser);
232 
233 /* called on frame change or before render */
234 void BKE_image_user_frame_calc(struct Image *ima, struct ImageUser *iuser, int cfra);
235 int BKE_image_user_frame_get(const struct ImageUser *iuser, int cfra, bool *r_is_in_range);
236 void BKE_image_user_file_path(struct ImageUser *iuser, struct Image *ima, char *path);
237 void BKE_image_editors_update_frame(const struct Main *bmain, int cfra);
238 
239 /* dependency graph update for image user users */
240 bool BKE_image_user_id_has_animation(struct ID *id);
241 void BKE_image_user_id_eval_animation(struct Depsgraph *depsgraph, struct ID *id);
242 
243 /* sets index offset for multilayer files */
244 struct RenderPass *BKE_image_multilayer_index(struct RenderResult *rr, struct ImageUser *iuser);
245 
246 /* sets index offset for multiview files */
247 void BKE_image_multiview_index(struct Image *ima, struct ImageUser *iuser);
248 
249 /* for multilayer images as well as for render-viewer */
250 bool BKE_image_is_multilayer(struct Image *ima);
251 bool BKE_image_is_multiview(struct Image *ima);
252 bool BKE_image_is_stereo(struct Image *ima);
253 struct RenderResult *BKE_image_acquire_renderresult(struct Scene *scene, struct Image *ima);
254 void BKE_image_release_renderresult(struct Scene *scene, struct Image *ima);
255 
256 /* for multilayer images as well as for singlelayer */
257 bool BKE_image_is_openexr(struct Image *ima);
258 
259 /* for multiple slot render, call this before render */
260 void BKE_image_backup_render(struct Scene *scene, struct Image *ima, bool free_current_slot);
261 
262 /* for singlelayer openexr saving */
263 bool BKE_image_save_openexr_multiview(struct Image *ima,
264                                       struct ImBuf *ibuf,
265                                       const char *filepath,
266                                       const int flags);
267 
268 /* goes over all textures that use images */
269 void BKE_image_free_all_textures(struct Main *bmain);
270 
271 /* does one image! */
272 void BKE_image_free_anim_ibufs(struct Image *ima, int except_frame);
273 
274 /* does all images with type MOVIE or SEQUENCE */
275 void BKE_image_all_free_anim_ibufs(struct Main *bmain, int cfra);
276 
277 void BKE_image_free_all_gputextures(struct Main *bmain);
278 void BKE_image_free_anim_gputextures(struct Main *bmain);
279 void BKE_image_free_old_gputextures(struct Main *bmain);
280 
281 bool BKE_image_memorypack(struct Image *ima);
282 void BKE_image_packfiles(struct ReportList *reports, struct Image *ima, const char *basepath);
283 void BKE_image_packfiles_from_mem(struct ReportList *reports,
284                                   struct Image *ima,
285                                   char *data,
286                                   const size_t data_len);
287 
288 /* prints memory statistics for images */
289 void BKE_image_print_memlist(struct Main *bmain);
290 
291 /* merge source into dest, and free source */
292 void BKE_image_merge(struct Main *bmain, struct Image *dest, struct Image *source);
293 
294 /* scale the image */
295 bool BKE_image_scale(struct Image *image, int width, int height);
296 
297 /* check if texture has alpha (depth=32) */
298 bool BKE_image_has_alpha(struct Image *image);
299 
300 /* check if texture has gpu texture code */
301 bool BKE_image_has_opengl_texture(struct Image *ima);
302 
303 /* get tile index for tiled images */
304 void BKE_image_get_tile_label(struct Image *ima,
305                               struct ImageTile *tile,
306                               char *label,
307                               int len_label);
308 
309 struct ImageTile *BKE_image_add_tile(struct Image *ima, int tile_number, const char *label);
310 bool BKE_image_remove_tile(struct Image *ima, struct ImageTile *tile);
311 
312 bool BKE_image_fill_tile(struct Image *ima,
313                          struct ImageTile *tile,
314                          int width,
315                          int height,
316                          const float color[4],
317                          int gen_type,
318                          int planes,
319                          bool is_float);
320 
321 struct ImageTile *BKE_image_get_tile(struct Image *ima, int tile_number);
322 struct ImageTile *BKE_image_get_tile_from_iuser(struct Image *ima, const struct ImageUser *iuser);
323 
324 int BKE_image_get_tile_from_pos(struct Image *ima,
325                                 const float uv[2],
326                                 float r_uv[2],
327                                 float r_ofs[2]);
328 
329 void BKE_image_get_size(struct Image *image, struct ImageUser *iuser, int *r_width, int *r_height);
330 void BKE_image_get_size_fl(struct Image *image, struct ImageUser *iuser, float r_size[2]);
331 void BKE_image_get_aspect(struct Image *image, float *r_aspx, float *r_aspy);
332 
333 /* image_gen.c */
334 void BKE_image_buf_fill_color(
335     unsigned char *rect, float *rect_float, int width, int height, const float color[4]);
336 void BKE_image_buf_fill_checker(unsigned char *rect, float *rect_float, int width, int height);
337 void BKE_image_buf_fill_checker_color(unsigned char *rect,
338                                       float *rect_float,
339                                       int width,
340                                       int height);
341 
342 /* Cycles hookup */
343 unsigned char *BKE_image_get_pixels_for_frame(struct Image *image, int frame, int tile);
344 float *BKE_image_get_float_pixels_for_frame(struct Image *image, int frame, int tile);
345 
346 /* Image modifications */
347 bool BKE_image_is_dirty(struct Image *image);
348 void BKE_image_mark_dirty(struct Image *image, struct ImBuf *ibuf);
349 bool BKE_image_buffer_format_writable(struct ImBuf *ibuf);
350 bool BKE_image_is_dirty_writable(struct Image *image, bool *is_format_writable);
351 
352 /* Guess offset for the first frame in the sequence */
353 int BKE_image_sequence_guess_offset(struct Image *image);
354 bool BKE_image_has_anim(struct Image *image);
355 bool BKE_image_has_packedfile(struct Image *image);
356 bool BKE_image_has_filepath(struct Image *ima);
357 bool BKE_image_is_animated(struct Image *image);
358 bool BKE_image_has_multiple_ibufs(struct Image *image);
359 void BKE_image_file_format_set(struct Image *image,
360                                int ftype,
361                                const struct ImbFormatOptions *options);
362 bool BKE_image_has_loaded_ibuf(struct Image *image);
363 struct ImBuf *BKE_image_get_ibuf_with_name(struct Image *image, const char *name);
364 struct ImBuf *BKE_image_get_first_ibuf(struct Image *image);
365 
366 /* Not to be use directly. */
367 struct GPUTexture *BKE_image_create_gpu_texture_from_ibuf(struct Image *image, struct ImBuf *ibuf);
368 
369 /* Get the GPUTexture for a given `Image`.
370  *
371  * `iuser` and `ibuf` are mutual exclusive parameters. The caller can pass the `ibuf` when already
372  * available. It is also required when requesting the GPUTexture for a render result. */
373 struct GPUTexture *BKE_image_get_gpu_texture(struct Image *image,
374                                              struct ImageUser *iuser,
375                                              struct ImBuf *ibuf);
376 struct GPUTexture *BKE_image_get_gpu_tiles(struct Image *image,
377                                            struct ImageUser *iuser,
378                                            struct ImBuf *ibuf);
379 struct GPUTexture *BKE_image_get_gpu_tilemap(struct Image *image,
380                                              struct ImageUser *iuser,
381                                              struct ImBuf *ibuf);
382 bool BKE_image_has_gpu_texture_premultiplied_alpha(struct Image *image, struct ImBuf *ibuf);
383 void BKE_image_update_gputexture(
384     struct Image *ima, struct ImageUser *iuser, int x, int y, int w, int h);
385 void BKE_image_paint_set_mipmap(struct Main *bmain, bool mipmap);
386 
387 /* Delayed free of OpenGL buffers by main thread */
388 void BKE_image_free_unused_gpu_textures(void);
389 
390 struct RenderSlot *BKE_image_add_renderslot(struct Image *ima, const char *name);
391 bool BKE_image_remove_renderslot(struct Image *ima, struct ImageUser *iuser, int slot);
392 struct RenderSlot *BKE_image_get_renderslot(struct Image *ima, int index);
393 bool BKE_image_clear_renderslot(struct Image *ima, struct ImageUser *iuser, int slot);
394 
395 #ifdef __cplusplus
396 }
397 #endif
398