Cleanup: style, use braces for blenkernel
[blender.git] / source / blender / blenkernel / intern / image_save.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2019
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/image_save.c
27  *  \ingroup bke
28  */
29
30 #include <errno.h>
31 #include <string.h>
32
33 #include "BLI_listbase.h"
34 #include "BLI_path_util.h"
35 #include "BLI_string.h"
36
37 #include "DNA_image_types.h"
38
39 #include "IMB_colormanagement.h"
40 #include "IMB_imbuf.h"
41 #include "IMB_imbuf_types.h"
42
43 #include "BKE_colortools.h"
44 #include "BKE_image.h"
45 #include "BKE_image_save.h"
46 #include "BKE_main.h"
47 #include "BKE_report.h"
48 #include "BKE_scene.h"
49
50 #include "RE_pipeline.h"
51
52 void BKE_image_save_options_init(ImageSaveOptions *opts, Main *bmain, Scene *scene)
53 {
54   memset(opts, 0, sizeof(*opts));
55
56   opts->bmain = bmain;
57   opts->scene = scene;
58
59   BKE_imformat_defaults(&opts->im_format);
60 }
61
62 static void image_save_post(ReportList *reports,
63                             Main *bmain,
64                             Image *ima,
65                             ImBuf *ibuf,
66                             int ok,
67                             ImageSaveOptions *opts,
68                             int save_copy,
69                             const char *filepath)
70 {
71   if (!ok) {
72     BKE_reportf(reports, RPT_ERROR, "Could not write image: %s", strerror(errno));
73     return;
74   }
75
76   if (save_copy) {
77     return;
78   }
79
80   if (opts->do_newpath) {
81     BLI_strncpy(ibuf->name, filepath, sizeof(ibuf->name));
82     BLI_strncpy(ima->name, filepath, sizeof(ima->name));
83   }
84
85   ibuf->userflags &= ~IB_BITMAPDIRTY;
86
87   /* change type? */
88   if (ima->type == IMA_TYPE_R_RESULT) {
89     ima->type = IMA_TYPE_IMAGE;
90
91     /* workaround to ensure the render result buffer is no longer used
92      * by this image, otherwise can crash when a new render result is
93      * created. */
94     if (ibuf->rect && !(ibuf->mall & IB_rect)) {
95       imb_freerectImBuf(ibuf);
96     }
97     if (ibuf->rect_float && !(ibuf->mall & IB_rectfloat)) {
98       imb_freerectfloatImBuf(ibuf);
99     }
100     if (ibuf->zbuf && !(ibuf->mall & IB_zbuf)) {
101       IMB_freezbufImBuf(ibuf);
102     }
103     if (ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat)) {
104       IMB_freezbuffloatImBuf(ibuf);
105     }
106   }
107   if (ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
108     ima->source = IMA_SRC_FILE;
109     ima->type = IMA_TYPE_IMAGE;
110   }
111
112   /* only image path, never ibuf */
113   if (opts->relative) {
114     const char *relbase = ID_BLEND_PATH(opts->bmain, &ima->id);
115     BLI_path_rel(ima->name, relbase); /* only after saving */
116   }
117
118   ColorManagedColorspaceSettings old_colorspace_settings;
119   BKE_color_managed_colorspace_settings_copy(&old_colorspace_settings, &ima->colorspace_settings);
120   IMB_colormanagement_colorspace_from_ibuf_ftype(&ima->colorspace_settings, ibuf);
121   if (!BKE_color_managed_colorspace_settings_equals(&old_colorspace_settings,
122                                                     &ima->colorspace_settings)) {
123     BKE_image_signal(bmain, ima, NULL, IMA_SIGNAL_COLORMANAGE);
124   }
125 }
126
127 static void imbuf_save_post(ImBuf *ibuf, ImBuf *colormanaged_ibuf)
128 {
129   if (colormanaged_ibuf != ibuf) {
130     /* This guys might be modified by image buffer write functions,
131      * need to copy them back from color managed image buffer to an
132      * original one, so file type of image is being properly updated.
133      */
134     ibuf->ftype = colormanaged_ibuf->ftype;
135     ibuf->foptions = colormanaged_ibuf->foptions;
136     ibuf->planes = colormanaged_ibuf->planes;
137
138     IMB_freeImBuf(colormanaged_ibuf);
139   }
140 }
141
142 /**
143  * \return success.
144  * \note ``ima->name`` and ``ibuf->name`` should end up the same.
145  * \note for multiview the first ``ibuf`` is important to get the settings.
146  */
147 bool BKE_image_save(
148     ReportList *reports, Main *bmain, Image *ima, ImageUser *iuser, ImageSaveOptions *opts)
149 {
150   void *lock;
151   ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock);
152   RenderResult *rr = NULL;
153   bool ok = false;
154
155   if (ibuf == NULL || (ibuf->rect == NULL && ibuf->rect_float == NULL)) {
156     BKE_image_release_ibuf(ima, ibuf, &lock);
157     goto cleanup;
158   }
159
160   ImBuf *colormanaged_ibuf = NULL;
161   const bool save_copy = opts->save_copy;
162   const bool save_as_render = opts->save_as_render;
163   ImageFormatData *imf = &opts->im_format;
164
165   if (ima->type == IMA_TYPE_R_RESULT) {
166     /* enforce user setting for RGB or RGBA, but skip BW */
167     if (opts->im_format.planes == R_IMF_PLANES_RGBA) {
168       ibuf->planes = R_IMF_PLANES_RGBA;
169     }
170     else if (opts->im_format.planes == R_IMF_PLANES_RGB) {
171       ibuf->planes = R_IMF_PLANES_RGB;
172     }
173   }
174   else {
175     /* TODO, better solution, if a 24bit image is painted onto it may contain alpha */
176     if ((opts->im_format.planes == R_IMF_PLANES_RGBA) &&
177         /* it has been painted onto */
178         (ibuf->userflags & IB_BITMAPDIRTY)) {
179       /* checks each pixel, not ideal */
180       ibuf->planes = BKE_imbuf_alpha_test(ibuf) ? R_IMF_PLANES_RGBA : R_IMF_PLANES_RGB;
181     }
182   }
183
184   /* we need renderresult for exr and rendered multiview */
185   rr = BKE_image_acquire_renderresult(opts->scene, ima);
186   bool is_mono = rr ? BLI_listbase_count_at_most(&rr->views, 2) < 2 :
187                       BLI_listbase_count_at_most(&ima->views, 2) < 2;
188   bool is_exr_rr = rr && ELEM(imf->imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER) &&
189                    RE_HasFloatPixels(rr);
190   bool is_multilayer = is_exr_rr && (imf->imtype == R_IMF_IMTYPE_MULTILAYER);
191   int layer = (iuser && !is_multilayer) ? iuser->layer : -1;
192
193   /* error handling */
194   if (!rr) {
195     if (imf->imtype == R_IMF_IMTYPE_MULTILAYER) {
196       BKE_report(reports, RPT_ERROR, "Did not write, no Multilayer Image");
197       BKE_image_release_ibuf(ima, ibuf, &lock);
198       goto cleanup;
199     }
200   }
201   else {
202     if (imf->views_format == R_IMF_VIEWS_STEREO_3D) {
203       if (!BKE_image_is_stereo(ima)) {
204         BKE_reportf(reports,
205                     RPT_ERROR,
206                     "Did not write, the image doesn't have a \"%s\" and \"%s\" views",
207                     STEREO_LEFT_NAME,
208                     STEREO_RIGHT_NAME);
209         BKE_image_release_ibuf(ima, ibuf, &lock);
210         goto cleanup;
211       }
212
213       /* it shouldn't ever happen*/
214       if ((BLI_findstring(&rr->views, STEREO_LEFT_NAME, offsetof(RenderView, name)) == NULL) ||
215           (BLI_findstring(&rr->views, STEREO_RIGHT_NAME, offsetof(RenderView, name)) == NULL)) {
216         BKE_reportf(reports,
217                     RPT_ERROR,
218                     "Did not write, the image doesn't have a \"%s\" and \"%s\" views",
219                     STEREO_LEFT_NAME,
220                     STEREO_RIGHT_NAME);
221         BKE_image_release_ibuf(ima, ibuf, &lock);
222         goto cleanup;
223       }
224     }
225     BKE_imbuf_stamp_info(rr, ibuf);
226   }
227
228   /* fancy multiview OpenEXR */
229   if (imf->views_format == R_IMF_VIEWS_MULTIVIEW && is_exr_rr) {
230     /* save render result */
231     ok = RE_WriteRenderResult(reports, rr, opts->filepath, imf, NULL, layer);
232     image_save_post(reports, bmain, ima, ibuf, ok, opts, true, opts->filepath);
233     BKE_image_release_ibuf(ima, ibuf, &lock);
234   }
235   /* regular mono pipeline */
236   else if (is_mono) {
237     if (is_exr_rr) {
238       ok = RE_WriteRenderResult(reports, rr, opts->filepath, imf, NULL, layer);
239     }
240     else {
241       colormanaged_ibuf = IMB_colormanagement_imbuf_for_write(
242           ibuf, save_as_render, true, &imf->view_settings, &imf->display_settings, imf);
243       ok = BKE_imbuf_write_as(colormanaged_ibuf, opts->filepath, imf, save_copy);
244       imbuf_save_post(ibuf, colormanaged_ibuf);
245     }
246     image_save_post(
247         reports, bmain, ima, ibuf, ok, opts, (is_exr_rr ? true : save_copy), opts->filepath);
248     BKE_image_release_ibuf(ima, ibuf, &lock);
249   }
250   /* individual multiview images */
251   else if (imf->views_format == R_IMF_VIEWS_INDIVIDUAL) {
252     int i;
253     unsigned char planes = ibuf->planes;
254     const int totviews = (rr ? BLI_listbase_count(&rr->views) : BLI_listbase_count(&ima->views));
255
256     if (!is_exr_rr) {
257       BKE_image_release_ibuf(ima, ibuf, &lock);
258     }
259
260     for (i = 0; i < totviews; i++) {
261       char filepath[FILE_MAX];
262       bool ok_view = false;
263       const char *view = rr ? ((RenderView *)BLI_findlink(&rr->views, i))->name :
264                               ((ImageView *)BLI_findlink(&ima->views, i))->name;
265
266       if (is_exr_rr) {
267         BKE_scene_multiview_view_filepath_get(&opts->scene->r, opts->filepath, view, filepath);
268         ok_view = RE_WriteRenderResult(reports, rr, filepath, imf, view, layer);
269         image_save_post(reports, bmain, ima, ibuf, ok_view, opts, true, filepath);
270       }
271       else {
272         /* copy iuser to get the correct ibuf for this view */
273         ImageUser view_iuser;
274
275         if (iuser) {
276           /* copy iuser to get the correct ibuf for this view */
277           view_iuser = *iuser;
278         }
279         else {
280           BKE_imageuser_default(&view_iuser);
281         }
282
283         view_iuser.view = i;
284         view_iuser.flag &= ~IMA_SHOW_STEREO;
285
286         if (rr) {
287           BKE_image_multilayer_index(rr, &view_iuser);
288         }
289         else {
290           BKE_image_multiview_index(ima, &view_iuser);
291         }
292
293         ibuf = BKE_image_acquire_ibuf(ima, &view_iuser, &lock);
294         ibuf->planes = planes;
295
296         BKE_scene_multiview_view_filepath_get(&opts->scene->r, opts->filepath, view, filepath);
297
298         colormanaged_ibuf = IMB_colormanagement_imbuf_for_write(
299             ibuf, save_as_render, true, &imf->view_settings, &imf->display_settings, imf);
300         ok_view = BKE_imbuf_write_as(colormanaged_ibuf, filepath, &opts->im_format, save_copy);
301         imbuf_save_post(ibuf, colormanaged_ibuf);
302         image_save_post(reports, bmain, ima, ibuf, ok_view, opts, true, filepath);
303         BKE_image_release_ibuf(ima, ibuf, lock);
304       }
305       ok &= ok_view;
306     }
307
308     if (is_exr_rr) {
309       BKE_image_release_ibuf(ima, ibuf, &lock);
310     }
311   }
312   /* stereo (multiview) images */
313   else if (opts->im_format.views_format == R_IMF_VIEWS_STEREO_3D) {
314     if (imf->imtype == R_IMF_IMTYPE_MULTILAYER) {
315       ok = RE_WriteRenderResult(reports, rr, opts->filepath, imf, NULL, layer);
316       image_save_post(reports, bmain, ima, ibuf, ok, opts, true, opts->filepath);
317       BKE_image_release_ibuf(ima, ibuf, &lock);
318     }
319     else {
320       ImBuf *ibuf_stereo[2] = {NULL};
321
322       unsigned char planes = ibuf->planes;
323       const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
324       int i;
325
326       /* we need to get the specific per-view buffers */
327       BKE_image_release_ibuf(ima, ibuf, &lock);
328
329       for (i = 0; i < 2; i++) {
330         ImageUser view_iuser;
331
332         if (iuser) {
333           view_iuser = *iuser;
334         }
335         else {
336           BKE_imageuser_default(&view_iuser);
337         }
338
339         view_iuser.flag &= ~IMA_SHOW_STEREO;
340
341         if (rr) {
342           int id = BLI_findstringindex(&rr->views, names[i], offsetof(RenderView, name));
343           view_iuser.view = id;
344           BKE_image_multilayer_index(rr, &view_iuser);
345         }
346         else {
347           view_iuser.view = i;
348           BKE_image_multiview_index(ima, &view_iuser);
349         }
350
351         ibuf = BKE_image_acquire_ibuf(ima, &view_iuser, &lock);
352
353         if (ibuf == NULL) {
354           BKE_report(
355               reports, RPT_ERROR, "Did not write, unexpected error when saving stereo image");
356           goto cleanup;
357         }
358
359         ibuf->planes = planes;
360
361         /* color manage the ImBuf leaving it ready for saving */
362         colormanaged_ibuf = IMB_colormanagement_imbuf_for_write(
363             ibuf, save_as_render, true, &imf->view_settings, &imf->display_settings, imf);
364
365         BKE_imbuf_write_prepare(colormanaged_ibuf, imf);
366         IMB_prepare_write_ImBuf(IMB_isfloat(colormanaged_ibuf), colormanaged_ibuf);
367
368         /* duplicate buffer to prevent locker issue when using render result */
369         ibuf_stereo[i] = IMB_dupImBuf(colormanaged_ibuf);
370
371         imbuf_save_post(ibuf, colormanaged_ibuf);
372         BKE_image_release_ibuf(ima, ibuf, lock);
373       }
374
375       ibuf = IMB_stereo3d_ImBuf(imf, ibuf_stereo[0], ibuf_stereo[1]);
376
377       /* save via traditional path */
378       ok = BKE_imbuf_write_as(ibuf, opts->filepath, imf, save_copy);
379
380       IMB_freeImBuf(ibuf);
381
382       for (i = 0; i < 2; i++) {
383         IMB_freeImBuf(ibuf_stereo[i]);
384       }
385     }
386   }
387
388 cleanup:
389   if (rr) {
390     BKE_image_release_renderresult(opts->scene, ima);
391   }
392
393   return ok;
394 }