Search in sources :

Example 36 with ColorConvertOp

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;
}
Also used : BufferedImage(java.awt.image.BufferedImage) ColorConvertOp(java.awt.image.ColorConvertOp) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) ConvolveOp(java.awt.image.ConvolveOp) Kernel(java.awt.image.Kernel) IndexColorModel(java.awt.image.IndexColorModel)

Example 37 with ColorConvertOp

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));
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) BufferedImage(java.awt.image.BufferedImage)

Example 38 with ColorConvertOp

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");
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) WritableRaster(java.awt.image.WritableRaster) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point) Test(org.junit.jupiter.api.Test)

Example 39 with ColorConvertOp

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());
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.jupiter.api.Test)

Aggregations

ColorConvertOp (java.awt.image.ColorConvertOp)39 BufferedImage (java.awt.image.BufferedImage)37 ColorModel (java.awt.image.ColorModel)12 ColorSpace (java.awt.color.ColorSpace)10 IndexColorModel (java.awt.image.IndexColorModel)10 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)5 Point (java.awt.Point)4 WritableRaster (java.awt.image.WritableRaster)3 IIOException (javax.imageio.IIOException)3 Color (java.awt.Color)2 Graphics (java.awt.Graphics)2 ComponentColorModel (java.awt.image.ComponentColorModel)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 Test (org.junit.jupiter.api.Test)2 ImageProcessingException (com.drew.imaging.ImageProcessingException)1 Metadata (com.drew.metadata.Metadata)1 Foundation (com.intellij.ui.mac.foundation.Foundation)1 ID (com.intellij.ui.mac.foundation.ID)1 Vector3f (com.jme3.math.Vector3f)1 ImageFeature (de.neotos.imageanalyzer.ImageFeature)1