use of java.awt.image.ColorModel in project Lucee by lucee.
the class BicubicScaleFilter method filter.
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
if (dst == null) {
ColorModel dstCM = src.getColorModel();
dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(width, height), dstCM.isAlphaPremultiplied(), null);
}
Graphics2D g = dst.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawImage(src, 0, 0, width, height, null);
g.dispose();
return dst;
}
use of java.awt.image.ColorModel in project Lucee by lucee.
the class CurlFilter method filter.
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
int width = src.getWidth();
int height = src.getHeight();
this.width = src.getWidth();
this.height = src.getHeight();
// int type = src.getType();
originalSpace = new Rectangle(0, 0, width, height);
transformedSpace = new Rectangle(0, 0, width, height);
transformSpace(transformedSpace);
if (dst == null) {
ColorModel dstCM = src.getColorModel();
dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null);
}
// WritableRaster dstRaster =
dst.getRaster();
int[] inPixels = getRGB(src, 0, 0, width, height, null);
if (interpolation == NEAREST_NEIGHBOUR)
return filterPixelsNN(dst, width, height, inPixels, transformedSpace);
int srcWidth = width;
int srcHeight = height;
int srcWidth1 = width - 1;
int srcHeight1 = height - 1;
int outWidth = transformedSpace.width;
int outHeight = transformedSpace.height;
int outX, outY;
// int index = 0;
int[] outPixels = new int[outWidth];
outX = transformedSpace.x;
outY = transformedSpace.y;
float[] out = new float[4];
for (int y = 0; y < outHeight; y++) {
for (int x = 0; x < outWidth; x++) {
transformInverse(outX + x, outY + y, out);
int srcX = (int) Math.floor(out[0]);
int srcY = (int) Math.floor(out[1]);
float xWeight = out[0] - srcX;
float yWeight = out[1] - srcY;
int nw, ne, sw, se;
if (srcX >= 0 && srcX < srcWidth1 && srcY >= 0 && srcY < srcHeight1) {
// Easy case, all corners are in the image
int i = srcWidth * srcY + srcX;
nw = inPixels[i];
ne = inPixels[i + 1];
sw = inPixels[i + srcWidth];
se = inPixels[i + srcWidth + 1];
} else {
// Some of the corners are off the image
nw = getPixel(inPixels, srcX, srcY, srcWidth, srcHeight);
ne = getPixel(inPixels, srcX + 1, srcY, srcWidth, srcHeight);
sw = getPixel(inPixels, srcX, srcY + 1, srcWidth, srcHeight);
se = getPixel(inPixels, srcX + 1, srcY + 1, srcWidth, srcHeight);
}
int rgb = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
float shade = out[2];
r = (int) (r * shade);
g = (int) (g * shade);
b = (int) (b * shade);
rgb = (rgb & 0xff000000) | (r << 16) | (g << 8) | b;
if (out[3] != 0)
outPixels[x] = PixelUtils.combinePixels(rgb, inPixels[srcWidth * y + x], PixelUtils.NORMAL);
else
outPixels[x] = rgb;
}
setRGB(dst, 0, y, transformedSpace.width, 1, outPixels);
}
return dst;
}
use of java.awt.image.ColorModel in project Lucee by lucee.
the class Image method rotate.
public void rotate(float x, float y, float angle, int interpolation) throws ExpressionException {
if (x == -1)
x = (float) getWidth() / 2;
if (y == -1)
y = (float) getHeight() / 2;
angle = (float) Math.toRadians(angle);
ColorModel cmSource = image().getColorModel();
if (cmSource instanceof IndexColorModel && cmSource.hasAlpha() && !cmSource.isAlphaPremultiplied()) {
image(paletteToARGB(image()));
cmSource = image().getColorModel();
}
BufferedImage alpha = null;
if (cmSource.hasAlpha() && !cmSource.isAlphaPremultiplied()) {
alpha = getAlpha(image());
image(removeAlpha(image()));
}
Interpolation interp = Interpolation.getInstance(0);
if (INTERPOLATION_BICUBIC == interpolation)
interp = Interpolation.getInstance(1);
else if (INTERPOLATION_BILINEAR == interpolation)
interp = Interpolation.getInstance(2);
if (alpha != null) {
ParameterBlock params = new ParameterBlock();
params.addSource(alpha);
params.add(x);
params.add(y);
params.add(angle);
params.add(interp);
params.add(new double[] { 0.0 });
RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION, (RenderingHints.VALUE_INTERPOLATION_BICUBIC));
hints.add(new RenderingHints(JAI.KEY_BORDER_EXTENDER, new BorderExtenderConstant(new double[] { 255.0 })));
hints.add(new RenderingHints(JAI.KEY_REPLACE_INDEX_COLOR_MODEL, Boolean.TRUE));
alpha = JAI.create("rotate", params, hints).getAsBufferedImage();
}
ParameterBlock params = new ParameterBlock();
params.addSource(image());
params.add(x);
params.add(y);
params.add(angle);
params.add(interp);
params.add(new double[] { 0.0 });
BorderExtender extender = new BorderExtenderConstant(new double[] { 0.0 });
RenderingHints hints = new RenderingHints(JAI.KEY_BORDER_EXTENDER, extender);
hints.add(new RenderingHints(JAI.KEY_REPLACE_INDEX_COLOR_MODEL, Boolean.TRUE));
image(JAI.create("rotate", params, hints).getAsBufferedImage());
if (alpha != null)
image(addAlpha(image(), alpha, 0, 0));
}
use of java.awt.image.ColorModel in project Lucee by lucee.
the class Image method addBorder.
/**
* add a border to image
* @param thickness
* @param color
* @param borderType
*/
public void addBorder(int thickness, Color color, int borderType) throws ExpressionException {
ColorModel cm = image().getColorModel();
if (((cm instanceof IndexColorModel)) && (cm.hasAlpha()) && (!cm.isAlphaPremultiplied())) {
image(paletteToARGB(image()));
cm = image().getColorModel();
}
BufferedImage alpha = null;
if ((cm.getNumComponents() > 3) && (cm.hasAlpha())) {
alpha = getAlpha(image());
image(removeAlpha(image()));
}
if (alpha != null) {
ParameterBlock params1 = new ParameterBlock();
params1.addSource(alpha);
// left
params1.add(thickness);
// right
params1.add(thickness);
// top
params1.add(thickness);
// bottom
params1.add(thickness);
params1.add(new BorderExtenderConstant(new double[] { 255D }));
RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
hints.add(new RenderingHints(JAI.KEY_REPLACE_INDEX_COLOR_MODEL, Boolean.TRUE));
alpha = JAI.create("border", params1, hints).getAsBufferedImage();
}
ParameterBlock params = new ParameterBlock();
params.addSource(image());
// left
params.add(thickness);
// right
params.add(thickness);
// top
params.add(thickness);
// bottom
params.add(thickness);
params.add(toBorderExtender(borderType, color));
image(JAI.create("border", params).getAsBufferedImage());
if (alpha != null) {
image(addAlpha(image(), alpha, thickness, thickness));
}
}
use of java.awt.image.ColorModel in project Lucee by lucee.
the class Image method hasAlpha.
/**
* This method returns true if the specified image has transparent pixels
* @param image
* @return
*/
public static boolean hasAlpha(java.awt.Image image) {
// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage) image;
return bimage.getColorModel().hasAlpha();
}
// Use a pixel grabber to retrieve the image's color model;
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
// Get the image's color model
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}
Aggregations