1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
|
/*
* Copyright (c) 2017-2019 Arm Limited.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef ARM_COMPUTE_TEST_VALIDATION_COLOR_CONVERT_H
#define ARM_COMPUTE_TEST_VALIDATION_COLOR_CONVERT_H
#include "Utils.h"
namespace arm_compute
{
namespace test
{
namespace colorconvert_helper
{
namespace detail
{
constexpr float red_coef_bt709 = 1.5748F;
constexpr float green_coef_bt709 = -0.1873f;
constexpr float green_coef2_bt709 = -0.4681f;
constexpr float blue_coef_bt709 = 1.8556f;
constexpr float rgb2yuv_bt709_kr = 0.2126f;
constexpr float rgb2yuv_bt709_kb = 0.0722f;
// K_g = 1 - K_r - K_b
constexpr float rgb2yuv_bt709_kg = 0.7152f;
// C_u = 1 / (2 * (1 - K_b))
constexpr float rgb2yuv_bt709_cu = 0.5389f;
// C_v = 1 / (2 * (1 - K_r))
constexpr float rgb2yuv_bt709_cv = 0.6350f;
constexpr float rgb2u8_red_coef = 0.2126f;
constexpr float rgb2u8_green_coef = 0.7152f;
constexpr float rgb2u8_blue_coef = 0.0722f;
template <typename T>
inline void store_rgb_from_src(const SimpleTensor<T> src, SimpleTensor<T> &rvec, SimpleTensor<T> &gvec, SimpleTensor<T> &bvec)
{
int width = src.shape().x();
int height = src.shape().y();
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
const Coordinates src_coord{ x, y };
const Coordinates vec_coord{ x, y };
const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
auto *rvec_pixel = reinterpret_cast<T *>(rvec(vec_coord));
auto *gvec_pixel = reinterpret_cast<T *>(gvec(vec_coord));
auto *bvec_pixel = reinterpret_cast<T *>(bvec(vec_coord));
rvec_pixel[0] = src_pixel[0]; // NOLINT
gvec_pixel[0] = src_pixel[1];
bvec_pixel[0] = src_pixel[2];
}
}
}
template <typename T>
inline void rgb_to_yuv_calculation(const SimpleTensor<T> rvec, const SimpleTensor<T> gvec, const SimpleTensor<T> bvec, SimpleTensor<T> &yvec, SimpleTensor<T> &uvec_top, SimpleTensor<T> &uvec_bottom,
SimpleTensor<T> &vvec_top, SimpleTensor<T> &vvec_bottom)
{
int width = rvec.shape().x();
int height = rvec.shape().y();
int uvec_coord_x = 0;
int uvec_coord_y = 0;
Coordinates uvec_coord{ uvec_coord_x, uvec_coord_y };
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; x += 2)
{
Coordinates coord{ x, y };
auto *yvec_pixel = reinterpret_cast<T *>(yvec(coord));
auto *uvec_top_pixel = reinterpret_cast<T *>(uvec_top(uvec_coord));
auto *uvec_bottom_pixel = reinterpret_cast<T *>(uvec_bottom(uvec_coord));
auto *vvec_top_pixel = reinterpret_cast<T *>(vvec_top(uvec_coord));
auto *vvec_bottom_pixel = reinterpret_cast<T *>(vvec_bottom(uvec_coord));
T border_value(0);
int rvec_val = validation::tensor_elem_at(rvec, coord, BorderMode::CONSTANT, border_value);
int gvec_val = validation::tensor_elem_at(gvec, coord, BorderMode::CONSTANT, border_value);
int bvec_val = validation::tensor_elem_at(bvec, coord, BorderMode::CONSTANT, border_value);
float result = rvec_val * rgb2yuv_bt709_kr + gvec_val * rgb2yuv_bt709_kg + bvec_val * rgb2yuv_bt709_kb;
yvec_pixel[0] = result;
uvec_top_pixel[0] = (bvec_val - result) * rgb2yuv_bt709_cu + 128.f;
vvec_top_pixel[0] = (rvec_val - result) * rgb2yuv_bt709_cv + 128.f;
coord.set(0, x + 1);
rvec_val = validation::tensor_elem_at(rvec, coord, BorderMode::CONSTANT, border_value);
gvec_val = validation::tensor_elem_at(gvec, coord, BorderMode::CONSTANT, border_value);
bvec_val = validation::tensor_elem_at(bvec, coord, BorderMode::CONSTANT, border_value);
result = rvec_val * rgb2yuv_bt709_kr + gvec_val * rgb2yuv_bt709_kg + bvec_val * rgb2yuv_bt709_kb;
yvec_pixel[1] = result;
uvec_bottom_pixel[0] = (bvec_val - result) * rgb2yuv_bt709_cu + 128.f;
vvec_bottom_pixel[0] = (rvec_val - result) * rgb2yuv_bt709_cv + 128.f;
uvec_coord.set(0, ++uvec_coord_x);
}
}
}
inline float compute_rgb_value(int y_value, int v_value, int u_value, unsigned char channel_idx)
{
float result = 0.f;
switch(channel_idx)
{
case 0:
{
const float red = (v_value - 128.f) * red_coef_bt709;
result = y_value + red;
break;
}
case 1:
{
const float green = (u_value - 128.f) * green_coef_bt709 + (v_value - 128.f) * green_coef2_bt709;
result = y_value + green;
break;
}
case 2:
{
const float blue = (u_value - 128.f) * blue_coef_bt709;
result = y_value + blue;
break;
}
default:
{
//Assuming Alpha channel
return 255;
}
}
return std::min(std::max(0.f, result), 255.f);
}
template <typename T>
inline void yuyv_to_rgb_calculation(const SimpleTensor<T> yvec, const SimpleTensor<T> vvec, const SimpleTensor<T> yyvec, const SimpleTensor<T> uvec, SimpleTensor<T> &dst)
{
const int dst_width = dst.shape().x();
const int dst_height = dst.shape().y();
for(int y = 0; y < dst_height; ++y)
{
int x_coord = 0;
for(int x = 0; x < dst_width; x += 2, ++x_coord)
{
const Coordinates dst_coord{ x, y };
auto *dst_pixel = reinterpret_cast<T *>(dst(dst_coord));
const T border_value(0);
const int yvec_val = validation::tensor_elem_at(yvec, { x_coord, y }, BorderMode::CONSTANT, border_value);
const int vvec_val = validation::tensor_elem_at(vvec, { x_coord, y }, BorderMode::CONSTANT, border_value);
const int yyvec_val = validation::tensor_elem_at(yyvec, { x_coord, y }, BorderMode::CONSTANT, border_value);
const int uvec_val = validation::tensor_elem_at(uvec, { x_coord, y }, BorderMode::CONSTANT, border_value);
//Compute first RGB value using Y plane
for(int channel_idx = 0; channel_idx < dst.num_channels(); ++channel_idx)
{
const float channel_value = compute_rgb_value(yvec_val, vvec_val, uvec_val, channel_idx);
dst_pixel[channel_idx] = channel_value;
}
//Compute second RGB value using YY plane
const Coordinates dst_coord2
{
x + 1, y
};
dst_pixel = reinterpret_cast<T *>(dst(dst_coord2));
for(int channel_idx = 0; channel_idx < dst.num_channels(); ++channel_idx)
{
const float channel_value = compute_rgb_value(yyvec_val, vvec_val, uvec_val, channel_idx);
dst_pixel[channel_idx] = channel_value;
}
}
}
}
template <typename T>
inline void colorconvert_rgb_to_rgbx(const SimpleTensor<T> src, SimpleTensor<T> &dst)
{
for(int channel_idx = 0; channel_idx < dst.num_channels(); ++channel_idx)
{
const int width = dst.shape().x();
const int height = dst.shape().y();
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
const Coordinates src_coord{ x, y };
const Coordinates dst_coord{ x, y };
const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
auto *dst_pixel = reinterpret_cast<T *>(dst(dst_coord));
if(channel_idx == 3)
{
dst_pixel[channel_idx] = 255;
continue;
}
dst_pixel[channel_idx] = src_pixel[channel_idx];
}
}
}
}
template <typename T>
inline void colorconvert_rgb_to_u8(const SimpleTensor<T> src, SimpleTensor<T> &dst)
{
const int width = dst.shape().x();
const int height = dst.shape().y();
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
const Coordinates src_coord{ x, y };
const Coordinates dst_coord{ x, y };
const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
auto *dst_pixel = reinterpret_cast<T *>(dst(dst_coord));
const float result = rgb2u8_red_coef * src_pixel[0] + rgb2u8_green_coef * src_pixel[1] + rgb2u8_blue_coef * src_pixel[2];
dst_pixel[0] = utility::clamp<float>(result, 0, 255);
}
}
}
template <typename T>
inline void colorconvert_rgbx_to_rgb(const SimpleTensor<T> src, SimpleTensor<T> &dst)
{
for(int channel_idx = 0; channel_idx < dst.num_channels(); ++channel_idx)
{
const int width = dst.shape().x();
const int height = dst.shape().y();
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
const Coordinates src_coord{ x, y };
const Coordinates dst_coord{ x, y };
const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
auto *dst_pixel = reinterpret_cast<T *>(dst(dst_coord));
dst_pixel[channel_idx] = src_pixel[channel_idx];
}
}
}
}
template <typename T>
inline void colorconvert_yuyv_to_rgb(const SimpleTensor<T> src, const Format format, SimpleTensor<T> &dst)
{
SimpleTensor<T> yvec(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
SimpleTensor<T> uvec(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
SimpleTensor<T> yyvec(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
SimpleTensor<T> vvec(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
const int step_x = (Format::YUYV422 == format || Format::UYVY422 == format) ? 2 : 1;
const int offset = (Format::YUYV422 == format) ? 0 : 1;
Coordinates elem_coord{ 0, 0 };
const int width = yvec.shape().x();
const int height = yvec.shape().y();
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
const Coordinates src_coord{ x * step_x, y };
const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
auto *yvec_pixel = reinterpret_cast<T *>(yvec(elem_coord));
auto *uvec_pixel = reinterpret_cast<T *>(uvec(elem_coord));
auto *yyvec_pixel = reinterpret_cast<T *>(yyvec(elem_coord));
auto *vvec_pixel = reinterpret_cast<T *>(vvec(elem_coord));
yvec_pixel[x] = src_pixel[0 + offset];
uvec_pixel[x] = src_pixel[1 - offset];
yyvec_pixel[x] = src_pixel[2 + offset];
vvec_pixel[x] = src_pixel[3 - offset];
}
elem_coord.set(1, y + 1);
}
yuyv_to_rgb_calculation(yvec, vvec, yyvec, uvec, dst);
}
template <typename T>
inline void colorconvert_iyuv_to_rgb(const std::vector<SimpleTensor<T>> &tensor_planes, SimpleTensor<T> &dst)
{
SimpleTensor<T> yvec(TensorShape{ tensor_planes[0].shape().x() / 2, tensor_planes[0].shape().y() }, Format::U8);
SimpleTensor<T> uvec(TensorShape{ tensor_planes[0].shape().x() / 2, tensor_planes[0].shape().y() }, Format::U8);
SimpleTensor<T> yyvec(TensorShape{ tensor_planes[0].shape().x() / 2, tensor_planes[0].shape().y() }, Format::U8);
SimpleTensor<T> vvec(TensorShape{ tensor_planes[0].shape().x() / 2, tensor_planes[0].shape().y() }, Format::U8);
Coordinates elem_coord{ 0, 0 };
const int yvec_width = yvec.shape().x();
const int yvec_height = yvec.shape().y();
for(int y = 0; y < yvec_height; ++y)
{
for(int x = 0; x < yvec_width; ++x)
{
const Coordinates src_coord{ x, y };
const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[0](src_coord));
auto *yvec_pixel = reinterpret_cast<T *>(yvec(elem_coord));
auto *yyvec_pixel = reinterpret_cast<T *>(yyvec(elem_coord));
yvec_pixel[x] = src_pixel[x];
yyvec_pixel[x] = src_pixel[x + 1];
}
elem_coord.set(1, y + 1);
}
const int uvec_width = uvec.shape().x();
const int uvec_height = uvec.shape().y();
Coordinates top_elem_coord{ 0, 0 };
Coordinates bottom_elem_coord{ 0, 1 };
for(int y = 0; y < uvec_height; y += 2)
{
for(int x = 0; x < uvec_width; ++x)
{
const Coordinates src_coord{ x, y / 2 };
const auto *u_pixel = reinterpret_cast<const T *>(tensor_planes[1](src_coord));
const auto *v_pixel = reinterpret_cast<const T *>(tensor_planes[2](src_coord));
auto *uvec_pixel_top = reinterpret_cast<T *>(uvec(top_elem_coord));
auto *vvec_pixel_top = reinterpret_cast<T *>(vvec(top_elem_coord));
auto *uvec_pixel_bottom = reinterpret_cast<T *>(uvec(bottom_elem_coord));
auto *vvec_pixel_bottom = reinterpret_cast<T *>(vvec(bottom_elem_coord));
uvec_pixel_top[x] = u_pixel[0];
vvec_pixel_top[x] = v_pixel[0];
uvec_pixel_bottom[x] = u_pixel[0];
vvec_pixel_bottom[x] = v_pixel[0];
}
top_elem_coord.set(1, y + 2);
bottom_elem_coord.set(1, top_elem_coord.y() + 1);
}
yuyv_to_rgb_calculation(yvec, vvec, yyvec, uvec, dst);
}
template <typename T>
inline void colorconvert_nv12_to_rgb(const Format format, const std::vector<SimpleTensor<T>> &tensor_planes, SimpleTensor<T> &dst)
{
SimpleTensor<T> yvec(TensorShape{ tensor_planes[0].shape().x() / 2, tensor_planes[0].shape().y() }, Format::U8);
SimpleTensor<T> uvec(TensorShape{ tensor_planes[0].shape().x() / 2, tensor_planes[0].shape().y() }, Format::U8);
SimpleTensor<T> yyvec(TensorShape{ tensor_planes[0].shape().x() / 2, tensor_planes[0].shape().y() }, Format::U8);
SimpleTensor<T> vvec(TensorShape{ tensor_planes[0].shape().x() / 2, tensor_planes[0].shape().y() }, Format::U8);
const int offset = (Format::NV12 == format) ? 0 : 1;
Coordinates elem_coord{ 0, 0 };
const int yvec_width = yvec.shape().x();
const int yvec_height = yvec.shape().y();
for(int y = 0; y < yvec_height; ++y)
{
for(int x = 0; x < yvec_width; ++x)
{
const Coordinates src_coord{ x, y };
const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[0](src_coord));
auto *yvec_pixel = reinterpret_cast<T *>(yvec(elem_coord));
auto *yyvec_pixel = reinterpret_cast<T *>(yyvec(elem_coord));
yvec_pixel[x] = src_pixel[x];
yyvec_pixel[x] = src_pixel[x + 1];
}
elem_coord.set(1, y + 1);
}
const int uvec_width = uvec.shape().x();
const int uvec_height = uvec.shape().y();
Coordinates top_elem_coord{ 0, 0 };
Coordinates bottom_elem_coord{ 0, 1 };
for(int y = 0; y < uvec_height; y += 2)
{
for(int x = 0; x < uvec_width; ++x)
{
const Coordinates src_coord{ x, y / 2 };
const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[1](src_coord));
auto *uvec_pixel_top = reinterpret_cast<T *>(uvec(top_elem_coord));
auto *vvec_pixel_top = reinterpret_cast<T *>(vvec(top_elem_coord));
auto *uvec_pixel_bottom = reinterpret_cast<T *>(uvec(bottom_elem_coord));
auto *vvec_pixel_bottom = reinterpret_cast<T *>(vvec(bottom_elem_coord));
uvec_pixel_top[x] = src_pixel[0 + offset];
vvec_pixel_top[x] = src_pixel[1 - offset];
uvec_pixel_bottom[x] = src_pixel[0 + offset];
vvec_pixel_bottom[x] = src_pixel[1 - offset];
}
top_elem_coord.set(1, y + 2);
bottom_elem_coord.set(1, top_elem_coord.y() + 1);
}
yuyv_to_rgb_calculation(yvec, vvec, yyvec, uvec, dst);
}
template <typename T>
inline void colorconvert_rgb_to_nv12(const SimpleTensor<T> src, std::vector<SimpleTensor<T>> &dst)
{
SimpleTensor<T> rvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
SimpleTensor<T> gvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
SimpleTensor<T> bvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
SimpleTensor<T> yvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
int vec_shape_x = src.shape().x() * src.shape().y();
SimpleTensor<T> uvec_top(TensorShape{ vec_shape_x, 1U }, Format::U8);
SimpleTensor<T> uvec_bottom(TensorShape{ vec_shape_x, 1U }, Format::U8);
SimpleTensor<T> vvec_top(TensorShape{ vec_shape_x, 1U }, Format::U8);
SimpleTensor<T> vvec_bottom(TensorShape{ vec_shape_x, 1U }, Format::U8);
store_rgb_from_src(src, rvec, gvec, bvec);
rgb_to_yuv_calculation(rvec, gvec, bvec, dst[0], uvec_top, uvec_bottom, vvec_top, vvec_bottom);
SimpleTensor<T> utmp(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
SimpleTensor<T> vtmp(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
uint32_t utmp_width = utmp.shape().x();
uint32_t utmp_height = utmp.shape().y();
uint32_t uvec_coord_x = 0;
uint32_t uvec_coord_y = 0;
Coordinates uvec_coord{ uvec_coord_x, uvec_coord_y };
for(uint32_t y = 0; y < utmp_height; y++)
{
for(uint32_t x = 0; x < utmp_width; x++)
{
Coordinates coord{ x, y };
auto *utmp_pixel = reinterpret_cast<T *>(utmp(coord));
auto *vtmp_pixel = reinterpret_cast<T *>(vtmp(coord));
T border_value(0);
int uvec_top_val = validation::tensor_elem_at(uvec_top, uvec_coord, BorderMode::CONSTANT, border_value);
int uvec_bottom_val = validation::tensor_elem_at(uvec_bottom, uvec_coord, BorderMode::CONSTANT, border_value);
int vvec_top_val = validation::tensor_elem_at(vvec_top, uvec_coord, BorderMode::CONSTANT, border_value);
int vvec_bottom_val = validation::tensor_elem_at(vvec_bottom, uvec_coord, BorderMode::CONSTANT, border_value);
utmp_pixel[0] = std::ceil(float(uvec_top_val + uvec_bottom_val) / 2);
vtmp_pixel[0] = std::ceil(float(vvec_top_val + vvec_bottom_val) / 2);
uvec_coord.set(0, ++uvec_coord_x);
}
}
uint32_t second_plane_x = dst[1].shape().x();
uint32_t second_plane_y = dst[1].shape().y();
uint32_t utmp_coord_x = 0;
uint32_t utmp_coord_y = 0;
for(uint32_t y = 0; y < second_plane_y; y++)
{
for(uint32_t x = 0; x < second_plane_x; x++)
{
Coordinates coord{ x, y };
Coordinates utmp_top_coord{ utmp_coord_x, utmp_coord_y };
Coordinates utmp_bottom_coord{ utmp_coord_x, utmp_coord_y + 1 };
auto *dst_pixel = reinterpret_cast<T *>(dst[1](coord));
T border_value(0);
int utmp_top_val = validation::tensor_elem_at(utmp, utmp_top_coord, BorderMode::CONSTANT, border_value);
int utmp_bottom_val = validation::tensor_elem_at(utmp, utmp_bottom_coord, BorderMode::CONSTANT, border_value);
int result = (utmp_top_val + utmp_bottom_val) / 2;
dst_pixel[0] = result;
int vtmp_top_val = validation::tensor_elem_at(vtmp, utmp_top_coord, BorderMode::CONSTANT, border_value);
int vtmp_bottom_val = validation::tensor_elem_at(vtmp, utmp_bottom_coord, BorderMode::CONSTANT, border_value);
result = (vtmp_top_val + vtmp_bottom_val) / 2;
dst_pixel[1] = result;
utmp_coord_x++;
if(utmp_coord_x >= utmp_width)
{
utmp_coord_x = 0;
utmp_coord_y += 2;
}
}
}
}
template <typename T>
inline void colorconvert_rgb_to_iyuv(const SimpleTensor<T> src, std::vector<SimpleTensor<T>> &dst)
{
SimpleTensor<T> rvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
SimpleTensor<T> gvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
SimpleTensor<T> bvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
SimpleTensor<T> yvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
int vec_shape_x = src.shape().x() * src.shape().y();
SimpleTensor<T> uvec_top(TensorShape{ vec_shape_x, 1U }, Format::U8);
SimpleTensor<T> uvec_bottom(TensorShape{ vec_shape_x, 1U }, Format::U8);
SimpleTensor<T> vvec_top(TensorShape{ vec_shape_x, 1U }, Format::U8);
SimpleTensor<T> vvec_bottom(TensorShape{ vec_shape_x, 1U }, Format::U8);
store_rgb_from_src(src, rvec, gvec, bvec);
rgb_to_yuv_calculation(rvec, gvec, bvec, dst[0], uvec_top, uvec_bottom, vvec_top, vvec_bottom);
SimpleTensor<T> utmp(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
SimpleTensor<T> vtmp(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
uint32_t utmp_width = utmp.shape().x();
uint32_t utmp_height = utmp.shape().y();
uint32_t uvec_coord_x = 0;
uint32_t uvec_coord_y = 0;
Coordinates uvec_coord{ uvec_coord_x, uvec_coord_y };
for(uint32_t y = 0; y < utmp_height; y++)
{
for(uint32_t x = 0; x < utmp_width; x++)
{
Coordinates coord{ x, y };
auto *utmp_pixel = reinterpret_cast<T *>(utmp(coord));
auto *vtmp_pixel = reinterpret_cast<T *>(vtmp(coord));
T border_value(0);
int uvec_top_val = validation::tensor_elem_at(uvec_top, uvec_coord, BorderMode::CONSTANT, border_value);
int uvec_bottom_val = validation::tensor_elem_at(uvec_bottom, uvec_coord, BorderMode::CONSTANT, border_value);
int vvec_top_val = validation::tensor_elem_at(vvec_top, uvec_coord, BorderMode::CONSTANT, border_value);
int vvec_bottom_val = validation::tensor_elem_at(vvec_bottom, uvec_coord, BorderMode::CONSTANT, border_value);
utmp_pixel[0] = std::ceil(float(uvec_top_val + uvec_bottom_val) / 2);
vtmp_pixel[0] = std::ceil(float(vvec_top_val + vvec_bottom_val) / 2);
uvec_coord.set(0, ++uvec_coord_x);
}
}
uint32_t second_plane_x = dst[1].shape().x();
uint32_t second_plane_y = dst[1].shape().y();
uint32_t utmp_coord_x = 0;
uint32_t utmp_coord_y = 0;
for(uint32_t y = 0; y < second_plane_y; y++)
{
for(uint32_t x = 0; x < second_plane_x; x++)
{
Coordinates coord{ x, y };
Coordinates utmp_top_coord{ utmp_coord_x, utmp_coord_y };
Coordinates utmp_bottom_coord{ utmp_coord_x, utmp_coord_y + 1 };
auto *u_pixel = reinterpret_cast<T *>(dst[1](coord));
auto *v_pixel = reinterpret_cast<T *>(dst[2](coord));
T border_value(0);
int utmp_top_val = validation::tensor_elem_at(utmp, utmp_top_coord, BorderMode::CONSTANT, border_value);
int utmp_bottom_val = validation::tensor_elem_at(utmp, utmp_bottom_coord, BorderMode::CONSTANT, border_value);
int result = (utmp_top_val + utmp_bottom_val) / 2;
u_pixel[0] = result;
int vtmp_top_val = validation::tensor_elem_at(vtmp, utmp_top_coord, BorderMode::CONSTANT, border_value);
int vtmp_bottom_val = validation::tensor_elem_at(vtmp, utmp_bottom_coord, BorderMode::CONSTANT, border_value);
result = (vtmp_top_val + vtmp_bottom_val) / 2;
v_pixel[0] = result;
utmp_coord_x++;
if(utmp_coord_x >= utmp_width)
{
utmp_coord_x = 0;
utmp_coord_y += 2;
}
}
}
}
template <typename T>
inline void colorconvert_rgb_to_yuv4(const SimpleTensor<T> src, std::vector<SimpleTensor<T>> &dst)
{
SimpleTensor<T> rvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
SimpleTensor<T> gvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
SimpleTensor<T> bvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
SimpleTensor<T> yvec(TensorShape{ dst[0].shape().x(), dst[0].shape().y() }, Format::U8);
int vec_shape_x = src.shape().x() * src.shape().y();
SimpleTensor<T> uvec_top(TensorShape{ vec_shape_x, 1U }, Format::U8);
SimpleTensor<T> uvec_bottom(TensorShape{ vec_shape_x, 1U }, Format::U8);
SimpleTensor<T> vvec_top(TensorShape{ vec_shape_x, 1U }, Format::U8);
SimpleTensor<T> vvec_bottom(TensorShape{ vec_shape_x, 1U }, Format::U8);
int width = src.shape().x();
int height = src.shape().y();
store_rgb_from_src(src, rvec, gvec, bvec);
rgb_to_yuv_calculation(rvec, gvec, bvec, dst[0], uvec_top, uvec_bottom, vvec_top, vvec_bottom);
int uvec_coord_x = 0;
int uvec_coord_y = 0;
Coordinates uvec_coord{ uvec_coord_x, uvec_coord_y };
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x += 2)
{
Coordinates coord{ x, y };
auto *plane_1_pixel = reinterpret_cast<T *>(dst[1](coord));
auto *plane_2_pixel = reinterpret_cast<T *>(dst[2](coord));
T border_value(0);
int uvec_top_val = validation::tensor_elem_at(uvec_top, uvec_coord, BorderMode::CONSTANT, border_value);
int uvec_bottom_val = validation::tensor_elem_at(uvec_bottom, uvec_coord, BorderMode::CONSTANT, border_value);
plane_1_pixel[0] = uvec_top_val;
plane_1_pixel[1] = uvec_bottom_val;
int vvec_top_val = validation::tensor_elem_at(vvec_top, uvec_coord, BorderMode::CONSTANT, border_value);
int vvec_bottom_val = validation::tensor_elem_at(vvec_bottom, uvec_coord, BorderMode::CONSTANT, border_value);
plane_2_pixel[0] = vvec_top_val;
plane_2_pixel[1] = vvec_bottom_val;
uvec_coord.set(0, ++uvec_coord_x);
}
}
}
template <typename T>
inline void colorconvert_yuyv_to_nv12(const SimpleTensor<T> src, const Format format, std::vector<SimpleTensor<T>> &dst)
{
SimpleTensor<T> uvvec_top(TensorShape{ dst[0].shape().x(), dst[0].shape().y() / 2 }, Format::U8);
SimpleTensor<T> uvvec_bottom(TensorShape{ dst[0].shape().x(), dst[0].shape().y() / 2 }, Format::U8);
const int offset = (Format::YUYV422 == format) ? 0 : 1;
int width = dst[0].shape().x();
int height = dst[0].shape().y();
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; x++)
{
const Coordinates dst_coord{ x, y };
const Coordinates uv_coord{ x, y / 2 };
const auto *src_pixel = reinterpret_cast<const T *>(src(dst_coord));
auto *y_pixel = reinterpret_cast<T *>(dst[0](dst_coord));
auto *uvvec_top_pixel = reinterpret_cast<T *>(uvvec_top(uv_coord));
auto *uvvec_bottom_pixel = reinterpret_cast<T *>(uvvec_bottom(uv_coord));
y_pixel[0] = src_pixel[0 + offset];
if(y % 2 == 0)
{
uvvec_top_pixel[0] = src_pixel[1 - offset];
}
else
{
uvvec_bottom_pixel[0] = src_pixel[1 - offset];
}
}
}
width = dst[1].shape().x();
height = dst[1].shape().y();
int uv_coord_x = 0;
int uv_coord_y = 0;
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; x++)
{
const Coordinates dst_coord{ x, y };
const Coordinates uv_coord{ uv_coord_x, uv_coord_y };
auto *uv_pixel = reinterpret_cast<T *>(dst[1](dst_coord));
const auto *uvvec_top_pixel = reinterpret_cast<T *>(uvvec_top(uv_coord));
const auto *uvvec_bottom_pixel = reinterpret_cast<T *>(uvvec_bottom(uv_coord));
uv_pixel[0] = (uvvec_top_pixel[0] + uvvec_bottom_pixel[0]) / 2;
uv_pixel[1] = (uvvec_top_pixel[1] + uvvec_bottom_pixel[1]) / 2;
uv_coord_x += 2;
}
uv_coord_x = 0;
uv_coord_y++;
}
}
template <typename T>
inline void colorconvert_yuyv_to_iyuv(const SimpleTensor<T> src, const Format format, std::vector<SimpleTensor<T>> &dst)
{
SimpleTensor<T> uvvec_top(TensorShape{ dst[0].shape().x(), dst[0].shape().y() / 2 }, Format::U8);
SimpleTensor<T> uvvec_bottom(TensorShape{ dst[0].shape().x(), dst[0].shape().y() / 2 }, Format::U8);
const int offset = (Format::YUYV422 == format) ? 0 : 1;
int width = dst[0].shape().x();
int height = dst[0].shape().y();
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; x++)
{
const Coordinates dst_coord{ x, y };
const Coordinates uv_coord{ x, y / 2 };
const auto *src_pixel = reinterpret_cast<const T *>(src(dst_coord));
auto *y_pixel = reinterpret_cast<T *>(dst[0](dst_coord));
auto *uvvec_top_pixel = reinterpret_cast<T *>(uvvec_top(uv_coord));
auto *uvvec_bottom_pixel = reinterpret_cast<T *>(uvvec_bottom(uv_coord));
y_pixel[0] = src_pixel[0 + offset];
if(y % 2 == 0)
{
uvvec_top_pixel[0] = src_pixel[1 - offset];
}
else
{
uvvec_bottom_pixel[0] = src_pixel[1 - offset];
}
}
}
width = dst[1].shape().x();
height = dst[1].shape().y();
int uv_coord_x = 0;
int uv_coord_y = 0;
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; x++)
{
const Coordinates dst_coord{ x, y };
const Coordinates uv_coord{ uv_coord_x, uv_coord_y };
auto *u_pixel = reinterpret_cast<T *>(dst[1](dst_coord));
auto *v_pixel = reinterpret_cast<T *>(dst[2](dst_coord));
const auto *uvvec_top_pixel = reinterpret_cast<T *>(uvvec_top(uv_coord));
const auto *uvvec_bottom_pixel = reinterpret_cast<T *>(uvvec_bottom(uv_coord));
u_pixel[0] = (uvvec_top_pixel[0] + uvvec_bottom_pixel[0]) / 2;
v_pixel[0] = (uvvec_top_pixel[1] + uvvec_bottom_pixel[1]) / 2;
uv_coord_x += 2;
}
uv_coord_x = 0;
uv_coord_y++;
}
}
template <typename T>
inline void nv_to_iyuv(const SimpleTensor<T> src, const Format src_format, SimpleTensor<T> &nv1, SimpleTensor<T> &nv2)
{
int width = src.shape().x();
int height = src.shape().y();
const int offset = (Format::NV12 == src_format) ? 1 : 0;
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; x++)
{
const Coordinates src_coord{ x, y };
const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
auto *u_pixel = reinterpret_cast<T *>(nv1(src_coord));
auto *v_pixel = reinterpret_cast<T *>(nv2(src_coord));
u_pixel[0] = src_pixel[1 - offset];
v_pixel[0] = src_pixel[0 + offset];
}
}
}
template <typename T>
inline void nv_to_yuv4(const SimpleTensor<T> src, const Format src_format, SimpleTensor<T> &nv1, SimpleTensor<T> &nv2)
{
int width = src.shape().x();
int height = src.shape().y();
const int offset = (Format::NV12 == src_format) ? 1 : 0;
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; x++)
{
const Coordinates src_coord{ x, y };
Coordinates dst_coord{ x * 2, y * 2 };
const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
auto *u_pixel = reinterpret_cast<T *>(nv1(dst_coord));
auto *v_pixel = reinterpret_cast<T *>(nv2(dst_coord));
u_pixel[0] = src_pixel[1 - offset];
u_pixel[1] = src_pixel[1 - offset];
v_pixel[0] = src_pixel[0 + offset];
v_pixel[1] = src_pixel[0 + offset];
dst_coord.set(1, y * 2 + 1);
u_pixel = reinterpret_cast<T *>(nv1(dst_coord));
v_pixel = reinterpret_cast<T *>(nv2(dst_coord));
u_pixel[0] = src_pixel[1 - offset];
u_pixel[1] = src_pixel[1 - offset];
v_pixel[0] = src_pixel[0 + offset];
v_pixel[1] = src_pixel[0 + offset];
}
}
}
template <typename T>
inline void colorconvert_nv_to_iyuv(const std::vector<SimpleTensor<T>> src, const Format src_format, std::vector<SimpleTensor<T>> &dst)
{
int width = dst[0].shape().x();
int height = dst[0].shape().y();
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
const Coordinates dst_coord{ x, y };
const auto *src_pixel = reinterpret_cast<const T *>(src[0](dst_coord));
auto *y_pixel = reinterpret_cast<T *>(dst[0](dst_coord));
y_pixel[0] = src_pixel[0];
}
}
nv_to_iyuv(src[1], src_format, dst[1], dst[2]);
}
template <typename T>
inline void colorconvert_nv_to_yuv4(const std::vector<SimpleTensor<T>> src, const Format src_format, std::vector<SimpleTensor<T>> &dst)
{
int width = dst[0].shape().x();
int height = dst[0].shape().y();
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
const Coordinates dst_coord{ x, y };
const auto *src_pixel = reinterpret_cast<const T *>(src[0](dst_coord));
auto *y_pixel = reinterpret_cast<T *>(dst[0](dst_coord));
y_pixel[0] = src_pixel[0];
}
}
nv_to_yuv4(src[1], src_format, dst[1], dst[2]);
}
} // namespace detail
} // color_convert_helper
} // namespace test
} // namespace arm_compute
#endif /*ARM_COMPUTE_TEST_VALIDATION_COLOR_CONVERT_H */
|