Example usage for java.awt.image ColorModel getGreen

List of usage examples for java.awt.image ColorModel getGreen

Introduction

In this page you can find the example usage for java.awt.image ColorModel getGreen.

Prototype

public int getGreen(Object inData) 

Source Link

Document

Returns the green color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace , sRGB.

Usage

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage[] split2RGB(BufferedImage img) {

    //--------------------v2.0--------------------------------------
    int w, h, r, g, b, pixel;

    if (img.getType() == BufferedImage.TYPE_INT_RGB) {
        w = img.getWidth();//from  ww w  . j  a  v  a 2  s.c o  m
        h = img.getHeight();
        BufferedImage[] out = new BufferedImage[3];
        WritableRaster[] rasters = new WritableRaster[3];
        for (int i = 0; i < 3; i++) {
            out[i] = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
            rasters[i] = out[i].getRaster();
        }

        ColorModel cm = img.getColorModel();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);

                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                rasters[0].setSample(x, y, 0, r);
                rasters[1].setSample(x, y, 0, g);
                rasters[2].setSample(x, y, 0, b);
            }
        }
        return out;
    } else {
        return null;
    }
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage[] split2RGBA(BufferedImage img) {
    int w, h, a, r, g, b, pixel;

    if (img.getType() == BufferedImage.TYPE_INT_ARGB) {
        w = img.getWidth();/*  w  w w.  j  av a 2  s. c o m*/
        h = img.getHeight();
        BufferedImage[] out = new BufferedImage[4];
        WritableRaster[] rasters = new WritableRaster[4];
        for (int i = 0; i < 4; i++) {
            out[i] = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
            rasters[i] = out[i].getRaster();
        }

        ColorModel cm = img.getColorModel();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);

                a = cm.getAlpha(pixel);
                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                rasters[3].setSample(x, y, 0, a);
                rasters[0].setSample(x, y, 0, r);
                rasters[1].setSample(x, y, 0, g);
                rasters[2].setSample(x, y, 0, b);
            }
        }
        return out;
    } else {
        return null;
    }
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage rgb2gray(BufferedImage img) {
    if (img.getType() == BufferedImage.TYPE_INT_ARGB || img.getType() == BufferedImage.TYPE_INT_RGB) {
        int w, h;
        w = img.getWidth();//from   ww  w .  j  av  a  2s  . c o  m
        h = img.getHeight();
        BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);

        //----------------------------v3.0----------------------
        ColorModel cm = img.getColorModel();
        int pixel, gr;
        int r, g, b;
        WritableRaster raster = out.getRaster();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);
                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                gr = (int) Math.round(((double) r + (double) g + (double) b) / 3.0);
                raster.setSample(x, y, 0, gr);
            }
        }
        return out;
    }
    return img;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static void fadeImages(BufferedImage source1, BufferedImage source2, BufferedImage target, int relX,
        int targetX) {
    int pixel1, pixel2, newPixel;
    double f;/*from   w w w  . ja  v a2 s. co  m*/
    int r1, g1, b1, r2, g2, b2;
    byte newR, newG, newB;
    ColorModel cm = source1.getColorModel();

    for (int x = relX; x < source1.getWidth(); x++) {
        f = linearF(x, relX, source1.getWidth());
        for (int y = 0; y < source1.getHeight(); y++) {
            pixel1 = source1.getRGB(x, y);
            pixel2 = source2.getRGB(x - relX, y);

            r1 = cm.getRed(pixel1);
            g1 = cm.getGreen(pixel1);
            b1 = cm.getBlue(pixel1);
            r2 = cm.getRed(pixel2);
            g2 = cm.getGreen(pixel2);
            b2 = cm.getBlue(pixel2);

            int tr = 10;

            if (r1 < tr && g1 < tr && b1 < tr) {
                newPixel = pixel2;
            } else if (r2 < tr && g2 < tr && b2 < tr) {
                newPixel = pixel1;
            } else {
                newR = (byte) Math.round(((double) r1) * (1 - f) + ((double) r2) * f);
                newG = (byte) Math.round(((double) g1) * (1 - f) + ((double) g2) * f);
                newB = (byte) Math.round(((double) b1) * (1 - f) + ((double) b2) * f);
                newPixel = (newR & 0xff) << 16 | (newG & 0xff) << 8 | (newB & 0xff) << 0;
            }
            target.setRGB(x + targetX, y, newPixel);
        }

    }
}

From source file:org.pentaho.di.core.gui.SwingGC.java

private void drawImage(SwingUniversalImage image, int locationX, int locationY, int imageSize) {
    if (isDrawingPixelatedImages() && image.isBitmap()) {
        BufferedImage img = image.getAsBitmapForSize(imageSize, imageSize);
        ColorModel cm = img.getColorModel();
        Raster raster = img.getRaster();

        for (int x = 0; x < img.getWidth(observer); x++) {
            for (int y = 0; y < img.getHeight(observer); y++) {
                Object pix = raster.getDataElements(x, y, null);
                gc.setColor(new Color(cm.getRed(pix), cm.getGreen(pix), cm.getBlue(pix), cm.getAlpha(pix)));
                gc.setStroke(new BasicStroke(1.0f));
                gc.drawLine(locationX + xOffset + x, locationY + yOffset + y, locationX + xOffset + x + 1,
                        locationY + yOffset + y + 1);
            }/*from   w w  w. j  a  va 2s.c  o  m*/
        }
    } else {
        image.drawToGraphics(gc, locationX, locationY, imageSize, imageSize);
    }
}

From source file:org.pentaho.di.core.gui.SwingGC.java

private void drawImage(SwingUniversalImage image, int centerX, int centerY, double angle, int imageSize) {
    if (isDrawingPixelatedImages() && image.isBitmap()) {
        BufferedImage img = image.getAsBitmapForSize(imageSize, imageSize, angle);
        ColorModel cm = img.getColorModel();
        Raster raster = img.getRaster();

        int offx = centerX + xOffset - img.getWidth() / 2;
        int offy = centerY + yOffset - img.getHeight() / 2;
        for (int x = 0; x < img.getWidth(observer); x++) {
            for (int y = 0; y < img.getHeight(observer); y++) {
                Object pix = raster.getDataElements(x, y, null);
                gc.setColor(new Color(cm.getRed(pix), cm.getGreen(pix), cm.getBlue(pix), cm.getAlpha(pix)));
                gc.setStroke(new BasicStroke(1.0f));
                gc.drawLine(offx + x, offy + y, offx + x + 1, offy + y + 1);
            }// w ww  . jav  a  2  s  .c  o  m
        }
    } else {
        image.drawToGraphics(gc, centerX, centerY, imageSize, imageSize, angle);
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.java

/**
 * Returns a {@link java.awt.image.BufferedImage} of the COSStream
 * set in the constructor or null if the COSStream could not be encoded.
 *
 * @return {@inheritDoc}/*from   ww  w  .j av  a  2  s  .com*/
 *
 * @throws IOException {@inheritDoc}
 */
public BufferedImage getRGBImage() throws IOException {
    if (image != null) {
        return image;
    }

    try {
        int width = getWidth();
        int height = getHeight();
        int bpc = getBitsPerComponent();

        byte[] array = getPDStream().getByteArray();
        if (array.length == 0) {
            LOG.error("Something went wrong ... the pixelmap doesn't contain any data.");
            return null;
        }
        // Get the ColorModel right
        PDColorSpace colorspace = getColorSpace();
        if (colorspace == null) {
            LOG.error("getColorSpace() returned NULL.  Predictor = " + getPredictor());
            return null;
        }

        ColorModel cm = null;
        if (colorspace instanceof PDIndexed) {
            PDIndexed csIndexed = (PDIndexed) colorspace;
            // the base color space uses 8 bit per component, as the indexed color values
            // of an indexed color space are always in a range from 0 to 255
            ColorModel baseColorModel = csIndexed.getBaseColorSpace().createColorModel(8);
            // number of possible color values in the target color space
            int numberOfColorValues = 1 << bpc;
            // number of indexed color values
            int highValue = csIndexed.getHighValue();
            // choose the correct size, sometimes there are more indexed values than needed
            // and sometimes there are fewer indexed value than possible
            int size = Math.min(numberOfColorValues - 1, highValue);
            byte[] index = csIndexed.getLookupData();
            boolean hasAlpha = baseColorModel.hasAlpha();
            COSBase maskArray = getMask();
            if (baseColorModel.getTransferType() != DataBuffer.TYPE_BYTE) {
                throw new IOException("Not implemented");
            }
            // the IndexColorModel uses RGB-based color values
            // which leads to 3 color components and a optional alpha channel
            int numberOfComponents = 3 + (hasAlpha ? 1 : 0);
            int buffersize = (size + 1) * numberOfComponents;
            byte[] colorValues = new byte[buffersize];
            byte[] inData = new byte[baseColorModel.getNumComponents()];
            int bufferIndex = 0;
            for (int i = 0; i <= size; i++) {
                System.arraycopy(index, i * inData.length, inData, 0, inData.length);
                // convert the indexed color values to RGB 
                colorValues[bufferIndex] = (byte) baseColorModel.getRed(inData);
                colorValues[bufferIndex + 1] = (byte) baseColorModel.getGreen(inData);
                colorValues[bufferIndex + 2] = (byte) baseColorModel.getBlue(inData);
                if (hasAlpha) {
                    colorValues[bufferIndex + 3] = (byte) baseColorModel.getAlpha(inData);
                }
                bufferIndex += numberOfComponents;
            }
            if (maskArray != null && maskArray instanceof COSArray) {
                cm = new IndexColorModel(bpc, size + 1, colorValues, 0, hasAlpha,
                        ((COSArray) maskArray).getInt(0));
            } else {
                cm = new IndexColorModel(bpc, size + 1, colorValues, 0, hasAlpha);
            }
        } else if (colorspace instanceof PDSeparation) {
            PDSeparation csSeparation = (PDSeparation) colorspace;
            int numberOfComponents = csSeparation.getAlternateColorSpace().getNumberOfComponents();
            PDFunction tintTransformFunc = csSeparation.getTintTransform();
            COSArray decode = getDecode();
            // we have to invert the tint-values,
            // if the Decode array exists and consists of (1,0)
            boolean invert = decode != null && decode.getInt(0) == 1;
            // TODO add interpolation for other decode values then 1,0
            int maxValue = (int) Math.pow(2, bpc) - 1;
            // destination array
            byte[] mappedData = new byte[width * height * numberOfComponents];
            int rowLength = width * numberOfComponents;
            float[] input = new float[1];
            for (int i = 0; i < height; i++) {
                int rowOffset = i * rowLength;
                for (int j = 0; j < width; j++) {
                    // scale tint values to a range of 0...1
                    int value = (array[i * width + j] + 256) % 256;
                    if (invert) {
                        input[0] = 1 - (value / maxValue);
                    } else {
                        input[0] = value / maxValue;
                    }
                    float[] mappedColor = tintTransformFunc.eval(input);
                    int columnOffset = j * numberOfComponents;
                    for (int k = 0; k < numberOfComponents; k++) {
                        // redo scaling for every single color value 
                        float mappedValue = mappedColor[k];
                        mappedData[rowOffset + columnOffset + k] = (byte) (mappedValue * maxValue);
                    }
                }
            }
            array = mappedData;
            cm = colorspace.createColorModel(bpc);
        } else if (bpc == 1) {
            byte[] map = null;
            if (colorspace instanceof PDDeviceGray) {
                COSArray decode = getDecode();
                // we have to invert the b/w-values,
                // if the Decode array exists and consists of (1,0)
                if (decode != null && decode.getInt(0) == 1) {
                    map = new byte[] { (byte) 0xff };
                } else {
                    map = new byte[] { (byte) 0x00, (byte) 0xff };
                }
            } else if (colorspace instanceof PDICCBased) {
                if (((PDICCBased) colorspace).getNumberOfComponents() == 1) {
                    map = new byte[] { (byte) 0xff };
                } else {
                    map = new byte[] { (byte) 0x00, (byte) 0xff };
                }
            } else {
                map = new byte[] { (byte) 0x00, (byte) 0xff };
            }
            cm = new IndexColorModel(bpc, map.length, map, map, map, Transparency.OPAQUE);
        } else {
            if (colorspace instanceof PDICCBased) {
                if (((PDICCBased) colorspace).getNumberOfComponents() == 1) {
                    byte[] map = new byte[] { (byte) 0xff };
                    cm = new IndexColorModel(bpc, 1, map, map, map, Transparency.OPAQUE);
                } else {
                    cm = colorspace.createColorModel(bpc);
                }
            } else {
                cm = colorspace.createColorModel(bpc);
            }
        }

        LOG.debug("ColorModel: " + cm.toString());
        WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
        DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
        byte[] bufferData = buffer.getData();

        System.arraycopy(array, 0, bufferData, 0,
                (array.length < bufferData.length ? array.length : bufferData.length));
        image = new BufferedImage(cm, raster, false, null);

        // If there is a 'soft mask' image then we use that as a transparency mask.
        PDXObjectImage smask = getSMaskImage();
        if (smask != null) {
            BufferedImage smaskBI = smask.getRGBImage();

            COSArray decodeArray = smask.getDecode();

            CompositeImage compositeImage = new CompositeImage(image, smaskBI);
            BufferedImage rgbImage = compositeImage.createMaskedImage(decodeArray);

            return rgbImage;
        } else if (getImageMask()) {
            BufferedImage stencilMask = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D graphics = (Graphics2D) stencilMask.getGraphics();
            if (getStencilColor() != null) {
                graphics.setColor(getStencilColor().getJavaColor());
            } else {
                // this might happen when using ExractImages, see PDFBOX-1145
                LOG.debug("no stencil color for PixelMap found, using Color.BLACK instead.");
                graphics.setColor(Color.BLACK);
            }

            graphics.fillRect(0, 0, width, height);
            // assume default values ([0,1]) for the DecodeArray
            // TODO DecodeArray == [1,0]
            graphics.setComposite(AlphaComposite.DstIn);
            graphics.drawImage(image, null, 0, 0);
            return stencilMask;
        } else {
            // if there is no mask, use the unaltered image.
            return image;
        }
    } catch (Exception exception) {
        LOG.error(exception, exception);
        //A NULL return is caught in pagedrawer.Invoke.process() so don't re-throw.
        //Returning the NULL falls through to Phillip Koch's TODO section.
        return null;
    }
}

From source file:ded.ui.DiagramController.java

/** Check to see if the font is rendering properly.  I have had a
  * lot of trouble getting this to work on a wide range of
  * machines and JVMs.  If the font rendering does not work, just
  * alert the user to the problem but keep going. */
public void checkFontRendering() {
    // Render the glyph for 'A' in a box just large enough to
    // contain it when rendered properly.
    int width = 9;
    int height = 11;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.createGraphics();
    ColorModel colorModel = bi.getColorModel();

    g.setColor(Color.WHITE);//from   w ww.j  a v  a  2  s .  c  om
    g.fillRect(0, 0, width, height);

    g.setColor(Color.BLACK);
    g.setFont(this.dedWindow.diagramFont);
    g.drawString("A", 0, 10);

    // Print that glyph as a string.
    StringBuilder sb = new StringBuilder();
    for (int y = 0; y < height; y++) {
        int bits = 0;
        for (int x = 0; x < width; x++) {
            int pixel = bi.getRGB(x, y);
            int red = colorModel.getRed(pixel);
            int green = colorModel.getGreen(pixel);
            int blue = colorModel.getBlue(pixel);
            int alpha = colorModel.getAlpha(pixel);
            boolean isWhite = (red == 255 && green == 255 && blue == 255 && alpha == 255);
            boolean isBlack = (red == 0 && green == 0 && blue == 0 && alpha == 255);
            sb.append(
                    isWhite ? "_" : isBlack ? "X" : ("(" + red + "," + green + "," + blue + "," + alpha + ")"));

            bits <<= 1;
            if (!isWhite) {
                bits |= 1;
            }
        }
        sb.append(String.format("  (0x%03X)\n", bits));
    }

    // Also include some of the font metrics.
    FontMetrics fm = g.getFontMetrics();
    sb.append("fm: ascent=" + fm.getAscent() + " leading=" + fm.getLeading() + " charWidth('A')="
            + fm.charWidth('A') + " descent=" + fm.getDescent() + " height=" + fm.getHeight() + "\n");

    String actualGlyph = sb.toString();

    g.dispose();

    // The expected glyph and metrics.
    String expectedGlyph = "_________  (0x000)\n" + "____X____  (0x010)\n" + "___X_X___  (0x028)\n"
            + "___X_X___  (0x028)\n" + "__X___X__  (0x044)\n" + "__X___X__  (0x044)\n" + "__XXXXX__  (0x07C)\n"
            + "_X_____X_  (0x082)\n" + "_X_____X_  (0x082)\n" + "_X_____X_  (0x082)\n" + "_________  (0x000)\n"
            + "fm: ascent=10 leading=1 charWidth('A')=9 descent=3 height=14\n";

    if (!expectedGlyph.equals(actualGlyph)) {
        // Currently, this is known to happen when using OpenJDK 6
        // and 7, with 6 being close to right and 7 being very bad.
        // I also have reports of it happening on certain Mac OS/X
        // systems, but I haven't been able to determine what the
        // important factor there is.
        String warningMessage = "There is a problem with the font rendering.  The glyph "
                + "for the letter 'A' should look like:\n" + expectedGlyph + "but it renders as:\n"
                + actualGlyph + "\n" + "This probably means there is a bug in the TrueType "
                + "font library.  You might try a different Java version.  "
                + "(I'm working on how to solve this permanently.)";
        System.err.println(warningMessage);
        this.log(warningMessage);
        SwingUtil.errorMessageBox(null /*component*/, warningMessage);
    }
}