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

sequencer_scopes.c « space_sequencer « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ba1dcc5eb8f20c511d4e2af203db04712d37e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2006-2008 Peter Schlaile < peter [at] schlaile [dot] de >. */

/** \file
 * \ingroup spseq
 */

#include <math.h>
#include <string.h>

#include "BLI_task.h"
#include "BLI_utildefines.h"

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

#include "sequencer_intern.h"

/* XXX(campbell): why is this function better than BLI_math version?
 * only difference is it does some normalize after, need to double check on this. */
static void rgb_to_yuv_normalized(const float rgb[3], float yuv[3])
{
  yuv[0] = 0.299f * rgb[0] + 0.587f * rgb[1] + 0.114f * rgb[2];
  yuv[1] = 0.492f * (rgb[2] - yuv[0]);
  yuv[2] = 0.877f * (rgb[0] - yuv[0]);

  /* Normalize. */
  yuv[1] *= 255.0f / (122 * 2.0f);
  yuv[1] += 0.5f;

  yuv[2] *= 255.0f / (157 * 2.0f);
  yuv[2] += 0.5f;
}

static void scope_put_pixel(const uchar *table, uchar *pos)
{
  uchar newval = table[*pos];
  pos[0] = pos[1] = pos[2] = newval;
  pos[3] = 255;
}

static void scope_put_pixel_single(const uchar *table, uchar *pos, int col)
{
  char newval = table[pos[col]];
  pos[col] = newval;
  pos[3] = 255;
}

static void wform_put_line(int w, uchar *last_pos, uchar *new_pos)
{
  if (last_pos > new_pos) {
    uchar *temp = new_pos;
    new_pos = last_pos;
    last_pos = temp;
  }

  while (last_pos < new_pos) {
    if (last_pos[0] == 0) {
      last_pos[0] = last_pos[1] = last_pos[2] = 32;
      last_pos[3] = 255;
    }
    last_pos += 4 * w;
  }
}

static void wform_put_line_single(int w, uchar *last_pos, uchar *new_pos, int col)
{
  if (last_pos > new_pos) {
    uchar *temp = new_pos;
    new_pos = last_pos;
    last_pos = temp;
  }

  while (last_pos < new_pos) {
    if (last_pos[col] == 0) {
      last_pos[col] = 32;
      last_pos[3] = 255;
    }
    last_pos += 4 * w;
  }
}

static void wform_put_border(uchar *tgt, int w, int h)
{
  int x, y;

  for (x = 0; x < w; x++) {
    uchar *p = tgt + 4 * x;
    p[1] = p[3] = 155;
    p[4 * w + 1] = p[4 * w + 3] = 155;
    p = tgt + 4 * (w * (h - 1) + x);
    p[1] = p[3] = 155;
    p[-4 * w + 1] = p[-4 * w + 3] = 155;
  }

  for (y = 0; y < h; y++) {
    uchar *p = tgt + 4 * w * y;
    p[1] = p[3] = 155;
    p[4 + 1] = p[4 + 3] = 155;
    p = tgt + 4 * (w * y + w - 1);
    p[1] = p[3] = 155;
    p[-4 + 1] = p[-4 + 3] = 155;
  }
}

static void wform_put_gridrow(uchar *tgt, float perc, int w, int h)
{
  tgt += (int)(perc / 100.0f * h) * w * 4;

  for (int i = 0; i < w * 2; i++) {
    tgt[0] = 255;

    tgt += 4;
  }
}

static void wform_put_grid(uchar *tgt, int w, int h)
{
  wform_put_gridrow(tgt, 90.0, w, h);
  wform_put_gridrow(tgt, 70.0, w, h);
  wform_put_gridrow(tgt, 10.0, w, h);
}

static ImBuf *make_waveform_view_from_ibuf_byte(ImBuf *ibuf)
{
  ImBuf *rval = IMB_allocImBuf(ibuf->x + 3, 515, 32, IB_rect);
  int x, y;
  const uchar *src = (uchar *)ibuf->rect;
  uchar *tgt = (uchar *)rval->rect;
  int w = ibuf->x + 3;
  int h = 515;
  float waveform_gamma = 0.2;
  uchar wtable[256];

  wform_put_grid(tgt, w, h);
  wform_put_border(tgt, w, h);

  for (x = 0; x < 256; x++) {
    wtable[x] = (uchar)(pow(((float)x + 1) / 256, waveform_gamma) * 255);
  }

  for (y = 0; y < ibuf->y; y++) {
    uchar *last_p = NULL;

    for (x = 0; x < ibuf->x; x++) {
      const uchar *rgb = src + 4 * (ibuf->x * y + x);
      float v = (float)IMB_colormanagement_get_luminance_byte(rgb) / 255.0f;
      uchar *p = tgt;
      p += 4 * (w * ((int)(v * (h - 3)) + 1) + x + 1);

      scope_put_pixel(wtable, p);
      p += 4 * w;
      scope_put_pixel(wtable, p);

      if (last_p != NULL) {
        wform_put_line(w, last_p, p);
      }
      last_p = p;
    }
  }

  return rval;
}

static ImBuf *make_waveform_view_from_ibuf_float(ImBuf *ibuf)
{
  ImBuf *rval = IMB_allocImBuf(ibuf->x + 3, 515, 32, IB_rect);
  int x, y;
  const float *src = ibuf->rect_float;
  uchar *tgt = (uchar *)rval->rect;
  int w = ibuf->x + 3;
  int h = 515;
  float waveform_gamma = 0.2;
  uchar wtable[256];

  wform_put_grid(tgt, w, h);

  for (x = 0; x < 256; x++) {
    wtable[x] = (uchar)(pow(((float)x + 1) / 256, waveform_gamma) * 255);
  }

  for (y = 0; y < ibuf->y; y++) {
    uchar *last_p = NULL;

    for (x = 0; x < ibuf->x; x++) {
      const float *rgb = src + 4 * (ibuf->x * y + x);
      float v = IMB_colormanagement_get_luminance(rgb);
      uchar *p = tgt;

      CLAMP(v, 0.0f, 1.0f);

      p += 4 * (w * ((int)(v * (h - 3)) + 1) + x + 1);

      scope_put_pixel(wtable, p);
      p += 4 * w;
      scope_put_pixel(wtable, p);

      if (last_p != NULL) {
        wform_put_line(w, last_p, p);
      }
      last_p = p;
    }
  }

  wform_put_border(tgt, w, h);

  return rval;
}

ImBuf *make_waveform_view_from_ibuf(ImBuf *ibuf)
{
  if (ibuf->rect_float) {
    return make_waveform_view_from_ibuf_float(ibuf);
  }
  return make_waveform_view_from_ibuf_byte(ibuf);
}

static ImBuf *make_sep_waveform_view_from_ibuf_byte(ImBuf *ibuf)
{
  ImBuf *rval = IMB_allocImBuf(ibuf->x + 3, 515, 32, IB_rect);
  int x, y;
  const uchar *src = (const uchar *)ibuf->rect;
  uchar *tgt = (uchar *)rval->rect;
  int w = ibuf->x + 3;
  int sw = ibuf->x / 3;
  int h = 515;
  float waveform_gamma = 0.2;
  uchar wtable[256];

  wform_put_grid(tgt, w, h);

  for (x = 0; x < 256; x++) {
    wtable[x] = (uchar)(pow(((float)x + 1) / 256, waveform_gamma) * 255);
  }

  for (y = 0; y < ibuf->y; y++) {
    uchar *last_p[3] = {NULL, NULL, NULL};

    for (x = 0; x < ibuf->x; x++) {
      int c;
      const uchar *rgb = src + 4 * (ibuf->x * y + x);
      for (c = 0; c < 3; c++) {
        uchar *p = tgt;
        p += 4 * (w * ((rgb[c] * (h - 3)) / 255 + 1) + c * sw + x / 3 + 1);

        scope_put_pixel_single(wtable, p, c);
        p += 4 * w;
        scope_put_pixel_single(wtable, p, c);

        if (last_p[c] != NULL) {
          wform_put_line_single(w, last_p[c], p, c);
        }
        last_p[c] = p;
      }
    }
  }

  wform_put_border(tgt, w, h);

  return rval;
}

static ImBuf *make_sep_waveform_view_from_ibuf_float(ImBuf *ibuf)
{
  ImBuf *rval = IMB_allocImBuf(ibuf->x + 3, 515, 32, IB_rect);
  int x, y;
  const float *src = ibuf->rect_float;
  uchar *tgt = (uchar *)rval->rect;
  int w = ibuf->x + 3;
  int sw = ibuf->x / 3;
  int h = 515;
  float waveform_gamma = 0.2;
  uchar wtable[256];

  wform_put_grid(tgt, w, h);

  for (x = 0; x < 256; x++) {
    wtable[x] = (uchar)(pow(((float)x + 1) / 256, waveform_gamma) * 255);
  }

  for (y = 0; y < ibuf->y; y++) {
    uchar *last_p[3] = {NULL, NULL, NULL};

    for (x = 0; x < ibuf->x; x++) {
      int c;
      const float *rgb = src + 4 * (ibuf->x * y + x);
      for (c = 0; c < 3; c++) {
        uchar *p = tgt;
        float v = rgb[c];

        CLAMP(v, 0.0f, 1.0f);

        p += 4 * (w * ((int)(v * (h - 3)) + 1) + c * sw + x / 3 + 1);

        scope_put_pixel_single(wtable, p, c);
        p += 4 * w;
        scope_put_pixel_single(wtable, p, c);

        if (last_p[c] != NULL) {
          wform_put_line_single(w, last_p[c], p, c);
        }
        last_p[c] = p;
      }
    }
  }

  wform_put_border(tgt, w, h);

  return rval;
}

ImBuf *make_sep_waveform_view_from_ibuf(ImBuf *ibuf)
{
  if (ibuf->rect_float) {
    return make_sep_waveform_view_from_ibuf_float(ibuf);
  }
  return make_sep_waveform_view_from_ibuf_byte(ibuf);
}

static void draw_zebra_byte(ImBuf *src, ImBuf *ibuf, float perc)
{
  uint limit = 255.0f * perc / 100.0f;
  uchar *p = (uchar *)src->rect;
  uchar *o = (uchar *)ibuf->rect;
  int x;
  int y;

  for (y = 0; y < ibuf->y; y++) {
    for (x = 0; x < ibuf->x; x++) {
      uchar r = *p++;
      uchar g = *p++;
      uchar b = *p++;
      uchar a = *p++;

      if (r >= limit || g >= limit || b >= limit) {
        if (((x + y) & 0x08) != 0) {
          r = 255 - r;
          g = 255 - g;
          b = 255 - b;
        }
      }
      *o++ = r;
      *o++ = g;
      *o++ = b;
      *o++ = a;
    }
  }
}

static void draw_zebra_float(ImBuf *src, ImBuf *ibuf, float perc)
{
  float limit = perc / 100.0f;
  const float *p = src->rect_float;
  uchar *o = (uchar *)ibuf->rect;
  int x;
  int y;

  for (y = 0; y < ibuf->y; y++) {
    for (x = 0; x < ibuf->x; x++) {
      float r = *p++;
      float g = *p++;
      float b = *p++;
      float a = *p++;

      if (r >= limit || g >= limit || b >= limit) {
        if (((x + y) & 0x08) != 0) {
          r = -r;
          g = -g;
          b = -b;
        }
      }

      *o++ = unit_float_to_uchar_clamp(r);
      *o++ = unit_float_to_uchar_clamp(g);
      *o++ = unit_float_to_uchar_clamp(b);
      *o++ = unit_float_to_uchar_clamp(a);
    }
  }
}

ImBuf *make_zebra_view_from_ibuf(ImBuf *ibuf, float perc)
{
  ImBuf *new_ibuf = IMB_allocImBuf(ibuf->x, ibuf->y, 32, IB_rect);

  if (ibuf->rect_float) {
    draw_zebra_float(ibuf, new_ibuf, perc);
  }
  else {
    draw_zebra_byte(ibuf, new_ibuf, perc);
  }
  return new_ibuf;
}

static void draw_histogram_marker(ImBuf *ibuf, int x)
{
  uchar *p = (uchar *)ibuf->rect;
  int barh = ibuf->y * 0.1;

  p += 4 * (x + ibuf->x * (ibuf->y - barh + 1));

  for (int i = 0; i < barh - 1; i++) {
    p[0] = p[1] = p[2] = 255;
    p += ibuf->x * 4;
  }
}

static void draw_histogram_bar(ImBuf *ibuf, int x, float val, int col)
{
  uchar *p = (uchar *)ibuf->rect;
  int barh = ibuf->y * val * 0.9f;

  p += 4 * (x + ibuf->x);

  for (int i = 0; i < barh; i++) {
    p[col] = 255;
    p += ibuf->x * 4;
  }
}

#define HIS_STEPS 512

typedef struct MakeHistogramViewData {
  const ImBuf *ibuf;
} MakeHistogramViewData;

static void make_histogram_view_from_ibuf_byte_fn(void *__restrict userdata,
                                                  const int y,
                                                  const TaskParallelTLS *__restrict tls)
{
  MakeHistogramViewData *data = userdata;
  const ImBuf *ibuf = data->ibuf;
  const uchar *src = (uchar *)ibuf->rect;

  uint32_t(*cur_bins)[HIS_STEPS] = tls->userdata_chunk;

  for (int x = 0; x < ibuf->x; x++) {
    const uchar *pixel = src + (y * ibuf->x + x) * 4;

    for (int j = 3; j--;) {
      cur_bins[j][pixel[j]]++;
    }
  }
}

static void make_histogram_view_from_ibuf_reduce(const void *__restrict UNUSED(userdata),
                                                 void *__restrict chunk_join,
                                                 void *__restrict chunk)
{
  uint32_t(*join_bins)[HIS_STEPS] = chunk_join;
  uint32_t(*bins)[HIS_STEPS] = chunk;

  for (int j = 3; j--;) {
    for (int i = 0; i < HIS_STEPS; i++) {
      join_bins[j][i] += bins[j][i];
    }
  }
}

static ImBuf *make_histogram_view_from_ibuf_byte(ImBuf *ibuf)
{
  ImBuf *rval = IMB_allocImBuf(515, 128, 32, IB_rect);
  int x;
  uint nr, ng, nb;

  uint bins[3][HIS_STEPS];

  memset(bins, 0, sizeof(bins));

  MakeHistogramViewData data = {
      .ibuf = ibuf,
  };
  TaskParallelSettings settings;
  BLI_parallel_range_settings_defaults(&settings);
  settings.use_threading = (ibuf->y >= 256);
  settings.userdata_chunk = bins;
  settings.userdata_chunk_size = sizeof(bins);
  settings.func_reduce = make_histogram_view_from_ibuf_reduce;
  BLI_task_parallel_range(0, ibuf->y, &data, make_histogram_view_from_ibuf_byte_fn, &settings);

  nr = nb = ng = 0;
  for (x = 0; x < HIS_STEPS; x++) {
    if (bins[0][x] > nr) {
      nr = bins[0][x];
    }
    if (bins[1][x] > ng) {
      ng = bins[1][x];
    }
    if (bins[2][x] > nb) {
      nb = bins[2][x];
    }
  }

  for (x = 0; x < HIS_STEPS; x++) {
    if (nr) {
      draw_histogram_bar(rval, x * 2 + 1, ((float)bins[0][x]) / nr, 0);
      draw_histogram_bar(rval, x * 2 + 2, ((float)bins[0][x]) / nr, 0);
    }
    if (ng) {
      draw_histogram_bar(rval, x * 2 + 1, ((float)bins[1][x]) / ng, 1);
      draw_histogram_bar(rval, x * 2 + 2, ((float)bins[1][x]) / ng, 1);
    }
    if (nb) {
      draw_histogram_bar(rval, x * 2 + 1, ((float)bins[2][x]) / nb, 2);
      draw_histogram_bar(rval, x * 2 + 2, ((float)bins[2][x]) / nb, 2);
    }
  }

  wform_put_border((uchar *)rval->rect, rval->x, rval->y);

  return rval;
}

BLI_INLINE int get_bin_float(float f)
{
  if (f < -0.25f) {
    return 0;
  }
  if (f >= 1.25f) {
    return 511;
  }

  return (int)(((f + 0.25f) / 1.5f) * 512);
}

static void make_histogram_view_from_ibuf_float_fn(void *__restrict userdata,
                                                   const int y,
                                                   const TaskParallelTLS *__restrict tls)
{
  const MakeHistogramViewData *data = userdata;
  const ImBuf *ibuf = data->ibuf;
  const float *src = ibuf->rect_float;

  uint32_t(*cur_bins)[HIS_STEPS] = tls->userdata_chunk;

  for (int x = 0; x < ibuf->x; x++) {
    const float *pixel = src + (y * ibuf->x + x) * 4;

    for (int j = 3; j--;) {
      cur_bins[j][get_bin_float(pixel[j])]++;
    }
  }
}

static ImBuf *make_histogram_view_from_ibuf_float(ImBuf *ibuf)
{
  ImBuf *rval = IMB_allocImBuf(515, 128, 32, IB_rect);
  int nr, ng, nb;
  int x;

  uint bins[3][HIS_STEPS];

  memset(bins, 0, sizeof(bins));

  MakeHistogramViewData data = {
      .ibuf = ibuf,
  };
  TaskParallelSettings settings;
  BLI_parallel_range_settings_defaults(&settings);
  settings.use_threading = (ibuf->y >= 256);
  settings.userdata_chunk = bins;
  settings.userdata_chunk_size = sizeof(bins);
  settings.func_reduce = make_histogram_view_from_ibuf_reduce;
  BLI_task_parallel_range(0, ibuf->y, &data, make_histogram_view_from_ibuf_float_fn, &settings);

  nr = nb = ng = 0;
  for (x = 0; x < HIS_STEPS; x++) {
    if (bins[0][x] > nr) {
      nr = bins[0][x];
    }
    if (bins[1][x] > ng) {
      ng = bins[1][x];
    }
    if (bins[2][x] > nb) {
      nb = bins[2][x];
    }
  }

  for (x = 0; x < HIS_STEPS; x++) {
    if (nr) {
      draw_histogram_bar(rval, x + 1, ((float)bins[0][x]) / nr, 0);
    }
    if (ng) {
      draw_histogram_bar(rval, x + 1, ((float)bins[1][x]) / ng, 1);
    }
    if (nb) {
      draw_histogram_bar(rval, x + 1, ((float)bins[2][x]) / nb, 2);
    }
  }

  draw_histogram_marker(rval, get_bin_float(0.0));
  draw_histogram_marker(rval, get_bin_float(1.0));
  wform_put_border((uchar *)rval->rect, rval->x, rval->y);

  return rval;
}

#undef HIS_STEPS

ImBuf *make_histogram_view_from_ibuf(ImBuf *ibuf)
{
  if (ibuf->rect_float) {
    return make_histogram_view_from_ibuf_float(ibuf);
  }
  return make_histogram_view_from_ibuf_byte(ibuf);
}

static void vectorscope_put_cross(uchar r, uchar g, uchar b, char *tgt, int w, int h, int size)
{
  float rgb[3], yuv[3];
  char *p;

  rgb[0] = (float)r / 255.0f;
  rgb[1] = (float)g / 255.0f;
  rgb[2] = (float)b / 255.0f;
  rgb_to_yuv_normalized(rgb, yuv);

  p = tgt + 4 * (w * (int)((yuv[2] * (h - 3) + 1)) + (int)((yuv[1] * (w - 3) + 1)));

  if (r == 0 && g == 0 && b == 0) {
    r = 255;
  }

  for (int y = -size; y <= size; y++) {
    for (int x = -size; x <= size; x++) {
      char *q = p + 4 * (y * w + x);
      q[0] = r;
      q[1] = g;
      q[2] = b;
      q[3] = 255;
    }
  }
}

static ImBuf *make_vectorscope_view_from_ibuf_byte(ImBuf *ibuf)
{
  ImBuf *rval = IMB_allocImBuf(515, 515, 32, IB_rect);
  int x, y;
  const char *src = (const char *)ibuf->rect;
  char *tgt = (char *)rval->rect;
  float rgb[3], yuv[3];
  int w = 515;
  int h = 515;
  float scope_gamma = 0.2;
  uchar wtable[256];

  for (x = 0; x < 256; x++) {
    wtable[x] = (uchar)(pow(((float)x + 1) / 256, scope_gamma) * 255);
  }

  for (x = 0; x < 256; x++) {
    vectorscope_put_cross(255, 0, 255 - x, tgt, w, h, 1);
    vectorscope_put_cross(255, x, 0, tgt, w, h, 1);
    vectorscope_put_cross(255 - x, 255, 0, tgt, w, h, 1);
    vectorscope_put_cross(0, 255, x, tgt, w, h, 1);
    vectorscope_put_cross(0, 255 - x, 255, tgt, w, h, 1);
    vectorscope_put_cross(x, 0, 255, tgt, w, h, 1);
  }

  for (y = 0; y < ibuf->y; y++) {
    for (x = 0; x < ibuf->x; x++) {
      const char *src1 = src + 4 * (ibuf->x * y + x);
      char *p;

      rgb[0] = (float)src1[0] / 255.0f;
      rgb[1] = (float)src1[1] / 255.0f;
      rgb[2] = (float)src1[2] / 255.0f;
      rgb_to_yuv_normalized(rgb, yuv);

      p = tgt + 4 * (w * (int)((yuv[2] * (h - 3) + 1)) + (int)((yuv[1] * (w - 3) + 1)));
      scope_put_pixel(wtable, (uchar *)p);
    }
  }

  vectorscope_put_cross(0, 0, 0, tgt, w, h, 3);

  return rval;
}

static ImBuf *make_vectorscope_view_from_ibuf_float(ImBuf *ibuf)
{
  ImBuf *rval = IMB_allocImBuf(515, 515, 32, IB_rect);
  int x, y;
  const float *src = ibuf->rect_float;
  char *tgt = (char *)rval->rect;
  float rgb[3], yuv[3];
  int w = 515;
  int h = 515;
  float scope_gamma = 0.2;
  uchar wtable[256];

  for (x = 0; x < 256; x++) {
    wtable[x] = (uchar)(pow(((float)x + 1) / 256, scope_gamma) * 255);
  }

  for (x = 0; x <= 255; x++) {
    vectorscope_put_cross(255, 0, 255 - x, tgt, w, h, 1);
    vectorscope_put_cross(255, x, 0, tgt, w, h, 1);
    vectorscope_put_cross(255 - x, 255, 0, tgt, w, h, 1);
    vectorscope_put_cross(0, 255, x, tgt, w, h, 1);
    vectorscope_put_cross(0, 255 - x, 255, tgt, w, h, 1);
    vectorscope_put_cross(x, 0, 255, tgt, w, h, 1);
  }

  for (y = 0; y < ibuf->y; y++) {
    for (x = 0; x < ibuf->x; x++) {
      const float *src1 = src + 4 * (ibuf->x * y + x);
      const char *p;

      memcpy(rgb, src1, sizeof(float[3]));

      clamp_v3(rgb, 0.0f, 1.0f);

      rgb_to_yuv_normalized(rgb, yuv);

      p = tgt + 4 * (w * (int)((yuv[2] * (h - 3) + 1)) + (int)((yuv[1] * (w - 3) + 1)));
      scope_put_pixel(wtable, (uchar *)p);
    }
  }

  vectorscope_put_cross(0, 0, 0, tgt, w, h, 3);

  return rval;
}

ImBuf *make_vectorscope_view_from_ibuf(ImBuf *ibuf)
{
  if (ibuf->rect_float) {
    return make_vectorscope_view_from_ibuf_float(ibuf);
  }
  return make_vectorscope_view_from_ibuf_byte(ibuf);
}