Welcome to mirror list, hosted at ThFree Co, Russian Federation.

image_format.cc « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8d1aeac76fb60235df34ebf3d0c29eb5a5ef937b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2001-2002 NaN Holding BV. All rights reserved. */

/** \file
 * \ingroup bke
 */

#include <cstring>

#include "DNA_defaults.h"
#include "DNA_scene_types.h"

#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"

#include "IMB_colormanagement.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"

#include "BKE_colortools.h"
#include "BKE_image_format.h"

/* Init/Copy/Free */

void BKE_image_format_init(ImageFormatData *imf, const bool render)
{
  *imf = *DNA_struct_default_get(ImageFormatData);

  BKE_color_managed_display_settings_init(&imf->display_settings);

  if (render) {
    BKE_color_managed_view_settings_init_render(
        &imf->view_settings, &imf->display_settings, "Filmic");
  }
  else {
    BKE_color_managed_view_settings_init_default(&imf->view_settings, &imf->display_settings);
  }

  BKE_color_managed_colorspace_settings_init(&imf->linear_colorspace_settings);
}

void BKE_image_format_copy(ImageFormatData *imf_dst, const ImageFormatData *imf_src)
{
  memcpy(imf_dst, imf_src, sizeof(*imf_dst));
  BKE_color_managed_display_settings_copy(&imf_dst->display_settings, &imf_src->display_settings);
  BKE_color_managed_view_settings_copy(&imf_dst->view_settings, &imf_src->view_settings);
  BKE_color_managed_colorspace_settings_copy(&imf_dst->linear_colorspace_settings,
                                             &imf_src->linear_colorspace_settings);
}

void BKE_image_format_free(ImageFormatData *imf)
{
  BKE_color_managed_view_settings_free(&imf->view_settings);
}

void BKE_image_format_blend_read_data(BlendDataReader *reader, ImageFormatData *imf)
{
  BKE_color_managed_view_settings_blend_read_data(reader, &imf->view_settings);
}

void BKE_image_format_blend_write(BlendWriter *writer, ImageFormatData *imf)
{
  BKE_color_managed_view_settings_blend_write(writer, &imf->view_settings);
}

/* File Types */

int BKE_imtype_to_ftype(const char imtype, ImbFormatOptions *r_options)
{
  memset(r_options, 0, sizeof(*r_options));

  if (imtype == R_IMF_IMTYPE_TARGA) {
    return IMB_FTYPE_TGA;
  }
  if (imtype == R_IMF_IMTYPE_RAWTGA) {
    r_options->flag = RAWTGA;
    return IMB_FTYPE_TGA;
  }
  if (imtype == R_IMF_IMTYPE_IRIS) {
    return IMB_FTYPE_IMAGIC;
  }
#ifdef WITH_HDR
  if (imtype == R_IMF_IMTYPE_RADHDR) {
    return IMB_FTYPE_RADHDR;
  }
#endif
  if (imtype == R_IMF_IMTYPE_PNG) {
    r_options->quality = 15;
    return IMB_FTYPE_PNG;
  }
#ifdef WITH_DDS
  if (imtype == R_IMF_IMTYPE_DDS) {
    return IMB_FTYPE_DDS;
  }
#endif
  if (imtype == R_IMF_IMTYPE_BMP) {
    return IMB_FTYPE_BMP;
  }
#ifdef WITH_TIFF
  if (imtype == R_IMF_IMTYPE_TIFF) {
    return IMB_FTYPE_TIF;
  }
#endif
  if (ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
    return IMB_FTYPE_OPENEXR;
  }
#ifdef WITH_CINEON
  if (imtype == R_IMF_IMTYPE_CINEON) {
    return IMB_FTYPE_CINEON;
  }
  if (imtype == R_IMF_IMTYPE_DPX) {
    return IMB_FTYPE_DPX;
  }
#endif
#ifdef WITH_OPENJPEG
  if (imtype == R_IMF_IMTYPE_JP2) {
    r_options->flag |= JP2_JP2;
    r_options->quality = 90;
    return IMB_FTYPE_JP2;
  }
#endif
#ifdef WITH_WEBP
  if (imtype == R_IMF_IMTYPE_WEBP) {
    r_options->quality = 90;
    return IMB_FTYPE_WEBP;
  }
#endif

  r_options->quality = 90;
  return IMB_FTYPE_JPG;
}

char BKE_ftype_to_imtype(const int ftype, const ImbFormatOptions *options)
{
  if (ftype == IMB_FTYPE_NONE) {
    return R_IMF_IMTYPE_TARGA;
  }
  if (ftype == IMB_FTYPE_IMAGIC) {
    return R_IMF_IMTYPE_IRIS;
  }
#ifdef WITH_HDR
  if (ftype == IMB_FTYPE_RADHDR) {
    return R_IMF_IMTYPE_RADHDR;
  }
#endif
  if (ftype == IMB_FTYPE_PNG) {
    return R_IMF_IMTYPE_PNG;
  }
#ifdef WITH_DDS
  if (ftype == IMB_FTYPE_DDS) {
    return R_IMF_IMTYPE_DDS;
  }
#endif
  if (ftype == IMB_FTYPE_BMP) {
    return R_IMF_IMTYPE_BMP;
  }
#ifdef WITH_TIFF
  if (ftype == IMB_FTYPE_TIF) {
    return R_IMF_IMTYPE_TIFF;
  }
#endif
  if (ftype == IMB_FTYPE_OPENEXR) {
    return R_IMF_IMTYPE_OPENEXR;
  }
#ifdef WITH_CINEON
  if (ftype == IMB_FTYPE_CINEON) {
    return R_IMF_IMTYPE_CINEON;
  }
  if (ftype == IMB_FTYPE_DPX) {
    return R_IMF_IMTYPE_DPX;
  }
#endif
  if (ftype == IMB_FTYPE_TGA) {
    if (options && (options->flag & RAWTGA)) {
      return R_IMF_IMTYPE_RAWTGA;
    }

    return R_IMF_IMTYPE_TARGA;
  }
#ifdef WITH_OPENJPEG
  if (ftype == IMB_FTYPE_JP2) {
    return R_IMF_IMTYPE_JP2;
  }
#endif
#ifdef WITH_WEBP
  if (ftype == IMB_FTYPE_WEBP) {
    return R_IMF_IMTYPE_WEBP;
  }
#endif

  return R_IMF_IMTYPE_JPEG90;
}

bool BKE_imtype_is_movie(const char imtype)
{
  switch (imtype) {
    case R_IMF_IMTYPE_AVIRAW:
    case R_IMF_IMTYPE_AVIJPEG:
    case R_IMF_IMTYPE_FFMPEG:
    case R_IMF_IMTYPE_H264:
    case R_IMF_IMTYPE_THEORA:
    case R_IMF_IMTYPE_XVID:
      return true;
  }
  return false;
}

bool BKE_imtype_supports_zbuf(const char imtype)
{
  switch (imtype) {
    case R_IMF_IMTYPE_IRIZ:
    case R_IMF_IMTYPE_OPENEXR: /* but not R_IMF_IMTYPE_MULTILAYER */
      return true;
  }
  return false;
}

bool BKE_imtype_supports_compress(const char imtype)
{
  switch (imtype) {
    case R_IMF_IMTYPE_PNG:
      return true;
  }
  return false;
}

bool BKE_imtype_supports_quality(const char imtype)
{
  switch (imtype) {
    case R_IMF_IMTYPE_JPEG90:
    case R_IMF_IMTYPE_JP2:
    case R_IMF_IMTYPE_AVIJPEG:
    case R_IMF_IMTYPE_WEBP:
      return true;
  }
  return false;
}

bool BKE_imtype_requires_linear_float(const char imtype)
{
  switch (imtype) {
    case R_IMF_IMTYPE_CINEON:
    case R_IMF_IMTYPE_DPX:
    case R_IMF_IMTYPE_RADHDR:
    case R_IMF_IMTYPE_OPENEXR:
    case R_IMF_IMTYPE_MULTILAYER:
      return true;
  }
  return false;
}

char BKE_imtype_valid_channels(const char imtype, bool write_file)
{
  char chan_flag = IMA_CHAN_FLAG_RGB; /* Assume all support RGB. */

  /* Alpha. */
  switch (imtype) {
    case R_IMF_IMTYPE_BMP:
      if (write_file) {
        break;
      }
      ATTR_FALLTHROUGH;
    case R_IMF_IMTYPE_TARGA:
    case R_IMF_IMTYPE_RAWTGA:
    case R_IMF_IMTYPE_IRIS:
    case R_IMF_IMTYPE_PNG:
    case R_IMF_IMTYPE_TIFF:
    case R_IMF_IMTYPE_OPENEXR:
    case R_IMF_IMTYPE_MULTILAYER:
    case R_IMF_IMTYPE_DDS:
    case R_IMF_IMTYPE_JP2:
    case R_IMF_IMTYPE_DPX:
    case R_IMF_IMTYPE_WEBP:
      chan_flag |= IMA_CHAN_FLAG_RGBA;
      break;
  }

  /* BW. */
  switch (imtype) {
    case R_IMF_IMTYPE_BMP:
    case R_IMF_IMTYPE_PNG:
    case R_IMF_IMTYPE_JPEG90:
    case R_IMF_IMTYPE_TARGA:
    case R_IMF_IMTYPE_RAWTGA:
    case R_IMF_IMTYPE_TIFF:
    case R_IMF_IMTYPE_IRIS:
      chan_flag |= IMA_CHAN_FLAG_BW;
      break;
  }

  return chan_flag;
}

char BKE_imtype_valid_depths(const char imtype)
{
  switch (imtype) {
    case R_IMF_IMTYPE_RADHDR:
      return R_IMF_CHAN_DEPTH_32;
    case R_IMF_IMTYPE_TIFF:
      return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
    case R_IMF_IMTYPE_OPENEXR:
      return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
    case R_IMF_IMTYPE_MULTILAYER:
      return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
    /* NOTE: CINEON uses an unusual 10bits-LOG per channel. */
    case R_IMF_IMTYPE_DPX:
      return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_10 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
    case R_IMF_IMTYPE_CINEON:
      return R_IMF_CHAN_DEPTH_10;
    case R_IMF_IMTYPE_JP2:
      return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
    case R_IMF_IMTYPE_PNG:
      return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
    /* Most formats are 8bit only. */
    default:
      return R_IMF_CHAN_DEPTH_8;
  }
}

char BKE_imtype_from_arg(const char *imtype_arg)
{
  if (STREQ(imtype_arg, "TGA")) {
    return R_IMF_IMTYPE_TARGA;
  }
  if (STREQ(imtype_arg, "IRIS")) {
    return R_IMF_IMTYPE_IRIS;
  }
#ifdef WITH_DDS
  if (STREQ(imtype_arg, "DDS")) {
    return R_IMF_IMTYPE_DDS;
  }
#endif
  if (STREQ(imtype_arg, "JPEG")) {
    return R_IMF_IMTYPE_JPEG90;
  }
  if (STREQ(imtype_arg, "IRIZ")) {
    return R_IMF_IMTYPE_IRIZ;
  }
  if (STREQ(imtype_arg, "RAWTGA")) {
    return R_IMF_IMTYPE_RAWTGA;
  }
  if (STREQ(imtype_arg, "AVIRAW")) {
    return R_IMF_IMTYPE_AVIRAW;
  }
  if (STREQ(imtype_arg, "AVIJPEG")) {
    return R_IMF_IMTYPE_AVIJPEG;
  }
  if (STREQ(imtype_arg, "PNG")) {
    return R_IMF_IMTYPE_PNG;
  }
  if (STREQ(imtype_arg, "BMP")) {
    return R_IMF_IMTYPE_BMP;
  }
#ifdef WITH_HDR
  if (STREQ(imtype_arg, "HDR")) {
    return R_IMF_IMTYPE_RADHDR;
  }
#endif
#ifdef WITH_TIFF
  if (STREQ(imtype_arg, "TIFF")) {
    return R_IMF_IMTYPE_TIFF;
  }
#endif
#ifdef WITH_OPENEXR
  if (STREQ(imtype_arg, "OPEN_EXR")) {
    return R_IMF_IMTYPE_OPENEXR;
  }
  if (STREQ(imtype_arg, "OPEN_EXR_MULTILAYER")) {
    return R_IMF_IMTYPE_MULTILAYER;
  }
  if (STREQ(imtype_arg, "EXR")) {
    return R_IMF_IMTYPE_OPENEXR;
  }
  if (STREQ(imtype_arg, "MULTILAYER")) {
    return R_IMF_IMTYPE_MULTILAYER;
  }
#endif
  if (STREQ(imtype_arg, "FFMPEG")) {
    return R_IMF_IMTYPE_FFMPEG;
  }
#ifdef WITH_CINEON
  if (STREQ(imtype_arg, "CINEON")) {
    return R_IMF_IMTYPE_CINEON;
  }
  if (STREQ(imtype_arg, "DPX")) {
    return R_IMF_IMTYPE_DPX;
  }
#endif
#ifdef WITH_OPENJPEG
  if (STREQ(imtype_arg, "JP2")) {
    return R_IMF_IMTYPE_JP2;
  }
#endif
#ifdef WITH_WEBP
  if (STREQ(imtype_arg, "WEBP")) {
    return R_IMF_IMTYPE_WEBP;
  }
#endif

  return R_IMF_IMTYPE_INVALID;
}

/* File Paths */

static bool do_add_image_extension(char *string,
                                   const char imtype,
                                   const ImageFormatData *im_format)
{
  const char *extension = nullptr;
  const char *extension_test;
  (void)im_format; /* may be unused, depends on build options */

  if (imtype == R_IMF_IMTYPE_IRIS) {
    if (!BLI_path_extension_check(string, extension_test = ".rgb")) {
      extension = extension_test;
    }
  }
  else if (imtype == R_IMF_IMTYPE_IRIZ) {
    if (!BLI_path_extension_check(string, extension_test = ".rgb")) {
      extension = extension_test;
    }
  }
#ifdef WITH_HDR
  else if (imtype == R_IMF_IMTYPE_RADHDR) {
    if (!BLI_path_extension_check(string, extension_test = ".hdr")) {
      extension = extension_test;
    }
  }
#endif
  else if (ELEM(imtype,
                R_IMF_IMTYPE_PNG,
                R_IMF_IMTYPE_FFMPEG,
                R_IMF_IMTYPE_H264,
                R_IMF_IMTYPE_THEORA,
                R_IMF_IMTYPE_XVID)) {
    if (!BLI_path_extension_check(string, extension_test = ".png")) {
      extension = extension_test;
    }
  }
#ifdef WITH_DDS
  else if (imtype == R_IMF_IMTYPE_DDS) {
    if (!BLI_path_extension_check(string, extension_test = ".dds")) {
      extension = extension_test;
    }
  }
#endif
  else if (ELEM(imtype, R_IMF_IMTYPE_TARGA, R_IMF_IMTYPE_RAWTGA)) {
    if (!BLI_path_extension_check(string, extension_test = ".tga")) {
      extension = extension_test;
    }
  }
  else if (imtype == R_IMF_IMTYPE_BMP) {
    if (!BLI_path_extension_check(string, extension_test = ".bmp")) {
      extension = extension_test;
    }
  }
#ifdef WITH_TIFF
  else if (imtype == R_IMF_IMTYPE_TIFF) {
    if (!BLI_path_extension_check_n(string, extension_test = ".tif", ".tiff", nullptr)) {
      extension = extension_test;
    }
  }
#endif
#ifdef WITH_OPENIMAGEIO
  else if (imtype == R_IMF_IMTYPE_PSD) {
    if (!BLI_path_extension_check(string, extension_test = ".psd")) {
      extension = extension_test;
    }
  }
#endif
#ifdef WITH_OPENEXR
  else if (ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
    if (!BLI_path_extension_check(string, extension_test = ".exr")) {
      extension = extension_test;
    }
  }
#endif
#ifdef WITH_CINEON
  else if (imtype == R_IMF_IMTYPE_CINEON) {
    if (!BLI_path_extension_check(string, extension_test = ".cin")) {
      extension = extension_test;
    }
  }
  else if (imtype == R_IMF_IMTYPE_DPX) {
    if (!BLI_path_extension_check(string, extension_test = ".dpx")) {
      extension = extension_test;
    }
  }
#endif
#ifdef WITH_OPENJPEG
  else if (imtype == R_IMF_IMTYPE_JP2) {
    if (im_format) {
      if (im_format->jp2_codec == R_IMF_JP2_CODEC_JP2) {
        if (!BLI_path_extension_check(string, extension_test = ".jp2")) {
          extension = extension_test;
        }
      }
      else if (im_format->jp2_codec == R_IMF_JP2_CODEC_J2K) {
        if (!BLI_path_extension_check(string, extension_test = ".j2c")) {
          extension = extension_test;
        }
      }
      else {
        BLI_assert_msg(0, "Unsupported jp2 codec was specified in im_format->jp2_codec");
      }
    }
    else {
      if (!BLI_path_extension_check(string, extension_test = ".jp2")) {
        extension = extension_test;
      }
    }
  }
#endif
#ifdef WITH_WEBP
  else if (imtype == R_IMF_IMTYPE_WEBP) {
    if (!BLI_path_extension_check(string, extension_test = ".webp")) {
      extension = extension_test;
    }
  }
#endif
  else {  //   R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90 etc
    if (!BLI_path_extension_check_n(string, extension_test = ".jpg", ".jpeg", nullptr)) {
      extension = extension_test;
    }
  }

  if (extension) {
    /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */
    /* remove any other known image extension */
    if (BLI_path_extension_check_array(string, imb_ext_image)) {
      return BLI_path_extension_replace(string, FILE_MAX, extension);
    }

    return BLI_path_extension_ensure(string, FILE_MAX, extension);
  }

  return false;
}

int BKE_image_path_ensure_ext_from_imformat(char *string, const ImageFormatData *im_format)
{
  return do_add_image_extension(string, im_format->imtype, im_format);
}

int BKE_image_path_ensure_ext_from_imtype(char *string, const char imtype)
{
  return do_add_image_extension(string, imtype, nullptr);
}

static void do_makepicstring(char *string,
                             const char *base,
                             const char *relbase,
                             int frame,
                             const char imtype,
                             const ImageFormatData *im_format,
                             const bool use_ext,
                             const bool use_frames,
                             const char *suffix)
{
  if (string == nullptr) {
    return;
  }
  BLI_strncpy(string, base, FILE_MAX - 10); /* weak assumption */
  BLI_path_abs(string, relbase);

  if (use_frames) {
    BLI_path_frame(string, frame, 4);
  }

  if (suffix) {
    BLI_path_suffix(string, FILE_MAX, suffix, "");
  }

  if (use_ext) {
    do_add_image_extension(string, imtype, im_format);
  }
}

void BKE_image_path_from_imformat(char *string,
                                  const char *base,
                                  const char *relbase,
                                  int frame,
                                  const ImageFormatData *im_format,
                                  const bool use_ext,
                                  const bool use_frames,
                                  const char *suffix)
{
  do_makepicstring(
      string, base, relbase, frame, im_format->imtype, im_format, use_ext, use_frames, suffix);
}

void BKE_image_path_from_imtype(char *string,
                                const char *base,
                                const char *relbase,
                                int frame,
                                const char imtype,
                                const bool use_ext,
                                const bool use_frames,
                                const char *suffix)
{
  do_makepicstring(string, base, relbase, frame, imtype, nullptr, use_ext, use_frames, suffix);
}

/* ImBuf Conversion */

void BKE_image_format_to_imbuf(ImBuf *ibuf, const ImageFormatData *imf)
{
  /* Write to ImBuf in preparation for file writing. */
  char imtype = imf->imtype;
  char compress = imf->compress;
  char quality = imf->quality;

  /* initialize all from image format */
  ibuf->foptions.flag = 0;

  if (imtype == R_IMF_IMTYPE_IRIS) {
    ibuf->ftype = IMB_FTYPE_IMAGIC;
  }
#ifdef WITH_HDR
  else if (imtype == R_IMF_IMTYPE_RADHDR) {
    ibuf->ftype = IMB_FTYPE_RADHDR;
  }
#endif
  else if (ELEM(imtype,
                R_IMF_IMTYPE_PNG,
                R_IMF_IMTYPE_FFMPEG,
                R_IMF_IMTYPE_H264,
                R_IMF_IMTYPE_THEORA,
                R_IMF_IMTYPE_XVID)) {
    ibuf->ftype = IMB_FTYPE_PNG;

    if (imtype == R_IMF_IMTYPE_PNG) {
      if (imf->depth == R_IMF_CHAN_DEPTH_16) {
        ibuf->foptions.flag |= PNG_16BIT;
      }

      ibuf->foptions.quality = compress;
    }
  }
#ifdef WITH_DDS
  else if (imtype == R_IMF_IMTYPE_DDS) {
    ibuf->ftype = IMB_FTYPE_DDS;
  }
#endif
  else if (imtype == R_IMF_IMTYPE_BMP) {
    ibuf->ftype = IMB_FTYPE_BMP;
  }
#ifdef WITH_TIFF
  else if (imtype == R_IMF_IMTYPE_TIFF) {
    ibuf->ftype = IMB_FTYPE_TIF;

    if (imf->depth == R_IMF_CHAN_DEPTH_16) {
      ibuf->foptions.flag |= TIF_16BIT;
    }
    if (imf->tiff_codec == R_IMF_TIFF_CODEC_NONE) {
      ibuf->foptions.flag |= TIF_COMPRESS_NONE;
    }
    else if (imf->tiff_codec == R_IMF_TIFF_CODEC_DEFLATE) {
      ibuf->foptions.flag |= TIF_COMPRESS_DEFLATE;
    }
    else if (imf->tiff_codec == R_IMF_TIFF_CODEC_LZW) {
      ibuf->foptions.flag |= TIF_COMPRESS_LZW;
    }
    else if (imf->tiff_codec == R_IMF_TIFF_CODEC_PACKBITS) {
      ibuf->foptions.flag |= TIF_COMPRESS_PACKBITS;
    }
  }
#endif
#ifdef WITH_OPENEXR
  else if (ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
    ibuf->ftype = IMB_FTYPE_OPENEXR;
    if (imf->depth == R_IMF_CHAN_DEPTH_16) {
      ibuf->foptions.flag |= OPENEXR_HALF;
    }
    ibuf->foptions.flag |= (imf->exr_codec & OPENEXR_COMPRESS);

    if (!(imf->flag & R_IMF_FLAG_ZBUF)) {
      /* Signal for exr saving. */
      IMB_freezbuffloatImBuf(ibuf);
    }
  }
#endif
#ifdef WITH_CINEON
  else if (imtype == R_IMF_IMTYPE_CINEON) {
    ibuf->ftype = IMB_FTYPE_CINEON;
    if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
      ibuf->foptions.flag |= CINEON_LOG;
    }
    if (imf->depth == R_IMF_CHAN_DEPTH_16) {
      ibuf->foptions.flag |= CINEON_16BIT;
    }
    else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
      ibuf->foptions.flag |= CINEON_12BIT;
    }
    else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
      ibuf->foptions.flag |= CINEON_10BIT;
    }
  }
  else if (imtype == R_IMF_IMTYPE_DPX) {
    ibuf->ftype = IMB_FTYPE_DPX;
    if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
      ibuf->foptions.flag |= CINEON_LOG;
    }
    if (imf->depth == R_IMF_CHAN_DEPTH_16) {
      ibuf->foptions.flag |= CINEON_16BIT;
    }
    else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
      ibuf->foptions.flag |= CINEON_12BIT;
    }
    else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
      ibuf->foptions.flag |= CINEON_10BIT;
    }
  }
#endif
  else if (imtype == R_IMF_IMTYPE_TARGA) {
    ibuf->ftype = IMB_FTYPE_TGA;
  }
  else if (imtype == R_IMF_IMTYPE_RAWTGA) {
    ibuf->ftype = IMB_FTYPE_TGA;
    ibuf->foptions.flag = RAWTGA;
  }
#ifdef WITH_OPENJPEG
  else if (imtype == R_IMF_IMTYPE_JP2) {
    if (quality < 10) {
      quality = 90;
    }
    ibuf->ftype = IMB_FTYPE_JP2;
    ibuf->foptions.quality = quality;

    if (imf->depth == R_IMF_CHAN_DEPTH_16) {
      ibuf->foptions.flag |= JP2_16BIT;
    }
    else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
      ibuf->foptions.flag |= JP2_12BIT;
    }

    if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
      ibuf->foptions.flag |= JP2_YCC;
    }

    if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_PRESET) {
      ibuf->foptions.flag |= JP2_CINE;
      if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48) {
        ibuf->foptions.flag |= JP2_CINE_48FPS;
      }
    }

    if (imf->jp2_codec == R_IMF_JP2_CODEC_JP2) {
      ibuf->foptions.flag |= JP2_JP2;
    }
    else if (imf->jp2_codec == R_IMF_JP2_CODEC_J2K) {
      ibuf->foptions.flag |= JP2_J2K;
    }
    else {
      BLI_assert_msg(0, "Unsupported jp2 codec was specified in im_format->jp2_codec");
    }
  }
#endif
#ifdef WITH_WEBP
  else if (imtype == R_IMF_IMTYPE_WEBP) {
    ibuf->ftype = IMB_FTYPE_WEBP;
    ibuf->foptions.quality = quality;
  }
#endif
  else {
    /* #R_IMF_IMTYPE_JPEG90, etc. default to JPEG. */
    if (quality < 10) {
      quality = 90;
    }
    ibuf->ftype = IMB_FTYPE_JPG;
    ibuf->foptions.quality = quality;
  }
}

void BKE_image_format_from_imbuf(ImageFormatData *im_format, const ImBuf *imbuf)
{
  /* Read from ImBuf after file read. */
  int ftype = imbuf->ftype;
  int custom_flags = imbuf->foptions.flag;
  char quality = imbuf->foptions.quality;

  BKE_image_format_init(im_format, false);

  /* file type */
  if (ftype == IMB_FTYPE_IMAGIC) {
    im_format->imtype = R_IMF_IMTYPE_IRIS;
  }
#ifdef WITH_HDR
  else if (ftype == IMB_FTYPE_RADHDR) {
    im_format->imtype = R_IMF_IMTYPE_RADHDR;
  }
#endif
  else if (ftype == IMB_FTYPE_PNG) {
    im_format->imtype = R_IMF_IMTYPE_PNG;

    if (custom_flags & PNG_16BIT) {
      im_format->depth = R_IMF_CHAN_DEPTH_16;
    }

    im_format->compress = quality;
  }

#ifdef WITH_DDS
  else if (ftype == IMB_FTYPE_DDS) {
    im_format->imtype = R_IMF_IMTYPE_DDS;
  }
#endif
  else if (ftype == IMB_FTYPE_BMP) {
    im_format->imtype = R_IMF_IMTYPE_BMP;
  }
#ifdef WITH_TIFF
  else if (ftype == IMB_FTYPE_TIF) {
    im_format->imtype = R_IMF_IMTYPE_TIFF;
    if (custom_flags & TIF_16BIT) {
      im_format->depth = R_IMF_CHAN_DEPTH_16;
    }
    if (custom_flags & TIF_COMPRESS_NONE) {
      im_format->tiff_codec = R_IMF_TIFF_CODEC_NONE;
    }
    if (custom_flags & TIF_COMPRESS_DEFLATE) {
      im_format->tiff_codec = R_IMF_TIFF_CODEC_DEFLATE;
    }
    if (custom_flags & TIF_COMPRESS_LZW) {
      im_format->tiff_codec = R_IMF_TIFF_CODEC_LZW;
    }
    if (custom_flags & TIF_COMPRESS_PACKBITS) {
      im_format->tiff_codec = R_IMF_TIFF_CODEC_PACKBITS;
    }
  }
#endif

#ifdef WITH_OPENEXR
  else if (ftype == IMB_FTYPE_OPENEXR) {
    im_format->imtype = R_IMF_IMTYPE_OPENEXR;
    if (custom_flags & OPENEXR_HALF) {
      im_format->depth = R_IMF_CHAN_DEPTH_16;
    }
    if (custom_flags & OPENEXR_COMPRESS) {
      im_format->exr_codec = R_IMF_EXR_CODEC_ZIP; /* Can't determine compression */
    }
    if (imbuf->zbuf_float) {
      im_format->flag |= R_IMF_FLAG_ZBUF;
    }
  }
#endif

#ifdef WITH_CINEON
  else if (ftype == IMB_FTYPE_CINEON) {
    im_format->imtype = R_IMF_IMTYPE_CINEON;
  }
  else if (ftype == IMB_FTYPE_DPX) {
    im_format->imtype = R_IMF_IMTYPE_DPX;
  }
#endif
  else if (ftype == IMB_FTYPE_TGA) {
    if (custom_flags & RAWTGA) {
      im_format->imtype = R_IMF_IMTYPE_RAWTGA;
    }
    else {
      im_format->imtype = R_IMF_IMTYPE_TARGA;
    }
  }
#ifdef WITH_OPENJPEG
  else if (ftype == IMB_FTYPE_JP2) {
    im_format->imtype = R_IMF_IMTYPE_JP2;
    im_format->quality = quality;

    if (custom_flags & JP2_16BIT) {
      im_format->depth = R_IMF_CHAN_DEPTH_16;
    }
    else if (custom_flags & JP2_12BIT) {
      im_format->depth = R_IMF_CHAN_DEPTH_12;
    }

    if (custom_flags & JP2_YCC) {
      im_format->jp2_flag |= R_IMF_JP2_FLAG_YCC;
    }

    if (custom_flags & JP2_CINE) {
      im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_PRESET;
      if (custom_flags & JP2_CINE_48FPS) {
        im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_48;
      }
    }

    if (custom_flags & JP2_JP2) {
      im_format->jp2_codec = R_IMF_JP2_CODEC_JP2;
    }
    else if (custom_flags & JP2_J2K) {
      im_format->jp2_codec = R_IMF_JP2_CODEC_J2K;
    }
    else {
      BLI_assert_msg(0, "Unsupported jp2 codec was specified in file type");
    }
  }
#endif
#ifdef WITH_WEBP
  else if (ftype == IMB_FTYPE_WEBP) {
    im_format->imtype = R_IMF_IMTYPE_WEBP;
    im_format->quality = quality;
  }
#endif

  else {
    im_format->imtype = R_IMF_IMTYPE_JPEG90;
    im_format->quality = quality;
  }

  /* planes */
  im_format->planes = imbuf->planes;
}

bool BKE_image_format_is_byte(const ImageFormatData *imf)
{
  return (imf->depth == R_IMF_CHAN_DEPTH_8) && (BKE_imtype_valid_depths(imf->imtype) & imf->depth);
}

/* Color Management */

void BKE_image_format_color_management_copy(ImageFormatData *imf, const ImageFormatData *imf_src)
{
  BKE_color_managed_view_settings_free(&imf->view_settings);

  BKE_color_managed_display_settings_copy(&imf->display_settings, &imf_src->display_settings);
  BKE_color_managed_view_settings_copy(&imf->view_settings, &imf_src->view_settings);
  BKE_color_managed_colorspace_settings_copy(&imf->linear_colorspace_settings,
                                             &imf_src->linear_colorspace_settings);
}

void BKE_image_format_color_management_copy_from_scene(ImageFormatData *imf, const Scene *scene)
{
  BKE_color_managed_view_settings_free(&imf->view_settings);

  BKE_color_managed_display_settings_copy(&imf->display_settings, &scene->display_settings);
  BKE_color_managed_view_settings_copy(&imf->view_settings, &scene->view_settings);
  STRNCPY(imf->linear_colorspace_settings.name,
          IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR));
}

/* Output */

void BKE_image_format_init_for_write(ImageFormatData *imf,
                                     const Scene *scene_src,
                                     const ImageFormatData *imf_src)
{
  *imf = (imf_src) ? *imf_src : scene_src->r.im_format;

  if (imf_src && imf_src->color_management == R_IMF_COLOR_MANAGEMENT_OVERRIDE) {
    /* Use settings specific to one node, image save operation, etc. */
    BKE_color_managed_display_settings_copy(&imf->display_settings, &imf_src->display_settings);
    BKE_color_managed_view_settings_copy(&imf->view_settings, &imf_src->view_settings);
    BKE_color_managed_colorspace_settings_copy(&imf->linear_colorspace_settings,
                                               &imf_src->linear_colorspace_settings);
  }
  else if (scene_src->r.im_format.color_management == R_IMF_COLOR_MANAGEMENT_OVERRIDE) {
    /* Use scene settings specific to render output. */
    BKE_color_managed_display_settings_copy(&imf->display_settings,
                                            &scene_src->r.im_format.display_settings);
    BKE_color_managed_view_settings_copy(&imf->view_settings,
                                         &scene_src->r.im_format.view_settings);
    BKE_color_managed_colorspace_settings_copy(&imf->linear_colorspace_settings,
                                               &scene_src->r.im_format.linear_colorspace_settings);
  }
  else {
    /* Use general scene settings also used for display. */
    BKE_color_managed_display_settings_copy(&imf->display_settings, &scene_src->display_settings);
    BKE_color_managed_view_settings_copy(&imf->view_settings, &scene_src->view_settings);
    STRNCPY(imf->linear_colorspace_settings.name,
            IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR));
  }
}