|
3 | 3 | #include <bit> |
4 | 4 | #include <concepts> |
5 | 5 | #include <cstring> |
6 | | -#include <functional> |
7 | 6 |
|
8 | 7 | #ifdef SOURCEPP_BUILD_WITH_TBB |
9 | 8 | #include <execution> |
@@ -519,4 +518,48 @@ template<PixelType P, typename C> |
519 | 518 | return out; |
520 | 519 | } |
521 | 520 |
|
| 521 | +/// Run a parallelizable/vectorizable operation on the given image data. |
| 522 | +template<PixelType IN, typename Func> |
| 523 | +void transformInPlace(std::span<std::byte> imageData, Func callback) { |
| 524 | + if (imageData.empty()) { |
| 525 | + return; |
| 526 | + } |
| 527 | + |
| 528 | + std::span imageDataSpan{reinterpret_cast<IN*>(imageData.data()), imageData.size() / sizeof(IN)}; |
| 529 | + std::transform( |
| 530 | +#ifdef SOURCEPP_BUILD_WITH_TBB |
| 531 | + std::execution::par_unseq, |
| 532 | +#endif |
| 533 | + imageDataSpan.begin(), |
| 534 | + imageDataSpan.end(), |
| 535 | + imageDataSpan.begin(), |
| 536 | + callback |
| 537 | + ); |
| 538 | +} |
| 539 | + |
| 540 | +/// Run a parallelizable/vectorizable operation on the given image data, and return new image data. |
| 541 | +template<PixelType IN, PixelType OUT, typename Func> |
| 542 | +[[nodiscard]] std::vector<std::byte> transform(std::span<const std::byte> imageData, Func callback) { |
| 543 | + if (imageData.empty()) { |
| 544 | + return {}; |
| 545 | + } |
| 546 | + |
| 547 | + std::vector<std::byte> newData; |
| 548 | + newData.resize(imageData.size() / sizeof(IN) * sizeof(OUT)); |
| 549 | + std::span newDataSpan{reinterpret_cast<OUT*>(newData.data()), newData.size() / sizeof(OUT)}; |
| 550 | + |
| 551 | + std::span imageDataSpan{reinterpret_cast<const IN*>(imageData.data()), imageData.size() / sizeof(IN)}; |
| 552 | + std::transform( |
| 553 | +#ifdef SOURCEPP_BUILD_WITH_TBB |
| 554 | + std::execution::par_unseq, |
| 555 | +#endif |
| 556 | + imageDataSpan.begin(), |
| 557 | + imageDataSpan.end(), |
| 558 | + newDataSpan.begin(), |
| 559 | + callback |
| 560 | + ); |
| 561 | + |
| 562 | + return newData; |
| 563 | +} |
| 564 | + |
522 | 565 | } // namespace vtfpp::ImagePixel |
0 commit comments