Skip to content

Commit 1fc200f

Browse files
janherlingmetafacebook-github-bot
authored andcommitted
Added new function to Platform::Android::Bitmap
Summary: The new function allows to convert a bitmap to a Frame. Reviewed By: enpe Differential Revision: D75725203 Privacy Context Container: L1293349 fbshipit-source-id: d79605d308b03afc2d594240d6ccfec35b599e56
1 parent e1c63a5 commit 1fc200f

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

impl/ocean/platform/android/Bitmap.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,57 @@ namespace Platform
1818
namespace Android
1919
{
2020

21+
Frame Bitmap::toFrame(JNIEnv* env, jobject bitmap, const bool copyData)
22+
{
23+
ocean_assert(env != nullptr && bitmap != nullptr);
24+
if (env == nullptr || bitmap == nullptr)
25+
{
26+
return Frame();
27+
}
28+
29+
AndroidBitmapInfo bitmapInfo;
30+
if (AndroidBitmap_getInfo(env, bitmap, &bitmapInfo) < 0)
31+
{
32+
return Frame();
33+
}
34+
35+
ocean_assert(bitmapInfo.format == ANDROID_BITMAP_FORMAT_RGBA_8888);
36+
if (bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
37+
{
38+
return Frame();
39+
}
40+
41+
const unsigned int width = bitmapInfo.width;
42+
const unsigned int height = bitmapInfo.height;
43+
const unsigned int strideBytes = bitmapInfo.stride;
44+
45+
unsigned int paddingElements = 0u;
46+
if (!Frame::strideBytes2paddingElements(FrameType::FORMAT_RGBA32, width, strideBytes, paddingElements))
47+
{
48+
return Frame();
49+
}
50+
51+
void* pixelData = nullptr;
52+
53+
if (AndroidBitmap_lockPixels(env, bitmap, &pixelData) < 0)
54+
{
55+
ocean_assert(false && "Could not lock the pixel data!");
56+
return Frame();
57+
}
58+
59+
const Frame::CopyMode copyMode = copyData ? Frame::CM_COPY_REMOVE_PADDING_LAYOUT : Frame::CM_USE_KEEP_LAYOUT;
60+
61+
Frame frame(FrameType(width, height, FrameType::FORMAT_RGBA32, FrameType::ORIGIN_UPPER_LEFT), pixelData, copyMode, paddingElements);
62+
63+
if (AndroidBitmap_unlockPixels(env, bitmap) < 0)
64+
{
65+
ocean_assert(false && "Could not unlock the pixel data!");
66+
return Frame();
67+
}
68+
69+
return frame;
70+
}
71+
2172
FrameType::PixelFormat Bitmap::translateFormat(const AndroidBitmapFormat format)
2273
{
2374
switch (format)

impl/ocean/platform/android/Bitmap.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class OCEAN_PLATFORM_ANDROID_EXPORT Bitmap
4444
* @return The corresponding Android bitmap format, ANDROID_BITMAP_FORMAT_NONE if unknown
4545
*/
4646
static AndroidBitmapFormat translateFormat(const FrameType::PixelFormat pixelFormat);
47+
48+
/**
49+
* Converts an Android bitmap to an Ocean frame.
50+
* The Android bitmap must have format ANDROID_BITMAP_FORMAT_RGBA_8888.
51+
* @param env The JNI environment, must be valid
52+
* @param bitmap The Android bitmap to convert, must be valid
53+
* @param copyData True, to copy the bitmap data; False, to use the bitmap data directly
54+
* @return The resulting Ocean frame, an invalid frame if the conversion failed
55+
*/
56+
static Frame toFrame(JNIEnv* env, jobject bitmap, const bool copyData = true);
4757
};
4858

4959
}

0 commit comments

Comments
 (0)