Blender V5.3
logImageCore.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 1999-2001 David Hodson <hodsond@acm.org>.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "logImageCore.h"
12#include "cineonlib.h"
13#include "logmemfile.h"
14
15#include <cmath>
16#include <cstdlib>
17#include <cstring>
18
19#include "BLI_assert.hh"
20#include "BLI_memory_utils.hh"
21#include "BLI_utildefines.hh"
22
23#include "IMB_imbuf.hh"
24
25#include "MEM_guardedalloc.h"
26
27namespace blender {
28
29/*
30 * Declaration of static functions
31 */
32
33static int logImageSetData10(LogImageFile *logImage,
34 const LogImageElement &logElement,
35 const float *data);
36static int logImageElementGetData(LogImageFile *logImage,
37 const LogImageElement &logElement,
38 float *data);
39static int logImageElementGetData1(LogImageFile *logImage,
40 const LogImageElement &logElement,
41 float *data);
42static int logImageElementGetData8(LogImageFile *logImage,
43 const LogImageElement &logElement,
44 float *data);
45static int logImageElementGetData10(LogImageFile *logImage,
46 const LogImageElement &logElement,
47 float *data);
49 const LogImageElement &logElement,
50 float *data);
51static int logImageElementGetData12(LogImageFile *logImage,
52 const LogImageElement &logElement,
53 float *data);
55 const LogImageElement &logElement,
56 float *data);
57static int logImageElementGetData16(LogImageFile *logImage,
58 const LogImageElement &logElement,
59 float *data);
60static int convertLogElementToRGBA(const float *src,
61 float *dst,
62 const LogImageFile *logImage,
63 const LogImageElement &logElement,
64 int dstIsLinearRGB);
65static int convertRGBAToLogElement(const float *src,
66 float *dst,
67 const LogImageFile *logImage,
68 const LogImageElement &logElement,
69 int srcIsLinearRGB);
70
71/*
72 * For debug purpose
73 */
74
75static int verbose = 0;
76
77void logImageSetVerbose(int verbosity)
78{
79 verbose = verbosity;
80 cineonSetVerbose(verbosity);
81}
82
83/*
84 * IO stuff
85 */
86
87int logImageIsCineon(const void *buffer, const uint size)
88{
89 uint magicNum;
90 if (size < sizeof(magicNum)) {
91 return 0;
92 }
93 magicNum = *static_cast<uint *>(const_cast<void *>(buffer));
94 return (magicNum == CINEON_FILE_MAGIC || magicNum == swap_uint(CINEON_FILE_MAGIC, 1));
95}
96
98{
99 if (logImageIsCineon(buffer, size)) {
100 return cineonOpen(buffer, size);
101 }
102
103 return nullptr;
104}
105
106LogImageFile *logImageCreate(const char *filepath, int width, int height, const char *creator)
107{
108 return cineonCreate(filepath, width, height, creator);
109}
110
112{
113 if (logImage != nullptr) {
114 if (logImage->file) {
115 fclose(logImage->file);
116 logImage->file = nullptr;
117 }
118 MEM_delete(logImage);
119 }
120}
121
122void logImageGetSize(const LogImageFile *logImage, int *width, int *height, int *depth)
123{
124 *width = logImage->width;
125 *height = logImage->height;
126 *depth = logImage->depth;
127}
128
129/*
130 * Helper
131 */
132
133static size_t getRowLength(size_t width, const LogImageElement &logElement)
134{
135 /* return the row length in bytes according to width and packing method */
136 switch (logElement.bitsPerSample) {
137 case 1:
138 return ((width * logElement.depth - 1) / 32 + 1) * 4;
139
140 case 8:
141 return ((width * logElement.depth - 1) / 4 + 1) * 4;
142
143 case 10:
144 if (logElement.packing == 0) {
145 return ((width * logElement.depth * 10 - 1) / 32 + 1) * 4;
146 }
147 else if (ELEM(logElement.packing, 1, 2)) {
148 return ((width * logElement.depth - 1) / 3 + 1) * 4;
149 }
150 break;
151 case 12:
152 if (logElement.packing == 0) {
153 return ((width * logElement.depth * 12 - 1) / 32 + 1) * 4;
154 }
155 else if (ELEM(logElement.packing, 1, 2)) {
156 return width * logElement.depth * 2;
157 }
158 break;
159 case 16:
160 return width * logElement.depth * 2;
161 }
162 return 0;
163}
164
165size_t getRowLength(size_t width, const LogImageElement *logElement)
166{
167 /* For the C-API. */
168
169 return getRowLength(width, *logElement);
170}
171
172/*
173 * Data writing
174 */
175
176int logImageSetDataRGBA(LogImageFile *logImage, const float *data, int dataIsLinearRGB)
177{
178 float *elementData;
179 int returnValue;
180
181 elementData = static_cast<float *>(imb_alloc_pixels(
182 logImage->width, logImage->height, logImage->depth, sizeof(float), true, __func__));
183 if (elementData == nullptr) {
184 return 1;
185 }
186
188 data, elementData, logImage, logImage->element[0], dataIsLinearRGB) != 0)
189 {
190 MEM_delete(elementData);
191 return 1;
192 }
193
194 /* Writing only supports 10 bits. */
195 returnValue = logImageSetData10(logImage, logImage->element[0], elementData);
196
197 MEM_delete(elementData);
198 return returnValue;
199}
200
201static int logImageSetData10(LogImageFile *logImage,
202 const LogImageElement &logElement,
203 const float *data)
204{
205 size_t rowLength = getRowLength(logImage->width, logElement);
206 uint pixel, index;
207 uint *row;
208
209 row = static_cast<uint *>(MEM_new_uninitialized(rowLength, __func__));
210 if (row == nullptr) {
211 if (verbose) {
212 printf("Cineon: Cannot allocate row.\n");
213 }
214 return 1;
215 }
216
217 for (size_t y = 0; y < logImage->height; y++) {
218 int offset = 22;
219 index = 0;
220 pixel = 0;
221
222 for (size_t x = 0; x < logImage->width * logImage->depth; x++) {
223 pixel |= uint(float_uint(data[y * logImage->width * logImage->depth + x], 1023)) << offset;
224 offset -= 10;
225 if (offset < 0) {
226 row[index] = swap_uint(pixel, logImage->isMSB);
227 index++;
228 pixel = 0;
229 offset = 22;
230 }
231 }
232 if (pixel != 0) {
233 row[index] = swap_uint(pixel, logImage->isMSB);
234 }
235
236 if (logimage_fwrite(row, rowLength, 1, logImage) == 0) {
237 if (verbose) {
238 printf("Cineon: Error while writing file.\n");
239 }
240 MEM_delete(row);
241 return 1;
242 }
243 }
244 MEM_delete(row);
245 return 0;
246}
247
248/*
249 * Data reading
250 */
251
252int logImageGetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB)
253{
254 /* Fills data with 32 bits float RGBA values. The image is either a single RGB or
255 * luminance element, or separate R, G and B elements. */
256 BLI_assert(ELEM(logImage->numElements, 1, 3));
257
258 float *elementData[3] = {};
259 BLI_SCOPED_DEFER([&]() {
260 for (float *element_data : elementData) {
261 MEM_delete(element_data);
262 }
263 });
264
265 for (int i = 0; i < logImage->numElements; i++) {
266 elementData[i] = static_cast<float *>(imb_alloc_pixels(logImage->width,
267 logImage->height,
268 logImage->element[i].depth,
269 sizeof(float),
270 true,
271 __func__));
272 if (elementData[i] == nullptr) {
273 if (verbose) {
274 printf("Cineon: Cannot allocate memory for elementData[%d]\n.", i);
275 }
276 return 1;
277 }
278
279 if (logImageElementGetData(logImage, logImage->element[i], elementData[i]) != 0) {
280 if (verbose) {
281 printf("Cineon: Cannot read elementData[%d]\n.", i);
282 }
283 return 1;
284 }
285 }
286
287 if (logImage->numElements == 1) {
289 elementData[0], data, logImage, logImage->element[0], dataIsLinearRGB);
290 }
291
292 /* Interleave separate R, G and B elements into a single RGB element. */
293 LogImageElement mergedElement = logImage->element[0];
294 mergedElement.descriptor = descriptor_RGB;
295 mergedElement.depth = 3;
296
297 float *mergedData = static_cast<float *>(
298 imb_alloc_pixels(logImage->width, logImage->height, 3, sizeof(float), true, __func__));
299 if (mergedData == nullptr) {
300 if (verbose) {
301 printf("Cineon: Cannot allocate mergedData.\n");
302 }
303 return 1;
304 }
305
306 const size_t numPixels = size_t(logImage->width) * size_t(logImage->height);
307 for (size_t i = 0; i < numPixels; i++) {
308 for (int c = 0; c < 3; c++) {
309 mergedData[i * 3 + c] = elementData[c][i];
310 }
311 }
312
313 const int returnValue = convertLogElementToRGBA(
314 mergedData, data, logImage, mergedElement, dataIsLinearRGB);
315 MEM_delete(mergedData);
316 return returnValue;
317}
318
320 const LogImageElement &logElement,
321 float *data)
322{
323 switch (logElement.bitsPerSample) {
324 case 1:
325 return logImageElementGetData1(logImage, logElement, data);
326
327 case 8:
328 return logImageElementGetData8(logImage, logElement, data);
329
330 case 10:
331 if (logElement.packing == 0) {
332 return logImageElementGetData10Packed(logImage, logElement, data);
333 }
334 else if (ELEM(logElement.packing, 1, 2)) {
335 return logImageElementGetData10(logImage, logElement, data);
336 }
337 break;
338
339 case 12:
340 if (logElement.packing == 0) {
341 return logImageElementGetData12Packed(logImage, logElement, data);
342 }
343 else if (ELEM(logElement.packing, 1, 2)) {
344 return logImageElementGetData12(logImage, logElement, data);
345 }
346 break;
347
348 case 16:
349 return logImageElementGetData16(logImage, logElement, data);
350 }
351 /* format not supported */
352 return 1;
353}
354
356 const LogImageElement &logElement,
357 float *data)
358{
359 uint pixel;
360
361 /* seek at the right place */
362 if (logimage_fseek(logImage, logElement.dataOffset) != 0) {
363 if (verbose) {
364 printf("Cineon: Couldn't seek at %d\n", logElement.dataOffset);
365 }
366 return 1;
367 }
368
369 /* read 1 bit data padded to 32 bits */
370 for (size_t y = 0; y < logImage->height; y++) {
371 for (size_t x = 0; x < logImage->width * logElement.depth; x += 32) {
372 if (logimage_read_uint(&pixel, logImage) != 0) {
373 if (verbose) {
374 printf("Cineon: EOF reached\n");
375 }
376 return 1;
377 }
378 pixel = swap_uint(pixel, logImage->isMSB);
379 for (int offset = 0; offset < 32 && x + offset < logImage->width; offset++) {
380 data[y * logImage->width * logElement.depth + x + offset] = float((pixel >> offset) &
381 0x01);
382 }
383 }
384 }
385 return 0;
386}
387
389 const LogImageElement &logElement,
390 float *data)
391{
392 size_t rowLength = getRowLength(logImage->width, logElement);
393 uchar pixel;
394
395 /* extract required pixels */
396 for (size_t y = 0; y < logImage->height; y++) {
397 /* 8 bits are 32-bits padded so we need to seek at each row */
398 if (logimage_fseek(logImage, logElement.dataOffset + y * rowLength) != 0) {
399 if (verbose) {
400 printf("Cineon: Couldn't seek at %d\n", int(logElement.dataOffset + y * rowLength));
401 }
402 return 1;
403 }
404
405 for (size_t x = 0; x < logImage->width * logElement.depth; x++) {
406 if (logimage_read_uchar(&pixel, logImage) != 0) {
407 if (verbose) {
408 printf("Cineon: EOF reached\n");
409 }
410 return 1;
411 }
412 data[y * logImage->width * logElement.depth + x] = float(pixel) / 255.0f;
413 }
414 }
415 return 0;
416}
417
419 const LogImageElement &logElement,
420 float *data)
421{
422 uint pixel;
423
424 /* seek to data */
425 if (logimage_fseek(logImage, logElement.dataOffset) != 0) {
426 if (verbose) {
427 printf("Cineon: Couldn't seek at %d\n", logElement.dataOffset);
428 }
429 return 1;
430 }
431
432 for (size_t y = 0; y < logImage->height; y++) {
433 int offset = -1;
434 for (size_t x = 0; x < logImage->width * logElement.depth; x++) {
435 /* we need to read the next long */
436 if (offset < 0) {
437 if (logElement.packing == 1) {
438 offset = 22;
439 }
440 else if (logElement.packing == 2) {
441 offset = 20;
442 }
443
444 if (logimage_read_uint(&pixel, logImage) != 0) {
445 if (verbose) {
446 printf("Cineon: EOF reached\n");
447 }
448 return 1;
449 }
450 pixel = swap_uint(pixel, logImage->isMSB);
451 }
452 data[y * logImage->width * logElement.depth + x] = float((pixel >> offset) & 0x3ff) /
453 1023.0f;
454 offset -= 10;
455 }
456 }
457
458 return 0;
459}
460
462 const LogImageElement &logElement,
463 float *data)
464{
465 size_t rowLength = getRowLength(logImage->width, logElement);
466 uint pixel, oldPixel;
467
468 /* converting bytes to pixels */
469 for (size_t y = 0; y < logImage->height; y++) {
470 /* seek to data */
471 if (logimage_fseek(logImage, y * rowLength + logElement.dataOffset) != 0) {
472 if (verbose) {
473 printf("Cineon: Couldn't seek at %u\n", uint(y * rowLength + logElement.dataOffset));
474 }
475 return 1;
476 }
477
478 oldPixel = 0;
479 int offset = 0;
480 int offset2 = 0;
481
482 for (size_t x = 0; x < logImage->width * logElement.depth; x++) {
483 if (offset2 != 0) {
484 offset = 10 - offset2;
485 offset2 = 0;
486 oldPixel = 0;
487 }
488 else if (offset == 32) {
489 offset = 0;
490 }
491 else if (offset + 10 > 32) {
492 /* next pixel is on two different longs */
493 oldPixel = (pixel >> offset);
494 offset2 = 32 - offset;
495 offset = 0;
496 }
497
498 if (offset == 0) {
499 /* we need to read the next long */
500 if (logimage_read_uint(&pixel, logImage) != 0) {
501 if (verbose) {
502 printf("Cineon: EOF reached\n");
503 }
504 return 1;
505 }
506 pixel = swap_uint(pixel, logImage->isMSB);
507 }
508 data[y * logImage->width * logElement.depth + x] =
509 float((((pixel << offset2) >> offset) & 0x3ff) | oldPixel) / 1023.0f;
510 offset += 10;
511 }
512 }
513 return 0;
514}
515
517 const LogImageElement &logElement,
518 float *data)
519{
520 uint sampleIndex;
521 uint numSamples = logImage->width * logImage->height * logElement.depth;
522 ushort pixel;
523
524 /* seek to data */
525 if (logimage_fseek(logImage, logElement.dataOffset) != 0) {
526 if (verbose) {
527 printf("Cineon: Couldn't seek at %d\n", logElement.dataOffset);
528 }
529 return 1;
530 }
531
532 /* convert bytes to pixels */
533 sampleIndex = 0;
534
535 for (sampleIndex = 0; sampleIndex < numSamples; sampleIndex++) {
536 if (logimage_read_ushort(&pixel, logImage) != 0) {
537 if (verbose) {
538 printf("Cineon: EOF reached\n");
539 }
540 return 1;
541 }
542 pixel = swap_ushort(pixel, logImage->isMSB);
543
544 if (logElement.packing == 1) { /* padded to the right */
545 data[sampleIndex] = float(pixel >> 4) / 4095.0f;
546 }
547 else if (logElement.packing == 2) { /* padded to the left */
548 data[sampleIndex] = float(pixel) / 4095.0f;
549 }
550 }
551 return 0;
552}
553
555 const LogImageElement &logElement,
556 float *data)
557{
558 size_t rowLength = getRowLength(logImage->width, logElement);
559 uint pixel, oldPixel;
560
561 /* converting bytes to pixels */
562 for (size_t y = 0; y < logImage->height; y++) {
563 /* seek to data */
564 if (logimage_fseek(logImage, y * rowLength + logElement.dataOffset) != 0) {
565 if (verbose) {
566 printf("Cineon: Couldn't seek at %u\n", uint(y * rowLength + logElement.dataOffset));
567 }
568 return 1;
569 }
570
571 oldPixel = 0;
572 int offset = 0;
573 int offset2 = 0;
574
575 for (size_t x = 0; x < logImage->width * logElement.depth; x++) {
576 if (offset2 != 0) {
577 offset = 12 - offset2;
578 offset2 = 0;
579 oldPixel = 0;
580 }
581 else if (offset == 32) {
582 offset = 0;
583 }
584 else if (offset + 12 > 32) {
585 /* next pixel is on two different longs */
586 oldPixel = (pixel >> offset);
587 offset2 = 32 - offset;
588 offset = 0;
589 }
590
591 if (offset == 0) {
592 /* we need to read the next long */
593 if (logimage_read_uint(&pixel, logImage) != 0) {
594 if (verbose) {
595 printf("Cineon: EOF reached\n");
596 }
597 return 1;
598 }
599 pixel = swap_uint(pixel, logImage->isMSB);
600 }
601 data[y * logImage->width * logElement.depth + x] =
602 float((((pixel << offset2) >> offset) & 0xfff) | oldPixel) / 4095.0f;
603 offset += 12;
604 }
605 }
606 return 0;
607}
608
610 const LogImageElement &logElement,
611 float *data)
612{
613 uint numSamples = logImage->width * logImage->height * logElement.depth;
614 uint sampleIndex;
615 ushort pixel;
616
617 /* seek to data */
618 if (logimage_fseek(logImage, logElement.dataOffset) != 0) {
619 if (verbose) {
620 printf("Cineon: Couldn't seek at %d\n", logElement.dataOffset);
621 }
622 return 1;
623 }
624
625 for (sampleIndex = 0; sampleIndex < numSamples; sampleIndex++) {
626 if (logimage_read_ushort(&pixel, logImage) != 0) {
627 if (verbose) {
628 printf("Cineon: EOF reached\n");
629 }
630 return 1;
631 }
632 pixel = swap_ushort(pixel, logImage->isMSB);
633 data[sampleIndex] = float(pixel) / 65535.0f;
634 }
635
636 return 0;
637}
638
639/*
640 * Color conversion
641 */
642
643static float *getLinToLogLut(const LogImageFile *logImage, const LogImageElement &logElement)
644{
645 float *lut;
646 float gain, negativeFilmGamma, offset, step;
647 uint lutsize = uint(logElement.maxValue + 1);
648 uint i;
649
650 lut = MEM_new_array_uninitialized<float>(lutsize, "getLinToLogLut");
651
652 negativeFilmGamma = 0.6;
653 step = logElement.refHighQuantity / logElement.maxValue;
654 gain = logElement.maxValue /
655 (1.0f - powf(10,
656 (logImage->referenceBlack - logImage->referenceWhite) * step /
657 negativeFilmGamma * logImage->gamma / 1.7f));
658 offset = gain - logElement.maxValue;
659
660 for (i = 0; i < lutsize; i++) {
661 lut[i] = (logImage->referenceWhite +
662 log10f(powf((i + offset) / gain, 1.7f / logImage->gamma)) /
663 (step / negativeFilmGamma)) /
664 logElement.maxValue;
665 }
666
667 return lut;
668}
669
670static float *getLogToLinLut(const LogImageFile *logImage, const LogImageElement &logElement)
671{
672 float *lut;
673 float breakPoint, gain, kneeGain, kneeOffset, negativeFilmGamma, offset, step, softClip;
674 /* float filmGamma; unused */
675 uint lutsize = uint(logElement.maxValue + 1);
676 uint i;
677
678 lut = MEM_new_array_uninitialized<float>(lutsize, "getLogToLinLut");
679
680 /* Building the Log -> Lin LUT */
681 step = logElement.refHighQuantity / logElement.maxValue;
682 negativeFilmGamma = 0.6;
683
684 /* these are default values */
685 /* filmGamma = 2.2f; unused */
686 softClip = 0;
687
688 breakPoint = logImage->referenceWhite - softClip;
689 gain = logElement.maxValue /
690 (1.0f - powf(10,
691 (logImage->referenceBlack - logImage->referenceWhite) * step /
692 negativeFilmGamma * logImage->gamma / 1.7f));
693 offset = gain - logElement.maxValue;
694 kneeOffset = powf(10,
695 (breakPoint - logImage->referenceWhite) * step / negativeFilmGamma *
696 logImage->gamma / 1.7f) *
697 gain -
698 offset;
699 kneeGain = (logElement.maxValue - kneeOffset) / powf(5 * softClip, softClip / 100);
700
701 for (i = 0; i < lutsize; i++) {
702 if (i < logImage->referenceBlack) {
703 lut[i] = 0.0f;
704 }
705 else if (i > breakPoint) {
706 lut[i] = (powf(i - breakPoint, softClip / 100) * kneeGain + kneeOffset) /
707 logElement.maxValue;
708 }
709 else {
710 lut[i] = (powf(10,
711 (float(i) - logImage->referenceWhite) * step / negativeFilmGamma *
712 logImage->gamma / 1.7f) *
713 gain -
714 offset) /
715 logElement.maxValue;
716 }
717 }
718
719 return lut;
720}
721
722static float *getLinToSrgbLut(const LogImageElement &logElement)
723{
724 float col, *lut;
725 uint lutsize = uint(logElement.maxValue + 1);
726 uint i;
727
728 lut = MEM_new_array_uninitialized<float>(lutsize, "getLogToLinLut");
729
730 for (i = 0; i < lutsize; i++) {
731 col = float(i) / logElement.maxValue;
732 if (col < 0.0031308f) {
733 lut[i] = (col < 0.0f) ? 0.0f : col * 12.92f;
734 }
735 else {
736 lut[i] = 1.055f * powf(col, 1.0f / 2.4f) - 0.055f;
737 }
738 }
739
740 return lut;
741}
742
743static float *getSrgbToLinLut(const LogImageElement &logElement)
744{
745 float col, *lut;
746 uint lutsize = uint(logElement.maxValue + 1);
747 uint i;
748
749 lut = MEM_new_array_uninitialized<float>(lutsize, "getLogToLinLut");
750
751 for (i = 0; i < lutsize; i++) {
752 col = float(i) / logElement.maxValue;
753 if (col < 0.04045f) {
754 lut[i] = (col < 0.0f) ? 0.0f : col * (1.0f / 12.92f);
755 }
756 else {
757 lut[i] = powf((col + 0.055f) * (1.0f / 1.055f), 2.4f);
758 }
759 }
760
761 return lut;
762}
763
764/* RGB elements always use the printing density transfer. */
765static void convertRGBA_RGB(const float *src,
766 float *dst,
767 const LogImageFile *logImage,
768 const LogImageElement &logElement)
769{
770 uint i;
771 const float *src_ptr = src;
772 float *dst_ptr = dst;
773 float *lut = getLinToLogLut(logImage, logElement);
774
775 for (i = 0; i < logImage->width * logImage->height; i++) {
776 *(dst_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
777 *(dst_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
778 *(dst_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
779 src_ptr++;
780 }
781
782 MEM_delete(lut);
783}
784
785static void convertRGB_RGBA(const float *src,
786 float *dst,
787 const LogImageFile *logImage,
788 const LogImageElement &logElement)
789{
790 uint i;
791 const float *src_ptr = src;
792 float *dst_ptr = dst;
793 float *lut = getLogToLinLut(logImage, logElement);
794
795 for (i = 0; i < logImage->width * logImage->height; i++) {
796 *(dst_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
797 *(dst_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
798 *(dst_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
799 *(dst_ptr++) = 1.0f;
800 }
801
802 MEM_delete(lut);
803}
804
805/* Luminance elements always use the linear transfer. */
806static void convertLuminance_RGBA(const float *src,
807 float *dst,
808 const LogImageFile *logImage,
809 const LogImageElement &logElement)
810{
811 uint i;
812 float value;
813 const float *src_ptr = src;
814 float *dst_ptr = dst;
815
816 const float refHighData = float(logElement.refHighData) / logElement.maxValue;
817 const float refLowData = float(logElement.refLowData) / logElement.maxValue;
818 const float scale = 1.0f / (refHighData - refLowData);
819
820 for (i = 0; i < logImage->width * logImage->height; i++) {
821 value = clamp_float((*(src_ptr++) - refLowData) * scale, 0.0f, 1.0f);
822 *(dst_ptr++) = value;
823 *(dst_ptr++) = value;
824 *(dst_ptr++) = value;
825 *(dst_ptr++) = 1.0f;
826 }
827}
828
829static int convertLogElementToRGBA(const float *src,
830 float *dst,
831 const LogImageFile *logImage,
832 const LogImageElement &logElement,
833 int dstIsLinearRGB)
834{
835 uint i;
836 float *src_ptr;
837 float *dst_ptr;
838
839 /* Convert data in src to linear RGBA in dst */
840 switch (logElement.descriptor) {
841 case descriptor_RGB:
842 convertRGB_RGBA(src, dst, logImage, logElement);
843 break;
844
846 convertLuminance_RGBA(src, dst, logImage, logElement);
847 break;
848
849 default:
850 return 1;
851 }
852
853 if (dstIsLinearRGB) {
854 /* convert data from sRGB to Linear RGB via lut */
855 float *lut = getSrgbToLinLut(logElement);
856 src_ptr = dst; /* no error here */
857 dst_ptr = dst;
858 for (i = 0; i < logImage->width * logImage->height; i++) {
859 *(dst_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
860 *(dst_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
861 *(dst_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
862 dst_ptr++;
863 src_ptr++;
864 }
865 MEM_delete(lut);
866 }
867 return 0;
868}
869
870static int convertRGBAToLogElement(const float *src,
871 float *dst,
872 const LogImageFile *logImage,
873 const LogImageElement &logElement,
874 int srcIsLinearRGB)
875{
876 uint i;
877 const float *srgbSrc;
878 float *srgbSrc_alloc;
879 float *srgbSrc_ptr;
880 const float *src_ptr = src;
881 float *lut;
882
883 if (srcIsLinearRGB != 0) {
884 /* we need to convert src to sRGB */
885 srgbSrc_alloc = static_cast<float *>(
886 imb_alloc_pixels(logImage->width, logImage->height, 4, sizeof(float), false, __func__));
887 if (srgbSrc_alloc == nullptr) {
888 return 1;
889 }
890
891 memcpy(srgbSrc_alloc,
892 src,
893 4 * size_t(logImage->width) * size_t(logImage->height) * sizeof(float));
894 srgbSrc_ptr = srgbSrc_alloc;
895
896 /* convert data from Linear RGB to sRGB via lut */
897 lut = getLinToSrgbLut(logElement);
898 for (i = 0; i < logImage->width * logImage->height; i++) {
899 *(srgbSrc_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
900 *(srgbSrc_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
901 *(srgbSrc_ptr++) = lut[float_uint(*(src_ptr++), logElement.maxValue)];
902 srgbSrc_ptr++;
903 src_ptr++;
904 }
905 MEM_delete(lut);
906 srgbSrc = srgbSrc_alloc;
907 }
908 else {
909 srgbSrc = src;
910 }
911
912 /* Convert linear RGBA data in src to the RGB element in dst. */
913 BLI_assert(logElement.descriptor == descriptor_RGB);
914 convertRGBA_RGB(srgbSrc, dst, logImage, logElement);
915
916 if (srcIsLinearRGB != 0) {
917 MEM_delete(srgbSrc_alloc);
918 }
919
920 return 0;
921}
922
923} // namespace blender
#define BLI_SCOPED_DEFER(function_to_defer)
Read Blender memory allocation and freeing.
#define ELEM(...)
#define CINEON_FILE_MAGIC
Definition cineonlib.h:17
#define powf(x, y)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
BMesh Iterators.
static float * getSrgbToLinLut(const LogImageElement &logElement)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t getRowLength(size_t width, const LogImageElement *logElement)
unsigned int uint
Definition BKE_text.h:55
int logimage_fseek(LogImageFile *logFile, uintptr_t offset)
Definition logmemfile.cc:20
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
BLI_INLINE float clamp_float(float x, float low, float high)
void logImageClose(LogImageFile *logImage)
unsigned short ushort
Definition BKE_text.h:56
int logImageIsCineon(const void *buffer, const uint size)
static float * getLogToLinLut(const LogImageFile *logImage, const LogImageElement &logElement)
void logImageGetSize(const LogImageFile *logImage, int *width, int *height, int *depth)
BLI_INLINE unsigned int float_uint(float value, unsigned int max)
static float * getLinToSrgbLut(const LogImageElement &logElement)
void logImageSetVerbose(int verbosity)
void * imb_alloc_pixels(unsigned int x, unsigned int y, unsigned int channels, size_t typesize, bool initialize_pixels, const char *alloc_name)
int logimage_read_ushort(ushort *x, LogImageFile *logFile)
Definition logmemfile.cc:65
BLI_INLINE unsigned int swap_uint(unsigned int x, int swap)
LogImageFile * logImageOpenFromMemory(const uchar *buffer, uint size)
static int logImageElementGetData10Packed(LogImageFile *logImage, const LogImageElement &logElement, float *data)
static int logImageElementGetData1(LogImageFile *logImage, const LogImageElement &logElement, float *data)
static int logImageElementGetData10(LogImageFile *logImage, const LogImageElement &logElement, float *data)
void cineonSetVerbose(int verbosity)
Definition cineonlib.cc:34
static int logImageElementGetData(LogImageFile *logImage, const LogImageElement &logElement, float *data)
unsigned char uchar
Definition BKE_text.h:58
static int logImageElementGetData12Packed(LogImageFile *logImage, const LogImageElement &logElement, float *data)
BMesh const char void * data
LogImageFile * cineonOpen(const uchar *buffer, size_t bufferSize)
Definition cineonlib.cc:126
LogImageFile * logImageCreate(const char *filepath, int width, int height, const char *creator)
int logImageSetDataRGBA(LogImageFile *logImage, const float *data, int dataIsLinearRGB)
static int convertLogElementToRGBA(const float *src, float *dst, const LogImageFile *logImage, const LogImageElement &logElement, int dstIsLinearRGB)
@ descriptor_RGB
@ descriptor_Luminance
static int logImageSetData10(LogImageFile *logImage, const LogImageElement &logElement, const float *data)
static int convertRGBAToLogElement(const float *src, float *dst, const LogImageFile *logImage, const LogImageElement &logElement, int srcIsLinearRGB)
int logimage_read_uint(uint *x, LogImageFile *logFile)
Definition logmemfile.cc:77
static int logImageElementGetData8(LogImageFile *logImage, const LogImageElement &logElement, float *data)
static int verbose
int logimage_read_uchar(uchar *x, LogImageFile *logFile)
Definition logmemfile.cc:53
static int logImageElementGetData16(LogImageFile *logImage, const LogImageElement &logElement, float *data)
static float * getLinToLogLut(const LogImageFile *logImage, const LogImageElement &logElement)
int logImageGetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB)
static void convertLuminance_RGBA(const float *src, float *dst, const LogImageFile *logImage, const LogImageElement &logElement)
int logimage_fwrite(const void *buffer, size_t size, uint count, LogImageFile *logFile)
Definition logmemfile.cc:29
static void convertRGBA_RGB(const float *src, float *dst, const LogImageFile *logImage, const LogImageElement &logElement)
BLI_INLINE unsigned short swap_ushort(unsigned short x, int swap)
static int logImageElementGetData12(LogImageFile *logImage, const LogImageElement &logElement, float *data)
LogImageFile * cineonCreate(const char *filepath, int width, int height, const char *creator)
Definition cineonlib.cc:334
static void convertRGB_RGBA(const float *src, float *dst, const LogImageFile *logImage, const LogImageElement &logElement)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
LogImageElement element[8]