Skip to content

Commit

Permalink
Add template helpers to PixelFrame
Browse files Browse the repository at this point in the history
Summary: These simple template methods make working with pixel data more expressive. No functional change.

Reviewed By: kiminoue7

Differential Revision: D53601784

fbshipit-source-id: a58857cda384e6cb2482a473b4560219ca49427f
  • Loading branch information
Georges Berenger authored and facebook-github-bot committed Feb 9, 2024
1 parent 5df6304 commit a0a96c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions vrs/utils/PixelFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ bool PixelFrame::normalizeFrame(shared_ptr<PixelFrame>& normalizedFrame, bool gr
normalizedFrame->wdata(), rdata(), getWidth(), getHeight(), getStride())) {
return false;
}
uint16_t* ptr = reinterpret_cast<uint16_t*>(normalizedFrame->wdata());
uint16_t* ptr = normalizedFrame->data<uint16_t>();
const uint32_t pixelCount = getWidth() * getHeight() * componentCount;
for (uint32_t i = 0; i < pixelCount; ++i) {
ptr[i] <<= bitsToShift;
Expand Down Expand Up @@ -706,15 +706,15 @@ bool PixelFrame::normalizeFrame(shared_ptr<PixelFrame>& normalizedFrame, bool gr
}
} else if (format == PixelFormat::GREY16 && bitsToShift > 0) {
// 12/10 bit pixel scaling to 16 bit
const uint16_t* srcPtr = reinterpret_cast<const uint16_t*>(rdata());
uint16_t* outPtr = reinterpret_cast<uint16_t*>(normalizedFrame->wdata());
const uint16_t* srcPtr = data<uint16_t>();
uint16_t* outPtr = normalizedFrame->data<uint16_t>();
const uint32_t pixelCount = getWidth() * getHeight() * componentCount;
for (uint32_t i = 0; i < pixelCount; ++i) {
outPtr[i] = static_cast<uint16_t>(srcPtr[i] << bitsToShift);
}
} else if (XR_VERIFY(this->size() == 2 * normalizedFrame->size())) {
// 16/12/10 bit pixel reduction to 8 bit
const uint16_t* srcPtr = reinterpret_cast<const uint16_t*>(rdata());
const uint16_t* srcPtr = data<uint16_t>();
uint8_t* outPtr = normalizedFrame->wdata();
const uint32_t pixelCount = getWidth() * getHeight() * componentCount;
for (uint32_t i = 0; i < pixelCount; ++i) {
Expand Down
8 changes: 8 additions & 0 deletions vrs/utils/PixelFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ class PixelFrame {
uint8_t* wdata() {
return frameBytes_.data();
}
template <class T>
inline const T* data(size_t byte_offset = 0) const {
return reinterpret_cast<const T*>(frameBytes_.data() + byte_offset);
}
template <class T>
inline T* data(size_t byte_offset = 0) {
return reinterpret_cast<T*>(frameBytes_.data() + byte_offset);
}
size_t size() const {
return frameBytes_.size();
}
Expand Down

0 comments on commit a0a96c6

Please sign in to comment.