Skip to content

Commit 9eab4b0

Browse files
vtfpp: add ImagePixel::transform and ImagePixel::transformInPlace
Co-authored-by: yuuko <yuuko+of@partyvan.io>
1 parent 5512f21 commit 9eab4b0

2 files changed

Lines changed: 203 additions & 377 deletions

File tree

include/vtfpp/ImagePixel.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <bit>
44
#include <concepts>
55
#include <cstring>
6-
#include <functional>
76

87
#ifdef SOURCEPP_BUILD_WITH_TBB
98
#include <execution>
@@ -519,4 +518,48 @@ template<PixelType P, typename C>
519518
return out;
520519
}
521520

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+
522565
} // namespace vtfpp::ImagePixel

0 commit comments

Comments
 (0)