<commit_before>package com.pedro.encoder.utils.gl;

import android.graphics.PointF;
import android.opengl.GLES20;
import android.opengl.Matrix;

/**
 * Created by pedro on 22/03/19.
 */

public class SizeCalculator {

  public static void calculateViewPort(boolean keepAspectRatio, int mode, int previewWidth,
      int previewHeight, int streamWidth, int streamHeight) {
    if (keepAspectRatio) {
      if (previewWidth > previewHeight) { //landscape
        if (mode == 0) { //adjust
          int realWidth = previewHeight * streamWidth / streamHeight;
          GLES20.glViewport((previewWidth - realWidth) / 2, 0, realWidth, previewHeight);
        } else { //fill
          int realHeight = previewWidth * streamHeight / streamWidth;
          GLES20.glViewport(0, -((realHeight - previewWidth) / 2), previewWidth, realHeight);
        }
      } else { //portrait
        if (mode == 0) { //adjust
          int realHeight = previewWidth * streamHeight / streamWidth;
          GLES20.glViewport(0, (previewHeight - realHeight) / 2, previewWidth, realHeight);
        } else { //fill
          int realWidth = previewHeight * streamWidth / streamHeight;
          GLES20.glViewport(-((realWidth - previewWidth) / 2), 0, realWidth, previewHeight);
        }
      }
    } else {
      GLES20.glViewport(0, 0, previewWidth, previewHeight);
    }
  }

  public static void updateMatrix(int rotation, int width, int height, boolean isPreview,
                                  boolean isPortrait, boolean flipStreamHorizontal,
                                  boolean flipStreamVertical, int mode, float[] MVPMatrix) {
    PointF scale;
    if (mode == 2 || mode == 3) {
      scale = getScaleForRotation(rotation, width, height, isPortrait, isPreview);
    } else {
      scale = new PointF(1f, 1f);
    }
    if (!isPreview) {
      float xFlip = flipStreamHorizontal ? -1f : 1f;
      float yFlip = flipStreamVertical ? -1f : 1f;
      scale = new PointF(scale.x * xFlip, scale.y * yFlip);
    }

    if (!isPreview && !isPortrait) rotation += 90;

    updateMatrix(rotation, scale, MVPMatrix);
  }

  private static void updateMatrix(int rotation, PointF scale, float[] MVPMatrix) {
    Matrix.setIdentityM(MVPMatrix, 0);
    Matrix.scaleM(MVPMatrix, 0, scale.x, scale.y, 1f);
    Matrix.rotateM(MVPMatrix, 0, rotation, 0f, 0f, -1f);
  }
  private static PointF getScaleForRotation(int rotation, int width, int height, boolean isPortrait,
      boolean isPreview) {
    float scaleX = 1f;
    float scaleY = 1f;
    if (!isPreview) {
      if (isPortrait && rotation != 0 && rotation != 180) { //portrait
        final float adjustedWidth = width * (width / (float) height);
        scaleY = adjustedWidth / height;
      } else if (!isPortrait && rotation != 90 && rotation != 270) { //landscape
        final float adjustedWidth = height * (height / (float) width);
        scaleX = adjustedWidth / width;
      }
    }
    return new PointF(scaleX, scaleY);
  }
}<commit_msg>Fixed rotation for different modes<commit_after>package com.pedro.encoder.utils.gl;

import android.graphics.PointF;
import android.opengl.GLES20;
import android.opengl.Matrix;

/**
 * Created by pedro on 22/03/19.
 */

public class SizeCalculator {

  public static void calculateViewPort(boolean keepAspectRatio, int mode, int previewWidth,
      int previewHeight, int streamWidth, int streamHeight) {
    if (keepAspectRatio) {
      if (previewWidth > previewHeight) { //landscape
        if (mode == 0) { //adjust
          int realWidth = previewHeight * streamWidth / streamHeight;
          GLES20.glViewport((previewWidth - realWidth) / 2, 0, realWidth, previewHeight);
        } else { //fill
          int realHeight = previewWidth * streamHeight / streamWidth;
          GLES20.glViewport(0, -((realHeight - previewWidth) / 2), previewWidth, realHeight);
        }
      } else { //portrait
        if (mode == 0) { //adjust
          int realHeight = previewWidth * streamHeight / streamWidth;
          GLES20.glViewport(0, (previewHeight - realHeight) / 2, previewWidth, realHeight);
        } else { //fill
          int realWidth = previewHeight * streamWidth / streamHeight;
          GLES20.glViewport(-((realWidth - previewWidth) / 2), 0, realWidth, previewHeight);
        }
      }
    } else {
      GLES20.glViewport(0, 0, previewWidth, previewHeight);
    }
  }

  public static void updateMatrix(int rotation, int width, int height, boolean isPreview,
                                  boolean isPortrait, boolean flipStreamHorizontal,
                                  boolean flipStreamVertical, int mode, float[] MVPMatrix) {
    PointF scale;
    if (mode == 2 || mode == 3) { // stream rotation is enabled
      scale = getScaleForRotation(rotation, width, height, isPortrait, isPreview);
      if (!isPreview && !isPortrait) rotation += 90;
    } else {
      scale = new PointF(1f, 1f);
    }

    if (!isPreview) {
      float xFlip = flipStreamHorizontal ? -1f : 1f;
      float yFlip = flipStreamVertical ? -1f : 1f;
      scale = new PointF(scale.x * xFlip, scale.y * yFlip);
    }

    updateMatrix(rotation, scale, MVPMatrix);
  }

  private static void updateMatrix(int rotation, PointF scale, float[] MVPMatrix) {
    Matrix.setIdentityM(MVPMatrix, 0);
    Matrix.scaleM(MVPMatrix, 0, scale.x, scale.y, 1f);
    Matrix.rotateM(MVPMatrix, 0, rotation, 0f, 0f, -1f);
  }
  private static PointF getScaleForRotation(int rotation, int width, int height, boolean isPortrait,
      boolean isPreview) {
    float scaleX = 1f;
    float scaleY = 1f;
    if (!isPreview) {
      if (isPortrait && rotation != 0 && rotation != 180) { //portrait
        final float adjustedWidth = width * (width / (float) height);
        scaleY = adjustedWidth / height;
      } else if (!isPortrait && rotation != 90 && rotation != 270) { //landscape
        final float adjustedWidth = height * (height / (float) width);
        scaleX = adjustedWidth / width;
      }
    }
    return new PointF(scaleX, scaleY);
  }
}
{'fraction_non_alphanumeric': 0.11120331950207468, 'fraction_numerical': 0.019253112033195022, 'mean_word_length': 2.62575210589651, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4627740', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1766, 'n_words': 502}