<commit_before>package com.jwetherell.motion_detection.image;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/**
 * This abstract class is used to process images.
 * 
 * @author <NAME> <<EMAIL>>
 */
public abstract class ImageProcessing {
	public static final int A = 0;
	public static final int R = 1;
	public static final int G = 2;
	public static final int B = 3;
	
	public static final int H = 0;
	public static final int S = 1;
	public static final int L = 2;

	//Get RGB from Integer
	public static float[] getARGB(int pixel) {
    	int a = (pixel >> 24) & 0xff;
    	int r = (pixel >> 16) & 0xff;
    	int g = (pixel >> 8) & 0xff;
    	int b = (pixel) & 0xff;
    	return (new float[]{a,r,g,b});
	}
	
	//Get HSL from RGB
    //H is 0-360 (degrees)
    //H and S are 0-100 (percent)
	public static float[] convertToHSL(int r, int g, int b) {
        float red = r / 255;
        float green = g / 255;
        float blue = b / 255;
        
		float minComponent = Math.min(red, Math.min(green, blue));
        float maxComponent = Math.max(red, Math.max(green, blue));
        float range = maxComponent - minComponent;
        float[] HSL = new float[3];
        
        HSL[L] = (maxComponent + minComponent) / 2;

        if(range == 0) { // Monochrome image
        	HSL[H] = HSL[S] = 0;
        } else {
            HSL[S] = 	(HSL[L] > 0.5) ? 
		    				range / (2 - range) 
		    			: 
		    				range / (maxComponent + minComponent);
            
            if(red == maxComponent) {
            	HSL[H] = (blue - green) / range;
            } else if(green == maxComponent) {
            	HSL[H] = 2 + (blue - red) / range;
            } else if(blue == maxComponent) {
            	HSL[H] = 4 +(red - green) / range;
            }
        }
        
        //convert to 0-360 (degrees)
        HSL[H] *= 60;
        if (HSL[H]<0) HSL[H] += 360;
        
        //convert to 0-100 (percent)
        HSL[S] *= 100;
        HSL[L] *= 100;
        
		return HSL;
	}

	public static float getBrightnessAtPoint(int pixel) {
		//Get RGB from Integer
    	int r = (pixel >> 16) & 0xff;
    	int g = (pixel >> 8) & 0xff;
    	int b = (pixel) & 0xff;
    	
    	//Convert RGB to HSL (not using method above because I don't want to create
    	//an extra float[] for every pixel.
        float red = r / 255;
        float green = g / 255;
        float blue = b / 255;
        float h=0,l=0;
        
		float minComponent = Math.min(red, Math.min(green, blue));
        float maxComponent = Math.max(red, Math.max(green, blue));
        float range = maxComponent - minComponent;
        
        l = (maxComponent + minComponent) / 2;

        if(range > 0) {
            if(red == maxComponent) {
            	h = (blue - green) / range;
            } else if(green == maxComponent) {
            	h = 2 + (blue - red) / range;
            } else if(blue == maxComponent) {
            	h = 4 +(red - green) / range;
            }
        }
        
        //convert to 0-360
        h *= 60;
        if (h<0) h += 360;
        
        //convert to 0-100
        l *= 100;

        //Convert the HSL into a single "brightness" representation
		return (float)((l * 0.5) + ((h / 360) * 50));
	}

	public static int[] decodeYUV420SP(byte[] yuv420sp, int width, int height) {
		if (yuv420sp==null) return null;
		
		final int frameSize = width * height;
		int[] rgb = new int[frameSize];

	    for (int j = 0, yp = 0; j < height; j++) {<commit_msg>Updated the RGB to HSL code to calucate correctly<commit_after>package com.jwetherell.motion_detection.image;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/**
 * This abstract class is used to process images.
 * 
 * @author <NAME> <<EMAIL>>
 */
public abstract class ImageProcessing {
	public static final int A = 0;
	public static final int R = 1;
	public static final int G = 2;
	public static final int B = 3;
	
	public static final int H = 0;
	public static final int S = 1;
	public static final int L = 2;

	//Get RGB from Integer
	public static float[] getARGB(int pixel) {
    	int a = (pixel >> 24) & 0xff;
    	int r = (pixel >> 16) & 0xff;
    	int g = (pixel >> 8) & 0xff;
    	int b = (pixel) & 0xff;
    	return (new float[]{a,r,g,b});
	}
	
	//Get HSL from RGB
    //H is 0-360 (degrees)
    //H and S are 0-100 (percent)
	public static float[] convertToHSL(int r, int g, int b) {
        float red = r / 255;
        float green = g / 255;
        float blue = b / 255;
        
		float minComponent = Math.min(red, Math.min(green, blue));
        float maxComponent = Math.max(red, Math.max(green, blue));
        float range = maxComponent - minComponent;
        float[] HSL = new float[3];
        
        HSL[L] = (maxComponent + minComponent) / 2;

        if(range == 0) { // Monochrome image
        	HSL[H] = HSL[S] = 0;
        } else {
            HSL[S] = 	(HSL[L] > 0.5) ? 
		    				range / (2 - range) 
		    			: 
		    				range / (maxComponent + minComponent);
            
            if(red == maxComponent) {
            	HSL[H] = (blue - green) / range;
            } else if(green == maxComponent) {
            	HSL[H] = 2 + (blue - red) / range;
            } else if(blue == maxComponent) {
            	HSL[H] = 4 +(red - green) / range;
            }
        }
        
        //convert to 0-360 (degrees)
        HSL[H] *= 60;
        if (HSL[H]<0) HSL[H] += 360;
        
        //convert to 0-100 (percent)
        HSL[S] *= 100;
        HSL[L] *= 100;
        
		return HSL;
	}

	public static float getBrightnessAtPoint(int pixel) {
		//Get RGB from Integer
    	int r = (pixel >> 16) & 0xff;
    	int g = (pixel >> 8) & 0xff;
    	int b = (pixel) & 0xff;
    	
    	//Convert RGB to HSL (not using method above because I don't want to create
    	//an extra float[] for every pixel.
        float red = r / 255;
        float green = g / 255;
        float blue = b / 255;
        float h=0,l=0;
        
		float minComponent = Math.min(red, Math.min(green, blue));
        float maxComponent = Math.max(red, Math.max(green, blue));
        float range = maxComponent - minComponent;
        
        l = (maxComponent + minComponent) / 2;

        if(range > 0) {
            if(red == maxComponent) {
            	h = (blue - green) / range;
            } else if(green == maxComponent) {
            	h = 2 + (blue - red) / range;
            } else if(blue == maxComponent) {
            	h = 4 +(red - green) / range;
            }
        }
        
        //convert to 0-360
        h *= 60;
        if (h<0) h += 360;
        
        //convert to 0-100
        l *= 100;

        //Convert the HSL into a single "brightness" representation
        //brightness is between 0-100 using 50% lightness and 50% hue
		return (float)((l * 0.5) + ((h / 360) * 50));
	}

	public static int[] decodeYUV420SP(byte[] yuv420sp, int width, int height) {
		if (yuv420sp==null) return null;
		
		final int frameSize = width * height;
		int[] rgb = new int[frameSize];

	    for (int j = 0, yp = 0; j < height; j++) {
{'fraction_non_alphanumeric': 0.13044080953701137, 'fraction_numerical': 0.03382312170779041, 'mean_word_length': 1.7686108979278587, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '14490', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2376, 'n_words': 762}