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(); }
/** * Create a grayscale image type specifier, given the number of * bits, data type and whether or not the data is signed. * * @param bits the number of bits used to specify a greyscale value * @param dataType a DataBuffer type constant * @param isSigned true if this type specifier should support * negative values, false otherwise * * @return a greyscal image type specifier * * @exception IllegalArgumentException if bits is not 1, 2, 4, 8 or * 16 * @exception IllegalArgumentException if dataType is not * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_SHORT or * DataBuffer.TYPE_USHORT * @exception if bits is larger than the number of bits in the given * data type */ public static ImageTypeSpecifier createGrayscale (int bits, int dataType, boolean isSigned, boolean isAlphaPremultiplied) { if (bits != 1 && bits != 2 && bits != 4 && bits != 8 && bits != 16) throw new IllegalArgumentException ("invalid bit size"); if (dataType != DataBuffer.TYPE_BYTE && dataType != DataBuffer.TYPE_SHORT && dataType != DataBuffer.TYPE_USHORT) throw new IllegalArgumentException ("invalid data type"); if (dataType == DataBuffer.TYPE_BYTE && bits > 8) throw new IllegalArgumentException ("number of bits too large for data type"); // FIXME: this is probably wrong: return new ImageTypeSpecifier (new DirectColorModel (bits, 0xff, 0x0, 0x0, 0xff), new MultiPixelPackedSampleModel (dataType, 1, 1, bits)); }
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 MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, 4); DataBuffer db = new DataBufferByte(data, w*h, 0); WritableRaster raster = Raster.createWritableRaster(sm, db, null); return new BufferedImage(palette, raster, false, null); }
/** * Creates a Tranpose operation. */ public RenderedImage create(ParameterBlock paramBlock, RenderingHints renderHints) { // Get ImageLayout from renderHints if any. ImageLayout layout = RIFUtil.getImageLayoutHint(renderHints); RenderedImage source = paramBlock.getRenderedSource(0); EnumeratedParameter type = (EnumeratedParameter)paramBlock.getObjectParameter(0); SampleModel sm = source.getSampleModel(); if ((sm instanceof MultiPixelPackedSampleModel) && (sm.getSampleSize(0) == 1) && (sm.getDataType() == DataBuffer.TYPE_BYTE || sm.getDataType() == DataBuffer.TYPE_USHORT || sm.getDataType() == DataBuffer.TYPE_INT)) { return new TransposeBinaryOpImage(source, renderHints, layout, type.getValue()); } else { return new TransposeOpImage(source, renderHints, layout, type.getValue()); } }
/** * Create a binary PlanarImage of the size/bounds specified by * the rectangle. */ private static PlanarImage createBinaryImage(Rectangle r) { if ((r.x == 0) && (r.y == 0)) { BufferedImage bi = new BufferedImage(r.width, r.height, BufferedImage.TYPE_BYTE_BINARY); return PlanarImage.wrapRenderedImage(bi); } else { SampleModel sm = new MultiPixelPackedSampleModel( DataBuffer.TYPE_BYTE, r.width, r.height, 1); // Create a TiledImage into which to write. return new TiledImage(r.x, r.y, r.width, r.height, r.x, r.y, sm, PlanarImage.createColorModel(sm)); } }
/** Creates an BinarizeOpImage with a given ParameterBlock */ public RenderedImage create(ParameterBlock paramBlock, RenderingHints renderingHints) { RenderedImage img = paramBlock.getRenderedSource(0); ImageLayout il = new ImageLayout(img); ColorModel cm = new IndexColorModel(1, 2, bwColors, bwColors, bwColors); SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, img.getWidth(), img.getHeight(), 1); il.setColorModel(cm); il.setSampleModel(sm); return new BinarizeOpImage(paramBlock.getRenderedSource(0), renderingHints, il, (Integer)paramBlock.getObjectParameter(0)); }
@Override public SampleModel convert(FieldAccessor fa, Instance instance) throws FieldAccessor.InvalidFieldException { int width = fa.getInt(instance, "width"); // NOI18N int height = fa.getInt(instance, "height"); // NOI18N int dataType = fa.getInt(instance, "dataType"); // NOI18N int scanlineStride = fa.getInt(instance, "scanlineStride"); // NOI18N int numberOfBits = fa.getInt(instance, "numberOfBits"); // NOI18N int dataBitOffset = fa.getInt(instance, "dataBitOffset"); // NOI18N return new MultiPixelPackedSampleModel(dataType, width, height, numberOfBits, scanlineStride, dataBitOffset); }
public boolean canEncodeImage(ImageTypeSpecifier type) { SampleModel sm = type.getSampleModel(); if (!(sm instanceof MultiPixelPackedSampleModel)) return false; if (sm.getSampleSize(0) != 1) return false; return true; }
public static void main(String[] args) { Vector<Class<? extends SampleModel>> classes = new Vector<Class<? extends SampleModel>>(); classes.add(ComponentSampleModel.class); classes.add(MultiPixelPackedSampleModel.class); classes.add(SinglePixelPackedSampleModel.class); classes.add(BandedSampleModel.class); classes.add(PixelInterleavedSampleModel.class); for (Class<? extends SampleModel> c : classes) { doTest(c); } }
/** * Return an image type specifier for an image that uses an indexed * colour model where each colour value has the specified number of * bits and type and where the colour tables are those given. * * @param redLUT the red index values * @param greenLUT the green index values * @param blueLUT the blue index values * @param alphaLUT the alpha index values * @param bits the number of bits per index value * @param dataType the type of each index value * * @return an indexed image type specifier * * @exception IllegalArgumentException if any of the colour arrays, * not including alphaLUT, is null * @exception IllegalArgumentException if bits is not 1, 2, 4, 8 or * 16 * @exception IllegalArgumentException if dataType is not * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_SHORT or * DataBuffer.TYPE_USHORT * @exception if bits is larger than the number of bits in the given * data type */ public static ImageTypeSpecifier createIndexed (byte[] redLUT, byte[] greenLUT, byte[] blueLUT, byte[] alphaLUT, int bits, int dataType) { if (redLUT == null || greenLUT == null || blueLUT == null) throw new IllegalArgumentException ("null colour table"); if (bits != 1 && bits != 2 && bits != 4 && bits != 8 && bits != 16) throw new IllegalArgumentException ("invalid bit size"); if (dataType != DataBuffer.TYPE_BYTE && dataType != DataBuffer.TYPE_SHORT && dataType != DataBuffer.TYPE_USHORT) throw new IllegalArgumentException ("invalid data type"); if (dataType == DataBuffer.TYPE_BYTE && bits > 8) throw new IllegalArgumentException ("number of bits too large for data type"); // FIXME: this is probably wrong: return new ImageTypeSpecifier (new IndexColorModel (bits, redLUT.length, redLUT, greenLUT, blueLUT, alphaLUT), new MultiPixelPackedSampleModel (dataType, 1, 1, bits)); }
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(); int size = (w*h) >> 1; // Scanline padded to dword offsets int wbytes = (w + (w & 1)) >> 1; int scansize = ((wbytes & 3) != 0)? (wbytes + 4 - (wbytes&3)) : wbytes; byte[] data = new byte[wbytes*h]; for(int y=h-1;y>=0;y--){ byte[] scanline = new byte[scansize]; if(in.read(scanline) != scansize) throw new IOException("Couldn't read image data."); for(int x=0;x<wbytes;x++) data[x + y*wbytes] = scanline[x]; } SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, 4); DataBuffer db = new DataBufferByte(data, w*h, 0); WritableRaster raster = Raster.createWritableRaster(sm, db, null); return new BufferedImage(palette, raster, false, null); }