<commit_before>package com.nucleus.texturing;

import java.io.FileNotFoundException;
import java.io.InputStreamReader;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.nucleus.SimpleLogger;
import com.nucleus.io.ExternalReference;
import com.nucleus.io.gson.TextureDeserializer;
import com.nucleus.opengl.GLES20Wrapper;
import com.nucleus.opengl.GLESWrapper.GLES20;
import com.nucleus.opengl.GLException;
import com.nucleus.resource.ResourceBias.RESOLUTION;
import com.nucleus.texturing.Texture2D.Format;
import com.nucleus.texturing.Texture2D.Type;

/**
 * Used to create texture objects.
 * Texture objects created using the same image (external reference) will share texture data, through the texture object
 * id.
 * 
 * @author <NAME>
 *
 */
public class TextureFactory {

    /**
     * Creates a texture for the specified image,
     * The texture will be uploaded to GL using the specified texture object name, if several mip-map levels are
     * supplied they will be used.
     * If the device current resolution is lower than the texture target resolution then a lower mip-map level is used.
     * 
     * @param gles The gles wrapper
     * @param imageFactory factor for image creation
     * @param source The texture source, the new texture will be a copy of this with the texture image loaded into GL.
     * @return A new texture object containing the texture image.
     */
    public static Texture2D createTexture(GLES20Wrapper gles, ImageFactory imageFactory, Texture2D source) {
        Texture2D texture = createTexture(source);
        prepareTexture(gles, imageFactory, texture);
        return texture;
    }

    /**
     * Creates a texture for the specified image, Format will be RGBA and type UNSIGNED_BYTE
     * The texture will be uploaded to GL using the specified texture object name, if several mip-map levels are
     * supplied they will be used.
     * If the device current resolution is lower than the texture target resolution then a lower mip-map level is used.
     * 
     * @param gles
     * @param imageFactory
     * @param id The id of the texture
     * @param externalReference
     * @param resolution
     * @param parameter
     * @param mipmap
     * @return A new texture object containing the texture image.
     */
    public static Texture2D createTexture(GLES20Wrapper gles, ImageFactory imageFactory, String id,
            ExternalReference externalReference, RESOLUTION resolution, TextureParameter parameter, int mipmap) {
        Texture2D source = new Texture2D(id, externalReference, resolution, parameter, mipmap, Format.RGBA,
                Type.UNSIGNED_BYTE);
        return createTexture(gles, imageFactory, source);
    }

    /**
     * Creates a copy of the texture contents (image)
     * 
     * @param source The source texture, this holds the texture data
     * @return Texture with same contents as the source
     */
    public static Texture2D createTexture(Texture2D source) {
        switch (source.textureType) {
        case Texture2D:
            return new Texture2D(source);
        case TiledTexture2D:
            return new TiledTexture2D((TiledTexture2D) source);
        case UVTexture2D:
            return new UVTexture2D((UVTexture2D) source);
        default:
            throw new IllegalArgumentException();
        }
    }

    public static Texture2D createTexture(ExternalReference ref) throws FileNotFoundException {
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Texture2D.class, new TextureDeserializer());
        Gson gson = builder.create();
        InputStreamReader reader = new InputStreamReader(ref.getAsStream());
        try {
            return gson.fromJson(reader, Texture2D.class);
        } catch (Exception e) {
            throw new RuntimeException("Exception reading " + ref.getSource(), e);
        }
    }

    /**
     * Creates a new empty texture object of the specified type
     * 
     * @param type The texture type
     * @return The texture
     */
    public static Texture2D createTexture(TextureType type) {
        switch (type) {
        case Texture2D:
            return new Texture2D();
        case TiledTexture2D:
            return new TiledTexture2D();
        case UVTexture2D:
            return new UVTexture2D();
        default:
            throw new IllegalArgumentException();
        }
    }

    /**
     * Creates a new TiledTexture
     * 
     * @param id
     * @param externalReference
     * @param targetResolution
     * @param params
     * @param mipmap
     * @param size
     * @param format
     * @param type
     * @return
     */
    public static TiledTexture2D createTiledTexture(String id, ExternalReference externalReference,
            RESOLUTION targetResolution, TextureParameter params, int mipmap, int[] size, Format format, Type type) {
        TiledTexture2D tex = new TiledTexture2D(id, externalReference, targetResolution, params, mipmap, size, format,
                type);
        return tex;
    }

    /**
     * Internal method to create a texture based on the texture setup source, the texture will be set with data from
     * texture setup and uploaded to GL.
     * 
     * @param gles
     * @param texture The texture
     * @param imageFactory The imagefactory to use for image creation
     */
    private static void prepareTexture(GLES20Wrapper gles, ImageFactory imageFactory, Texture2D texture) {
        int[] textures = new int[1];
        gles.glGenTextures(1, textures, 0);

        int textureID = textures[0];
        Image[] textureImg = TextureUtils
                .loadTextureMIPMAP(imageFactory, texture);
        try {
            TextureUtils.uploadTextures(gles, GLES20.GL_TEXTURE0, texture, textureID, textureImg);
            // TODO Do not need to keep images after texture is uploaded
            texture.setup(textureID, textureImg);
            SimpleLogger.d(TextureFactory.class, "Uploaded texture " + texture.toString());
        } catch (GLException e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * Copies the texture data, ie the texture name and image data to the destination.
     * 
     * @param source
     * @param destination
     */
    public static void copyTextureData(Texture2D source, Texture2D destination) {
        destination.setup(source.getName(), source.images);
    }

}
<commit_msg>Better error handling if .tex file not found<commit_after>package com.nucleus.texturing;

import java.io.FileNotFoundException;
import java.io.InputStreamReader;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.nucleus.SimpleLogger;
import com.nucleus.io.ExternalReference;
import com.nucleus.io.gson.TextureDeserializer;
import com.nucleus.opengl.GLES20Wrapper;
import com.nucleus.opengl.GLESWrapper.GLES20;
import com.nucleus.opengl.GLException;
import com.nucleus.resource.ResourceBias.RESOLUTION;
import com.nucleus.texturing.Texture2D.Format;
import com.nucleus.texturing.Texture2D.Type;

/**
 * Used to create texture objects.
 * Texture objects created using the same image (external reference) will share texture data, through the texture object
 * id.
 * 
 * @author <NAME>
 *
 */
public class TextureFactory {

    /**
     * Creates a texture for the specified image,
     * The texture will be uploaded to GL using the specified texture object name, if several mip-map levels are
     * supplied they will be used.
     * If the device current resolution is lower than the texture target resolution then a lower mip-map level is used.
     * 
     * @param gles The gles wrapper
     * @param imageFactory factor for image creation
     * @param source The texture source, the new texture will be a copy of this with the texture image loaded into GL.
     * @return A new texture object containing the texture image.
     */
    public static Texture2D createTexture(GLES20Wrapper gles, ImageFactory imageFactory, Texture2D source) {
        Texture2D texture = createTexture(source);
        prepareTexture(gles, imageFactory, texture);
        return texture;
    }

    /**
     * Creates a texture for the specified image, Format will be RGBA and type UNSIGNED_BYTE
     * The texture will be uploaded to GL using the specified texture object name, if several mip-map levels are
     * supplied they will be used.
     * If the device current resolution is lower than the texture target resolution then a lower mip-map level is used.
     * 
     * @param gles
     * @param imageFactory
     * @param id The id of the texture
     * @param externalReference
     * @param resolution
     * @param parameter
     * @param mipmap
     * @return A new texture object containing the texture image.
     */
    public static Texture2D createTexture(GLES20Wrapper gles, ImageFactory imageFactory, String id,
            ExternalReference externalReference, RESOLUTION resolution, TextureParameter parameter, int mipmap) {
        Texture2D source = new Texture2D(id, externalReference, resolution, parameter, mipmap, Format.RGBA,
                Type.UNSIGNED_BYTE);
        return createTexture(gles, imageFactory, source);
    }

    /**
     * Creates a copy of the texture contents (image)
     * 
     * @param source The source texture, this holds the texture data
     * @return Texture with same contents as the source
     */
    public static Texture2D createTexture(Texture2D source) {
        switch (source.textureType) {
        case Texture2D:
            return new Texture2D(source);
        case TiledTexture2D:
            return new TiledTexture2D((TiledTexture2D) source);
        case UVTexture2D:
            return new UVTexture2D((UVTexture2D) source);
        default:
            throw new IllegalArgumentException();
        }
    }

    public static Texture2D createTexture(ExternalReference ref) throws FileNotFoundException {
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Texture2D.class, new TextureDeserializer());
        Gson gson = builder.create();
        try {
            InputStreamReader reader = new InputStreamReader(ref.getAsStream());
            return gson.fromJson(reader, Texture2D.class);
        } catch (Exception e) {
            throw new RuntimeException("Exception reading " + ref.getSource(), e);
        }
    }

    /**
     * Creates a new empty texture object of the specified type
     * 
     * @param type The texture type
     * @return The texture
     */
    public static Texture2D createTexture(TextureType type) {
        switch (type) {
        case Texture2D:
            return new Texture2D();
        case TiledTexture2D:
            return new TiledTexture2D();
        case UVTexture2D:
            return new UVTexture2D();
        default:
            throw new IllegalArgumentException();
        }
    }

    /**
     * Creates a new TiledTexture
     * 
     * @param id
     * @param externalReference
     * @param targetResolution
     * @param params
     * @param mipmap
     * @param size
     * @param format
     * @param type
     * @return
     */
    public static TiledTexture2D createTiledTexture(String id, ExternalReference externalReference,
            RESOLUTION targetResolution, TextureParameter params, int mipmap, int[] size, Format format, Type type) {
        TiledTexture2D tex = new TiledTexture2D(id, externalReference, targetResolution, params, mipmap, size, format,
                type);
        return tex;
    }

    /**
     * Internal method to create a texture based on the texture setup source, the texture will be set with data from
     * texture setup and uploaded to GL.
     * 
     * @param gles
     * @param texture The texture
     * @param imageFactory The imagefactory to use for image creation
     */
    private static void prepareTexture(GLES20Wrapper gles, ImageFactory imageFactory, Texture2D texture) {
        int[] textures = new int[1];
        gles.glGenTextures(1, textures, 0);

        int textureID = textures[0];
        Image[] textureImg = TextureUtils
                .loadTextureMIPMAP(imageFactory, texture);
        try {
            TextureUtils.uploadTextures(gles, GLES20.GL_TEXTURE0, texture, textureID, textureImg);
            // TODO Do not need to keep images after texture is uploaded
            texture.setup(textureID, textureImg);
            SimpleLogger.d(TextureFactory.class, "Uploaded texture " + texture.toString());
        } catch (GLException e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * Copies the texture data, ie the texture name and image data to the destination.
     * 
     * @param source
     * @param destination
     */
    public static void copyTextureData(Texture2D source, Texture2D destination) {
        destination.setup(source.getName(), source.images);
    }

}

{'fraction_non_alphanumeric': 0.07313675412119805, 'fraction_numerical': 0.007894125841653122, 'mean_word_length': 2.716422203048605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '682434', 'n_tokens_mistral': 3508, 'n_tokens_neox': 3378, 'n_words': 1175}