Helper function that writes an OpenGL ETC1Texture to an output stream formatted as a PKM file. - Android android.opengl

Android examples for android.opengl:OpenGL Texture

Description

Helper function that writes an OpenGL ETC1Texture to an output stream formatted as a PKM file.

Demo Code


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import nicastel.renderscripttexturecompressor.etc1.rs.ScriptC_etc1compressor;
import android.opengl.ETC1;
import android.opengl.GLES10;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;

public class Main{
    /**//  w ww  . java  2s  .c o m
     * Helper function that writes an ETC1Texture to an output stream formatted as a PKM file.
     * @param texture the input texture.
     * @param output the stream to write the formatted texture data to.
     * @throws IOException
     */
    public static void writeTexture(ETC1Texture texture, OutputStream output)
            throws IOException {
        ByteBuffer dataBuffer = texture.getData();
        dataBuffer.rewind();
        System.out.println(dataBuffer.remaining());
        int originalPosition = dataBuffer.position();
        try {
            int width = texture.getWidth();
            int height = texture.getHeight();
            ByteBuffer header = ByteBuffer.allocateDirect(
                    ETC1.ETC_PKM_HEADER_SIZE)
                    .order(ByteOrder.nativeOrder());
            ETC1.formatHeader(header, width, height);
            header.position(0);
            byte[] ioBuffer = new byte[4096];
            header.get(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE);
            output.write(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE);
            while (dataBuffer.remaining() > 0) {
                int chunkSize = Math.min(ioBuffer.length,
                        dataBuffer.remaining());
                dataBuffer.get(ioBuffer, 0, chunkSize);
                output.write(ioBuffer, 0, chunkSize);
            }
        } finally {
            dataBuffer.position(originalPosition);
        }
    }
}

Related Tutorials