/** * Returns true if the given value is contained in the given array. * * @param array an array of ints to search * @param value the int for which to search * @return true if <code>array</code> contains at least one occurence of * <code>value</code> * @see #indexOf(int[], int) */ public static boolean containsValue(int[] array, int value) { return indexOf(array, value) != -1; }
/** * Translates up to the first 2 bytes of a byte array to a short. * If there are fewer than 2 bytes available, the MSBs are all * assumed to be zero (regardless of endianness). * * @param bytes array of bytes to use for translation * @param little true if the bytes are provided in little-endian order * @return the short value that results from concatenating the specified bytes */ public static short bytesToShort(byte[] bytes, boolean little) { return bytesToShort(bytes, 0, 2, little); }
/** Translates the short value into an array of two bytes. */ public static byte[] shortToBytes(short value, boolean little) { byte[] v = new byte[2]; unpackBytes(value, v, 0, 2, little); return v; }
short[] s = new short[b.length / 2]; for (int i=0; i<s.length; i++) { s[i] = bytesToShort(b, i * 2, 2, little); float[] f = new float[b.length / 4]; for (int i=0; i<f.length; i++) { f[i] = bytesToFloat(b, i * 4, 4, little); int[] i = new int[b.length / 4]; for (int j=0; j<i.length; j++) { i[j] = bytesToInt(b, j * 4, 4, little); double[] d = new double[b.length / 8]; for (int i=0; i<d.length; i++) { d[i] = bytesToDouble(b, i * 8, 8, little); long[] l = new long[b.length / 8]; for (int i=0; i<l.length; i++) { l[i] = bytesToLong(b, i * 8, 8, little);
/** * 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); }
/** * Allocates a 1-dimensional byte array matching the product of the given * sizes. * * @param sizes list of sizes from which to allocate the array * @return a byte array of the appropriate size * @throws IllegalArgumentException if the total size exceeds 2GB, which is * the maximum size of an array in Java; or if any size argument is * zero or negative */ public static byte[] allocate(int... sizes) throws IllegalArgumentException { if (sizes == null) return null; if (sizes.length == 0) return new byte[0]; int total = safeMultiply32(sizes); return new byte[total]; }
/** Scan a plane for the channel min and max values. */ public static Double[] scanData(byte[] plane, int bits, boolean littleEndian) { int max = 0; int min = Integer.MAX_VALUE; if (bits <= 8) { for (int j=0; j<plane.length; j++) { if (plane[j] < min) min = plane[j]; if (plane[j] > max) max = plane[j]; } } else if (bits == 16) { for (int j=0; j<plane.length; j+=2) { short s = DataTools.bytesToShort(plane, j, 2, littleEndian); if (s < min) min = s; if (s > max) max = s; } } else if (bits == 32) { for (int j=0; j<plane.length; j+=4) { int s = DataTools.bytesToInt(plane, j, 4, littleEndian); if (s < min) min = s; if (s > max) max = s; } } Double[] rtn = new Double[2]; rtn[0] = new Double(min); rtn[1] = new Double(max); return rtn; }
Object s = DataTools.makeDataArray( buf, destBytes, FormatTools.isFloatingPoint(getPixelType()), isLittleEndian()); long max = (long) Math.pow(2, destBytes * 8) - 1; for (int i=0; i<Array.getLength(s); i++) { double scale = Array.getDouble(s, i) / 255; DataTools.unpackBytes( (long) (scale * max), buf, i * destBytes, destBytes, isLittleEndian());
String pixelType = meta.getPixelsType(series).toString(); int type = FormatTools.pixelTypeFromString(pixelType); if (!DataTools.containsValue(getPixelTypes(), type)) { throw new FormatException("Unsupported image type '" + pixelType + "'."); Object array = DataTools.makeDataArray(buf, bpp, fp, little);
/** * Translates up to the first 8 bytes of a byte array beyond the given * offset to a long. If there are fewer than 8 bytes available, * the MSBs are all assumed to be zero (regardless of endianness). */ public static long bytesToLong(byte[] bytes, int off, boolean little) { return bytesToLong(bytes, off, 8, little); }
public long[] toArray() throws IOException { long fp = stream.getFilePointer(); stream.seek(start); byte[] rawBytes = new byte[size * 8]; stream.readFully(rawBytes); stream.seek(fp); return (long[]) DataTools.makeDataArray(rawBytes, 8, false, stream.isLittleEndian()); }
/** * Translates up to the first 8 bytes of a byte array to a double. * If there are fewer than 8 bytes available, the MSBs are all * assumed to be zero (regardless of endianness). * * @param bytes array of bytes to be translated to a double * @param little true if the bytes are provided in little-endian order * @return the double value that results from concatenating the specified bytes */ public static double bytesToDouble(byte[] bytes, boolean little) { return bytesToDouble(bytes, 0, 8, little); }
try { IFormatWriter w = (IFormatWriter) writers[i].getClass().newInstance(); if (DataTools.containsValue(w.getPixelTypes(compressionTypes[q]), reader.getPixelType()))
int offset = i * BOX_SIZE * bpp * (interleaved ? rgb : 1); if (pixelType == FormatTools.FLOAT) { idx[i] = (int) DataTools.bytesToFloat(plane, offset, bpp, little); } else if (pixelType == FormatTools.DOUBLE) { idx[i] = (int) DataTools.bytesToDouble(plane, offset, bpp, little); } else if (2 == bpp) { idx[i] = (int) DataTools.bytesToShort(plane, offset, bpp, little); } else { idx[i] = DataTools.bytesToInt(plane, offset, bpp, little);
@Override public int readInt() throws IOException { fp += 4; int v = stream.readInt(); return order.equals(ByteOrder.LITTLE_ENDIAN) ? DataTools.swap(v) : v; }
/** * 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); }
/** * Allocates a 1-dimensional byte array matching the product of the given * sizes. * * @param sizes list of sizes from which to allocate the array * @return a byte array of the appropriate size * @throws IllegalArgumentException if the total size exceeds 2GB, which is * the maximum size of an array in Java; or if any size argument is * zero or negative */ public static byte[] allocate(int... sizes) throws IllegalArgumentException { if (sizes == null) return null; if (sizes.length == 0) return new byte[0]; int total = safeMultiply32(sizes); return new byte[total]; }
/** Scan a plane for the channel min and max values. */ public static Double[] scanData(byte[] plane, int bits, boolean littleEndian) { int max = 0; int min = Integer.MAX_VALUE; if (bits <= 8) { for (int j=0; j<plane.length; j++) { if (plane[j] < min) min = plane[j]; if (plane[j] > max) max = plane[j]; } } else if (bits == 16) { for (int j=0; j<plane.length; j+=2) { short s = DataTools.bytesToShort(plane, j, 2, littleEndian); if (s < min) min = s; if (s > max) max = s; } } else if (bits == 32) { for (int j=0; j<plane.length; j+=4) { int s = DataTools.bytesToInt(plane, j, 4, littleEndian); if (s < min) min = s; if (s > max) max = s; } } Double[] rtn = new Double[2]; rtn[0] = new Double(min); rtn[1] = new Double(max); return rtn; }
Object s = DataTools.makeDataArray( buf, destBytes, FormatTools.isFloatingPoint(getPixelType()), isLittleEndian()); long max = (long) Math.pow(2, destBytes * 8) - 1; for (int i=0; i<Array.getLength(s); i++) { double scale = Array.getDouble(s, i) / 255; DataTools.unpackBytes( (long) (scale * max), buf, i * destBytes, destBytes, isLittleEndian());