use of java.awt.image.BandCombineOp in project digilib by robcast.
the class ImageLoaderDocuImage method colorOp.
/*
* (non-Javadoc)
*
* @see
* digilib.image.DocuImageImpl#colorOp(digilib.image.DocuImage.ColorOps)
*/
public void colorOp(ColorOp colop) throws ImageOpException {
if (colop == ColorOp.GRAYSCALE) {
/*
* convert image to grayscale
*/
logger.debug("Color op: grayscaling");
ColorModel cm = img.getColorModel();
if (cm.getNumColorComponents() < 3) {
// grayscale already
logger.debug("Color op: not grayscaling");
return;
}
ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), renderHint);
// let filter create new image
img = op.filter(img, null);
} else if (colop == ColorOp.NTSC_GRAY) {
/*
* convert image to grayscale NTSC-style: luminance = 0.2989*red +
* 0.5870*green + 0.1140*blue
*/
logger.debug("Color op: NTSC gray");
logger.debug("img={}", img);
ColorModel cm = img.getColorModel();
if (cm.getNumColorComponents() < 3 || cm instanceof IndexColorModel) {
// grayscale already or not possible
logger.debug("Color op: unable to NTSC gray");
return;
}
float[][] combineFn = new float[1][4];
combineFn[0] = new float[] { 0.299f, 0.587f, 0.114f, 0f };
BandCombineOp op = new BandCombineOp(combineFn, renderHint);
// BandCombineOp only works on Rasters so we create a
// new image and use its Raster
BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
op.filter(img.getRaster(), dest.getRaster());
img = dest;
} else if (colop == ColorOp.BITONAL) {
/*
* convert image to bitonal black and white
* (nothing clever is done)
*/
logger.debug("Color op: bitonal");
logger.debug("img={}", img);
BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
dest.createGraphics().drawImage(img, null, 0, 0);
img = dest;
logger.debug("bitonal img={}", img);
} else if (colop == ColorOp.INVERT) {
/*
* invert colors i.e. invert every channel
*/
logger.debug("Color op: inverting");
LookupTable invtbl = null;
ColorModel cm = img.getColorModel();
if (cm instanceof IndexColorModel) {
// invert not possible
// TODO: should we convert?
logger.debug("Color op: unable to invert");
return;
}
if (imageHacks.get(Hacks.needsInvertRgba) && cm.hasAlpha()) {
// fix for some cases
invtbl = invertRgbaByteTable;
} else {
invtbl = invertSingleByteTable;
}
LookupOp op = new LookupOp(invtbl, renderHint);
logger.debug("colop: image={}", img);
op.filter(img, img);
} else if (colop == ColorOp.MAP_GRAY_BGR) {
/*
* false color image from grayscale (0: blue, 128: green, 255: red)
*/
logger.debug("Color op: map_gray_bgr");
// convert to grayscale
ColorConvertOp grayOp = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), renderHint);
// create new 3-channel image
int destType = BufferedImage.TYPE_INT_RGB;
if (imageHacks.get(Hacks.needsMapBgr)) {
// special case for funny Java2D implementations
if (img.getColorModel().hasAlpha()) {
destType = BufferedImage.TYPE_4BYTE_ABGR_PRE;
} else {
destType = BufferedImage.TYPE_3BYTE_BGR;
}
}
BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), destType);
img = grayOp.filter(img, dest);
logger.debug("map_gray: image={}", img);
// convert to false color
LookupOp mapOp = new LookupOp(mapBgrByteTable, renderHint);
mapOp.filter(img, img);
logger.debug("mapped image={}", img);
}
}
use of java.awt.image.BandCombineOp in project MeteoInfo by meteoinfo.
the class ShadowFilter method filter.
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
int width = src.getWidth();
int height = src.getHeight();
float xOffset = distance * (float) Math.cos(angle);
float yOffset = -distance * (float) Math.sin(angle);
if (dst == null) {
if (addMargins) {
ColorModel cm = src.getColorModel();
dst = new BufferedImage(cm, cm.createCompatibleWritableRaster(src.getWidth() + (int) (Math.abs(xOffset) + radius), src.getHeight() + (int) (Math.abs(yOffset) + radius)), cm.isAlphaPremultiplied(), null);
} else
dst = createCompatibleDestImage(src, null);
}
float shadowR = ((shadowColor >> 16) & 0xff) / 255f;
float shadowG = ((shadowColor >> 8) & 0xff) / 255f;
float shadowB = (shadowColor & 0xff) / 255f;
// Make a black mask from the image's alpha channel
float[][] extractAlpha = { { 0, 0, 0, shadowR }, { 0, 0, 0, shadowG }, { 0, 0, 0, shadowB }, { 0, 0, 0, opacity } };
BufferedImage shadow = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
new BandCombineOp(extractAlpha, null).filter(src.getRaster(), shadow.getRaster());
shadow = new GaussianFilter(radius).filter(shadow, null);
Graphics2D g = dst.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
if (addMargins) {
float radius2 = radius / 2;
float topShadow = Math.max(0, radius - yOffset);
float leftShadow = Math.max(0, radius - xOffset);
g.translate(leftShadow, topShadow);
}
g.drawRenderedImage(shadow, AffineTransform.getTranslateInstance(xOffset, yOffset));
if (!shadowOnly) {
g.setComposite(AlphaComposite.SrcOver);
g.drawRenderedImage(src, null);
}
g.dispose();
return dst;
}
use of java.awt.image.BandCombineOp in project vassal by vassalengine.
the class SymbolSet method generateAlphaMask.
/**
* Convert a black-and-white bitmap to a mask image.
*/
private static BufferedImage generateAlphaMask(BufferedImage img) {
final BandCombineOp op;
if (img.getSampleModel().getNumBands() == 1)
op = new BandCombineOp(ONE_BAND_MATRIX, null);
else
op = new BandCombineOp(THREE_BAND_MATRIX, null);
final BufferedImage bi = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
op.filter(img.getRaster(), bi.getRaster());
return bi;
}
use of java.awt.image.BandCombineOp in project Lucee by lucee.
the class ShadowFilter method filter.
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
int width = src.getWidth();
int height = src.getHeight();
if (dst == null) {
if (addMargins) {
ColorModel cm = src.getColorModel();
dst = new BufferedImage(cm, cm.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), cm.isAlphaPremultiplied(), null);
} else
dst = createCompatibleDestImage(src, null);
}
float shadowR = ((shadowColor >> 16) & 0xff) / 255f;
float shadowG = ((shadowColor >> 8) & 0xff) / 255f;
float shadowB = (shadowColor & 0xff) / 255f;
// Make a black mask from the image's alpha channel
float[][] extractAlpha = { { 0, 0, 0, shadowR }, { 0, 0, 0, shadowG }, { 0, 0, 0, shadowB }, { 0, 0, 0, opacity } };
BufferedImage shadow = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
new BandCombineOp(extractAlpha, null).filter(src.getRaster(), shadow.getRaster());
shadow = new GaussianFilter(radius).filter(shadow, (BufferedImage) null);
float xOffset = distance * (float) Math.cos(angle);
float yOffset = -distance * (float) Math.sin(angle);
Graphics2D g = dst.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
if (addMargins) {
// float radius2 = radius/2;
float topShadow = Math.max(0, radius - yOffset);
float leftShadow = Math.max(0, radius - xOffset);
g.translate(topShadow, leftShadow);
}
g.drawRenderedImage(shadow, AffineTransform.getTranslateInstance(xOffset, yOffset));
if (!shadowOnly) {
g.setComposite(AlphaComposite.SrcOver);
g.drawRenderedImage(src, null);
}
g.dispose();
return dst;
}
use of java.awt.image.BandCombineOp in project Pixelitor by lbalazscs.
the class ShadowFilter method filter.
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
int width = src.getWidth();
int height = src.getHeight();
float xOffset = distance * (float) Math.cos(angle);
float yOffset = -distance * (float) Math.sin(angle);
if (dst == null) {
if (addMargins) {
ColorModel cm = src.getColorModel();
dst = new BufferedImage(cm, cm.createCompatibleWritableRaster(src.getWidth() + (int) (Math.abs(xOffset) + radius), src.getHeight() + (int) (Math.abs(yOffset) + radius)), cm.isAlphaPremultiplied(), null);
} else {
dst = createCompatibleDestImage(src, null);
}
}
float shadowR = ((shadowColor >> 16) & 0xff) / 255.0f;
float shadowG = ((shadowColor >> 8) & 0xff) / 255.0f;
float shadowB = (shadowColor & 0xff) / 255.0f;
// Make a black mask from the image's alpha channel
float[][] extractAlpha = { { 0, 0, 0, shadowR }, { 0, 0, 0, shadowG }, { 0, 0, 0, shadowB }, { 0, 0, 0, opacity } };
BufferedImage shadow = new BufferedImage(width, height, TYPE_INT_ARGB);
new BandCombineOp(extractAlpha, null).filter(src.getRaster(), shadow.getRaster());
// shadow = new GaussianFilter(radius, filterName).filter(shadow, null);
shadow = new BoxBlurFilter(radius, radius, 3, filterName).filter(shadow, null);
Graphics2D g = dst.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
if (addMargins) {
// float radius2 = radius / 2;
float topShadow = Math.max(0, radius - yOffset);
float leftShadow = Math.max(0, radius - xOffset);
g.translate(leftShadow, topShadow);
}
g.drawRenderedImage(shadow, AffineTransform.getTranslateInstance(xOffset, yOffset));
if (!shadowOnly) {
g.setComposite(AlphaComposite.SrcOver);
g.drawRenderedImage(src, null);
}
g.dispose();
return dst;
}
Aggregations