Skip to content

Commit

Permalink
Add HSL Adjustments to the demo.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 476373520
  • Loading branch information
leonwind authored and marcbaechinger committed Oct 19, 2022
1 parent 0969118 commit 5725ebb
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
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 String HSL_ADJUSTMENTS_HUE = "hsl_adjustments_hue";
public static final String HSL_ADJUSTMENTS_SATURATION = "hsl_adjustments_saturation";
public static final String HSL_ADJUSTMENTS_LIGHTNESS = "hsl_adjustments_lightness";
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 @@ -110,6 +113,7 @@ public final class ConfigurationActivity extends AppCompatActivity {
"Edge detector (Media Pipe)",
"Color filters",
"RGB Adjustments",
"HSL Adjustments",
"Contrast",
"Periodic vignette",
"3D spin",
Expand All @@ -118,8 +122,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
};
private static final int COLOR_FILTERS_INDEX = 2;
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 int HSL_ADJUSTMENT_INDEX = 4;
private static final int CONTRAST_INDEX = 5;
private static final int PERIODIC_VIGNETTE_INDEX = 6;
private static final String SAME_AS_INPUT_OPTION = "same as input";
private static final float HALF_DIAGONAL = 1f / (float) Math.sqrt(2);

Expand Down Expand Up @@ -148,6 +153,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
private float rgbAdjustmentGreenScale;
private float rgbAdjustmentBlueScale;
private float contrastValue;
private float hueAdjustment;
private float saturationAdjustment;
private float lightnessAdjustment;
private float periodicVignetteCenterX;
private float periodicVignetteCenterY;
private float periodicVignetteInnerRadius;
Expand Down Expand Up @@ -308,6 +316,9 @@ private void startTransformation(View view) {
bundle.putFloat(RGB_ADJUSTMENT_RED_SCALE, rgbAdjustmentRedScale);
bundle.putFloat(RGB_ADJUSTMENT_GREEN_SCALE, rgbAdjustmentGreenScale);
bundle.putFloat(RGB_ADJUSTMENT_BLUE_SCALE, rgbAdjustmentBlueScale);
bundle.putFloat(HSL_ADJUSTMENTS_HUE, hueAdjustment);
bundle.putFloat(HSL_ADJUSTMENTS_SATURATION, saturationAdjustment);
bundle.putFloat(HSL_ADJUSTMENTS_LIGHTNESS, lightnessAdjustment);
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 @@ -384,6 +395,9 @@ private void selectDemoEffect(DialogInterface dialog, int which, boolean isCheck
case CONTRAST_INDEX:
controlContrastSettings();
break;
case HSL_ADJUSTMENT_INDEX:
controlHslAdjustmentSettings();
break;
case PERIODIC_VIGNETTE_INDEX:
controlPeriodicVignetteSettings();
break;
Expand Down Expand Up @@ -441,6 +455,28 @@ private void controlContrastSettings() {
.show();
}

private void controlHslAdjustmentSettings() {
View dialogView =
getLayoutInflater().inflate(R.layout.hsl_adjustment_options, /* root= */ null);
Slider hueAdjustmentSlider = checkNotNull(dialogView.findViewById(R.id.hsl_adjustments_hue));
Slider saturationAdjustmentSlider =
checkNotNull(dialogView.findViewById(R.id.hsl_adjustments_saturation));
Slider lightnessAdjustmentSlider =
checkNotNull(dialogView.findViewById(R.id.hsl_adjustment_lightness));
new AlertDialog.Builder(/* context= */ this)
.setTitle(R.string.hsl_adjustment_options)
.setView(dialogView)
.setPositiveButton(
android.R.string.ok,
(DialogInterface dialogInterface, int i) -> {
hueAdjustment = hueAdjustmentSlider.getValue();
saturationAdjustment = saturationAdjustmentSlider.getValue();
lightnessAdjustment = lightnessAdjustmentSlider.getValue();
})
.create()
.show();
}

private void controlPeriodicVignetteSettings() {
View dialogView =
getLayoutInflater().inflate(R.layout.periodic_vignette_options, /* root= */ null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import androidx.media3.effect.Contrast;
import androidx.media3.effect.GlEffect;
import androidx.media3.effect.GlTextureProcessor;
import androidx.media3.effect.HslAdjustment;
import androidx.media3.effect.RgbAdjustment;
import androidx.media3.effect.RgbFilter;
import androidx.media3.effect.RgbMatrix;
Expand Down Expand Up @@ -345,9 +346,18 @@ private Transformer createTransformer(@Nullable Bundle bundle, String filePath)
.build());
}
if (selectedEffects[4]) {
effects.add(new Contrast(bundle.getFloat(ConfigurationActivity.CONTRAST_VALUE)));
effects.add(
new HslAdjustment.Builder()
.adjustHue(bundle.getFloat(ConfigurationActivity.HSL_ADJUSTMENTS_HUE))
.adjustSaturation(
bundle.getFloat(ConfigurationActivity.HSL_ADJUSTMENTS_SATURATION))
.adjustLightness(bundle.getFloat(ConfigurationActivity.HSL_ADJUSTMENTS_LIGHTNESS))
.build());
}
if (selectedEffects[5]) {
effects.add(new Contrast(bundle.getFloat(ConfigurationActivity.CONTRAST_VALUE)));
}
if (selectedEffects[6]) {
effects.add(
(GlEffect)
(Context context, boolean useHdr) ->
Expand Down
73 changes: 73 additions & 0 deletions demos/transformer/src/main/res/layout/hsl_adjustment_options.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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/hue_adjustment" />
<com.google.android.material.slider.Slider
android:id="@+id/hsl_adjustments_hue"
android:valueFrom="-360"
android:value="0"
android:valueTo="360"
android:layout_gravity="right"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:text="@string/saturation_adjustment" />
<com.google.android.material.slider.Slider
android:id="@+id/hsl_adjustments_saturation"
android:valueFrom="-100"
android:value="0"
android:valueTo="100"
android:layout_gravity="right"/>
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:text="@string/lightness_adjustment" />
<com.google.android.material.slider.Slider
android:id="@+id/hsl_adjustment_lightness"
android:valueFrom="-100"
android:value="0"
android:valueTo="100"
android:layout_gravity="right"/>
</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 @@ -55,4 +55,8 @@
<string name="center_x">Center X</string>
<string name="center_y">Center Y</string>
<string name="radius_range">Radius range</string>
<string name="hsl_adjustment_options" translatable="false">HSL Adjustment Options</string>
<string name="hue_adjustment">Hue Adjustment</string>
<string name="saturation_adjustment">Saturation Adjustment</string>
<string name="lightness_adjustment">Lightness Adjustment</string>
</resources>

0 comments on commit 5725ebb

Please sign in to comment.