/** * Translates up to the first 4 bytes of a byte array beyond the given * offset to an int. If there are fewer than 4 bytes available, * the MSBs are all assumed to be zero (regardless of endianness). */ public static int bytesToInt(byte[] bytes, int off, boolean little) { return bytesToInt(bytes, off, 4, little); }
/** * Translates up to the first 4 bytes of a byte array to an int. * If there are fewer than 4 bytes available, the MSBs are all * assumed to be zero (regardless of endianness). * * @param bytes array of unsigned bytes to be translated to a int * @param little true if the bytes are provided in little-endian order * @return the int value that results from concatenating the specified bytes */ public static int bytesToInt(short[] bytes, boolean little) { return bytesToInt(bytes, 0, 4, little); }
/** Converts a LUT and an array of indices into an array of RGB tuples. */ public static short[][] indexedToRGB(short[][] lut, byte[] b, boolean le) { short[][] rtn = new short[lut.length][b.length / 2]; for (int i=0; i<b.length/2; i++) { for (int j=0; j<lut.length; j++) { int index = DataTools.bytesToInt(b, i*2, 2, le); rtn[j][i] = lut[j][index]; } } return rtn; }
/** * Private method used to retrieve an Integer value from the SPE header * @param index The index of the expected value in the header array * @return The value read at the given header entry location */ private int getInt(int index) { return DataTools.bytesToInt(header, index, true); }
/** * Private method used to retrieve an Integer value from the SPE header * @param index The index of the expected value in the header array * @return The value read at the given header entry location */ private int getInt(int index) { return DataTools.bytesToInt(header, index, true); }
/** * Translates up to the first 4 bytes of a byte array to an int. * If there are fewer than 4 bytes available, the MSBs are all * assumed to be zero (regardless of endianness). */ public static int bytesToInt(byte[] bytes, boolean little) { return bytesToInt(bytes, 0, 4, little); }
/** * Translates up to the first 4 bytes of a byte array to an int. * If there are fewer than 4 bytes available, the MSBs are all * assumed to be zero (regardless of endianness). */ public static int bytesToInt(short[] bytes, boolean little) { return bytesToInt(bytes, 0, 4, little); }
/** Converts a LUT and an array of indices into an array of RGB tuples. */ public static short[][] indexedToRGB(short[][] lut, byte[] b, boolean le) { short[][] rtn = new short[lut.length][b.length / 2]; for (int i=0; i<b.length/2; i++) { for (int j=0; j<lut.length; j++) { int index = DataTools.bytesToInt(b, i*2, 2, le); rtn[j][i] = lut[j][index]; } } return rtn; }
/** * Translates up to the first 4 bytes of a byte array beyond the given * offset to an int. If there are fewer than 4 bytes available, * the MSBs are all assumed to be zero (regardless of endianness). */ public static int bytesToInt(short[] bytes, int off, boolean little) { return bytesToInt(bytes, off, 4, little); }
/** * Translates up to the first 4 bytes of a byte array to an int. * If there are fewer than 4 bytes available, the MSBs are all * assumed to be zero (regardless of endianness). * * * @param bytes array of bytes to be translated to a int * @param little true if the bytes are provided in little-endian order * @return the int value that results from concatenating the specified bytes */ public static int bytesToInt(byte[] bytes, boolean little) { return bytesToInt(bytes, 0, 4, little); }
/** * Translates up to the first 4 bytes of a byte array beyond the given * offset to an int. If there are fewer than 4 bytes available, * the MSBs are all assumed to be zero (regardless of endianness). * * @param bytes array of unsigned bytes to be translated to a int * @param off offset to the first byte in the array * @param little true if the bytes are provided in little-endian order * @return the int value that results from concatenating the specified bytes */ public static int bytesToInt(short[] bytes, int off, boolean little) { return bytesToInt(bytes, off, 4, little); }
/** * Translates up to the first 4 bytes of a byte array beyond the given * offset to an int. If there are fewer than 4 bytes available, * the MSBs are all assumed to be zero (regardless of endianness). * * @param bytes array of bytes to be translated to a int * @param off offset to the first byte in the array * @param little true if the bytes are provided in little-endian order * @return the int value that results from concatenating the specified bytes */ public static int bytesToInt(byte[] bytes, int off, boolean little) { return bytesToInt(bytes, off, 4, little); }
/** * Perform autoscaling on the given byte array; * map min to 0 and max to 255. If the number of bytes per pixel is 1, then * nothing happens. */ public static byte[] autoscale(byte[] b, int min, int max, int bpp, boolean little) { if (bpp == 1) return b; byte[] out = new byte[b.length / bpp]; for (int i=0; i<b.length; i+=bpp) { int s = DataTools.bytesToInt(b, i, bpp, little); if (s >= max) s = 255; else if (s <= min) s = 0; else { float diff = max - min; float dist = (s - min) / diff; s = (int)(dist * 255); } out[i / bpp] = (byte) s; } return out; }
/** * Translates up to the first len bytes of a byte array beyond the given * offset to a float. If there are fewer than len bytes available, * the MSBs are all assumed to be zero (regardless of endianness). */ public static float bytesToFloat(byte[] bytes, int off, int len, boolean little) { return Float.intBitsToFloat(bytesToInt(bytes, off, len, little)); }
/** * Translates up to the first len bytes of a byte array beyond a given * offset to a float. If there are fewer than len bytes available, * the MSBs are all assumed to be zero (regardless of endianness). */ public static float bytesToFloat(short[] bytes, int off, int len, boolean little) { return Float.intBitsToFloat(bytesToInt(bytes, off, len, little)); }
/** * Translates up to the first len bytes of a byte array beyond the given * offset to a float. If there are fewer than len bytes available, * the MSBs are all assumed to be zero (regardless of endianness). * * @param bytes array of bytes to be translated to a float * @param off offset to the first byte in the array * @param len number of bytes to use * @param little true if the bytes are provided in little-endian order * @return the float value that results from concatenating the specified bytes */ public static float bytesToFloat(byte[] bytes, int off, int len, boolean little) { return Float.intBitsToFloat(bytesToInt(bytes, off, len, little)); }
/** * Translates up to the first len bytes of a byte array beyond a given * offset to a float. If there are fewer than len bytes available, * the MSBs are all assumed to be zero (regardless of endianness). * * @param bytes array of unsigned bytes to be translated to a float * @param off offset to the first byte in the array * @param len number of bytes to use * @param little true if the bytes are provided in little-endian order * @return the float value that results from concatenating the specified bytes */ public static float bytesToFloat(short[] bytes, int off, int len, boolean little) { return Float.intBitsToFloat(bytesToInt(bytes, off, len, little)); }
/** Returns true if the given filename is a gzip file. */ public static boolean isGZipFile(String file) throws IOException { if (!file.toLowerCase().endsWith(".gz")) return false; FileInputStream s = new FileInputStream(file); byte[] b = new byte[2]; s.read(b); s.close(); return DataTools.bytesToInt(b, true) == GZIPInputStream.GZIP_MAGIC; }
/** * @param file the path to the GZip file * @return true if the given filename is a gzip file * @throws IOException if the file cannot be read */ public static boolean isGZipFile(String file) throws IOException { if (!file.toLowerCase().endsWith(".gz")) return false; FileInputStream s = new FileInputStream(file); byte[] b = new byte[2]; s.read(b); s.close(); return DataTools.bytesToInt(b, true) == GZIPInputStream.GZIP_MAGIC; }
/** Performs in-place differencing according to the given predictor value. */ public static void difference(byte[] input, IFD ifd) throws FormatException { int predictor = ifd.getIFDIntValue(IFD.PREDICTOR, 1); if (predictor == 2) { LOGGER.debug("performing horizontal differencing"); int[] bitsPerSample = ifd.getBitsPerSample(); long width = ifd.getTileWidth(); boolean little = ifd.isLittleEndian(); int planarConfig = ifd.getPlanarConfiguration(); int bytes = ifd.getBytesPerSample()[0]; int len = bytes * (planarConfig == 2 ? 1 : bitsPerSample.length); for (int b=input.length-bytes; b>=0; b-=bytes) { if (b / len % width == 0) continue; int value = DataTools.bytesToInt(input, b, bytes, little); value -= DataTools.bytesToInt(input, b - len, bytes, little); DataTools.unpackBytes(value, input, b, bytes, little); } } else if (predictor != 1) { throw new FormatException("Unknown Predictor (" + predictor + ")"); } }