下面列出了java.awt.image.ColorConvertOp#filter() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static boolean testImage(int dataType, int rBits, int gBits, int bBits,
int cs, BufferedImage gldImage,
double accuracy)
{
BufferedImage src = ImageFactory.createCCMImage(cs, dataType);
BufferedImage dst = ImageFactory.createDstImage(
BufferedImage.TYPE_INT_RGB);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dst);
ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
bBits);
boolean result = cmp.compare(gldImage, dst);
if (!result) {
System.err.println(cmp.getStat());
}
return result;
}
static boolean testSubImage(int x0, int y0, int dx, int dy, int type,
int rBits, int gBits, int bBits,
int cs, BufferedImage gldImage,
double accuracy)
{
BufferedImage src = ImageFactory.createDCMImage(type, cs);
BufferedImage subSrc = src.getSubimage(x0, y0, dx, dy);
BufferedImage dst = ImageFactory.createDstImage(
BufferedImage.TYPE_INT_RGB);
BufferedImage subDst = dst.getSubimage(x0, y0, dx, dy);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(subSrc, subDst);
ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
bBits);
boolean result = cmp.compare(subDst, gldImage, x0, y0, dx, dy);
if (!result) {
System.err.println(cmp.getStat());
}
return result;
}
static boolean testImage(int type, int rBits, int gBits, int bBits,
int cs, BufferedImage gldImage,
double accuracy)
{
BufferedImage src = ImageFactory.createDCMImage(type, cs);
BufferedImage dst = ImageFactory.createDstImage(
BufferedImage.TYPE_INT_RGB);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dst);
ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
bBits);
boolean result = cmp.compare(gldImage, dst);
if (!result) {
System.err.println(cmp.getStat());
}
return result;
}
static boolean testSubImage(int x0, int y0, int dx, int dy,
int dataType, int rBits, int gBits,
int bBits, int cs, BufferedImage gldImage,
double accuracy)
{
BufferedImage src = ImageFactory.createCCMImage(cs, dataType);
BufferedImage subSrc = src.getSubimage(x0, y0, dx, dy);
BufferedImage dst = ImageFactory.createDstImage(
BufferedImage.TYPE_INT_RGB);
BufferedImage subDst = dst.getSubimage(x0, y0, dx, dy);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(subSrc, subDst);
ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
bBits);
boolean result = cmp.compare(subDst, gldImage, x0, y0, dx, dy);
if (!result) {
System.err.println(cmp.getStat());
}
return result;
}
/**
* Gets a grayscale version of the image of the given size.
*
* @param d The requested size.
* @return The {@code BufferedImage}.
*/
private BufferedImage getGrayscaleImage(Dimension d) {
final BufferedImage im = getColorImage(d); // Get the scaled image
if (im == null) return null;
int width = im.getWidth();
int height = im.getHeight();
// TODO: Find out why making a copy is necessary here to prevent
// images from getting too dark.
BufferedImage srcImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = srcImage.createGraphics();
g.drawImage(im, 0, 0, null);
g.dispose();
ColorConvertOp filter = new ColorConvertOp(
ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
return filter.filter(srcImage, null);
}
static boolean testSubImage(int x0, int y0, int dx, int dy, int type,
int rBits, int gBits, int bBits,
int cs, BufferedImage gldImage,
double accuracy)
{
BufferedImage src = ImageFactory.createDCMImage(type, cs);
BufferedImage subSrc = src.getSubimage(x0, y0, dx, dy);
BufferedImage dst = ImageFactory.createDstImage(
BufferedImage.TYPE_INT_RGB);
BufferedImage subDst = dst.getSubimage(x0, y0, dx, dy);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(subSrc, subDst);
ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
bBits);
boolean result = cmp.compare(subDst, gldImage, x0, y0, dx, dy);
if (!result) {
System.err.println(cmp.getStat());
}
return result;
}
public static void main(String[] args) {
ICC_Profile pSRGB = ICC_Profile.getInstance(CS_sRGB);
byte[] raw_data = pSRGB.getData();
setRenderingIntent(0x1000000, raw_data);
ICC_Profile p = ICC_Profile.getInstance(raw_data);
ICC_ColorSpace cs = new ICC_ColorSpace(p);
// perfrom test color conversion
ColorConvertOp op = new ColorConvertOp(cs,
ColorSpace.getInstance(CS_sRGB), null);
BufferedImage src = new BufferedImage(1, 1, TYPE_3BYTE_BGR);
BufferedImage dst = new BufferedImage(1, 1, TYPE_3BYTE_BGR);
try {
op.filter(src.getRaster(), dst.getRaster());
} catch (CMMException e) {
throw new RuntimeException("Test failed.", e);
}
System.out.println("Test passed.");
}
static boolean testImage(int dataType, int rBits, int gBits, int bBits,
int cs, BufferedImage gldImage,
double accuracy)
{
BufferedImage src = ImageFactory.createCCMImage(cs, dataType);
BufferedImage dst = ImageFactory.createDstImage(
BufferedImage.TYPE_INT_RGB);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dst);
ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
bBits);
boolean result = cmp.compare(gldImage, dst);
if (!result) {
System.err.println(cmp.getStat());
}
return result;
}
public static void main(String[] args) {
ICC_Profile pSRGB = ICC_Profile.getInstance(CS_sRGB);
byte[] raw_data = pSRGB.getData();
setRenderingIntent(0x1000000, raw_data);
ICC_Profile p = ICC_Profile.getInstance(raw_data);
ICC_ColorSpace cs = new ICC_ColorSpace(p);
// perfrom test color conversion
ColorConvertOp op = new ColorConvertOp(cs,
ColorSpace.getInstance(CS_sRGB), null);
BufferedImage src = new BufferedImage(1, 1, TYPE_3BYTE_BGR);
BufferedImage dst = new BufferedImage(1, 1, TYPE_3BYTE_BGR);
try {
op.filter(src.getRaster(), dst.getRaster());
} catch (CMMException e) {
throw new RuntimeException("Test failed.", e);
}
System.out.println("Test passed.");
}
static boolean testImage(int type, int rBits, int gBits, int bBits,
int cs, BufferedImage gldImage,
double accuracy)
{
BufferedImage src = ImageFactory.createDCMImage(type, cs);
BufferedImage dst = ImageFactory.createDstImage(
BufferedImage.TYPE_INT_RGB);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dst);
ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
bBits);
boolean result = cmp.compare(gldImage, dst);
if (!result) {
System.err.println(cmp.getStat());
}
return result;
}
/**
* Converts the source image to 4-bit colour
* using the given colour map. No transparency.
* @param src the source image to convert
* @param cmap the colour map, which should contain no more than 16 entries
* The entries are in the form RRGGBB (hex).
* @return a copy of the source image with a 4-bit colour depth, with the custom colour pallette
*/
public static BufferedImage convert4(BufferedImage src, int[] cmap) {
IndexColorModel icm = new IndexColorModel(
4, cmap.length, cmap, 0, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE
);
BufferedImage dest = new BufferedImage(
src.getWidth(), src.getHeight(),
BufferedImage.TYPE_BYTE_BINARY,
icm
);
ColorConvertOp cco = new ColorConvertOp(
src.getColorModel().getColorSpace(),
dest.getColorModel().getColorSpace(),
null
);
cco.filter(src, dest);
return dest;
}
public static BufferedImage cmyk2rgb(Raster cmykRaster,
ICC_Profile cmykProfile) throws IOException {
if (cmykProfile == null) {
cmykProfile = ImageValue.eciCmykProfile();
}
ICC_ColorSpace cmykCS = new ICC_ColorSpace(cmykProfile);
BufferedImage rgbImage = new BufferedImage(cmykRaster.getWidth(), cmykRaster.getHeight(), BufferedImage.TYPE_INT_RGB);
WritableRaster rgbRaster = rgbImage.getRaster();
ColorSpace rgbCS = rgbImage.getColorModel().getColorSpace();
ColorConvertOp cmykToRgb = new ColorConvertOp(cmykCS, rgbCS, null);
cmykToRgb.filter(cmykRaster, rgbRaster);
return rgbImage;
}
public static void main(String[] args) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
// create source image filled with an opaque color
BufferedImage src = createSrc();
int srcAlpha = getAlpha(src);
System.out.printf("Src alpha: 0x%02x\n", srcAlpha);
// create clear (transparent black) destination image
BufferedImage dst = createDst();
int dstAlpha = getAlpha(dst);
System.out.printf("Dst alpha: 0x%02x\n", dstAlpha);
dst = op.filter(src, dst);
dstAlpha = getAlpha(dst);
// we expect that destination image is opaque
// i.e. alpha is transferred from source to
// the destination
System.out.printf("Result alpha: 0x%02x\n", dstAlpha);
if (srcAlpha != dstAlpha) {
throw new RuntimeException("Test failed!");
}
System.out.println("Test passed");
}
public static void main(String args[]) {
BufferedImage src
= new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB);
// Set src pixel values
Color pelColor = new Color(100, 100, 100, 128);
for (int i = 0; i < 10; i++) {
src.setRGB(0, i, pelColor.getRGB());
}
ColorModel cm = new ComponentColorModel
(ColorSpace.getInstance(ColorSpace.CS_GRAY),
new int [] {8,8}, true,
src.getColorModel().isAlphaPremultiplied(),
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
SampleModel sm = new PixelInterleavedSampleModel
(DataBuffer.TYPE_BYTE, 100, 100, 2, 200,
new int [] { 0, 1 });
WritableRaster wr = Raster.createWritableRaster(sm, new Point(0,0));
BufferedImage dst =
new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
dst = dst.getSubimage(0, 0, 1, 10);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dst);
for (int i = 0; i < 10; i++) {
if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) {
throw new RuntimeException(
"Incorrect destination alpha value.");
}
}
}
public void runTest(Object octx, int numReps) {
final Context ctx = (Context)octx;
final ColorConvertOp op = ctx.op_rst;
final Raster src = ctx.rsrc;
WritableRaster dst = ctx.rdst;
do {
try {
dst = op.filter(src, dst);
} catch (Exception e) {
e.printStackTrace();
}
} while (--numReps >= 0);
}
public static void main(String args[]) {
BufferedImage src
= new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB);
// Set src pixel values
Color pelColor = new Color(100, 100, 100, 128);
for (int i = 0; i < 10; i++) {
src.setRGB(0, i, pelColor.getRGB());
}
ColorModel cm = new ComponentColorModel
(ColorSpace.getInstance(ColorSpace.CS_GRAY),
new int [] {8,8}, true,
src.getColorModel().isAlphaPremultiplied(),
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
SampleModel sm = new PixelInterleavedSampleModel
(DataBuffer.TYPE_BYTE, 100, 100, 2, 200,
new int [] { 0, 1 });
WritableRaster wr = Raster.createWritableRaster(sm, new Point(0,0));
BufferedImage dst =
new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
dst = dst.getSubimage(0, 0, 1, 10);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dst);
for (int i = 0; i < 10; i++) {
if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) {
throw new RuntimeException(
"Incorrect destination alpha value.");
}
}
}
public void runTest(Object octx, int numReps) {
final Context ctx = (Context)octx;
final ColorConvertOp op = ctx.op_img;
final BufferedImage src = ctx.src;
BufferedImage dst = ctx.dst;
do {
try {
dst = op.filter(src, dst);
} catch (Exception e) {
e.printStackTrace();
}
} while (--numReps >= 0);
}
public void runTest(Object octx, int numReps) {
final Context ctx = (Context)octx;
final ColorConvertOp op = ctx.op_rst;
final Raster src = ctx.rsrc;
WritableRaster dst = ctx.rdst;
do {
try {
dst = op.filter(src, dst);
} catch (Exception e) {
e.printStackTrace();
}
} while (--numReps >= 0);
}
public BufferedImage filter(BufferedImage sourceImage, Object... args) {
float a = (Float) args[0];
BufferedImage heightMap = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
BufferedImage bumpMap = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
ColorConvertOp gscale = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
gscale.filter(sourceImage, heightMap);
for (int x = 0; x < bumpMap.getWidth(); x++) {
for (int y = 0; y < bumpMap.getHeight(); y++) {
bumpMap.setRGB(x, y, generateBumpPixel(heightMap, x, y, a));
}
}
return bumpMap;
}
/**
* Converts a given image into grayscalse
* @param src
* @return
*/
public static BufferedImage convertToGray(BufferedImage src){
ColorConvertOp grayOp = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
return grayOp.filter(src,null);
}