<commit_before>/*
 * 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.
 */
package androidx.media3.effect;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.Math.abs;
import static java.lang.Math.max;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.media.Image;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.GlUtil;
import com.google.android.exoplayer2.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Utilities for instrumentation tests for the {@link GlEffectsFrameProcessor} and {@link
 * SingleFrameGlTextureProcessor SingleFrameGlTextureProcessors}.
 */
public class BitmapTestUtil {

  private static final String TAG = "BitmapTestUtil";

  /**
   * Maximum allowed average pixel difference between the expected and actual edited images in pixel
   * difference-based tests. The value is chosen so that differences in decoder behavior across
   * emulator versions don't affect whether the test passes for most emulators, but substantial
   * distortions introduced by changes in the behavior of the {@link SingleFrameGlTextureProcessor
   * SingleFrameGlTextureProcessors} will cause the test to fail.
   *
   * <p>To run pixel difference-based tests on physical devices, please use a value of 5f, rather
   * than 0.1f. This higher value will ignore some very small errors, but will allow for some
   * differences caused by graphics implementations to be ignored. When the difference is close to
   * the threshold, manually inspect expected/actual bitmaps to confirm failure, as it's possible
   * this is caused by a difference in the codec or graphics implementation as opposed to a {@link
   * SingleFrameGlTextureProcessor} issue.
   */
  public static final float MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE = 0.1f;

  /**
   * Reads a bitmap from the specified asset location.
   *
   * @param assetString Relative path to the asset within the assets directory.
   * @return A {@link Bitmap}.
   * @throws IOException If the bitmap can't be read.
   */
  public static Bitmap readBitmap(String assetString) throws IOException {
    Bitmap bitmap;
    try (InputStream inputStream = getApplicationContext().getAssets().open(assetString)) {
      bitmap = BitmapFactory.decodeStream(inputStream);
    }
    return bitmap;
  }

  /**
   * Returns a bitmap with the same information as the provided alpha/red/green/blue 8-bits per
   * component image.
   */<commit_msg>Increase pixel difference threshold<commit_after>/*
 * 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.
 */
package androidx.media3.effect;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.Math.abs;
import static java.lang.Math.max;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.media.Image;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.GlUtil;
import com.google.android.exoplayer2.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Utilities for instrumentation tests for the {@link GlEffectsFrameProcessor} and {@link
 * SingleFrameGlTextureProcessor SingleFrameGlTextureProcessors}.
 */
public class BitmapTestUtil {

  private static final String TAG = "BitmapTestUtil";

  /**
   * Maximum allowed average pixel difference between the expected and actual edited images in pixel
   * difference-based tests. The value is chosen so that differences in decoder behavior across
   * emulator versions don't affect whether the test passes for most emulators, but substantial
   * distortions introduced by changes in the behavior of the {@link SingleFrameGlTextureProcessor
   * SingleFrameGlTextureProcessors} will cause the test to fail.
   *
   * <p>To run pixel difference-based tests on physical devices, please use a value of 5f, rather
   * than 0.5f. This higher value will ignore some very small errors, but will allow for some
   * differences caused by graphics implementations to be ignored. When the difference is close to
   * the threshold, manually inspect expected/actual bitmaps to confirm failure, as it's possible
   * this is caused by a difference in the codec or graphics implementation as opposed to a {@link
   * SingleFrameGlTextureProcessor} issue.
   */
  public static final float MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE = 0.5f;

  /**
   * Reads a bitmap from the specified asset location.
   *
   * @param assetString Relative path to the asset within the assets directory.
   * @return A {@link Bitmap}.
   * @throws IOException If the bitmap can't be read.
   */
  public static Bitmap readBitmap(String assetString) throws IOException {
    Bitmap bitmap;
    try (InputStream inputStream = getApplicationContext().getAssets().open(assetString)) {
      bitmap = BitmapFactory.decodeStream(inputStream);
    }
    return bitmap;
  }

  /**
   * Returns a bitmap with the same information as the provided alpha/red/green/blue 8-bits per
   * component image.
   */
{'fraction_non_alphanumeric': 0.07178002894356006, 'fraction_numerical': 0.005499276410998553, 'mean_word_length': 4.324345146379045, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6547393', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1754, 'n_words': 723}