public class TableLoader
extends java.lang.Object
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:
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 and Description |
---|
TableLoader() |
Modifier and Type | Method and 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[][] |
loadGIFFile(java.io.InputStream str)
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(java.io.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[][] |
loadPNGFile(java.io.InputStream str)
Loads PNG files and returns the result as an int[][].
|
static int[][] |
loadPNGFile(java.io.InputStream str,
boolean flipY)
Loads PNG files and returns the result as an int[][].
|
static int[][] |
loadPNMFile(java.io.InputStream str)
Loads plain or raw PGM files or plain or raw PBM files and return the result as an int[][].
|
static int[][] |
loadPNMFile(java.io.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[][] |
loadTextFile(java.io.InputStream stream)
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(java.io.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.
|
public static int[][] loadPNMFile(java.io.InputStream str, boolean flipY) throws java.io.IOException
java.io.IOException
public static int[][] loadPNMFile(java.io.InputStream str) throws java.io.IOException
java.io.IOException
public static double[][] loadTextFile(java.io.InputStream str, boolean flipY) throws java.lang.RuntimeException, java.io.IOException
java.lang.RuntimeException
java.io.IOException
public static double[][] loadTextFile(java.io.InputStream stream) throws java.io.IOException
java.io.IOException
public static int[][] loadGIFFile(java.io.InputStream str, boolean flipY) throws java.io.IOException
java.io.IOException
public static int[][] loadGIFFile(java.io.InputStream str) throws java.io.IOException
java.io.IOException
public static int[][] loadPNGFile(java.io.InputStream str, boolean flipY) throws java.io.IOException
java.io.IOException
public static int[][] loadPNGFile(java.io.InputStream str) throws java.io.IOException
java.io.IOException
public static int[][] convertToIntArray(double[][] vals)
public static double[][] convertToDoubleArray(int[][] vals)
public static long[][] convertToLongArray(double[][] vals)
public static long[][] convertToLongArray(int[][] vals)