Blender V5.3
stereoimbuf.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2015 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstddef>
11
12#include "IMB_imbuf.hh"
13#include "IMB_imbuf_types.hh"
14
15#include "BLI_math_vector_c.hh"
16#include "BLI_utildefines.hh"
17
18#include "DNA_scene_types.h"
19
20namespace blender {
21
22/* prototypes */
23struct Stereo3DData;
24static void imb_stereo3d_write_doit(Stereo3DData *s3d_data, const Stereo3dFormat *s3d);
25static void imb_stereo3d_read_doit(Stereo3DData *s3d_data, const Stereo3dFormat *s3d);
26
28 struct {
29 float *left, *right, *stereo;
31 struct {
34 size_t x, y, channels;
36};
37
38/* -------------------------------------------------------------------- */
41
43{
44 int x, y;
45 size_t width = s3d->x;
46 size_t height = s3d->y;
47 const size_t channels = s3d->channels;
48
49 const int stride_from = width;
50 const int stride_to = width;
51
52 const int anaglyph_encoding[3][3] = {
53 {0, 1, 1},
54 {1, 0, 1},
55 {0, 0, 1},
56 };
57
58 int r, g, b;
59
60 r = anaglyph_encoding[mode][0];
61 g = anaglyph_encoding[mode][1];
62 b = anaglyph_encoding[mode][2];
63
64 if (s3d->is_float) {
65 float *rect_left = s3d->rectf.left;
66 float *rect_right = s3d->rectf.right;
67 float *rect_to = s3d->rectf.stereo;
68
69 if (channels == 3) {
70 for (y = 0; y < height; y++) {
71 float *to = rect_to + stride_to * y * 3;
72 float *from[2] = {
73 rect_left + stride_from * y * 3,
74 rect_right + stride_from * y * 3,
75 };
76
77 for (x = 0; x < width; x++, from[0] += 3, from[1] += 3, to += 3) {
78 to[0] = from[r][0];
79 to[1] = from[g][1];
80 to[2] = from[b][2];
81 }
82 }
83 }
84 else if (channels == 4) {
85 for (y = 0; y < height; y++) {
86 float *to = rect_to + stride_to * y * 4;
87 float *from[2] = {
88 rect_left + stride_from * y * 4,
89 rect_right + stride_from * y * 4,
90 };
91
92 for (x = 0; x < width; x++, from[0] += 4, from[1] += 4, to += 4) {
93 to[0] = from[r][0];
94 to[1] = from[g][1];
95 to[2] = from[b][2];
96 to[3] = std::max(from[0][3], from[1][3]);
97 }
98 }
99 }
100 }
101 else {
102 uchar *rect_left = s3d->rect.left;
103 uchar *rect_right = s3d->rect.right;
104 uchar *rect_to = s3d->rect.stereo;
105
106 if (channels == 3) {
107 for (y = 0; y < height; y++) {
108 uchar *to = rect_to + stride_to * y * 3;
109 uchar *from[2] = {
110 rect_left + stride_from * y * 3,
111 rect_right + stride_from * y * 3,
112 };
113
114 for (x = 0; x < width; x++, from[0] += 3, from[1] += 3, to += 3) {
115 to[0] = from[r][0];
116 to[1] = from[g][1];
117 to[2] = from[b][2];
118 }
119 }
120 }
121 else if (channels == 4) {
122 for (y = 0; y < height; y++) {
123 uchar *to = rect_to + stride_to * y * 4;
124 uchar *from[2] = {
125 rect_left + stride_from * y * 4,
126 rect_right + stride_from * y * 4,
127 };
128
129 for (x = 0; x < width; x++, from[0] += 4, from[1] += 4, to += 4) {
130 to[0] = from[r][0];
131 to[1] = from[g][1];
132 to[2] = from[b][2];
133 to[3] = std::max(from[0][3], from[1][3]);
134 }
135 }
136 }
137 }
138}
139
141 enum eStereo3dInterlaceType mode,
142 const bool swap)
143{
144 int x, y;
145 size_t width = s3d->x;
146 size_t height = s3d->y;
147 const size_t channels = s3d->channels;
148
149 const int stride_from = width;
150 const int stride_to = width;
151
152 if (s3d->is_float) {
153 const float *rect_left = s3d->rectf.left;
154 const float *rect_right = s3d->rectf.right;
155 float *rect_to = s3d->rectf.stereo;
156
157 switch (mode) {
158 case S3D_INTERLACE_ROW: {
159 char i = char(swap);
160 for (y = 0; y < height; y++) {
161 float *to = rect_to + stride_to * y * channels;
162 const float *from[2] = {
163 rect_left + stride_from * y * channels,
164 rect_right + stride_from * y * channels,
165 };
166 memcpy(to, from[i], sizeof(float) * channels * stride_from);
167 i = !i;
168 }
169 break;
170 }
172 if (channels == 1) {
173 for (y = 0; y < height; y++) {
174 float *to = rect_to + stride_to * y;
175 const float *from[2] = {
176 rect_left + stride_from * y,
177 rect_right + stride_from * y,
178 };
179
180 char i = char(swap);
181 for (x = 0; x < width; x++, from[0] += 1, from[1] += 1, to += 1) {
182 to[0] = from[i][0];
183 i = !i;
184 }
185 }
186 }
187 else if (channels == 3) {
188 for (y = 0; y < height; y++) {
189 float *to = rect_to + stride_to * y * 3;
190 const float *from[2] = {
191 rect_left + stride_from * y * 3,
192 rect_right + stride_from * y * 3,
193 };
194
195 char i = char(swap);
196 for (x = 0; x < width; x++, from[0] += 3, from[1] += 3, to += 3) {
197 copy_v3_v3(to, from[i]);
198 i = !i;
199 }
200 }
201 }
202 else if (channels == 4) {
203 for (y = 0; y < height; y++) {
204 float *to = rect_to + stride_to * y * channels;
205 const float *from[2] = {
206 rect_left + stride_from * y * channels,
207 rect_right + stride_from * y * channels,
208 };
209
210 char i = char(swap);
211 for (x = 0; x < width; x++, from[0] += 4, from[1] += 4, to += 4) {
212 copy_v4_v4(to, from[i]);
213 i = !i;
214 }
215 }
216 }
217 break;
218 }
220 if (channels == 1) {
221 char i = char(swap);
222 for (y = 0; y < height; y++) {
223 float *to = rect_to + stride_to * y;
224 const float *from[2] = {
225 rect_left + stride_from * y,
226 rect_right + stride_from * y,
227 };
228 char j = i;
229 for (x = 0; x < width; x++, from[0] += 1, from[1] += 1, to += 1) {
230 to[0] = from[j][0];
231 j = !j;
232 }
233 i = !i;
234 }
235 }
236 else if (channels == 3) {
237 char i = char(swap);
238 for (y = 0; y < height; y++) {
239 float *to = rect_to + stride_to * y * 3;
240 const float *from[2] = {
241 rect_left + stride_from * y * 3,
242 rect_right + stride_from * y * 3,
243 };
244 char j = i;
245 for (x = 0; x < width; x++, from[0] += 3, from[1] += 3, to += 3) {
246 copy_v3_v3(to, from[j]);
247 j = !j;
248 }
249 i = !i;
250 }
251 }
252 else if (channels == 4) {
253 char i = char(swap);
254 for (y = 0; y < height; y++) {
255 float *to = rect_to + stride_to * y * 4;
256 const float *from[2] = {
257 rect_left + stride_from * y * 4,
258 rect_right + stride_from * y * 4,
259 };
260 char j = i;
261 for (x = 0; x < width; x++, from[0] += 4, from[1] += 4, to += 4) {
262 copy_v4_v4(to, from[j]);
263 j = !j;
264 }
265 i = !i;
266 }
267 }
268 break;
269 }
270 default: {
271 break;
272 }
273 }
274 }
275 else {
276 const uchar *rect_left = s3d->rect.left;
277 const uchar *rect_right = s3d->rect.right;
278 uchar *rect_to = s3d->rect.stereo;
279
280 switch (mode) {
281 case S3D_INTERLACE_ROW: {
282 char i = char(swap);
283 for (y = 0; y < height; y++) {
284 uchar *to = rect_to + stride_to * y * channels;
285 const uchar *from[2] = {
286 rect_left + stride_from * y * channels,
287 rect_right + stride_from * y * channels,
288 };
289 memcpy(to, from[i], sizeof(uchar) * channels * stride_from);
290 i = !i;
291 }
292 break;
293 }
295 if (channels == 1) {
296 for (y = 0; y < height; y++) {
297 uchar *to = rect_to + stride_to * y;
298 const uchar *from[2] = {
299 rect_left + stride_from * y,
300 rect_right + stride_from * y,
301 };
302 char i = char(swap);
303 for (x = 0; x < width; x++, from[0] += 1, from[1] += 1, to += 1) {
304 to[0] = from[i][0];
305 i = !i;
306 }
307 }
308 }
309 else if (channels == 3) {
310 for (y = 0; y < height; y++) {
311 uchar *to = rect_to + stride_to * y * 3;
312 const uchar *from[2] = {
313 rect_left + stride_from * y * 3,
314 rect_right + stride_from * y * 3,
315 };
316 char i = char(swap);
317 for (x = 0; x < width; x++, from[0] += 3, from[1] += 3, to += 3) {
318 copy_v3_v3_uchar(to, from[i]);
319 i = !i;
320 }
321 }
322 }
323 else if (channels == 4) {
324 for (y = 0; y < height; y++) {
325 uchar *to = rect_to + stride_to * y * 4;
326 const uchar *from[2] = {
327 rect_left + stride_from * y * 4,
328 rect_right + stride_from * y * 4,
329 };
330 char i = char(swap);
331 for (x = 0; x < width; x++, from[0] += 4, from[1] += 4, to += 4) {
332 copy_v4_v4_uchar(to, from[i]);
333 i = !i;
334 }
335 }
336 }
337 break;
338 }
340 if (channels == 1) {
341 char i = char(swap);
342 for (y = 0; y < height; y++) {
343 uchar *to = rect_to + stride_to * y;
344 const uchar *from[2] = {
345 rect_left + stride_from * y,
346 rect_right + stride_from * y,
347 };
348 char j = i;
349 for (x = 0; x < width; x++, from[0] += 1, from[1] += 1, to += 1) {
350 to[0] = from[j][0];
351 j = !j;
352 }
353 i = !i;
354 }
355 }
356 else if (channels == 3) {
357 char i = char(swap);
358 for (y = 0; y < height; y++) {
359 uchar *to = rect_to + stride_to * y * 3;
360 const uchar *from[2] = {
361 rect_left + stride_from * y * 3,
362 rect_right + stride_from * y * 3,
363 };
364 char j = i;
365 for (x = 0; x < width; x++, from[0] += 3, from[1] += 3, to += 3) {
366 copy_v3_v3_uchar(to, from[j]);
367 j = !j;
368 }
369 i = !i;
370 }
371 }
372 else if (channels == 4) {
373 char i = char(swap);
374 for (y = 0; y < height; y++) {
375 uchar *to = rect_to + stride_to * y * 4;
376 const uchar *from[2] = {
377 rect_left + stride_from * y * 4,
378 rect_right + stride_from * y * 4,
379 };
380 char j = i;
381 for (x = 0; x < width; x++, from[0] += 4, from[1] += 4, to += 4) {
382 copy_v4_v4_uchar(to, from[j]);
383 j = !j;
384 }
385 i = !i;
386 }
387 }
388 break;
389 }
390 default: {
391 break;
392 }
393 }
394 }
395}
396
397/* stereo3d output (s3d->rectf.stereo) is always unsqueezed */
398static void imb_stereo3d_write_sidebyside(const Stereo3DData *s3d, const bool crosseyed)
399{
400 int y;
401 size_t width = s3d->x;
402 size_t height = s3d->y;
403 const size_t channels = s3d->channels;
404
405 const int stride_from = width;
406 const int stride_to = width * 2;
407
408 const int l = int(crosseyed);
409 const int r = !l;
410
411 if (s3d->is_float) {
412 const float *rect_left = s3d->rectf.left;
413 const float *rect_right = s3d->rectf.right;
414 float *rect_to = s3d->rectf.stereo;
415
416 for (y = 0; y < height; y++) {
417 float *to = rect_to + stride_to * y * channels;
418 const float *from[2] = {
419 rect_left + stride_from * y * channels,
420 rect_right + stride_from * y * channels,
421 };
422
423 memcpy(to, from[l], sizeof(float) * channels * stride_from);
424 memcpy(to + channels * stride_from, from[r], sizeof(float) * channels * stride_from);
425 }
426 }
427 else {
428 const uchar *rect_left = s3d->rect.left;
429 const uchar *rect_right = s3d->rect.right;
430 uchar *rect_to = s3d->rect.stereo;
431
432 for (y = 0; y < height; y++) {
433 uchar *to = rect_to + stride_to * y * channels;
434 const uchar *from[2] = {
435 rect_left + stride_from * y * channels,
436 rect_right + stride_from * y * channels,
437 };
438
439 memcpy(to, from[l], sizeof(uchar) * channels * stride_from);
440 memcpy(to + channels * stride_from, from[r], sizeof(uchar) * channels * stride_from);
441 }
442 }
443}
444
445/* stereo3d output (s3d->rectf.stereo) is always unsqueezed */
447{
448 int y;
449 size_t width = s3d->x;
450 size_t height = s3d->y;
451 const size_t channels = s3d->channels;
452
453 const int stride_from = width;
454 const int stride_to = width;
455
456 if (s3d->is_float) {
457 const float *rect_left = s3d->rectf.left;
458 const float *rect_right = s3d->rectf.right;
459 float *rect_to = s3d->rectf.stereo;
460
461 for (y = 0; y < height; y++) {
462 float *to = rect_to + stride_to * y * channels;
463 const float *from[2] = {
464 rect_left + stride_from * y * channels,
465 rect_right + stride_from * y * channels,
466 };
467
468 memcpy(to, from[1], sizeof(float) * channels * stride_from);
469 memcpy(
470 to + channels * height * stride_from, from[0], sizeof(float) * channels * stride_from);
471 }
472 }
473 else {
474 const uchar *rect_left = s3d->rect.left;
475 const uchar *rect_right = s3d->rect.right;
476 uchar *rect_to = s3d->rect.stereo;
477
478 for (y = 0; y < height; y++) {
479 uchar *to = rect_to + stride_to * y * channels;
480 const uchar *from[2] = {
481 rect_left + stride_from * y * channels,
482 rect_right + stride_from * y * channels,
483 };
484
485 memcpy(to, from[1], sizeof(uchar) * channels * stride_from);
486 memcpy(
487 to + channels * height * stride_from, from[0], sizeof(uchar) * channels * stride_from);
488 }
489 }
490}
491
493
494/* -------------------------------------------------------------------- */
497
498void IMB_stereo3d_write_dimensions(const char mode,
499 const bool is_squeezed,
500 const size_t width,
501 const size_t height,
502 size_t *r_width,
503 size_t *r_height)
504{
505 switch (mode) {
507 *r_width = is_squeezed ? width : width * 2;
508 *r_height = height;
509 break;
510 }
512 *r_width = width;
513 *r_height = is_squeezed ? height : height * 2;
514 break;
515 }
518 default: {
519 *r_width = width;
520 *r_height = height;
521 break;
522 }
523 }
524}
525
526void IMB_stereo3d_read_dimensions(const char mode,
527 const bool is_squeezed,
528 const size_t width,
529 const size_t height,
530 size_t *r_width,
531 size_t *r_height)
532{
533 switch (mode) {
535 *r_width = is_squeezed ? width / 2 : width;
536 *r_height = height;
537 break;
538 }
540 *r_width = width;
541 *r_height = is_squeezed ? height / 2 : height;
542 break;
543 }
546 default: {
547 *r_width = width;
548 *r_height = height;
549 break;
550 }
551 }
552}
553
555
556/* -------------------------------------------------------------------- */
559
561 const Stereo3dFormat *s3d,
562 const size_t x,
563 const size_t y)
564{
566 return;
567 }
568
569 if ((s3d->flag & S3D_SQUEEZED_FRAME) == 0) {
570 return;
571 }
572
574}
575
577 const Stereo3dFormat *s3d,
578 const size_t x,
579 const size_t y)
580{
582 return;
583 }
584
585 if ((s3d->flag & S3D_SQUEEZED_FRAME) == 0) {
586 return;
587 }
588
590}
591
593
594/* -------------------------------------------------------------------- */
597
599 const bool is_float,
600 const size_t x,
601 const size_t y,
602 const size_t channels,
603 int *rect_left,
604 int *rect_right,
605 int *rect_stereo,
606 float *rectf_left,
607 float *rectf_right,
608 float *rectf_stereo)
609{
610 s3d_data->is_float = is_float;
611 s3d_data->x = x;
612 s3d_data->y = y;
613 s3d_data->channels = channels;
614 s3d_data->rect.left = reinterpret_cast<uchar *>(rect_left);
615 s3d_data->rect.right = reinterpret_cast<uchar *>(rect_right);
616 s3d_data->rect.stereo = reinterpret_cast<uchar *>(rect_stereo);
617 s3d_data->rectf.left = rectf_left;
618 s3d_data->rectf.right = rectf_right;
619 s3d_data->rectf.stereo = rectf_stereo;
620}
621
622ImBuf *IMB_stereo3d_ImBuf(const ImageFormatData *im_format, ImBuf *ibuf_left, ImBuf *ibuf_right)
623{
624 ImBuf *ibuf_stereo = nullptr;
625 Stereo3DData s3d_data = {{nullptr}};
626 size_t width, height;
627 const bool is_byte = ibuf_left->byte_data() && ibuf_right->byte_data();
628 const bool is_float = ibuf_left->float_data() && ibuf_right->float_data() &&
629 !(is_byte && im_format->depth <= 8);
630
631 if (!(is_float || is_byte)) {
632 return nullptr;
633 }
634
636 im_format->stereo3d_format.display_mode, false, ibuf_left->x, ibuf_left->y, &width, &height);
637 ibuf_stereo = IMB_allocImBuf(width, height, ImBufFlags::Zero);
638 ibuf_stereo->color_mode = ibuf_left->color_mode;
639
640 if (is_float) {
641 IMB_alloc_float_pixels(ibuf_stereo, ibuf_left->channels);
642 ibuf_stereo->float_buffer.colorspace = ibuf_left->float_buffer.colorspace;
643 }
644 else {
645 IMB_alloc_byte_pixels(ibuf_stereo);
646 ibuf_stereo->byte_buffer.colorspace = ibuf_left->byte_buffer.colorspace;
647 }
648
649 ibuf_stereo->flags = ibuf_left->flags;
650
651 imb_stereo3d_data_init(&s3d_data,
652 is_float,
653 ibuf_left->x,
654 ibuf_left->y,
655 ibuf_left->channels,
656 reinterpret_cast<int *>(ibuf_left->byte_data_for_write()),
657 reinterpret_cast<int *>(ibuf_right->byte_data_for_write()),
658 reinterpret_cast<int *>(ibuf_stereo->byte_data_for_write()),
659 ibuf_left->float_data_for_write(),
660 ibuf_right->float_data_for_write(),
661 ibuf_stereo->float_data_for_write());
662
663 imb_stereo3d_write_doit(&s3d_data, &im_format->stereo3d_format);
664 imb_stereo3d_squeeze_ImBuf(ibuf_stereo, &im_format->stereo3d_format, ibuf_left->x, ibuf_left->y);
665
666 return ibuf_stereo;
667}
668
669static void imb_stereo3d_write_doit(Stereo3DData *s3d_data, const Stereo3dFormat *s3d)
670{
671 switch (s3d->display_mode) {
674 break;
678 (s3d->flag & S3D_INTERLACE_SWAP) != 0);
679 break;
682 break;
685 break;
686 default:
687 break;
688 }
689}
690
692
693/* -------------------------------------------------------------------- */
696
698{
699 int x, y;
700 size_t width = s3d->x;
701 size_t height = s3d->y;
702 const size_t channels = s3d->channels;
703
704 const int stride_from = width;
705 const int stride_to = width;
706
707 const int anaglyph_encoding[3][3] = {
708 {0, 1, 1},
709 {1, 0, 1},
710 {0, 0, 1},
711 };
712
713 int r, g, b;
714
715 r = anaglyph_encoding[mode][0];
716 g = anaglyph_encoding[mode][1];
717 b = anaglyph_encoding[mode][2];
718
719 if (s3d->is_float) {
720 float *rect_left = s3d->rectf.left;
721 float *rect_right = s3d->rectf.right;
722 float *rect_from = s3d->rectf.stereo;
723
724 if (channels == 3) {
725 for (y = 0; y < height; y++) {
726 float *from = rect_from + stride_from * y * 3;
727 float *to[2] = {
728 rect_left + stride_to * y * 3,
729 rect_right + stride_to * y * 3,
730 };
731
732 for (x = 0; x < width; x++, from += 3, to[0] += 3, to[1] += 3) {
733 to[r][0] = from[0];
734 to[g][1] = from[1];
735 to[b][2] = from[2];
736 }
737 }
738 }
739 else if (channels == 4) {
740 for (y = 0; y < height; y++) {
741 float *from = rect_from + stride_from * y * 4;
742 float *to[2] = {
743 rect_left + stride_to * y * 4,
744 rect_right + stride_to * y * 4,
745 };
746
747 for (x = 0; x < width; x++, from += 4, to[0] += 4, to[1] += 4) {
748 to[r][0] = from[0];
749 to[g][1] = from[1];
750 to[b][2] = from[2];
751 to[0][3] = to[1][3] = from[3];
752 }
753 }
754 }
755 }
756 else {
757 uchar *rect_left = s3d->rect.left;
758 uchar *rect_right = s3d->rect.right;
759 uchar *rect_from = s3d->rect.stereo;
760
761 if (channels == 3) {
762 for (y = 0; y < height; y++) {
763 uchar *from = rect_from + stride_from * y * 3;
764 uchar *to[2] = {
765 rect_left + stride_to * y * 3,
766 rect_right + stride_to * y * 3,
767 };
768
769 for (x = 0; x < width; x++, from += 3, to[0] += 3, to[1] += 3) {
770 to[r][0] = from[0];
771 to[g][1] = from[1];
772 to[b][2] = from[2];
773 }
774 }
775 }
776 else if (channels == 4) {
777 for (y = 0; y < height; y++) {
778 uchar *from = rect_from + stride_from * y * 4;
779 uchar *to[2] = {
780 rect_left + stride_to * y * 4,
781 rect_right + stride_to * y * 4,
782 };
783
784 for (x = 0; x < width; x++, from += 4, to[0] += 4, to[1] += 4) {
785 to[r][0] = from[0];
786 to[g][1] = from[1];
787 to[b][2] = from[2];
788 to[0][3] = to[1][3] = from[3];
789 }
790 }
791 }
792 }
793}
794
796 enum eStereo3dInterlaceType mode,
797 const bool swap)
798{
799 int x, y;
800 size_t width = s3d->x;
801 size_t height = s3d->y;
802 const size_t channels = s3d->channels;
803
804 const int stride_from = width;
805 const int stride_to = width;
806
807 if (s3d->is_float) {
808 float *rect_left = s3d->rectf.left;
809 float *rect_right = s3d->rectf.right;
810 const float *rect_from = s3d->rectf.stereo;
811
812 switch (mode) {
813 case S3D_INTERLACE_ROW: {
814 char i = char(swap);
815 for (y = 0; y < height; y++) {
816 const float *from = rect_from + stride_from * y * channels;
817 float *to[2] = {
818 rect_left + stride_to * y * channels,
819 rect_right + stride_to * y * channels,
820 };
821 memcpy(to[i], from, sizeof(float) * channels * stride_to);
822 i = !i;
823 }
824 break;
825 }
827 if (channels == 1) {
828 for (y = 0; y < height; y++) {
829 const float *from = rect_from + stride_from * y;
830 float *to[2] = {
831 rect_left + stride_to * y,
832 rect_right + stride_to * y,
833 };
834
835 char i = char(swap);
836 for (x = 0; x < width; x++, from += 1, to[0] += 1, to[1] += 1) {
837 to[i][0] = from[0];
838 i = !i;
839 }
840 }
841 }
842 else if (channels == 3) {
843 for (y = 0; y < height; y++) {
844 const float *from = rect_from + stride_from * y * 3;
845 float *to[2] = {
846 rect_left + stride_to * y * 3,
847 rect_right + stride_to * y * 3,
848 };
849
850 char i = char(swap);
851 for (x = 0; x < width; x++, from += 3, to[0] += 3, to[1] += 3) {
852 copy_v3_v3(to[i], from);
853 i = !i;
854 }
855 }
856 }
857 else if (channels == 4) {
858 for (y = 0; y < height; y++) {
859 const float *from = rect_from + stride_from * y * channels;
860 float *to[2] = {
861 rect_left + stride_to * y * channels,
862 rect_right + stride_to * y * channels,
863 };
864
865 char i = char(swap);
866 for (x = 0; x < width; x++, from += 4, to[0] += 4, to[1] += 4) {
867 copy_v4_v4(to[i], from);
868 i = !i;
869 }
870 }
871 }
872 break;
873 }
875 if (channels == 1) {
876 char i = char(swap);
877 for (y = 0; y < height; y++) {
878 const float *from = rect_from + stride_from * y;
879 float *to[2] = {
880 rect_left + stride_to * y,
881 rect_right + stride_to * y,
882 };
883 char j = i;
884 for (x = 0; x < width; x++, from += 1, to[0] += 1, to[1] += 1) {
885 to[j][0] = from[0];
886 j = !j;
887 }
888 i = !i;
889 }
890 }
891 else if (channels == 3) {
892 char i = char(swap);
893 for (y = 0; y < height; y++) {
894 const float *from = rect_from + stride_from * y * 3;
895 float *to[2] = {
896 rect_left + stride_to * y * 3,
897 rect_right + stride_to * y * 3,
898 };
899 char j = i;
900 for (x = 0; x < width; x++, from += 3, to[0] += 3, to[1] += 3) {
901 copy_v3_v3(to[j], from);
902 j = !j;
903 }
904 i = !i;
905 }
906 }
907 else if (channels == 4) {
908 char i = char(swap);
909 for (y = 0; y < height; y++) {
910 const float *from = rect_from + stride_from * y * 4;
911 float *to[2] = {
912 rect_left + stride_to * y * 4,
913 rect_right + stride_to * y * 4,
914 };
915 char j = i;
916 for (x = 0; x < width; x++, from += 4, to[0] += 4, to[1] += 4) {
917 copy_v4_v4(to[j], from);
918 j = !j;
919 }
920 i = !i;
921 }
922 }
923 break;
924 }
925 default: {
926 break;
927 }
928 }
929 }
930 else {
931 uchar *rect_left = s3d->rect.right;
932 uchar *rect_right = s3d->rect.left;
933 const uchar *rect_from = s3d->rect.stereo;
934
935 switch (mode) {
936 case S3D_INTERLACE_ROW: {
937 char i = char(swap);
938 for (y = 0; y < height; y++) {
939 const uchar *from = rect_from + stride_from * y * channels;
940 uchar *to[2] = {
941 rect_left + stride_to * y * channels,
942 rect_right + stride_to * y * channels,
943 };
944 memcpy(to[i], from, sizeof(uchar) * channels * stride_to);
945 i = !i;
946 }
947 break;
948 }
950 if (channels == 1) {
951 for (y = 0; y < height; y++) {
952 const uchar *from = rect_from + stride_from * y;
953 uchar *to[2] = {
954 rect_left + stride_to * y,
955 rect_right + stride_to * y,
956 };
957 char i = char(swap);
958 for (x = 0; x < width; x++, from += 1, to[0] += 1, to[1] += 1) {
959 to[i][0] = from[0];
960 i = !i;
961 }
962 }
963 }
964 else if (channels == 3) {
965 for (y = 0; y < height; y++) {
966 const uchar *from = rect_from + stride_from * y * 3;
967 uchar *to[2] = {
968 rect_left + stride_to * y * 3,
969 rect_right + stride_to * y * 3,
970 };
971 char i = char(swap);
972 for (x = 0; x < width; x++, from += 3, to[0] += 3, to[1] += 3) {
973 copy_v3_v3_uchar(to[i], from);
974 i = !i;
975 }
976 }
977 }
978 else if (channels == 4) {
979 for (y = 0; y < height; y++) {
980 const uchar *from = rect_from + stride_from * y * 4;
981 uchar *to[2] = {
982 rect_left + stride_to * y * 4,
983 rect_right + stride_to * y * 4,
984 };
985 char i = char(swap);
986 for (x = 0; x < width; x++, from += 4, to[0] += 4, to[1] += 4) {
987 copy_v4_v4_uchar(to[i], from);
988 i = !i;
989 }
990 }
991 }
992 break;
993 }
995 if (channels == 1) {
996 char i = char(swap);
997 for (y = 0; y < height; y++) {
998 const uchar *from = rect_from + stride_from * y;
999 uchar *to[2] = {
1000 rect_left + stride_to * y,
1001 rect_right + stride_to * y,
1002 };
1003 char j = i;
1004 for (x = 0; x < width; x++, from += 1, to[0] += 1, to[1] += 1) {
1005 to[j][0] = from[0];
1006 j = !j;
1007 }
1008 i = !i;
1009 }
1010 }
1011 else if (channels == 3) {
1012 char i = char(swap);
1013 for (y = 0; y < height; y++) {
1014 const uchar *from = rect_from + stride_from * y * 3;
1015 uchar *to[2] = {
1016 rect_left + stride_to * y * 3,
1017 rect_right + stride_to * y * 3,
1018 };
1019 char j = i;
1020 for (x = 0; x < width; x++, from += 3, to[0] += 3, to[1] += 3) {
1021 copy_v3_v3_uchar(to[j], from);
1022 j = !j;
1023 }
1024 i = !i;
1025 }
1026 }
1027 else if (channels == 4) {
1028 char i = char(swap);
1029 for (y = 0; y < height; y++) {
1030 const uchar *from = rect_from + stride_from * y * 4;
1031 uchar *to[2] = {
1032 rect_left + stride_to * y * 4,
1033 rect_right + stride_to * y * 4,
1034 };
1035 char j = i;
1036 for (x = 0; x < width; x++, from += 4, to[0] += 4, to[1] += 4) {
1037 copy_v4_v4_uchar(to[j], from);
1038 j = !j;
1039 }
1040 i = !i;
1041 }
1042 }
1043 break;
1044 }
1045 default: {
1046 break;
1047 }
1048 }
1049 }
1050}
1051
1052/* stereo input (s3d->rectf.stereo) is always unsqueezed */
1053static void imb_stereo3d_read_sidebyside(const Stereo3DData *s3d, const bool crosseyed)
1054{
1055 int y;
1056 size_t width = s3d->x;
1057 size_t height = s3d->y;
1058 const size_t channels = s3d->channels;
1059
1060 const int stride_from = width * 2;
1061 const int stride_to = width;
1062
1063 const int l = int(crosseyed);
1064 const int r = !l;
1065
1066 if (s3d->is_float) {
1067 float *rect_left = s3d->rectf.left;
1068 float *rect_right = s3d->rectf.right;
1069 const float *rect_from = s3d->rectf.stereo;
1070
1071 for (y = 0; y < height; y++) {
1072 const float *from = rect_from + stride_from * y * channels;
1073 float *to[2] = {
1074 rect_left + stride_to * y * channels,
1075 rect_right + stride_to * y * channels,
1076 };
1077
1078 memcpy(to[l], from, sizeof(float) * channels * stride_to);
1079 memcpy(to[r], from + channels * stride_to, sizeof(float) * channels * stride_to);
1080 }
1081 }
1082 else {
1083 uchar *rect_left = s3d->rect.left;
1084 uchar *rect_right = s3d->rect.right;
1085 const uchar *rect_from = s3d->rect.stereo;
1086
1087 /* always RGBA input/output */
1088 for (y = 0; y < height; y++) {
1089 const uchar *from = rect_from + stride_from * y * channels;
1090 uchar *to[2] = {
1091 rect_left + stride_to * y * channels,
1092 rect_right + stride_to * y * channels,
1093 };
1094
1095 memcpy(to[l], from, sizeof(uchar) * channels * stride_to);
1096 memcpy(to[r], from + channels * stride_to, sizeof(uchar) * channels * stride_to);
1097 }
1098 }
1099}
1100
1101/* stereo input (s3d->rectf.stereo) is always unsqueezed */
1103{
1104 int y;
1105 size_t width = s3d->x;
1106 size_t height = s3d->y;
1107 const size_t channels = s3d->channels;
1108
1109 const int stride_from = width;
1110 const int stride_to = width;
1111
1112 if (s3d->is_float) {
1113 float *rect_left = s3d->rectf.left;
1114 float *rect_right = s3d->rectf.right;
1115 const float *rect_from = s3d->rectf.stereo;
1116
1117 for (y = 0; y < height; y++) {
1118 const float *from = rect_from + stride_from * y * channels;
1119 float *to[2] = {
1120 rect_left + stride_to * y * channels,
1121 rect_right + stride_to * y * channels,
1122 };
1123
1124 memcpy(to[1], from, sizeof(float) * channels * stride_to);
1125 memcpy(to[0], from + channels * height * stride_to, sizeof(float) * channels * stride_to);
1126 }
1127 }
1128 else {
1129 uchar *rect_left = s3d->rect.left;
1130 uchar *rect_right = s3d->rect.right;
1131 const uchar *rect_from = s3d->rect.stereo;
1132
1133 for (y = 0; y < height; y++) {
1134 const uchar *from = rect_from + stride_from * y * channels;
1135 uchar *to[2] = {
1136 rect_left + stride_to * y * channels,
1137 rect_right + stride_to * y * channels,
1138 };
1139
1140 memcpy(to[1], from, sizeof(uchar) * channels * stride_to);
1141 memcpy(to[0], from + channels * height * stride_to, sizeof(uchar) * channels * stride_to);
1142 }
1143 }
1144}
1145
1147
1148/* -------------------------------------------------------------------- */
1151
1153 ImBuf *ibuf_stereo3d,
1154 ImBuf **r_ibuf_left,
1155 ImBuf **r_ibuf_right)
1156{
1157 Stereo3DData s3d_data = {{nullptr}};
1158 ImBuf *ibuf_left, *ibuf_right;
1159 size_t width, height;
1160 const bool is_float = (ibuf_stereo3d->float_data() != nullptr);
1161
1163 ((s3d->flag & S3D_SQUEEZED_FRAME) == 0),
1164 ibuf_stereo3d->x,
1165 ibuf_stereo3d->y,
1166 &width,
1167 &height);
1168
1169 ibuf_left = IMB_allocImBuf(width, height, ImBufFlags::Zero);
1170 ibuf_right = IMB_allocImBuf(width, height, ImBufFlags::Zero);
1171 ibuf_left->color_mode = ibuf_right->color_mode = ibuf_stereo3d->color_mode;
1172
1173 if (is_float) {
1174 IMB_alloc_float_pixels(ibuf_left, ibuf_stereo3d->channels);
1175 IMB_alloc_float_pixels(ibuf_right, ibuf_stereo3d->channels);
1176 ibuf_left->float_buffer.colorspace = ibuf_stereo3d->float_buffer.colorspace;
1177 ibuf_right->float_buffer.colorspace = ibuf_stereo3d->float_buffer.colorspace;
1178 }
1179 else {
1180 IMB_alloc_byte_pixels(ibuf_left);
1181 IMB_alloc_byte_pixels(ibuf_right);
1182 ibuf_left->byte_buffer.colorspace = ibuf_stereo3d->byte_buffer.colorspace;
1183 ibuf_right->byte_buffer.colorspace = ibuf_stereo3d->byte_buffer.colorspace;
1184 }
1185
1186 ibuf_left->flags = ibuf_stereo3d->flags;
1187 ibuf_right->flags = ibuf_stereo3d->flags;
1188
1189 /* we always work with unsqueezed formats */
1191 ((s3d->flag & S3D_SQUEEZED_FRAME) == 0),
1192 ibuf_stereo3d->x,
1193 ibuf_stereo3d->y,
1194 &width,
1195 &height);
1196 imb_stereo3d_unsqueeze_ImBuf(ibuf_stereo3d, s3d, width, height);
1197
1198 imb_stereo3d_data_init(&s3d_data,
1199 is_float,
1200 ibuf_left->x,
1201 ibuf_left->y,
1202 ibuf_left->channels,
1203 reinterpret_cast<int *>(ibuf_left->byte_data_for_write()),
1204 reinterpret_cast<int *>(ibuf_right->byte_data_for_write()),
1205 reinterpret_cast<int *>(ibuf_stereo3d->byte_data_for_write()),
1206 ibuf_left->float_data_for_write(),
1207 ibuf_right->float_data_for_write(),
1208 ibuf_stereo3d->float_data_for_write());
1209
1210 imb_stereo3d_read_doit(&s3d_data, s3d);
1211
1212 IMB_freeImBuf(ibuf_stereo3d);
1213
1214 *r_ibuf_left = ibuf_left;
1215 *r_ibuf_right = ibuf_right;
1216}
1217
1218static void imb_stereo3d_read_doit(Stereo3DData *s3d_data, const Stereo3dFormat *s3d)
1219{
1220 switch (s3d->display_mode) {
1223 break;
1227 (s3d->flag & S3D_INTERLACE_SWAP) != 0);
1228 break;
1231 break;
1234 break;
1235 default:
1236 break;
1237 }
1238}
1239
1241
1242} // namespace blender
#define ELEM(...)
BMesh Iterators.
static void imb_stereo3d_unsqueeze_ImBuf(ImBuf *ibuf, const Stereo3dFormat *s3d, const size_t x, const size_t y)
MINLINE void copy_v3_v3(float r[3], const float a[3])
static void imb_stereo3d_write_sidebyside(const Stereo3DData *s3d, const bool crosseyed)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static void imb_stereo3d_read_interlace(const Stereo3DData *s3d, enum eStereo3dInterlaceType mode, const bool swap)
static void imb_stereo3d_read_sidebyside(const Stereo3DData *s3d, const bool crosseyed)
bool IMB_alloc_byte_pixels(ImBuf *ibuf, bool initialize_pixels=true)
@ S3D_DISPLAY_SIDEBYSIDE
@ S3D_DISPLAY_TOPBOTTOM
@ S3D_DISPLAY_INTERLACE
static void imb_stereo3d_write_topbottom(const Stereo3DData *s3d)
ImBuf * IMB_stereo3d_ImBuf(const ImageFormatData *im_format, ImBuf *ibuf_left, ImBuf *ibuf_right)
static void imb_stereo3d_squeeze_ImBuf(ImBuf *ibuf, const Stereo3dFormat *s3d, const size_t x, const size_t y)
static void imb_stereo3d_read_doit(Stereo3DData *s3d_data, const Stereo3dFormat *s3d)
void IMB_stereo3d_read_dimensions(char mode, bool is_squeezed, size_t width, size_t height, size_t *r_width, size_t *r_height)
static void imb_stereo3d_read_topbottom(const Stereo3DData *s3d)
void IMB_freeImBuf(ImBuf *ibuf)
unsigned char uchar
Definition BKE_text.h:58
void IMB_stereo3d_write_dimensions(char mode, bool is_squeezed, size_t width, size_t height, size_t *r_width, size_t *r_height)
@ S3D_INTERLACE_CHECKERBOARD
@ S3D_SIDEBYSIDE_CROSSEYED
static void imb_stereo3d_write_anaglyph(const Stereo3DData *s3d, enum eStereo3dAnaglyphType mode)
MINLINE void copy_v4_v4_uchar(unsigned char r[4], const unsigned char a[4])
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static void imb_stereo3d_data_init(Stereo3DData *s3d_data, const bool is_float, const size_t x, const size_t y, const size_t channels, int *rect_left, int *rect_right, int *rect_stereo, float *rectf_left, float *rectf_right, float *rectf_stereo)
void IMB_ImBufFromStereo3d(const Stereo3dFormat *s3d, ImBuf *ibuf_stereo3d, ImBuf **r_ibuf_left, ImBuf **r_ibuf_right)
static void imb_stereo3d_read_anaglyph(const Stereo3DData *s3d, enum eStereo3dAnaglyphType mode)
MINLINE void copy_v3_v3_uchar(unsigned char r[3], const unsigned char a[3])
MINLINE void copy_v4_v4(float r[4], const float a[4])
bool IMB_alloc_float_pixels(ImBuf *ibuf, unsigned int channels, bool initialize_pixels=true)
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
static void imb_stereo3d_write_doit(Stereo3DData *s3d_data, const Stereo3dFormat *s3d)
static void imb_stereo3d_write_interlace(const Stereo3DData *s3d, enum eStereo3dInterlaceType mode, const bool swap)
#define swap(a, b)
Definition sort.cc:61
const ColorSpace * colorspace
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
ImColorMode color_mode
float * float_data_for_write()
Definition allocimbuf.cc:79
struct blender::Stereo3DData::@277103323243107077105311244346077232222306242046 rectf
struct blender::Stereo3DData::@243122220221317352175050323254226006247356120366 rect
eStereo3dAnaglyphType anaglyph_type
eStereoDisplayMode display_mode
eStereo3dInterlaceType interlace_type