<commit_before>package com.wordsaretoys.soar;

import java.nio.ByteBuffer;

import android.opengl.GLES20;


/**
 * represents a single texture object.
 * construct textures from surfaces.
 * 
 * @author chris
 *
 */
public class Texture {
	
	private int[] id = new int[1];

	public Texture() {
	}

	/**
	 * generates a texture from a surface object
	 * 
	 * surface SHOULD be normalized to (0..1) and 
	 * MUST have width & height as powers of two!
	 * 
	 * @param surf
	 * @return true if build suceeded
	 */
	public boolean build(Surface surf) {
		// create and populate a byte buffer
		int length = surf.length;
		double[] data = surf.data;
		ByteBuffer bb = ByteBuffer.allocateDirect(length);
		for (int i = 0; i < length; i++) {
			bb.put( (byte)(data[i] * 256) );
		}
		
		// build into a texture
		return build(GLES20.GL_LUMINANCE, bb, surf.width, surf.height);
	}
	
	/**
	 * build a buffer into a GL texture 
	 * @param format buffer data format (GL_LUMINANCE, GL_RGB, GL_RGBA)
	 * @param bb byte buffer
	 * @param width, height texture dimensions
	 * @return true if build succeeded
	 */
	public boolean build(int format, ByteBuffer bb, int width, int height) {
	
		// allocate a GL texture
		GLES20.glGenTextures(1, id, 0);

		// reposition buffer pointer
		bb.position(0);

		// copy texture data and generate mipmap
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, id[0]);
		GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, format, width, height, 0, format, GLES20.GL_UNSIGNED_BYTE, bb);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
		GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
		
		return (GLES20.glGetError() == 0);
	}
	
	/**
	 * bind the texture to a sampler and texture unit
	 * 
	 * used in conjunction with shader.activate()<commit_msg>Adds GL buffer disposal to build().<commit_after>package com.wordsaretoys.soar;

import java.nio.ByteBuffer;

import android.opengl.GLES20;


/**
 * represents a single texture object.
 * construct textures from surfaces.
 * 
 * @author chris
 *
 */
public class Texture {
	
	private int[] id = new int[1];

	public Texture() {
		id[0] = -1;
	}

	/**
	 * generates a texture from a surface object
	 * 
	 * surface SHOULD be normalized to (0..1) and 
	 * MUST have width & height as powers of two!
	 * 
	 * @param surf
	 * @return true if build suceeded
	 */
	public boolean build(Surface surf) {
		// create and populate a byte buffer
		int length = surf.length;
		double[] data = surf.data;
		ByteBuffer bb = ByteBuffer.allocateDirect(length);
		for (int i = 0; i < length; i++) {
			bb.put( (byte)(data[i] * 256) );
		}
		
		// build into a texture
		return build(GLES20.GL_LUMINANCE, bb, surf.width, surf.height);
	}
	
	/**
	 * build a buffer into a GL texture 
	 * @param format buffer data format (GL_LUMINANCE, GL_RGB, GL_RGBA)
	 * @param bb byte buffer
	 * @param width, height texture dimensions
	 * @return true if build succeeded
	 */
	public boolean build(int format, ByteBuffer bb, int width, int height) {
		// release any existing resources
		release();
		
		// allocate a GL texture
		GLES20.glGenTextures(1, id, 0);

		// reposition buffer pointer
		bb.position(0);

		// copy texture data and generate mipmap
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, id[0]);
		GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, format, width, height, 0, format, GLES20.GL_UNSIGNED_BYTE, bb);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
		GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
		
		return (GLES20.glGetError() == 0);
	}
	
	/**
	 * bind the texture to a sampler and texture unit
	 * 
	 * used in conjunction with shader.activate()
{'fraction_non_alphanumeric': 0.11158262606090864, 'fraction_numerical': 0.032451323015476784, 'mean_word_length': 3.7030516431924885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '1995731', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1375, 'n_words': 385}