Skip to content

Commit

Permalink
feat: implement flip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodgomesc committed Jan 30, 2024
1 parent 7fc2715 commit 92d73e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions android/src/main/cpp/ResizePlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,36 @@ FrameBuffer ResizePlugin::cropARGBBuffer(vision::FrameBuffer frameBuffer, int x,
return destination;
}

FrameBuffer ResizePlugin::flipARGBBuffer(FrameBuffer frameBuffer, bool flip) {
if (!flip) {
return frameBuffer;
}

__android_log_print(ANDROID_LOG_INFO, TAG, "Flipping ARGB buffer...");

size_t channels = getChannelCount(PixelFormat::ARGB);
size_t channelSize = getBytesPerChannel(DataType::UINT8);
size_t argbSize = frameBuffer.width * frameBuffer.height * channels * channelSize;
if (_flipBuffer == nullptr || _flipBuffer->getDirectSize() != argbSize) {
_flipBuffer = allocateBuffer(argbSize, "_flipBuffer");
}
FrameBuffer destination = {
.width = frameBuffer.width,
.height = frameBuffer.height,
.pixelFormat = PixelFormat::ARGB,
.dataType = DataType::UINT8,
.buffer = _flipBuffer,
};

int status = libyuv::ARGBMirror(frameBuffer.data(), frameBuffer.bytesPerRow(),
destination.data(), destination.bytesPerRow(),
frameBuffer.width, frameBuffer.height);
if (status != 0) {
throw std::runtime_error("Failed to flip ARGB Buffer! Status: " + std::to_string(status));
}

return destination;
}

FrameBuffer ResizePlugin::rotateARGBBuffer(FrameBuffer frameBuffer, int rotation) {
if (rotation == 0) {
Expand Down Expand Up @@ -324,6 +354,7 @@ jni::global_ref<jni::JByteBuffer> ResizePlugin::resize(jni::alias_ref<JImage> im
int cropWidth, int cropHeight,
int scaleWidth, int scaleHeight,
int rotation,
bool flip,
int /* PixelFormat */ pixelFormatOrdinal,
int /* DataType */ dataTypeOrdinal)
{
Expand All @@ -342,6 +373,9 @@ jni::global_ref<jni::JByteBuffer> ResizePlugin::resize(jni::alias_ref<JImage> im
// 4. Rotate ARGB
result = rotateARGBBuffer(result, rotation);

// 5 Flip ARGB if needed
result = flipARGBBuffer(result, flip);

// 5. Convert from ARGB -> ????
result = convertARGBBufferTo(result, pixelFormat);

Expand Down
3 changes: 3 additions & 0 deletions android/src/main/cpp/ResizePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct ResizePlugin : public HybridClass<ResizePlugin> {
int cropWidth, int cropHeight,
int scaleWidth, int scaleHeight,
int rotation,
bool flip,
int /* PixelFormat */ pixelFormat, int /* DataType */ dataType);

FrameBuffer imageToFrameBuffer(alias_ref<JImage> image);
Expand All @@ -53,6 +54,7 @@ struct ResizePlugin : public HybridClass<ResizePlugin> {
FrameBuffer convertARGBBufferTo(FrameBuffer frameBuffer, PixelFormat toFormat);
FrameBuffer convertBufferToDataType(FrameBuffer frameBuffer, DataType dataType);
FrameBuffer rotateARGBBuffer(FrameBuffer frameBuffer, int rotation);
FrameBuffer flipARGBBuffer(FrameBuffer frameBuffer, bool flip);
global_ref<JByteBuffer> allocateBuffer(size_t size, std::string debugName);

private:
Expand All @@ -65,6 +67,7 @@ struct ResizePlugin : public HybridClass<ResizePlugin> {
global_ref<JByteBuffer> _cropBuffer;
global_ref<JByteBuffer> _scaleBuffer;
global_ref<JByteBuffer> _rotatedBuffer;
global_ref<JByteBuffer> _flipBuffer;
// ARGB (?x?) -> !!!! (?x?)
global_ref<JByteBuffer> _customFormatBuffer;
// Custom Data Type (e.g. float32)
Expand Down

0 comments on commit 92d73e2

Please sign in to comment.