<commit_before>package phoenix;

import phoenix.Resource;

import lime.gl.GL;
import lime.gl.GLTexture;

import lime.utils.UInt8Array;

import lime.utils.Libs;
import lime.utils.ArrayBuffer;

enum FilterType {
    nearest;
    linear;    
}
enum ClampType {
    edge;
    repeat;    
    mirror;    
}

class Rectangle {
    public var x:Float;
    public var y:Float;
    public var width:Float;
    public var height:Float;
    public function new(_x,_y,_w,_h) {
        x = _x; 
        y = _y; 
        width = _w; 
        height = _h; 
    }
}

class Texture extends Resource {

    public var texture : GLTexture;
    public var data : UInt8Array;

    public var width : Int = -1;
    public var height : Int = -1;   
    public var loaded : Bool = false;
    public var onload : Texture -> Void;

    public function new( _manager : ResourceManager, _size : Dynamic = null ) {
        
        super( _manager, ResourceType.texture );

        id = 'Untitled';
        build( _size, null );
    }

    public function build(_size : Dynamic, _color: Dynamic) {

        if(_size == null) _size = {x:0, y:0};
        if(_color == null) _color = {r:1,g:1,b:1,a:1};
        if(_size.x > 0 && _size.y > 0) {

            width = Std.int(_size.x);
            height = Std.int(_size.y);

                //clear up old data in case
            data = null;
                //create a new set of pixels data
            data = new UInt8Array( cast new ArrayBuffer(  ) ); //width * height * 4
                //fill it up!
            for(x in 0 ... width) {
                for(y in 0 ... height) {
                    setPixel({x:x, y:y}, _color);
                }
            }

            texture = GL.createTexture();
        }
    }

    public function create_from_bytes_html(_asset_name:String, _asset_bytes, _width, _height ) {

        trace('MAX TEXTURE SIZE!! ' + GL.getParameter(GL.MAX_TEXTURE_SIZE));

        texture = GL.createTexture();

            //if no problems
        id = _asset_name;
        width = Std.int(_width);
        height = Std.int(_height);       

        //Now we can bind it
        bind();
            //And send GL the data
        GL.texImage2D(GL.TEXTURE_2D, 0, GL.RGBA, width, height, 0, GL.RGBA, GL.UNSIGNED_BYTE, cast _asset_bytes );

            //Set the properties
        set_filtering( FilterType.linear );
        set_clamp( ClampType.repeat );

        // image_bytes = null;
        // data = null; //todo - sven use lock/unlock
    }

        //Create from the bytes from Assets.getBytes() or other method
    public function create_from_bytes( _asset_name:String, _asset_bytes:haxe.io.Bytes ) {
        
        // trace(_asset_bytes);

        trace('MAX TEXTURE SIZE!! ' + GL.getParameter(GL.MAX_TEXTURE_SIZE));

        // #if lime_native
        var nme_bitmap_handle = nme_bitmap_data_from_bytes(_asset_bytes, null);
        var _width = nme_bitmap_data_width( nme_bitmap_handle );
        var _height = nme_bitmap_data_height( nme_bitmap_handle );
        
        var image_bytes : lime.utils.ByteArray = cast nme_bitmap_data_get_pixels( nme_bitmap_handle, {x:0, y:0, width:_width, height:_height } );
        // #end
        
        texture = GL.createTexture();

            //if no problems
        id = _asset_name;
        width = Std.int(_width);
        height = Std.int(_height);            

        data = new UInt8Array( image_bytes.getData() );
        var image_length = width * height;

            //ARGB to RGBA cos format 
            //todo : do inside nme side.
        for(i in 0 ... image_length) {

            var a = data[i*4+0];
            var r = data[i*4+1];
            var g = data[i*4+2];
            var b = data[i*4+3];

            //rgba would then be

            data[i*4+0] = r;
            data[i*4+1] = g;
            data[i*4+2] = b;
            data[i*4+3] = a;
        }

            //Now we can bind it
        bind();
            //And send GL the data
        GL.texImage2D(GL.TEXTURE_2D, 0, GL.RGBA, width, height, 0, GL.RGBA, GL.UNSIGNED_BYTE, data );

            //Set the properties<commit_msg>Fixing texture issues hopefully<commit_after>package phoenix;

import phoenix.Resource;

import lime.gl.GL;
import lime.gl.GLTexture;

import lime.utils.UInt8Array;

import lime.utils.Libs;
import lime.utils.ArrayBuffer;

enum FilterType {
    nearest;
    linear;    
}
enum ClampType {
    edge;
    repeat;    
    mirror;    
}

class Rectangle {
    public var x:Float;
    public var y:Float;
    public var width:Float;
    public var height:Float;
    public function new(_x,_y,_w,_h) {
        x = _x; 
        y = _y; 
        width = _w; 
        height = _h; 
    }
}

class Texture extends Resource {

    public var texture : GLTexture;
    public var data : UInt8Array;

    public var width : Int = -1;
    public var height : Int = -1;   
    public var loaded : Bool = false;
    public var onload : Texture -> Void;

    public function new( _manager : ResourceManager, _size : Dynamic = null ) {
        
        super( _manager, ResourceType.texture );

        id = 'Untitled';
        build( _size, null );
    }

    public function build(_size : Dynamic, _color: Dynamic) {

        if(_size == null) _size = {x:0, y:0};
        if(_color == null) _color = {r:1,g:1,b:1,a:1};
        if(_size.x > 0 && _size.y > 0) {

            width = Std.int(_size.x);
            height = Std.int(_size.y);

                //clear up old data in case
            data = null;
                //create a new set of pixels data
            data = new UInt8Array( cast new ArrayBuffer(  ) ); //width * height * 4
                //fill it up!
            for(x in 0 ... width) {
                for(y in 0 ... height) {
                    setPixel({x:x, y:y}, _color);
                }
            }

            texture = GL.createTexture();
        }
    }

    public function create_from_bytes_html(_asset_name:String, _asset_bytes, _width, _height ) {

        var max_size = GL.getParameter(GL.MAX_TEXTURE_SIZE);

        texture = GL.createTexture();

            //if no problems
        id = _asset_name;
        width = Std.int(_width);
        height = Std.int(_height);       

        if(_width > max_size) throw "texture bigger than MAX_TEXTURE_SIZE (" + max_size + ") " + _asset_name;
        if(_height > max_size) throw "texture bigger than MAX_TEXTURE_SIZE (" + max_size + ") " + _asset_name;
        
        //Now we can bind it
        bind();
            //And send GL the data
        GL.texImage2D(GL.TEXTURE_2D, 0, GL.RGBA, width, height, 0, GL.RGBA, GL.UNSIGNED_BYTE, cast _asset_bytes );

            //Set the properties
        set_filtering( FilterType.linear );
        set_clamp( ClampType.repeat );

        // image_bytes = null;
        // data = null; //todo - sven use lock/unlock
    }

        //Create from the bytes from Assets.getBytes() or other method
    public function create_from_bytes( _asset_name:String, _asset_bytes:haxe.io.Bytes ) {
        
        // trace(_asset_bytes);

        var max_size = GL.getParameter(GL.MAX_TEXTURE_SIZE);

        var nme_bitmap_handle = nme_bitmap_data_from_bytes(_asset_bytes, null);
        if(nme_bitmap_handle == null) throw "cannot create bitmap " + _asset_name;

        var _width = nme_bitmap_data_width( nme_bitmap_handle );
        var _height = nme_bitmap_data_height( nme_bitmap_handle );
        
        if(_width > max_size) throw "texture bigger than MAX_TEXTURE_SIZE (" + max_size + ") " + _asset_name;
        if(_height > max_size) throw "texture bigger than MAX_TEXTURE_SIZE (" + max_size + ") " + _asset_name;

        var image_bytes : lime.utils.ByteArray;

        try {
            image_bytes = cast nme_bitmap_data_get_pixels( nme_bitmap_handle, {x:0, y:0, width:_width, height:_height } );
        } catch(e:Dynamic) {
            trace(e);
            throw " fail!";
        }
        
        texture = GL.createTexture();

            //if no problems
        id = _asset_name;
        width = Std.int(_width);
        height = Std.int(_height);            

        data = new UInt8Array( cast image_bytes );
        var image_length = width * height;

            //ARGB to RGBA cos format 
            //todo : do inside nme side.
        for(i in 0 ... image_length) {

            var a = data[i*4+0];
            var r = data[i*4+1];
            var g = data[i*4+2];
            var b = data[i*4+3];

            //rgba would then be

            data[i*4+0] = r;
            data[i*4+1] = g;
            data[i*4+2] = b;
            data[i*4+3] = a;
        }

            //Now we can bind it
        bind();
            //And send GL the data
        GL.texImage2D(GL.TEXTURE_2D, 0, GL.RGBA, width, height, 0, GL.RGBA, GL.UNSIGNED_BYTE, data );

            //Set the properties
{'fraction_non_alphanumeric': 0.11142565960819839, 'fraction_numerical': 0.00996489638772506, 'mean_word_length': 1.7843631778058007, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '306327', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2788, 'n_words': 728}