<commit_before>package io.github.movementspeed.nhglib.files;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Array;
import io.github.movementspeed.nhglib.graphics.ogl.NhgFloatTextureData;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author <NAME>
 *         http://aicp70.wixsite.com/aboutme/single-post/2015/10/29/1-HDR-file-and-its-readin-program-by-JAVA
 *         This class is used to convert a HDR format image into a three-dimension float array represents the
 *         RGB channels of the original image.
 */
public class HDRData {
    //the width of the HDR image
    private int width;
    //the height of the HDR image
    private int height;
    //This three-dimension float array is storing the three channels' information of the image.
    //For example, pixels[2][3][1] presents the red channel[][][1] of row No.2 and col No.3 's pixel
    private float[][][] pixels;
    //This three-dimension int array is storing the four channels' information.
    //[][][] the first and second is location information
    //[][][] the third one is the R,G,B,E. which is its associated information.
    private int[][][] buffers;
    //This two-dimension float array is storing the luminance information of the pixel.
    //We use the YUV format to calculate the luminance by lum=0.299*R+0.587*G+0.114*B
    private float[][] lum;

    private float[] flatPixelArray;

    //The mean value of the lum[][]
    private float lummean;
    //The maximum value of the lum[][]
    private float lummax;
    //The minimum value of the lum[][]
    private float lummin;

    private Texture texture;
    private Pixmap pixmap;

    public void clear() {
        pixels = null;
        buffers = null;
        lum = null;
        flatPixelArray = null;
        width = 0;
        height = 0;
        lummean = 0;
        lummax = 0;
        lummin = 0;
        texture.dispose();
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public float[][] getLumArray() {
        return lum;
    }

    public float[][][] getPixelArray() {
        return pixels;
    }

    public float[] getFlatPixelArray() {
        Array<Float> floats = new Array<>();
        flatPixelArray = new float[width * height * 3];

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                float r = pixels[y][x][0];
                float g = pixels[y][x][1];
                float b = pixels[y][x][2];

                floats.addAll(r, g, b);
            }
        }

        for (int i = 0; i < floats.size; i++) {
            flatPixelArray[i] = floats.get(i);
        }

        return flatPixelArray;
    }

    public float getLummax() {
        return lummax;
    }

    public float getLummin() {
        return lummin;
    }

    public float getLummean() {
        return lummean;
    }

    //Construction method if the input is a file.
    public HDRData(byte[] in) throws IOException {
        if (in == null)
            throw new NullPointerException();

        read(in);
    }

    public Texture toTexture() {
        float[] rgb = getFlatPixelArray();
        float min = Float.MAX_VALUE, max = Float.MIN_VALUE;

        for (float f : rgb) {
            if (f < min) {
                min = f;
            }

            if (f > max) {
                max = f;
            }
        }

        if (Gdx.app.getType() == Application.ApplicationType.Desktop || Gdx.graphics.supportsExtension("OES_texture_float")) {
            NhgFloatTextureData data = new NhgFloatTextureData(width, height, 3);
            data.prepare();
            data.getBuffer().put(rgb);
            data.getBuffer().flip();

            texture = new Texture(data);
        } else {
            pixmap = new Pixmap(width, height, Pixmap.Format.RGB888);

            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    float r = pixels[y][x][0];
                    float g = pixels[y][x][1];
                    float b = pixels[y][x][2];

                    r /= r + 1.0f;
                    g /= g + 1.0f;
                    b /= b + 1.0f;

                    pixmap.drawPixel(x, y, Color.rgba8888(r, g, b, 1.0f));
                }
            }

            texture = new Texture(pixmap);
        }

        return texture;
    }

    //Construction method if the input is a InputStream.
    //Parse the HDR file by its format. HDR format encode can be seen in Radiance HDR(.pic,.hdr) file format
    private void read(byte[] in) throws IOException {
        ByteCounter counter = new ByteCounter();
        //Parse HDR file's header line
        //readLine(InputStream in) method will be introduced later.

        //The first line of the HDR file. If it is a HDR file, the first line should be "#?RADIANCE"
        //If not, we will throw a IllegalArgumentException.
        String isHDR = readLine(counter, in);
        if (!isHDR.equals("#?RADIANCE"))
            throw new IllegalArgumentException("Unrecognized format: " + isHDR);

        //Besides the first line, there are serval lines describe the different information of this HDR file.
        //Maybe it will have the exposure time, format(Must be either"32-bit_rle_rgbe" or "32-bit_rle_xyze")
        //Also the owner's information, the software's versioin, etc.

        //The above information is not so important for us.
        //The only important information for us is the Resolution which shows the size of the HDR image
        //The resolution information's format is fixed. Usaually, it will be -Y 1024 +X 2048 something like this.
        String inform = readLine(counter, in);
        while (!inform.equals("")) {
            inform = readLine(counter, in);
        }
        inform = readLine(counter, in);
        String[] tokens = inform.split(" ", 4);
        if (tokens[0].charAt(1) == 'Y') {<commit_msg>- Trying to fix HDRData taking memory which is no longer needed<commit_after>package io.github.movementspeed.nhglib.files;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.FloatArray;
import io.github.movementspeed.nhglib.graphics.ogl.NhgFloatTextureData;
import io.github.movementspeed.nhglib.utils.graphics.GLUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author <NAME>
 *         http://aicp70.wixsite.com/aboutme/single-post/2015/10/29/1-HDR-file-and-its-readin-program-by-JAVA
 *         This class is used to convert a HDR format image into a three-dimension float array represents the
 *         RGB channels of the original image.
 */
public class HDRData {
    //the width of the HDR image
    private int width;
    //the height of the HDR image
    private int height;
    //This three-dimension float array is storing the three channels' information of the image.
    //For example, pixels[2][3][1] presents the red channel[][][1] of row No.2 and col No.3 's pixel
    private float[][][] pixels;
    //This three-dimension int array is storing the four channels' information.
    //[][][] the first and second is location information
    //[][][] the third one is the R,G,B,E. which is its associated information.
    private int[][][] buffers;
    //This two-dimension float array is storing the luminance information of the pixel.
    //We use the YUV format to calculate the luminance by lum=0.299*R+0.587*G+0.114*B
    private float[][] lum;

    private float[] flatPixelArray;

    //The mean value of the lum[][]
    private float lummean;
    //The maximum value of the lum[][]
    private float lummax;
    //The minimum value of the lum[][]
    private float lummin;

    private Texture texture;
    private Pixmap pixmap;

    public void clear() {
        pixels = null;
        buffers = null;
        lum = null;
        flatPixelArray = null;
        width = 0;
        height = 0;
        lummean = 0;
        lummax = 0;
        lummin = 0;
        texture.dispose();
        texture = null;
        System.gc();
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public float[][] getLumArray() {
        return lum;
    }

    public float[][][] getPixelArray() {
        return pixels;
    }

    public float[] getFlatPixelArray() {
        FloatArray floats = new FloatArray();
        flatPixelArray = new float[width * height * 3];

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                float r = pixels[y][x][0];
                float g = pixels[y][x][1];
                float b = pixels[y][x][2];

                floats.addAll(r, g, b);
            }
        }

        for (int i = 0; i < floats.size; i++) {
            flatPixelArray[i] = floats.get(i);
        }

        return flatPixelArray;
    }

    public float getLummax() {
        return lummax;
    }

    public float getLummin() {
        return lummin;
    }

    public float getLummean() {
        return lummean;
    }

    //Construction method if the input is a file.
    public HDRData(byte[] in) throws IOException {
        if (in == null)
            throw new NullPointerException();

        read(in);
    }

    public Texture toTexture() {
        float[] rgb = getFlatPixelArray();
        float min = Float.MAX_VALUE, max = Float.MIN_VALUE;

        for (float f : rgb) {
            if (f < min) {
                min = f;
            }

            if (f > max) {
                max = f;
            }
        }

        if (GLUtils.isFloatTextureSupported()) {
            NhgFloatTextureData data = new NhgFloatTextureData(width, height, 3);
            data.prepare();
            data.getBuffer().put(rgb);
            data.getBuffer().flip();

            texture = new Texture(data);
        } else {
            pixmap = new Pixmap(width, height, Pixmap.Format.RGB888);

            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    float r = pixels[y][x][0];
                    float g = pixels[y][x][1];
                    float b = pixels[y][x][2];

                    r /= r + 1.0f;
                    g /= g + 1.0f;
                    b /= b + 1.0f;

                    pixmap.drawPixel(x, y, Color.rgba8888(r, g, b, 1.0f));
                }
            }

            texture = new Texture(pixmap);
            pixmap.dispose();
        }

        System.gc();
        return texture;
    }

    //Construction method if the input is a InputStream.
    //Parse the HDR file by its format. HDR format encode can be seen in Radiance HDR(.pic,.hdr) file format
    private void read(byte[] in) throws IOException {
        ByteCounter counter = new ByteCounter();
        //Parse HDR file's header line
        //readLine(InputStream in) method will be introduced later.

        //The first line of the HDR file. If it is a HDR file, the first line should be "#?RADIANCE"
        //If not, we will throw a IllegalArgumentException.
        String isHDR = readLine(counter, in);
        if (!isHDR.equals("#?RADIANCE"))
            throw new IllegalArgumentException("Unrecognized format: " + isHDR);

        //Besides the first line, there are serval lines describe the different information of this HDR file.
        //Maybe it will have the exposure time, format(Must be either"32-bit_rle_rgbe" or "32-bit_rle_xyze")
        //Also the owner's information, the software's versioin, etc.

        //The above information is not so important for us.
        //The only important information for us is the Resolution which shows the size of the HDR image
        //The resolution information's format is fixed. Usaually, it will be -Y 1024 +X 2048 something like this.
        String inform = readLine(counter, in);
        while (!inform.equals("")) {
            inform = readLine(counter, in);
        }
        inform = readLine(counter, in);
        String[] tokens = inform.split(" ", 4);
        if (tokens[0].charAt(1) == 'Y') {
{'fraction_non_alphanumeric': 0.11204887066956032, 'fraction_numerical': 0.01237842617152962, 'mean_word_length': 1.9815480469686078, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, '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': '4669801', 'n_tokens_mistral': 3848, 'n_tokens_neox': 3578, 'n_words': 1198}