Java DataInputStream Read loadColorTable(final File file)

Here you can find the source of loadColorTable(final File file)

Description

Loads the color table from the given file.

License

Open Source License

Declaration

public static float[][] loadColorTable(final File file) 

Method Source Code

//package com.java2s;
/*/*  www  .  j  a v  a2  s.  co  m*/
 * #%L
 * VisBio application for visualization of multidimensional biological
 * image data.
 * %%
 * Copyright (C) 2002 - 2014 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */

import java.io.DataInputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

public class Main {
    /** Color table file length. */
    protected static final int LUT_LEN = 768;

    /** Loads the color table from the given file. */
    public static float[][] loadColorTable(final File file) {
        if (file == null || !file.exists())
            return null;
        final long fileLen = file.length();
        if (fileLen > Integer.MAX_VALUE)
            return null;
        final int offset = (int) fileLen - LUT_LEN;
        if (offset < 0)
            return null;

        // read in LUT data
        final int[] b = new int[LUT_LEN];
        try {
            final DataInputStream fin = new DataInputStream(new FileInputStream(file));
            int skipped = 0;
            while (skipped < offset)
                skipped += fin.skipBytes(offset - skipped);
            for (int i = 0; i < LUT_LEN; i++)
                b[i] = fin.readUnsignedByte();
            fin.close();
        } catch (final IOException exc) {
            return null;
        }

        // convert data to VisAD format
        final int len = LUT_LEN / 3;
        int count = 0;
        final float[][] table = new float[3][len];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < len; j++)
                table[i][j] = b[count++] / 255f;
        }
        return table;
    }
}

Related

  1. loadChecksum(Bundle b, BundleContext bc)
  2. loadIntArrayFromFile(File file)
  3. readBytes(DataInputStream dataStream, int numberOfBytes)
  4. readBytes(DataInputStream input, int size)
  5. readBytes(DataInputStream stream, int size)