@Override public ColorModel getColorModel(int transparency) { switch (transparency) { case Transparency.OPAQUE: // REMIND: once the ColorModel spec is changed, this should be // an opaque premultiplied DCM... return new DirectColorModel(24, 0xff0000, 0xff00, 0xff); case Transparency.BITMASK: return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000); case Transparency.TRANSLUCENT: ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); return new DirectColorModel(cs, 32, 0xff0000, 0xff00, 0xff, 0xff000000, true, DataBuffer.TYPE_INT); default: return null; } }
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; }
/** * Returns the color model associated with this configuration that * supports the specified transparency. */ public ColorModel getColorModel(int transparency) { if (model.getTransparency() == transparency) { return model; } switch (transparency) { case Transparency.OPAQUE: return new DirectColorModel(24, 0xff0000, 0xff00, 0xff); case Transparency.BITMASK: return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000); case Transparency.TRANSLUCENT: return ColorModel.getRGBdefault(); default: return null; } }
private static void verifyEquals(DirectColorModel m1, DirectColorModel m2) { if (m1.equals(null)) { throw new RuntimeException("equals(null) returns true"); } if (!(m1.equals(m2))) { throw new RuntimeException("equals() method is not working" + " properly"); } if (!(m2.equals(m1))) { throw new RuntimeException("equals() method is not working" + " properly"); } if (m1.hashCode() != m2.hashCode()) { throw new RuntimeException("HashCode is not same for same" + " PackedColorModels"); } }
private static void testMaskArrayEquality() { /* * Test with different maskArray values, since PackedColorModel * is abstract we use subclass DirectColorModel. */ DirectColorModel model1 = new DirectColorModel(24, 0x00FF0000, 0x0000FF00, 0x000000FF); DirectColorModel model2 = new DirectColorModel(24, 0x000000FF, 0x0000FF00, 0x00FF0000); if (model1.equals(model2)) { throw new RuntimeException("equals() method is determining" + " ColorMap equality improperly"); } if (model2.equals(model1)) { throw new RuntimeException("equals() method is determining" + " ColorMap equality improperly"); } }
private static void testConstructor2() { /* * verify equality with constructor * DirectColorModel(ColorSpace space, int bits, int rmask, int gmask, * int bmask, int amask, boolean isAlphaPremultiplied, int transferType) */ DirectColorModel model1 = new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF, false, DataBuffer.TYPE_BYTE); DirectColorModel model2 = new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF, false, DataBuffer.TYPE_BYTE); verifyEquals(model1, model2); }
/** * Get the image to write. This is the mosaic image with alpha channel stripped, as this * doesn't work with the JPEG export. */ private BufferedImage getImageToWrite() throws InterruptedException { BufferedImage image = SwingFXUtils.fromFXImage(mainController.getMosaicImage(), null); final int[] RGB_MASKS = {0xFF0000, 0xFF00, 0xFF}; final ColorModel rgbOpaque = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]); PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, true); pg.grabPixels(); int width = pg.getWidth(), height = pg.getHeight(); DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight()); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null); BufferedImage bi = new BufferedImage(rgbOpaque, raster, false, null); return bi; }
/** This checks to see if two DirectColorModels are identical. * Apparently the "equals" method in DirectColorModel doesn't really work. */ private static boolean equals(DirectColorModel d1,DirectColorModel d2) { if(d1.getAlphaMask()!=d2.getAlphaMask()) return false; if(d1.getGreenMask()!=d2.getGreenMask()) return false; if(d1.getRedMask()!=d2.getRedMask()) return false; if(d1.getBlueMask()!=d2.getBlueMask()) return false; if(d1.getColorSpace()!=d2.getColorSpace()) return false; if(d1.isAlphaPremultiplied()!=d2.isAlphaPremultiplied()) return false; if(d1.getTransferType()!=d2.getTransferType()) return false; if(d1.getTransparency()!=d2.getTransparency()) return false; return true; }
protected static ColorModel fixColorModel(CachableRed src) { ColorModel cm = src.getColorModel(); if (cm.hasAlpha()) { if (!cm.isAlphaPremultiplied()) cm = GraphicsUtil.coerceColorModel(cm, true); return cm; } int b = src.getSampleModel().getNumBands()+1; if (b > 4) throw new IllegalArgumentException ("CompositeRed can only handle up to three band images"); int [] masks = new int[4]; for (int i=0; i < b-1; i++) masks[i] = 0xFF0000 >> (8*i); masks[3] = 0xFF << (8*(b-1)); ColorSpace cs = cm.getColorSpace(); return new DirectColorModel(cs, 8*b, masks[0], masks[1], masks[2], masks[3], true, DataBuffer.TYPE_INT); }
public static CachableRed construct(CachableRed src, ColorModel cm) { ColorModel srcCM = src.getColorModel(); if ((cm.hasAlpha() != srcCM.hasAlpha()) || (cm.isAlphaPremultiplied() != srcCM.isAlphaPremultiplied())) return new FormatRed(src, cm); if (cm.getNumComponents() != srcCM.getNumComponents()) throw new IllegalArgumentException ("Incompatible ColorModel given"); if ((srcCM instanceof ComponentColorModel) && (cm instanceof ComponentColorModel)) return src; if ((srcCM instanceof DirectColorModel) && (cm instanceof DirectColorModel)) return src; return new FormatRed(src, cm); }
public static void debugRaster(Raster raster) { ColorModel colorModel = new DirectColorModel( ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, 0x00ff0000,// Red 0x0000ff00,// Green 0x000000ff,// Blue 0xff000000,// Alpha true, // Alpha Premultiplied DataBuffer.TYPE_INT ); Raster correctlyTranslated = raster.createChild(raster.getMinX(), raster.getMinY(), raster.getWidth(), raster.getHeight(), 0, 0, null); BufferedImage debugImage = new BufferedImage(colorModel, (WritableRaster) correctlyTranslated, true, null); debugImage(debugImage); }
/** * 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)); }