private void SetStereoMode(BaseMediaPlayer.StereoMode stereoMode)
        {
            if (stereoMode == mediaPlayer.CurrentStereoMode)
            {
                return;
            }

            if (mediaPlayer.MediaScreen == null)
            {
                return;
            }

            mediaPlayer.CurrentStereoMode = stereoMode;
            float frameAspectRatio =
                ImageBasedProjectionDetectorHelpers.CalculateFrameAspectRatio(mediaPlayer.RawAspectRatio, stereoMode);

            mediaPlayer.CurrentAspectRatio = frameAspectRatio;

            mediaPlayer.SaveCurrentFormat();
        }
        private StereoProjectionFormat CalculateFormat(Texture2D originalTexture)
        {
            if (renderTexture == null)
            {
                return(null);
            }

            int width  = renderTexture.width;
            int height = renderTexture.height;

            // Read the blitted texture.
            RenderTexture.active = renderTexture;
            Texture2D targetTexture = new Texture2D(width, height);

            targetTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            DisposeRenderTexture();

            // Get the pixels from the texture.
            Color32[] pixels = targetTexture.GetPixels32();

            // Calculate the lightest and darkest color values in the image.
            // This is used to normalize the differences between pixels.
            Vector3 darkest;
            Vector3 lightest;

            CalculateLightestAndDarkestColors(pixels, out lightest, out darkest);

            int halfHeight = height / 2;
            int halfWidth  = width / 2;

      #if DEBUG_IMAGE_DETECTOR
            Texture2D fullTex      = new Texture2D(width, height, TextureFormat.RGB24, false);
            Texture2D leftRightTex = new Texture2D(halfWidth, height, TextureFormat.RGB24, false);
            Texture2D topBottomTex = new Texture2D(width, halfHeight, TextureFormat.RGB24, false);
      #endif

            Vector3 leftRightSum = Vector3.zero;
            Vector3 topBottomSum = Vector3.zero;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int     pixelIndex      = LocationToIndex(x, y, width, height);
                    Color   pixel           = pixels[pixelIndex];
                    Vector3 normalizedPixel = NormalizeColorToRange(pixel, lightest, darkest);

                    if (x < halfWidth)
                    {
                        int     rightPixelIndex      = LocationToIndex(x + halfWidth, y, width, height);
                        Color   rightPixel           = pixels[rightPixelIndex];
                        Vector3 normalizedRightPixel = NormalizeColorToRange(rightPixel, lightest, darkest);
                        Vector3 diffPixel            = normalizedPixel - normalizedRightPixel;

                        leftRightSum += SquaredVector(diffPixel);
            #if DEBUG_IMAGE_DETECTOR
                        leftRightTex.SetPixel(x, y, pixel - rightPixel);
            #endif
                    }

                    if (y < halfHeight)
                    {
                        int     bottomPixelIndex      = LocationToIndex(x, y + halfHeight, width, height);
                        Color   bottomPixel           = pixels[bottomPixelIndex];
                        Vector3 normalizedBottomPixel = NormalizeColorToRange(bottomPixel, lightest, darkest);
                        Vector3 diffPixel             = normalizedPixel - normalizedBottomPixel;

                        topBottomSum += SquaredVector(diffPixel);
            #if DEBUG_IMAGE_DETECTOR
                        topBottomTex.SetPixel(x, y, pixel - bottomPixel);
            #endif
                    }

          #if DEBUG_IMAGE_DETECTOR
                    fullTex.SetPixel(x, y, pixel);
          #endif
                }
            }

      #if DEBUG_IMAGE_DETECTOR
            string mediaPath = MediaPlayer.FilePath;
            mediaPath = Path.GetFileNameWithoutExtension(mediaPath);

            fullTex.Apply();
            byte[] bytes    = fullTex.EncodeToPNG();
            string fullPath = Application.persistentDataPath + "/" + mediaPath + "Full.png";
            File.WriteAllBytes(fullPath, bytes);
            leftRightTex.Apply();
            bytes = leftRightTex.EncodeToPNG();
            string leftRightPath = Application.persistentDataPath + "/" + mediaPath + "LeftRightDiff.png";
            File.WriteAllBytes(leftRightPath, bytes);
            topBottomTex.Apply();
            bytes = topBottomTex.EncodeToPNG();
            string topBottomPath = Application.persistentDataPath + "/" + mediaPath + "TopBottomDiff.png";
            File.WriteAllBytes(topBottomPath, bytes);

            Debug.Log("Wrote Image Diffs." +
                      "\nFull=" + fullPath +
                      "\nLeftRight=" + leftRightPath +
                      "\nTopBottom=" + topBottomPath);
      #endif

            // Left Right
            int     numPixelsLeftRight       = halfWidth * height;
            Vector3 leftRightRatio           = leftRightSum / numPixelsLeftRight;
            float   leftRightSimilarityRatio = AverageValueOfVector(leftRightRatio);

            // Top Bottom
            int     numPixelsTopBottom       = width * halfHeight;
            Vector3 topBottomRatio           = topBottomSum / numPixelsTopBottom;
            float   topBottomSimilarityRatio = AverageValueOfVector(topBottomRatio);

      #if DEBUG_IMAGE_DETECTOR
            Debug.Log("Left/Right similarity ratio = " + leftRightSimilarityRatio);
            Debug.Log("Top/Bottom similarity ratio = " + topBottomSimilarityRatio);
      #endif

            StereoProjectionFormat format = new StereoProjectionFormat();

            if (leftRightSimilarityRatio < SIMILARITY_THRESHOLD &&
                leftRightSimilarityRatio < topBottomSimilarityRatio)
            {
                format.stereoMode = BaseMediaPlayer.StereoMode.LeftRight;
            }
            else if (topBottomSimilarityRatio < SIMILARITY_THRESHOLD)
            {
                format.stereoMode = BaseMediaPlayer.StereoMode.TopBottom;
            }

            format.frameAspectRatio =
                ImageBasedProjectionDetectorHelpers.CalculateFrameAspectRatio(originalTexture, format.stereoMode);

            if (format.frameAspectRatio >= SPHERICAL_THRESHOLD)
            {
                format.projectionMode = BaseMediaPlayer.ProjectionMode.Projection360;
            }

      #if DEBUG_IMAGE_DETECTOR
            Debug.Log("Frame Aspect Ratio = " + format.frameAspectRatio);
      #endif

            return(format);
        }