-
Notifications
You must be signed in to change notification settings - Fork 471
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
Fix ALPHA_8 format support for bitmap in Cue. #2058
base: release
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package androidx.media3.common.text; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import android.graphics.Bitmap; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import java.nio.Buffer; | ||
import java.nio.ByteBuffer; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** Tests for {@link Cue}. */ | ||
@RunWith(AndroidJUnit4.class) | ||
public class CueTest { | ||
|
||
@Test | ||
public void roundTripViaSerializableBundle_withBitmapAlpha8_yieldsEqualInstance() { | ||
Bitmap bitmap = Bitmap.createBitmap(8, 8, Bitmap.Config.ALPHA_8); | ||
byte[] bytes = new byte[bitmap.getWidth() * bitmap.getHeight()]; | ||
for (byte i = 0; i < bytes.length; i++) { | ||
bytes[i] = i; | ||
} | ||
Buffer buffer = ByteBuffer.wrap(bytes); | ||
bitmap.copyPixelsFromBuffer(buffer); | ||
Cue cue = | ||
new Cue.Builder().setBitmap(bitmap).build(); | ||
Cue modifiedCue = Cue.fromBundle(cue.toSerializableBundle()); | ||
|
||
assertThat(modifiedCue).isEqualTo(cue); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,8 @@ | |
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.nio.Buffer; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import org.checkerframework.dataflow.qual.Pure; | ||
|
||
|
@@ -835,6 +837,9 @@ public Cue build() { | |
private static final String FIELD_MULTI_ROW_ALIGNMENT = Util.intToStringMaxRadix(2); | ||
private static final String FIELD_BITMAP_PARCELABLE = Util.intToStringMaxRadix(3); | ||
private static final String FIELD_BITMAP_BYTES = Util.intToStringMaxRadix(18); | ||
private static final String FIELD_BITMAP_ORIGIN_WIDTH = Util.intToStringMaxRadix(19); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the meaning of the word 'origin' here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bitmap compress don't support ALPHA_8, so we have to store the bitmap original size and buffer, then when read cue, we need use the bitmap size. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bitmap compress don't support ALPHA_8, so we have to store the bitmap original size and buffer, then when read cue, we need use the bitmap size. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand why you need to transfer the size and width separately to the bytes. I don't understand what the meaning of the word 'origin' is. My first assumption is that this means the (0,0) point: https://en.wikipedia.org/wiki/Origin_(mathematics), but I'm not sure that really makes sense. So what is the intended meaning? |
||
private static final String FIELD_BITMAP_ORIGIN_HEIGHT = Util.intToStringMaxRadix(20); | ||
private static final String FIELD_BITMAP_COMPRESSED = Util.intToStringMaxRadix(21); | ||
private static final String FIELD_LINE = Util.intToStringMaxRadix(4); | ||
private static final String FIELD_LINE_TYPE = Util.intToStringMaxRadix(5); | ||
private static final String FIELD_LINE_ANCHOR = Util.intToStringMaxRadix(6); | ||
|
@@ -862,10 +867,22 @@ public Cue build() { | |
public Bundle toSerializableBundle() { | ||
Bundle bundle = toBundleWithoutBitmap(); | ||
if (bitmap != null) { | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
// The PNG format is lossless, and the quality parameter is ignored. | ||
checkState(bitmap.compress(Bitmap.CompressFormat.PNG, /* quality= */ 0, output)); | ||
bundle.putByteArray(FIELD_BITMAP_BYTES, output.toByteArray()); | ||
if (bitmap.getConfig() == Bitmap.Config.ALPHA_8) { | ||
// Bitmap compress not support ALPHA_8, so we just copy the buffer. | ||
bundle.putBoolean(FIELD_BITMAP_COMPRESSED, false); | ||
bundle.putInt(FIELD_BITMAP_ORIGIN_WIDTH, bitmap.getWidth()); | ||
bundle.putInt(FIELD_BITMAP_ORIGIN_HEIGHT, bitmap.getHeight()); | ||
byte[] bytes = new byte[bitmap.getWidth() * bitmap.getHeight()]; | ||
Buffer buffer = ByteBuffer.wrap(bytes); | ||
bitmap.copyPixelsToBuffer(buffer); | ||
bundle.putByteArray(FIELD_BITMAP_BYTES, bytes); | ||
} else { | ||
bundle.putBoolean(FIELD_BITMAP_COMPRESSED, true); | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
// The PNG format is lossless, and the quality parameter is ignored. | ||
checkState(bitmap.compress(Bitmap.CompressFormat.PNG, /* quality= */ 0, output)); | ||
bundle.putByteArray(FIELD_BITMAP_BYTES, output.toByteArray()); | ||
} | ||
} | ||
return bundle; | ||
} | ||
|
@@ -956,10 +973,22 @@ public static Cue fromBundle(Bundle bundle) { | |
if (bitmap != null) { | ||
builder.setBitmap(bitmap); | ||
} else { | ||
boolean compressed = bundle.getBoolean(FIELD_BITMAP_COMPRESSED); | ||
@Nullable byte[] bitmapBytes = bundle.getByteArray(FIELD_BITMAP_BYTES); | ||
if (bitmapBytes != null) { | ||
builder.setBitmap( | ||
BitmapFactory.decodeByteArray(bitmapBytes, /* offset= */ 0, bitmapBytes.length)); | ||
if (compressed) { | ||
builder.setBitmap( | ||
BitmapFactory.decodeByteArray(bitmapBytes, /* offset= */ 0, bitmapBytes.length)); | ||
} else { | ||
int bitmapWidth = bundle.getInt(FIELD_BITMAP_ORIGIN_WIDTH); | ||
int bitmapHeight = bundle.getInt(FIELD_BITMAP_ORIGIN_HEIGHT); | ||
if (bitmapWidth > 0 && bitmapHeight > 0) { | ||
bitmap = Bitmap.createBitmap(bitmapWidth,bitmapHeight, Bitmap.Config.ALPHA_8); | ||
Buffer buffer = ByteBuffer.wrap(bitmapBytes); | ||
bitmap.copyPixelsFromBuffer(buffer); | ||
builder.setBitmap(bitmap); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional: de-duplicate the 3 @Nullable Bitmap bitmap = bundle.getParcelable(FIELD_BITMAP_PARCELABLE);
if (bitmap == null) {
// handle bytes case (both compressed and uncompressed) with
// existing code, but assigning `bitmap` each time instead of setting
// it on the builder.
}
builder.setBitmap(bitmap); // Passing null is fine here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
} | ||
} | ||
} | ||
} | ||
if (bundle.containsKey(FIELD_LINE) && bundle.containsKey(FIELD_LINE_TYPE)) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's just move the whole of
CueTest
toandroidTest
instead of splitting it like this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The robolectric test suite not support ALPHA_8 bitmap, so we have to use AndroidTest.
See Request support bitmap with ALPHA_8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand.
I'm asking you to move the existing
CueTest
fromlibraries/common/src/test/java/androidx/media3/common/text
tolibraries/common/src/androidTest/java/androidx/media3/common/text
and then add this new test method to it.The current change results in two classes called
androidx.media3.common.text.CueTest
which results in undefined behaviour when debugging in Android Studio, and having to look in two places when checking what parts ofCue
are tested.