下面列出了怎么用java.awt.image.RescaleOp的API类实例代码及写法,或者点击链接到github查看源代码。
public void applyDrawDecay(float factor, boolean bRedrawPointImg){
//System.out.println("applyDrawDecay: factor="+factor);
// 1)
int v = Color.GRAY.getRed();
//System.out.println("applyDrawDecay: v="+v);
RescaleOp brightenOp = new RescaleOp(1f, (255-v)*factor, null);
// 2)
//RescaleOp brightenOp = new RescaleOp(1f + factor, 0, null);
// 3)
//RescaleOp brightenOp = new RescaleOp(1f, (255)*factor, null);
pointImg = brightenOp.filter(pointImg, null);
if (bRedrawPointImg) {
ApplyToCanvas(pointImg);
RedrawPointLayer();
}
}
/**
* Create a faded version of an image.
*
* @param img The {@code Image} to fade.
* @param fade The amount of fading.
* @param target The offset.
* @return The faded image.
*/
public static BufferedImage fadeImage(Image img, float fade, float target) {
int w = img.getWidth(null);
int h = img.getHeight(null);
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.drawImage(img, 0, 0, null);
float offset = target * (1.0f - fade);
float[] scales = { fade, fade, fade, 1.0f };
float[] offsets = { offset, offset, offset, 0.0f };
RescaleOp rop = new RescaleOp(scales, offsets, null);
g.drawImage(bi, rop, 0, 0);
g.dispose();
return bi;
}
private void paintBlock(Graphics2D g2, int baseLine, int middle, int x, int y, int depth, Material material) {
if (material.blockType == Constants.BLK_AIR) {
return;
}
if (texturePack != null) {
if (depth > 0) {
RescaleOp rescaleOp = new RescaleOp(new float[] {1.0f, 1.0f, 1.0f, (float) Math.pow(2.0, -depth)}, new float[] {0.0f, 0.0f, 0.0f, 0.0f}, null);
g2.drawImage(getImage(material, texturePack), rescaleOp, middle + x * 16 - 8, baseLine - y * 16 - 8);
} else {
g2.drawImage(getImage(material, texturePack), middle + x * 16 - 8, baseLine - y * 16 - 8, null);
}
} else {
int colour = colourScheme.getColour(material);
if (depth > 0) {
colour = ColourUtils.mix(colour, WHITE, (int) (Math.pow(0.5, depth) * 256 + 0.5));
}
g2.setColor(new Color(colour));
g2.fillRect(middle + x * 16 - 8, baseLine - y * 16 - 8, 16, 16);
}
}
private void runTest(int sType, int dType, int expect) {
BufferedImage src = new BufferedImage(w, h, sType);
BufferedImage dst = new BufferedImage(w, h, dType);
String msg = getMsgText(sType, dType);
Graphics2D g2d = src.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, w, h);
RescaleOp res = new RescaleOp(scaleFactor, offset, null);
res.filter(src, dst);
if (saveImage) {
try {
String fname = getFileName(sType, dType);
ImageIO.write(dst, "png", new File(fname));
} catch (IOException e) {
}
}
check(dst, expect, msg);
}
@Override
protected Icon getIcon ()
{
// TODO Use that to get the state (-> highlight or not)
TransitionAwareUI transitionAwareUI = (TransitionAwareUI) slider.getUI();
StateTransitionTracker stateTransitionTracker = transitionAwareUI.getTransitionTracker();
// stateTransitionTracker.getModelStateInfo().getCurrModelState();
final Icon icon = super.getIcon();
final BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
final Graphics iconGraphics = image.createGraphics();
icon.paintIcon(slider, iconGraphics, 0, 0);
// Make it brighter (very simple approach)
final RescaleOp rescaleOp = new RescaleOp(2.0f, 50, null);
rescaleOp.filter(image, image);
ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
op.filter(image, image);
return new ImageIcon(image);
}
/**
* Prepare a version of the place holder Sprite, that is suitable for the
* transparency mode of the client.
*
* @param original original placeholder Sprite
* @return an adjusted Sprite, or the original if no adjusting is needed
*/
private Sprite preparePlaceholder(Sprite original) {
if ((original == null) || (TransparencyMode.TRANSPARENCY == Transparency.BITMASK)) {
return original;
}
/*
* Using full alpha in the client.
*
* Create a black and white, but translucent version of the same image.
* The filtering has been chosen so that the slot images we use become
* suitably B&W, not for any general rule.
*
* What we'd really want is drawing an opaque B&W image in soft light
* mode, but swing back buffer does not actually support Composites
* despite being accessed via Graphics2D.
*/
BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = img.createGraphics();
original.draw(g, 0, 0);
RescaleOp rescaleOp = new RescaleOp(new float[] {3.0f, 3.0f, 3.0f, 0.5f }, new float[] {-450f, -450f, -450f, 0f}, null);
rescaleOp.filter(img, img);
g.dispose();
return new ImageSprite(img);
}
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");
}
}
public static void disableBufImgOp(RenderQueue rq, BufferedImageOp biop) {
if (biop instanceof ConvolveOp) {
disableConvolveOp(rq);
} else if (biop instanceof RescaleOp) {
disableRescaleOp(rq);
} else if (biop instanceof LookupOp) {
disableLookupOp(rq);
} else {
throw new InternalError("Unknown BufferedImageOp");
}
}
/**************************** RescaleOp support *****************************/
public static boolean isRescaleOpValid(RescaleOp rop,
BufferedImage srcImg)
{
int numFactors = rop.getNumFactors();
ColorModel srcCM = srcImg.getColorModel();
if (srcCM instanceof IndexColorModel) {
throw new
IllegalArgumentException("Rescaling cannot be "+
"performed on an indexed image");
}
if (numFactors != 1 &&
numFactors != srcCM.getNumColorComponents() &&
numFactors != srcCM.getNumComponents())
{
throw new IllegalArgumentException("Number of scaling constants "+
"does not equal the number of"+
" of color or color/alpha "+
" components");
}
int csType = srcCM.getColorSpace().getType();
if (csType != ColorSpace.TYPE_RGB &&
csType != ColorSpace.TYPE_GRAY)
{
// Not prepared to deal with other color spaces
return false;
}
if (numFactors == 2 || numFactors > 4) {
// Not really prepared to handle this at the native level, so...
return false;
}
return true;
}
/**************************** RescaleOp support *****************************/
public static boolean isRescaleOpValid(RescaleOp rop,
BufferedImage srcImg)
{
int numFactors = rop.getNumFactors();
ColorModel srcCM = srcImg.getColorModel();
if (srcCM instanceof IndexColorModel) {
throw new
IllegalArgumentException("Rescaling cannot be "+
"performed on an indexed image");
}
if (numFactors != 1 &&
numFactors != srcCM.getNumColorComponents() &&
numFactors != srcCM.getNumComponents())
{
throw new IllegalArgumentException("Number of scaling constants "+
"does not equal the number of"+
" of color or color/alpha "+
" components");
}
int csType = srcCM.getColorSpace().getType();
if (csType != ColorSpace.TYPE_RGB &&
csType != ColorSpace.TYPE_GRAY)
{
// Not prepared to deal with other color spaces
return false;
}
if (numFactors == 2 || numFactors > 4) {
// Not really prepared to handle this at the native level, so...
return false;
}
return true;
}
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");
}
}
public static void disableBufImgOp(RenderQueue rq, BufferedImageOp biop) {
if (biop instanceof ConvolveOp) {
disableConvolveOp(rq);
} else if (biop instanceof RescaleOp) {
disableRescaleOp(rq);
} else if (biop instanceof LookupOp) {
disableLookupOp(rq);
} else {
throw new InternalError("Unknown BufferedImageOp");
}
}
/**************************** RescaleOp support *****************************/
public static boolean isRescaleOpValid(RescaleOp rop,
BufferedImage srcImg)
{
int numFactors = rop.getNumFactors();
ColorModel srcCM = srcImg.getColorModel();
if (srcCM instanceof IndexColorModel) {
throw new
IllegalArgumentException("Rescaling cannot be "+
"performed on an indexed image");
}
if (numFactors != 1 &&
numFactors != srcCM.getNumColorComponents() &&
numFactors != srcCM.getNumComponents())
{
throw new IllegalArgumentException("Number of scaling constants "+
"does not equal the number of"+
" of color or color/alpha "+
" components");
}
int csType = srcCM.getColorSpace().getType();
if (csType != ColorSpace.TYPE_RGB &&
csType != ColorSpace.TYPE_GRAY)
{
// Not prepared to deal with other color spaces
return false;
}
if (numFactors == 2 || numFactors > 4) {
// Not really prepared to handle this at the native level, so...
return false;
}
return true;
}
private static ImageIcon makeRolloverIcon(ImageIcon srcIcon) {
int w = srcIcon.getIconWidth();
int h = srcIcon.getIconHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
srcIcon.paintIcon(null, g2, 0, 0);
float[] scaleFactors = {.5f, .5f, .5f, 1f};
float[] offsets = {0f, 0f, 0f, 0f};
RescaleOp op = new RescaleOp(scaleFactors, offsets, g2.getRenderingHints());
g2.dispose();
return new ImageIcon(op.filter(img, null));
}
/**************************** RescaleOp support *****************************/
public static boolean isRescaleOpValid(RescaleOp rop,
BufferedImage srcImg)
{
int numFactors = rop.getNumFactors();
ColorModel srcCM = srcImg.getColorModel();
if (srcCM instanceof IndexColorModel) {
throw new
IllegalArgumentException("Rescaling cannot be "+
"performed on an indexed image");
}
if (numFactors != 1 &&
numFactors != srcCM.getNumColorComponents() &&
numFactors != srcCM.getNumComponents())
{
throw new IllegalArgumentException("Number of scaling constants "+
"does not equal the number of"+
" of color or color/alpha "+
" components");
}
int csType = srcCM.getColorSpace().getType();
if (csType != ColorSpace.TYPE_RGB &&
csType != ColorSpace.TYPE_GRAY)
{
// Not prepared to deal with other color spaces
return false;
}
if (numFactors == 2 || numFactors > 4) {
// Not really prepared to handle this at the native level, so...
return false;
}
return true;
}
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");
}
}
public static void disableBufImgOp(RenderQueue rq, BufferedImageOp biop) {
if (biop instanceof ConvolveOp) {
disableConvolveOp(rq);
} else if (biop instanceof RescaleOp) {
disableRescaleOp(rq);
} else if (biop instanceof LookupOp) {
disableLookupOp(rq);
} else {
throw new InternalError("Unknown BufferedImageOp");
}
}
/**************************** RescaleOp support *****************************/
public static boolean isRescaleOpValid(RescaleOp rop,
BufferedImage srcImg)
{
int numFactors = rop.getNumFactors();
ColorModel srcCM = srcImg.getColorModel();
if (srcCM instanceof IndexColorModel) {
throw new
IllegalArgumentException("Rescaling cannot be "+
"performed on an indexed image");
}
if (numFactors != 1 &&
numFactors != srcCM.getNumColorComponents() &&
numFactors != srcCM.getNumComponents())
{
throw new IllegalArgumentException("Number of scaling constants "+
"does not equal the number of"+
" of color or color/alpha "+
" components");
}
int csType = srcCM.getColorSpace().getType();
if (csType != ColorSpace.TYPE_RGB &&
csType != ColorSpace.TYPE_GRAY)
{
// Not prepared to deal with other color spaces
return false;
}
if (numFactors == 2 || numFactors > 4) {
// Not really prepared to handle this at the native level, so...
return false;
}
return true;
}
@Override
public void paint(Graphics g)
{
RescaleOp ro = new RescaleOp(0.6f, 0, null);
tempImage = ro.filter(image, null);
g.drawImage(tempImage, 0, 0, this);
if (!isShown)
{
setOpacity(1);
isShown = true;
}
}
@Override
public void setAlpha(double alpha) {
if (img == null) return;
Dimension dim = getDimension();
BufferedImage newImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImg.createGraphics();
RescaleOp op = new RescaleOp(new float[]{1.0f, 1.0f, 1.0f, (float)alpha}, new float[]{0,0,0,0}, null);
g.drawImage(img, op, 0, 0);
g.dispose();
img = newImg;
}
@Override
public void paint(Graphics g)
{
RescaleOp ro = new RescaleOp(0.8f, 0, null);
tempImage = ro.filter(image, null);
g.drawImage(tempImage, 0, 0, this);
}
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");
}
}
public static void disableBufImgOp(RenderQueue rq, BufferedImageOp biop) {
if (biop instanceof ConvolveOp) {
disableConvolveOp(rq);
} else if (biop instanceof RescaleOp) {
disableRescaleOp(rq);
} else if (biop instanceof LookupOp) {
disableLookupOp(rq);
} else {
throw new InternalError("Unknown BufferedImageOp");
}
}
/**
* Centers the given Image on the tile.
*
* @param g a {@code Graphics2D}
* @param image the BufferedImage
* @param rop An optional RescaleOp for fog of war.
*/
public void displayCenteredImage(Graphics2D g, BufferedImage image,
RescaleOp rop) {
g.drawImage(
image,
rop,
(tileWidth - image.getWidth())/2,
(tileHeight - image.getHeight())/2);
}
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");
}
}
public static void disableBufImgOp(RenderQueue rq, BufferedImageOp biop) {
if (biop instanceof ConvolveOp) {
disableConvolveOp(rq);
} else if (biop instanceof RescaleOp) {
disableRescaleOp(rq);
} else if (biop instanceof LookupOp) {
disableLookupOp(rq);
} else {
throw new InternalError("Unknown BufferedImageOp");
}
}
public void applyDrawDecay(float factor){
RescaleOp brightenOp = new RescaleOp(1f, 150f/factor, null);
pointCanvas = brightenOp.filter(pointCanvas, null);
layerPointCanvas.setImage(pointCanvas);
layerPointCanvas.repaint();
}
public static void disableBufImgOp(RenderQueue rq, BufferedImageOp biop) {
if (biop instanceof ConvolveOp) {
disableConvolveOp(rq);
} else if (biop instanceof RescaleOp) {
disableRescaleOp(rq);
} else if (biop instanceof LookupOp) {
disableLookupOp(rq);
} else {
throw new InternalError("Unknown BufferedImageOp");
}
}
/**************************** RescaleOp support *****************************/
public static boolean isRescaleOpValid(RescaleOp rop,
BufferedImage srcImg)
{
int numFactors = rop.getNumFactors();
ColorModel srcCM = srcImg.getColorModel();
if (srcCM instanceof IndexColorModel) {
throw new
IllegalArgumentException("Rescaling cannot be "+
"performed on an indexed image");
}
if (numFactors != 1 &&
numFactors != srcCM.getNumColorComponents() &&
numFactors != srcCM.getNumComponents())
{
throw new IllegalArgumentException("Number of scaling constants "+
"does not equal the number of"+
" of color or color/alpha "+
" components");
}
int csType = srcCM.getColorSpace().getType();
if (csType != ColorSpace.TYPE_RGB &&
csType != ColorSpace.TYPE_GRAY)
{
// Not prepared to deal with other color spaces
return false;
}
if (numFactors == 2 || numFactors > 4) {
// Not really prepared to handle this at the native level, so...
return false;
}
return true;
}
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");
}
}