Blender V5.3
colortools.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cfloat>
11#include <cmath>
12#include <cstdlib>
13#include <cstring>
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_color_types.h"
18#include "DNA_curve_types.h"
19
20#include "BLI_math_base.hh"
21#include "BLI_math_color_c.hh"
23#include "BLI_math_vector.hh"
25#include "BLI_rect.hh"
26#include "BLI_string_utf8.hh"
27#include "BLI_task_c.hh"
28#include "BLI_utildefines.hh"
29
30#include "BKE_colortools.hh"
31#include "BKE_curve.hh"
32#include "BKE_fcurve.hh"
33
35#include "IMB_imbuf_types.hh"
36
37#include "OCIO_scope.hh"
38
39#include "BLO_read_write.hh"
40
41namespace blender {
42
43/* ********************************* color curve ********************* */
44
45/* ***************** operations on full struct ************* */
46
48 int tot,
49 float minx,
50 float miny,
51 float maxx,
52 float maxy,
53 eBezTriple_Handle default_handle_type)
54{
55 int a;
56 float clipminx, clipminy, clipmaxx, clipmaxy;
57
59 if (tot == 4) {
60 cumap->cur = 3; /* rhms, hack for 'col' curve? */
61 }
62
63 clipminx = min_ff(minx, maxx);
64 clipminy = min_ff(miny, maxy);
65 clipmaxx = max_ff(minx, maxx);
66 clipmaxy = max_ff(miny, maxy);
67
68 BLI_rctf_init(&cumap->curr, clipminx, clipmaxx, clipminy, clipmaxy);
69 cumap->clipr = cumap->curr;
70
71 cumap->white[0] = cumap->white[1] = cumap->white[2] = 1.0f;
72 cumap->bwmul[0] = cumap->bwmul[1] = cumap->bwmul[2] = 1.0f;
73
74 for (a = 0; a < tot; a++) {
75 if (default_handle_type == HD_VECT) {
77 }
78 else if (default_handle_type == HD_AUTO_ANIM) {
80 }
81 else {
82 cumap->cm[a].default_handle_type = {};
83 }
84
85 cumap->cm[a].totpoint = 2;
86 cumap->cm[a].curve = MEM_new_array<CurveMapPoint>(2, "curve points");
87
88 cumap->cm[a].curve[0].x = minx;
89 cumap->cm[a].curve[0].y = miny;
90 cumap->cm[a].curve[0].flag |= cumap->cm[a].default_handle_type;
91 cumap->cm[a].curve[1].x = maxx;
92 cumap->cm[a].curve[1].y = maxy;
93 cumap->cm[a].curve[1].flag |= cumap->cm[a].default_handle_type;
94 }
95
96 cumap->changed_timestamp = 0;
97}
98
99CurveMapping *BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
100{
101 CurveMapping *cumap;
102
103 cumap = MEM_new<CurveMapping>("new curvemap");
104
105 BKE_curvemapping_set_defaults(cumap, tot, minx, miny, maxx, maxy, HD_AUTO);
106
107 return cumap;
108}
109
111{
112 if (cumap->cm[index].curve) {
113 MEM_delete(cumap->cm[index].curve);
114 cumap->cm[index].curve = nullptr;
115 }
116 if (cumap->cm[index].table) {
117 MEM_delete(cumap->cm[index].table);
118 cumap->cm[index].table = nullptr;
119 }
120 if (cumap->cm[index].premultable) {
121 MEM_delete(cumap->cm[index].premultable);
122 cumap->cm[index].premultable = nullptr;
123 }
124}
125
127{
128 int a;
129
130 for (a = 0; a < CM_TOT; a++) {
132 }
133}
134
136{
137 if (cumap) {
139 MEM_delete(cumap);
140 }
141}
142
144 CurveMapping *target, const CurveMapping *cumap, int to_idx, int from_idx, bool make_copy)
145{
146 if (make_copy) {
147 target->cm[to_idx] = dna::shallow_copy(cumap->cm[from_idx]);
148 }
149
150 if (cumap->cm[from_idx].curve) {
151 target->cm[to_idx].curve = static_cast<CurveMapPoint *>(
152 MEM_dupalloc(cumap->cm[from_idx].curve));
153 }
154 if (cumap->cm[from_idx].table) {
155 target->cm[to_idx].table = static_cast<CurveMapPoint *>(
156 MEM_dupalloc(cumap->cm[from_idx].table));
157 }
158 if (cumap->cm[from_idx].premultable) {
159 target->cm[to_idx].premultable = static_cast<CurveMapPoint *>(
160 MEM_dupalloc(cumap->cm[from_idx].premultable));
161 }
162}
163
165{
166 int a;
167
168 *target = dna::shallow_copy(*cumap);
169
170 for (a = 0; a < CM_TOT; a++) {
171 BKE_curvemapping_copy_data_single(target, cumap, a, a, false);
172 }
173}
174
176{
177 if (cumap) {
178 CurveMapping *cumapn = MEM_dupalloc(cumap);
179 BKE_curvemapping_copy_data(cumapn, cumap);
180 return cumapn;
181 }
182 return nullptr;
183}
184
185void BKE_curvemapping_set_black_white_ex(const float black[3],
186 const float white[3],
187 float r_bwmul[3])
188{
189 int a;
190
191 for (a = 0; a < 3; a++) {
192 const float delta = max_ff(white[a] - black[a], 1e-5f);
193 r_bwmul[a] = 1.0f / delta;
194 }
195}
196
198 const float black[3],
199 const float white[3])
200{
201 if (white) {
202 copy_v3_v3(cumap->white, white);
203 }
204 if (black) {
205 copy_v3_v3(cumap->black, black);
206 }
207
209 cumap->changed_timestamp++;
210}
211
212/* ***************** operations on single curve ************* */
213/* ********** NOTE: requires BKE_curvemapping_changed() call after ******** */
214
216{
217 CurveMapPoint *cmp;
218 int a, b, removed = 0;
219
220 /* must have 2 points minimum */
221 if (cuma->totpoint <= 2) {
222 return false;
223 }
224
225 cmp = MEM_new_array<CurveMapPoint>(size_t(cuma->totpoint), "curve points");
226
227 /* well, lets keep the two outer points! */
228 for (a = 0, b = 0; a < cuma->totpoint; a++) {
229 if (&cuma->curve[a] != point) {
230 cmp[b] = cuma->curve[a];
231 b++;
232 }
233 else {
234 removed++;
235 }
236 }
237
238 MEM_delete(cuma->curve);
239 cuma->curve = cmp;
240 cuma->totpoint -= removed;
241 return (removed != 0);
242}
243
244void BKE_curvemap_remove(CurveMap *cuma, const short flag)
245{
246 CurveMapPoint *cmp = MEM_new_array<CurveMapPoint>(size_t(cuma->totpoint), "curve points");
247 int a, b, removed = 0;
248
249 /* well, lets keep the two outer points! */
250 cmp[0] = cuma->curve[0];
251 for (a = 1, b = 1; a < cuma->totpoint - 1; a++) {
252 if (!(cuma->curve[a].flag & flag)) {
253 cmp[b] = cuma->curve[a];
254 b++;
255 }
256 else {
257 removed++;
258 }
259 }
260 cmp[b] = cuma->curve[a];
261
262 MEM_delete(cuma->curve);
263 cuma->curve = cmp;
264 cuma->totpoint -= removed;
265}
266
268{
269 cuma->totpoint++;
270 CurveMapPoint *new_path = MEM_new_array<CurveMapPoint>(size_t(cuma->totpoint), "curve points");
271 CurveMapPoint *new_pt = nullptr;
272
273 /* Insert fragments of the old one and the new point to the new curve. */
274 bool foundloc = false;
275 for (int i_new = 0, i_old = 0; i_new < cuma->totpoint; i_new++) {
276 if ((foundloc == false) && ((i_new + 1 == cuma->totpoint) || (x < cuma->curve[i_new].x))) {
277 new_path[i_new].x = x;
278 new_path[i_new].y = y;
279 new_path[i_new].flag = CUMA_SELECT;
280 new_path[i_new].flag |= cuma->default_handle_type;
281 foundloc = true;
282 new_pt = &new_path[i_new];
283 }
284 else {
285 new_path[i_new].x = cuma->curve[i_old].x;
286 new_path[i_new].y = cuma->curve[i_old].y;
287 /* Make sure old points don't remain selected and active. */
288 new_path[i_new].flag = cuma->curve[i_old].flag & ~(CUMA_SELECT | CUMA_ACTIVE);
289 new_path[i_new].shorty = cuma->curve[i_old].shorty;
290 i_old++;
291 }
292 }
293
294 /* Free old curve and replace it with new one. */
295 MEM_delete(cuma->curve);
296 cuma->curve = new_path;
297
298 return new_pt;
299}
300
301void BKE_curvemap_reset(CurveMap *cuma, const rctf *clipr, int preset, CurveMapSlopeType slope)
302{
303 if (cuma->curve) {
304 MEM_delete(cuma->curve);
305 }
306
307 switch (preset) {
310 cuma->totpoint = 2;
311 break;
313 cuma->totpoint = 5;
314 break;
316 cuma->totpoint = 8;
317 break;
318 case CURVE_PRESET_MAX:
319 cuma->totpoint = 2;
320 break;
322 cuma->totpoint = 8;
323 break;
325 cuma->totpoint = 6;
326 break;
328 cuma->totpoint = 6;
329 break;
331 cuma->totpoint = 7;
332 break;
334 cuma->totpoint = 3;
335 break;
336 }
337
338 cuma->curve = MEM_new_array<CurveMapPoint>(cuma->totpoint, "curve points");
339
340 for (int i = 0; i < cuma->totpoint; i++) {
341 cuma->curve[i].flag = cuma->default_handle_type;
342 }
343
344 switch (preset) {
346 cuma->curve[0].x = clipr->xmin;
347 cuma->curve[0].y = clipr->ymax;
348 cuma->curve[1].x = clipr->xmax;
349 cuma->curve[1].y = clipr->ymin;
353 cuma->curve[0].flag |= CUMA_HANDLE_VECTOR;
354 cuma->curve[1].flag |= CUMA_HANDLE_VECTOR;
355 }
356 break;
358 cuma->curve[0].x = clipr->xmin;
359 cuma->curve[0].y = (clipr->ymin + clipr->ymax) / 2.0f;
360 cuma->curve[1].x = clipr->xmax;
361 cuma->curve[1].y = (clipr->ymin + clipr->ymax) / 2.0f;
362 break;
364 cuma->curve[0].x = 0;
365 cuma->curve[0].y = 1;
366 cuma->curve[1].x = 0.25;
367 cuma->curve[1].y = 0.5625;
368 cuma->curve[2].x = 0.50;
369 cuma->curve[2].y = 0.25;
370 cuma->curve[3].x = 0.75;
371 cuma->curve[3].y = 0.0625;
372 cuma->curve[4].x = 1;
373 cuma->curve[4].y = 0;
376 cuma->curve[0].flag |= CUMA_HANDLE_VECTOR;
378 cuma->curve[4].flag |= CUMA_HANDLE_VECTOR;
379 }
380 break;
382 cuma->curve[0].x = 0;
383 cuma->curve[0].y = 1;
384 cuma->curve[1].x = 0.12;
385 cuma->curve[1].y = 0.96;
386 cuma->curve[2].x = 0.25;
387 cuma->curve[2].y = 0.84;
388 cuma->curve[3].x = 0.42;
389 cuma->curve[3].y = 0.62;
390 cuma->curve[4].x = 0.58;
391 cuma->curve[4].y = 0.38;
392 cuma->curve[5].x = 0.75;
393 cuma->curve[5].y = 0.16;
394 cuma->curve[6].x = 0.88;
395 cuma->curve[6].y = 0.04;
396 cuma->curve[7].x = 1;
397 cuma->curve[7].y = 0;
398 break;
399 case CURVE_PRESET_MAX:
400 cuma->curve[0].x = 0;
401 cuma->curve[0].y = 1;
402 cuma->curve[1].x = 1;
403 cuma->curve[1].y = 1;
404 break;
405 case CURVE_PRESET_MID8: {
406 for (int i = 0; i < cuma->totpoint; i++) {
407 cuma->curve[i].x = i / float(cuma->totpoint);
408 cuma->curve[i].y = 0.5;
409 }
410 break;
411 }
413 cuma->curve[0].x = 0;
414 cuma->curve[0].y = 1;
415 cuma->curve[1].x = 0.5;
416 cuma->curve[1].y = 0.866;
417 cuma->curve[2].x = 0.6765;
418 cuma->curve[2].y = 0.7364;
419 cuma->curve[3].x = 0.8582;
420 cuma->curve[3].y = 0.5133;
421 cuma->curve[4].x = 0.967;
422 cuma->curve[4].y = 0.2547;
423 cuma->curve[5].x = 1;
424 cuma->curve[5].y = 0;
425 break;
427 cuma->curve[0].x = 0;
428 cuma->curve[0].y = 1;
429 cuma->curve[1].x = 0.25;
430 cuma->curve[1].y = 0.866;
431 cuma->curve[2].x = 0.5;
432 cuma->curve[2].y = 0.707;
433 cuma->curve[3].x = 0.75;
434 cuma->curve[3].y = 0.5;
435 cuma->curve[4].x = 0.9375;
436 cuma->curve[4].y = 0.25;
437 cuma->curve[5].x = 1;
438 cuma->curve[5].y = 0;
441 cuma->curve[0].flag |= CUMA_HANDLE_VECTOR;
443 cuma->curve[5].flag |= CUMA_HANDLE_VECTOR;
444 }
445 break;
447 cuma->curve[0].x = 0;
448 cuma->curve[0].y = 0.025f;
449 cuma->curve[1].x = 0.16f;
450 cuma->curve[1].y = 0.135f;
451 cuma->curve[2].x = 0.298f;
452 cuma->curve[2].y = 0.36f;
453
454 cuma->curve[3].x = 0.50f;
455 cuma->curve[3].y = 1.0f;
456
457 cuma->curve[4].x = 0.70f;
458 cuma->curve[4].y = 0.36f;
459 cuma->curve[5].x = 0.84f;
460 cuma->curve[5].y = 0.135f;
461 cuma->curve[6].x = 1.0f;
462 cuma->curve[6].y = 0.025f;
463 break;
465 cuma->curve[0].x = 0.0f;
466 cuma->curve[0].y = 0.025f;
467
468 cuma->curve[1].x = 0.50f;
469 cuma->curve[1].y = 1.0f;
470
471 cuma->curve[2].x = 1.0f;
472 cuma->curve[2].y = 0.025f;
473 break;
474 }
475
476 /* mirror curve in x direction to have positive slope
477 * rather than default negative slope */
478 if (slope == CurveMapSlopeType::Positive) {
480 BLI_assert(cuma->totpoint == 2);
481 /* The LINE and CONSTANT_MEDIAN presets are defined by a single pair of points, relative to
482 * the clip region. */
483 std::swap(cuma->curve[0].y, cuma->curve[1].y);
484 }
485 else if (preset == CURVE_PRESET_MID8) {
486 /* All points in the MID8 preset have y = 0.5, so no action is needed. */
487 }
488 else {
489 int i, last = cuma->totpoint - 1;
490 /* For all curves other than the LINE and CONSTANT_MEDIAN curves, we assume that the x period
491 * is from [0.0, 1.0] inclusive. Resetting the curve for these presets does not take into
492 * account the current clipping region. */
493 BLI_assert(cuma->curve[0].x == 0.0f && cuma->curve[last].x == 1.0f);
494 CurveMapPoint *newpoints = MEM_dupalloc(cuma->curve);
495 for (i = 0; i < cuma->totpoint; i++) {
496 newpoints[i].x = 1.0f - cuma->curve[last - i].x;
497 newpoints[i].y = cuma->curve[last - i].y;
498 }
499 MEM_delete(cuma->curve);
500 cuma->curve = newpoints;
501 }
502 }
503 else if (slope == CurveMapSlopeType::PositiveNegative) {
504 const int num_points = cuma->totpoint * 2 - 1;
505 CurveMapPoint *new_points = MEM_new_array<CurveMapPoint>(size_t(num_points),
506 "curve symmetric points");
507 for (int i = 0; i < cuma->totpoint; i++) {
508 const int src_last_point = cuma->totpoint - i - 1;
509 const int dst_last_point = num_points - i - 1;
510 new_points[i] = cuma->curve[src_last_point];
511 new_points[i].x = (1.0f - cuma->curve[src_last_point].x) * 0.5f;
512 new_points[dst_last_point] = new_points[i];
513 new_points[dst_last_point].x = 0.5f + cuma->curve[src_last_point].x * 0.5f;
514 }
515 cuma->totpoint = num_points;
516 MEM_delete(cuma->curve);
517 cuma->curve = new_points;
518 }
519
520 if (cuma->table) {
521 MEM_delete(cuma->table);
522 cuma->table = nullptr;
523 }
524}
525
527{
528 CurveMapPoint *pts = cuma->curve;
529 for (int i = 1;; i++) {
530 int k = (i + 1) / 2;
531 int idx = (i & 1) ? (i_last - k) : (i_last + k);
532
533 if (idx < 0 || idx >= cuma->totpoint) {
534 if (i_last - k < 0 && i_last + k >= cuma->totpoint) {
535 return;
536 }
537 continue;
538 }
539
540 if (pts[idx].flag & CUMA_SELECT) {
541 pts[idx].flag |= CUMA_ACTIVE;
542 return;
543 }
544 }
545}
546
548{
549 int a;
550
551 for (a = 0; a < cuma->totpoint; a++) {
552 if (cuma->curve[a].flag & CUMA_SELECT) {
554 if (type == HD_VECT) {
555 cuma->curve[a].flag |= CUMA_HANDLE_VECTOR;
556 }
557 else if (type == HD_AUTO_ANIM) {
558 cuma->curve[a].flag |= CUMA_HANDLE_AUTO_ANIM;
559 }
560 else {
561 /* pass */
562 }
563 }
564 }
565}
566
567/* *********************** Making the tables and display ************** */
568
572static void calchandle_curvemap(BezTriple *bezt, const BezTriple *prev, const BezTriple *next)
573{
574/* defines to avoid confusion */
575#define p2_h1 ((p2) - 3)
576#define p2_h2 ((p2) + 3)
577
578 const float *p1, *p3;
579 float *p2;
580 float pt[3];
581 float len, len_a, len_b;
582 float dvec_a[2], dvec_b[2];
583
584 if (bezt->h1 == 0 && bezt->h2 == 0) {
585 return;
586 }
587
588 p2 = bezt->vec[1];
589
590 if (prev == nullptr) {
591 p3 = next->vec[1];
592 pt[0] = 2.0f * p2[0] - p3[0];
593 pt[1] = 2.0f * p2[1] - p3[1];
594 p1 = pt;
595 }
596 else {
597 p1 = prev->vec[1];
598 }
599
600 if (next == nullptr) {
601 p1 = prev->vec[1];
602 pt[0] = 2.0f * p2[0] - p1[0];
603 pt[1] = 2.0f * p2[1] - p1[1];
604 p3 = pt;
605 }
606 else {
607 p3 = next->vec[1];
608 }
609
610 sub_v2_v2v2(dvec_a, p2, p1);
611 sub_v2_v2v2(dvec_b, p3, p2);
612
613 len_a = len_v2(dvec_a);
614 len_b = len_v2(dvec_b);
615
616 if (len_a == 0.0f) {
617 len_a = 1.0f;
618 }
619 if (len_b == 0.0f) {
620 len_b = 1.0f;
621 }
622
623 if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM) || ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) { /* auto */
624 float tvec[2];
625 tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
626 tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
627
628 len = len_v2(tvec) * 2.5614f;
629 if (len != 0.0f) {
630
631 if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) {
632 len_a /= len;
633 madd_v2_v2v2fl(p2_h1, p2, tvec, -len_a);
634
635 if ((bezt->h1 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
636 const float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
637 const float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
638 if ((ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f)) {
639 bezt->vec[0][1] = bezt->vec[1][1];
640 }
641 else { /* handles should not be beyond y coord of two others */
642 if (ydiff1 <= 0.0f) {
643 bezt->vec[0][1] = std::max(prev->vec[1][1], bezt->vec[0][1]);
644 }
645 else {
646 bezt->vec[0][1] = std::min(prev->vec[1][1], bezt->vec[0][1]);
647 }
648 }
649 }
650 }
651 if (ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {
652 len_b /= len;
653 madd_v2_v2v2fl(p2_h2, p2, tvec, len_b);
654
655 if ((bezt->h2 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
656 const float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
657 const float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
658 if ((ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f)) {
659 bezt->vec[2][1] = bezt->vec[1][1];
660 }
661 else { /* handles should not be beyond y coord of two others */
662 if (ydiff1 <= 0.0f) {
663 bezt->vec[2][1] = std::min(next->vec[1][1], bezt->vec[2][1]);
664 }
665 else {
666 bezt->vec[2][1] = std::max(next->vec[1][1], bezt->vec[2][1]);
667 }
668 }
669 }
670 }
671 }
672 }
673
674 if (bezt->h1 == HD_VECT) { /* vector */
675 madd_v2_v2v2fl(p2_h1, p2, dvec_a, -1.0f / 3.0f);
676 }
677 if (bezt->h2 == HD_VECT) {
678 madd_v2_v2v2fl(p2_h2, p2, dvec_b, 1.0f / 3.0f);
679 }
680
681#undef p2_h1
682#undef p2_h2
683}
684
685/* in X, out Y.
686 * X is presumed to be outside first or last */
687static float curvemap_calc_extend(const CurveMapping *cumap,
688 const CurveMap *cuma,
689 float x,
690 const float first[2],
691 const float last[2])
692{
693 if (x <= first[0]) {
694 if ((cumap->flag & CUMA_EXTEND_EXTRAPOLATE) == 0) {
695 /* extrapolate horizontally */
696 return first[1];
697 }
698
699 if (cuma->ext_in[0] == 0.0f) {
700 return first[1] + cuma->ext_in[1] * 10000.0f;
701 }
702
703 return first[1] + cuma->ext_in[1] * (x - first[0]) / cuma->ext_in[0];
704 }
705 if (x >= last[0]) {
706 if ((cumap->flag & CUMA_EXTEND_EXTRAPOLATE) == 0) {
707 /* extrapolate horizontally */
708 return last[1];
709 }
710
711 if (cuma->ext_out[0] == 0.0f) {
712 return last[1] - cuma->ext_out[1] * 10000.0f;
713 }
714
715 return last[1] + cuma->ext_out[1] * (x - last[0]) / cuma->ext_out[0];
716 }
717 return 0.0f;
718}
719
720/* Evaluates CM_RESOL number of points on the Bezier segment defined by the given start and end
721 * Bezier triples, writing the output to the points array. */
722static void curve_eval_bezier_point(float start[3][3], float end[3][3], float *point)
723{
724 BKE_curve_correct_bezpart(start[1], start[2], end[0], end[1]);
726 start[1][0], start[2][0], end[0][0], end[1][0], point, CM_RESOL - 1, sizeof(float[2]));
728 start[1][1], start[2][1], end[0][1], end[1][1], point + 1, CM_RESOL - 1, sizeof(float[2]));
729}
730
731/* only creates a table for a single channel in CurveMapping */
732static void curvemap_make_table(const CurveMapping *cumap, CurveMap *cuma)
733{
734 const rctf *clipr = &cumap->clipr;
735
736 /* Wrapping ensures that the heights of the first and last points are the same. It adds two
737 * virtual points, which are copies of the first and last points, and moves them to the opposite
738 * side of the curve offset by the table range. The handles of these points are calculated, as if
739 * they were between the last and first real points. */
740
741 const bool use_wrapping = cumap->flag & CUMA_USE_WRAPPING;
742
743 if (cuma->curve == nullptr) {
744 return;
745 }
746
747 /* default rect also is table range */
748 cuma->mintable = clipr->xmin;
749 cuma->maxtable = clipr->xmax;
750 const int bezt_totpoint = max_ii(cuma->totpoint, 2);
751
752 /* Rely on Blender interpolation for bezier curves, support extra functionality here as well. */
753 BezTriple *bezt = MEM_new_array_zeroed<BezTriple>(bezt_totpoint, "beztarr");
754
755 /* Valid curve has at least 2 points. */
756 if (cuma->totpoint >= 2) {
757 CurveMapPoint *cmp = cuma->curve;
758
759 for (int a = 0; a < bezt_totpoint; a++) {
760 cuma->mintable = min_ff(cuma->mintable, cmp[a].x);
761 cuma->maxtable = max_ff(cuma->maxtable, cmp[a].x);
762 bezt[a].vec[1][0] = cmp[a].x;
763 bezt[a].vec[1][1] = cmp[a].y;
764 if (cmp[a].flag & CUMA_HANDLE_VECTOR) {
765 bezt[a].h1 = bezt[a].h2 = HD_VECT;
766 }
767 else if (cmp[a].flag & CUMA_HANDLE_AUTO_ANIM) {
768 bezt[a].h1 = bezt[a].h2 = HD_AUTO_ANIM;
769 }
770 else {
771 bezt[a].h1 = bezt[a].h2 = HD_AUTO;
772 }
773 }
774 }
775 else {
776 /* Fallback when points are missing. */
777 cuma->mintable = 0.0f;
778 cuma->maxtable = 0.0f;
779 zero_v2(bezt[0].vec[1]);
780 zero_v2(bezt[1].vec[1]);
781 bezt[0].h1 = HD_AUTO;
782 bezt[0].h2 = HD_AUTO;
783 bezt[1].h1 = HD_AUTO;
784 bezt[1].h2 = HD_AUTO;
785 }
786
787 const BezTriple *bezt_next = nullptr;
788 const BezTriple *bezt_prev = nullptr;
789
790 /* Create two extra points for wrapping curves. */
791 BezTriple bezt_pre = bezt[bezt_totpoint - 1];
792 BezTriple bezt_post = bezt[0];
793
794 BezTriple *bezt_post_ptr;
795
796 float table_range = cuma->maxtable - cuma->mintable;
797 if (use_wrapping) {
798 /* Handle location of pre and post points for wrapping curves. */
799 bezt_pre.h1 = bezt_pre.h2 = bezt[bezt_totpoint - 1].h2;
800 bezt_pre.vec[1][0] = bezt[bezt_totpoint - 1].vec[1][0] - table_range;
801 bezt_pre.vec[1][1] = bezt[bezt_totpoint - 1].vec[1][1];
802
803 bezt_post.h1 = bezt_post.h2 = bezt[0].h1;
804 bezt_post.vec[1][0] = bezt[0].vec[1][0] + table_range;
805 bezt_post.vec[1][1] = bezt[0].vec[1][1];
806
807 bezt_prev = &bezt_pre;
808 bezt_post_ptr = &bezt_post;
809 }
810 else {
811 bezt_prev = nullptr;
812 bezt_post_ptr = nullptr;
813 }
814
815 /* Process middle elements */
816 for (int a = 0; a < bezt_totpoint; a++) {
817 bezt_next = (a != bezt_totpoint - 1) ? &bezt[a + 1] : bezt_post_ptr;
818 calchandle_curvemap(&bezt[a], bezt_prev, bezt_next);
819 bezt_prev = &bezt[a];
820 }
821
822 /* Correct handles of pre and post points for wrapping curves. */
823 bezt_pre.vec[0][0] = bezt[bezt_totpoint - 1].vec[0][0] - table_range;
824 bezt_pre.vec[0][1] = bezt[bezt_totpoint - 1].vec[0][1];
825 bezt_pre.vec[2][0] = bezt[bezt_totpoint - 1].vec[2][0] - table_range;
826 bezt_pre.vec[2][1] = bezt[bezt_totpoint - 1].vec[2][1];
827
828 bezt_post.vec[0][0] = bezt[0].vec[0][0] + table_range;
829 bezt_post.vec[0][1] = bezt[0].vec[0][1];
830 bezt_post.vec[2][0] = bezt[0].vec[2][0] + table_range;
831 bezt_post.vec[2][1] = bezt[0].vec[2][1];
832
833 /* first and last handle need correction, instead of pointing to center of next/prev,
834 * we let it point to the closest handle */
835 if (bezt_totpoint > 2 && !use_wrapping) {
836 float hlen, nlen, vec[3];
837
838 if (bezt[0].h2 == HD_AUTO) {
839
840 hlen = len_v3v3(bezt[0].vec[1], bezt[0].vec[2]); /* original handle length */
841 /* clip handle point */
842 copy_v3_v3(vec, bezt[1].vec[0]);
843 vec[0] = std::max(vec[0], bezt[0].vec[1][0]);
844
845 sub_v3_v3(vec, bezt[0].vec[1]);
846 nlen = len_v3(vec);
847 if (nlen > FLT_EPSILON) {
848 mul_v3_fl(vec, hlen / nlen);
849 add_v3_v3v3(bezt[0].vec[2], vec, bezt[0].vec[1]);
850 sub_v3_v3v3(bezt[0].vec[0], bezt[0].vec[1], vec);
851 }
852 }
853 int a = bezt_totpoint - 1;
854 if (bezt[a].h2 == HD_AUTO) {
855
856 hlen = len_v3v3(bezt[a].vec[1], bezt[a].vec[0]); /* original handle length */
857 /* clip handle point */
858 copy_v3_v3(vec, bezt[a - 1].vec[2]);
859 vec[0] = std::min(vec[0], bezt[a].vec[1][0]);
860
861 sub_v3_v3(vec, bezt[a].vec[1]);
862 nlen = len_v3(vec);
863 if (nlen > FLT_EPSILON) {
864 mul_v3_fl(vec, hlen / nlen);
865 add_v3_v3v3(bezt[a].vec[0], vec, bezt[a].vec[1]);
866 sub_v3_v3v3(bezt[a].vec[2], bezt[a].vec[1], vec);
867 }
868 }
869 }
870
871 /* make the bezier curve */
872 if (cuma->table) {
873 MEM_delete(cuma->table);
874 }
875
876 const int totpoint = use_wrapping ? (bezt_totpoint + 1) * CM_RESOL :
877 (bezt_totpoint - 1) * CM_RESOL;
878 float *allpoints = MEM_new_array_zeroed<float>(size_t(totpoint) * 2, "table");
879 float *point = allpoints;
880
881 /* Handle pre point for wrapping */
882 if (use_wrapping) {
883 curve_eval_bezier_point(bezt_pre.vec, bezt[0].vec, point);
884 point += 2 * CM_RESOL;
885 }
886
887 /* Process middle elements */
888 for (int a = 0; a < bezt_totpoint - 1; a++, point += 2 * CM_RESOL) {
889 int b = a + 1;
890 curve_eval_bezier_point(bezt[a].vec, bezt[b].vec, point);
891 }
892
893 if (use_wrapping) {
894 /* Handle post point for wrapping */
895 curve_eval_bezier_point(bezt[bezt_totpoint - 1].vec, bezt_post.vec, point);
896 }
897 /* Store first and last handle for extrapolation, unit length. (Only relevant when not using
898 * wrapping.) */
899 cuma->ext_in[0] = bezt[0].vec[0][0] - bezt[0].vec[1][0];
900 cuma->ext_in[1] = bezt[0].vec[0][1] - bezt[0].vec[1][1];
901 float ext_in_range = sqrtf(cuma->ext_in[0] * cuma->ext_in[0] +
902 cuma->ext_in[1] * cuma->ext_in[1]);
903 cuma->ext_in[0] /= ext_in_range;
904 cuma->ext_in[1] /= ext_in_range;
905
906 int out_a = bezt_totpoint - 1;
907 cuma->ext_out[0] = bezt[out_a].vec[1][0] - bezt[out_a].vec[2][0];
908 cuma->ext_out[1] = bezt[out_a].vec[1][1] - bezt[out_a].vec[2][1];
909 float ext_out_range = sqrtf(cuma->ext_out[0] * cuma->ext_out[0] +
910 cuma->ext_out[1] * cuma->ext_out[1]);
911 cuma->ext_out[0] /= ext_out_range;
912 cuma->ext_out[1] /= ext_out_range;
913
914 /* cleanup */
915 MEM_delete(bezt);
916
917 float range = CM_TABLEDIV * table_range;
918 cuma->range = 1.0f / range;
919
920 /* now make a table with CM_TABLE equal x distances */
921 float *firstpoint = allpoints;
922 float *lastpoint = allpoints + 2 * (totpoint - 1);
923 point = allpoints;
924
925 CurveMapPoint *cmp = MEM_new_array<CurveMapPoint>(CM_TABLE + 1, "dist table");
926
927 for (int a = 0; a <= CM_TABLE; a++) {
928 float cur_x = cuma->mintable + range * float(a);
929 cmp[a].x = cur_x;
930
931 /* Get the first point with x coordinate larger than cur_x. */
932 while (cur_x >= point[0] && point != lastpoint) {
933 point += 2;
934 }
935 /* Check if we are on or outside the start or end point. */
936 if ((point == firstpoint || (point == lastpoint && cur_x >= point[0])) && !use_wrapping) {
937 if (compare_ff(cur_x, point[0], 1e-6f)) {
938 /* When on the point exactly, use the value directly to avoid precision
939 * issues with extrapolation of extreme slopes. */
940 cmp[a].y = point[1];
941 }
942 else {
943 /* Extrapolate values that lie outside the start and end point. */
944 cmp[a].y = curvemap_calc_extend(cumap, cuma, cur_x, firstpoint, lastpoint);
945 }
946 }
947 else {
948 float fac1 = point[0] - point[-2];
949 float fac2 = point[0] - cur_x;
950 if (fac1 > FLT_EPSILON) {
951 fac1 = fac2 / fac1;
952 }
953 else {
954 fac1 = 0.0f;
955 }
956 cmp[a].y = fac1 * point[-1] + (1.0f - fac1) * point[1];
957 }
958 }
959
960 MEM_delete(allpoints);
961 cuma->table = cmp;
962}
963
965{
966 /* It uses a flag to prevent pre-multiply or free to happen twice. */
967
968 int a;
969
970 if (restore) {
971 if (cumap->flag & CUMA_PREMULLED) {
972 for (a = 0; a < 3; a++) {
973 MEM_delete(cumap->cm[a].table);
974 cumap->cm[a].table = cumap->cm[a].premultable;
975 cumap->cm[a].premultable = nullptr;
976
977 copy_v2_v2(cumap->cm[a].ext_in, cumap->cm[a].premul_ext_in);
978 copy_v2_v2(cumap->cm[a].ext_out, cumap->cm[a].premul_ext_out);
979 zero_v2(cumap->cm[a].premul_ext_in);
980 zero_v2(cumap->cm[a].premul_ext_out);
981 }
982
983 cumap->flag &= ~CUMA_PREMULLED;
984 }
985 }
986 else {
987 if ((cumap->flag & CUMA_PREMULLED) == 0) {
988 /* verify and copy */
989 for (a = 0; a < 3; a++) {
990 if (cumap->cm[a].table == nullptr) {
991 curvemap_make_table(cumap, cumap->cm + a);
992 }
993 cumap->cm[a].premultable = cumap->cm[a].table;
994 cumap->cm[a].table = MEM_new_array<CurveMapPoint>(CM_TABLE + 1, "premul table");
995 memcpy(
996 cumap->cm[a].table, cumap->cm[a].premultable, (CM_TABLE + 1) * sizeof(CurveMapPoint));
997 }
998
999 if (cumap->cm[3].table == nullptr) {
1000 curvemap_make_table(cumap, cumap->cm + 3);
1001 }
1002
1003 /* premul */
1004 for (a = 0; a < 3; a++) {
1005 int b;
1006 for (b = 0; b <= CM_TABLE; b++) {
1007 cumap->cm[a].table[b].y = BKE_curvemap_evaluateF(
1008 cumap, cumap->cm + 3, cumap->cm[a].table[b].y);
1009 }
1010
1011 copy_v2_v2(cumap->cm[a].premul_ext_in, cumap->cm[a].ext_in);
1012 copy_v2_v2(cumap->cm[a].premul_ext_out, cumap->cm[a].ext_out);
1013 mul_v2_v2(cumap->cm[a].ext_in, cumap->cm[3].ext_in);
1014 mul_v2_v2(cumap->cm[a].ext_out, cumap->cm[3].ext_out);
1015 }
1016
1017 cumap->flag |= CUMA_PREMULLED;
1018 }
1019 }
1020}
1021
1022/* ************************ more CurveMapping calls *************** */
1024{
1025 CurveMapPoint *active_pt = nullptr;
1026 for (int i = 0; i < cuma->totpoint; i++) {
1027 CurveMapPoint *pt = &cuma->curve[i];
1028 if (pt->flag & CUMA_SELECT) {
1029 active_pt = pt;
1030 if (pt->flag & CUMA_ACTIVE) {
1031 break;
1032 }
1033 }
1034 }
1035 return active_pt;
1036}
1037
1039{
1040 for (int i = 0; i < cuma->totpoint; i++) {
1041 CurveMapPoint *pt = &cuma->curve[i];
1042 if (pt->flag & CUMA_SELECT) {
1043 pt->x += offset.x;
1044 pt->y += offset.y;
1045 }
1046 }
1047}
1048
1049void BKE_curvemapping_changed(CurveMapping *cumap, const bool rem_doubles)
1050{
1051 CurveMap *cuma = cumap->cm + cumap->cur;
1052 CurveMapPoint *cmp = cuma->curve;
1053 const rctf *clipr = &cumap->clipr;
1054 float thresh = 0.01f * BLI_rctf_size_x(clipr);
1055 float dx = 0.0f, dy = 0.0f;
1056 int a;
1057
1058 cumap->changed_timestamp++;
1059
1060 /* clamp with clip */
1061 if (cumap->flag & CUMA_DO_CLIP) {
1062 for (a = 0; a < cuma->totpoint; a++) {
1063 if (cmp[a].flag & CUMA_SELECT) {
1064 if (cmp[a].x < clipr->xmin) {
1065 dx = min_ff(dx, cmp[a].x - clipr->xmin);
1066 }
1067 else if (cmp[a].x > clipr->xmax) {
1068 dx = max_ff(dx, cmp[a].x - clipr->xmax);
1069 }
1070 if (cmp[a].y < clipr->ymin) {
1071 dy = min_ff(dy, cmp[a].y - clipr->ymin);
1072 }
1073 else if (cmp[a].y > clipr->ymax) {
1074 dy = max_ff(dy, cmp[a].y - clipr->ymax);
1075 }
1076 }
1077 }
1078 for (a = 0; a < cuma->totpoint; a++) {
1079 if (cmp[a].flag & CUMA_SELECT) {
1080 cmp[a].x -= dx;
1081 cmp[a].y -= dy;
1082 }
1083 }
1084
1085 /* ensure zoom-level respects clipping */
1086 if (BLI_rctf_size_x(&cumap->curr) > BLI_rctf_size_x(&cumap->clipr)) {
1087 cumap->curr.xmin = cumap->clipr.xmin;
1088 cumap->curr.xmax = cumap->clipr.xmax;
1089 }
1090 if (BLI_rctf_size_y(&cumap->curr) > BLI_rctf_size_y(&cumap->clipr)) {
1091 cumap->curr.ymin = cumap->clipr.ymin;
1092 cumap->curr.ymax = cumap->clipr.ymax;
1093 }
1094 }
1095
1096 std::stable_sort(cuma->curve,
1097 cuma->curve + cuma->totpoint,
1098 [](const CurveMapPoint &a, const CurveMapPoint &b) { return a.x < b.x; });
1099
1100 /* remove doubles, threshold set on 1% of default range */
1101 if (rem_doubles && cuma->totpoint > 2) {
1102 for (a = 0; a < cuma->totpoint - 1; a++) {
1103 dx = cmp[a].x - cmp[a + 1].x;
1104 dy = cmp[a].y - cmp[a + 1].y;
1105 if (sqrtf(dx * dx + dy * dy) < thresh) {
1106 if (a == 0) {
1107 cmp[a + 1].flag |= CUMA_REMOVE;
1108 if (cmp[a + 1].flag & CUMA_SELECT) {
1109 cmp[a].flag |= CUMA_SELECT;
1110 }
1111 if (cmp[a + 1].flag & CUMA_ACTIVE) {
1112 cmp[a].flag |= CUMA_ACTIVE;
1113 }
1114 }
1115 else {
1116 cmp[a].flag |= CUMA_REMOVE;
1117 if (cmp[a].flag & CUMA_SELECT) {
1118 cmp[a + 1].flag |= CUMA_SELECT;
1119 }
1120 if (cmp[a].flag & CUMA_ACTIVE) {
1121 cmp[a + 1].flag |= CUMA_ACTIVE;
1122 }
1123 }
1124 break; /* we assume 1 deletion per edit is ok */
1125 }
1126 }
1127 if (a != cuma->totpoint - 1) {
1129 }
1130 }
1131 curvemap_make_table(cumap, cuma);
1132}
1133
1135{
1136 int a, cur = cumap->cur;
1137
1138 for (a = 0; a < CM_TOT; a++) {
1139 if (cumap->cm[a].curve) {
1140 cumap->cur = a;
1141 BKE_curvemapping_changed(cumap, false);
1142 }
1143 }
1144
1145 cumap->cur = cur;
1146}
1147
1149{
1150 cumap->curr = cumap->clipr;
1151}
1152
1153float BKE_curvemap_evaluateF(const CurveMapping *cumap, const CurveMap *cuma, float value)
1154{
1155 BLI_assert_msg(cuma->table, "Table must be initialized, see 'BKE_curvemapping_init'");
1156
1157 /* index in table */
1158 float fi = (value - cuma->mintable) * cuma->range;
1159 int i = int(fi);
1160
1161 /* fi is table float index and should check against table range i.e. [0.0 CM_TABLE] */
1162 if (fi < 0.0f || fi > CM_TABLE) {
1163 return curvemap_calc_extend(cumap, cuma, value, &cuma->table[0].x, &cuma->table[CM_TABLE].x);
1164 }
1165
1166 if (i < 0) {
1167 return cuma->table[0].y;
1168 }
1169 if (i >= CM_TABLE) {
1170 return cuma->table[CM_TABLE].y;
1171 }
1172
1173 fi = fi - float(i);
1174 return (1.0f - fi) * cuma->table[i].y + (fi)*cuma->table[i + 1].y;
1175}
1176
1177float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
1178{
1179 const CurveMap *cuma = cumap->cm + cur;
1180 float val = BKE_curvemap_evaluateF(cumap, cuma, value);
1181
1182 /* account for clipping */
1183 if (cumap->flag & CUMA_DO_CLIP) {
1184 if (val < cumap->clipr.ymin) {
1185 val = cumap->clipr.ymin;
1186 }
1187 else if (val > cumap->clipr.ymax) {
1188 val = cumap->clipr.ymax;
1189 }
1190 }
1191
1192 return val;
1193}
1194
1195void BKE_curvemapping_evaluate3F(const CurveMapping *cumap, float vecout[3], const float vecin[3])
1196{
1197 vecout[0] = BKE_curvemap_evaluateF(cumap, &cumap->cm[0], vecin[0]);
1198 vecout[1] = BKE_curvemap_evaluateF(cumap, &cumap->cm[1], vecin[1]);
1199 vecout[2] = BKE_curvemap_evaluateF(cumap, &cumap->cm[2], vecin[2]);
1200}
1201
1203 float vecout[3],
1204 const float vecin[3])
1205{
1206 vecout[0] = BKE_curvemap_evaluateF(
1207 cumap, &cumap->cm[0], BKE_curvemap_evaluateF(cumap, &cumap->cm[3], vecin[0]));
1208 vecout[1] = BKE_curvemap_evaluateF(
1209 cumap, &cumap->cm[1], BKE_curvemap_evaluateF(cumap, &cumap->cm[3], vecin[1]));
1210 vecout[2] = BKE_curvemap_evaluateF(
1211 cumap, &cumap->cm[2], BKE_curvemap_evaluateF(cumap, &cumap->cm[3], vecin[2]));
1212}
1213
1214/* Contrary to standard tone curve implementations, the film-like implementation tries to preserve
1215 * the hue of the colors as much as possible. To understand why this might be a problem, consider
1216 * the violet color (0.5, 0.0, 1.0). If this color was to be evaluated at a power curve x^4, the
1217 * color will be blue (0.0625, 0.0, 1.0). So the color changes and not just its luminosity, which
1218 * is what film-like tone curves tries to avoid.
1219 *
1220 * First, the channels with the lowest and highest values are identified and evaluated at the
1221 * curve. Then, the third channel---the median---is computed while maintaining the original hue of
1222 * the color. To do that, we look at the equation for deriving the hue from RGB values. Assuming
1223 * the maximum, minimum, and median channels are known, and ignoring the 1/3 period offset of the
1224 * hue, the equation is:
1225 *
1226 * hue = (median - min) / (max - min) [1]
1227 *
1228 * Since we have the new values for the minimum and maximum after evaluating at the curve, we also
1229 * have:
1230 *
1231 * hue = (new_median - new_min) / (new_max - new_min) [2]
1232 *
1233 * Since we want the hue to be equivalent, by equating [1] and [2] and rearranging:
1234 *
1235 * (new_median - new_min) / (new_max - new_min) = (median - min) / (max - min)
1236 * new_median - new_min = (new_max - new_min) * (median - min) / (max - min)
1237 * new_median = new_min + (new_max - new_min) * (median - min) / (max - min)
1238 * new_median = new_min + (median - min) * ((new_max - new_min) / (max - min)) [QED]
1239 *
1240 * Which gives us the median color that preserves the hue. More intuitively, the median is computed
1241 * such that the change in the distance from the median to the minimum is proportional to the
1242 * change in the distance from the minimum to the maximum. Finally, each of the new minimum,
1243 * maximum, and median values are written to the color channel that they were originally extracted
1244 * from. */
1245static float3 evaluate_film_like(const CurveMapping *curve_mapping, float3 input)
1246{
1247 /* Film-like curves are only evaluated on the combined curve, which is the fourth curve map. */
1248 const CurveMap *curve_map = curve_mapping->cm + 3;
1249
1250 /* Find the maximum, minimum, and median of the color channels. */
1251 const float minimum = math::reduce_min(input);
1252 const float maximum = math::reduce_max(input);
1253 const float median = math::max(math::min(input.x, input.y),
1254 math::min(input.z, math::max(input.x, input.y)));
1255
1256 const float new_min = BKE_curvemap_evaluateF(curve_mapping, curve_map, minimum);
1257 const float new_max = BKE_curvemap_evaluateF(curve_mapping, curve_map, maximum);
1258
1259 /* Compute the new median using the ratio between the new and the original range. */
1260 const float scaling_ratio = (new_max - new_min) / (maximum - minimum);
1261 const float new_median = new_min + (median - minimum) * scaling_ratio;
1262
1263 /* Write each value to its original channel. */
1264 const float3 median_or_min = float3(input.x == minimum ? new_min : new_median,
1265 input.y == minimum ? new_min : new_median,
1266 input.z == minimum ? new_min : new_median);
1267 return float3(input.x == maximum ? new_max : median_or_min.x,
1268 input.y == maximum ? new_max : median_or_min.y,
1269 input.z == maximum ? new_max : median_or_min.z);
1270}
1271
1273 float vecout[3],
1274 const float vecin[3],
1275 const float black[3],
1276 const float bwmul[3])
1277{
1278 const float r = (vecin[0] - black[0]) * bwmul[0];
1279 const float g = (vecin[1] - black[1]) * bwmul[1];
1280 const float b = (vecin[2] - black[2]) * bwmul[2];
1281 const float balanced_color[3] = {r, g, b};
1282
1283 switch (cumap->tone) {
1284 default:
1285 case CURVE_TONE_STANDARD: {
1286 vecout[0] = BKE_curvemap_evaluateF(cumap, &cumap->cm[0], r);
1287 vecout[1] = BKE_curvemap_evaluateF(cumap, &cumap->cm[1], g);
1288 vecout[2] = BKE_curvemap_evaluateF(cumap, &cumap->cm[2], b);
1289 break;
1290 }
1291 case CURVE_TONE_FILMLIKE: {
1292 const float3 output = evaluate_film_like(cumap, balanced_color);
1293 copy_v3_v3(vecout, output);
1294 break;
1295 }
1296 }
1297}
1298
1300 float vecout[3],
1301 const float vecin[3])
1302{
1303 BKE_curvemapping_evaluate_premulRGBF_ex(cumap, vecout, vecin, cumap->black, cumap->bwmul);
1304}
1305
1307 uchar vecout_byte[3],
1308 const uchar vecin_byte[3])
1309{
1310 float vecin[3], vecout[3];
1311
1312 vecin[0] = float(vecin_byte[0]) / 255.0f;
1313 vecin[1] = float(vecin_byte[1]) / 255.0f;
1314 vecin[2] = float(vecin_byte[2]) / 255.0f;
1315
1316 BKE_curvemapping_evaluate_premulRGBF(cumap, vecout, vecin);
1317
1318 vecout_byte[0] = unit_float_to_uchar_clamp(vecout[0]);
1319 vecout_byte[1] = unit_float_to_uchar_clamp(vecout[1]);
1320 vecout_byte[2] = unit_float_to_uchar_clamp(vecout[2]);
1321}
1322
1324{
1325 if (cumap->black[0] != 0.0f) {
1326 return true;
1327 }
1328 if (cumap->black[1] != 0.0f) {
1329 return true;
1330 }
1331 if (cumap->black[2] != 0.0f) {
1332 return true;
1333 }
1334 if (cumap->white[0] != 1.0f) {
1335 return true;
1336 }
1337 if (cumap->white[1] != 1.0f) {
1338 return true;
1339 }
1340 if (cumap->white[2] != 1.0f) {
1341 return true;
1342 }
1343
1344 for (int a = 0; a < CM_TOT; a++) {
1345 if (cumap->cm[a].curve) {
1346 if (cumap->cm[a].totpoint != 2) {
1347 return true;
1348 }
1349
1350 if (cumap->cm[a].curve[0].x != 0.0f) {
1351 return true;
1352 }
1353 if (cumap->cm[a].curve[0].y != 0.0f) {
1354 return true;
1355 }
1356 if (cumap->cm[a].curve[1].x != 1.0f) {
1357 return true;
1358 }
1359 if (cumap->cm[a].curve[1].y != 1.0f) {
1360 return true;
1361 }
1362 }
1363 }
1364 return false;
1365}
1366
1367void BKE_curvemapping_get_range_minimums(const CurveMapping *curve_mapping, float minimums[CM_TOT])
1368{
1369 for (int i = 0; i < CM_TOT; i++) {
1370 minimums[i] = curve_mapping->cm[i].mintable;
1371 }
1372}
1373
1375 float dividers[CM_TOT])
1376{
1377 for (int i = 0; i < CM_TOT; i++) {
1378 const CurveMap *curve_map = &curve_mapping->cm[i];
1379 dividers[i] = 1.0f / max_ff(1e-8f, curve_map->maxtable - curve_map->mintable);
1380 }
1381}
1382
1384 float start_slopes[CM_TOT],
1385 float end_slopes[CM_TOT])
1386{
1387 float range_dividers[CM_TOT];
1388 BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
1389 for (int i = 0; i < CM_TOT; i++) {
1390 const CurveMap *curve_map = &curve_mapping->cm[i];
1391 /* If extrapolation is not enabled, the slopes are horizontal. */
1392 if (!(curve_mapping->flag & CUMA_EXTEND_EXTRAPOLATE)) {
1393 start_slopes[i] = 0.0f;
1394 end_slopes[i] = 0.0f;
1395 continue;
1396 }
1397
1398 if (curve_map->ext_in[0] != 0.0f) {
1399 start_slopes[i] = curve_map->ext_in[1] / (curve_map->ext_in[0] * range_dividers[i]);
1400 }
1401 else {
1402 start_slopes[i] = 1e8f;
1403 }
1404
1405 if (curve_map->ext_out[0] != 0.0f) {
1406 end_slopes[i] = curve_map->ext_out[1] / (curve_map->ext_out[0] * range_dividers[i]);
1407 }
1408 else {
1409 end_slopes[i] = 1e8f;
1410 }
1411 }
1412}
1413
1415{
1416 if (!(curve_mapping->flag & CUMA_EXTEND_EXTRAPOLATE)) {
1417 return false;
1418 }
1419 const CurveMap *curve_map = &curve_mapping->cm[index];
1420 if (curve_map->maxtable - curve_map->mintable != 1.0f) {
1421 return false;
1422 }
1423 if (curve_map->ext_in[0] != curve_map->ext_in[1]) {
1424 return false;
1425 }
1426 if (curve_map->ext_out[0] != curve_map->ext_out[1]) {
1427 return false;
1428 }
1429 if (curve_map->totpoint != 2) {
1430 return false;
1431 }
1432 if (curve_map->curve[0].x != 0 || curve_map->curve[0].y != 0) {
1433 return false;
1434 }
1435 if (curve_map->curve[1].x != 0 || curve_map->curve[1].y != 0) {
1436 return false;
1437 }
1438 return true;
1439}
1440
1442{
1443 int a;
1444
1445 if (cumap == nullptr) {
1446 return;
1447 }
1448
1449 for (a = 0; a < CM_TOT; a++) {
1450 if (cumap->cm[a].table == nullptr) {
1451 curvemap_make_table(cumap, cumap->cm + a);
1452 }
1453 }
1454}
1455
1456void BKE_curvemapping_table_F(const CurveMapping *cumap, float **array, int *size)
1457{
1458 int a;
1459
1460 *size = CM_TABLE + 1;
1461 *array = MEM_new_array_zeroed<float>(4 * size_t(*size), "CurveMapping");
1462
1463 for (a = 0; a < *size; a++) {
1464 if (cumap->cm[0].table) {
1465 (*array)[a * 4 + 0] = cumap->cm[0].table[a].y;
1466 }
1467 }
1468}
1469
1470void BKE_curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
1471{
1472 int a;
1473
1474 *size = CM_TABLE + 1;
1475 *array = MEM_new_array_zeroed<float>(4 * size_t(*size), "CurveMapping");
1476
1477 for (a = 0; a < *size; a++) {
1478 if (cumap->cm[0].table) {
1479 (*array)[a * 4 + 0] = cumap->cm[0].table[a].y;
1480 }
1481 if (cumap->cm[1].table) {
1482 (*array)[a * 4 + 1] = cumap->cm[1].table[a].y;
1483 }
1484 if (cumap->cm[2].table) {
1485 (*array)[a * 4 + 2] = cumap->cm[2].table[a].y;
1486 }
1487 if (cumap->cm[3].table) {
1488 (*array)[a * 4 + 3] = cumap->cm[3].table[a].y;
1489 }
1490 }
1491}
1492
1494{
1495 if (cumap->cm[3].totpoint > 0) {
1496 return 4;
1497 }
1498 if (cumap->cm[2].totpoint > 0) {
1499 return 3;
1500 }
1501 if (cumap->cm[1].totpoint > 0) {
1502 return 2;
1503 }
1504
1505 return 1;
1506}
1507
1509{
1510 writer->write_struct(cumap, [writer](BlendStructWriter<CurveMapping> &struct_writer) {
1511 CurveMapping &shallow_cumap = struct_writer.shallow_data;
1512 if (!writer->is_undo()) {
1513 shallow_cumap.changed_timestamp = 0;
1514 }
1515 for (const int i : IndexRange(CM_TOT)) {
1516 shallow_cumap.cm[i].range = 0;
1517 shallow_cumap.cm[i].mintable = 0;
1518 shallow_cumap.cm[i].maxtable = 0;
1519 zero_v2(shallow_cumap.cm[i].ext_in);
1520 zero_v2(shallow_cumap.cm[i].ext_out);
1521 shallow_cumap.cm[i].table = nullptr;
1522 shallow_cumap.cm[i].premultable = nullptr;
1523 zero_v2(shallow_cumap.cm[i].premul_ext_in);
1524 zero_v2(shallow_cumap.cm[i].premul_ext_out);
1525 }
1526 });
1528}
1529
1531{
1532 for (int a = 0; a < CM_TOT; a++) {
1533 writer->write_struct_array(cumap->cm[a].totpoint, cumap->cm[a].curve);
1534 }
1535}
1536
1538{
1539 /* flag seems to be able to hang? Maybe old files... not bad to clear anyway */
1540 cumap->flag &= ~CUMA_PREMULLED;
1541
1542 for (int a = 0; a < CM_TOT; a++) {
1543 BLO_read_array_and_validate_size(reader, &cumap->cm[a].curve, &cumap->cm[a].totpoint);
1544 cumap->cm[a].table = nullptr;
1545 cumap->cm[a].premultable = nullptr;
1546 }
1547}
1548
1549/* ***************** Histogram **************** */
1550
1551#define INV_255 (1.0f / 255.0f)
1552
1554{
1555 int bin = int((f * 255.0f) + 0.5f); /* 0.5 to prevent quantization differences */
1556
1557 /* NOTE: clamp integer instead of float to avoid problems with NaN. */
1558 CLAMP(bin, 0, 255);
1559
1560 return bin;
1561}
1562
1563static void save_sample_line(Scopes *scopes,
1564 ColormanageProcessor *cm_processor_colors,
1565 const float3x3 &yuv_matrix,
1566 const int idx,
1567 const float fx,
1568 const float position_rgb[3],
1569 const float position_ycc[3],
1570 const float color_rgb[3])
1571{
1572 const float3 clamped = float3(std::min(position_rgb[0], 1.0f),
1573 std::min(position_rgb[1], 1.0f),
1574 std::min(position_rgb[2], 1.0f));
1575 const float3 yuv = yuv_matrix * clamped;
1576 scopes->vecscope[idx + 0] = yuv.y;
1577 scopes->vecscope[idx + 1] = yuv.z;
1578
1579 int color_idx = (idx / 2) * 3;
1580 float hsv[3];
1581 rgb_to_hsv(color_rgb[0], color_rgb[1], color_rgb[2], &hsv[0], &hsv[1], &hsv[2]);
1582 hsv[1] *= 0.5f;
1583 hsv[2] = 1.0f;
1584 hsv_to_rgb(hsv[0],
1585 hsv[1],
1586 hsv[2],
1587 &scopes->vecscope_rgb[color_idx + 0],
1588 &scopes->vecscope_rgb[color_idx + 1],
1589 &scopes->vecscope_rgb[color_idx + 2]);
1590
1591 /* Compute color in frame-buffer color-space. */
1592 if (cm_processor_colors) {
1593 cm_processor_colors->apply_v3(&scopes->vecscope_rgb[color_idx + 0]);
1594 }
1595
1596 switch (scopes->wavefrm_mode) {
1597 case SCOPES_WAVEFRM_RGB:
1599 scopes->waveform_1[idx + 0] = fx;
1600 scopes->waveform_1[idx + 1] = std::min(position_rgb[0], 1.0f);
1601 scopes->waveform_2[idx + 0] = fx;
1602 scopes->waveform_2[idx + 1] = std::min(position_rgb[1], 1.0f);
1603 scopes->waveform_3[idx + 0] = fx;
1604 scopes->waveform_3[idx + 1] = std::min(position_rgb[2], 1.0f);
1605 break;
1607 scopes->waveform_1[idx + 0] = fx;
1608 scopes->waveform_1[idx + 1] = std::min(position_ycc[0], 1.0f);
1609 break;
1613 scopes->waveform_1[idx + 0] = fx;
1614 scopes->waveform_1[idx + 1] = std::min(position_ycc[0], 1.0f);
1615 scopes->waveform_2[idx + 0] = fx;
1616 scopes->waveform_2[idx + 1] = std::min(position_ycc[1], 1.0f);
1617 scopes->waveform_3[idx + 0] = fx;
1618 scopes->waveform_3[idx + 1] = std::min(position_ycc[2], 1.0f);
1619 break;
1620 }
1621}
1622
1624 ImBuf *ibuf,
1625 const ColorManagedViewSettings *view_settings,
1626 const ColorManagedDisplaySettings *display_settings)
1627{
1628 int i, x, y;
1629
1630 int x1 = roundf(hist->co[0][0] * ibuf->x);
1631 int x2 = roundf(hist->co[1][0] * ibuf->x);
1632 int y1 = roundf(hist->co[0][1] * ibuf->y);
1633 int y2 = roundf(hist->co[1][1] * ibuf->y);
1634
1635 std::optional<ColormanageProcessor> cm_processor;
1636
1637 hist->channels = 3;
1638 hist->x_resolution = 256;
1639 hist->xmax = 1.0f;
1640 // hist->ymax = 1.0f; /* now do this on the operator _only_ */
1641
1642 if (ibuf->byte_data() == nullptr && ibuf->float_data() == nullptr) {
1643 return;
1644 }
1645
1646 if (ibuf->float_data()) {
1648 view_settings, display_settings, DISPLAY_SPACE_SCOPE);
1649 }
1650
1651 for (i = 0; i < 256; i++) {
1652 x = int(0.5f + x1 + float(i) * (x2 - x1) / 255.0f);
1653 y = int(0.5f + y1 + float(i) * (y2 - y1) / 255.0f);
1654
1655 if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y) {
1656 hist->data_luma[i] = hist->data_r[i] = hist->data_g[i] = hist->data_b[i] = hist->data_a[i] =
1657 0.0f;
1658 }
1659 else {
1660 if (float *fp = ibuf->float_data_for_write()) {
1661 float rgba[4];
1662 fp = (fp + (ibuf->channels) * (y * ibuf->x + x));
1663
1664 switch (ibuf->channels) {
1665 case 4:
1666 copy_v4_v4(rgba, fp);
1667 cm_processor->apply_v4(rgba);
1668 break;
1669 case 3:
1670 copy_v3_v3(rgba, fp);
1671 cm_processor->apply_v3(rgba);
1672 rgba[3] = 1.0f;
1673 break;
1674 case 2:
1675 copy_v3_fl(rgba, fp[0]);
1676 rgba[3] = fp[1];
1677 break;
1678 case 1:
1679 copy_v3_fl(rgba, fp[0]);
1680 rgba[3] = 1.0f;
1681 break;
1682 default:
1684 }
1685
1687 hist->data_r[i] = rgba[0];
1688 hist->data_g[i] = rgba[1];
1689 hist->data_b[i] = rgba[2];
1690 hist->data_a[i] = rgba[3];
1691 }
1692 else if (uchar *cp = ibuf->byte_data_for_write()) {
1693 cp = cp + 4 * (y * ibuf->x + x);
1695 hist->data_r[i] = float(cp[0]) / 255.0f;
1696 hist->data_g[i] = float(cp[1]) / 255.0f;
1697 hist->data_b[i] = float(cp[2]) / 255.0f;
1698 hist->data_a[i] = float(cp[3]) / 255.0f;
1699 }
1700 }
1701 }
1702}
1703
1719
1726 float min[3], max[3];
1727};
1728
1729static void scopes_update_cb(void *__restrict userdata,
1730 const int y,
1731 const TaskParallelTLS *__restrict tls)
1732{
1733 const ScopesUpdateData *data = static_cast<const ScopesUpdateData *>(userdata);
1734
1735 Scopes *scopes = data->scopes;
1736 const ImBuf *ibuf = data->ibuf;
1737 ColormanageProcessor *cm_processor_positions = data->cm_processor_positions;
1738 ColormanageProcessor *cm_processor_colors = data->cm_processor_colors;
1739 const int ycc_mode = data->ycc_mode;
1740
1741 ScopesUpdateDataChunk *data_chunk = static_cast<ScopesUpdateDataChunk *>(tls->userdata_chunk);
1742 uint *bin_lum = data_chunk->bin_lum;
1743 uint *bin_r = data_chunk->bin_r;
1744 uint *bin_g = data_chunk->bin_g;
1745 uint *bin_b = data_chunk->bin_b;
1746 uint *bin_a = data_chunk->bin_a;
1747 float *min = data_chunk->min;
1748 float *max = data_chunk->max;
1749
1750 const float *rf = nullptr;
1751 const uchar *rc = nullptr;
1752 const int rows_per_sample_line = ibuf->y / scopes->sample_lines;
1753 const int savedlines = y / rows_per_sample_line;
1754 const bool do_sample_line = (savedlines < scopes->sample_lines) &&
1755 (y % rows_per_sample_line) == 0;
1756 const int channels = (data->float_data) ? ibuf->channels : 4;
1757
1758 if (data->float_data) {
1759 rf = data->float_data + size_t(y) * ibuf->x * channels;
1760 }
1761 else {
1762 rc = data->byte_data + size_t(y) * ibuf->x * channels;
1763 }
1764
1765 for (int x = 0; x < ibuf->x; x++) {
1766 float rgba[4], luma;
1767
1768 if (data->float_data) {
1769 switch (ibuf->channels) {
1770 case 4:
1771 copy_v4_v4(rgba, rf);
1772 break;
1773 case 3:
1774 copy_v3_v3(rgba, rf);
1775 rgba[3] = 1.0f;
1776 break;
1777 case 2:
1778 copy_v3_fl(rgba, rf[0]);
1779 rgba[3] = rf[1];
1780 break;
1781 case 1:
1782 copy_v3_fl(rgba, rf[0]);
1783 rgba[3] = 1.0f;
1784 break;
1785 default:
1787 }
1788 rf += channels;
1789 }
1790 else {
1791 rgba_uchar_to_float(rgba, rc);
1792 rc += channels;
1793 }
1794
1795 /* Compute position in scope colorspace. */
1796 float position_rgba[4];
1797 copy_v4_v4(position_rgba, rgba);
1798 if (cm_processor_positions) {
1799 cm_processor_positions->apply_v4(position_rgba);
1800 }
1801
1802 /* we still need luma for histogram */
1803 luma = math::dot(data->luma_coefficients, float3(position_rgba));
1804
1805 /* check for min max */
1806 float position_ycc[3];
1807 if (ycc_mode == -1) {
1808 minmax_v3v3_v3(min, max, position_rgba);
1809 }
1810 else {
1811 rgb_to_ycc(position_rgba[0],
1812 position_rgba[1],
1813 position_rgba[2],
1814 &position_ycc[0],
1815 &position_ycc[1],
1816 &position_ycc[2],
1817 ycc_mode);
1818 mul_v3_fl(position_ycc, INV_255);
1819 minmax_v3v3_v3(min, max, position_ycc);
1820 }
1821 /* Increment count for histogram. */
1822 bin_lum[get_bin_float(luma)]++;
1823 bin_r[get_bin_float(position_rgba[0])]++;
1824 bin_g[get_bin_float(position_rgba[1])]++;
1825 bin_b[get_bin_float(position_rgba[2])]++;
1826 bin_a[get_bin_float(position_rgba[3])]++;
1827
1828 /* save sample if needed */
1829 if (do_sample_line) {
1830 const float fx = float(x) / float(ibuf->x);
1831 const int idx = 2 * (ibuf->x * savedlines + x);
1832
1833 save_sample_line(scopes,
1834 cm_processor_colors,
1835 data->yuv_matrix,
1836 idx,
1837 fx,
1838 position_rgba,
1839 position_ycc,
1840 rgba);
1841 }
1842 }
1843}
1844
1845static void scopes_update_reduce(const void *__restrict /*userdata*/,
1846 void *__restrict chunk_join,
1847 void *__restrict chunk)
1848{
1849 ScopesUpdateDataChunk *join_chunk = static_cast<ScopesUpdateDataChunk *>(chunk_join);
1850 const ScopesUpdateDataChunk *data_chunk = static_cast<const ScopesUpdateDataChunk *>(chunk);
1851
1852 uint *bin_lum = join_chunk->bin_lum;
1853 uint *bin_r = join_chunk->bin_r;
1854 uint *bin_g = join_chunk->bin_g;
1855 uint *bin_b = join_chunk->bin_b;
1856 uint *bin_a = join_chunk->bin_a;
1857 const uint *bin_lum_c = data_chunk->bin_lum;
1858 const uint *bin_r_c = data_chunk->bin_r;
1859 const uint *bin_g_c = data_chunk->bin_g;
1860 const uint *bin_b_c = data_chunk->bin_b;
1861 const uint *bin_a_c = data_chunk->bin_a;
1862
1863 const float *min = data_chunk->min;
1864 const float *max = data_chunk->max;
1865
1866 for (int b = 256; b--;) {
1867 bin_lum[b] += bin_lum_c[b];
1868 bin_r[b] += bin_r_c[b];
1869 bin_g[b] += bin_g_c[b];
1870 bin_b[b] += bin_b_c[b];
1871 bin_a[b] += bin_a_c[b];
1872 }
1873
1874 for (int c = 3; c--;) {
1875 join_chunk->min[c] = std::min(min[c], join_chunk->min[c]);
1876 join_chunk->max[c] = std::max(max[c], join_chunk->max[c]);
1877 }
1878}
1879
1881 ImBuf *ibuf,
1882 const ColorManagedViewSettings *view_settings,
1883 const ColorManagedDisplaySettings *display_settings)
1884{
1885 int a;
1886 uint nl, na, nr, ng, nb;
1887 double divl, diva, divr, divg, divb;
1888 int ycc_mode = -1;
1889 std::optional<ColormanageProcessor> cm_processor_positions;
1890 std::optional<ColormanageProcessor> cm_processor_colors;
1891
1892 if (!ibuf->byte_data() && !ibuf->float_data()) {
1893 return;
1894 }
1895
1896 if (scopes->ok == 1) {
1897 return;
1898 }
1899
1900 if (scopes->hist.ymax == 0.0f) {
1901 scopes->hist.ymax = 1.0f;
1902 }
1903
1904 /* hmmmm */
1905 if (!ELEM(ibuf->channels, 3, 4)) {
1906 return;
1907 }
1908
1909 scopes->hist.channels = 3;
1910 scopes->hist.x_resolution = 256;
1911
1912 switch (scopes->wavefrm_mode) {
1913 case SCOPES_WAVEFRM_RGB:
1914 /* fall-through */
1916 ycc_mode = -1;
1917 break;
1920 ycc_mode = BLI_YCC_JFIF_0_255;
1921 break;
1923 ycc_mode = BLI_YCC_ITU_BT601;
1924 break;
1926 ycc_mode = BLI_YCC_ITU_BT709;
1927 break;
1928 }
1929
1930 /* convert to number of lines with logarithmic scale */
1931 scopes->sample_lines = (scopes->accuracy * 0.01f) * (scopes->accuracy * 0.01f) * ibuf->y;
1932 CLAMP_MIN(scopes->sample_lines, 1);
1933
1934 if (scopes->sample_full) {
1935 scopes->sample_lines = ibuf->y;
1936 }
1937
1938 /* scan the image */
1939 for (a = 0; a < 3; a++) {
1940 scopes->minmax[a][0] = 25500.0f;
1941 scopes->minmax[a][1] = -25500.0f;
1942 }
1943
1944 scopes->waveform_tot = ibuf->x * scopes->sample_lines;
1945
1946 if (scopes->waveform_1) {
1947 MEM_delete(scopes->waveform_1);
1948 }
1949 if (scopes->waveform_2) {
1950 MEM_delete(scopes->waveform_2);
1951 }
1952 if (scopes->waveform_3) {
1953 MEM_delete(scopes->waveform_3);
1954 }
1955 if (scopes->vecscope) {
1956 MEM_delete(scopes->vecscope);
1957 }
1958 if (scopes->vecscope_rgb) {
1959 MEM_delete(scopes->vecscope_rgb);
1960 }
1961
1962 scopes->waveform_1 = MEM_new_array_zeroed<float>(2 * size_t(scopes->waveform_tot),
1963 "waveform point channel 1");
1964 scopes->waveform_2 = MEM_new_array_zeroed<float>(2 * size_t(scopes->waveform_tot),
1965 "waveform point channel 2");
1966 scopes->waveform_3 = MEM_new_array_zeroed<float>(2 * size_t(scopes->waveform_tot),
1967 "waveform point channel 3");
1968 scopes->vecscope = MEM_new_array_zeroed<float>(2 * size_t(scopes->waveform_tot),
1969 "vectorscope point channel");
1970 scopes->vecscope_rgb = MEM_new_array_zeroed<float>(3 * size_t(scopes->waveform_tot),
1971 "vectorscope color channel");
1972
1974 ibuf, view_settings, display_settings, DISPLAY_SPACE_SCOPE);
1976 ibuf, view_settings, display_settings, DISPLAY_SPACE_DRAW);
1977
1978 const ocio::ScopeInfo scope_info = IMB_colormanagement_get_scope_info(display_settings,
1979 view_settings);
1980
1981 /* Keep number of threads in sync with the merge parts below. */
1983 data.scopes = scopes;
1984 data.ibuf = ibuf;
1985 data.cm_processor_positions = (cm_processor_positions) ? &cm_processor_positions.value() :
1986 nullptr;
1987 data.cm_processor_colors = (cm_processor_colors) ? &cm_processor_colors.value() : nullptr;
1988 data.luma_coefficients = scope_info.luma_coefficients;
1989 data.yuv_matrix = scope_info.yuv_matrix;
1990 data.float_data = ibuf->float_data();
1991 data.byte_data = ibuf->byte_data();
1992 data.ycc_mode = ycc_mode;
1993
1994 ScopesUpdateDataChunk data_chunk = {{0}};
1995 INIT_MINMAX(data_chunk.min, data_chunk.max);
1996
1997 TaskParallelSettings settings;
1999 settings.use_threading = (ibuf->y > 256);
2000 settings.userdata_chunk = &data_chunk;
2001 settings.userdata_chunk_size = sizeof(data_chunk);
2003 BLI_task_parallel_range(0, ibuf->y, &data, scopes_update_cb, &settings);
2004
2005 /* convert hist data to float (proportional to max count) */
2006 nl = na = nr = nb = ng = 0;
2007 for (a = 0; a < 256; a++) {
2008 nl = std::max(data_chunk.bin_lum[a], nl);
2009 nr = std::max(data_chunk.bin_r[a], nr);
2010 ng = std::max(data_chunk.bin_g[a], ng);
2011 nb = std::max(data_chunk.bin_b[a], nb);
2012 na = std::max(data_chunk.bin_a[a], na);
2013 }
2014 divl = nl ? 1.0 / double(nl) : 1.0;
2015 diva = na ? 1.0 / double(na) : 1.0;
2016 divr = nr ? 1.0 / double(nr) : 1.0;
2017 divg = ng ? 1.0 / double(ng) : 1.0;
2018 divb = nb ? 1.0 / double(nb) : 1.0;
2019
2020 for (a = 0; a < 256; a++) {
2021 scopes->hist.data_luma[a] = data_chunk.bin_lum[a] * divl;
2022 scopes->hist.data_r[a] = data_chunk.bin_r[a] * divr;
2023 scopes->hist.data_g[a] = data_chunk.bin_g[a] * divg;
2024 scopes->hist.data_b[a] = data_chunk.bin_b[a] * divb;
2025 scopes->hist.data_a[a] = data_chunk.bin_a[a] * diva;
2026 }
2027
2028 scopes->ok = 1;
2029}
2030
2032{
2033 MEM_SAFE_DELETE(scopes->waveform_1);
2034 MEM_SAFE_DELETE(scopes->waveform_2);
2035 MEM_SAFE_DELETE(scopes->waveform_3);
2036 MEM_SAFE_DELETE(scopes->vecscope);
2037 MEM_SAFE_DELETE(scopes->vecscope_rgb);
2038}
2039
2041{
2042 scopes->accuracy = 30.0;
2043 scopes->hist.mode = HISTO_MODE_RGB;
2044 scopes->wavefrm_alpha = 0.3;
2045 scopes->vecscope_alpha = 0.3;
2046 scopes->wavefrm_height = 100;
2047 scopes->vecscope_height = 100;
2048 scopes->hist.height = 100;
2049 scopes->ok = 0;
2050 scopes->waveform_1 = nullptr;
2051 scopes->waveform_2 = nullptr;
2052 scopes->waveform_3 = nullptr;
2053 scopes->vecscope = nullptr;
2054 scopes->vecscope_rgb = nullptr;
2055}
2056
2058{
2059 const char *display_name = IMB_colormanagement_display_get_default_name();
2060
2061 STRNCPY_UTF8(settings->display_device, display_name);
2063}
2064
2066 const ColorManagedDisplaySettings *settings)
2067{
2068 STRNCPY_UTF8(new_settings->display_device, settings->display_device);
2069 new_settings->emulation = settings->emulation;
2070}
2071
2073 const ColorManagedDisplaySettings *display_settings,
2074 const char *view_transform)
2075{
2077 display_settings->display_device);
2078 BLI_assert(display);
2079
2080 if (!view_transform) {
2082 }
2083
2084 /* TODO(sergey): Find a way to make look query more reliable with non
2085 * default configuration. */
2086 STRNCPY_UTF8(view_settings->view_transform, view_transform);
2087 STRNCPY_UTF8(view_settings->look, "None");
2088
2089 view_settings->flag = eColorManageView_Flag{};
2090 view_settings->gamma = 1.0f;
2091 view_settings->exposure = 0.0f;
2092 view_settings->temperature = 6500.0f;
2093 view_settings->tint = 10.0f;
2094 view_settings->curve_mapping = nullptr;
2095
2096 IMB_colormanagement_validate_settings(display_settings, view_settings);
2097}
2098
2100 const ColorManagedViewSettings *settings)
2101{
2103
2104 if (settings->curve_mapping) {
2105 new_settings->curve_mapping = BKE_curvemapping_copy(settings->curve_mapping);
2106 }
2107 else {
2108 new_settings->curve_mapping = nullptr;
2109 }
2110}
2111
2113 ColorManagedViewSettings *new_settings, const ColorManagedViewSettings *settings)
2114{
2115 STRNCPY_UTF8(new_settings->look, settings->look);
2116 STRNCPY_UTF8(new_settings->view_transform, settings->view_transform);
2117
2118 new_settings->flag = settings->flag;
2119 new_settings->exposure = settings->exposure;
2120 new_settings->gamma = settings->gamma;
2121 new_settings->temperature = settings->temperature;
2122 new_settings->tint = settings->tint;
2123}
2124
2126{
2127 if (settings->curve_mapping) {
2129 settings->curve_mapping = nullptr;
2130 }
2131}
2132
2134 const ColorManagedViewSettings *settings)
2135{
2136 if (settings->curve_mapping) {
2138 }
2139}
2140
2142 ColorManagedViewSettings *settings)
2143{
2144 BLO_read_struct(reader, CurveMapping, &settings->curve_mapping);
2145
2146 if (settings->curve_mapping) {
2147 BKE_curvemapping_blend_read(reader, settings->curve_mapping);
2148 }
2149}
2150
2152 ColorManagedColorspaceSettings *colorspace_settings)
2153{
2154 STRNCPY_UTF8(colorspace_settings->name, "");
2155 STRNCPY_UTF8(colorspace_settings->interop_id, "");
2156}
2157
2159 ColorManagedColorspaceSettings *colorspace_settings,
2160 const ColorManagedColorspaceSettings *settings)
2161{
2162 STRNCPY_UTF8(colorspace_settings->name, settings->name);
2163 STRNCPY_UTF8(colorspace_settings->interop_id, settings->interop_id);
2164}
2165
2167 const ColorManagedColorspaceSettings *settings2)
2168{
2169 return STREQ(settings1->name, settings2->name);
2170}
2171
2172} // namespace blender
#define BLI_YCC_JFIF_0_255
#define BLI_YCC_ITU_BT601
#define BLI_YCC_ITU_BT709
#define STRNCPY_UTF8(dst, src)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define CM_TOT
#define CM_TABLEDIV
#define CM_RESOL
#define CM_TABLE
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define INIT_MINMAX(min, max)
#define BLI_assert_unreachable()
#define BLI_INLINE
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define CLAMP_MIN(a, b)
btScalar max
#define output
static ColormanageProcessor display_processor_new(const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, ColorManagedDisplaySpace display_space=DISPLAY_SPACE_DRAW, bool inverse=false, const char *from_colorspace=nullptr)
void apply_v3(float pixel[3]) const
void apply_v4(float pixel[4]) const
static std::optional< ColormanageProcessor > display_processor_for_imbuf(const ImBuf *ibuf, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, ColorManagedDisplaySpace display_space=DISPLAY_SPACE_DRAW)
constexpr IndexRange()=default
#define INV_255
#define p2_h1
#define p2_h2
#define roundf(x)
#define FLT_EPSILON
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
VecBase< float, 2 > float2
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
T reduce_max(const VecBase< T, Size > &a)
T reduce_min(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
BMesh Iterators.
MINLINE float min_ff(float a, float b)
CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition colortools.cc:99
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void save_sample_line(Scopes *scopes, ColormanageProcessor *cm_processor_colors, const float3x3 &yuv_matrix, const int idx, const float fx, const float position_rgb[3], const float position_ycc[3], const float color_rgb[3])
CurveMapping * BKE_curvemapping_copy(const CurveMapping *cumap)
void BKE_curvemap_remove(CurveMap *cuma, short flag)
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
void BKE_curvemapping_evaluate3F(const CurveMapping *cumap, float vecout[3], const float vecin[3])
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
Definition math_color.cc:25
void BKE_curvemapping_evaluateRGBF(const CurveMapping *cumap, float vecout[3], const float vecin[3])
static float3 evaluate_film_like(const CurveMapping *curve_mapping, float3 input)
const char * IMB_colormanagement_display_get_default_view_transform_name(const ColorManagedDisplay *display)
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
bool BKE_curvemap_remove_point(CurveMap *cuma, CurveMapPoint *point)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
static void calchandle_curvemap(BezTriple *bezt, const BezTriple *prev, const BezTriple *next)
void BKE_color_managed_view_settings_copy_keep_curve_mapping(ColorManagedViewSettings *new_settings, const ColorManagedViewSettings *settings)
@ CURVE_TONE_STANDARD
@ CURVE_TONE_FILMLIKE
void BKE_curvemapping_copy_data(CurveMapping *target, const CurveMapping *cumap)
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)
float BKE_curvemap_evaluateF(const CurveMapping *cumap, const CurveMap *cuma, float value)
void BKE_curvemapping_blend_write(BlendWriter *writer, const CurveMapping *cumap)
void BKE_curvemapping_curves_blend_write(BlendWriter *writer, const CurveMapping *cumap)
@ SCOPES_WAVEFRM_LUMA
@ SCOPES_WAVEFRM_YCC_601
@ SCOPES_WAVEFRM_YCC_709
@ SCOPES_WAVEFRM_RGB_PARADE
@ SCOPES_WAVEFRM_YCC_JPEG
BLI_INLINE unsigned char IMB_colormanagement_get_luminance_byte(const unsigned char rgb[3])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
void BKE_curvemapping_copy_data_single(CurveMapping *target, const CurveMapping *cumap, int to_idx, int from_idx, bool make_copy)
void BKE_color_managed_display_settings_init(ColorManagedDisplaySettings *settings)
@ CURVE_PRESET_SMOOTH
@ CURVE_PRESET_CONSTANT_MEDIAN
MINLINE int max_ii(int a, int b)
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
void BKE_color_managed_view_settings_blend_read_data(BlendDataReader *reader, ColorManagedViewSettings *settings)
void BKE_curvemapping_premultiply(CurveMapping *cumap, bool restore)
void BKE_curvemapping_free(CurveMapping *cumap)
void BKE_color_managed_view_settings_copy(ColorManagedViewSettings *new_settings, const ColorManagedViewSettings *settings)
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void mul_v2_v2(float r[2], const float a[2])
void BKE_curvemap_activate_nearest_point(struct CurveMap *cuma, const int i_last)
static void curvemap_make_table(const CurveMapping *cumap, CurveMap *cuma)
MINLINE void sub_v3_v3(float r[3], const float a[3])
void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
void BKE_curvemapping_init(CurveMapping *cumap)
void BKE_curvemapping_evaluate_premulRGB(const CurveMapping *cumap, unsigned char vecout_byte[3], const unsigned char vecin_byte[3])
void BKE_curvemapping_blend_read(BlendDataReader *reader, CurveMapping *cumap)
BLI_INLINE int get_bin_float(float f)
void BKE_curvemapping_free_data_single(CurveMapping *cumap, int index)
static void curve_eval_bezier_point(float start[3][3], float end[3][3], float *point)
@ CUMA_HANDLE_AUTO_ANIM
unsigned char uchar
Definition BKE_text.h:58
void BKE_color_managed_display_settings_copy(ColorManagedDisplaySettings *new_settings, const ColorManagedDisplaySettings *settings)
ocio::ScopeInfo IMB_colormanagement_get_scope_info(const ColorManagedDisplaySettings *display_settings, const ColorManagedViewSettings *view_settings)
BMesh const char void * data
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
void BKE_curvemap_reset(CurveMap *cuma, const rctf *clipr, int preset, CurveMapSlopeType slope)
void IMB_colormanagement_validate_settings(const ColorManagedDisplaySettings *display_settings, ColorManagedViewSettings *view_settings)
void BKE_curvemapping_changed(CurveMapping *cumap, bool rem_doubles)
static void scopes_update_reduce(const void *__restrict, void *__restrict chunk_join, void *__restrict chunk)
void BKE_curvemapping_reset_view(CurveMapping *cumap)
void BKE_curvemapping_set_black_white_ex(const float black[3], const float white[3], float r_bwmul[3])
void BKE_curve_correct_bezpart(const float v1[2], float v2[2], float v3[2], const float v4[2])
Definition curve.cc:5464
void BKE_curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
CurveMapPoint * BKE_curvemap_active_get(CurveMap *cuma)
void BKE_scopes_free(Scopes *scopes)
int BKE_curvemapping_num_channels(const CurveMapping *cumap)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
static void scopes_update_cb(void *__restrict userdata, const int y, const TaskParallelTLS *__restrict tls)
void BKE_color_managed_colorspace_settings_init(ColorManagedColorspaceSettings *colorspace_settings)
void BKE_curvemapping_compute_slopes(const CurveMapping *curve_mapping, float start_slopes[4], float end_slopes[4])
MatBase< float, 3, 3 > float3x3
void BKE_curvemapping_changed_all(CurveMapping *cumap)
void BKE_curvemapping_set_black_white(CurveMapping *cumap, const float black[3], const float white[3])
void BKE_curvemapping_set_defaults(CurveMapping *cumap, int tot, float minx, float miny, float maxx, float maxy, eBezTriple_Handle default_handle_type)
Definition colortools.cc:47
void BKE_curvemap_translate_selection(CurveMap *cuma, const blender::float2 &offset)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
void BKE_curvemapping_evaluate_premulRGBF(const CurveMapping *cumap, float vecout[3], const float vecin[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void BKE_curvemapping_free_data(CurveMapping *cumap)
void BKE_scopes_new(Scopes *scopes)
MINLINE void copy_v3_fl(float r[3], float f)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
MINLINE float max_ff(float a, float b)
const char * IMB_colormanagement_display_get_default_name()
void BKE_scopes_update(Scopes *scopes, ImBuf *ibuf, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings)
bool BKE_color_managed_colorspace_settings_equals(const ColorManagedColorspaceSettings *settings1, const ColorManagedColorspaceSettings *settings2)
CurveMapPoint * BKE_curvemap_insert(CurveMap *cuma, float x, float y)
void BKE_curvemapping_compute_range_dividers(const CurveMapping *curve_mapping, float dividers[4])
bool BKE_curvemapping_is_map_identity(const CurveMapping *curve_mapping, int index)
void BKE_color_managed_view_settings_free(ColorManagedViewSettings *settings)
@ COLORMANAGE_DISPLAY_EMULATION_AUTO
const ColorManagedDisplay * IMB_colormanagement_display_get_named(const char *name)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void BKE_curvemap_handle_set(CurveMap *cuma, int type)
void BKE_color_managed_view_settings_blend_write(BlendWriter *writer, const ColorManagedViewSettings *settings)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
VecBase< float, 3 > float3
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1696
static float curvemap_calc_extend(const CurveMapping *cumap, const CurveMap *cuma, float x, const float first[2], const float last[2])
void BKE_curvemapping_get_range_minimums(const CurveMapping *curve_mapping, float minimums[4])
MINLINE void zero_v2(float r[2])
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
void BKE_histogram_update_sample_line(Histogram *hist, ImBuf *ibuf, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings)
bool BKE_curvemapping_RGBA_does_something(const CurveMapping *cumap)
MINLINE int compare_ff(float a, float b, float max_diff)
void BKE_curvemapping_table_F(const CurveMapping *cumap, float **array, int *size)
void BKE_curvemapping_evaluate_premulRGBF_ex(const CurveMapping *cumap, float vecout[3], const float vecin[3], const float black[3], const float bwmul[3])
ocio::Display ColorManagedDisplay
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
Definition rct.cc:418
MINLINE void madd_v2_v2v2fl(float r[2], const float a[2], const float b[2], float f)
@ CUMA_EXTEND_EXTRAPOLATE
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
eBezTriple_Handle h2
eBezTriple_Handle h1
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
eColorManageDisplay_Emulation emulation
struct CurveMapping * curve_mapping
eCurveMapPoint_Flag flag
CurveMapPoint * curve
eCurveMapPoint_Flag default_handle_type
CurveMapPoint * table
CurveMapPoint * premultable
eCurveMappingTone tone
eCurveMappingFlags flag
eHistogram_Mode mode
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
float * float_data_for_write()
Definition allocimbuf.cc:79
ColormanageProcessor * cm_processor_colors
ColormanageProcessor * cm_processor_positions
eScopes_WaveformMode wavefrm_mode
struct Histogram hist
TaskParallelReduceFunc func_reduce
blender::float3x3 yuv_matrix
Definition OCIO_scope.hh:45
blender::float3 luma_coefficients
Definition OCIO_scope.hh:43