Package sim.util

Class TableLoader

java.lang.Object
sim.util.TableLoader

public class TableLoader extends Object
TABLELOADER.java

This class provides utility methods for loading tables from files into int[][] or double[][] arrays. In MASON it's particularly useful for loading files of numbers or graphics into an IntGrid2D or DoubleGrid2D to display.

TableLoader at present can load the following kinds of files:

  • "Plain" PBM files. These store binary (1 and 0) bitmaps in a text-readable format.
  • "Raw" PBM files. These store binary (1 and 0) bitmaps in a somewhat more compressed binary format.
  • "Plain" PGM files. These store grayscale images (from 0 to under some MAXVAL you can define) in a text-readable format.
  • "Raw" PGM files. These store grayscale images (from 0 to under some MAXVAL you can define) in a somewhat more compressed binary format.
  • PNG and GIF files which store binary, 8-bit grayscale, or 8-bit indexed data.
  • Whitespace-delimited text files. These store each array row as a single line of numbers. The numbers are set off with spaces or tabs.

PBM And PGM ("PNM" files)    The first four formats are defined by the Netpbm file format specification. Various graphics programs can emit PBM or PGM files. These files are collectively known as PNM files. MASON reads these files into int[][] arrays. Note that graphics programs typically emit PBM (black and white) files in the opposite numerical format than you'd expect: 1 is black, and 0 is white. On the other hand, PGM (monochrome) files are emitted however your graphics program sees fit, typically with 0 being black and MAXVAL-1, whatever you've set it to, being white.

If you're constructing these files by hand, note that MASON is a bit more generous about plain formats than the specification allows: MASON permits lines of any length, and you can have a MAXVAL of any size you like, as long as its within the integer data type range (normally, PGM only allows lines of about 70 chars and a MAXVAL of no more than 2^16).

PNG and GIF files    These files must have colors stored as binary (black and white), 8-bit grayscale, or 8-bit indexed color.

Whitespace-delimited text filesnbsp;   These files consist of rows of numbers, each row delimited with newlines. The numbers in each row are delimited with spaces or tabs. Unlike the PBM/PGM format, you cannot at present have comments in the file. The files are loaded into double[][] arrays, though TableLoader provides a simple utility conversion function to int[][] if you're sure that all the values are actually integers and would like an int array.

MASON determines the row width of the first row by parsing through the first line. Thereafter it checks to make sure that all subsequent rows are the same width (in terms of number of elements) and thus that the int[][] array is rectangular.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[][]
    convertToDoubleArray(int[][] vals)
    Converts an int[][] array to a double[][] array.
    static int[][]
    convertToIntArray(double[][] vals)
    Converts a double[][] array to an int[][] array only if all values are within the int range.
    static long[][]
    convertToLongArray(double[][] vals)
    Converts a double[][] array to a long[][] array only if all values are within the long range.
    static long[][]
    convertToLongArray(int[][] vals)
    Converts an int[][] array to a long[][] array.
    static int[][]
    Loads GIF files and returns the result as an int[][], where each integer value represents the color table index of the pixel.
    static int[][]
    loadGIFFile(InputStream str, boolean flipY)
    Loads GIF files and returns the result as an int[][], where each integer value represents the color table index of the pixel.
    static int[][]
    Loads PNG files and returns the result as an int[][].
    static int[][]
    loadPNGFile(InputStream str, boolean flipY)
    Loads PNG files and returns the result as an int[][].
    static int[][]
    Loads plain or raw PGM files or plain or raw PBM files and return the result as an int[][].
    static int[][]
    loadPNMFile(InputStream str, boolean flipY)
    Loads plain or raw PGM files or plain or raw PBM files and return the result as an int[][].
    static double[][]
    Loads into a double[][] a plain text file of numbers, with newlines dividing the numbers into rows and tabs or spaces delimiting columns.
    static double[][]
    loadTextFile(InputStream str, boolean flipY)
    Loads into a double[][] a plain text file of numbers, with newlines dividing the numbers into rows and tabs or spaces delimiting columns.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TableLoader

      public TableLoader()
  • Method Details

    • loadPNMFile

      public static int[][] loadPNMFile(InputStream str, boolean flipY) throws IOException
      Loads plain or raw PGM files or plain or raw PBM files and return the result as an int[][]. If flipY is true, then the Y dimension is flipped.
      Throws:
      IOException
    • loadPNMFile

      public static int[][] loadPNMFile(InputStream str) throws IOException
      Loads plain or raw PGM files or plain or raw PBM files and return the result as an int[][]. The Y dimension is not flipped.
      Throws:
      IOException
    • loadTextFile

      public static double[][] loadTextFile(InputStream str, boolean flipY) throws RuntimeException, IOException
      Loads into a double[][] a plain text file of numbers, with newlines dividing the numbers into rows and tabs or spaces delimiting columns. If flipY is true, then the Y dimension is flipped.
      Throws:
      RuntimeException
      IOException
    • loadTextFile

      public static double[][] loadTextFile(InputStream stream) throws IOException
      Loads into a double[][] a plain text file of numbers, with newlines dividing the numbers into rows and tabs or spaces delimiting columns. The Y dimension is not flipped.
      Throws:
      IOException
    • loadGIFFile

      public static int[][] loadGIFFile(InputStream str, boolean flipY) throws IOException
      Loads GIF files and returns the result as an int[][], where each integer value represents the color table index of the pixel. If flipY is true, then the Y dimension is flipped.
      Throws:
      IOException
    • loadGIFFile

      public static int[][] loadGIFFile(InputStream str) throws IOException
      Loads GIF files and returns the result as an int[][], where each integer value represents the color table index of the pixel. The Y dimension is not flipped.
      Throws:
      IOException
    • loadPNGFile

      public static int[][] loadPNGFile(InputStream str, boolean flipY) throws IOException
      Loads PNG files and returns the result as an int[][]. The only PNG formats permitted are those with up to 256 grays (including simple black and white) or indexed colors from an up to 256-sized color table. Each integer value represents the gray level or the color table index value of the pixel. flipY is true, then the Y dimension is flipped.
      Throws:
      IOException
    • loadPNGFile

      public static int[][] loadPNGFile(InputStream str) throws IOException
      Loads PNG files and returns the result as an int[][]. The only PNG formats permitted are those with up to 256 grays (including simple black and white) or indexed colors from an up to 256-sized color table. Each integer value represents the gray level or the color table index value of the pixel. The Y dimension is not flipped.
      Throws:
      IOException
    • convertToIntArray

      public static int[][] convertToIntArray(double[][] vals)
      Converts a double[][] array to an int[][] array only if all values are within the int range. If not, returns null.
    • convertToDoubleArray

      public static double[][] convertToDoubleArray(int[][] vals)
      Converts an int[][] array to a double[][] array.
    • convertToLongArray

      public static long[][] convertToLongArray(double[][] vals)
      Converts a double[][] array to a long[][] array only if all values are within the long range. If not, returns null.
    • convertToLongArray

      public static long[][] convertToLongArray(int[][] vals)
      Converts an int[][] array to a long[][] array.