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

speed optimization on convert(Frame):Bitmap #379

Merged
merged 1 commit into from
Apr 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/main/java/org/bytedeco/javacv/AndroidFrameConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
public class AndroidFrameConverter extends FrameConverter<Bitmap> {
Bitmap bitmap;
ByteBuffer buffer;
byte[] row;

/**
* Convert YUV 4:2:0 SP (NV21) data to BGR, as received, for example,
Expand Down Expand Up @@ -142,35 +143,44 @@ public Frame convert(byte[] data, int width, int height) {
int height = frame.imageHeight;
int stride = frame.imageStride;
int rowBytes = bitmap.getRowBytes();
int rgba;
if (frame.imageChannels == 1) {
if (buffer == null || buffer.capacity() < height * rowBytes) {
buffer = ByteBuffer.allocate(height * rowBytes);
}
if (row == null || row.length != stride)
row = new byte[stride];
for (int y = 0; y < height; y++) {
in.position(y * stride);
in.get(row);
for (int x = 0; x < width; x++) {
// GRAY -> RGBA
byte B = in.get(y * stride + x);
buffer.put(y * rowBytes + 4 * x, B);
buffer.put(y * rowBytes + 4 * x + 1, B);
buffer.put(y * rowBytes + 4 * x + 2, B);
buffer.put(y * rowBytes + 4 * x + 3, (byte)0xFF);
byte B = row[x];
rgba = ( B & 0xff) << 24 |
(B & 0xff) << 16 |
(B & 0xff) << 8 | 0xff;
buffer.putInt(y * rowBytes + 4 * x, rgba);
}
}
bitmap.copyPixelsFromBuffer(buffer.position(0));
} else if (frame.imageChannels == 3) {
if (buffer == null || buffer.capacity() < height * rowBytes) {
buffer = ByteBuffer.allocate(height * rowBytes);
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// BGR -> RGBA
byte B = in.get(y * stride + 3 * x );
byte G = in.get(y * stride + 3 * x + 1);
byte R = in.get(y * stride + 3 * x + 2);
buffer.put(y * rowBytes + 4 * x, R);
buffer.put(y * rowBytes + 4 * x + 1, G);
buffer.put(y * rowBytes + 4 * x + 2, B);
buffer.put(y * rowBytes + 4 * x + 3, (byte)0xFF);
if (in.remaining() > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

No need to check for in.remaining() anymore!

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// BGR -> RGBA
if (x < width - 1 || y < height - 1) {
rgba = in.getInt(y * stride + 3 * x);
} else {
int r = in.get(y * stride + 3 * x) & 0xff;
int g = in.get(y * stride + 3 * x + 1) & 0xff;
int b = in.get(y * stride + 3 * x + 2) & 0xff;
rgba = (r << 24) | (g << 16) | (b << 8);
}
buffer.putInt(y * rowBytes + 4 * x, (rgba << 8) | 0xff);
Copy link
Member

Choose a reason for hiding this comment

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

There's going to be a problem here I think for the last pixel. It's not being shifted.

}
}
}
bitmap.copyPixelsFromBuffer(buffer.position(0));
Expand Down