Module ij
Package ij.util

Class Tools


  • public class Tools
    extends java.lang.Object
    This class contains static utility methods.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char[] hexDigits
      This array contains the 16 hex digits '0'-'F'.
    • Constructor Summary

      Constructors 
      Constructor Description
      Tools()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addToArray​(float[] a, float value)
      Adds a number to all array elements
      static java.lang.String c2hex​(java.awt.Color c)
      Converts a Color to an 7 byte hex string starting with '#'.
      static java.lang.String copyFile​(java.lang.String path1, java.lang.String path2)
      Copies the contents of the file at 'path1' to 'path2', returning an error message (as a non-empty string) if there is an error.
      static java.lang.String decodeEscaped​(java.lang.String str, char delim)
      Decodes backslash-escaped characters from the String until the character 'delim' is found.
      static java.lang.String f2hex​(float f)
      Converts a float to an 9 byte hex string starting with '#'.
      static java.lang.String fixNewLines​(java.lang.String s)
      Converts carriage returns to line feeds.
      static int getDecimalPlaces​(double n)
      Returns the number of decimal places needed to display a number, or -2 if exponential notation should be used.
      static int getDecimalPlaces​(double n1, double n2)
      Returns the number of decimal places needed to display two numbers, or -2 if exponential notation should be used.
      static java.lang.String getHash​(java.lang.String method, boolean fromFile, java.lang.String pathOrString)
      Returns the checksum of a string or file, or "0" if no success.
      static double[] getMinMax​(double[] a)
      Determines the minimum and maximum value in the array a and returns them as 2-element array {minimum, maximum}.
      static double[] getMinMax​(float[] a)
      Determines the minimum and maximum value in the array a and returns them as 2-element array {minimum, maximum}.
      static double getNumberFromList​(java.lang.String list, java.lang.String key)
      Retrieves a number form a list of key-number pairs like "value1=1234.5 area=1.2e6".
      static double getNumberFromList​(java.lang.String list, java.lang.String key, double defaultValue)  
      static ImageStatistics getStatistics​(double[] a)  
      static java.lang.String getStringFromList​(java.lang.String list, java.lang.String key)
      Retrieves a String form a list of key-number pairs like "value1="abc" str='the Text'".
      static java.lang.String getStringFromList​(java.lang.String list, java.lang.String key, java.lang.String defaultValue)  
      static java.lang.String int2hex​(int i, int digits)
      Converts an int to a zero-padded hex string of fixed length 'digits'.
      static java.lang.String openFromIJJarAsString​(java.lang.String path)
      Opens a text file in ij.jar as a String (example path: "/macros/Circle_Tool.txt").
      static double parseDouble​(java.lang.String s)
      Returns a double containg the value represented by the specified String.
      static double parseDouble​(java.lang.String s, double defaultValue)
      Returns a double containg the value represented by the specified String.
      static int[] rank​(double[] values)
      Returns a sorted list of indices of the specified double array.
      static int[] rank​(java.lang.String[] data)
      Returns a sorted list of indices of the specified String array.
      static double[] resampleArray​(double[] y1, int len2)
      Returns an array linearly resampled to a different length.
      static java.lang.String[] split​(java.lang.String str)
      Splits a string into substrings using the default delimiter set, which is " \t\n\r" (space, tab, newline and carriage-return).
      static java.lang.String[] split​(java.lang.String str, java.lang.String delim)
      Splits a string into substring using the characters contained in the second argument as the delimiter set.
      static double[] toDouble​(float[] a)
      Converts the float array 'a' to a double array.
      static float[] toFloat​(double[] a)
      Converts the double array 'a' to a float array.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • hexDigits

        public static final char[] hexDigits
        This array contains the 16 hex digits '0'-'F'.
    • Constructor Detail

      • Tools

        public Tools()
    • Method Detail

      • c2hex

        public static java.lang.String c2hex​(java.awt.Color c)
        Converts a Color to an 7 byte hex string starting with '#'.
      • f2hex

        public static java.lang.String f2hex​(float f)
        Converts a float to an 9 byte hex string starting with '#'.
      • int2hex

        public static java.lang.String int2hex​(int i,
                                               int digits)
        Converts an int to a zero-padded hex string of fixed length 'digits'. If the number is too high, it gets truncated, keeping only the lowest 'digits' characters.
      • getStatistics

        public static ImageStatistics getStatistics​(double[] a)
      • getMinMax

        public static double[] getMinMax​(double[] a)
        Determines the minimum and maximum value in the array a and returns them as 2-element array {minimum, maximum}.
      • getMinMax

        public static double[] getMinMax​(float[] a)
        Determines the minimum and maximum value in the array a and returns them as 2-element array {minimum, maximum}.
      • toDouble

        public static double[] toDouble​(float[] a)
        Converts the float array 'a' to a double array.
      • toFloat

        public static float[] toFloat​(double[] a)
        Converts the double array 'a' to a float array.
      • addToArray

        public static void addToArray​(float[] a,
                                      float value)
        Adds a number to all array elements
      • fixNewLines

        public static java.lang.String fixNewLines​(java.lang.String s)
        Converts carriage returns to line feeds.
      • parseDouble

        public static double parseDouble​(java.lang.String s,
                                         double defaultValue)
        Returns a double containg the value represented by the specified String.
        Parameters:
        s - the string to be parsed.
        defaultValue - the value returned if s does not contain a parsable double
        Returns:
        The double value represented by the string argument or defaultValue if the string does not contain a parsable double
      • parseDouble

        public static double parseDouble​(java.lang.String s)
        Returns a double containg the value represented by the specified String.
        Parameters:
        s - the string to be parsed.
        Returns:
        The double value represented by the string argument or Double.NaN if the string does not contain a parsable double
      • getDecimalPlaces

        public static int getDecimalPlaces​(double n)
        Returns the number of decimal places needed to display a number, or -2 if exponential notation should be used.
      • getDecimalPlaces

        public static int getDecimalPlaces​(double n1,
                                           double n2)
        Returns the number of decimal places needed to display two numbers, or -2 if exponential notation should be used.
      • split

        public static java.lang.String[] split​(java.lang.String str)
        Splits a string into substrings using the default delimiter set, which is " \t\n\r" (space, tab, newline and carriage-return).
      • split

        public static java.lang.String[] split​(java.lang.String str,
                                               java.lang.String delim)
        Splits a string into substring using the characters contained in the second argument as the delimiter set.
      • rank

        public static int[] rank​(double[] values)
        Returns a sorted list of indices of the specified double array. Modified from: http://stackoverflow.com/questions/951848 by N.Vischer.
      • rank

        public static int[] rank​(java.lang.String[] data)
        Returns a sorted list of indices of the specified String array.
      • resampleArray

        public static double[] resampleArray​(double[] y1,
                                             int len2)
        Returns an array linearly resampled to a different length.
      • openFromIJJarAsString

        public static java.lang.String openFromIJJarAsString​(java.lang.String path)
        Opens a text file in ij.jar as a String (example path: "/macros/Circle_Tool.txt").
      • copyFile

        public static java.lang.String copyFile​(java.lang.String path1,
                                                java.lang.String path2)
        Copies the contents of the file at 'path1' to 'path2', returning an error message (as a non-empty string) if there is an error. Based on the method with the same name in Tobias Pietzsch's TifBenchmark class.
      • getNumberFromList

        public static double getNumberFromList​(java.lang.String list,
                                               java.lang.String key)
        Retrieves a number form a list of key-number pairs like "value1=1234.5 area=1.2e6". The "=" (if present) must be part of the 'key' string. Delimiters may be commas, semicolons or whitespace. There must be no whitespace between key and number. Returns Double.NaN if 'list' is null, if the key is not found, if the number is 'NaN' or invalid.
      • getNumberFromList

        public static double getNumberFromList​(java.lang.String list,
                                               java.lang.String key,
                                               double defaultValue)
      • getStringFromList

        public static java.lang.String getStringFromList​(java.lang.String list,
                                                         java.lang.String key)
        Retrieves a String form a list of key-number pairs like "value1="abc" str='the Text'". The "=" (if present) must be part of the 'key' string. Strings containing commas, semicolons or whitespace must be enclosed in single or double quotes. There must be no whitespace between key and quotes or key and unquoted String. Strings must not contain a key. In case of doubt, e.g., replace the '=' with '\=' Escape sequences with backslashes are recognized, also backslash-u???? for unicode. Returns null if 'list' is null or if the key is not found
      • getStringFromList

        public static java.lang.String getStringFromList​(java.lang.String list,
                                                         java.lang.String key,
                                                         java.lang.String defaultValue)
      • decodeEscaped

        public static java.lang.String decodeEscaped​(java.lang.String str,
                                                     char delim)
        Decodes backslash-escaped characters from the String until the character 'delim' is found. With 'delim'=-1, does not search for a delimiter, with 'delim'=(char)-2, (unescaped) whitespace, commas and semicolons are interpreted as delimiters. Also decodes unicode backslash-u???? characters
      • getHash

        public static java.lang.String getHash​(java.lang.String method,
                                               boolean fromFile,
                                               java.lang.String pathOrString)
        Returns the checksum of a string or file, or "0" if no success. The 'method' argument must be "MD5" or "SHA-256".