Package net.sourceforge.jiu.util
Class ArrayConverter
- java.lang.Object
-
- net.sourceforge.jiu.util.ArrayConverter
-
public class ArrayConverter extends Object
Helper class with static methods to convert between byte arrays and primitive types. Useful for serialization.- Since:
- 0.9.0
- Author:
- Marco Schmidt
-
-
Field Summary
Fields Modifier and Type Field Description private static int
INT_SIZE
private static int
SHORT_SIZE
-
Constructor Summary
Constructors Modifier Constructor Description private
ArrayConverter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static void
checkArray(byte[] array, int offset, int length)
Makes sure that the arguments define a valid (existing) array interval.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.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.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.static void
decodePacked1Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
static void
decodePacked2Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
Decodes bytes with four two-bit samples to single bytes.static void
decodePacked4Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
Decodes bytes with two four-bit samples to single bytes.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.static void
encodePacked2Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numSamples)
static void
encodePacked4Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numSamples)
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.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 anint
.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 anint
.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 ashort
.static int
getShortBEAsInt(byte[] src, int srcOffset)
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 ashort
.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.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.static void
setShortBE(byte[] dest, int destOffset, short newValue)
static void
setShortLE(byte[] dest, int destOffset, short newValue)
-
-
-
Field Detail
-
SHORT_SIZE
private static final int SHORT_SIZE
- See Also:
- Constant Field Values
-
INT_SIZE
private static final int INT_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
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
- Throws:
IllegalArgumentException
-
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.- Parameters:
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.- Parameters:
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- Since:
- 0.12.0
-
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.- Parameters:
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 expectsnumPackedBytes
bytes atsrc[srcOffset]
(these will be read and interpreted) andnumPackedBytes * 4
atdest[destOffset]
(where the decoded byte values will be stored.- Parameters:
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- Since:
- 0.10.0
-
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 expectsnumPackedBytes
bytes atsrc[srcOffset]
(these will be read and interpreted) andnumPackedBytes * 2
atdest[destOffset]
(where the decoded byte values will be stored.- Parameters:
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.- Since:
- 0.10.0
-
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.- Since:
- 0.10.0
-
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 anint
.- Parameters:
src
- the array from which bytes are readsrcOffset
- the index into the array from which the bytes are read- Returns:
- 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 anint
.- Parameters:
src
- the array from which bytes are readsrcOffset
- the index into the array from which the bytes are read- Returns:
- 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 ashort
.- Parameters:
src
- the array from which two bytes are readsrcOffset
- the index into the array from which the two bytes are read- Returns:
- 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 ashort
.- Parameters:
src
- the array from which two bytes are readsrcOffset
- the index into the array from which the two bytes are read- Returns:
- 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.- Parameters:
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.- Parameters:
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)
-
-