Java ByteBuffer Get getShiftedI32(final int bytesPerPixel, final ByteBuffer data, final boolean retainDataPos)

Here you can find the source of getShiftedI32(final int bytesPerPixel, final ByteBuffer data, final boolean retainDataPos)

Description

Returns shifted bytes from the given data at current position of maximal 4 bytesPerPixel .

License

Open Source License

Parameter

Parameter Description
bytesPerPixel number of bytes per pixel to fetch, a maximum of 4 are allowed
data byte buffer covering complete pixel at position offset
retainDataPos if true, absolute ByteBuffer#get(int) is used and the data position stays unchanged. Otherwise relative ByteBuffer#get() is used and the data position changes.

Return

the shifted 32bit integer value of the pixel

Declaration

public static int getShiftedI32(final int bytesPerPixel,
        final ByteBuffer data, final boolean retainDataPos) 

Method Source Code

//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    /**//from   w ww . j a  v  a 2  s.  c  om
     * Returns shifted bytes from the given {@code data} at given {@code offset}
     * of maximal 4 {@code bytesPerPixel}.
     * @param bytesPerPixel number of bytes per pixel to fetch, a maximum of 4 are allowed
     * @param data byte buffer covering complete pixel at position {@code offset}
     * @param offset byte offset of pixel {@code data} start
     * @return the shifted 32bit integer value of the pixel
     */
    public static int getShiftedI32(final int bytesPerPixel,
            final byte[] data, final int offset) {
        if (bytesPerPixel <= 4) {
            int shiftedI32 = 0;
            for (int i = 0; i < bytesPerPixel; i++) {
                shiftedI32 |= (0xff & data[offset + i]) << 8 * i;
            }
            return shiftedI32;
        } else {
            throw new UnsupportedOperationException(bytesPerPixel
                    + " bytesPerPixel too big, i.e. > 4");
        }
    }

    /**
     * Returns shifted bytes from the given {@code data} at current position
     * of maximal 4 {@code bytesPerPixel}.
     * @param bytesPerPixel number of bytes per pixel to fetch, a maximum of 4 are allowed
     * @param data byte buffer covering complete pixel at position {@code offset}
     * @param retainDataPos if true, absolute {@link ByteBuffer#get(int)} is used and the {@code data} position stays unchanged.
     *                      Otherwise relative {@link ByteBuffer#get()} is used and the {@code data} position changes.
     * @return the shifted 32bit integer value of the pixel
     */
    public static int getShiftedI32(final int bytesPerPixel,
            final ByteBuffer data, final boolean retainDataPos) {
        if (bytesPerPixel <= 4) {
            int shiftedI32 = 0;
            if (retainDataPos) {
                final int offset = data.position();
                for (int i = 0; i < bytesPerPixel; i++) {
                    shiftedI32 |= (0xff & data.get(offset + i)) << 8 * i;
                }
            } else {
                for (int i = 0; i < bytesPerPixel; i++) {
                    shiftedI32 |= (0xff & data.get()) << 8 * i;
                }
            }
            return shiftedI32;
        } else {
            throw new UnsupportedOperationException(bytesPerPixel
                    + " bytesPerPixel too big, i.e. > 4");
        }
    }
}

Related

  1. getRemainingArray(ByteBuffer buffer)
  2. getRepeatSequenceCount(ByteBuffer buffer, int limitCount)
  3. getRSString(ByteBuffer buffer)
  4. getSByte(ByteBuffer buffer)
  5. getSequenceNumber(ByteBuffer chunk)
  6. getSize(int size, ByteBuffer buffer)
  7. getSizePrefix(ByteBuffer bb)
  8. getSmallSmartInt(ByteBuffer buffer)
  9. getSmart(ByteBuffer buf)