use of java.awt.image.ColorConvertOp in project BroadleafCommerce by BroadleafCommerce.
the class GaussianBlur method filter.
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
if (kernelSize < 1 || kernelSize > NUM_KERNELS) {
return src;
}
if (numOfPasses < 1) {
return src;
}
if (src == null) {
throw new NullPointerException("src image is null");
}
if (src == dst) {
throw new IllegalArgumentException("src image cannot be the same as the dst image");
}
boolean needToConvert = false;
ColorModel srcCM = src.getColorModel();
ColorModel dstCM;
BufferedImage origDst = dst;
if (srcCM instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel) srcCM;
src = icm.convertToIntDiscrete(src.getRaster(), false);
srcCM = src.getColorModel();
}
if (dst == null) {
dst = createCompatibleDestImage(src, null);
dstCM = srcCM;
origDst = dst;
} else {
dstCM = dst.getColorModel();
if (srcCM.getColorSpace().getType() != dstCM.getColorSpace().getType()) {
needToConvert = true;
dst = createCompatibleDestImage(src, null);
dstCM = dst.getColorModel();
} else if (dstCM instanceof IndexColorModel) {
dst = createCompatibleDestImage(src, null);
dstCM = dst.getColorModel();
}
}
float[] matrix = GAUSSIAN_BLUR_KERNELS[kernelSize - 1];
Kernel gaussianBlur1 = new Kernel(matrix.length, 1, matrix);
Kernel gaussianBlur2 = new Kernel(1, matrix.length, matrix);
ConvolveOp gaussianOp1 = new ConvolveOp(gaussianBlur1, ConvolveOp.EDGE_NO_OP, null);
ConvolveOp gaussianOp2 = new ConvolveOp(gaussianBlur2, ConvolveOp.EDGE_NO_OP, null);
BufferedImage tempImage = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
dst = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
BufferedImage nextSource = src;
for (int i = 0; i < numOfPasses; i++) {
tempImage = gaussianOp1.filter(nextSource, tempImage);
dst = gaussianOp2.filter(tempImage, dst);
nextSource = dst;
}
if (needToConvert) {
ColorConvertOp ccop = new ColorConvertOp(hints);
ccop.filter(dst, origDst);
} else if (origDst != dst) {
java.awt.Graphics2D g = origDst.createGraphics();
try {
g.drawImage(dst, 0, 0, null);
} finally {
g.dispose();
}
}
return origDst;
}
use of java.awt.image.ColorConvertOp in project java-swing-tips by aterai.
the class GrayImageFilter method makeGrayImageIcon1.
private static ImageIcon makeGrayImageIcon1(Image img) {
int w = img.getWidth(null);
int h = img.getHeight(null);
BufferedImage source = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics g = source.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
ColorConvertOp ccOp = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
return new ImageIcon(ccOp.filter(source, null));
}
use of java.awt.image.ColorConvertOp in project pdfbox by apache.
the class LosslessFactoryTest method testCreateLosslessFrom16Bit.
@Test
void testCreateLosslessFrom16Bit() throws IOException {
PDDocument document = new PDDocument();
BufferedImage image = ImageIO.read(this.getClass().getResourceAsStream("png.png"));
ColorSpace targetCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int dataBufferType = DataBuffer.TYPE_USHORT;
final ColorModel colorModel = new ComponentColorModel(targetCS, false, false, ColorModel.OPAQUE, dataBufferType);
WritableRaster targetRaster = Raster.createInterleavedRaster(dataBufferType, image.getWidth(), image.getHeight(), targetCS.getNumComponents(), new Point(0, 0));
BufferedImage img16Bit = new BufferedImage(colorModel, targetRaster, false, new Hashtable<>());
ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), targetCS, null);
op.filter(image, img16Bit);
PDImageXObject ximage = LosslessFactory.createFromImage(document, img16Bit);
validate(ximage, 16, img16Bit.getWidth(), img16Bit.getHeight(), "png", PDDeviceRGB.INSTANCE.getName());
checkIdent(image, ximage.getImage());
doWritePDF(document, ximage, TESTRESULTSDIR, "misc-16bit.pdf");
}
use of java.awt.image.ColorConvertOp in project pdfbox by apache.
the class LosslessFactoryTest method testCreateLosslessFromImageCMYK.
/**
* Test lossless encoding of CMYK images
*/
@Test
void testCreateLosslessFromImageCMYK() throws IOException {
PDDocument document = new PDDocument();
BufferedImage image = ImageIO.read(this.getClass().getResourceAsStream("png.png"));
final ColorSpace targetCS = new ICC_ColorSpace(ICC_Profile.getInstance(this.getClass().getResourceAsStream("/org/apache/pdfbox/resources/icc/ISOcoated_v2_300_bas.icc")));
ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), targetCS, null);
BufferedImage imageCMYK = op.filter(image, null);
PDImageXObject ximage = LosslessFactory.createFromImage(document, imageCMYK);
validate(ximage, 8, imageCMYK.getWidth(), imageCMYK.getHeight(), "png", "ICCBased");
doWritePDF(document, ximage, TESTRESULTSDIR, "cmyk.pdf");
// still slight difference of 1 color level
// checkIdent(imageCMYK, ximage.getImage());
}
Aggregations