Java Code Examples for java.nio.IntBuffer#get()

The following examples show how to use java.nio.IntBuffer#get() . 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: IntegerArrayIO.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public static int[] readArray(InputStream input) throws IOException {
    DataInputStream dataInput = new DataInputStream(input);
    int length = dataInput.readInt();

    ByteBuffer tmpBuffer = ByteBuffer.allocate(length * INT_BYTES);
    ReadableByteChannel channel = Channels.newChannel(dataInput);
    channel.read(tmpBuffer);

    tmpBuffer.rewind();
    IntBuffer intBuffer = tmpBuffer.asIntBuffer();

    int[] array = new int[length];
    intBuffer.get(array);

    return array;
}
 
Example 2
Source File: VorbisTrack.java    From ure with MIT License 6 votes vote down vote up
VorbisTrack(String filePath, AtomicInteger sampleIndex) {
    try {
        encodedAudio = ioResourceToByteBuffer(filePath, 256*1024);
    } catch (IOException e) { e.printStackTrace(); }

    try (MemoryStack stack = stackPush()) {
        IntBuffer error = stack.mallocInt(1);
        handle = stb_vorbis_open_memory(encodedAudio,error,null);
        if (handle == NULL)
            throw new RuntimeException("Failed to open OGG file.  Error: " + error.get(0));
        STBVorbisInfo info = STBVorbisInfo.mallocStack(stack);
        stb_vorbis_get_info(handle, info);
        this.channels = info.channels();
        this.sampleRate = info.sample_rate();
        log.debug("vorbis file detected " + Integer.toString(channels) + " channel " + Integer.toString(sampleRate) + " samplerate");
    }
    this.samplesLength = stb_vorbis_stream_length_in_samples(handle);
    this.samplesSec = stb_vorbis_stream_length_in_seconds(handle);
    this.sampleIndex = sampleIndex;
    sampleIndex.set(0);
}
 
Example 3
Source File: USpeaker.java    From ure with MIT License 6 votes vote down vote up
public void start() {
    if (!initialized) return;
    log.debug("buffering playback for bgm '" + filename + "' on deck " + deckID);
    sampleIndex = new AtomicInteger();
    track = new VorbisTrack(config.getResourcePath() + filename, sampleIndex);
    this.format = track.channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
    this.pcm = memAllocShort(BUFFER_SIZE);
    alcSetThreadContext(context);
    IntBuffer b = makePlaySource(0f);
    source = b.get(0);
    //alSourcei(source, AL_DIRECT_CHANNELS_SOFT, AL_TRUE);
    buffers = memAllocInt(2);
    alGenBuffers(buffers);
    for (int i=0;i<buffers.limit();i++) {
        if (stream(buffers.get(i))==0) {
            log.error("failed to buffer " + filename);
            return;
        }
    }
    alSourceQueueBuffers(source,buffers);
    alSourcePlay(source);
    log.info("playback started for bgm '" + filename + "' on deck " + deckID);
}
 
Example 4
Source File: GLRenderer.java    From HoloKilo with GNU General Public License v3.0 6 votes vote down vote up
static public int loadShader(int type, String shaderCode)
{
    int shader = GLES20.glCreateShader(type);

    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);
    IntBuffer compile = IntBuffer.allocate(1);
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compile);
    if (compile.get(0) == GLES20.GL_FALSE) {
        Log.e(Config.TAG, "Error:");
        Log.e(Config.TAG, shaderCode);
        Log.e(Config.TAG, "Fault:");
        printLog(shader);
        return 0;
    }

    return shader;
}
 
Example 5
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example 6
Source File: MonocleRobot.java    From Monocle with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void getScreenCapture(int x, int y, int width, int height, int[] data, boolean scaleToFit) {
    Application.checkEventThread();
    NativeScreen screen = NativePlatformFactory.getNativePlatform().getScreen();
    final int scrWidth = screen.getWidth();
    final int scrHeight = screen.getHeight();

    synchronized (NativeScreen.framebufferSwapLock) {
        IntBuffer buffer = screen.getScreenCapture().asIntBuffer();

        if (x == 0 && y == 0 && width == scrWidth && height == scrHeight) {
            // Easy case, the entire screen is being captured.
            System.arraycopy(buffer.array(), 0, data, 0, buffer.array().length);
            return;
        }

        int rowStop = Math.min(y + height, scrHeight);
        int colStop = Math.min(x + width, scrWidth);
        for (int row = y; row < rowStop; row++) {
            for (int col = x; col < colStop; col++) {
                data[(row - y) * (colStop - x) + (col - x)] = buffer.get(row * scrWidth + col);
            }
        }
    }
}
 
Example 7
Source File: Image.java    From JavaAV with GNU General Public License v2.0 6 votes vote down vote up
public static void copy(IntBuffer srcBuf, int srcStep, IntBuffer dstBuf, int dstStep) {
	int w = Math.min(srcStep, dstStep);
	int srcLine = srcBuf.position();
	int dstLine = dstBuf.position();

	while (srcLine < srcBuf.capacity() && dstLine < dstBuf.capacity()) {
		srcBuf.position(srcLine);
		dstBuf.position(dstLine);

		w = Math.min(Math.min(w, srcBuf.remaining()), dstBuf.remaining());

		for (int x = 0; x < w; x++) {
			int in = srcBuf.get();
			int out = in;
			dstBuf.put(out);
		}

		srcLine += srcStep;
		dstLine += dstStep;
	}
}
 
Example 8
Source File: Framebuffer.java    From Monocle with GNU General Public License v2.0 5 votes vote down vote up
void write(WritableByteChannel out) throws IOException {
    bb.clear();
    if (byteDepth == 4) {
        out.write(bb);
    } else if (byteDepth == 2) {
        if (lineByteBuffer == null) {
            lineByteBuffer = ByteBuffer.allocate(width * 2);
            lineByteBuffer.order(ByteOrder.nativeOrder());
            linePixelBuffer = lineByteBuffer.asShortBuffer();
        }
        IntBuffer srcPixels = bb.asIntBuffer();
        ShortBuffer shortBuffer = (ShortBuffer) linePixelBuffer;
        for (int i = 0; i < height; i++) {
            shortBuffer.clear();
            for (int j = 0; j < width; j++) {
                int pixel32 = srcPixels.get();
                int r = ((((pixel32 >> 19) & 31) * 539219) >> 8) & (31 << 11);
                int g = ((((pixel32 >> 10) & 63) * 265395) >> 13) & (63 << 5);
                int b = (((pixel32 >> 3) & 31) * 539219) >> 19;
                int pixel16 = r | g | b;
                shortBuffer.put((short) pixel16);
            }
            lineByteBuffer.clear();
            out.write(lineByteBuffer);
        }
    }
}
 
Example 9
Source File: DumpArrayColumnBinaryMaker.java    From multiple-dimension-spread with Apache License 2.0 5 votes vote down vote up
public ArrayCellManager( final Spread spread , final IntBuffer buffer ){
  int length = buffer.capacity();
  cellArray = new ICell[length];
  int currentIndex = 0;
  for( int i = 0 ; i < length ; i++ ){
    int arrayLength = buffer.get();
    if( arrayLength != 0 ){
      int start = currentIndex;
      int end = start + arrayLength;
      cellArray[i] = new ArrayCell( new SpreadArrayLink( spread , i , start , end ) );
      currentIndex += arrayLength;
    }
  }
}
 
Example 10
Source File: IntBitPacking.java    From metrics with Apache License 2.0 5 votes vote down vote up
protected void decompress(
        IntBuffer src,
        IntOutputStream dst,
        IntFilter filter)
{
    while (src.hasRemaining()) {
        int head = src.get();
        for (int i = (this.blockNum - 1) * 8; i >= 0; i -= 8) {
            int validBits = (int)((head >> i) & 0xff);
            unpack(src, dst, validBits, this.blockLen, filter);
        }
    }
    return;
}
 
Example 11
Source File: SoundPhysics.java    From Sound-Physics with GNU General Public License v3.0 5 votes vote down vote up
private static int alcGetInt(ALCdevice device, int pname)
{
	ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4);
	IntBuffer intBuffer = byteBuffer.asIntBuffer();
	
	//ALC10.alcGetInteger(device, pname, intBuffer);
	
	return intBuffer.get(0);
}
 
Example 12
Source File: NDArray.java    From djl with Apache License 2.0 5 votes vote down vote up
/**
 * Converts this {@code NDArray} to an int array.
 *
 * @return an int array
 * @throws IllegalStateException when {@link DataType} of this {@code NDArray} mismatches
 */
default int[] toIntArray() {
    if (getDataType() != DataType.INT32) {
        throw new IllegalStateException(
                "DataType mismatch, Required int" + " Actual " + getDataType());
    }
    IntBuffer ib = toByteBuffer().asIntBuffer();
    int[] ret = new int[ib.remaining()];
    ib.get(ret);
    return ret;
}
 
Example 13
Source File: JnaUtils.java    From djl with Apache License 2.0 4 votes vote down vote up
public static int getGpuCount() {
    IntBuffer count = IntBuffer.allocate(1);
    checkCall(LIB.MXGetGPUCount(count));

    return count.get();
}
 
Example 14
Source File: TinkerBoardMmapGpio.java    From diozero with MIT License 4 votes vote down vote up
@Override
public void setMode(int gpio, DeviceMode mode) {
	IntBuffer mux_int_buffer = (gpio < 24) ? pmuIntBuffer : grfIntBuffer;
	int iomux_offset = getIoMuxOffsetForGpio(gpio);
	if (iomux_offset == UNKNOWN) {
		Logger.warn("Unknown IOMUX offset for GPIO #{}", Integer.valueOf(gpio));
		return;
	}
	// Configure mode
	int config_val = mux_int_buffer.get(iomux_offset);
	switch (mode) {
	case DIGITAL_INPUT:
	case DIGITAL_OUTPUT:
		if (gpio == 238 || gpio == 239) {
			config_val = (config_val | (0x0f<<(16+(gpio%8-4)*4))) & (~(0x0f<<((gpio%8-4)*4)));
		} else {
			config_val = (config_val | (0x03<<((gpio%8)*2+16))) & (~(0x03<<((gpio%8)*2)));
		}
		mux_int_buffer.put(iomux_offset, config_val);
		
		// Set digital direction
		int bank;
		int shift;
		if (gpio < 24) {
			bank = gpio / 32;
			shift = gpio % 32;
		} else {
			bank = (gpio + 8) / 32;
			shift = (gpio + 8) % 32;
		}
		int gpio_val = gpioBanks[bank].gpioIntBuffer.get(GPIO_SWPORTA_DDR_INT_OFFSET);
		if (mode == DeviceMode.DIGITAL_INPUT) {
			gpio_val &= ~(1<<shift);
		} else {
			gpio_val |= (1<<shift);
		}
		gpioBanks[bank].gpioIntBuffer.put(GPIO_SWPORTA_DDR_INT_OFFSET, gpio_val);
		break;
	case PWM_OUTPUT:
		Logger.warn("Mode {} not yet implemented for GPIO #{}", mode, Integer.valueOf(gpio));
		return;
	default:
		Logger.warn("Invalid mode ({}) for GPIO #{}", mode, Integer.valueOf(gpio));
		return;
	}
}
 
Example 15
Source File: IntBitPackingUnpacks.java    From metrics with Apache License 2.0 4 votes vote down vote up
public static void unpack17(
        int[] buf,
        IntBuffer src,
        IntOutputStream dst,
        IntFilter filter)
{
    final int m = 0x1ffff;
    int n, c;

    n = src.get();
    buf[ 0] = filter.filterInt(n >>> 15 & m);
    c = n <<  2 & m;
    n = src.get();
    buf[ 1] = filter.filterInt(c | n >>> 30);
    buf[ 2] = filter.filterInt(n >>> 13 & m);
    c = n <<  4 & m;
    n = src.get();
    buf[ 3] = filter.filterInt(c | n >>> 28);
    buf[ 4] = filter.filterInt(n >>> 11 & m);
    c = n <<  6 & m;
    n = src.get();
    buf[ 5] = filter.filterInt(c | n >>> 26);
    buf[ 6] = filter.filterInt(n >>>  9 & m);
    c = n <<  8 & m;
    n = src.get();
    buf[ 7] = filter.filterInt(c | n >>> 24);
    buf[ 8] = filter.filterInt(n >>>  7 & m);
    c = n << 10 & m;
    n = src.get();
    buf[ 9] = filter.filterInt(c | n >>> 22);
    buf[10] = filter.filterInt(n >>>  5 & m);
    c = n << 12 & m;
    n = src.get();
    buf[11] = filter.filterInt(c | n >>> 20);
    buf[12] = filter.filterInt(n >>>  3 & m);
    c = n << 14 & m;
    n = src.get();
    buf[13] = filter.filterInt(c | n >>> 18);
    buf[14] = filter.filterInt(n >>>  1 & m);
    c = n << 16 & m;
    n = src.get();
    buf[15] = filter.filterInt(c | n >>> 16);
    c = n <<  1 & m;
    n = src.get();
    buf[16] = filter.filterInt(c | n >>> 31);
    buf[17] = filter.filterInt(n >>> 14 & m);
    c = n <<  3 & m;
    n = src.get();
    buf[18] = filter.filterInt(c | n >>> 29);
    buf[19] = filter.filterInt(n >>> 12 & m);
    c = n <<  5 & m;
    n = src.get();
    buf[20] = filter.filterInt(c | n >>> 27);
    buf[21] = filter.filterInt(n >>> 10 & m);
    c = n <<  7 & m;
    n = src.get();
    buf[22] = filter.filterInt(c | n >>> 25);
    buf[23] = filter.filterInt(n >>>  8 & m);
    c = n <<  9 & m;
    n = src.get();
    buf[24] = filter.filterInt(c | n >>> 23);
    buf[25] = filter.filterInt(n >>>  6 & m);
    c = n << 11 & m;
    n = src.get();
    buf[26] = filter.filterInt(c | n >>> 21);
    buf[27] = filter.filterInt(n >>>  4 & m);
    c = n << 13 & m;
    n = src.get();
    buf[28] = filter.filterInt(c | n >>> 19);
    buf[29] = filter.filterInt(n >>>  2 & m);
    c = n << 15 & m;
    n = src.get();
    buf[30] = filter.filterInt(c | n >>> 17);
    buf[31] = filter.filterInt(n >>>  0 & m);

    dst.write(buf, 0, 32);
}
 
Example 16
Source File: IntBitPackingUnpacks.java    From metrics with Apache License 2.0 4 votes vote down vote up
public static void unpack25(
        int[] buf,
        IntBuffer src,
        IntOutputStream dst,
        IntFilter filter)
{
    final int m = 0x1ffffff;
    int n, c;

    n = src.get();
    buf[ 0] = filter.filterInt(n >>>  7 & m);
    c = n << 18 & m;
    n = src.get();
    buf[ 1] = filter.filterInt(c | n >>> 14);
    c = n << 11 & m;
    n = src.get();
    buf[ 2] = filter.filterInt(c | n >>> 21);
    c = n <<  4 & m;
    n = src.get();
    buf[ 3] = filter.filterInt(c | n >>> 28);
    buf[ 4] = filter.filterInt(n >>>  3 & m);
    c = n << 22 & m;
    n = src.get();
    buf[ 5] = filter.filterInt(c | n >>> 10);
    c = n << 15 & m;
    n = src.get();
    buf[ 6] = filter.filterInt(c | n >>> 17);
    c = n <<  8 & m;
    n = src.get();
    buf[ 7] = filter.filterInt(c | n >>> 24);
    c = n <<  1 & m;
    n = src.get();
    buf[ 8] = filter.filterInt(c | n >>> 31);
    buf[ 9] = filter.filterInt(n >>>  6 & m);
    c = n << 19 & m;
    n = src.get();
    buf[10] = filter.filterInt(c | n >>> 13);
    c = n << 12 & m;
    n = src.get();
    buf[11] = filter.filterInt(c | n >>> 20);
    c = n <<  5 & m;
    n = src.get();
    buf[12] = filter.filterInt(c | n >>> 27);
    buf[13] = filter.filterInt(n >>>  2 & m);
    c = n << 23 & m;
    n = src.get();
    buf[14] = filter.filterInt(c | n >>>  9);
    c = n << 16 & m;
    n = src.get();
    buf[15] = filter.filterInt(c | n >>> 16);
    c = n <<  9 & m;
    n = src.get();
    buf[16] = filter.filterInt(c | n >>> 23);
    c = n <<  2 & m;
    n = src.get();
    buf[17] = filter.filterInt(c | n >>> 30);
    buf[18] = filter.filterInt(n >>>  5 & m);
    c = n << 20 & m;
    n = src.get();
    buf[19] = filter.filterInt(c | n >>> 12);
    c = n << 13 & m;
    n = src.get();
    buf[20] = filter.filterInt(c | n >>> 19);
    c = n <<  6 & m;
    n = src.get();
    buf[21] = filter.filterInt(c | n >>> 26);
    buf[22] = filter.filterInt(n >>>  1 & m);
    c = n << 24 & m;
    n = src.get();
    buf[23] = filter.filterInt(c | n >>>  8);
    c = n << 17 & m;
    n = src.get();
    buf[24] = filter.filterInt(c | n >>> 15);
    c = n << 10 & m;
    n = src.get();
    buf[25] = filter.filterInt(c | n >>> 22);
    c = n <<  3 & m;
    n = src.get();
    buf[26] = filter.filterInt(c | n >>> 29);
    buf[27] = filter.filterInt(n >>>  4 & m);
    c = n << 21 & m;
    n = src.get();
    buf[28] = filter.filterInt(c | n >>> 11);
    c = n << 14 & m;
    n = src.get();
    buf[29] = filter.filterInt(c | n >>> 18);
    c = n <<  7 & m;
    n = src.get();
    buf[30] = filter.filterInt(c | n >>> 25);
    buf[31] = filter.filterInt(n >>>  0 & m);

    dst.write(buf, 0, 32);
}
 
Example 17
Source File: CMap.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
CMapFormat12(ByteBuffer buffer, int offset, char[] xlat) {
    if (xlat != null) {
        throw new RuntimeException("xlat array for cmap fmt=12");
    }

    numGroups = buffer.getInt(offset+12);
    startCharCode = new long[numGroups];
    endCharCode = new long[numGroups];
    startGlyphID = new int[numGroups];
    buffer.position(offset+16);
    buffer = buffer.slice();
    IntBuffer ibuffer = buffer.asIntBuffer();
    for (int i=0; i<numGroups; i++) {
        startCharCode[i] = ibuffer.get() & INTMASK;
        endCharCode[i] = ibuffer.get() & INTMASK;
        startGlyphID[i] = ibuffer.get() & INTMASK;
    }

    /* Finds the high bit by binary searching through the bits */
    int value = numGroups;

    if (value >= 1 << 16) {
        value >>= 16;
        highBit += 16;
    }

    if (value >= 1 << 8) {
        value >>= 8;
        highBit += 8;
    }

    if (value >= 1 << 4) {
        value >>= 4;
        highBit += 4;
    }

    if (value >= 1 << 2) {
        value >>= 2;
        highBit += 2;
    }

    if (value >= 1 << 1) {
        value >>= 1;
        highBit += 1;
    }

    power = 1 << highBit;
    extra = numGroups - power;
}
 
Example 18
Source File: Terrain.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
/**
 * A Terrain is composed by blocks, each block is a GameItem constructed
 * from a HeightMap.
 *
 * @param terrainSize The number of blocks will be terrainSize * terrainSize
 * @param scale The scale to be applied to each terrain block
 * @param minY The minimum y value, before scaling, of each terrain block
 * @param maxY The maximum y value, before scaling, of each terrain block
 * @param heightMapFile
 * @param textureFile
 * @param textInc
 * @throws Exception
 */
public Terrain(int terrainSize, float scale, float minY, float maxY, String heightMapFile, String textureFile, int textInc) throws Exception {
    this.terrainSize = terrainSize;
    gameItems = new GameItem[terrainSize * terrainSize];

    ByteBuffer buf = null;
    int width;
    int height;
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load(heightMapFile, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file [" + heightMapFile  + "] not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    // The number of vertices per column and row
    verticesPerCol = width - 1;
    verticesPerRow = height - 1;

    heightMapMesh = new HeightMapMesh(minY, maxY, buf, width, height, textureFile, textInc);
    boundingBoxes = new Box2D[terrainSize][terrainSize];
    for (int row = 0; row < terrainSize; row++) {
        for (int col = 0; col < terrainSize; col++) {
            float xDisplacement = (col - ((float) terrainSize - 1) / (float) 2) * scale * HeightMapMesh.getXLength();
            float zDisplacement = (row - ((float) terrainSize - 1) / (float) 2) * scale * HeightMapMesh.getZLength();

            GameItem terrainBlock = new GameItem(heightMapMesh.getMesh());
            terrainBlock.setScale(scale);
            terrainBlock.setPosition(xDisplacement, 0, zDisplacement);
            gameItems[row * terrainSize + col] = terrainBlock;

            boundingBoxes[row][col] = getBoundingBox(terrainBlock);
        }
    }

    stbi_image_free(buf);
}
 
Example 19
Source File: Gl_320_query_occlusion.java    From jogl-samples with MIT License 3 votes vote down vote up
private boolean initQuery(GL3 gl3) {

        gl3.glGenQueries(1, queryName);

        IntBuffer queryBits = GLBuffers.newDirectIntBuffer(1);
        gl3.glGetQueryiv(GL_SAMPLES_PASSED, GL_QUERY_COUNTER_BITS, queryBits);

        boolean validated = queryBits.get(0) >= 32;

        BufferUtils.destroyDirectBuffer(queryBits);

        return validated && checkError(gl3, "initQuery");
    }
 
Example 20
Source File: Gl_320_query_conditional.java    From jogl-samples with MIT License 3 votes vote down vote up
private boolean initQuery(GL3 gl3) {

        gl3.glGenQueries(1, queryName);

        IntBuffer queryBits = GLBuffers.newDirectIntBuffer(1);
        gl3.glGetQueryiv(GL_SAMPLES_PASSED, GL_QUERY_COUNTER_BITS, queryBits);

        boolean validated = queryBits.get(0) >= 32;

        BufferUtils.destroyDirectBuffer(queryBits);

        return validated && checkError(gl3, "initQuery");
    }