Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: flip + rotation #25

Merged
merged 4 commits into from
Mar 12, 2024
Merged

feat: flip + rotation #25

merged 4 commits into from
Mar 12, 2024

Conversation

rodgomesc
Copy link
Contributor

No description provided.

Copy link
Owner

@mrousavy mrousavy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - this is a really interesting feature!

I know this is a draft PR, but I couldn't help and already leave a few thoughts on this, hope you don't mind 🙈

android/src/main/cpp/ResizePlugin.cpp Show resolved Hide resolved
android/src/main/cpp/ResizePlugin.cpp Outdated Show resolved Hide resolved
android/src/main/cpp/ResizePlugin.cpp Outdated Show resolved Hide resolved
android/src/main/cpp/ResizePlugin.cpp Outdated Show resolved Hide resolved
android/src/main/cpp/ResizePlugin.cpp Outdated Show resolved Hide resolved
Comment on lines 180 to 190
if (_rotatedBuffer == nullptr || _rotatedBuffer->getDirectSize() != argbSize) {
_rotatedBuffer = allocateBuffer(argbSize, "_rotatedBuffer");
}

FrameBuffer destination = {
.width = rotatedWidth,
.height = rotatedHeight,
.pixelFormat = PixelFormat::ARGB,
.dataType = DataType::UINT8,
.buffer = _rotatedBuffer,
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can avoid the copy and do this in-memory? The size is always the same, only width height and bytesPerRow changes, no?

In vImage on iOS it can work in-memory. Might not be the case here for libyuv tho..

android/src/main/cpp/ResizePlugin.cpp Outdated Show resolved Hide resolved
android/src/main/cpp/ResizePlugin.cpp Outdated Show resolved Hide resolved
android/src/main/cpp/ResizePlugin.h Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
@mrousavy
Copy link
Owner

Also JFYI; I will only merge PRs that work the same on both iOS and Android, so we need iOS support here as well. I can work on that at some point, but right now I think I have other priorities (VisionCamera Android preview stretching)

@rodgomesc rodgomesc force-pushed the feat/rotation branch 2 times, most recently from 92d73e2 to 083914f Compare January 31, 2024 12:18
@rodgomesc rodgomesc changed the title wip: implement android rotation support feat: flip + rotation Mar 11, 2024
@rodgomesc rodgomesc force-pushed the feat/rotation branch 6 times, most recently from 13093a9 to 690f60c Compare March 11, 2024 11:21
@rodgomesc rodgomesc force-pushed the feat/rotation branch 2 times, most recently from 19d8e85 to ab2f2bc Compare March 11, 2024 11:25
@@ -13,6 +13,7 @@ jsconfig.json
#
build/
*.pbxuser
.xcode.env.local
Copy link
Contributor Author

@rodgomesc rodgomesc Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should not be sent to GitHub, as Xcode creates it at build time

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but it might break builds if it gets generated and the user doesn't know about it. So I tend to leave it in gitignore, because then the user sees that this file was generated (in git status / GitHub desktop overview)

@rodgomesc rodgomesc requested a review from mrousavy March 11, 2024 11:34
@rodgomesc rodgomesc marked this pull request as ready for review March 11, 2024 11:35
Copy link
Owner

@mrousavy mrousavy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heyo Rodrigo - this is amazing, thank you so much for this PR!

I have a few code nitpicks first to make sure this is as error-proof as possible, and as nice-to-use as possible :)

@@ -13,6 +13,7 @@ jsconfig.json
#
build/
*.pbxuser
.xcode.env.local
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but it might break builds if it gets generated and the user doesn't know about it. So I tend to leave it in gitignore, because then the user sees that this file was generated (in git status / GitHub desktop overview)

@@ -150,12 +150,78 @@ FrameBuffer ResizePlugin::cropARGBBuffer(vision::FrameBuffer frameBuffer, int x,
return destination;
}

FrameBuffer ResizePlugin::flipARGBBuffer(FrameBuffer frameBuffer, bool flip) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this entire thing (function and flip parameter) to "mirror" instead of "flip". Let's use the term mirror everywhere in the codebase.

android/src/main/cpp/ResizePlugin.cpp Show resolved Hide resolved
@@ -277,8 +343,8 @@ FrameBuffer ResizePlugin::convertBufferToDataType(FrameBuffer frameBuffer, DataT
}

jni::global_ref<jni::JByteBuffer> ResizePlugin::resize(jni::alias_ref<JImage> image, int cropX, int cropY, int cropWidth, int cropHeight,
int scaleWidth, int scaleHeight, int /* PixelFormat */ pixelFormatOrdinal,
int /* DataType */ dataTypeOrdinal) {
int scaleWidth, int scaleHeight, int rotation, bool flip,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re my previous comment; rotation should also be rotationOrdinal - it should be an Enum in Kotlin.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And rename flip to mirror again

android/src/main/cpp/ResizePlugin.h Show resolved Hide resolved

export default function App() {
const permission = useCameraPermission();
const device = useCameraDevice('back');
const device = useCameraDevice('front');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert

@@ -35,6 +35,8 @@ export default function App() {
},
pixelFormat: 'rgba',
dataType: 'uint8',
rotation: ROTATION.R90,
flip: true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flip -> mirror

package.json Outdated
@@ -1,6 +1,6 @@
{
"name": "vision-camera-resize-plugin",
"version": "2.0.1",
"version": "2.1.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert

src/index.ts Outdated
Comment on lines 11 to 16
export enum ROTATION {
R0 = 0,
R90 = 90,
R180 = 180,
R270 = 270,
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, no let's not use JS enums here. Check out how I built the pixelFormat, this is a TS union instead of a JS enum. JS Enums just aren't that flexible.
I'm also not a big fan of the names here.

Let's try to do something like

rotate: '0deg' | '90deg' | '180deg' | '270deg'

Or

orientation: 'portrait' | 'landscape-right' | 'portrait-upside-down' | 'landscape-left'

where the latter defines the target orientation. As in, it makes sure to rotate the frame so it will later be in that exact orientation, no matter what input orientation it currently is in. Get what I mean?
Like if it currently is in portrait, I want it to be in landscape-left, it will rotate by however degrees it should to get to that orientation.

I think that makes more sense, no?

@rodgomesc
Copy link
Contributor Author

True, but it might break builds if it gets generated and the user doesn't know about it. So I tend to leave it in gitignore, because then the user sees that this file was generated (in git status / GitHub desktop overview)

sorry i'm a bit confuse on what's your suggestion here, i'm my case the xcode build only finished successfully after i deleted this file

@mrousavy
Copy link
Owner

True, but it might break builds if it gets generated and the user doesn't know about it. So I tend to leave it in gitignore, because then the user sees that this file was generated (in git status / GitHub desktop overview)

sorry i'm a bit confuse on what's your suggestion here, i'm my case the xcode build only finished successfully after i deleted this file

Yea so this is a it weird - it seems like CocoaPods will generate this file, with an invalid path to node.

Once you try to build the Xcode project after installing pods, it will fail.

When the file is in .gitignore, the user (or developer; one of us) will never know why the build failed, it will be hard to pinpoint it to that specific file.

If we instead leave this file in git, we will notice that this file was created because it will show up in git status, then we'll be like ahh yea we need to delete that.

'ya know? It's kind of a weird problem but better to leave it in than silently fail

@rodgomesc rodgomesc force-pushed the feat/rotation branch 8 times, most recently from 3ed2cf6 to 39c8c85 Compare March 11, 2024 13:57
@rodgomesc rodgomesc requested a review from mrousavy March 11, 2024 13:58
@rodgomesc
Copy link
Contributor Author

rodgomesc commented Mar 11, 2024

When the file is in .gitignore, the user (or developer; one of us) will never know why the build failed, it will be hard to pinpoint it to that specific file.

the error (image bellow) is not a easy spot, and not descriptive at all i spent at least 40 min trying to figure out why my build was failing, would be nice if we can avoid this from happing with anyone else so i'm clarifying some things to make sure we are on the same page:

  1. leaving the xcode.env.local with your node location will fail the build for everyone else except for you
  2. deleting it will build the app successfuly for you and everyone else
  3. react-native template itself has it on gitignore
  4. as peer docs this file overrides the local config

if you want to reproduce this

replace the xcode.env.local with my node location and try to build ios app

export NODE_BINARY=/Users/rodrigogomes/.nvm/versions/node/v18.2.0/bin/node

image

@rodgomesc
Copy link
Contributor Author

full disclosure: I don't want to be a nuisance here, I'm only insisting because I think what's happening isn't about you agreeing or disagreeing with the xcode.env.local thing, but rather about us still not being on the same page regarding what's going on

@mrousavy
Copy link
Owner

Yes I know that this file makes the build break - I tend to not put it into the gitignore so I can always see in my git status that it has been created. In reality, this should not be created I think. This is a pod install side-effect. But we can leave it in, no worries.

@mrousavy mrousavy merged commit 9280118 into mrousavy:main Mar 12, 2024
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants