Helper class with static methods to convert between byte arrays and primitive types.
Useful for serialization.
checkArray
private static void checkArray(byte[] array,
int offset,
int length)
throws IllegalArgumentException
Makes sure that the arguments define a valid (existing) array interval.
This includes:
- array is non-null
- offset is >= 0 and smaller than array.length
- length is > 0
- offset + length is <= array.length
convertPacked2BitIntensityTo8Bit
public static void convertPacked2BitIntensityTo8Bit(byte[] src,
int srcOffset,
byte[] dest,
int destOffset,
int numPackedBytes)
Converts bytes with two four-bit-intensity samples to 8 byte intensity
values, each stored in one byte.
Two-bit values can be 0, 1, 2 or 3.
These values will be scaled to the full [0;255] range so that 0 remains 0,
1 becomes 85, 2 becomes 170 and 3 becomes 255.
A little discussion on how to implement this method
was held in the German Java newsgroup
de.comp.lang.java.
The message I wrote to start the thread has the ID
1ef7du4vfqsd2pskb6jukut6pnhn87htt2@4ax.com
.
Read the
thread
at Google Groups.
src
- byte array, each byte stores four two-bit intensity valuessrcOffset
- index into srcdest
- byte array, each byte stores an eight-bit intensity valuesdestOffset
- index into destnumPackedBytes
- number of bytes in src to be decoded
convertPacked4BitIntensityTo8Bit
public static void convertPacked4BitIntensityTo8Bit(byte[] src,
int srcOffset,
byte[] dest,
int destOffset,
int numPackedBytes)
Converts bytes with four two-bit-intensity samples to byte-sized intensity values.
Four-bit values can be from 0 to 15.
These values will be scaled to the full [0;255] range so that 0 remains 0,
1 becomes 17, 2 becomes 34, ..., and 15 becomes 255.
The most significant four bits in a byte become the left, the least significant
four bits the right pixel.
src
- byte array, each byte stores two four-bit intensity valuessrcOffset
- index into srcdest
- byte array, each byte stores an eight-bit intensity valuesdestOffset
- index into destnumPackedBytes
- number of bytes in src to be decoded
copyPackedBytes
public static void copyPackedBytes(byte[] src,
int srcOffset,
int srcBitOffset,
byte[] dest,
int destOffset,
int destBitOffset,
int numSamples)
Copies a number of bit values from one byte array to another.
src
- array from which is copiedsrcOffset
- index into the src array of the first byte from which is copiedsrcBitOffset
- first bit within src[srcOffset] from which is copied (0 is left-most, 1 is second left-most, 7 is right-most)dest
- array to which is copieddestOffset
- index into the dest array of the first byte to which is copieddestBitOffset
- first bit within dest[destOffset] to which is copied (0 is left-most, 1 is second left-most, 7 is right-most)numSamples
- number of bits to be copied
decodePacked1Bit
public static void decodePacked1Bit(byte[] src,
int srcOffset,
byte[] dest,
int destOffset,
int numPackedBytes)
decodePacked2Bit
public static void decodePacked2Bit(byte[] src,
int srcOffset,
byte[] dest,
int destOffset,
int numPackedBytes)
Decodes bytes with four two-bit samples to single bytes.
The two most significant bits of a source byte become the first value,
the two least significant bits the fourth value.
The method expects
numPackedBytes
bytes at
src[srcOffset]
(these will be read and interpreted) and
numPackedBytes * 4
at
dest[destOffset]
(where the decoded
byte values will be stored.
src
- byte array, each byte stores four two-bit valuessrcOffset
- index into srcdest
- byte array, each byte stores a single decoded value (from 0 to 3)destOffset
- index into destnumPackedBytes
- number of bytes in src to be decoded
decodePacked4Bit
public static void decodePacked4Bit(byte[] src,
int srcOffset,
byte[] dest,
int destOffset,
int numPackedBytes)
Decodes bytes with two four-bit samples to single bytes.
The four most significant bits of a source byte become the first value,
the least significant four bits the second value.
The method expects
numPackedBytes
bytes at
src[srcOffset]
(these will be read and interpreted) and
numPackedBytes * 2
at
dest[destOffset]
(where the decoded
byte values will be stored.
src
- byte array, each byte stores two four-bit valuessrcOffset
- index into srcdest
- byte array, each byte stores a single decoded valuedestOffset
- index into destnumPackedBytes
- number of bytes in src to be decoded
decodePackedRGB565BigEndianToRGB24
public static void decodePackedRGB565BigEndianToRGB24(byte[] src,
int srcOffset,
byte[] red,
int redOffset,
byte[] green,
int greenOffset,
byte[] blue,
int blueOffset,
int numPixels)
Convert 16 bit RGB samples stored in big endian (BE) byte order
with 5 bits for red and blue and 6 bits for green to 24
bit RGB byte samples.
encodePacked2Bit
public static void encodePacked2Bit(byte[] src,
int srcOffset,
byte[] dest,
int destOffset,
int numSamples)
encodePacked4Bit
public static void encodePacked4Bit(byte[] src,
int srcOffset,
byte[] dest,
int destOffset,
int numSamples)
encodeRGB24ToPackedRGB565BigEndian
public static void encodeRGB24ToPackedRGB565BigEndian(byte[] red,
int redOffset,
byte[] green,
int greenOffset,
byte[] blue,
int blueOffset,
byte[] dest,
int destOffset,
int numPixels)
Convert 24 bit RGB pixels to 16 bit pixels stored in big endian (BE) byte order
with 5 bits for red and blue and 6 bits for green.
getIntBE
public static int getIntBE(byte[] src,
int srcOffset)
Reads four consecutive bytes from the given array at the
given position in big endian order and returns them as
an int
.
src
- the array from which bytes are readsrcOffset
- the index into the array from which the bytes are read
- int value taken from the array
getIntLE
public static int getIntLE(byte[] src,
int srcOffset)
Reads four consecutive bytes from the given array at the
given position in little endian order and returns them as
an int
.
src
- the array from which bytes are readsrcOffset
- the index into the array from which the bytes are read
- short value taken from the array
getShortBE
public static short getShortBE(byte[] src,
int srcOffset)
Reads two consecutive bytes from the given array at the
given position in big endian order and returns them as
a short
.
src
- the array from which two bytes are readsrcOffset
- the index into the array from which the two bytes are read
- short value taken from the array
getShortBEAsInt
public static int getShortBEAsInt(byte[] src,
int srcOffset)
getShortLE
public static short getShortLE(byte[] src,
int srcOffset)
Reads two consecutive bytes from the given array at the
given position in little endian order and returns them as
a short
.
src
- the array from which two bytes are readsrcOffset
- the index into the array from which the two bytes are read
- short value taken from the array
setIntBE
public static void setIntBE(byte[] dest,
int destOffset,
int newValue)
Writes an int value into four consecutive bytes of a byte array,
in big endian (network) byte order.
dest
- the array to which bytes are writtendestOffset
- index of the array to which the first byte is writtennewValue
- the int value to be written to the array
setIntLE
public static void setIntLE(byte[] dest,
int destOffset,
int newValue)
Writes an int value into four consecutive bytes of a byte array,
in little endian (Intel) byte order.
dest
- the array to which bytes are writtendestOffset
- index of the array to which the first byte is writtennewValue
- the int value to be written to the array
setShortBE
public static void setShortBE(byte[] dest,
int destOffset,
short newValue)
setShortLE
public static void setShortLE(byte[] dest,
int destOffset,
short newValue)