Blender V5.3
image_format.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "DNA_scene_types.h"
12
13#include "BLI_path_utils.hh"
14#include "BLI_string_utf8.hh"
15#include "BLI_utildefines.hh"
16
18#include "IMB_imbuf_types.hh"
19
20#include "MOV_util.hh"
21
22#include "BKE_colortools.hh"
23#include "BKE_image_format.hh"
24#include "BKE_path_templates.hh"
25
26#include "BLT_translation.hh"
27
28namespace blender {
29
31
32/* Init/Copy/Free */
33
44
53
58
60{
61 /* If the color space is set to a data space, this is probably the user's intention, so leave it
62 * as is. */
63 if (IMB_colormanagement_space_name_is_data(format->linear_colorspace_settings.name)) {
64 return;
65 }
66
67 const bool image_requires_linear = BKE_imtype_requires_linear_float(format->imtype);
68 /* TODO: This is wrong, there are more linear colorspaces than scene linear. */
70 format->linear_colorspace_settings.name);
71
72 /* The color space is either not set or is linear but the image requires non-linear or vice
73 * versa. So set to the default for the image type. */
74 if (format->linear_colorspace_settings.name[0] == '\0' || image_requires_linear != is_linear) {
75 const int role = image_requires_linear ? COLOR_ROLE_SCENE_LINEAR : COLOR_ROLE_DEFAULT_BYTE;
76 const char *default_color_space = IMB_colormanagement_role_colorspace_name_get(role);
77 IMB_colormanagement_colorspace_settings_set(&format->linear_colorspace_settings,
78 default_color_space);
79 }
80}
81
86
91
93 ID *owner_id,
94 const MediaType media_type)
95{
96 format->media_type = media_type;
97
98 switch (media_type) {
100 if (!BKE_imtype_is_image(format->imtype)) {
101 BKE_image_format_set(format, owner_id, R_IMF_IMTYPE_PNG);
102 }
103 break;
105 if (!BKE_imtype_is_multi_layer_image(format->imtype)) {
107 }
108 break;
109 case MEDIA_TYPE_VIDEO:
110 if (!BKE_imtype_is_movie(format->imtype)) {
111 BKE_image_format_set(format, owner_id, R_IMF_IMTYPE_FFMPEG);
112 }
113 break;
114 }
115}
116
117void BKE_image_format_set(ImageFormatData *imf, ID *owner_id, const char imtype)
118{
119 imf->imtype = imtype;
120
121 /* Update media type in case it doesn't match the new imtype. Note that normally, one would use
122 * the BKE_image_format_media_type_set function to set the media type, but that function itself
123 * calls this function to update the imtype, and while this wouldn't case recursion since the
124 * imtype is already conforming, it is better to err on the side of caution and set the media
125 * type manually. */
126 if (BKE_imtype_is_image(imf->imtype)) {
128 }
131 }
132 else if (BKE_imtype_is_movie(imf->imtype)) {
134 }
135 else {
137 }
138
139 const bool is_render = (owner_id && GS(owner_id->name) == ID_SCE);
140 /* see note below on why this is */
141 const char chan_flag = BKE_imtype_valid_channels(imf->imtype) |
142 (is_render ? IMA_CHAN_FLAG_BW : 0);
143
144 /* ensure depth and color settings match */
145 if ((imf->color_mode == ImColorMode::BW) && !(chan_flag & IMA_CHAN_FLAG_BW)) {
147 }
148 if ((imf->color_mode == ImColorMode::RGBA) && !(chan_flag & IMA_CHAN_FLAG_RGBA)) {
150 }
151
152 /* ensure usable depth */
153 {
154 const eImageFormatDepth depth_ok = BKE_imtype_valid_depths(imf->imtype);
155 if ((imf->depth & depth_ok) == 0) {
156 imf->depth = BKE_imtype_first_valid_depth(depth_ok);
157 }
158 }
159
160 if (owner_id && GS(owner_id->name) == ID_SCE) {
161 Scene *scene = reinterpret_cast<Scene *>(owner_id);
162 RenderData *rd = &scene->r;
164 }
165
166 /* Verify `imf->views_format`. */
167 if (imf->imtype == R_IMF_IMTYPE_MULTILAYER) {
170 }
171 }
172 else if (imf->imtype != R_IMF_IMTYPE_OPENEXR) {
175 }
176 }
177
179}
180
181/* File Types */
182
184{
185 *r_options = ImbFormatOptions();
186
187 if (imtype == R_IMF_IMTYPE_TARGA) {
188 return IMB_FTYPE_TGA;
189 }
190 if (imtype == R_IMF_IMTYPE_RAWTGA) {
191 r_options->flag = RAWTGA;
192 return IMB_FTYPE_TGA;
193 }
194 if (imtype == R_IMF_IMTYPE_IRIS) {
195 return IMB_FTYPE_IRIS;
196 }
197 if (imtype == R_IMF_IMTYPE_RADHDR) {
198 return IMB_FTYPE_RADHDR;
199 }
200 if (imtype == R_IMF_IMTYPE_PNG) {
201 return IMB_FTYPE_PNG;
202 }
203 if (imtype == R_IMF_IMTYPE_DDS) {
204 return IMB_FTYPE_DDS;
205 }
206 if (imtype == R_IMF_IMTYPE_BMP) {
207 return IMB_FTYPE_BMP;
208 }
209 if (imtype == R_IMF_IMTYPE_TIFF) {
210 return IMB_FTYPE_TIF;
211 }
213 return IMB_FTYPE_OPENEXR;
214 }
215#ifdef WITH_IMAGE_CINEON
216 if (imtype == R_IMF_IMTYPE_CINEON) {
217 return IMB_FTYPE_CINEON;
218 }
219 if (imtype == R_IMF_IMTYPE_DPX) {
220 return IMB_FTYPE_DPX;
221 }
222#endif
223#ifdef WITH_IMAGE_OPENJPEG
224 if (imtype == R_IMF_IMTYPE_JP2) {
225 r_options->flag |= JP2_JP2;
226 return IMB_FTYPE_JP2;
227 }
228#endif
229#ifdef WITH_IMAGE_WEBP
230 if (imtype == R_IMF_IMTYPE_WEBP) {
231 return IMB_FTYPE_WEBP;
232 }
233#endif
234 if (imtype == R_IMF_IMTYPE_AVIF) {
235 r_options->quality = 90;
236 return IMB_FTYPE_AVIF;
237 }
238
239 return IMB_FTYPE_JPG;
240}
241
243{
244 if (ftype == IMB_FTYPE_NONE) {
245 return R_IMF_IMTYPE_TARGA;
246 }
247 if (ftype == IMB_FTYPE_IRIS) {
248 return R_IMF_IMTYPE_IRIS;
249 }
250 if (ftype == IMB_FTYPE_RADHDR) {
251 return R_IMF_IMTYPE_RADHDR;
252 }
253 if (ftype == IMB_FTYPE_PNG) {
254 return R_IMF_IMTYPE_PNG;
255 }
256 if (ftype == IMB_FTYPE_DDS) {
257 return R_IMF_IMTYPE_DDS;
258 }
259 if (ftype == IMB_FTYPE_BMP) {
260 return R_IMF_IMTYPE_BMP;
261 }
262 if (ftype == IMB_FTYPE_TIF) {
263 return R_IMF_IMTYPE_TIFF;
264 }
265 if (ftype == IMB_FTYPE_OPENEXR) {
267 }
268#ifdef WITH_IMAGE_CINEON
269 if (ftype == IMB_FTYPE_CINEON) {
270 return R_IMF_IMTYPE_CINEON;
271 }
272 if (ftype == IMB_FTYPE_DPX) {
273 return R_IMF_IMTYPE_DPX;
274 }
275#endif
276 if (ftype == IMB_FTYPE_TGA) {
277 if (options && (options->flag & RAWTGA)) {
278 return R_IMF_IMTYPE_RAWTGA;
279 }
280
281 return R_IMF_IMTYPE_TARGA;
282 }
283#ifdef WITH_IMAGE_OPENJPEG
284 if (ftype == IMB_FTYPE_JP2) {
285 return R_IMF_IMTYPE_JP2;
286 }
287#endif
288#ifdef WITH_IMAGE_WEBP
289 if (ftype == IMB_FTYPE_WEBP) {
290 return R_IMF_IMTYPE_WEBP;
291 }
292#endif
293 if (ftype == IMB_FTYPE_AVIF) {
294 return R_IMF_IMTYPE_AVIF;
295 }
296
297 return R_IMF_IMTYPE_JPEG90;
298}
299
300bool BKE_imtype_is_image(const char imtype)
301{
302 return !BKE_imtype_is_multi_layer_image(imtype) && !BKE_imtype_is_movie(imtype);
303}
304
305bool BKE_imtype_is_multi_layer_image(const char imtype)
306{
307 return imtype == R_IMF_IMTYPE_MULTILAYER;
308}
309
310bool BKE_imtype_is_movie(const char imtype)
311{
312 return imtype == R_IMF_IMTYPE_FFMPEG;
313}
314
315bool BKE_imtype_supports_compress(const char imtype)
316{
317 switch (imtype) {
318 case R_IMF_IMTYPE_PNG:
319 return true;
320 }
321 return false;
322}
323
324bool BKE_imtype_supports_quality(const char imtype)
325{
326 switch (imtype) {
328 case R_IMF_IMTYPE_JP2:
331 return true;
332 }
333 return false;
334}
335
336bool BKE_imtype_requires_linear_float(const char imtype)
337{
338 switch (imtype) {
340 case R_IMF_IMTYPE_DPX:
344 return true;
345 }
346 return false;
347}
348
349char BKE_imtype_valid_channels(const char imtype)
350{
351 char chan_flag = IMA_CHAN_FLAG_RGB; /* Assume all support RGB. */
352
353 /* Alpha. */
354 switch (imtype) {
355 case R_IMF_IMTYPE_BMP:
359 case R_IMF_IMTYPE_PNG:
363 case R_IMF_IMTYPE_DDS:
364 case R_IMF_IMTYPE_JP2:
365 case R_IMF_IMTYPE_DPX:
368 chan_flag |= IMA_CHAN_FLAG_RGBA;
369 break;
370 }
371
372 /* BW. */
373 switch (imtype) {
374 case R_IMF_IMTYPE_BMP:
375 case R_IMF_IMTYPE_PNG:
382 case R_IMF_IMTYPE_JP2:
384 chan_flag |= IMA_CHAN_FLAG_BW;
385 break;
386 }
387
388 return chan_flag;
389}
390
392{
393 switch (imtype) {
395 return R_IMF_CHAN_DEPTH_32;
402 /* NOTE: CINEON uses an unusual 10bits-LOG per channel. */
403 case R_IMF_IMTYPE_DPX:
406 return R_IMF_CHAN_DEPTH_10;
407 case R_IMF_IMTYPE_JP2:
409 case R_IMF_IMTYPE_PNG:
413 /* Most formats are 8bit only. */
414 default:
415 return R_IMF_CHAN_DEPTH_8;
416 }
417}
418
420{
421 UNUSED_VARS(owner_id); /* Might be unused depending on build options. */
422
424 /* Depending on video codec selected, valid color bit depths might vary. */
425 if (imtype == R_IMF_IMTYPE_FFMPEG) {
426 const bool is_render_out = (owner_id && GS(owner_id->name) == ID_SCE);
427 if (is_render_out) {
428 const Scene *scene = id_cast<const Scene *>(owner_id);
429 depths |= MOV_codec_valid_bit_depths(scene->r.ffcodecdata.codec_id_get());
430 }
431 }
432 return depths;
433}
434
436{
437 /* set first available depth */
438 const eImageFormatDepth depth_ls[] = {
445 };
446 for (int i = 0; depth_ls[i] != eImageFormatDepth(0); i++) {
447 if (valid_depths & depth_ls[i]) {
448 return depth_ls[i];
449 }
450 }
451 return R_IMF_CHAN_DEPTH_8;
452}
453
454char BKE_imtype_from_arg(const char *imtype_arg)
455{
456 if (STREQ(imtype_arg, "TGA")) {
457 return R_IMF_IMTYPE_TARGA;
458 }
459 if (STREQ(imtype_arg, "IRIS")) {
460 return R_IMF_IMTYPE_IRIS;
461 }
462 if (STREQ(imtype_arg, "JPEG")) {
463 return R_IMF_IMTYPE_JPEG90;
464 }
465 if (STREQ(imtype_arg, "RAWTGA")) {
466 return R_IMF_IMTYPE_RAWTGA;
467 }
468 if (STREQ(imtype_arg, "PNG")) {
469 return R_IMF_IMTYPE_PNG;
470 }
471 if (STREQ(imtype_arg, "BMP")) {
472 return R_IMF_IMTYPE_BMP;
473 }
474 if (STREQ(imtype_arg, "HDR")) {
475 return R_IMF_IMTYPE_RADHDR;
476 }
477 if (STREQ(imtype_arg, "TIFF")) {
478 return R_IMF_IMTYPE_TIFF;
479 }
480 if (STREQ(imtype_arg, "OPEN_EXR")) {
482 }
483 if (STREQ(imtype_arg, "OPEN_EXR_MULTILAYER")) {
485 }
486 if (STREQ(imtype_arg, "EXR")) {
488 }
489 if (STREQ(imtype_arg, "MULTILAYER")) {
491 }
492#ifdef WITH_FFMPEG
493 if (STREQ(imtype_arg, "FFMPEG")) {
494 return R_IMF_IMTYPE_FFMPEG;
495 }
496#endif
497#ifdef WITH_IMAGE_CINEON
498 if (STREQ(imtype_arg, "CINEON")) {
499 return R_IMF_IMTYPE_CINEON;
500 }
501 if (STREQ(imtype_arg, "DPX")) {
502 return R_IMF_IMTYPE_DPX;
503 }
504#endif
505#ifdef WITH_IMAGE_OPENJPEG
506 if (STREQ(imtype_arg, "JP2")) {
507 return R_IMF_IMTYPE_JP2;
508 }
509#endif
510#ifdef WITH_IMAGE_WEBP
511 if (STREQ(imtype_arg, "WEBP")) {
512 return R_IMF_IMTYPE_WEBP;
513 }
514#endif
515 if (STREQ(imtype_arg, "AVIF")) {
516 return R_IMF_IMTYPE_AVIF;
517 }
518
520}
521
522/* File Paths */
523
524static int image_path_ext_from_imformat_impl(const char imtype,
525 const ImageFormatData *im_format,
526 const char *r_ext[BKE_IMAGE_PATH_EXT_MAX])
527{
528 int ext_num = 0;
529 (void)im_format; /* may be unused, depends on build options */
530
531 if (imtype == R_IMF_IMTYPE_IRIS) {
532 r_ext[ext_num++] = ".rgb";
533 }
534 else if (imtype == R_IMF_IMTYPE_IRIZ) {
535 r_ext[ext_num++] = ".rgb";
536 }
537 else if (imtype == R_IMF_IMTYPE_RADHDR) {
538 r_ext[ext_num++] = ".hdr";
539 }
540 else if (ELEM(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG)) {
541 r_ext[ext_num++] = ".png";
542 }
543 else if (imtype == R_IMF_IMTYPE_DDS) {
544 r_ext[ext_num++] = ".dds";
545 }
546 else if (ELEM(imtype, R_IMF_IMTYPE_TARGA, R_IMF_IMTYPE_RAWTGA)) {
547 r_ext[ext_num++] = ".tga";
548 }
549 else if (imtype == R_IMF_IMTYPE_BMP) {
550 r_ext[ext_num++] = ".bmp";
551 }
552 else if (imtype == R_IMF_IMTYPE_TIFF) {
553 r_ext[ext_num++] = ".tif";
554 r_ext[ext_num++] = ".tiff";
555 }
556 else if (imtype == R_IMF_IMTYPE_PSD) {
557 r_ext[ext_num++] = ".psd";
558 }
560 r_ext[ext_num++] = ".exr";
561 }
562#ifdef WITH_IMAGE_CINEON
563 else if (imtype == R_IMF_IMTYPE_CINEON) {
564 r_ext[ext_num++] = ".cin";
565 }
566 else if (imtype == R_IMF_IMTYPE_DPX) {
567 r_ext[ext_num++] = ".dpx";
568 }
569#endif
570#ifdef WITH_IMAGE_OPENJPEG
571 else if (imtype == R_IMF_IMTYPE_JP2) {
572 if (im_format) {
573 if (im_format->jp2_codec == R_IMF_JP2_CODEC_JP2) {
574 r_ext[ext_num++] = ".jp2";
575 }
576 else if (im_format->jp2_codec == R_IMF_JP2_CODEC_J2K) {
577 r_ext[ext_num++] = ".j2c";
578 }
579 else {
580 BLI_assert_msg(0, "Unsupported jp2 codec was specified in im_format->jp2_codec");
581 }
582 }
583 else {
584 r_ext[ext_num++] = ".jp2";
585 }
586 }
587#endif
588#ifdef WITH_IMAGE_WEBP
589 else if (imtype == R_IMF_IMTYPE_WEBP) {
590 r_ext[ext_num++] = ".webp";
591 }
592#endif
593 else if (imtype == R_IMF_IMTYPE_AVIF) {
594 r_ext[ext_num++] = ".avif";
595 }
596 else {
597 /* Handles: #R_IMF_IMTYPE_JPEG90 etc. */
598 r_ext[ext_num++] = ".jpg";
599 r_ext[ext_num++] = ".jpeg";
600 }
602 r_ext[ext_num] = nullptr;
603 return ext_num;
604}
605
607 const char *r_ext[BKE_IMAGE_PATH_EXT_MAX])
608{
609 return image_path_ext_from_imformat_impl(im_format->imtype, im_format, r_ext);
610}
611
612int BKE_image_path_ext_from_imtype(const char imtype, const char *r_ext[BKE_IMAGE_PATH_EXT_MAX])
613{
614 return image_path_ext_from_imformat_impl(imtype, nullptr, r_ext);
615}
616
617static bool do_ensure_image_extension(char *filepath,
618 const size_t filepath_maxncpy,
619 const char imtype,
620 const ImageFormatData *im_format)
621{
622 const char *ext_array[BKE_IMAGE_PATH_EXT_MAX];
623 int ext_array_num = image_path_ext_from_imformat_impl(imtype, im_format, ext_array);
624 if (ext_array_num && !BLI_path_extension_check_array(filepath, ext_array)) {
625 /* Removing *any* extension may remove part of the user defined name (if they include '.')
626 * however in the case there is already a known image extension,
627 * remove it to avoid`.png.tga`, for example. */
629 return BLI_path_extension_replace(filepath, filepath_maxncpy, ext_array[0]);
630 }
631 return BLI_path_extension_ensure(filepath, filepath_maxncpy, ext_array[0]);
632 }
633
634 return false;
635}
636
638 const size_t filepath_maxncpy,
639 const ImageFormatData *im_format)
640{
641 return do_ensure_image_extension(filepath, filepath_maxncpy, im_format->imtype, im_format);
642}
643
645 const size_t filepath_maxncpy,
646 const char imtype)
647{
648 return do_ensure_image_extension(filepath, filepath_maxncpy, imtype, nullptr);
649}
650
652 char filepath[FILE_MAX],
653 const char *base,
654 const char *relbase,
655 const path_templates::VariableMap *template_variables,
656 int frame,
657 const char imtype,
658 const ImageFormatData *im_format,
659 const bool use_ext,
660 const bool use_frames,
661 const char *suffix)
662{
663 if (filepath == nullptr) {
664 return {};
665 }
666 BLI_strncpy(filepath, base, FILE_MAX - 10); /* weak assumption */
667
668 if (template_variables) {
670 filepath, FILE_MAX, *template_variables);
671 if (!variable_errors.is_empty()) {
672 return variable_errors;
673 }
674 }
675
676 BLI_path_abs(filepath, relbase);
677
678 if (use_frames) {
679 BLI_path_frame(filepath, FILE_MAX, frame, 4);
680 }
681 else {
682 /* Avoid empty filename if there is no frame and only a directory was specified. */
683 const size_t len = strlen(filepath);
684 if (len && BLI_path_slash_is_native_compat(filepath[len - 1])) {
685 BLI_path_suffix(filepath, FILE_MAX, DATA_("Untitled"), "");
686 }
687 }
688
689 if (suffix) {
690 BLI_path_suffix(filepath, FILE_MAX, suffix, "");
691 }
692
693 if (use_ext) {
694 do_ensure_image_extension(filepath, FILE_MAX, imtype, im_format);
695 }
696
697 return {};
698}
699
701 char *filepath,
702 const char *base,
703 const char *relbase,
704 const path_templates::VariableMap *template_variables,
705 int frame,
706 const ImageFormatData *im_format,
707 const bool use_ext,
708 const bool use_frames,
709 const char *suffix)
710{
711 return do_makepicstring(filepath,
712 base,
713 relbase,
714 template_variables,
715 frame,
716 im_format->imtype,
717 im_format,
718 use_ext,
719 use_frames,
720 suffix);
721}
722
724 char *filepath,
725 const char *base,
726 const char *relbase,
727 const path_templates::VariableMap *template_variables,
728 int frame,
729 const char imtype,
730 const bool use_ext,
731 const bool use_frames,
732 const char *suffix)
733{
734 return do_makepicstring(filepath,
735 base,
736 relbase,
737 template_variables,
738 frame,
739 imtype,
740 nullptr,
741 use_ext,
742 use_frames,
743 suffix);
744}
745
746/* ImBuf Conversion */
747
749{
750 /* Write to ImBuf in preparation for file writing. */
751 char imtype = imf->imtype;
752 char compress = imf->compress;
753 char quality = imf->quality;
754
755 /* initialize all from image format */
756 ibuf->foptions.flag = 0;
757
758 if (imtype == R_IMF_IMTYPE_IRIS) {
759 ibuf->ftype = IMB_FTYPE_IRIS;
760 }
761 else if (imtype == R_IMF_IMTYPE_RADHDR) {
762 ibuf->ftype = IMB_FTYPE_RADHDR;
763 }
764 else if (ELEM(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG)) {
765 ibuf->ftype = IMB_FTYPE_PNG;
766
767 if (imtype == R_IMF_IMTYPE_PNG) {
768 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
769 ibuf->foptions.flag |= PNG_16BIT;
770 }
771
772 ibuf->foptions.compress = compress;
773 }
774 }
775 else if (imtype == R_IMF_IMTYPE_DDS) {
776 ibuf->ftype = IMB_FTYPE_DDS;
777 }
778 else if (imtype == R_IMF_IMTYPE_BMP) {
779 ibuf->ftype = IMB_FTYPE_BMP;
780 }
781 else if (imtype == R_IMF_IMTYPE_TIFF) {
782 ibuf->ftype = IMB_FTYPE_TIF;
783
784 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
785 ibuf->foptions.flag |= TIF_16BIT;
786 }
787 if (imf->tiff_codec == R_IMF_TIFF_CODEC_NONE) {
789 }
790 else if (imf->tiff_codec == R_IMF_TIFF_CODEC_DEFLATE) {
792 }
793 else if (imf->tiff_codec == R_IMF_TIFF_CODEC_LZW) {
795 }
796 else if (imf->tiff_codec == R_IMF_TIFF_CODEC_PACKBITS) {
798 }
799 }
801 ibuf->ftype = IMB_FTYPE_OPENEXR;
802 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
803 ibuf->foptions.flag |= OPENEXR_HALF;
804 }
805 ibuf->foptions.flag |= (imf->exr_codec & OPENEXR_CODEC_MASK);
806 ibuf->foptions.quality = quality;
809 }
810 }
811#ifdef WITH_IMAGE_CINEON
812 else if (imtype == R_IMF_IMTYPE_CINEON) {
813 ibuf->ftype = IMB_FTYPE_CINEON;
815 ibuf->foptions.flag |= CINEON_LOG;
816 }
817 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
818 ibuf->foptions.flag |= CINEON_16BIT;
819 }
820 else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
821 ibuf->foptions.flag |= CINEON_12BIT;
822 }
823 else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
824 ibuf->foptions.flag |= CINEON_10BIT;
825 }
826 }
827 else if (imtype == R_IMF_IMTYPE_DPX) {
828 ibuf->ftype = IMB_FTYPE_DPX;
830 ibuf->foptions.flag |= CINEON_LOG;
831 }
832 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
833 ibuf->foptions.flag |= CINEON_16BIT;
834 }
835 else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
836 ibuf->foptions.flag |= CINEON_12BIT;
837 }
838 else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
839 ibuf->foptions.flag |= CINEON_10BIT;
840 }
841 }
842#endif
843 else if (imtype == R_IMF_IMTYPE_TARGA) {
844 ibuf->ftype = IMB_FTYPE_TGA;
845 }
846 else if (imtype == R_IMF_IMTYPE_RAWTGA) {
847 ibuf->ftype = IMB_FTYPE_TGA;
848 ibuf->foptions.flag = RAWTGA;
849 }
850#ifdef WITH_IMAGE_OPENJPEG
851 else if (imtype == R_IMF_IMTYPE_JP2) {
852 if (quality < 10) {
853 quality = 90;
854 }
855 ibuf->ftype = IMB_FTYPE_JP2;
856 ibuf->foptions.quality = quality;
857
858 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
859 ibuf->foptions.flag |= JP2_16BIT;
860 }
861 else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
862 ibuf->foptions.flag |= JP2_12BIT;
863 }
864
865 if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
866 ibuf->foptions.flag |= JP2_YCC;
867 }
868
870 ibuf->foptions.flag |= JP2_CINE;
871 if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48) {
872 ibuf->foptions.flag |= JP2_CINE_48FPS;
873 }
874 }
875
876 if (imf->jp2_codec == R_IMF_JP2_CODEC_JP2) {
877 ibuf->foptions.flag |= JP2_JP2;
878 }
879 else if (imf->jp2_codec == R_IMF_JP2_CODEC_J2K) {
880 ibuf->foptions.flag |= JP2_J2K;
881 }
882 else {
883 BLI_assert_msg(0, "Unsupported jp2 codec was specified in im_format->jp2_codec");
884 }
885 }
886#endif
887#ifdef WITH_IMAGE_WEBP
888 else if (imtype == R_IMF_IMTYPE_WEBP) {
889 ibuf->ftype = IMB_FTYPE_WEBP;
890 ibuf->foptions.quality = quality;
891 }
892#endif
893 else if (imtype == R_IMF_IMTYPE_AVIF) {
894 ibuf->ftype = IMB_FTYPE_AVIF;
895 ibuf->foptions.quality = quality;
896
897 if (imf->depth == R_IMF_CHAN_DEPTH_10) {
898 ibuf->foptions.flag |= AVIF_10BIT;
899 }
900 else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
901 ibuf->foptions.flag |= AVIF_12BIT;
902 }
903 }
904 else {
905 /* #R_IMF_IMTYPE_JPEG90, etc. default to JPEG. */
906 if (quality < 10) {
907 quality = 90;
908 }
909 ibuf->ftype = IMB_FTYPE_JPG;
910 ibuf->foptions.quality = quality;
911 }
912}
913
914static eImageFormatDepth imtype_best_depth(const ImBuf *ibuf, const char imtype)
915{
916 const eImageFormatDepth depth_ok = BKE_imtype_valid_depths(imtype);
917
918 if (ibuf->float_data()) {
919 if (depth_ok & R_IMF_CHAN_DEPTH_32) {
920 return R_IMF_CHAN_DEPTH_32;
921 }
922 if (depth_ok & R_IMF_CHAN_DEPTH_16) {
923 return R_IMF_CHAN_DEPTH_16;
924 }
925 if (depth_ok & R_IMF_CHAN_DEPTH_12) {
926 return R_IMF_CHAN_DEPTH_12;
927 }
928 return R_IMF_CHAN_DEPTH_8;
929 }
930
931 if (depth_ok & R_IMF_CHAN_DEPTH_8) {
932 return R_IMF_CHAN_DEPTH_8;
933 }
934 if (depth_ok & R_IMF_CHAN_DEPTH_12) {
935 return R_IMF_CHAN_DEPTH_12;
936 }
937 if (depth_ok & R_IMF_CHAN_DEPTH_16) {
938 return R_IMF_CHAN_DEPTH_16;
939 }
940 if (depth_ok & R_IMF_CHAN_DEPTH_32) {
941 return R_IMF_CHAN_DEPTH_32;
942 }
943 return R_IMF_CHAN_DEPTH_8; /* fallback, should not get here */
944}
945
947{
948 /* Read from ImBuf after file read. */
949 eImbFileType ftype = imbuf->ftype;
950 int custom_flags = imbuf->foptions.flag;
951 char quality = imbuf->foptions.quality;
952 char compress = imbuf->foptions.compress;
953 bool is_depth_set = false;
954
955 BKE_image_format_init(im_format);
956 im_format->media_type = MEDIA_TYPE_IMAGE;
957
958 /* file type */
959 if (ftype == IMB_FTYPE_IRIS) {
960 im_format->imtype = R_IMF_IMTYPE_IRIS;
961 }
962 else if (ftype == IMB_FTYPE_RADHDR) {
963 im_format->imtype = R_IMF_IMTYPE_RADHDR;
964 }
965 else if (ftype == IMB_FTYPE_PNG) {
966 im_format->imtype = R_IMF_IMTYPE_PNG;
967
968 if (custom_flags & PNG_16BIT) {
969 im_format->depth = R_IMF_CHAN_DEPTH_16;
970 is_depth_set = true;
971 }
972
973 im_format->compress = compress;
974 }
975 else if (ftype == IMB_FTYPE_DDS) {
976 im_format->imtype = R_IMF_IMTYPE_DDS;
977 }
978 else if (ftype == IMB_FTYPE_BMP) {
979 im_format->imtype = R_IMF_IMTYPE_BMP;
980 }
981 else if (ftype == IMB_FTYPE_TIF) {
982 im_format->imtype = R_IMF_IMTYPE_TIFF;
983 if (custom_flags & TIF_16BIT) {
984 im_format->depth = R_IMF_CHAN_DEPTH_16;
985 is_depth_set = true;
986 }
987 if (custom_flags & TIF_COMPRESS_NONE) {
989 }
990 if (custom_flags & TIF_COMPRESS_DEFLATE) {
992 }
993 if (custom_flags & TIF_COMPRESS_LZW) {
994 im_format->tiff_codec = R_IMF_TIFF_CODEC_LZW;
995 }
996 if (custom_flags & TIF_COMPRESS_PACKBITS) {
998 }
999 }
1000
1001 else if (ftype == IMB_FTYPE_OPENEXR) {
1002 im_format->imtype = R_IMF_IMTYPE_OPENEXR;
1003 char exr_codec = custom_flags & OPENEXR_CODEC_MASK;
1004 if (custom_flags & OPENEXR_HALF) {
1005 im_format->depth = R_IMF_CHAN_DEPTH_16;
1006 is_depth_set = true;
1007 }
1008 else if (ELEM(exr_codec, R_IMF_EXR_CODEC_B44, R_IMF_EXR_CODEC_B44A)) {
1009 /* B44 and B44A are only selectable for half precision images, default to ZIP compression */
1010 exr_codec = R_IMF_EXR_CODEC_ZIP;
1011 }
1012 if (exr_codec < R_IMF_EXR_CODEC_MAX) {
1013 im_format->exr_codec = exr_codec;
1014 }
1015 if (custom_flags & OPENEXR_MULTIPART) {
1016 im_format->exr_flag |= R_IMF_EXR_FLAG_MULTIPART;
1017 }
1018 }
1019
1020#ifdef WITH_IMAGE_CINEON
1021 else if (ftype == IMB_FTYPE_CINEON) {
1022 im_format->imtype = R_IMF_IMTYPE_CINEON;
1023 }
1024 else if (ftype == IMB_FTYPE_DPX) {
1025 im_format->imtype = R_IMF_IMTYPE_DPX;
1026 }
1027#endif
1028 else if (ftype == IMB_FTYPE_TGA) {
1029 if (custom_flags & RAWTGA) {
1030 im_format->imtype = R_IMF_IMTYPE_RAWTGA;
1031 }
1032 else {
1033 im_format->imtype = R_IMF_IMTYPE_TARGA;
1034 }
1035 }
1036#ifdef WITH_IMAGE_OPENJPEG
1037 else if (ftype == IMB_FTYPE_JP2) {
1038 im_format->imtype = R_IMF_IMTYPE_JP2;
1039 im_format->quality = quality;
1040
1041 if (custom_flags & JP2_16BIT) {
1042 im_format->depth = R_IMF_CHAN_DEPTH_16;
1043 is_depth_set = true;
1044 }
1045 else if (custom_flags & JP2_12BIT) {
1046 im_format->depth = R_IMF_CHAN_DEPTH_12;
1047 is_depth_set = true;
1048 }
1049
1050 if (custom_flags & JP2_YCC) {
1051 im_format->jp2_flag |= R_IMF_JP2_FLAG_YCC;
1052 }
1053
1054 if (custom_flags & JP2_CINE) {
1056 if (custom_flags & JP2_CINE_48FPS) {
1057 im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_48;
1058 }
1059 }
1060
1061 if (custom_flags & JP2_JP2) {
1062 im_format->jp2_codec = R_IMF_JP2_CODEC_JP2;
1063 }
1064 else if (custom_flags & JP2_J2K) {
1065 im_format->jp2_codec = R_IMF_JP2_CODEC_J2K;
1066 }
1067 else {
1068 BLI_assert_msg(0, "Unsupported jp2 codec was specified in file type");
1069 }
1070 }
1071#endif
1072#ifdef WITH_IMAGE_WEBP
1073 else if (ftype == IMB_FTYPE_WEBP) {
1074 im_format->imtype = R_IMF_IMTYPE_WEBP;
1075 im_format->quality = quality;
1076 }
1077#endif
1078
1079 else if (ftype == IMB_FTYPE_AVIF) {
1080 im_format->imtype = R_IMF_IMTYPE_AVIF;
1081 im_format->quality = quality;
1082
1083 if (custom_flags & AVIF_10BIT) {
1084 im_format->depth = R_IMF_CHAN_DEPTH_10;
1085 is_depth_set = true;
1086 }
1087 else if (custom_flags & AVIF_12BIT) {
1088 im_format->depth = R_IMF_CHAN_DEPTH_12;
1089 is_depth_set = true;
1090 }
1091 }
1092 else {
1093 im_format->imtype = R_IMF_IMTYPE_JPEG90;
1094 im_format->quality = quality;
1095 }
1096
1097 /* Default depth, accounting for float buffer and format support */
1098 if (!is_depth_set) {
1099 im_format->depth = imtype_best_depth(imbuf, im_format->imtype);
1100 }
1101
1102 im_format->color_mode = imbuf->color_mode;
1103}
1104
1106{
1107 return (imf->depth == R_IMF_CHAN_DEPTH_8) && (BKE_imtype_valid_depths(imf->imtype) & imf->depth);
1108}
1109
1110/* Color Management */
1111
1121
1132
1133/* Output */
1134
1136 const Scene *scene_src,
1137 const ImageFormatData *imf_src,
1138 const bool allow_video)
1139{
1140 *imf = (imf_src) ? *imf_src : scene_src->r.im_format;
1141
1142 /* For image saving we can not have use media type video. */
1143 if (!allow_video) {
1144 if (scene_src && imf->media_type == MEDIA_TYPE_VIDEO) {
1145 BKE_image_format_media_type_set(imf, const_cast<ID *>(&scene_src->id), MEDIA_TYPE_IMAGE);
1146 }
1147 }
1148
1149 if (imf_src && imf_src->color_management == R_IMF_COLOR_MANAGEMENT_OVERRIDE) {
1150 /* Use settings specific to one node, image save operation, etc. */
1154 &imf_src->linear_colorspace_settings);
1155 }
1157 /* Use scene settings specific to render output. */
1159 &scene_src->r.im_format.display_settings);
1161 &scene_src->r.im_format.view_settings);
1164 }
1165 else {
1166 /* Use general scene settings also used for display. */
1172 }
1173}
1174
1175} // namespace blender
#define IMA_CHAN_FLAG_RGB
#define IMA_CHAN_FLAG_RGBA
#define IMA_CHAN_FLAG_BW
#define BKE_IMAGE_PATH_EXT_MAX
Functions and classes for evaluating template expressions in filepaths.
#define FILE_MAX
#define DATA_(msgid)
#define TIF_COMPRESS_PACKBITS
#define TIF_COMPRESS_NONE
#define AVIF_12BIT
#define TIF_COMPRESS_DEFLATE
#define RAWTGA
#define TIF_COMPRESS_LZW
#define PNG_16BIT
#define OPENEXR_CODEC_MASK
#define OPENEXR_MULTIPART
#define TIF_16BIT
#define AVIF_10BIT
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
bool is_empty() const
CCL_NAMESPACE_BEGIN struct Options options
#define OPENEXR_HALF
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
bool BKE_image_format_is_byte(const ImageFormatData *imf)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static Vector< path_templates::Error > do_makepicstring(char filepath[FILE_MAX], const char *base, const char *relbase, const path_templates::VariableMap *template_variables, int frame, const char imtype, const ImageFormatData *im_format, const bool use_ext, const bool use_frames, const char *suffix)
void BKE_image_format_set(ImageFormatData *imf, ID *owner_id, const char imtype)
void BKE_image_format_media_type_set(ImageFormatData *format, ID *owner_id, const MediaType media_type)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void BKE_image_format_color_management_copy_from_scene(ImageFormatData *imf, const Scene *scene)
@ MEDIA_TYPE_MULTI_LAYER_IMAGE
void BKE_image_format_to_imbuf(ImBuf *ibuf, const ImageFormatData *imf)
void BKE_image_format_blend_write(BlendWriter *writer, ImageFormatData *imf)
BLI_INLINE bool BLI_path_slash_is_native_compat(const char ch)
static eImageFormatDepth imtype_best_depth(const ImBuf *ibuf, const char imtype)
bool bool BLI_path_suffix(char *path, size_t path_maxncpy, const char *suffix, const char *sep) ATTR_NONNULL(1
void BKE_color_managed_view_settings_init(ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, const char *view_transform)
void BKE_color_managed_colorspace_settings_copy(ColorManagedColorspaceSettings *colorspace_settings, const ColorManagedColorspaceSettings *settings)
eImbFileType BKE_imtype_to_ftype(char imtype, ImbFormatOptions *r_options)
bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits) ATTR_NONNULL(1)
@ R_IMF_COLOR_MANAGEMENT_OVERRIDE
void BKE_color_managed_display_settings_init(ColorManagedDisplaySettings *settings)
static bool do_ensure_image_extension(char *filepath, const size_t filepath_maxncpy, const char imtype, const ImageFormatData *im_format)
Vector< bke::path_templates::Error > BKE_image_path_from_imformat(char *filepath, const char *base, const char *relbase, const bke::path_templates::VariableMap *template_variables, int frame, const ImageFormatData *im_format, bool use_ext, bool use_frames, const char *suffix)
void BKE_color_managed_view_settings_blend_read_data(BlendDataReader *reader, ColorManagedViewSettings *settings)
bool BKE_imtype_is_multi_layer_image(char imtype)
void BKE_color_managed_view_settings_copy(ColorManagedViewSettings *new_settings, const ColorManagedViewSettings *settings)
eImageFormatDepth MOV_codec_valid_bit_depths(IMB_Ffmpeg_Codec_ID codec_id)
void BKE_image_format_from_imbuf(ImageFormatData *im_format, const ImBuf *imbuf)
bool BKE_imtype_supports_compress(char imtype)
bool BKE_imtype_is_image(char imtype)
int BKE_image_path_ext_from_imtype(const char imtype, const char *r_ext[BKE_IMAGE_PATH_EXT_MAX])
Vector< bke::path_templates::Error > BKE_path_apply_template(char *path, int path_maxncpy, const bke::path_templates::VariableMap &template_variables)
@ R_IMF_IMTYPE_MULTILAYER
bool BLI_path_extension_ensure(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static int image_path_ext_from_imformat_impl(const char imtype, const ImageFormatData *im_format, const char *r_ext[BKE_IMAGE_PATH_EXT_MAX])
void BKE_color_managed_display_settings_copy(ColorManagedDisplaySettings *new_settings, const ColorManagedDisplaySettings *settings)
const char * IMB_colormanagement_role_colorspace_name_get(int role)
@ R_IMF_JP2_FLAG_CINE_PRESET
@ R_IMF_JP2_FLAG_CINE_48
void IMB_colormanagement_colorspace_settings_set(ColorManagedColorspaceSettings *settings, const char *name)
void BKE_image_format_blend_read_data(BlendDataReader *reader, ImageFormatData *imf)
bool BKE_imtype_supports_quality(char imtype)
int BKE_image_path_ext_from_imformat_ensure(char *filepath, size_t filepath_maxncpy, const ImageFormatData *im_format)
bool BLI_path_extension_replace(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
void BKE_image_format_init(ImageFormatData *imf)
void BKE_color_managed_colorspace_settings_init(ColorManagedColorspaceSettings *colorspace_settings)
void BKE_image_format_color_management_copy(ImageFormatData *imf, const ImageFormatData *imf_src)
eImageFormatDepth BKE_imtype_valid_depths(char imtype)
void BKE_image_format_free(ImageFormatData *imf)
void BKE_image_format_init_for_write(ImageFormatData *imf, const Scene *scene_src, const ImageFormatData *imf_src, const bool allow_video=false)
bool BKE_imtype_requires_linear_float(char imtype)
bool BKE_imtype_is_movie(char imtype)
char BKE_ftype_to_imtype(eImbFileType ftype, const ImbFormatOptions *options)
eImageFormatDepth BKE_imtype_first_valid_depth(eImageFormatDepth valid_depths)
void MOV_validate_output_settings(RenderData *rd, const ImageFormatData *imf)
@ R_IMF_CINEON_FLAG_LOG
bool BLI_path_extension_check_array(const char *path, const char **ext_array) ATTR_NONNULL(1
char BKE_imtype_from_arg(const char *imtype_arg)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
Vector< bke::path_templates::Error > BKE_image_path_from_imtype(char *filepath, const char *base, const char *relbase, const bke::path_templates::VariableMap *template_variables, int frame, char imtype, bool use_ext, bool use_frames, const char *suffix)
@ R_IMF_VIEWS_MULTIVIEW
@ R_IMF_VIEWS_INDIVIDUAL
@ R_IMF_VIEWS_STEREO_3D
@ R_IMF_EXR_FLAG_MULTIPART
void BKE_color_managed_view_settings_free(ColorManagedViewSettings *settings)
void BKE_color_managed_view_settings_blend_write(BlendWriter *writer, const ColorManagedViewSettings *settings)
char BKE_imtype_valid_channels(char imtype)
bool IMB_colormanagement_space_name_is_data(const char *name)
void BKE_image_format_update_color_space_for_type(ImageFormatData *format)
int BKE_image_path_ext_from_imtype_ensure(char *filepath, size_t filepath_maxncpy, char imtype)
int BKE_image_path_ext_from_imformat(const ImageFormatData *im_format, const char *r_ext[BKE_IMAGE_PATH_EXT_MAX])
eImageFormatDepth BKE_imtype_valid_depths_with_video(char imtype, const ID *owner_id)
void BKE_image_format_copy(ImageFormatData *imf_dst, const ImageFormatData *imf_src)
bool IMB_colormanagement_space_name_is_scene_linear(const char *name)
@ R_IMF_TIFF_CODEC_DEFLATE
@ R_IMF_TIFF_CODEC_PACKBITS
@ R_IMF_TIFF_CODEC_NONE
char name[258]
Definition DNA_ID.h:433
ImbFormatOptions foptions
const float * float_data() const
eImbFileType ftype
ColorManagedColorspaceSettings linear_colorspace_settings
ColorManagedViewSettings view_settings
ColorManagedDisplaySettings display_settings
eImageFormat_ViewsFormat views_format
ImageFormatData im_format
struct FFMpegCodecData ffcodecdata
ColorManagedViewSettings view_settings
ColorManagedDisplaySettings display_settings
struct RenderData r