Example usage for java.awt.image BufferedImage getColorModel

List of usage examples for java.awt.image BufferedImage getColorModel

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getColorModel.

Prototype

public ColorModel getColorModel() 

Source Link

Document

Returns the ColorModel .

Usage

From source file:com.fengduo.bee.service.impl.file.FileServiceImpl.java

/**
 * ?/*w  ww  . j ava  2  s . c  o m*/
 * 
 * @param source
 * @param targetW
 * @param targetH
 * @param ifScaling ?
 * @return
 */
public static BufferedImage resize(BufferedImage source, int targetW, int targetH, boolean ifScaling) {
    // targetWtargetH
    int type = source.getType();
    BufferedImage target = null;
    double sx = (double) targetW / source.getWidth();
    double sy = (double) targetH / source.getHeight();
    // targetWtargetH??,?if else???
    if (ifScaling) {
        if (sx > sy) {
            sx = sy;
            targetW = (int) (sx * source.getWidth());
        } else {
            sy = sx;
            targetH = (int) (sy * source.getHeight());
        }
    }
    if (type == BufferedImage.TYPE_CUSTOM) { // handmade
        ColorModel cm = source.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        target = new BufferedImage(cm, raster, alphaPremultiplied, null);
    } else
        target = new BufferedImage(targetW, targetH, type);
    Graphics2D g = target.createGraphics();
    // smoother than exlax:
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
    g.dispose();
    return target;
}

From source file:io.warp10.script.processing.image.Pdecode.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
    Object top = stack.pop();//from  w  ww  .ja v a2 s.  c o  m

    String data = top.toString();

    if (!(top instanceof String) || !data.startsWith("data:image/")) {
        throw new WarpScriptException(getName() + " expects a base64 data URI on top of the stack.");
    }

    data = data.substring(data.indexOf(",") + 1);

    byte[] bytes = Base64.decodeBase64(data);

    Image awtImage = new ImageIcon(bytes).getImage();

    if (awtImage instanceof BufferedImage) {
        BufferedImage buffImage = (BufferedImage) awtImage;
        int space = buffImage.getColorModel().getColorSpace().getType();
        if (space == ColorSpace.TYPE_CMYK) {
            throw new WarpScriptException(getName() + " only supports RGB images.");
        }
    }

    PImage image = new PImage(awtImage);

    //
    // Check transparency
    //

    if (null != image.pixels) {
        for (int i = 0; i < image.pixels.length; i++) {
            // since transparency is often at corners, hopefully this
            // will find a non-transparent pixel quickly and exit
            if ((image.pixels[i] & 0xff000000) != 0xff000000) {
                image.format = PImage.ARGB;
                break;
            }
        }
    }

    stack.push(image);

    return stack;
}

From source file:osmb.program.tiledatawriter.TileImageJpegDataWriter.java

@Override
public void processImage(BufferedImage image, OutputStream out) throws IOException {
    if (image.getColorModel().hasAlpha()) {
        // Javas JPEG writes has a bug when the image has alpha transparency
        // see http://stackoverflow.com/questions/4386446/problem-using-imageio-write-jpg-file

        BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = imageRGB.createGraphics();
        g.drawImage(image, null, 0, 0);//  w w  w. j  a va2s.  co m
        g.dispose();
        image = imageRGB;
    }

    ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);
    jpegImageWriter.setOutput(imageOut);
    IIOImage ioImage = new IIOImage(image, null, null);
    jpegImageWriter.write(null, ioImage, iwp);
}

From source file:osmcd.program.tiledatawriter.TileImageJpegDataWriter.java

public void processImage(BufferedImage image, OutputStream out) throws IOException {

    if (image.getColorModel().hasAlpha()) {
        // Javas JPEG writes has a bug when the image has alpha transparency
        // see http://stackoverflow.com/questions/4386446/problem-using-imageio-write-jpg-file

        BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = imageRGB.createGraphics();
        g.drawImage(image, null, 0, 0);/* w ww .  ja  va  2s.c om*/
        g.dispose();
        image = imageRGB;
    }

    ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);
    jpegImageWriter.setOutput(imageOut);
    IIOImage ioImage = new IIOImage(image, null, null);
    jpegImageWriter.write(null, ioImage, iwp);
}

From source file:RasterImageTest.java

/**
 * Makes the Mandelbrot image./* w w  w. ja v  a 2s .  c  om*/
 * @param width the width
 * @parah height the height
 * @return the image
 */
public BufferedImage makeMandelbrot(int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = image.getRaster();
    ColorModel model = image.getColorModel();

    Color fractalColor = Color.red;
    int argb = fractalColor.getRGB();
    Object colorData = model.getDataElements(argb, null);

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            double a = XMIN + i * (XMAX - XMIN) / width;
            double b = YMIN + j * (YMAX - YMIN) / height;
            if (!escapesToInfinity(a, b))
                raster.setDataElements(i, j, colorData);
        }
    return image;
}

From source file:net.pms.medialibrary.commons.helpers.FileImportHelper.java

private static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }/*  w  ww  .  j ava  2 s.  c  om*/

    // 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();
}

From source file:com.alibaba.simpleimage.CompositeImageProcessorTest.java

protected void check(File src, File dest) throws Exception {
    FileInputStream in = null;//w  w  w .j av a2  s . com
    FileInputStream in2 = null;
    try {
        in = new FileInputStream(dest);
        in2 = new FileInputStream(src);
        ImageRender rr = new ReadRender(in, false);
        ImageRender rr2 = new ReadRender(in2, false);
        ImageWrapper srcImg = rr2.render();
        ImageWrapper dstImg = rr.render();
        BufferedImage dstBi = dstImg.getAsBufferedImage();

        assertTrue(dstBi.getColorModel().getColorSpace().isCS_sRGB());
        assertTrue(dstBi.getWidth() <= 1024);
        assertTrue(dstBi.getHeight() <= 1024);
        if (srcImg.getImageFormat() != ImageFormat.GIF) {
            if (srcImg.getQuality() >= 50) {
                assertTrue(dstImg.getQuality() == srcImg.getQuality());
            } else {
                assertTrue(dstImg.getQuality() == 50);
            }

            assertTrue(srcImg.getHorizontalSamplingFactor(0) == dstImg.getHorizontalSamplingFactor(0));
            assertTrue(srcImg.getVerticalSamplingFactor(0) == dstImg.getVerticalSamplingFactor(0));
            assertTrue(srcImg.getHorizontalSamplingFactor(1) == dstImg.getHorizontalSamplingFactor(1));
            assertTrue(srcImg.getVerticalSamplingFactor(1) == dstImg.getVerticalSamplingFactor(1));
            assertTrue(srcImg.getHorizontalSamplingFactor(2) == dstImg.getHorizontalSamplingFactor(2));
            assertTrue(srcImg.getVerticalSamplingFactor(2) == dstImg.getVerticalSamplingFactor(2));
        }
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(in2);
    }
}

From source file:org.jahia.services.image.AbstractJava2DImageService.java

protected BufferedImage getDestImage(int newWidth, int newHeight, BufferedImage originalImage) {
    BufferedImage dest;//from   w  ww  .  j  a  va 2 s.co m
    if (originalImage.getColorModel() instanceof IndexColorModel) {
        // dest = new BufferedImage(newWidth, newHeight, originalImage.getType(), (IndexColorModel) originalImage.getColorModel());
        dest = new BufferedImage(originalImage.getColorModel(),
                originalImage.getColorModel().createCompatibleWritableRaster(newWidth, newHeight), false,
                new Hashtable<Object, Object>());
    } else {
        dest = new BufferedImage(newWidth, newHeight, originalImage.getType());
    }
    return dest;
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactoryTest.java

@Test
public void testCreateFromImageRGB() throws IOException {
    PDDocument document = new PDDocument();
    BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));
    assertEquals(3, image.getColorModel().getNumComponents());
    PDImageXObject ximage = JPEGFactory.createFromImage(image);
    validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName());

    doWritePDF(document, ximage, testResultsDir, "jpegrgb.pdf");
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactoryTest.java

@Test
public void testCreateFromImage256() throws IOException {
    PDDocument document = new PDDocument();
    BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg"));
    assertEquals(1, image.getColorModel().getNumComponents());
    PDImageXObject ximage = JPEGFactory.createFromImage(image);
    validate(ximage, 8, 344, 287, "jpg", PDDeviceGray.INSTANCE.getName());

    doWritePDF(document, ximage, testResultsDir, "jpeg256.pdf");
}