Example usage for java.awt.image WritableRaster getHeight

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

Introduction

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

Prototype

public final int getHeight() 

Source Link

Document

Returns the height in pixels of the Raster.

Usage

From source file:tilt.image.Blob.java

/**
 * Test with small image/* w  w  w . ja  va 2 s  .  c o  m*/
 * @param args ignored
 */
public static void main(String[] args) {
    try {
        Options opts = new Options(new JSONObject());
        String url = "http://ecdosis.net/test.png";
        Double[][] cc = { { 0.0, 0.0 }, { 100.0, 0.0 }, { 100.0, 100.0 }, { 0.0, 100.0 } };
        Picture p = new Picture(opts, url, new TextIndex("", ""), cc, InetAddress.getByName("127.0.0.1"));
        p.convertToTwoTone();
        BufferedImage bandw = ImageIO.read(p.twotone);
        WritableRaster wr = bandw.getRaster();
        Blob largest = null;
        int max = 0;
        int[] iArray = new int[1];
        WritableRaster darkRegions = bandw.copyData(null);
        Blob.setToWhite(darkRegions);
        for (int y = 0; y < wr.getHeight(); y++) {
            for (int x = 0; x < wr.getWidth(); x++) {
                Blob b = new Blob(darkRegions, opts, null);
                wr.getPixel(x, y, iArray);
                if (iArray[0] == 0) {
                    b.expandArea(wr, new Point(x, y));
                    if (b.size() > max) {
                        System.out.println("Found new blob at " + x + "," + y + " size=" + b.size());
                        largest = b;
                        max = b.size();
                    }
                }
            }
        }
        if (largest != null) {
            WritableRaster dirt = bandw.copyData(null);
            Blob.setToWhite(dirt);
            largest.save(dirt, wr, largest.firstBlackPixel);
            System.out.println(largest.toString());
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
}

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

/**
 * Invert pixel color if the image has a adobe marker ... not used now
 * @param raster/*from   w  w  w .  j  ava  2s .  co m*/
 */
@SuppressWarnings("unused")
private static void convertInvertedColors(WritableRaster raster) {
    int height = raster.getHeight();
    int width = raster.getWidth();
    int stride = width * 4;
    int[] pixelRow = new int[stride];
    for (int h = 0; h < height; h++) {
        raster.getPixels(0, h, width, 1, pixelRow);
        for (int x = 0; x < stride; x++)
            pixelRow[x] = 255 - pixelRow[x];
        raster.setPixels(0, h, width, 1, pixelRow);
    }
}

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

/**
 * Convert image profile from Ycck to Cmyk
 * @param raster/* w  w  w. ja v a2  s  . co  m*/
 */
private static void convertYcckToCmyk(WritableRaster raster) {
    int height = raster.getHeight();
    int width = raster.getWidth();
    int stride = width * 4;
    int[] pixelRow = new int[stride];
    for (int h = 0; h < height; h++) {
        raster.getPixels(0, h, width, 1, pixelRow);

        for (int x = 0; x < stride; x += 4) {
            int y = pixelRow[x];
            int cb = pixelRow[x + 1];
            int cr = pixelRow[x + 2];

            int c = (int) (y + 1.402 * cr - 178.956);
            int m = (int) (y - 0.34414 * cb - 0.71414 * cr + 135.95984);
            y = (int) (y + 1.772 * cb - 226.316);

            if (c < 0)
                c = 0;
            else if (c > 255)
                c = 255;
            if (m < 0)
                m = 0;
            else if (m > 255)
                m = 255;
            if (y < 0)
                y = 0;
            else if (y > 255)
                y = 255;

            pixelRow[x] = 255 - c;
            pixelRow[x + 1] = 255 - m;
            pixelRow[x + 2] = 255 - y;
        }
        raster.setPixels(0, h, width, 1, pixelRow);
    }
}

From source file:tilt.image.Blob.java

/**
 * Set a raster to white (now only called by Picture)
 * @param wr the raster to white out//from www.  ja va 2s .  c  om
 * @return the average number of black pixels per pixel
 */
public static float setToWhite(WritableRaster wr) {
    int width = wr.getWidth();
    int height = wr.getHeight();
    Rectangle r = new Rectangle(0, 0, width, height);
    return setToWhite(wr, r);
}

From source file:org.shaman.terrain.vegetation.ImpositorCreator.java

public static void convertScreenShot(ByteBuffer bgraBuf, BufferedImage out) {
    WritableRaster wr = out.getRaster();
    DataBufferByte db = (DataBufferByte) wr.getDataBuffer();

    byte[] cpuArray = db.getData();

    // copy native memory to java memory
    bgraBuf.clear();/*  w w w.  j a va2  s.  c  o m*/
    bgraBuf.get(cpuArray);
    bgraBuf.clear();

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

    // flip the components the way AWT likes them

    // calcuate half of height such that all rows of the array are written to
    // e.g. for odd heights, write 1 more scanline
    int heightdiv2ceil = height % 2 == 1 ? (height / 2) + 1 : height / 2;
    for (int y = 0; y < heightdiv2ceil; y++) {
        for (int x = 0; x < width; x++) {
            int inPtr = (y * width + x) * 4;
            int outPtr = ((height - y - 1) * width + x) * 4;

            byte b1 = cpuArray[inPtr + 0];
            byte g1 = cpuArray[inPtr + 1];
            byte r1 = cpuArray[inPtr + 2];
            byte a1 = cpuArray[inPtr + 3];

            byte b2 = cpuArray[outPtr + 0];
            byte g2 = cpuArray[outPtr + 1];
            byte r2 = cpuArray[outPtr + 2];
            byte a2 = cpuArray[outPtr + 3];

            cpuArray[outPtr + 0] = a1;
            cpuArray[outPtr + 1] = r1;//b1;
            cpuArray[outPtr + 2] = g1;
            cpuArray[outPtr + 3] = b1;//r1;

            cpuArray[inPtr + 0] = a2;
            cpuArray[inPtr + 1] = r2;//b2;
            cpuArray[inPtr + 2] = g2;
            cpuArray[inPtr + 3] = b2;//r2;
        }
    }
}

From source file:tilt.image.Blob.java

public static float setToWhite(WritableRaster wr, Rectangle bounds) {
    int blackPixels = 0;
    int yEnd = bounds.height + bounds.y;
    int xEnd = bounds.width + bounds.x;
    // set all to white
    int[] iArray = new int[bounds.width];
    int[] dirty = new int[bounds.width];
    for (int x = 0; x < bounds.width; x++)
        iArray[x] = 255;//  w  ww.j  a va2 s  .  c  om
    for (int y = bounds.y; y < yEnd; y++) {
        wr.getPixels(bounds.x, y, bounds.width, 1, dirty);
        for (int i = 0; i < dirty.length; i++)
            if (dirty[i] == 0)
                blackPixels++;
        wr.setPixels(bounds.x, y, bounds.width, 1, iArray);
    }
    return (float) blackPixels / (float) (wr.getWidth() * wr.getHeight());
}

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 {//from  ww  w.j  av  a  2s. co 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);/*from  w w w . ja va  2  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:CheckAlignmentOpImage.java

public Raster computeTile(int x, int y) {
    Raster r1 = source1.getTile(x, y);
    Raster r2 = source2.getTile(x, y);

    int xBounds = r1.getWidth();
    if (r2.getWidth() < xBounds)
        xBounds = r2.getWidth();//w w w . j av  a  2 s.  co  m
    int yBounds = r1.getHeight();
    if (r2.getHeight() < yBounds)
        yBounds = r2.getHeight();

    WritableRaster wr;
    wr = r1.createCompatibleWritableRaster(xBounds, yBounds);

    int tmpi;
    int tmpj;
    for (int i = 0; i < wr.getWidth(); i++)
        for (int j = 0; j < wr.getHeight(); j++) {
            tmpi = i / samplingPeriod;
            tmpj = j / samplingPeriod;
            if ((tmpi % 2 == 0) && (tmpj % 2 == 0))
                wr.setDataElements(i, j, r2.getDataElements(i, j, null));
            else if ((tmpi % 2 != 0) && (tmpj % 2 != 0))
                wr.setDataElements(i, j, r2.getDataElements(i, j, null));
            else
                wr.setDataElements(i, j, r1.getDataElements(i, j, null));
        }
    return wr;
}

From source file:GraphicsUtil.java

protected static void mult_INT_PACK_Data(WritableRaster wr) {
    // System.out.println("Multiply Int: " + wr);

    SinglePixelPackedSampleModel sppsm;
    sppsm = (SinglePixelPackedSampleModel) wr.getSampleModel();

    final int width = wr.getWidth();

    final int scanStride = sppsm.getScanlineStride();
    DataBufferInt db = (DataBufferInt) wr.getDataBuffer();
    final int base = (db.getOffset() + sppsm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(),
            wr.getMinY() - wr.getSampleModelTranslateY()));
    // Access the pixel data array
    final int pixels[] = db.getBankData()[0];
    for (int y = 0; y < wr.getHeight(); y++) {
        int sp = base + y * scanStride;
        final int end = sp + width;
        while (sp < end) {
            int pixel = pixels[sp];
            int a = pixel >>> 24;
            if ((a >= 0) && (a < 255)) {
                pixels[sp] = ((a << 24) | ((((pixel & 0xFF0000) * a) >> 8) & 0xFF0000)
                        | ((((pixel & 0x00FF00) * a) >> 8) & 0x00FF00)
                        | ((((pixel & 0x0000FF) * a) >> 8) & 0x0000FF));
            }/*from w  w w  . ja va  2s  .co m*/
            sp++;
        }
    }
}