下面列出了怎么用java.awt.image.BufferedImageOp的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Draws a BufferedImage that is filtered with a BufferedImageOp.
* The rendering attributes applied include the clip, transform
* and composite attributes. This is equivalent to:
* <pre>
* img1 = op.filter(img, null);
* drawImage(img1, new AffineTransform(1f,0f,0f,1f,x,y), null);
* </pre>
* @param op The filter to be applied to the image before drawing.
* @param img The BufferedImage to be drawn.
* This method does nothing if <code>img</code> is null.
* @param x,y The location in user space where the image should be drawn.
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void drawImage(BufferedImage img,
BufferedImageOp op,
int x,
int y) {
if (img == null) {
return;
}
int srcWidth = img.getWidth(null);
int srcHeight = img.getHeight(null);
if (op != null) {
img = op.filter(img, null);
}
if (srcWidth <= 0 || srcHeight <= 0) {
return;
} else {
AffineTransform xform = new AffineTransform(1f,0f,0f,1f,x,y);
drawImageToPlatform(img, xform, null,
0, 0, srcWidth, srcHeight, false);
}
}
@Override
public void transformImage(SunGraphics2D sg, BufferedImage img,
BufferedImageOp op, int x, int y)
{
if (op != null) {
if (op instanceof AffineTransformOp) {
AffineTransformOp atop = (AffineTransformOp) op;
transformImage(sg, img, x, y,
atop.getTransform(),
atop.getInterpolationType());
return;
} else {
if (OGLBufImgOps.renderImageWithOp(sg, img, op, x, y)) {
return;
}
}
img = op.filter(img, null);
}
copyImage(sg, img, x, y, null);
}
public void drawImage(BufferedImage bImg,
BufferedImageOp op,
int x,
int y) {
if (bImg == null) {
return;
}
try {
imagepipe.transformImage(this, bImg, op, x, y);
} catch (InvalidPipeException e) {
try {
revalidateAll();
imagepipe.transformImage(this, bImg, op, x, y);
} catch (InvalidPipeException e2) {
// Still catching the exception; we are not yet ready to
// validate the surfaceData correctly. Fail for now and
// try again next time around.
}
} finally {
surfaceData.markDirty();
}
}
public void runTest(Object ctx, int numReps) {
ImageOpTests.Context ictx = (ImageOpTests.Context)ctx;
BufferedImageOp op = ictx.bufImgOp;
BufferedImage src = ictx.bufSrc;
BufferedImage dst = ictx.bufDst;
if (ictx.touchSrc) {
Graphics gSrc = src.getGraphics();
do {
gSrc.fillRect(0, 0, 1, 1);
op.filter(src, dst);
} while (--numReps > 0);
} else {
do {
op.filter(src, dst);
} while (--numReps > 0);
}
}
public static void doTest(BufferedImageOp op) {
BufferedImage src = createSrcImage();
BufferedImage dst = createImage();
BufferedImage ret = null;
try {
ret = ImagingLib.filter(op, src, dst);
} catch (Exception e) {
throw new RuntimeException("Test FAILED.", e);
}
if (ret == null) {
throw new RuntimeException("Test FAILED: null output");
}
System.out.println("ret: " + ret);
System.out.println("Test PASSED for " + op.getClass().getName());
}
public void transformImage(SunGraphics2D sg, BufferedImage img,
BufferedImageOp op, int x, int y)
{
if (op != null) {
if (op instanceof AffineTransformOp) {
AffineTransformOp atop = (AffineTransformOp) op;
transformImage(sg, img, x, y,
atop.getTransform(),
atop.getInterpolationType());
return;
} else {
img = op.filter(img, null);
}
}
copyImage(sg, img, x, y, null);
}
public static void doTest(BufferedImageOp op) {
BufferedImage src = createSrcImage();
BufferedImage dst = createImage();
BufferedImage ret = null;
try {
ret = ImagingLib.filter(op, src, dst);
} catch (Exception e) {
throw new RuntimeException("Test FAILED.", e);
}
if (ret == null) {
throw new RuntimeException("Test FAILED: null output");
}
System.out.println("ret: " + ret);
System.out.println("Test PASSED for " + op.getClass().getName());
}
@Nonnull
public static Image scaleImage(Image image, double scale) {
if (scale == 1.0) return image;
if (image instanceof JBHiDPIScaledImage) {
return ((JBHiDPIScaledImage)image).scale(scale);
}
int w = image.getWidth(null);
int h = image.getHeight(null);
if (w <= 0 || h <= 0) {
return image;
}
int width = (int)Math.round(scale * w);
int height = (int)Math.round(scale * h);
// Using "QUALITY" instead of "ULTRA_QUALITY" results in images that are less blurry
// because ultra quality performs a few more passes when scaling, which introduces blurriness
// when the scaling factor is relatively small (i.e. <= 3.0f) -- which is the case here.
return Scalr.resize(ImageUtil.toBufferedImage(image), Scalr.Method.QUALITY, Scalr.Mode.FIT_EXACT, width, height, (BufferedImageOp[])null);
}
/**
* Draws a BufferedImage that is filtered with a BufferedImageOp.
* The rendering attributes applied include the clip, transform
* and composite attributes. This is equivalent to:
* <pre>
* img1 = op.filter(img, null);
* drawImage(img1, new AffineTransform(1f,0f,0f,1f,x,y), null);
* </pre>
* @param op The filter to be applied to the image before drawing.
* @param img The BufferedImage to be drawn.
* This method does nothing if <code>img</code> is null.
* @param x,y The location in user space where the image should be drawn.
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void drawImage(BufferedImage img,
BufferedImageOp op,
int x,
int y) {
if (img == null) {
return;
}
int srcWidth = img.getWidth(null);
int srcHeight = img.getHeight(null);
if (op != null) {
img = op.filter(img, null);
}
if (srcWidth <= 0 || srcHeight <= 0) {
return;
} else {
AffineTransform xform = new AffineTransform(1f,0f,0f,1f,x,y);
drawImageToPlatform(img, xform, null,
0, 0, srcWidth, srcHeight, false);
}
}
public void runTest(Object ctx, int numReps) {
ImageOpTests.Context ictx = (ImageOpTests.Context)ctx;
BufferedImageOp op = ictx.bufImgOp;
BufferedImage src = ictx.bufSrc;
BufferedImage dst = ictx.bufDst;
if (ictx.touchSrc) {
Graphics gSrc = src.getGraphics();
do {
gSrc.fillRect(0, 0, 1, 1);
op.filter(src, dst);
} while (--numReps > 0);
} else {
do {
op.filter(src, dst);
} while (--numReps > 0);
}
}
@Override
public void transformImage(SunGraphics2D sg, BufferedImage img,
BufferedImageOp op, int x, int y)
{
if (op != null) {
if (op instanceof AffineTransformOp) {
AffineTransformOp atop = (AffineTransformOp) op;
transformImage(sg, img, x, y,
atop.getTransform(),
atop.getInterpolationType());
return;
} else {
if (OGLBufImgOps.renderImageWithOp(sg, img, op, x, y)) {
return;
}
}
img = op.filter(img, null);
}
copyImage(sg, img, x, y, null);
}
public static void doTest(BufferedImageOp op) {
BufferedImage src = createSrcImage();
BufferedImage dst = createImage();
BufferedImage ret = null;
try {
ret = ImagingLib.filter(op, src, dst);
} catch (Exception e) {
throw new RuntimeException("Test FAILED.", e);
}
if (ret == null) {
throw new RuntimeException("Test FAILED: null output");
}
System.out.println("ret: " + ret);
System.out.println("Test PASSED for " + op.getClass().getName());
}
public void runTest(Object ctx, int numReps) {
ImageOpTests.Context ictx = (ImageOpTests.Context)ctx;
BufferedImageOp op = ictx.bufImgOp;
BufferedImage src = ictx.bufSrc;
BufferedImage dst = ictx.bufDst;
if (ictx.touchSrc) {
Graphics gSrc = src.getGraphics();
do {
gSrc.fillRect(0, 0, 1, 1);
op.filter(src, dst);
} while (--numReps > 0);
} else {
do {
op.filter(src, dst);
} while (--numReps > 0);
}
}
public void transformImage(SunGraphics2D sg, BufferedImage img,
BufferedImageOp op, int x, int y)
{
if (op != null) {
if (op instanceof AffineTransformOp) {
AffineTransformOp atop = (AffineTransformOp) op;
transformImage(sg, img, x, y,
atop.getTransform(),
atop.getInterpolationType());
return;
} else {
img = op.filter(img, null);
}
}
copyImage(sg, img, x, y, null);
}
private static BufferedImageOp getLookupOp() {
byte[] inv = new byte[256];
for (int i = 0; i < 256; i++) {
inv[i] = (byte)(255 - i);
}
ByteLookupTable table = new ByteLookupTable(0, inv);
return new LookupOp(table, null);
}
public static void main(String[] args) {
BufferedImageOp op = createTestOp();
try {
System.out.print("Integer-based images... ");
doTest(op, TYPE_INT_ARGB, TYPE_INT_ARGB_PRE);
System.out.println("done.");
System.out.print("Byte-based images... ");
doTest(op, TYPE_4BYTE_ABGR, TYPE_4BYTE_ABGR_PRE);
System.out.println("done");
} catch (ImagingOpException e) {
throw new RuntimeException("Test FAILED", e);
}
}
/**
* @see Scalr#crop(BufferedImage, int, int, BufferedImageOp...)
*/
public static Future<BufferedImage> crop(final BufferedImage src,
final int width, final int height, final BufferedImageOp... ops)
throws IllegalArgumentException, ImagingOpException {
checkService();
return service.submit(new Callable<BufferedImage>() {
public BufferedImage call() throws Exception {
return Scalr.crop(src, width, height, ops);
}
});
}
private static BufferedImageOp createTestOp() {
final int size = 1;
final float v = 1f / (size * size);
final float[] k_data = new float[size * size];
Arrays.fill(k_data, v);
Kernel k = new Kernel(size, size, k_data);
return new ConvolveOp(k);
}
/**
* @see Scalr#resize(BufferedImage, Mode, int, BufferedImageOp...)
*/
public static Future<BufferedImage> resize(final BufferedImage src,
final Mode resizeMode, final int targetSize,
final BufferedImageOp... ops) throws IllegalArgumentException,
ImagingOpException {
checkService();
return service.submit(new Callable<BufferedImage>() {
public BufferedImage call() throws Exception {
return Scalr.resize(src, resizeMode, targetSize, ops);
}
});
}
@Override
public BufferedImage doTransform(BufferedImage src, BufferedImage dest) {
int numRedLevels = redLevels.getValue();
int numGreenLevels = greenLevels.getValue();
int numBlueLevels = blueLevels.getValue();
var rgbLookup = new RGBLookup();
rgbLookup.initFromPosterize(numRedLevels, numGreenLevels, numBlueLevels);
BufferedImageOp filterOp = new FastLookupOp((ShortLookupTable) rgbLookup.getLookupOp());
filterOp.filter(src, dest);
return dest;
}
/**
* @see Scalr#apply(BufferedImage, BufferedImageOp...)
*/
public static Future<BufferedImage> apply(final BufferedImage src, final BufferedImageOp... ops) throws IllegalArgumentException, ImagingOpException {
checkService();
return service.submit(new Callable<BufferedImage>() {
public BufferedImage call() throws Exception {
return Scalr.apply(src, ops);
}
});
}
/**
* @see Scalr#resize(BufferedImage, Method, int, int, BufferedImageOp...)
*/
public static Future<BufferedImage> resize(final BufferedImage src, final Method scalingMethod, final int targetWidth, final int targetHeight, final BufferedImageOp... ops) {
checkService();
return service.submit(new Callable<BufferedImage>() {
public BufferedImage call() throws Exception {
return Scalr.resize(src, scalingMethod, targetWidth, targetHeight, ops);
}
});
}
private static BufferedImageOp getFilter() {
var f = new KaleidoscopeFilter("Kaleidoscope Test");
f.setZoom(1.0f);
f.setProgressTracker(ProgressTracker.NULL_TRACKER);
return f;
}
public static void enableBufImgOp(RenderQueue rq, SurfaceData srcData,
BufferedImage srcImg,
BufferedImageOp biop)
{
if (biop instanceof ConvolveOp) {
enableConvolveOp(rq, srcData, (ConvolveOp)biop);
} else if (biop instanceof RescaleOp) {
enableRescaleOp(rq, srcData, srcImg, (RescaleOp)biop);
} else if (biop instanceof LookupOp) {
enableLookupOp(rq, srcData, srcImg, (LookupOp)biop);
} else {
throw new InternalError("Unknown BufferedImageOp");
}
}
private static BufferedImageOp getLookupOp() {
byte[] inv = new byte[256];
for (int i = 0; i < 256; i++) {
inv[i] = (byte)(255 - i);
}
ByteLookupTable table = new ByteLookupTable(0, inv);
return new LookupOp(table, null);
}
/**
* @see Scalr#resize(BufferedImage, int, int, BufferedImageOp...)
*/
public static Future<BufferedImage> resize(final BufferedImage src, final int targetWidth, final int targetHeight,
final BufferedImageOp... ops) throws IllegalArgumentException, ImagingOpException {
checkService();
return service.submit(new Callable<BufferedImage>() {
public BufferedImage call() throws Exception {
return Scalr.resize(src, targetWidth, targetHeight, ops);
}
});
}
/**
* @see Scalr#rotate(BufferedImage, Rotation, BufferedImageOp...)
*/
public static Future<BufferedImage> rotate(final BufferedImage src, final Rotation rotation,
final BufferedImageOp... ops) throws IllegalArgumentException, ImagingOpException {
checkService();
return service.submit(new Callable<BufferedImage>() {
public BufferedImage call() throws Exception {
return Scalr.rotate(src, rotation, ops);
}
});
}
/**
* @see Scalr#resize(BufferedImage, int, int, BufferedImageOp...)
*/
public static Future<BufferedImage> resize(final BufferedImage src,
final int targetWidth, final int targetHeight,
final BufferedImageOp... ops) throws IllegalArgumentException,
ImagingOpException {
checkService();
return service.submit(new Callable<BufferedImage>() {
public BufferedImage call() throws Exception {
return Scalr.resize(src, targetWidth, targetHeight, ops);
}
});
}
public static void main(String[] args) {
System.out.println("AffineTransformOp:");
BufferedImageOp op = getAffineTransformOp();
doTest(op);
System.out.println("ConvolveOp:");
op = getConvolveOp();
doTest(op);
System.out.println("LookupOp:");
op = getLookupOp();
doTest(op);
}
/**
* @see Graphics2D#drawImage(BufferedImage, BufferedImageOp, int, int)
*/
@Override
public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
BufferedImage result = img;
if (op != null) {
result = op.createCompatibleDestImage(img, img.getColorModel());
result = op.filter(img, result);
}
drawImage(result, x, y, null);
}