protected BufferedImage paintToBufferedImage()

in src/main/java/org/apache/xmlgraphics/image/loader/impl/ImageConverterG2D2Bitmap.java [92:168]


    protected BufferedImage paintToBufferedImage(ImageGraphics2D g2dImage,
            int bitsPerPixel, boolean withAlpha, int resolution, boolean cmyk) {
        ImageSize size = g2dImage.getSize();

        RenderingHints additionalHints = null;
        int bmw = (int)Math.ceil(UnitConv.mpt2px(size.getWidthMpt(), resolution));
        int bmh = (int)Math.ceil(UnitConv.mpt2px(size.getHeightMpt(), resolution));
        BufferedImage bi;
        switch (bitsPerPixel) {
        case 1:
            bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_BINARY);
            withAlpha = false;
            //withAlpha is ignored in this case
            additionalHints = new RenderingHints(null);
            //The following usually has no effect but some class libraries might support it
            additionalHints.put(RenderingHints.KEY_DITHERING,
                    RenderingHints.VALUE_DITHER_ENABLE);
            break;
        case 8:
            if (withAlpha) {
                bi = createGrayBufferedImageWithAlpha(bmw, bmh);
            } else {
                bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_GRAY);
            }
            break;
        default:
            if (withAlpha) {
                bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_ARGB);
            } else {
                if (cmyk) {
                    ComponentColorModel ccm = new ComponentColorModel(
                            new DeviceCMYKColorSpace(), false, false, 1, DataBuffer.TYPE_BYTE);
                    int[] bands = {0, 1, 2, 3};
                    PixelInterleavedSampleModel sm = new PixelInterleavedSampleModel(
                            DataBuffer.TYPE_BYTE, bmw, bmh, 4, bmw * 4, bands);
                    WritableRaster raster = WritableRaster.createWritableRaster(sm, new Point(0, 0));
                    bi = new BufferedImage(ccm, raster, false, null);
                } else {
                    bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_RGB);
                }
            }
        }

        Graphics2D g2d = bi.createGraphics();
        try {
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
            setRenderingHintsForBufferedImage(g2d);
            if (additionalHints != null) {
                g2d.addRenderingHints(additionalHints);
            }

            g2d.setBackground(Color.white);
            g2d.setColor(Color.black);
            if (!withAlpha) {
                g2d.clearRect(0, 0, bmw, bmh);
            }
            /* debug code
            int off = 2;
            g2d.drawLine(off, 0, off, bmh);
            g2d.drawLine(bmw - off, 0, bmw - off, bmh);
            g2d.drawLine(0, off, bmw, off);
            g2d.drawLine(0, bmh - off, bmw, bmh - off);
            */
            double sx = (double)bmw / size.getWidthMpt();
            double sy = (double)bmh / size.getHeightMpt();
            g2d.scale(sx, sy);

            //Paint the image on the BufferedImage
            Rectangle2D area = new Rectangle2D.Double(
                    0.0, 0.0, size.getWidthMpt(), size.getHeightMpt());
            g2dImage.getGraphics2DImagePainter().paint(g2d, area);
        } finally {
            g2d.dispose();
        }
        return bi;
    }