<commit_before>﻿/******************************************************************************\
* Copyright (C) Leap Motion, Inc. 2011-2014.                                   *
* Leap Motion proprietary. Licensed under Apache 2.0                           *
* Available at http://www.apache.org/licenses/LICENSE-2.0.html                 *
\******************************************************************************/

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Leap;

// To use the LeapImageRetriever you must be on version 2.1+
// and enable "Allow Images" in the Leap Motion settings.
public class LeapImageRetriever : MonoBehaviour {
    public const string IR_SHADER_VARIANT_NAME = "LEAP_FORMAT_IR";
    public const string RGB_SHADER_VARIANT_NAME = "LEAP_FORMAT_RGB";
	public const string DEPTH_TEXTURE_VARIANT_NAME = "USE_DEPTH_TEXTURE";
    public const int IMAGE_WARNING_WAIT = 10;

    public enum EYE {
        LEFT = 0,
        RIGHT = 1
    }

    public enum SYNC_MODE {
        SYNC_WITH_HANDS,
        LOW_LATENCY
    }

    public EYE eye = (EYE)(-1);
    [Tooltip ("Should the image match the tracked hand, or should it be displayed as fast as possible")]
    public SYNC_MODE syncMode = SYNC_MODE.LOW_LATENCY;
    public float gammaCorrection = 1.0f;


    private int _missedImages = 0;
    private Controller _controller;

    //Information about the current format the retriever is configured for.  Used to detect changes in format
    private int _currentWidth = 0;
    private int _currentHeight = 0;
    private Image.FormatType _currentFormat = (Image.FormatType)(-1);

    //ImageList to use during rendering.  Can either be updated in OnPreRender or in Update
    private ImageList _imageList;

    //Holders for Image Based Materials
    private static List<LeapImageBasedMaterial> _registeredImageBasedMaterials = new List<LeapImageBasedMaterial>();
    private static List<LeapImageBasedMaterial> _imageBasedMaterialsToInit = new List<LeapImageBasedMaterial>();

    // Main texture.
    private Texture2D _mainTexture = null;
    private byte[] _mainTextureData = null;

    //Used to recalculate the distortion every time a hand enters the frame.  Used because there is no way to tell if the device has flipped (which changes the distortion)
    private bool _requestDistortionRecalc = false;
    private bool _forceDistortionRecalc = false;

    // Distortion textures.
    private Texture2D _distortion = null;
    private Color32[] _distortionPixels = null;

    public static void registerImageBasedMaterial(LeapImageBasedMaterial imageBasedMaterial) {
        _registeredImageBasedMaterials.Add(imageBasedMaterial);
        _imageBasedMaterialsToInit.Add(imageBasedMaterial);
    }

    public static void unregisterImageBasedMaterial(LeapImageBasedMaterial imageBasedMaterial) {
        _registeredImageBasedMaterials.Remove(imageBasedMaterial);
    }

    private void initImageBasedMaterial(LeapImageBasedMaterial imageBasedMaterial) {
        Material material = imageBasedMaterial.GetComponent<Renderer>().material;

        switch (_currentFormat) {
            case Image.FormatType.INFRARED:
                material.EnableKeyword(IR_SHADER_VARIANT_NAME);
                material.DisableKeyword(RGB_SHADER_VARIANT_NAME);
                break;
            case (Image.FormatType)4:
                material.EnableKeyword(RGB_SHADER_VARIANT_NAME);
                material.DisableKeyword(IR_SHADER_VARIANT_NAME);
                break;
            default:
                Debug.LogWarning("Unexpected format type " + _currentFormat);
                break;
        }

		if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth)) {
			material.EnableKeyword(DEPTH_TEXTURE_VARIANT_NAME);
		} else {
			material.DisableKeyword(DEPTH_TEXTURE_VARIANT_NAME);
		}

        imageBasedMaterial.GetComponent<Renderer>().material.SetFloat("_LeapGammaCorrectionExponent", 1.0f / gammaCorrection);
    }

    private void updateImageBasedMaterial(LeapImageBasedMaterial imageBasedMaterial, ref Image image) {
        imageBasedMaterial.GetComponent<Renderer>().material.SetTexture("_LeapTexture", _mainTexture);

        Vector4 projection = new Vector4();
        projection.x = GetComponent<Camera>().projectionMatrix[0, 2];
        projection.z = GetComponent<Camera>().projectionMatrix[0, 0];
        projection.w = GetComponent<Camera>().projectionMatrix[1, 1];
        imageBasedMaterial.GetComponent<Renderer>().material.SetVector("_LeapProjection", projection);

        if (_distortion == null) {
            initDistortion(image);
            loadDistortion(image);
            _forceDistortionRecalc = false;
        }

        if (_forceDistortionRecalc || (_requestDistortionRecalc && _controller.Frame().Hands.Count != 0)) {
            loadDistortion(image);
            _requestDistortionRecalc = false;
            _forceDistortionRecalc = false;
        }

        imageBasedMaterial.GetComponent<Renderer>().material.SetTexture("_LeapDistortion", _distortion);
    }

    private TextureFormat getTextureFormat(Image image) {
        switch (image.Format) {<commit_msg>Fixed spaces<commit_after>﻿/******************************************************************************\
* Copyright (C) Leap Motion, Inc. 2011-2014.                                   *
* Leap Motion proprietary. Licensed under Apache 2.0                           *
* Available at http://www.apache.org/licenses/LICENSE-2.0.html                 *
\******************************************************************************/

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Leap;

// To use the LeapImageRetriever you must be on version 2.1+
// and enable "Allow Images" in the Leap Motion settings.
public class LeapImageRetriever : MonoBehaviour {
    public const string IR_SHADER_VARIANT_NAME = "LEAP_FORMAT_IR";
    public const string RGB_SHADER_VARIANT_NAME = "LEAP_FORMAT_RGB";
    public const string DEPTH_TEXTURE_VARIANT_NAME = "USE_DEPTH_TEXTURE";
    public const int IMAGE_WARNING_WAIT = 10;

    public enum EYE {
        LEFT = 0,
        RIGHT = 1
    }

    public enum SYNC_MODE {
        SYNC_WITH_HANDS,
        LOW_LATENCY
    }

    public EYE eye = (EYE)(-1);
    [Tooltip ("Should the image match the tracked hand, or should it be displayed as fast as possible")]
    public SYNC_MODE syncMode = SYNC_MODE.LOW_LATENCY;
    public float gammaCorrection = 1.0f;


    private int _missedImages = 0;
    private Controller _controller;

    //Information about the current format the retriever is configured for.  Used to detect changes in format
    private int _currentWidth = 0;
    private int _currentHeight = 0;
    private Image.FormatType _currentFormat = (Image.FormatType)(-1);

    //ImageList to use during rendering.  Can either be updated in OnPreRender or in Update
    private ImageList _imageList;

    //Holders for Image Based Materials
    private static List<LeapImageBasedMaterial> _registeredImageBasedMaterials = new List<LeapImageBasedMaterial>();
    private static List<LeapImageBasedMaterial> _imageBasedMaterialsToInit = new List<LeapImageBasedMaterial>();

    // Main texture.
    private Texture2D _mainTexture = null;
    private byte[] _mainTextureData = null;

    //Used to recalculate the distortion every time a hand enters the frame.  Used because there is no way to tell if the device has flipped (which changes the distortion)
    private bool _requestDistortionRecalc = false;
    private bool _forceDistortionRecalc = false;

    // Distortion textures.
    private Texture2D _distortion = null;
    private Color32[] _distortionPixels = null;

    public static void registerImageBasedMaterial(LeapImageBasedMaterial imageBasedMaterial) {
        _registeredImageBasedMaterials.Add(imageBasedMaterial);
        _imageBasedMaterialsToInit.Add(imageBasedMaterial);
    }

    public static void unregisterImageBasedMaterial(LeapImageBasedMaterial imageBasedMaterial) {
        _registeredImageBasedMaterials.Remove(imageBasedMaterial);
    }

    private void initImageBasedMaterial(LeapImageBasedMaterial imageBasedMaterial) {
        Material material = imageBasedMaterial.GetComponent<Renderer>().material;

        switch (_currentFormat) {
            case Image.FormatType.INFRARED:
                material.EnableKeyword(IR_SHADER_VARIANT_NAME);
                material.DisableKeyword(RGB_SHADER_VARIANT_NAME);
                break;
            case (Image.FormatType)4:
                material.EnableKeyword(RGB_SHADER_VARIANT_NAME);
                material.DisableKeyword(IR_SHADER_VARIANT_NAME);
                break;
            default:
                Debug.LogWarning("Unexpected format type " + _currentFormat);
                break;
        }

        if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth)) {
            material.EnableKeyword(DEPTH_TEXTURE_VARIANT_NAME);
        } else {
            material.DisableKeyword(DEPTH_TEXTURE_VARIANT_NAME);
        }

        imageBasedMaterial.GetComponent<Renderer>().material.SetFloat("_LeapGammaCorrectionExponent", 1.0f / gammaCorrection);
    }

    private void updateImageBasedMaterial(LeapImageBasedMaterial imageBasedMaterial, ref Image image) {
        imageBasedMaterial.GetComponent<Renderer>().material.SetTexture("_LeapTexture", _mainTexture);

        Vector4 projection = new Vector4();
        projection.x = GetComponent<Camera>().projectionMatrix[0, 2];
        projection.z = GetComponent<Camera>().projectionMatrix[0, 0];
        projection.w = GetComponent<Camera>().projectionMatrix[1, 1];
        imageBasedMaterial.GetComponent<Renderer>().material.SetVector("_LeapProjection", projection);

        if (_distortion == null) {
            initDistortion(image);
            loadDistortion(image);
            _forceDistortionRecalc = false;
        }

        if (_forceDistortionRecalc || (_requestDistortionRecalc && _controller.Frame().Hands.Count != 0)) {
            loadDistortion(image);
            _requestDistortionRecalc = false;
            _forceDistortionRecalc = false;
        }

        imageBasedMaterial.GetComponent<Renderer>().material.SetTexture("_LeapDistortion", _distortion);
    }

    private TextureFormat getTextureFormat(Image image) {
        switch (image.Format) {
{'fraction_non_alphanumeric': 0.10457142857142857, 'fraction_numerical': 0.00780952380952381, 'mean_word_length': 3.316070694615701, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1207709', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2725, 'n_words': 669}