/** Convers an Index16ColorModel to a 2D short array. */ public static short[][] getLookupTable(ColorModel model) { if (!(model instanceof Index16ColorModel)) return null; Index16ColorModel m = (Index16ColorModel) model; short[][] lut = new short[3][]; lut[0] = m.getReds(); lut[1] = m.getGreens(); lut[2] = m.getBlues(); return lut; }
/** * Use the lookup table from the reader (if present) to set * the color model in the given writer * @param writer the {@link loci.formats.IFormatWriter} on which to set a color model * @throws FormatException * @throws IOException */ private void applyLUT(IFormatWriter writer) throws FormatException, IOException { if (lookup) { byte[][] lut = reader.get8BitLookupTable(); if (lut != null) { IndexColorModel model = new IndexColorModel(8, lut[0].length, lut[0], lut[1], lut[2]); writer.setColorModel(model); } else { short[][] lut16 = reader.get16BitLookupTable(); if (lut16 != null) { Index16ColorModel model = new Index16ColorModel(16, lut16[0].length, lut16, reader.isLittleEndian()); writer.setColorModel(model); } } } }
/** Converts an indexed color BufferedImage to an RGB BufferedImage. */ public static BufferedImage indexedToRGB(BufferedImage img, boolean le) { byte[][] indices = getPixelBytes(img, le); if (indices.length > 1) return img; int pixelType = getPixelType(img); boolean signed = FormatTools.isSigned(pixelType); if (pixelType == FormatTools.UINT8) { IndexColorModel model = (IndexColorModel) img.getColorModel(); byte[][] b = new byte[3][indices[0].length]; for (int i=0; i<indices[0].length; i++) { b[0][i] = (byte) (model.getRed(indices[0][i] & 0xff) & 0xff); b[1][i] = (byte) (model.getGreen(indices[0][i] & 0xff) & 0xff); b[2][i] = (byte) (model.getBlue(indices[0][i] & 0xff) & 0xff); } return makeImage(b, img.getWidth(), img.getHeight(), signed); } else if (pixelType == FormatTools.UINT16) { Index16ColorModel model = (Index16ColorModel) img.getColorModel(); short[][] s = new short[3][indices[0].length / 2]; for (int i=0; i<s[0].length; i++) { int ndx = DataTools.bytesToInt(indices[0], i*2, 2, le) & 0xffff; s[0][i] = (short) (model.getRed(ndx) & 0xffff); s[1][i] = (short) (model.getRed(ndx) & 0xffff); s[2][i] = (short) (model.getRed(ndx) & 0xffff); } return makeImage(s, img.getWidth(), img.getHeight(), signed); } return null; }
model = new Index16ColorModel(16, table[0].length, table, r.isLittleEndian());
/** Converts an indexed color BufferedImage to an RGB BufferedImage. */ public static BufferedImage indexedToRGB(BufferedImage img, boolean le) { byte[][] indices = getPixelBytes(img, le); if (indices.length > 1) return img; int pixelType = getPixelType(img); boolean signed = FormatTools.isSigned(pixelType); if (pixelType == FormatTools.UINT8) { IndexColorModel model = (IndexColorModel) img.getColorModel(); byte[][] b = new byte[3][indices[0].length]; for (int i=0; i<indices[0].length; i++) { b[0][i] = (byte) (model.getRed(indices[0][i] & 0xff) & 0xff); b[1][i] = (byte) (model.getGreen(indices[0][i] & 0xff) & 0xff); b[2][i] = (byte) (model.getBlue(indices[0][i] & 0xff) & 0xff); } return makeImage(b, img.getWidth(), img.getHeight(), signed); } else if (pixelType == FormatTools.UINT16) { Index16ColorModel model = (Index16ColorModel) img.getColorModel(); short[][] s = new short[3][indices[0].length / 2]; for (int i=0; i<s[0].length; i++) { int ndx = DataTools.bytesToInt(indices[0], i*2, 2, le) & 0xffff; s[0][i] = (short) (model.getRed(ndx) & 0xffff); s[1][i] = (short) (model.getRed(ndx) & 0xffff); s[2][i] = (short) (model.getRed(ndx) & 0xffff); } return makeImage(s, img.getWidth(), img.getHeight(), signed); } return null; }
w.setColorModel(new Index16ColorModel(16, newLut[0].length, newLut, littleEndian));
/** Convers an Index16ColorModel to a 2D short array. */ public static short[][] getLookupTable(ColorModel model) { if (!(model instanceof Index16ColorModel)) return null; Index16ColorModel m = (Index16ColorModel) model; short[][] lut = new short[3][]; lut[0] = m.getReds(); lut[1] = m.getGreens(); lut[2] = m.getBlues(); return lut; }
model = new Index16ColorModel(16, table[0].length, table, r.isLittleEndian());
w.setColorModel(new Index16ColorModel(16, newLut[0].length, newLut, littleEndian));