Skip to content

Commit

Permalink
Add RGB Adjustments to transformer demo.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 476049125
  • Loading branch information
leonwind authored and marcbaechinger committed Sep 30, 2022
1 parent 9ec4e13 commit 9ccdd22
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
public static final String PERIODIC_VIGNETTE_OUTER_RADIUS = "periodic_vignette_outer_radius";
public static final String COLOR_FILTER_SELECTION = "color_filter_selection";
public static final String CONTRAST_VALUE = "contrast_value";
public static final String RGB_ADJUSTMENT_RED_SCALE = "rgb_adjustment_red_scale";
public static final String RGB_ADJUSTMENT_GREEN_SCALE = "rgb_adjustment_green_scale";
public static final String RGB_ADJUSTMENT_BLUE_SCALE = "rgb_adjustment_blue_scale";
public static final int COLOR_FILTER_GRAYSCALE = 0;
public static final int COLOR_FILTER_INVERTED = 1;
public static final int COLOR_FILTER_SEPIA = 2;
Expand Down Expand Up @@ -106,15 +109,17 @@ public final class ConfigurationActivity extends AppCompatActivity {
"Dizzy crop",
"Edge detector (Media Pipe)",
"Color filters",
"RGB Adjustments",
"Contrast",
"Periodic vignette",
"3D spin",
"Overlay logo & timer",
"Zoom in start",
};
private static final int COLOR_FILTERS_INDEX = 2;
private static final int CONTRAST_INDEX = 3;
private static final int PERIODIC_VIGNETTE_INDEX = 4;
private static final int RGB_ADJUSTMENTS_INDEX = 3;
private static final int CONTRAST_INDEX = 4;
private static final int PERIODIC_VIGNETTE_INDEX = 5;
private static final String SAME_AS_INPUT_OPTION = "same as input";
private static final float HALF_DIAGONAL = 1f / (float) Math.sqrt(2);

Expand All @@ -139,6 +144,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
private long trimStartMs;
private long trimEndMs;
private int colorFilterSelection;
private float rgbAdjustmentRedScale;
private float rgbAdjustmentGreenScale;
private float rgbAdjustmentBlueScale;
private float contrastValue;
private float periodicVignetteCenterX;
private float periodicVignetteCenterY;
Expand Down Expand Up @@ -297,6 +305,9 @@ private void startTransformation(View view) {
bundle.putBooleanArray(DEMO_EFFECTS_SELECTIONS, demoEffectsSelections);
bundle.putInt(COLOR_FILTER_SELECTION, colorFilterSelection);
bundle.putFloat(CONTRAST_VALUE, contrastValue);
bundle.putFloat(RGB_ADJUSTMENT_RED_SCALE, rgbAdjustmentRedScale);
bundle.putFloat(RGB_ADJUSTMENT_GREEN_SCALE, rgbAdjustmentGreenScale);
bundle.putFloat(RGB_ADJUSTMENT_BLUE_SCALE, rgbAdjustmentBlueScale);
bundle.putFloat(PERIODIC_VIGNETTE_CENTER_X, periodicVignetteCenterX);
bundle.putFloat(PERIODIC_VIGNETTE_CENTER_Y, periodicVignetteCenterY);
bundle.putFloat(PERIODIC_VIGNETTE_INNER_RADIUS, periodicVignetteInnerRadius);
Expand Down Expand Up @@ -367,6 +378,9 @@ private void selectDemoEffect(DialogInterface dialog, int which, boolean isCheck
case COLOR_FILTERS_INDEX:
controlColorFiltersSettings();
break;
case RGB_ADJUSTMENTS_INDEX:
controlRgbAdjustmentsScale();
break;
case CONTRAST_INDEX:
controlContrastSettings();
break;
Expand Down Expand Up @@ -394,6 +408,27 @@ private void controlColorFiltersSettings() {
.show();
}

private void controlRgbAdjustmentsScale() {
View dialogView =
getLayoutInflater().inflate(R.layout.rgb_adjustment_options, /* root= */ null);
Slider redScaleSlider = checkNotNull(dialogView.findViewById(R.id.rgb_adjustment_red_scale));
Slider greenScaleSlider =
checkNotNull(dialogView.findViewById(R.id.rgb_adjustment_green_scale));
Slider blueScaleSlider = checkNotNull(dialogView.findViewById(R.id.rgb_adjustment_blue_scale));
new AlertDialog.Builder(/* context= */ this)
.setTitle(R.string.rgb_adjustment_options)
.setView(dialogView)
.setPositiveButton(
android.R.string.ok,
(DialogInterface dialogInterface, int i) -> {
rgbAdjustmentRedScale = redScaleSlider.getValue();
rgbAdjustmentGreenScale = greenScaleSlider.getValue();
rgbAdjustmentBlueScale = blueScaleSlider.getValue();
})
.create()
.show();
}

private void controlContrastSettings() {
View dialogView = getLayoutInflater().inflate(R.layout.contrast_options, /* root= */ null);
Slider contrastSlider = checkNotNull(dialogView.findViewById(R.id.contrast_slider));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import androidx.media3.effect.Contrast;
import androidx.media3.effect.GlEffect;
import androidx.media3.effect.GlTextureProcessor;
import androidx.media3.effect.RgbAdjustment;
import androidx.media3.effect.RgbFilter;
import androidx.media3.effect.RgbMatrix;
import androidx.media3.exoplayer.ExoPlayer;
Expand Down Expand Up @@ -335,9 +336,17 @@ private Transformer createTransformer(@Nullable Bundle bundle, String filePath)
}
}
if (selectedEffects[3]) {
effects.add(new Contrast(bundle.getFloat(ConfigurationActivity.CONTRAST_VALUE)));
effects.add(
new RgbAdjustment.Builder()
.setRedScale(bundle.getFloat(ConfigurationActivity.RGB_ADJUSTMENT_RED_SCALE))
.setGreenScale(bundle.getFloat(ConfigurationActivity.RGB_ADJUSTMENT_GREEN_SCALE))
.setBlueScale(bundle.getFloat(ConfigurationActivity.RGB_ADJUSTMENT_BLUE_SCALE))
.build());
}
if (selectedEffects[4]) {
effects.add(new Contrast(bundle.getFloat(ConfigurationActivity.CONTRAST_VALUE)));
}
if (selectedEffects[5]) {
effects.add(
(GlEffect)
(Context context, boolean useHdr) ->
Expand All @@ -352,13 +361,13 @@ private Transformer createTransformer(@Nullable Bundle bundle, String filePath)
ConfigurationActivity.PERIODIC_VIGNETTE_OUTER_RADIUS),
bundle.getFloat(ConfigurationActivity.PERIODIC_VIGNETTE_OUTER_RADIUS)));
}
if (selectedEffects[5]) {
if (selectedEffects[6]) {
effects.add(MatrixTransformationFactory.createSpin3dEffect());
}
if (selectedEffects[6]) {
if (selectedEffects[7]) {
effects.add((GlEffect) BitmapOverlayProcessor::new);
}
if (selectedEffects[7]) {
if (selectedEffects[8]) {
effects.add(MatrixTransformationFactory.createZoomInTransition());
}
transformerBuilder.setVideoEffects(effects.build());
Expand Down
76 changes: 76 additions & 0 deletions demos/transformer/src/main/res/layout/rgb_adjustment_options.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2022 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".ConfigurationActivity">

<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:layout_marginTop="32dp"
android:measureWithLargestChild="true"
android:paddingLeft="24dp"
android:paddingRight="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TableRow
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:text="@string/rgb_adjustment_scale_red" />
<com.google.android.material.slider.Slider
android:id="@+id/rgb_adjustment_red_scale"
android:valueFrom="0"
android:value="1"
android:valueTo="2"
android:layout_gravity="right"
app:labelBehavior="gone"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:text="@string/rgb_adjustment_scale_green" />
<com.google.android.material.slider.Slider
android:id="@+id/rgb_adjustment_green_scale"
android:valueFrom="0"
android:value="1"
android:valueTo="2"
android:layout_gravity="right"
app:labelBehavior="gone"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:text="@string/rgb_adjustment_scale_blue" />
<com.google.android.material.slider.Slider
android:id="@+id/rgb_adjustment_blue_scale"
android:valueFrom="0"
android:value="1"
android:valueTo="2"
android:layout_gravity="right"
app:labelBehavior="gone"/>
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions demos/transformer/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<item>Sepia</item>
</string-array>
<string name="contrast_value" translatable="false">Contrast value</string>
<string name="rgb_adjustment_options" translatable="false">Scale RGB Channels individually</string>
<string name="rgb_adjustment_scale_red" translatable="false">Scale Red</string>
<string name="rgb_adjustment_scale_green" translatable="false">Scale Green</string>
<string name="rgb_adjustment_scale_blue" translatable="false">Scale Blue</string>
<string name="center_x">Center X</string>
<string name="center_y">Center Y</string>
<string name="radius_range">Radius range</string>
Expand Down

0 comments on commit 9ccdd22

Please sign in to comment.