Java Code Examples for java.nio.ShortBuffer#wrap()

The following examples show how to use java.nio.ShortBuffer#wrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GLHelper.java    From tilt-game-android with MIT License 6 votes vote down vote up
public static Buffer getPixels(final Bitmap pBitmap, final PixelFormat pPixelFormat, final ByteOrder pByteOrder) {
	final int[] pixelsARGB_8888 = GLHelper.getPixelsARGB_8888(pBitmap);

	switch (pPixelFormat) {
		case RGB_565:
			return ShortBuffer.wrap(GLHelper.convertARGB_8888toRGB_565(pixelsARGB_8888, pByteOrder)); // TODO Is ShortBuffer or IntBuffer faster?
		case RGBA_8888:
			// HACK =(
			final ByteOrder reverseByteOrder = (pByteOrder == ByteOrder.LITTLE_ENDIAN) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
			return IntBuffer.wrap(GLHelper.convertARGB_8888toRGBA_8888(pixelsARGB_8888, reverseByteOrder));
		case RGBA_4444:
			return ShortBuffer.wrap(GLHelper.convertARGB_8888toRGBA_4444(pixelsARGB_8888, pByteOrder)); // TODO Is ShortBuffer or IntBuffer faster?
		case A_8:
			return ByteBuffer.wrap(GLHelper.convertARGB_8888toA_8(pixelsARGB_8888));
		default:
			throw new IllegalArgumentException("Unexpected " + PixelFormat.class.getSimpleName() + ": '" + pPixelFormat + "'.");
	}
}
 
Example 2
Source File: GLHelper.java    From 30-android-libraries-in-30-days with Apache License 2.0 6 votes vote down vote up
public static Buffer getPixels(final Bitmap pBitmap, final PixelFormat pPixelFormat, final ByteOrder pByteOrder) {
	final int[] pixelsARGB_8888 = GLHelper.getPixelsARGB_8888(pBitmap);

	switch(pPixelFormat) {
		case RGB_565:
			return ShortBuffer.wrap(GLHelper.convertARGB_8888toRGB_565(pixelsARGB_8888, pByteOrder)); // TODO Is ShortBuffer or IntBuffer faster?
		case RGBA_8888:
			// HACK =(
			final ByteOrder reverseByteOrder = (pByteOrder == ByteOrder.LITTLE_ENDIAN) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
			return IntBuffer.wrap(GLHelper.convertARGB_8888toRGBA_8888(pixelsARGB_8888, reverseByteOrder));
		case RGBA_4444:
			return ShortBuffer.wrap(GLHelper.convertARGB_8888toRGBA_4444(pixelsARGB_8888, pByteOrder)); // TODO Is ShortBuffer or IntBuffer faster?
		case A_8:
			return ByteBuffer.wrap(GLHelper.convertARGB_8888toA_8(pixelsARGB_8888));
		default:
			throw new IllegalArgumentException("Unexpected " + PixelFormat.class.getSimpleName() + ": '" + pPixelFormat + "'.");
	}
}
 
Example 3
Source File: DataBuffers.java    From java with Apache License 2.0 5 votes vote down vote up
/**
 * Create a buffer from an array of shorts into a data buffer.
 *
 * @param array array of shorts
 * @param readOnly true if the buffer created must be read-only
 * @param makeCopy true if the array must be copied, false will wrap the provided array
 * @return a new buffer
 */
public static ShortDataBuffer of(short[] array, boolean readOnly, boolean makeCopy) {
  short[] bufferArray = makeCopy ? Arrays.copyOf(array, array.length) : array;
  if (RawDataBufferFactory.canBeUsed()) {
    return RawDataBufferFactory.create(bufferArray, readOnly);
  }
  ShortBuffer buf = ShortBuffer.wrap(bufferArray);
  return NioDataBufferFactory.create(readOnly ? buf.asReadOnlyBuffer() : buf);
}
 
Example 4
Source File: NioBufferRefConverterTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void methodOfNioBufferWithCovariantTypes_afterDesugarInvocationOfShortBufferMethod(
    @RuntimeMethodHandle(
            className = "NioBufferInvocations",
            memberName = "getShortBufferPosition")
        MethodHandle after)
    throws Throwable {
  ShortBuffer buffer = ShortBuffer.wrap(new short[] {10, 20, 30});
  int expectedPos = 2;

  ShortBuffer result = (ShortBuffer) after.invoke(buffer, expectedPos);
  assertThat(result.position()).isEqualTo(expectedPos);
}
 
Example 5
Source File: TriangleBatch.java    From constellation with Apache License 2.0 4 votes vote down vote up
/**
 * We've finished providing data; compact the data, map the buffers, and set
 * up the vertex array.
 * <p>
 * You should really save the results of the indexing for future use if the
 * model data is static (doesn't change).
 *
 * @param gl the current OpenGL context.
 */
public void end(final GL3 gl) {
    // Create the master vertex array object.
    gl.glGenVertexArrays(1, vertexArrayBufferObject, 0);
    gl.glBindVertexArray(vertexArrayBufferObject[0]);

    // Create the buffer objects.
    gl.glGenBuffers(4, bufferObjects, 0);

    // Copy data to video memory.
    FloatBuffer buf;

    // Vertex data.
    buf = FloatBuffer.wrap(GLTools.toFloatArray(allVerts));
    gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
    gl.glEnableVertexAttribArray(ShaderManager.ATTRIBUTE_VERTEX);
    gl.glBufferData(GL3.GL_ARRAY_BUFFER, GLBuffers.SIZEOF_FLOAT * 3 * nNumVerts, buf, GL3.GL_STATIC_DRAW);
    gl.glVertexAttribPointer(ShaderManager.ATTRIBUTE_VERTEX, 3, GL3.GL_FLOAT, false, 0, 0);

    // Normal data.
    buf = FloatBuffer.wrap(GLTools.toFloatArray(allNorms));
    gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, bufferObjects[NORMAL_DATA]);
    gl.glEnableVertexAttribArray(ShaderManager.ATTRIBUTE_NORMAL);
    gl.glBufferData(GL3.GL_ARRAY_BUFFER, GLBuffers.SIZEOF_FLOAT * 3 * nNumVerts, buf, GL3.GL_STATIC_DRAW);
    gl.glVertexAttribPointer(ShaderManager.ATTRIBUTE_NORMAL, 3, GL3.GL_FLOAT, false, 0, 0);

    // Texture coordinates.
    buf = FloatBuffer.wrap(GLTools.toFloatArray(allTexCoords));
    gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, bufferObjects[TEXTURE_DATA]);
    gl.glEnableVertexAttribArray(ShaderManager.ATTRIBUTE_TEXTURE0);
    gl.glBufferData(GL3.GL_ARRAY_BUFFER, GLBuffers.SIZEOF_FLOAT * nNumVerts * 2, buf, GL3.GL_STATIC_DRAW);
    gl.glVertexAttribPointer(ShaderManager.ATTRIBUTE_TEXTURE0, 2, GL3.GL_FLOAT, false, 0, 0);

    // Indexes.
    ShortBuffer shortBuf = ShortBuffer.wrap(allIndexes);
    gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, bufferObjects[INDEX_DATA]);
    gl.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, GLBuffers.SIZEOF_SHORT * nNumIndexes, shortBuf, GL3.GL_STATIC_DRAW);

    // Done
    gl.glBindVertexArray(0);

    // Free older, larger arrays.
    // Reassign pointers so they are marked as unused.
    allIndexes = null;
    allVerts = null;
    allNorms = null;
    allTexCoords = null;
}
 
Example 6
Source File: UnsafeMemoryHandle.java    From java with Apache License 2.0 4 votes vote down vote up
ShortBuffer toArrayShortBuffer() {
  return ShortBuffer.wrap((short[])object, (int)((byteOffset - UnsafeReference.UNSAFE.arrayBaseOffset(short[].class)) / scale), (int)size);
}
 
Example 7
Source File: Optimizer.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
protected final static ModelInfo optimize(/*String tex_name, */int num_vertices, float[] vertices, float[] normals, float[] colors, float[] uvs, byte[][] skin_names, float[][] skin_weights) {
		short[] indices = new short[num_vertices];
		float[] r_vertices = new float[vertices.length];
		float[] r_colors = new float[colors.length];
		float[] r_normals = new float[normals.length];
		float[] r_uvs = new float[uvs.length];
		byte[][] r_skin_names = new byte[skin_names.length][];
		float[][] r_skin_weights = new float[skin_weights.length][];

		int index = 0;

		for (int i = 0; i < num_vertices; i++) {
			int j;
			for (j = 0; j < index; j++) {
				boolean equals = floatsEquals(i, j, 3, vertices, r_vertices) &&
								 floatsEquals(i, j, 3, normals, r_normals) &&
								 floatsEquals(i, j, 3, colors, r_colors) &&
								 floatsEquals(i, j, 2, uvs, r_uvs) &&
								 floatArrayEquals(i, j, skin_weights, r_skin_weights) &&
								 byteArrayEquals(i, j, skin_names, r_skin_names);
				if (equals)
					break;
			}
			if (j == index) {
				copyFloats(i, index, 3, vertices, r_vertices);
				copyFloats(i, index, 3, normals, r_normals);
				copyFloats(i, index, 3, colors, r_colors);
				copyFloats(i, index, 2, uvs, r_uvs);
				copyObjects(i, index, 1, skin_names, r_skin_names);
				copyObjects(i, index, 1, skin_weights, r_skin_weights);
				indices[i] = (short)index;
				index++;
			} else {
				indices[i] = (short)j;
			}
		}
		r_vertices = stripArray(index*3, r_vertices);
		r_normals = stripArray(index*3, r_normals);
		r_colors = stripArray(index*3, r_colors);
		r_uvs = stripArray(index*2, r_uvs);
		r_skin_names = stripArray(index, r_skin_names);
		r_skin_weights = stripArray(index, r_skin_weights);
		ShortBuffer index_buffer = ShortBuffer.wrap(indices);
		IndexListOptimizer.optimize(index_buffer);
//		System.out.println("resulting vertices = " + index);
		return new ModelInfo(/*tex_name,*/ indices, r_vertices, r_normals, r_colors, r_uvs, r_skin_names, r_skin_weights);
	}
 
Example 8
Source File: Optimizer.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
protected final static ModelInfo optimize(/*String tex_name, */int num_vertices, float[] vertices, float[] normals, float[] colors, float[] uvs, float[] uvs2, byte[][] skin_names, float[][] skin_weights) {
		short[] indices = new short[num_vertices];
		float[] r_vertices = new float[vertices.length];
		float[] r_colors = new float[colors.length];
		float[] r_normals = new float[normals.length];
		float[] r_uvs = new float[uvs.length];
		float[] r_uvs2 = new float[uvs2.length];
		byte[][] r_skin_names = new byte[skin_names.length][];
		float[][] r_skin_weights = new float[skin_weights.length][];

		int index = 0;

		for (int i = 0; i < num_vertices; i++) {
			int j;
			for (j = 0; j < index; j++) {
				boolean equals = floatsEquals(i, j, 3, vertices, r_vertices) &&
								 floatsEquals(i, j, 3, normals, r_normals) &&
								 floatsEquals(i, j, 3, colors, r_colors) &&
								 floatsEquals(i, j, 2, uvs, r_uvs) &&
								 floatsEquals(i, j, 2, uvs2, r_uvs2) &&
								 floatArrayEquals(i, j, skin_weights, r_skin_weights) &&
								 byteArrayEquals(i, j, skin_names, r_skin_names);
				if (equals)
					break;
			}
			if (j == index) {
				copyFloats(i, index, 3, vertices, r_vertices);
				copyFloats(i, index, 3, normals, r_normals);
				copyFloats(i, index, 3, colors, r_colors);
				copyFloats(i, index, 2, uvs, r_uvs);
				copyFloats(i, index, 2, uvs2, r_uvs2);
				copyObjects(i, index, 1, skin_names, r_skin_names);
				copyObjects(i, index, 1, skin_weights, r_skin_weights);
				indices[i] = (short)index;
				index++;
			} else {
				indices[i] = (short)j;
			}
		}
		r_vertices = stripArray(index*3, r_vertices);
		r_normals = stripArray(index*3, r_normals);
		r_colors = stripArray(index*3, r_colors);
		r_uvs = stripArray(index*2, r_uvs);
		r_uvs2 = stripArray(index*2, r_uvs2);
		r_skin_names = stripArray(index, r_skin_names);
		r_skin_weights = stripArray(index, r_skin_weights);
//		System.out.println("resulting vertices = " + index);

		boolean using_texture_coords2 = false;
		for (int i = 0; i < r_uvs2.length; i++)
			if (r_uvs2[i] != 0f)
				using_texture_coords2 = true;
		if (!using_texture_coords2)
			r_uvs2 = null;
		ShortBuffer index_buffer = ShortBuffer.wrap(indices);
		IndexListOptimizer.optimize(index_buffer);
		return new ModelInfo(/*tex_name,*/ indices, r_vertices, r_normals, r_colors, r_uvs, r_uvs2, r_skin_names, r_skin_weights);
	}
 
Example 9
Source File: OldAndroidNIOTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testNIO_short_array() throws Exception {
    // Test short array-based buffer
    short[] shortArray = new short[8];
    ShortBuffer sb = ShortBuffer.wrap(shortArray);
    shortBufferTest(sb);
}
 
Example 10
Source File: SampleData.java    From red5-hls-plugin with Apache License 2.0 4 votes vote down vote up
private SampleData(short[] buffer) {
	this.id = index.getAndIncrement();
	this.buffer = ShortBuffer.wrap(buffer);
	this.pts = Global.NO_PTS;
}
 
Example 11
Source File: SampleData.java    From red5-hls-plugin with Apache License 2.0 4 votes vote down vote up
private SampleData(short[] buffer, long pts) {
	this.id = index.getAndIncrement();
	this.buffer = ShortBuffer.wrap(buffer);
	this.pts = pts;
}
 
Example 12
Source File: SingleImage.java    From settlers-remake with MIT License 2 votes vote down vote up
/**
 * Creates a new image by linking this images data to the data of the provider.
 *
 * @param metadata
 * 		The mata data to use.
 * @param data
 * 		The data to use.
 */
public SingleImage(ImageMetadata metadata, short[] data, String name) {
	this(ShortBuffer.wrap(data), metadata.width, metadata.height, metadata.offsetX, metadata.offsetY, name);
}