Search in sources :

Example 66 with ColorModel

use of java.awt.image.ColorModel in project BroadleafCommerce by BroadleafCommerce.

the class Crop method filter.

/* (non-Javadoc)
     * @see java.awt.image.BufferedImageOp#filter(java.awt.image.BufferedImage, java.awt.image.BufferedImage)
     */
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    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();
        }
    }
    java.awt.image.CropImageFilter cropfilter = new java.awt.image.CropImageFilter(region.x, region.y, region.width, region.height);
    Image returnImage = Toolkit.getDefaultToolkit().createImage(new java.awt.image.FilteredImageSource(src.getSource(), cropfilter));
    dst = ImageConverter.convertImage(returnImage);
    origDst = dst;
    if (needToConvert) {
        ColorConvertOp ccop = new ColorConvertOp(hints);
        ccop.filter(dst, origDst);
    } else if (origDst != dst) {
        java.awt.Graphics2D g2 = origDst.createGraphics();
        try {
            g2.drawImage(dst, 0, 0, null);
        } finally {
            g2.dispose();
        }
    }
    return origDst;
}
Also used : BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) ColorConvertOp(java.awt.image.ColorConvertOp) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) IndexColorModel(java.awt.image.IndexColorModel) java.awt(java.awt)

Example 67 with ColorModel

use of java.awt.image.ColorModel in project BroadleafCommerce by BroadleafCommerce.

the class ImageConverter method getImage.

public static BufferedImage getImage(int[] pixels, int width, int height) {
    ColorModel cm = ColorModel.getRGBdefault();
    MemoryImageSource imageSource = new MemoryImageSource(width, height, cm, pixels, 0, width);
    imageSource.setAnimated(true);
    Image temp = Toolkit.getDefaultToolkit().createImage(imageSource);
    BufferedImage image = convertImage(temp);
    return image;
}
Also used : ColorModel(java.awt.image.ColorModel) BufferedImage(java.awt.image.BufferedImage) MemoryImageSource(java.awt.image.MemoryImageSource) BufferedImage(java.awt.image.BufferedImage)

Example 68 with ColorModel

use of java.awt.image.ColorModel in project BroadleafCommerce by BroadleafCommerce.

the class Rotate method filter.

/* (non-Javadoc)
     * @see java.awt.image.BufferedImageOp#filter(java.awt.image.BufferedImage, java.awt.image.BufferedImage)
     */
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    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();
    }
    int neww;
    int newh;
    int w = src.getWidth();
    int h = src.getHeight();
    if (dst == null) {
        double sin = Math.abs(Math.sin(Math.toRadians(amount))), cos = Math.abs(Math.cos(Math.toRadians(amount)));
        neww = (int) Math.floor(w * cos + h * sin);
        newh = (int) Math.floor(h * cos + w * sin);
        dst = createCompatibleDestImage(src, null, neww, newh);
        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();
        }
        neww = dst.getWidth();
        newh = dst.getHeight();
    }
    Graphics2D g = dst.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(Math.toRadians(amount), w / 2, h / 2);
    g.drawRenderedImage(src, null);
    g.dispose();
    origDst = dst;
    if (needToConvert) {
        ColorConvertOp ccop = new ColorConvertOp(hints);
        ccop.filter(dst, origDst);
    } else if (origDst != dst) {
        Graphics2D g2 = origDst.createGraphics();
        try {
            g2.drawImage(dst, 0, 0, null);
        } finally {
            g2.dispose();
        }
    }
    return origDst;
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) BufferedImage(java.awt.image.BufferedImage) IndexColorModel(java.awt.image.IndexColorModel)

Example 69 with ColorModel

use of java.awt.image.ColorModel in project BroadleafCommerce by BroadleafCommerce.

the class UnsharpMask method filter.

/* (non-Javadoc)
     * @see java.awt.image.BufferedImageOp#filter(java.awt.image.BufferedImage, java.awt.image.BufferedImage)
     */
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    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();
        }
    }
    int[] originalPixels = ImageConverter.getPixels(src);
    GaussianBlur blur = new GaussianBlur(radius, 1, hints);
    dst = blur.filter(src, null);
    int[] uMaskBlur = ImageConverter.getPixels(dst);
    int imageWidth = dst.getWidth();
    int imageHeight = dst.getHeight();
    int index = 0;
    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            int R1 = (originalPixels[index] >> 16) & 0xff;
            int G1 = (originalPixels[index] >> 8) & 0xff;
            int B1 = (originalPixels[index] >> 0) & 0xff;
            int R2 = (uMaskBlur[index] >> 16) & 0xff;
            int G2 = (uMaskBlur[index] >> 8) & 0xff;
            int B2 = (uMaskBlur[index] >> 0) & 0xff;
            int R3 = (int) (value * (float) R1 - (value - 1F) * (float) R2);
            int G3 = (int) (value * (float) G1 - (value - 1F) * (float) G2);
            int B3 = (int) (value * (float) B1 - (value - 1F) * (float) B2);
            // fix overflows
            if (R3 > 255)
                R3 = 255;
            if (R3 < 0)
                R3 = 0;
            if (G3 > 255)
                G3 = 255;
            if (G3 < 0)
                G3 = 0;
            if (B3 > 255)
                B3 = 255;
            if (B3 < 0)
                B3 = 0;
            originalPixels[index] = (originalPixels[index] & 0xff000000) | (R3 << 16) | (G3 << 8) | (B3 << 0);
            index++;
        }
    }
    dst = ImageConverter.getImage(originalPixels, imageWidth, imageHeight);
    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 : ColorConvertOp(java.awt.image.ColorConvertOp) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) BufferedImage(java.awt.image.BufferedImage) IndexColorModel(java.awt.image.IndexColorModel)

Example 70 with ColorModel

use of java.awt.image.ColorModel in project freeplane by freeplane.

the class BigBufferedImage method createBigBufferedImage.

private static BufferedImage createBigBufferedImage(File tempDir, int width, int height, int imageType) throws FileNotFoundException, IOException {
    FileDataBuffer buffer = new FileDataBuffer(tempDir, width * height, 4);
    ColorModel colorModel = null;
    BandedSampleModel sampleModel = null;
    switch(imageType) {
        case TYPE_INT_RGB:
            colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 0 }, false, false, ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
            sampleModel = new BandedSampleModel(DataBuffer.TYPE_BYTE, width, height, 3);
            break;
        case TYPE_INT_ARGB:
            colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false, ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
            sampleModel = new BandedSampleModel(DataBuffer.TYPE_BYTE, width, height, 4);
            break;
        default:
            throw new IllegalArgumentException("Unsupported image type: " + imageType);
    }
    SimpleRaster raster = new SimpleRaster(sampleModel, buffer, new Point(0, 0));
    BigBufferedImage image = new BigBufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
    return image;
}
Also used : ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) BandedSampleModel(java.awt.image.BandedSampleModel) ComponentColorModel(java.awt.image.ComponentColorModel) Point(java.awt.Point)

Aggregations

ColorModel (java.awt.image.ColorModel)188 BufferedImage (java.awt.image.BufferedImage)92 IndexColorModel (java.awt.image.IndexColorModel)80 WritableRaster (java.awt.image.WritableRaster)57 ComponentColorModel (java.awt.image.ComponentColorModel)47 SampleModel (java.awt.image.SampleModel)40 DirectColorModel (java.awt.image.DirectColorModel)39 ColorSpace (java.awt.color.ColorSpace)28 Raster (java.awt.image.Raster)24 Rectangle (java.awt.Rectangle)21 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)20 Point (java.awt.Point)15 ComponentSampleModel (java.awt.image.ComponentSampleModel)15 Graphics2D (java.awt.Graphics2D)13 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)13 IOException (java.io.IOException)13 ColorConvertOp (java.awt.image.ColorConvertOp)12 DataBuffer (java.awt.image.DataBuffer)12 DataBufferInt (java.awt.image.DataBufferInt)11 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)11