Example usage for java.awt.image WritableRaster getPixel

List of usage examples for java.awt.image WritableRaster getPixel

Introduction

In this page you can find the example usage for java.awt.image WritableRaster getPixel.

Prototype

public int[] getPixel(int x, int y, int[] iArray) 

Source Link

Document

Returns the samples in an array of int for the specified pixel.

Usage

From source file:GraphicsUtil.java

public static void divideAlpha(WritableRaster wr) {
    if (is_BYTE_COMP_Data(wr.getSampleModel()))
        divide_BYTE_COMP_Data(wr);
    else if (is_INT_PACK_Data(wr.getSampleModel(), true))
        divide_INT_PACK_Data(wr);
    else {//  ww  w .ja va  2  s . c  o  m
        int x0, x1, y0, y1, a, b;
        float ialpha;
        int bands = wr.getNumBands();
        int[] pixel = null;

        x0 = wr.getMinX();
        x1 = x0 + wr.getWidth();
        y0 = wr.getMinY();
        y1 = y0 + wr.getHeight();
        for (int y = y0; y < y1; y++)
            for (int x = x0; x < x1; x++) {
                pixel = wr.getPixel(x, y, pixel);
                a = pixel[bands - 1];
                if ((a > 0) && (a < 255)) {
                    ialpha = 255 / (float) a;
                    for (b = 0; b < bands - 1; b++)
                        pixel[b] = (int) (pixel[b] * ialpha + 0.5f);
                    wr.setPixel(x, y, pixel);
                }
            }
    }
}

From source file:GraphicsUtil.java

public static void multiplyAlpha(WritableRaster wr) {
    if (is_BYTE_COMP_Data(wr.getSampleModel()))
        mult_BYTE_COMP_Data(wr);// w  w w . ja v a2 s.  c o m
    else if (is_INT_PACK_Data(wr.getSampleModel(), true))
        mult_INT_PACK_Data(wr);
    else {
        int[] pixel = null;
        int bands = wr.getNumBands();
        float norm = 1f / 255f;
        int x0, x1, y0, y1, a, b;
        float alpha;
        x0 = wr.getMinX();
        x1 = x0 + wr.getWidth();
        y0 = wr.getMinY();
        y1 = y0 + wr.getHeight();
        for (int y = y0; y < y1; y++)
            for (int x = x0; x < x1; x++) {
                pixel = wr.getPixel(x, y, pixel);
                a = pixel[bands - 1];
                if ((a >= 0) && (a < 255)) {
                    alpha = a * norm;
                    for (b = 0; b < bands - 1; b++)
                        pixel[b] = (int) (pixel[b] * alpha + 0.5f);
                    wr.setPixel(x, y, pixel);
                }
            }
    }
}

From source file:tilt.image.Blob.java

/**
 * Check if there is one dirty dot not already seen.
 * @param wr the raster to search/* w w  w. java 2s  . co  m*/
 * @param start the first blackdot
 * @param iArray the array containing the dirty dot
 */
void checkDirtyDot(WritableRaster wr, Point start, int[] iArray) {
    hull = new ArrayList<>();
    // do it without recursion
    stack.push(start);
    while (!stack.empty()) {
        Point p = stack.pop();
        wr.getPixel(p.x, p.y, iArray);
        if (iArray[0] <= getBlackLevel(p)) {
            if (dirt != null)
                dirt.getPixel(p.x, p.y, iArray);
            else
                parent.getPixel(p.x, p.y, iArray);
            if (iArray[0] != 0) {
                iArray[0] = 0;
                // set one pixel known to be black 
                setBlackPixel(p.x, p.y, iArray);
                // top
                if (p.y > 0)
                    stack.push(new Point(p.x, p.y - 1));
                // bottom
                if (p.y < wr.getHeight() - 1)
                    stack.push(new Point(p.x, p.y + 1));
                // left
                if (p.x > 0)
                    stack.push(new Point(p.x - 1, p.y));
                // right
                if (p.x < wr.getWidth() - 1)
                    stack.push(new Point(p.x + 1, p.y));
                // bot-left
                if (p.y < wr.getHeight() - 1 && p.x > 0)
                    stack.push(new Point(p.x - 1, p.y + 1));
                // bot-right
                if (p.y < wr.getHeight() - 1 && p.x < wr.getWidth() - 1)
                    stack.push(new Point(p.x + 1, p.y + 1));
                // top-left
                if (p.y > 0 && p.x > 0)
                    stack.push(new Point(p.x - 1, p.y - 1));
                // top-right
                if (p.y > 0 && p.x < wr.getWidth() - 1)
                    stack.push(new Point(p.x + 1, p.y - 1));
            }
        } else
            hull.add(p);
    }
}

From source file:org.apache.jetspeed.security.mfa.impl.CaptchaImageResource.java

protected void noiseEffects(Graphics2D gfx, BufferedImage image) {
    // XOR circle
    int dx = randomInt(width, 2 * width);
    int dy = randomInt(width, 2 * height);
    int x = randomInt(0, width / 2);
    int y = randomInt(0, height / 2);

    gfx.setXORMode(Color.GRAY);/*from  ww w. ja v  a  2 s. c o  m*/
    if (config.isFontSizeRandom())
        gfx.setStroke(new BasicStroke(randomInt(config.getFontSize() / 8, config.getFontSize() / 2)));
    else
        gfx.setStroke(new BasicStroke(config.getFontSize()));

    gfx.drawOval(x, y, dx, dy);

    WritableRaster rstr = image.getRaster();
    int[] vColor = new int[3];
    int[] oldColor = new int[3];
    Random vRandom = new Random(System.currentTimeMillis());
    // noise
    for (x = 0; x < width; x++) {
        for (y = 0; y < height; y++) {
            rstr.getPixel(x, y, oldColor);

            // hard noise
            vColor[0] = 0 + (int) (Math.floor(vRandom.nextFloat() * 1.03) * 255);
            // soft noise
            vColor[0] = vColor[0] ^ (170 + (int) (vRandom.nextFloat() * 80));
            // xor to image
            vColor[0] = vColor[0] ^ oldColor[0];
            vColor[1] = vColor[0];
            vColor[2] = vColor[0];

            rstr.setPixel(x, y, vColor);
        }
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.java

private BufferedImage applyMask(BufferedImage image, BufferedImage mask, boolean isSoft) throws IOException {
    if (mask == null) {
        return image;
    }//from  w ww .  j a  va 2 s.c o  m

    int width = image.getWidth();
    int height = image.getHeight();

    // scale mask to fit image, or image to fit mask, whichever is larger
    if (mask.getWidth() < width || mask.getHeight() < height) {
        mask = scaleImage(mask, width, height);
    } else if (mask.getWidth() > width || mask.getHeight() > height) {
        width = mask.getWidth();
        height = mask.getHeight();
        image = scaleImage(image, width, height);
    }

    // compose to ARGB
    BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster src = image.getRaster();
    WritableRaster dest = masked.getRaster();
    WritableRaster alpha = mask.getRaster();

    float[] rgb = new float[4];
    float[] rgba = new float[4];
    float[] alphaPixel = null;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            src.getPixel(x, y, rgb);

            rgba[0] = rgb[0];
            rgba[1] = rgb[1];
            rgba[2] = rgb[2];

            alphaPixel = alpha.getPixel(x, y, alphaPixel);
            if (isSoft) {
                rgba[3] = alphaPixel[0];
            } else {
                rgba[3] = 255 - alphaPixel[0];
            }

            dest.setPixel(x, y, rgba);
        }
    }

    return masked;
}

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

private BufferedImage applyMask(BufferedImage image, BufferedImage mask, boolean isSoft) {
    if (mask == null) {
        return image;
    }/*w  w w.  j a  v a  2  s. c  o m*/

    int width = image.getWidth();
    int height = image.getHeight();

    // scale mask to fit image, or image to fit mask, whichever is larger
    if (mask.getWidth() < width || mask.getHeight() < height) {
        mask = scaleImage(mask, width, height);
    } else if (mask.getWidth() > width || mask.getHeight() > height) {
        width = mask.getWidth();
        height = mask.getHeight();
        image = scaleImage(image, width, height);
    }

    // compose to ARGB
    BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster src = image.getRaster();
    WritableRaster dest = masked.getRaster();
    WritableRaster alpha = mask.getRaster();

    float[] rgb = new float[4];
    float[] rgba = new float[4];
    float[] alphaPixel = null;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            src.getPixel(x, y, rgb);

            rgba[0] = rgb[0];
            rgba[1] = rgb[1];
            rgba[2] = rgb[2];

            alphaPixel = alpha.getPixel(x, y, alphaPixel);
            if (isSoft) {
                rgba[3] = alphaPixel[0];
            } else {
                rgba[3] = 255 - alphaPixel[0];
            }

            dest.setPixel(x, y, rgba);
        }
    }

    return masked;
}

From source file:tilt.image.page.Line.java

/**
 * Move the leftmost x position up to the first pixel that is black
 * @param wr the raster of the image/*from www  .j a va  2s  .c o  m*/
 * @param hScale the width of a rectangle or slice
 * @param vScale the height of a rectangle or slice
 * @param black value of a black pixel
 */
public void refineLeft(WritableRaster wr, int hScale, int vScale, int black) {
    if (points.size() > 0) {
        int[] iArray = new int[1];
        java.awt.Point leftMost = points.get(0).toAwt();
        int top = Math.round(leftMost.y);
        int bottom = top + vScale;
        int right = leftMost.x + hScale;
        int left = (leftMost.x - hScale > 0) ? leftMost.x - hScale : 0;
        int nPixels = 0;
        for (int x = left; x < right; x++) {
            for (int y = top; y <= bottom; y++) {
                wr.getPixel(x, y, iArray);
                if (iArray[0] <= black) {
                    nPixels++;
                    if (nPixels >= 2) {
                        leftMost.x = x;
                        break;
                    }
                }
            }
            if (nPixels >= 2)
                break;
        }
        points.get(0).x = leftMost.x;
        points.get(0).y = leftMost.y;
    }
}

From source file:tilt.image.page.Line.java

/**
 * Move the rightmost x position inwards to the first pixel that is black
 * @param wr the raster of the image//from  w  w  w .ja  v  a2 s  .  co m
 * @param hScale the width of a rectangle or slice
 * @param vScale the height of a rectangle or slice
 * @param black value of a black pixel
 */
public void refineRight(WritableRaster wr, int hScale, int vScale, int black) {
    boolean boundary = false;
    int[] iArray = new int[1];
    while (!boundary && points.size() > 0) {
        java.awt.Point rightMost = points.get(points.size() - 1).toAwt();
        int top = rightMost.y - (vScale / 2);
        int bottom = top + (vScale / 2);
        int right = rightMost.x;
        int left = (rightMost.x - hScale > 0) ? rightMost.x - hScale : 0;
        int nPixels = 0;
        for (int x = right; x >= left; x--) {
            int y;
            for (y = top; y <= bottom; y++) {
                wr.getPixel(x, y, iArray);
                if (iArray[0] <= black) {
                    nPixels++;
                    if (nPixels >= 2) {
                        rightMost.x = x;
                        boundary = true;
                        break;
                    }
                }
            }
            if (nPixels >= 2)
                break;
        }
        if (boundary) {
            Point endP = points.get(points.size() - 1);
            endP.x = rightMost.x;
            checkPoint(points.size() - 1);
        } else {
            points.remove(points.size() - 1);
        }
    }
}

From source file:tilt.image.Blob.java

/**
 * Is this blob surrounded by a wide band of white?
 * @param wr the raster to search in/*from  www  .ja va  2 s . c  om*/
 * @param hStandoff the amount of white horizontal standoff
 * @param vStandoff the amount of vertical white standoff
 * @return true if it is
 */
boolean hasWhiteStandoff(WritableRaster wr, int hStandoff, int vStandoff) {
    // 1. original blob bounds
    Rectangle inner = new Rectangle(topLeft().x, topLeft().y, getWidth(), getHeight());
    Rectangle outer = (Rectangle) inner.clone();
    // 2. outset rect by standoff
    if (outer.x >= hStandoff)
        outer.x -= hStandoff;
    else
        outer.x = 0;
    if (outer.y - vStandoff > 0)
        outer.y -= vStandoff;
    else
        outer.y = 0;
    if (outer.x + outer.width + hStandoff * 2 < wr.getWidth())
        outer.width += hStandoff * 2;
    else
        outer.width = wr.getWidth() - outer.x;
    if (outer.y + outer.height + vStandoff * 2 < wr.getHeight())
        outer.height += vStandoff * 2;
    else
        outer.height = wr.getHeight() - outer.y;
    // 3. test for black pixels in that area
    int[] iArray = new int[1];
    int maxY = outer.y + outer.height;
    int maxX = outer.x + outer.width;
    int nBlacks = 0;
    int maxRogues = opts.getInt(Options.Keys.maxRoguePixels);
    for (int y = outer.y; y < maxY; y++) {
        for (int x = outer.x; x < maxX; x++) {
            Point loc = new Point(x, y);
            if (!inner.contains(loc)) {
                wr.getPixel(x, y, iArray);
                if (iArray[0] == 0) {
                    if (nBlacks == maxRogues) {
                        return false;
                    } else
                        nBlacks++;
                }
            }
        }
    }
    return true;
}

From source file:tilt.image.Picture.java

/**
 * Convert from greyscale to twotone (black and white)
 * Adapted from OCRopus/*  ww  w . j  a v  a2 s  .  c  om*/
 * Copyright 2006-2008 Deutsches Forschungszentrum fuer Kuenstliche 
 * Intelligenz or its licensors, as applicable.
 * http://ocropus.googlecode.com/svn/trunk/ocr-binarize/ocr-binarize-sauvola.cc
 * @throws Exception 
 */
void convertToTwoTone() throws ImageException {
    try {
        int MAXVAL = 256;
        double k = 0.34;
        if (greyscale == null)
            convertToGreyscale();
        BufferedImage grey = ImageIO.read(greyscale);
        WritableRaster grey_image = grey.getRaster();
        WritableRaster bin_image = grey.copyData(null);
        int square = (int) Math.floor(grey_image.getWidth() * 0.025);
        if (square == 0)
            square = Math.min(20, grey_image.getWidth());
        if (square > grey_image.getHeight())
            square = grey_image.getHeight();
        int whalf = square >> 1;
        if (whalf == 0)
            throw new Exception("whalf==0!");
        int image_width = grey_image.getWidth();
        int image_height = grey_image.getHeight();
        // Calculate the integral image, and integral of the squared image
        // original algorithm ate up too much memory, use floats for longs
        float[][] integral_image = new float[image_width][image_height];
        float[][] rowsum_image = new float[image_width][image_height];
        float[][] integral_sqimg = new float[image_width][image_height];
        float[][] rowsum_sqimg = new float[image_width][image_height];
        int xmin, ymin, xmax, ymax;
        double diagsum, idiagsum, diff, sqdiagsum, sqidiagsum, sqdiff, area;
        double mean, std, threshold;
        // for get/setPixel
        int[] iArray = new int[1];
        int[] oArray = new int[1];
        for (int j = 0; j < image_height; j++) {
            grey_image.getPixel(0, j, iArray);
            rowsum_image[0][j] = iArray[0];
            rowsum_sqimg[0][j] = iArray[0] * iArray[0];
        }
        for (int i = 1; i < image_width; i++) {
            for (int j = 0; j < image_height; j++) {
                grey_image.getPixel(i, j, iArray);
                rowsum_image[i][j] = rowsum_image[i - 1][j] + iArray[0];
                rowsum_sqimg[i][j] = rowsum_sqimg[i - 1][j] + iArray[0] * iArray[0];
            }
        }
        for (int i = 0; i < image_width; i++) {
            integral_image[i][0] = rowsum_image[i][0];
            integral_sqimg[i][0] = rowsum_sqimg[i][0];
        }
        for (int i = 0; i < image_width; i++) {
            for (int j = 1; j < image_height; j++) {
                integral_image[i][j] = integral_image[i][j - 1] + rowsum_image[i][j];
                integral_sqimg[i][j] = integral_sqimg[i][j - 1] + rowsum_sqimg[i][j];
            }
        }
        // compute mean and std.dev. using the integral image
        for (int i = 0; i < image_width; i++) {
            for (int j = 0; j < image_height; j++) {
                xmin = Math.max(0, i - whalf);
                ymin = Math.max(0, j - whalf);
                xmax = Math.min(image_width - 1, i + whalf);
                ymax = Math.min(image_height - 1, j + whalf);
                area = (xmax - xmin + 1) * (ymax - ymin + 1);
                grey_image.getPixel(i, j, iArray);
                // area can't be 0 here
                if (area == 0)
                    throw new Exception("area can't be 0 here!");
                if (xmin == 0 && ymin == 0) {
                    // Point at origin
                    diff = integral_image[xmax][ymax];
                    sqdiff = integral_sqimg[xmax][ymax];
                } else if (xmin == 0 && ymin != 0) {
                    // first column
                    diff = integral_image[xmax][ymax] - integral_image[xmax][ymin - 1];
                    sqdiff = integral_sqimg[xmax][ymax] - integral_sqimg[xmax][ymin - 1];
                } else if (xmin != 0 && ymin == 0) {
                    // first row
                    diff = integral_image[xmax][ymax] - integral_image[xmin - 1][ymax];
                    sqdiff = integral_sqimg[xmax][ymax] - integral_sqimg[xmin - 1][ymax];
                } else {
                    // rest of the image
                    diagsum = integral_image[xmax][ymax] + integral_image[xmin - 1][ymin - 1];
                    idiagsum = integral_image[xmax][ymin - 1] + integral_image[xmin - 1][ymax];
                    diff = diagsum - idiagsum;
                    sqdiagsum = integral_sqimg[xmax][ymax] + integral_sqimg[xmin - 1][ymin - 1];
                    sqidiagsum = integral_sqimg[xmax][ymin - 1] + integral_sqimg[xmin - 1][ymax];
                    sqdiff = sqdiagsum - sqidiagsum;
                }
                mean = diff / area;
                std = Math.sqrt((sqdiff - diff * diff / area) / (area - 1));
                threshold = mean * (1 + k * ((std / 128) - 1));
                if (iArray[0] < threshold)
                    oArray[0] = 0;
                else
                    oArray[0] = MAXVAL - 1;
                bin_image.setPixel(i, j, oArray);
            }
        }
        twotone = File.createTempFile(PictureRegistry.PREFIX, PictureRegistry.SUFFIX);
        grey.setData(bin_image);
        ImageIO.write(grey, "png", twotone);
    } catch (Exception e) {
        throw new ImageException(e);
    }
}