static boolean copyGlyphToBufferedImageIntAlpha(FT2Library.FT_Bitmap bitmap, BufferedImage img, int x, int y, Color color) { if (x + bitmap.width > img.getWidth()) { return false; } if (y + bitmap.rows > img.getHeight()) { return false; } final DataBufferInt dataBuffer = (DataBufferInt) img.getRaster().getDataBuffer(); final int[] data = dataBuffer.getData(); final int stride = ((SinglePixelPackedSampleModel) img.getSampleModel()).getScanlineStride(); ByteBuffer bb = bitmap.buffer.getByteBuffer(0, Math.abs(bitmap.pitch) * bitmap.rows); int bbOff = (bitmap.pitch < 0) ? (-bitmap.pitch * (bitmap.rows - 1)) : 0; int dataOff = dataBuffer.getOffset() + y * stride + x; int colorValue = (color == null ? Color.WHITE : color).getRGB() & 0xFFFFFF; for (int r = 0; r < bitmap.rows; r++, bbOff += bitmap.pitch, dataOff += stride) { for (int c = 0; c < bitmap.width; c++) { data[dataOff + c] = colorValue | (bb.get(bbOff + c) << 24); } } return true; }
private static byte[] extractDirectRGBInt( int aWidth, int aHeight, DirectColorModel aColorModel, SinglePixelPackedSampleModel aSampleModel, DataBufferInt aDataBuffer ) { byte[] out = new byte[ aWidth * aHeight * 3 ]; int rMask = aColorModel.getRedMask(); int gMask = aColorModel.getGreenMask(); int bMask = aColorModel.getBlueMask(); int rShift = getShift( rMask ); int gShift = getShift( gMask ); int bShift = getShift( bMask ); int[] bank = aDataBuffer.getBankData()[ 0 ]; int scanlineStride = aSampleModel.getScanlineStride(); int scanIx = 0; for ( int b = 0, y = 0; y < aHeight; y++ ) { int pixIx = scanIx; for ( int x = 0; x < aWidth; x++, b += 3 ) { int pixel = bank[ pixIx++ ]; out[ b ] = ( byte ) ( ( pixel & rMask ) >>> rShift ); out[ b + 1 ] = ( byte ) ( ( pixel & gMask ) >>> gShift ); out[ b + 2 ] = ( byte ) ( ( pixel & bMask ) >>> bShift ); } scanIx += scanlineStride; } return out; }
public boolean canEncodeImage(ImageTypeSpecifier type) { int dataType= type.getSampleModel().getDataType(); if (dataType < DataBuffer.TYPE_BYTE || dataType > DataBuffer.TYPE_INT) return false; SampleModel sm = type.getSampleModel(); int numBands = sm.getNumBands(); if (!(numBands == 1 || numBands == 3)) return false; if (numBands == 1 && dataType != DataBuffer.TYPE_BYTE) return false; if (dataType > DataBuffer.TYPE_BYTE && !(sm instanceof SinglePixelPackedSampleModel)) return false; return true; }
public static int getElementSize(SampleModel sm) { int elementSize = DataBuffer.getDataTypeSize(sm.getDataType()); if (sm instanceof MultiPixelPackedSampleModel) { MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel)sm; return mppsm.getSampleSize(0) * mppsm.getNumBands(); } else if (sm instanceof ComponentSampleModel) { return sm.getNumBands() * elementSize; } else if (sm instanceof SinglePixelPackedSampleModel) { return elementSize; } return elementSize * sm.getNumBands(); }
/** * @param windowWidth * @param windowHeight * @param pBuffer * @param windowSize * @param backgroundTransparent * @return an Image */ static Object allocateRgbImage(int windowWidth, int windowHeight, int[] pBuffer, int windowSize, boolean backgroundTransparent) { //backgroundTransparent not working with antialiasDisplay. I have no idea why. BH 9/24/08 /* DEAD CODE if (false && backgroundTransparent) return new BufferedImage( rgbColorModelT, Raster.createWritableRaster( new SinglePixelPackedSampleModel( DataBuffer.TYPE_INT, windowWidth, windowHeight, sampleModelBitMasksT), new DataBufferInt(pBuffer, windowSize), null), false, null); */ return new BufferedImage(rgbColorModel, Raster.createWritableRaster( new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, windowWidth, windowHeight, sampleModelBitMasks), new DataBufferInt(pBuffer, windowSize), null), false, null); }
private void checkCompatible(SampleModel model){ // Check model is ok: should be SinglePixelPackedSampleModel if(!(model instanceof SinglePixelPackedSampleModel)) throw new IllegalArgumentException ("MorphologyOp only works with Rasters " + "using SinglePixelPackedSampleModels"); // Check number of bands int nBands = model.getNumBands(); if(nBands!=4) throw new IllegalArgumentException ("MorphologyOp only words with Rasters having 4 bands"); // Check that integer packed. if(model.getDataType()!=DataBuffer.TYPE_INT) throw new IllegalArgumentException ("MorphologyOp only works with Rasters using DataBufferInt"); // Check bit masks int[] bitOffsets=((SinglePixelPackedSampleModel)model).getBitOffsets(); for(int i=0; i<bitOffsets.length; i++){ if(bitOffsets[i]%8 != 0) throw new IllegalArgumentException ("MorphologyOp only works with Rasters using 8 bits " + "per band : " + i + " : " + bitOffsets[i]); } }
public BufferedImage decode(ImageInputStream in) throws IOException, BMPException { IndexColorModel palette = readPalette(in); skipToImage(in); Dimension d = infoHeader.getSize(); int h = (int)d.getHeight(); int w = (int)d.getWidth(); byte[] data = uncompress(w, h, in); SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, new int[] {0xFF}); DataBuffer db = new DataBufferByte(data, w*h, 0); WritableRaster raster = Raster.createWritableRaster(sm, db, null); return new BufferedImage(palette, raster, false, null); }
/** * i_rasterとバッファを共有するBufferedImageを生成します。 * * @param i_raster * @return * @throws NyARRuntimeException */ public static BufferedImage createWrappedBufferedImage(INyARGrayscaleRaster i_raster) throws NyARRuntimeException { BufferedImage bfi; NyARIntSize s = i_raster.getSize(); switch (i_raster.getBufferType()) { case NyARBufferType.INT1D_GRAY_8: { int[] b = (int[]) i_raster.getBuffer(); DataBufferInt d = new DataBufferInt(b, b.length); int[] msk = { 0x0000ff, 0x0000ff, 0x0000ff }; bfi = new BufferedImage(new DirectColorModel(24, msk[0], msk[1], msk[2]), Raster.createWritableRaster( new SinglePixelPackedSampleModel(d.getDataType(), s.w, s.h, msk), d, null), true, null); } break; default: throw new NyARRuntimeException(); } return bfi; }
/** * Writes a {@link BufferedImage} to the specified {@link OutputStream} using the PNGJ library * which is much faster than Java's ImageIO library. * * This implementation was copied from * <a href="https://github.com/leonbloy/pngj/wiki/Snippets"> * https://github.com/leonbloy/pngj/wiki/Snippets * </a>. * * @param bufferedImage image to write. * @param compressionLevel 0 (no compression) - 9 (max compression) * @param filterType internal prediction filter type. * @param outputStream target stream. * * @throws IOException * if the image is not ARGB or it's data buffer contains the wrong number of banks. */ public static void writePngImage(final BufferedImage bufferedImage, final int compressionLevel, final FilterType filterType, final OutputStream outputStream) throws IOException { if (bufferedImage.getType() != BufferedImage.TYPE_INT_ARGB) { throw new IOException("invalid image type (" + bufferedImage.getType() + "), must be BufferedImage.TYPE_INT_ARGB"); } final ImageInfo imageInfo = new ImageInfo(bufferedImage.getWidth(), bufferedImage.getHeight(), 8, true); final PngWriter pngWriter = new PngWriter(outputStream, imageInfo); pngWriter.setCompLevel(compressionLevel); pngWriter.setFilterType(filterType); final DataBufferInt dataBuffer =((DataBufferInt) bufferedImage.getRaster().getDataBuffer()); if (dataBuffer.getNumBanks() != 1) { throw new IOException("invalid number of banks (" + dataBuffer.getNumBanks() + "), must be 1"); } final SinglePixelPackedSampleModel sampleModel = (SinglePixelPackedSampleModel) bufferedImage.getSampleModel(); final ImageLineInt line = new ImageLineInt(imageInfo); final int[] data = dataBuffer.getData(); for (int row = 0; row < imageInfo.rows; row++) { int elem = sampleModel.getOffset(0, row); for (int col = 0; col < imageInfo.cols; col++) { final int sample = data[elem++]; ImageLineHelper.setPixelRGBA8(line, col, sample); } pngWriter.writeRow(line, row); } pngWriter.end(); // // This looked like a nicer option, but only works for DataBufferByte (not DataBufferInt) // final ImageLineSetARGBbi lines = new ImageLineSetARGBbi(bufferedImage, imageInfo); // pngWriter.writeRows(lines); // pngWriter.end(); }
@Override protected void setUp() throws Exception { super.setUp(); dbb1 = new DataBufferByte(w * h); dbb2 = new DataBufferByte(scanlineStride * (h - 1) + w); dbu1 = new DataBufferUShort(w * h); dbu2 = new DataBufferUShort(scanlineStride * (h - 1) + w); dbi1 = new DataBufferInt(w * h); dbi2 = new DataBufferInt(scanlineStride * (h - 1) + w); sppsmb1 = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, bitMaskB); sppsmb2 = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, scanlineStride, bitMaskB); sppsmu1 = new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT, w, h, bitMaskUS); sppsmu2 = new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT, w, h, scanlineStride, bitMaskUS); sppsmi1 = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, w, h, bitMaskI); sppsmi2 = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, w, h, scanlineStride, bitMaskI); initTestData(); }
/** * Returns a width- and height-aligned image representation sharing data w/ {@link #image}. * @param width * @param height * @return * @throws IllegalArgumentException if requested size exceeds image size */ public BufferedImage getAlignedImage(final int width, final int height) throws IllegalArgumentException { if( width * height > image.getWidth() * image.getHeight() ) { throw new IllegalArgumentException("Requested size exceeds image size: "+width+"x"+height+" > "+image.getWidth()+"x"+image.getHeight()); } if( width == image.getWidth() && height == image.getHeight() ) { return image; } else { final ColorModel cm = image.getColorModel(); final WritableRaster raster0 = image.getRaster(); final DataBuffer dataBuffer = raster0.getDataBuffer(); final SinglePixelPackedSampleModel sppsm0 = (SinglePixelPackedSampleModel) raster0.getSampleModel(); final SinglePixelPackedSampleModel sppsm1 = new SinglePixelPackedSampleModel(dataBuffer.getDataType(), width, height, width /* scanLineStride */, sppsm0.getBitMasks()); final WritableRaster raster1 = Raster.createWritableRaster(sppsm1, dataBuffer, null); return new BufferedImage (cm, raster1, cm.isAlphaPremultiplied(), null); } }
/** * Return a Raster containing the colors generated for the graphics * operation. * * @param x * ,y,w,h The area in device space for which colors are * generated. */ @Override public Raster getRaster(int x, int y, int w, int h) { Raster rast = saved; if (rast == null || rast.getWidth() < w || rast.getHeight() < h) { rast = getCachedRaster(model, w, h); saved = rast; } DataBufferInt rasterDB = (DataBufferInt) rast.getDataBuffer(); int[] pixels = rasterDB.getBankData()[0]; int off = rasterDB.getOffset(); int scanlineStride = ((SinglePixelPackedSampleModel) rast.getSampleModel()).getScanlineStride(); int adjust = scanlineStride - w; fillRaster(pixels, off, adjust, x, y, w, h); return rast; }