List of usage examples for java.awt.image ComponentColorModel ComponentColorModel
public ComponentColorModel(ColorSpace colorSpace, int[] bits, boolean hasAlpha, boolean isAlphaPremultiplied, int transparency, int transferType)
From source file:org.shaman.terrain.polygonal.GraphToHeightmap.java
private void saveMatrix(float[][] matrix, String filename, float min, float max) { byte[] buffer = new byte[size * size]; int i = 0;// w ww . j av a 2 s .co m for (int x = 0; x < size; ++x) { for (int y = 0; y < size; ++y) { buffer[i] = (byte) ((matrix[x][y] - min) * 255 / (max - min)); i++; } } ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); int[] nBits = { 8 }; ColorModel cm = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); SampleModel sm = cm.createCompatibleSampleModel(size, size); DataBufferByte db = new DataBufferByte(buffer, size * size); WritableRaster raster = Raster.createWritableRaster(sm, db, null); BufferedImage result = new BufferedImage(cm, raster, false, null); try { ImageIO.write(result, "png", new File(filename)); } catch (IOException ex) { Logger.getLogger(SketchTerrain.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.shaman.terrain.polygonal.GraphToHeightmap.java
private void saveColorMatrix(float[][][] matrix, String filename, float min, float max) { byte[] buffer = new byte[size * size * 3]; int i = 0;/*ww w . j a va 2 s. co m*/ for (int x = 0; x < size; ++x) { for (int y = 0; y < size; ++y) { buffer[i] = (byte) ((matrix[x][y][0] - min) * 255 / (max - min)); i++; buffer[i] = (byte) ((matrix[x][y][1] - min) * 255 / (max - min)); i++; buffer[i] = (byte) ((matrix[x][y][2] - min) * 255 / (max - min)); i++; } } ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB); int[] nBits = { 8, 8, 8 }; ColorModel cm = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); SampleModel sm = cm.createCompatibleSampleModel(size, size); DataBufferByte db = new DataBufferByte(buffer, size * size * 3); WritableRaster raster = Raster.createWritableRaster(sm, db, null); BufferedImage result = new BufferedImage(cm, raster, false, null); try { ImageIO.write(result, "png", new File(filename)); } catch (IOException ex) { Logger.getLogger(SketchTerrain.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:TextureByReference.java
public static BufferedImage convertToCustomRGBA(BufferedImage bImage) { if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) { ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB); }/*from www . j a v a2 s. c om*/ int width = bImage.getWidth(); int height = bImage.getHeight(); ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); int[] nBits = { 8, 8, 8, 8 }; ColorModel cm = new ComponentColorModel(cs, nBits, true, false, Transparency.OPAQUE, 0); int[] bandOffset = { 0, 1, 2, 3 }; WritableRaster newRaster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 4, 4, bandOffset, null); byte[] byteData = ((DataBufferByte) newRaster.getDataBuffer()).getData(); Raster origRaster = bImage.getData(); int[] pixel = new int[4]; int k = 0; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { pixel = origRaster.getPixel(i, j, pixel); byteData[k++] = (byte) (pixel[0]); byteData[k++] = (byte) (pixel[1]); byteData[k++] = (byte) (pixel[2]); byteData[k++] = (byte) (pixel[3]); } } BufferedImage newImage = new BufferedImage(cm, newRaster, false, null); // if (newImage.getType() == BufferedImage.TYPE_CUSTOM) { // System.out.println("Type is custom"); // } return newImage; }
From source file:TextureByReference.java
public static BufferedImage convertToCustomRGB(BufferedImage bImage) { if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) { ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB); }/*www. ja va2 s.com*/ int width = bImage.getWidth(); int height = bImage.getHeight(); ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); int[] nBits = { 8, 8, 8 }; ColorModel cm = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, 0); int[] bandOffset = { 0, 1, 2 }; WritableRaster newRaster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 3, 3, bandOffset, null); byte[] byteData = ((DataBufferByte) newRaster.getDataBuffer()).getData(); Raster origRaster = bImage.getData(); int[] pixel = new int[4]; int k = 0; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { pixel = origRaster.getPixel(i, j, pixel); byteData[k++] = (byte) (pixel[0]); byteData[k++] = (byte) (pixel[1]); byteData[k++] = (byte) (pixel[2]); } } BufferedImage newImage = new BufferedImage(cm, newRaster, false, null); // if (newImage.getType() == BufferedImage.TYPE_CUSTOM) { // System.out.println("Type is custom"); // } return newImage; }