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:org.dcm4che3.tool.dcm2jpg.Dcm2Jpg.java

private BufferedImage convert(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    return cm.getNumComponents() == 3 ? BufferedImageUtils.convertToIntRGB(bi) : bi;
}

From source file:org.dcm4che.tool.dcm2jpg.Dcm2Jpg.java

private BufferedImage convert(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    if (cm instanceof PaletteColorModel)
        return ((PaletteColorModel) cm).convertToIntDiscrete(bi.getData());
    return bi;//from   ww w. ja  va  2  s.co  m
}

From source file:org.github.jipsg.sanselan.ImageConversionSanselanTest.java

/**
 * Convert images having a transparency layer (alpha-channel) to JPG. Without
 * further handling the alpha-channel will be rendered black.
 * The test fails with "org.apache.commons.imaging.ImageWriteException: This image format (Jpeg-Custom) cannot be written."
 *///  w  w w.ja v  a  2s  . c  o  m
@Test(expected = org.apache.commons.imaging.ImageWriteException.class)
public void testWriteTransparentImagesAsJpeg() throws Exception {

    String formatName = "jpeg";
    List<File> sourceImageFileList = new ArrayList<File>();

    sourceImageFileList.add(getImageFile("gif", "test-image-transparent.gif"));
    sourceImageFileList.add(getImageFile("png", "test-image-transparent.png"));

    for (File sourceImageFile : sourceImageFileList) {

        BufferedImage bufferedImage = createBufferedImage(sourceImageFile);
        assertValidBufferedImage(bufferedImage);
        assertTrue("Expecting transparency", bufferedImage.getColorModel().hasAlpha());
        assertTrue("Expecting ARGB color model", bufferedImage.getType() == BufferedImage.TYPE_INT_ARGB);

        File targetImageFile = createOutputFileName("testWriteTransparentImagesAsJpeg", sourceImageFile,
                formatName);
        writeBufferedImage(bufferedImage, formatName, targetImageFile);
    }
}

From source file:org.github.jipsg.sanselan.ImageConversionSanselanTest.java

/**
 * Convert images having a transparency layer (alpha-channel) to JPG. Fill
 * the alpha-channel with Color.WHITE to have a useful image.
 * The test fails with "org.apache.commons.imaging.ImageWriteException: This image format (Jpeg-Custom) cannot be written."
 *//* ww w.ja v a2 s  . c  o m*/
@Test(expected = org.apache.commons.imaging.ImageWriteException.class)
public void testWriteTransparentImagesWithAlphaChannelHandlingAsJpeg() throws Exception {

    String formatName = "jpeg";
    List<File> sourceImageFileList = new ArrayList<File>();

    sourceImageFileList.add(getImageFile("gif", "test-image-transparent.gif"));
    sourceImageFileList.add(getImageFile("png", "test-image-transparent.png"));

    for (File sourceImageFile : sourceImageFileList) {

        BufferedImage bufferedImage = createBufferedImage(sourceImageFile);
        assertValidBufferedImage(bufferedImage);
        assertTrue("Expecting transparency", bufferedImage.getColorModel().hasAlpha());
        assertTrue("Expecting ARGB color model", bufferedImage.getType() == BufferedImage.TYPE_INT_ARGB);

        BufferedImage rgbBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = rgbBufferedImage.createGraphics();
        graphics.drawImage(bufferedImage, 0, 0, null);
        graphics.dispose();
        assertValidBufferedImage(rgbBufferedImage);
        assertFalse("Expecting no transparency", rgbBufferedImage.getColorModel().hasAlpha());
        assertEquals("Expecting RGB color model", BufferedImage.TYPE_INT_RGB, rgbBufferedImage.getType());

        File targetImageFile = createOutputFileName("testWriteTransparentImagesWithAlphaChannelHandlingAsJpeg",
                sourceImageFile, formatName);
        writeBufferedImage(bufferedImage, formatName, targetImageFile);
    }
}

From source file:app.springapp.ImageConverter.java

BufferedImage deepCopy(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = bi.copyData(null);
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}

From source file:haven.Utils.java

public static BufferedImage outline(BufferedImage img, Color col) {
    Coord sz = imgsz(img).add(2, 2);/*from   ww w .ja  v  a2  s . c  om*/
    BufferedImage ol = TexI.mkbuf(sz);
    Object fcol = ol.getColorModel().getDataElements(col.getRGB(), null);
    Raster src = img.getRaster();
    WritableRaster dst = ol.getRaster();
    for (int y = 0; y < sz.y; y++) {
        for (int x = 0; x < sz.x; x++) {
            boolean t;
            if ((y == 0) || (x == 0) || (y == sz.y - 1) || (x == sz.x - 1)) {
                t = true;
            } else {
                t = src.getSample(x - 1, y - 1, 3) < 250;
            }
            if (!t)
                continue;
            if (((x > 1) && (y > 0) && (y < sz.y - 1) && (src.getSample(x - 2, y - 1, 3) >= 250))
                    || ((x > 0) && (y > 1) && (x < sz.x - 1) && (src.getSample(x - 1, y - 2, 3) >= 250))
                    || ((x < sz.x - 2) && (y > 0) && (y < sz.y - 1) && (src.getSample(x, y - 1, 3) >= 250))
                    || ((x > 0) && (y < sz.y - 2) && (x < sz.x - 1) && (src.getSample(x - 1, y, 3) >= 250)))
                dst.setDataElements(x, y, fcol);
        }
    }
    return (ol);
}

From source file:org.polymap.rhei.batik.engine.svg.Svg2PngTest.java

@Test
public void testScaleSVG() throws Exception {
    ImageConfiguration imageConfig = new ImageConfiguration();
    imageConfig.setName("default");
    imageConfig.setDepth(ColorDepth.B4);
    imageConfig.setAdjHue(0.3f);//w ww . ja  v  a 2  s .  c  o m
    imageConfig.setAdjSaturation(0.8f);
    imageConfig.setAdjBrightness(0f);
    imageConfig.setColorType(COLOR_TYPE.RGB);

    File pngPath = new File("build/Svg2PngTest");
    pngPath.mkdirs();
    FileUtils.cleanDirectory(pngPath);

    File svgFile = new File(pngPath, EXAMPLE_SVG2);
    FileUtils.copyURLToFile(getClass().getResource(EXAMPLE_SVG2), svgFile);

    svg2Png.transcode(pngPath.getAbsolutePath(), Collections.singletonList(svgFile),
            Collections.singletonList(Scale.P16), Collections.singletonList(imageConfig));

    BufferedImage image = ImageIO
            .read(new File(pngPath + "/default/16/" + EXAMPLE_SVG2.replace(".svg", ".png")));
    Assert.assertNotNull(image);
    Assert.assertEquals(16f, image.getHeight(), 0.5f);
    Assert.assertEquals(16f, image.getWidth(), 0.5f);
    Assert.assertEquals(1, image.getColorModel().getPixelSize());
}

From source file:net.d53dev.dslfy.web.service.GifService.java

public BufferedImage convertRGBAToGIF(BufferedImage src, int transColor) {
    BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    Graphics g = dst.getGraphics();
    g.setColor(new Color(transColor));
    g.fillRect(0, 0, dst.getWidth(), dst.getHeight());
    {/*from w  ww  .jav a2s. co m*/
        IndexColorModel indexedModel = (IndexColorModel) dst.getColorModel();
        WritableRaster raster = dst.getRaster();
        int sample = raster.getSample(0, 0, 0);
        int size = indexedModel.getMapSize();
        byte[] rr = new byte[size];
        byte[] gg = new byte[size];
        byte[] bb = new byte[size];
        indexedModel.getReds(rr);
        indexedModel.getGreens(gg);
        indexedModel.getBlues(bb);
        IndexColorModel newModel = new IndexColorModel(8, size, rr, gg, bb, sample);
        dst = new BufferedImage(newModel, raster, dst.isAlphaPremultiplied(), null);
    }
    dst.createGraphics().drawImage(src, 0, 0, null);
    return dst;
}

From source file:com.ackpdfbox.app.imageio.TIFFUtil.java

/**
 * Updates the given ImageIO metadata with Sun's custom TIFF tags, as described in
 * the <a href="https://svn.apache.org/repos/asf/xmlgraphics/commons/tags/commons-1_3_1/src/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java">org.apache.xmlgraphics.image.writer.ImageIOTIFFImageWriter
 * sources</a>, /*from  w  w w  . j a  va 2 s .  co  m*/
 * the <a href="http://download.java.net/media/jai-imageio/javadoc/1.0_01/com/sun/media/imageio/plugins/tiff/package-summary.html">com.sun.media.imageio.plugins.tiff
 * package javadoc</a>
 * and the <a href="http://partners.adobe.com/public/developer/tiff/index.html">TIFF
 * specification</a>.
 *
 * @param image buffered image which will be written
 * @param metadata ImageIO metadata
 * @param dpi image dots per inch
 * @throws IIOInvalidTreeException if something goes wrong
 */
static void updateMetadata(IIOMetadata metadata, BufferedImage image, int dpi) throws IIOInvalidTreeException {
    debugLogMetadata(metadata, SUN_TIFF_FORMAT);

    if (!SUN_TIFF_FORMAT.equals(metadata.getNativeMetadataFormatName())) {
        LOG.debug("Using unknown TIFF image writer: " + metadata.getNativeMetadataFormatName());
        return;
    }

    IIOMetadataNode root = new IIOMetadataNode(SUN_TIFF_FORMAT);
    IIOMetadataNode ifd;
    if (root.getElementsByTagName("TIFFIFD").getLength() == 0) {
        ifd = new IIOMetadataNode("TIFFIFD");
        ifd.setAttribute("tagSets", tagSetClassName);
        root.appendChild(ifd);
    } else {
        ifd = (IIOMetadataNode) root.getElementsByTagName("TIFFIFD").item(0);
    }

    // standard metadata does not work, so we set the DPI manually
    ifd.appendChild(createRationalField(282, "XResolution", dpi, 1));
    ifd.appendChild(createRationalField(283, "YResolution", dpi, 1));
    ifd.appendChild(createShortField(296, "ResolutionUnit", 2)); // Inch

    ifd.appendChild(createLongField(278, "RowsPerStrip", image.getHeight()));
    ifd.appendChild(createAsciiField(305, "Software", "PDFBOX"));

    if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1) {
        // set PhotometricInterpretation WhiteIsZero
        // because of bug in Windows XP preview
        ifd.appendChild(createShortField(262, "PhotometricInterpretation", 0));
    }

    metadata.mergeTree(SUN_TIFF_FORMAT, root);

    debugLogMetadata(metadata, SUN_TIFF_FORMAT);
}

From source file:com.aegon.pdf2tiff.TIFFUtil.java

/**
 * Updates the given ImageIO metadata with Sun's custom TIFF tags.
 * {@see https://svn.apache.org/repos/asf/xmlgraphics/commons/tags/commons-1_3_1/src/java/org/
 *       apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java}
 * {@see http://download.java.net/media/jai-imageio/javadoc/1.0_01/com/sun/media/imageio/
 *       plugins/tiff/package-summary.html}
 * {@see http://partners.adobe.com/public/developer/tiff/index.html}
 * @param image buffered image which will be written
 * @param metadata ImageIO metadata//from w  ww.  jav a  2s  . c o  m
 * @param dpi image dots per inch
 */
public static void updateMetadata(IIOMetadata metadata, BufferedImage image, int dpi) {
    debugLogMetadata(metadata, SUN_TIFF_FORMAT);

    if (!SUN_TIFF_FORMAT.equals(metadata.getNativeMetadataFormatName())) {
        LOG.debug("Using unknown TIFF image writer: " + metadata.getNativeMetadataFormatName());
        return;
    }

    IIOMetadataNode root = new IIOMetadataNode(SUN_TIFF_FORMAT);
    IIOMetadataNode ifd;
    if (root.getElementsByTagName("TIFFIFD").getLength() == 0) {
        ifd = new IIOMetadataNode("TIFFIFD");
        ifd.setAttribute("tagSets", "com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet");
        root.appendChild(ifd);
    } else {
        ifd = (IIOMetadataNode) root.getElementsByTagName("TIFFIFD").item(0);
    }

    // standard metadata does not work, so we set the DPI manually
    ifd.appendChild(createRationalField(282, "XResolution", dpi, 1));
    ifd.appendChild(createRationalField(283, "YResolution", dpi, 1));
    ifd.appendChild(createShortField(296, "ResolutionUnit", 2)); // Inch

    ifd.appendChild(createLongField(278, "RowsPerStrip", image.getHeight()));
    ifd.appendChild(createAsciiField(305, "Software", "PDFBOX"));

    if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1) {
        // set PhotometricInterpretation WhiteIsZero
        // because of bug in Windows XP preview
        ifd.appendChild(createShortField(262, "PhotometricInterpretation", 0));
    }

    try {
        metadata.mergeTree(SUN_TIFF_FORMAT, root);
    } catch (IIOInvalidTreeException e) {
        // should never happen
        throw new RuntimeException(e);
    }

    debugLogMetadata(metadata, SUN_TIFF_FORMAT);
}