-
Notifications
You must be signed in to change notification settings - Fork 0
/
logctool.cpp
1361 lines (1224 loc) · 51.9 KB
/
logctool.cpp
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022 - present Mikael Sundell.
//
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <regex>
#include <variant>
// imath
#include <Imath/ImathMatrix.h>
#include <Imath/ImathVec.h>
// openimageio
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/typedesc.h>
#include <OpenImageIO/argparse.h>
#include <OpenImageIO/filesystem.h>
#include <OpenImageIO/sysutil.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
using namespace OIIO;
// opencolorio
#include <OpenColorIO/OpenColorIO.h>
using namespace OpenColorIO_v2_3;
// boost
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace boost::property_tree;
// prints
template <typename T>
static void
print_info(std::string param, const T& value = T()) {
std::cout << "info: " << param << value << std::endl;
}
static void
print_info(std::string param) {
print_info<std::string>(param);
}
template <typename T>
static void
print_warning(std::string param, const T& value = T()) {
std::cout << "warning: " << param << value << std::endl;
}
static void
print_warning(std::string param) {
print_warning<std::string>(param);
}
template <typename T>
static void
print_error(std::string param, const T& value = T()) {
std::cerr << "error: " << param << value << std::endl;
}
static void
print_error(std::string param) {
print_error<std::string>(param);
}
// logc tool
struct LogCTool
{
bool help = false;
bool verbose = false;
bool transforms = false;
int ei = 800;
int width = 1024;
int height = 512;
int channels = 3;
float midgray = 0.18;
std::string dataformat = "float";
std::string transform;
std::string outputtype = "stepchart";
std::string outputfilename;
std::string outputfalsecolorcubefile;
std::string outputstopscubefile;
bool outputlinear = false;
bool outputnolabels = false;
int code = EXIT_SUCCESS;
};
static LogCTool tool;
static void
print_help(ArgParse& ap)
{
ap.print_help();
}
// utils - dates
std::string datetime()
{
std::time_t now = time(NULL);
struct tm tm;
Sysutil::get_local_time(&now, &tm);
char datetime[20];
strftime(datetime, 20, "%Y-%m-%d %H:%M:%S", &tm);
return std::string(datetime);
}
// utils - colors
Imath::Vec3<float> rgb_from_hsv(const Imath::Vec3<float>& hsv) {
float hue = hsv.x;
float saturation = hsv.y;
float value = hsv.z;
if (hue < std::numeric_limits<float>::epsilon()) {
return Imath::Vec3<float>(0, 0, 0);
}
int hi = static_cast<int>(std::floor(hue / 60.0f)) % 6;
float f = hue / 60.0f - static_cast<float>(hi);
float p = value * (1.0f - saturation);
float q = value * (1.0f - f * saturation);
float t = value * (1.0f - (1.0f - f) * saturation);
float r, g, b;
switch (hi) {
case 0: r = value; g = t; b = p; break;
case 1: r = q; g = value; b = p; break;
case 2: r = p; g = value; b = t; break;
case 3: r = p; g = q; b = value; break;
case 4: r = t; g = p; b = value; break;
case 5: r = value; g = p; b = q; break;
}
return Imath::Vec3<float>(r, g, b);
}
float color_by_gamma(float value, float gamma) {
return pow(value, gamma);
}
// utils - types
int int_by_10bit(int value)
{
return (value >> 6); // bit shift by 6 for 10 bit representation
}
std::string str_by_float(float value)
{
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << value;
return oss.str();
}
std::string str_by_percent(float value)
{
std::ostringstream oss;
oss << static_cast<int>(value * 100) << '%';
return oss.str();
}
std::string str_by_int(int value)
{
return std::to_string(value);
}
std::string str_by_10bit(int value)
{
return std::to_string(int_by_10bit(value)); // 10bit stored 16bit and bitshifted for strings
}
// utils - filesystem
std::string program_path(const std::string& path)
{
return Filesystem::parent_path(Sysutil::this_program_path()) + path;
}
std::string font_path(const std::string& font)
{
return Filesystem::parent_path(Sysutil::this_program_path()) + "/fonts/" + font;
}
std::string resources_path(const std::string& resource)
{
return Filesystem::parent_path(Sysutil::this_program_path()) + "/resources/" + resource;
}
// utils - colorspaces
Imath::Vec3<float> mult_from_matrix(const Imath::Vec3<float>& src, const Imath::Matrix33<float>& matrix) {
return src * matrix.transposed(); // imath row-order from column-order convention
}
Imath::Vec3<float> ciexyzd50_from_lab(const Imath::Vec3<float>& src) {
const double Xn = 0.9642;
const double Yn = 1.00000;
const double Zn = 0.8251;
double fy = (src.x + 16.0) / 116.0;
double fx = fy + (src.y / 500.0);
double fz = fy - (src.z / 200.0);
double fx3 = std::pow(fx, 3.0);
double fz3 = std::pow(fz, 3.0);
float X = (fx3 > 0.008856) ? fx3 : ((fx - 16.0 / 116.0) / 7.787);
float Y = (src.x > 8.0) ? std::pow(((src.x + 16.0) / 116.0), 3.0) : (src.x / 903.3);
float Z = (fz3 > 0.008856) ? fz3 : ((fz - 16.0 / 116.0) / 7.787);
X *= Xn;
Y *= Yn;
Z *= Zn;
return Imath::Vec3<float>(X, Y, Z);
}
Imath::Vec3<float> ciexyzd65_from_D50(const Imath::Vec3<float>& src) {
// http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
Imath::Matrix33<float> matrix(
0.9555766f, -0.0230393f, 0.0631636f,
-0.0282895f, 1.0099416f, 0.0210077f,
0.0122982f, -0.020483f, 1.3299098f);
return mult_from_matrix(src, matrix);
}
// logc3 colorspace
struct LogC3Colorspace
{
int ei;
float cut;
float a;
float b;
float c;
float d;
float e;
float f;
float lin2log(float lin)
{
return ((lin > cut) ? c * log10(a * lin + b) + d : e * lin + f);
}
float log2lin(float log)
{
return ((log > e * cut + f) ? (pow(10, (log - d) / c) - b) / a : (log - f) / e);
}
Imath::Vec3<float> ciexyz_awg(Imath::Vec3<float> color)
{
Imath::Matrix33<float> matrix(
1.789066f, -0.482534f, -0.200076f,
-0.639849f, 1.396400f, 0.194432f,
-0.041532f, 0.082335f, 0.878868f);
return mult_from_matrix(color, matrix);
}
Imath::Vec3<float> wide_awg(Imath::Vec3<float> color)
{
Imath::Matrix33<float> matrix(
0.638008f, 0.214704f, 0.097744f,
0.291954f, 0.823841f, -0.115795f,
0.002798f, -0.067034f, 1.153294f);
return mult_from_matrix(color, matrix);
}
};
// lut transform
struct LutTransform
{
std::string description;
std::string filename;
};
// colorchecker patch
struct ColorCheckerPatch
{
int no;
std::string number;
float sRGB_r;
float sRGB_g;
float sRGB_b;
float cieLabd50_l;
float cieLabd50_a;
float cieLabd50_b;
float munsell_hue;
float munsell_value;
float munsell_chroma;
};
// main
int
main( int argc, const char * argv[])
{
// Helpful for debugging to make sure that any crashes dump a stack
// trace.
Sysutil::setup_crash_stacktrace("stdout");
Filesystem::convert_native_arguments(argc, (const char**)argv);
ArgParse ap;
ap.intro("logctool -- a set of utilities for processing logc encoded images\n");
ap.usage("logctool [options] filename...")
.add_help(false)
.exit_on_error(true);
ap.separator("General flags:");
ap.arg("--help", &tool.help)
.help("Print help message");
ap.arg("-v", &tool.verbose)
.help("Verbose status messages");
ap.arg("--transforms", &tool.transforms)
.help("List all transforms");
ap.arg("--ei %d:EI", &tool.ei)
.help("LogC exposure index");
ap.arg("--dataformat %s:DATAFORMAT", &tool.dataformat)
.help("LogC format (default: float, uint8, uint10, uint16 and uint32)");
ap.arg("--transform %s:TRANSFORM", &tool.transform)
.help("LUT transform");
ap.separator("Output flags:");
ap.arg("--outputtype %s:OUTTYPE", &tool.outputtype)
.help("Output type (default: stepchart, colorchecker)");
ap.arg("--outputfilename %s:OUTFILENAME", &tool.outputfilename)
.help("Output filename of log steps");
ap.arg("--outputwidth %d:WIDTH", &tool.width)
.help("Output width of log steps");
ap.arg("--outputheight %d:HEIGHT", &tool.height)
.help("Output height of log steps");
ap.arg("--outputlinear", &tool.outputlinear)
.help("Output linear steps");
ap.arg("--outputnolabels", &tool.outputnolabels)
.help("Output no labels");
ap.arg("--outputfalsecolorcubefile %s:FILE", &tool.outputfalsecolorcubefile)
.help("Optional output false color cube (lut) file");
ap.arg("--outputstopscubefile %s:FILE", &tool.outputstopscubefile)
.help("Optional output stops cube (lut) file");
// clang-format on
if (ap.parse_args(argc, (const char**)argv) < 0) {
print_error("Could no parse arguments: ", ap.geterror());
print_help(ap);
ap.abort();
return EXIT_FAILURE;
}
if (ap["help"].get<int>()) {
print_help(ap);
ap.abort();
return EXIT_SUCCESS;
}
if (!tool.transforms) {
if (!tool.ei) {
print_error("missing parameter: ", "ei");
ap.briefusage();
ap.abort();
return EXIT_FAILURE;
}
if (!tool.dataformat.size()) {
print_error("missing parameter: ", "dataformat");
ap.briefusage();
ap.abort();
return EXIT_FAILURE;
}
if (!tool.outputtype.size()) {
print_error("missing parameter: ", "outputtype");
ap.briefusage();
ap.abort();
return EXIT_FAILURE;
}
if (!tool.outputfilename.size()) {
print_error("missing parameter: ", "outputfilename");
ap.briefusage();
ap.abort();
return EXIT_FAILURE;
}
if (argc <= 1) {
ap.briefusage();
print_error("For detailed help: logctool --help");
return EXIT_FAILURE;
}
}
// logc program
print_info("logctool -- a set of utilities for processing logc encoded images");
// colorspaces
std::map<std::string, LutTransform> transforms;
std::string jsonfile = resources_path("logctool.json");
std::ifstream json(jsonfile);
if (json.is_open()) {
ptree pt;
read_json(jsonfile, pt);
for (const std::pair<const ptree::key_type, ptree>& item : pt) {
std::string name = item.first;
const ptree data = item.second;
LutTransform transform {
resources_path(data.get<std::string>("description", "")),
resources_path(data.get<std::string>("filename", "")),
};
if (!Filesystem::exists(transform.filename)) {
print_warning("'filename' does not exist for transform: ", transform.filename);
continue;
}
transforms[name] = transform;
}
} else {
print_error("could not open transforms file: ", jsonfile);
ap.abort();
return EXIT_FAILURE;
}
if (tool.transforms) {
print_info("Transforms:");
for (const std::pair<std::string, LutTransform>& pair : transforms) {
print_info(" ", pair.first);
}
return EXIT_SUCCESS;
}
if (tool.transform.size()) {
if (!transforms.count(tool.transform)) {
print_error("unknown transform: ", tool.transform);
ap.abort();
return EXIT_FAILURE;
}
}
// logc midgray
float midgray = 0.18f;
float midlog = 0.0;
// logc colorspace
LogC3Colorspace colorspace;
std::vector<LogC3Colorspace> colorspaces =
{
// ei cut a b c d e f
LogC3Colorspace() = { 160, 0.005561, 5.555556, 0.080216, 0.269036, 0.381991, 5.842037, 0.092778 },
LogC3Colorspace() = { 200, 0.006208, 5.555556, 0.076621, 0.266007, 0.382478, 5.776265, 0.092782 },
LogC3Colorspace() = { 250, 0.006871, 5.555556, 0.072941, 0.262978, 0.382966, 5.710494, 0.092786 },
LogC3Colorspace() = { 320, 0.007622, 5.555556, 0.068768, 0.259627, 0.383508, 5.637732, 0.092791 },
LogC3Colorspace() = { 400, 0.008318, 5.555556, 0.064901, 0.256598, 0.383999, 5.571960, 0.092795 },
LogC3Colorspace() = { 500, 0.009031, 5.555556, 0.060939, 0.253569, 0.384493, 5.506188, 0.092800 },
LogC3Colorspace() = { 640, 0.009840, 5.555556, 0.056443, 0.250219, 0.385040, 5.433426, 0.092805 },
// 800 default gamma
LogC3Colorspace() = { 800, 0.010591, 5.555556, 0.052272, 0.247190, 0.385537, 5.367655, 0.092809 },
LogC3Colorspace() = { 1000, 0.011361, 5.555556, 0.047996, 0.244161, 0.386036, 5.301883, 0.092814 },
LogC3Colorspace() = { 1280, 0.012235, 5.555556, 0.043137, 0.240810, 0.386590, 5.229121, 0.092819 },
LogC3Colorspace() = { 1600, 0.013047, 5.555556, 0.038625, 0.237781, 0.387093, 5.163350, 0.092824 }
};
for(LogC3Colorspace logccolorspace : colorspaces) {
if (tool.ei == logccolorspace.ei) {
colorspace = logccolorspace;
break;
}
}
// image data
print_info("image data");
if (colorspace.ei > 0) {
print_info("ei: ", colorspace.ei);
if (tool.verbose) {
print_info("cut: ", colorspace.cut);
print_info("a: ", colorspace.a);
print_info("b: ", colorspace.b);
print_info("c: ", colorspace.c);
print_info("d: ", colorspace.d);
print_info("e: ", colorspace.e);
print_info("f: ", colorspace.f);
}
}
else {
print_error("unknown ei: ", tool.ei);
ap.abort();
return EXIT_FAILURE;
}
// image format
TypeDesc typedesc = TypeDesc::UNKNOWN;
bool is10bit = false;
if (tool.dataformat == "float") {
typedesc = TypeDesc::FLOAT;
}
else if (tool.dataformat == "uint8") {
typedesc = TypeDesc::UINT8;
}
else if (tool.dataformat == "uint10") {
typedesc = TypeDesc::UINT16;
is10bit = true; // 10bit stored 16bit and bitshifted in formats like DPX
}
else if (tool.dataformat == "uint16") {
typedesc = TypeDesc::UINT16;
}
else if (tool.dataformat == "uint32") {
typedesc = TypeDesc::UINT32;
}
else {
print_error("unknown data format: ", tool.dataformat);
ap.abort();
return EXIT_FAILURE;
}
// LUT info
ConstConfigRcPtr config = Config::CreateRaw();
ConstCPUProcessorRcPtr transformProcessor;
if (tool.transform.size()) {
LutTransform transform = transforms[tool.transform];
FileTransformRcPtr filetransform = FileTransform::Create();
filetransform->setSrc(transform.filename.c_str());
filetransform->setInterpolation(INTERP_BEST);
ConstProcessorRcPtr processor = config->getProcessor(filetransform);
transformProcessor = processor->getDefaultCPUProcessor();
}
print_info("filename: ", tool.outputfilename);
print_info("format: ", typedesc);
if (is10bit) {
print_info(" 10bit: ", "yes");
}
// transform
if (!tool.transform.empty()) {
print_info("transform: ", tool.transform);
}
// image data
int width = tool.width;
int height = tool.height;
int channels = tool.channels;
int imagesize = width * height * channels;
if (tool.verbose) {
print_info(" width: ", width);
print_info(" height: ", height);
print_info(" channels: ", channels);
}
size_t typesize = typedesc.size();
size_t typelimit = pow(2, typesize*8) - 1;
size_t type10bitlimit = pow(2, 10) - 1;
if (tool.verbose) {
print_info(" typesize: ", typesize);
print_info(" typelimit: ", typelimit);
print_info(" type10bitlimit: ", type10bitlimit);
}
if (tool.outputtype == "stepchart") {
print_info("image: stepchart");
// signal
int signalsize = 17;
void* signaldata = (void*)malloc(typesize * signalsize);
memset(signaldata, 0, typesize * signalsize);
print_info("signal stops: ", signalsize);
for(int s=0; s<signalsize; s++) {
int relstop = s-8;
float lin = pow(2, relstop) * midgray;
float log = 0.0;
if (tool.outputlinear) {
log = lin;
} else {
log = std::min<float>(colorspace.lin2log(lin), typelimit);
}
if (tool.verbose) {
print_info(" stop: ", relstop);
print_info(" lin: ", lin);
print_info(" log: ", log);
}
if (tool.transform.size()) {
float rgb[3] = { log, log, log };
transformProcessor->applyRGB(rgb);
log = rgb[0];
if (tool.verbose) {
print_info(" lut: ", log);
}
}
if (typedesc.is_floating_point()) {
memcpy((char*)signaldata + typesize*s, &log, typesize);
if (relstop == 0) {
midlog = log;
}
if (tool.verbose) {
print_info(" value: ", log);
}
}
else {
int value = round(typelimit * log);
memcpy((char*)signaldata + typesize*s, &value, typesize);
if (relstop == 0) {
midlog = log;
}
if (tool.verbose) {
if (is10bit) {
print_info(" value: ", str_by_10bit(value));
} else {
print_info(" value: ", str_by_int(value));
}
}
}
}
// output image
{
void* imagedata = malloc(typesize * imagesize);
memset(imagedata, 0, typesize * imagesize);
// image algo
ImageSpec spec (width, height, channels, typedesc);
if (is10bit) {
spec.attribute("oiio:BitsPerSample", 10);
}
ImageBuf imageBuf = ImageBuf(spec, imagedata);
int stopwidth = floor(width / signalsize);
void* pixeldata = (void*)malloc(typesize);
std::map<int, std::pair<int, float>> stops;
for(int y=0; y<height; ++y) {
int stopcount=0;
for(int x=0; x<width; ++x) {
int stop = std::min<int>(signalsize - 1, stopcount);
if (((float)y / height) > 0.5) {
float relstop = (((float)x / width) * (signalsize - 1)) - 8;
float lin = pow(2, relstop) * midgray;
float log = 0.0;
if (tool.outputlinear) {
log = lin;
} else {
log = colorspace.lin2log(lin);;
}
if (tool.transform.size()) {
float rgb[3] = { log, log, log };
transformProcessor->applyRGB(rgb);
log = rgb[0];
}
if (typedesc.is_floating_point()) {
memcpy(pixeldata, &log, typesize);
} else {
int value = round(typelimit * log);
memcpy(pixeldata, &value, typesize);
}
}
else {
if (x % stopwidth == 0) {
int stop = std::min<int>(signalsize - 1, stopcount);
int relativestop = stop-8;
memcpy(pixeldata, (char*)signaldata + typesize*stop, typesize);
if (stops.find(relativestop) == stops.end()) {
if (typedesc.is_floating_point()) {
float log = 0.0;
memcpy(&log, pixeldata, typesize);
stops[relativestop] = std::pair<int, float>(stop * stopwidth + stopwidth/2, log);
} else {
int value = 0;
memcpy(&value, pixeldata, typesize);
stops[relativestop] = std::pair<int, float>(stop * stopwidth + stopwidth/2, value);
}
}
stopcount++;
}
}
for(int c=0; c<channels; c++) {
int offset = channels * (width * y + x);
memcpy((char*)imagedata + typesize*offset + typesize*c, pixeldata, typesize);
}
}
}
// labels
{
if (!tool.outputnolabels) {
float fillwidth = width * 0.4;
float fillheight = height * 0.2;
float fillcolor[3] = { midlog, midlog, midlog };
std::string font = "Roboto.ttf";
float fontsmall = height * 0.025;
float fontmedium = height * 0.04;
float fontlarge = height * 0.08;
float fontcolor[] = { 1, 1, 1, 1 };
float xbegin = (width - fillwidth) / 2.0;
float ybegin = (height - fillheight) / 2.0;
ImageBufAlgo::fill(imageBuf, fillcolor,
ROI(
xbegin, width - xbegin, ybegin, height - ybegin
)
);
for (std::pair<int, std::pair<int, float>> stop : stops) {
int x = stop.second.first;
float value = stop.second.second;
ImageBufAlgo::render_text(imageBuf,
x,
height * 0.04,
std::to_string(stop.first),
fontmedium,
font_path(font),
fontcolor,
ImageBufAlgo::TextAlignX::Center,
ImageBufAlgo::TextAlignY::Center
);
// code and signal
{
std::string code, signal;
if (typedesc.is_floating_point()) {
code = str_by_float(value);
signal = str_by_percent(value);
} else {
if (is10bit) {
code = str_by_10bit(value);
signal = str_by_percent((float)int_by_10bit(value) / type10bitlimit);
} else {
code = str_by_int(value);
signal = str_by_percent(value / typelimit);
}
}
ImageBufAlgo::render_text(
imageBuf,
x,
height * 0.04 + fontmedium,
code,
fontsmall,
font_path(font),
fontcolor,
ImageBufAlgo::TextAlignX::Center,
ImageBufAlgo::TextAlignY::Center
);
ImageBufAlgo::render_text(
imageBuf,
x,
height * 0.04 + fontmedium * 2,
signal,
fontsmall,
font_path(font),
fontcolor,
ImageBufAlgo::TextAlignX::Center,
ImageBufAlgo::TextAlignY::Center
);
}
}
ImageBufAlgo::render_text(imageBuf,
width / 2.0,
height / 2.0,
"LogC3 Ø:" +
str_by_float(midgray) +
" EI:" +
str_by_int(tool.ei),
fontlarge,
font_path(font),
fontcolor,
ImageBufAlgo::TextAlignX::Center,
ImageBufAlgo::TextAlignY::Center
);
std::string logctool =
"Logctool " +
datetime() +
" " +
Filesystem::filename(tool.outputfilename) +
" (" +
tool.dataformat +
" " +
std::to_string(spec.width) +
"x" +
std::to_string(spec.height) +
")";
if (!tool.transform.empty()) {
logctool += " - transform: " + tool.transform;
}
ImageBufAlgo::render_text(
imageBuf,
width * 0.02,
height - height * 0.04,
logctool,
fontsmall,
font_path(font),
fontcolor,
ImageBufAlgo::TextAlignX::Left,
ImageBufAlgo::TextAlignY::Center
);
ImageBufAlgo::render_text(
imageBuf,
width - (width * 0.02),
height - height * 0.04,
"stepchart",
fontsmall,
font_path(font),
fontcolor,
ImageBufAlgo::TextAlignX::Right,
ImageBufAlgo::TextAlignY::Center
);
if (tool.transform.size()) {
ImageBufAlgo::render_text(
imageBuf,
width / 2.0,
height / 2.0 + fontlarge,
"Transform: " + tool.transform,
fontmedium * 0.8,
font_path(font),
fontcolor,
ImageBufAlgo::TextAlignX::Center,
ImageBufAlgo::TextAlignY::Center
);
}
}
}
print_info("writing output file: ", tool.outputfilename);
if (!imageBuf.write(tool.outputfilename)) {
print_error("could not write file: ", imageBuf.geterror());
}
}
}
else if (tool.outputtype == "colorchecker") {
print_info("type: colorchecker");
// colorchecker
std::vector<ColorCheckerPatch> patches;
std::string jsonfile = resources_path("colorchecker.json");
std::ifstream json(jsonfile);
if (json.is_open()) {
ptree pt;
read_json(jsonfile, pt);
for (const std::pair<const ptree::key_type, ptree>& item : pt) {
int no = std::stoi(item.first);
const ptree& data = item.second;
ColorCheckerPatch patch;
patch.no = no;
patch.number = data.get<std::string>("number", "");
patch.sRGB_r = data.get<float>("sRGB.R", 0.0f);
patch.sRGB_g = data.get<float>("sRGB.G", 0.0f);
patch.sRGB_b = data.get<float>("sRGB.B", 0.0f);
patch.cieLabd50_l = data.get<float>("CIE L*a*b*.L*", 0.0f);
patch.cieLabd50_a = data.get<float>("CIE L*a*b*.a*", 0.0f);
patch.cieLabd50_b = data.get<float>("CIE L*a*b*.b*", 0.0f);
std::string munsell_hue = data.get<std::string>("Munsell Notation.Hue", "");
patch.munsell_hue = 0.0f;
if (!munsell_hue.empty()) {
size_t space_pos = munsell_hue.find(' ');
if (space_pos != std::string::npos) {
std::string hue_numeric = munsell_hue.substr(0, space_pos);
try {
patch.munsell_hue = std::stof(hue_numeric);
} catch (...) {
patch.munsell_hue = 0.0f;
}
}
}
patch.munsell_value = data.get<float>("Munsell Notation.Value", 0.0f);
patch.munsell_chroma = data.get<float>("Munsell Notation.Chroma", 0.0f);
patches.push_back(patch);
}
}
else {
print_error("could not open colorpatches file: ", jsonfile);
ap.abort();
return EXIT_FAILURE;
}
if (patches.size() != 24) {
print_error("could not match colorpatches 4 rows x 6 colums = 24, is now: ", patches.size());
ap.abort();
return EXIT_FAILURE;
}
// output image
{
void* imagedata = (void*)malloc(typesize * imagesize);
memset(imagedata, 0, typesize * imagesize);
// image algo
ImageSpec spec (width, height, channels, typedesc);
if (is10bit) {
spec.attribute("oiio:BitsPerSample", 10);
}
ImageBuf imageBuf = ImageBuf(spec, imagedata);
// colorchecker patches
{
float spacing = width * 0.02;
int colorswidth = width * 0.8;
int patchrows = 4;
int patchcols = 6;
int patchwidth = (colorswidth - (patchcols + 1) * spacing) / patchcols;
int patchheight = ((height - height * 0.05) - (patchrows + 1) * spacing) / patchrows;
for (int row = 0; row < patchrows; ++row) {
for (int col = 0; col < patchcols; ++col) {
int no = (row * patchcols) + col;
ColorCheckerPatch& patch = patches[no];
float d50_l = patch.cieLabd50_l;
float d50_a = patch.cieLabd50_a;
float d50_b = patch.cieLabd50_b;
Imath::Vec3<float> xyz =
ciexyzd65_from_D50(ciexyzd50_from_lab(Imath::Vec3<float>(d50_l, d50_a, d50_b)));
Imath::Vec3<float> awg = colorspace.ciexyz_awg(xyz);
Imath::Vec3<float> log;
if (tool.outputlinear) {
log = awg;
} else {
log = Imath::Vec3<float>(
colorspace.lin2log(awg.x), colorspace.lin2log(awg.y), colorspace.lin2log(awg.z)
);
}
if (tool.transform.size()) {
float rgb[3] = { log.x, log.y, log.z };
transformProcessor->applyRGB(rgb);
log.x = rgb[0];
log.y = rgb[1];
log.z = rgb[2];
}
void* pixeldata = malloc(typesize * channels);
if (typedesc.is_floating_point()) {
float pixel[3] = { log.x, log.y, log.z };
memcpy(pixeldata, pixel, typesize * channels);
} else {
float colors[3] = { log.x, log.y, log.z };
for (int i = 0; i < 3; ++i) {
int value = round(colors[i] * typelimit);
memcpy((char*)pixeldata + i * typesize, &value, typesize);
}
}
int xwidth = col * (patchwidth + spacing) + spacing;
int yheight = (col + 1) * (patchwidth + spacing);
for (int y = row * (patchheight + spacing) + spacing; y < (row + 1) * (patchheight + spacing); ++y) {
for (int x = col * (patchwidth + spacing) + spacing; x < (col + 1) * (patchwidth + spacing); ++x) {
int offset = (y * width + x) * channels;
for (int c = 0; c < channels; ++c) {
memcpy((char*)imagedata + typesize * (offset + c), (char*)pixeldata + typesize * c, typesize);
}
}
}
// labels
{
if (!tool.outputnolabels) {
std::string font = "Roboto.ttf";
float sizecode = height * 0.015;
float sizelabel = height * 0.025;
float fontcolor[] = { 1, 1, 1, 1 };
std::string code;
if (typedesc.is_floating_point()) {
code = str_by_float(log.x) + ", " + str_by_float(log.y) + ", " + str_by_float(log.z);
} else {
int x = round(typelimit * log.x);
int y = round(typelimit * log.y);
int z = round(typelimit * log.z);
if (is10bit) {
code = str_by_10bit(x) + ", " + str_by_10bit(y) + ", " + str_by_10bit(z);
} else {
code = str_by_int(x) + ", " + str_by_int(y) + ", " + str_by_int(z);
}
}
int x = col * (patchwidth + spacing) + spacing + (patchwidth / 2);
int y = row * (patchheight + spacing) + spacing;
ImageBufAlgo::render_text(
imageBuf,
x,
y + (patchheight * 0.75),
patch.number,
sizecode,
font_path(font),
fontcolor,
ImageBufAlgo::TextAlignX::Center,
ImageBufAlgo::TextAlignY::Center
);
ImageBufAlgo::render_text(
imageBuf,
x,
y + (patchheight * 0.9),
code,
sizelabel,
font_path(font),
fontcolor,
ImageBufAlgo::TextAlignX::Center,