List of usage examples for java.awt.image ColorModel createCompatibleSampleModel
public SampleModel createCompatibleSampleModel(int w, int h)
From source file:org.gallery.web.controller.ImageController.java
public static boolean compressImg(BufferedImage src, File outfile, double d) { FileOutputStream out = null;//from ww w. j av a2 s.c om ImageWriter imgWrier; ImageWriteParam imgWriteParams; // ? jpg imgWrier = ImageIO.getImageWritersByFormatName("jpg").next(); imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(null); // ??MODE_EXPLICIT imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT); // ?qality?0~1 imgWriteParams.setCompressionQuality((float) d); imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED); ColorModel colorModel = ColorModel.getRGBdefault(); // ? imgWriteParams.setDestinationType( new javax.imageio.ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16))); try { out = new FileOutputStream(outfile); imgWrier.reset(); // out?write, ImageOutputStream? // OutputStream imgWrier.setOutput(ImageIO.createImageOutputStream(out)); // write??? imgWrier.write(null, new IIOImage(src, null, null), imgWriteParams); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:de.codesourcery.jasm16.emulator.devices.impl.RawImage.java
public RawImage(int width, int height) { data = new int[width * height]; final DataBufferInt dataBuffer = new DataBufferInt(data, width * height); final ColorModel cm = new DirectColorModel(24, 0xff0000, 0xff00, 0xff); final SampleModel sm = cm.createCompatibleSampleModel(width, height); final WritableRaster wr = Raster.createWritableRaster(sm, dataBuffer, null); image = new BufferedImage(cm, wr, false, null); }
From source file:org.apache.flex.compiler.internal.embedding.transcoders.JPEGTranscoder.java
private byte[] bufferedImageToJPEG(ImageInfo imageInfo, int[] pixels) throws Exception { BufferedImage bufferedImage = new BufferedImage(imageInfo.width, imageInfo.height, BufferedImage.TYPE_INT_ARGB); bufferedImage.setRGB(0, 0, imageInfo.width, imageInfo.height, pixels, 0, imageInfo.width); ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam writeParam = writer.getDefaultWriteParam(); ColorModel colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff); ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(1, 1)); writeParam.setDestinationType(imageTypeSpecifier); writeParam.setSourceBands(new int[] { 0, 1, 2 }); writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); float q = 1.0f; if (quality != null) q = quality.floatValue();//from w w w . j av a 2 s . c o m writeParam.setCompressionQuality(q); DAByteArrayOutputStream buffer = new DAByteArrayOutputStream(); writer.setOutput(new MemoryCacheImageOutputStream(buffer)); IIOImage ioImage = new IIOImage(bufferedImage, null, null); writer.write(null, ioImage, writeParam); writer.dispose(); return buffer.getDirectByteArray(); }
From source file:org.polymap.core.data.image.ImageEncodeProcessor.java
private void imageioEncodeJPEG(Image image, ChunkedResponseOutputStream out) throws IOException { // this code is from http://forums.sun.com/thread.jspa?threadID=5197061 ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam param = writer.getDefaultWriteParam(); param.setSourceBands(new int[] { 0, 1, 2 }); ColorModel cm = new DirectColorModel(24, 0x00ff0000, // Red 0x0000ff00, // Green 0x000000ff, // Blue 0x0); // Alpha param.setDestinationType(new ImageTypeSpecifier(cm, cm.createCompatibleSampleModel(1, 1))); ImageOutputStream imageOut = ImageIO.createImageOutputStream(out); writer.setOutput(imageOut);//from w ww. j a v a2s . c o m writer.write(null, new IIOImage((RenderedImage) image, null, null), param); writer.dispose(); imageOut.close(); }
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;//ww w . ja v 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;//from w ww .j a v a 2s.c o 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); } }