Example usage for java.awt.image WritableRaster getDataBuffer

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

Introduction

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

Prototype

public DataBuffer getDataBuffer() 

Source Link

Document

Returns the DataBuffer associated with this Raster.

Usage

From source file:GraphicsUtil.java

protected static void divide_INT_PACK_Data(WritableRaster wr) {
    // System.out.println("Divide Int");

    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()));
    int pixel, a, aFP;
    // 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) {
            pixel = pixels[sp];/*  ww w. j  ava 2 s.  co m*/
            a = pixel >>> 24;
            if (a <= 0) {
                pixels[sp] = 0x00FFFFFF;
            } else if (a < 255) {
                aFP = (0x00FF0000 / a);
                pixels[sp] = ((a << 24) | (((((pixel & 0xFF0000) >> 16) * aFP) & 0xFF0000))
                        | (((((pixel & 0x00FF00) >> 8) * aFP) & 0xFF0000) >> 8)
                        | (((((pixel & 0x0000FF)) * aFP) & 0xFF0000) >> 16));
            }
            sp++;
        }
    }
}

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));
            }// w w w.j a va 2s.  co  m
            sp++;
        }
    }
}

From source file:GraphicsUtil.java

protected static void divide_INT_PACK_Data(WritableRaster wr) {
    // System.out.println("Divide Int");

    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) {
                pixels[sp] = 0x00FFFFFF;
            } else if (a < 255) {
                int aFP = (0x00FF0000 / a);
                pixels[sp] = ((a << 24) | (((((pixel & 0xFF0000) >> 16) * aFP) & 0xFF0000))
                        | (((((pixel & 0x00FF00) >> 8) * aFP) & 0xFF0000) >> 8)
                        | (((((pixel & 0x0000FF)) * aFP) & 0xFF0000) >> 16));
            }/*from  w w w .  j  a va2  s .  co m*/
            sp++;
        }
    }
}

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)) { // this does NOT include a == 255 (0xff) !
                pixels[sp] = ((a << 24) | ((((pixel & 0xFF0000) * a) >> 8) & 0xFF0000)
                        | ((((pixel & 0x00FF00) * a) >> 8) & 0x00FF00)
                        | ((((pixel & 0x0000FF) * a) >> 8) & 0x0000FF));
            }/*from   www.jav  a 2  s.c om*/
            sp++;
        }
    }
}

From source file:GraphicsUtil.java

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

    ComponentSampleModel csm;/*ww w  . j a  va2  s .  c o  m*/
    csm = (ComponentSampleModel) wr.getSampleModel();

    final int width = wr.getWidth();

    final int scanStride = csm.getScanlineStride();
    final int pixStride = csm.getPixelStride();
    final int[] bandOff = csm.getBandOffsets();

    DataBufferByte db = (DataBufferByte) wr.getDataBuffer();
    final int base = (db.getOffset() + csm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(),
            wr.getMinY() - wr.getSampleModelTranslateY()));

    int a = 0;
    int aOff = bandOff[bandOff.length - 1];
    int bands = bandOff.length - 1;
    int b, i;

    // Access the pixel data array
    final byte pixels[] = db.getBankData()[0];
    for (int y = 0; y < wr.getHeight(); y++) {
        int sp = base + y * scanStride;
        final int end = sp + width * pixStride;
        while (sp < end) {
            a = pixels[sp + aOff] & 0xFF;
            if (a != 0xFF)
                for (b = 0; b < bands; b++) {
                    i = sp + bandOff[b];
                    pixels[i] = (byte) (((pixels[i] & 0xFF) * a) >> 8);
                }
            sp += pixStride;
        }
    }
}

From source file:GraphicsUtil.java

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

    ComponentSampleModel csm;/*from  w ww  .  j  ava 2 s .co m*/
    csm = (ComponentSampleModel) wr.getSampleModel();

    final int width = wr.getWidth();

    final int scanStride = csm.getScanlineStride();
    final int pixStride = csm.getPixelStride();
    final int[] bandOff = csm.getBandOffsets();

    DataBufferByte db = (DataBufferByte) wr.getDataBuffer();
    final int base = (db.getOffset() + csm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(),
            wr.getMinY() - wr.getSampleModelTranslateY()));

    int a = 0;
    int aOff = bandOff[bandOff.length - 1];
    int bands = bandOff.length - 1;
    int b, i;
    // Access the pixel data array
    final byte pixels[] = db.getBankData()[0];
    for (int y = 0; y < wr.getHeight(); y++) {
        int sp = base + y * scanStride;
        final int end = sp + width * pixStride;
        while (sp < end) {
            a = pixels[sp + aOff] & 0xFF;
            if (a == 0) {
                for (b = 0; b < bands; b++)
                    pixels[sp + bandOff[b]] = (byte) 0xFF;
            } else if (a < 255) {
                int aFP = (0x00FF0000 / a);
                for (b = 0; b < bands; b++) {
                    i = sp + bandOff[b];
                    pixels[i] = (byte) (((pixels[i] & 0xFF) * aFP) >>> 16);
                }
            }
            sp += pixStride;
        }
    }
}

From source file:GraphicsUtil.java

/**
 * Creates a new raster that has a <b>copy</b> of the data in
 * <tt>ras</tt>.  This is highly optimized for speed.  There is
 * no provision for changing any aspect of the SampleModel.
 * However you can specify a new location for the returned raster.
 *
 * This method should be used when you need to change the contents
 * of a Raster that you do not "own" (ie the result of a
 * <tt>getData</tt> call).//w ww. j  a  va  2 s.c om
 *
 * @param ras The Raster to copy.
 *
 * @param minX The x location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @param minY The y location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @return    A writable copy of <tt>ras</tt>
 */
public static WritableRaster copyRaster(Raster ras, int minX, int minY) {
    WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), new Point(0, 0));
    ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(),
            ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null);

    // Use System.arraycopy to copy the data between the two...
    DataBuffer srcDB = ras.getDataBuffer();
    DataBuffer retDB = ret.getDataBuffer();
    if (srcDB.getDataType() != retDB.getDataType()) {
        throw new IllegalArgumentException("New DataBuffer doesn't match original");
    }
    int len = srcDB.getSize();
    int banks = srcDB.getNumBanks();
    int[] offsets = srcDB.getOffsets();
    for (int b = 0; b < banks; b++) {
        switch (srcDB.getDataType()) {
        case DataBuffer.TYPE_BYTE: {
            DataBufferByte srcDBT = (DataBufferByte) srcDB;
            DataBufferByte retDBT = (DataBufferByte) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        case DataBuffer.TYPE_INT: {
            DataBufferInt srcDBT = (DataBufferInt) srcDB;
            DataBufferInt retDBT = (DataBufferInt) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        case DataBuffer.TYPE_SHORT: {
            DataBufferShort srcDBT = (DataBufferShort) srcDB;
            DataBufferShort retDBT = (DataBufferShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        case DataBuffer.TYPE_USHORT: {
            DataBufferUShort srcDBT = (DataBufferUShort) srcDB;
            DataBufferUShort retDBT = (DataBufferUShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        }
    }

    return ret;
}

From source file:GraphicsUtil.java

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

    ComponentSampleModel csm;//  w  w  w . ja v  a  2s.c  o m
    csm = (ComponentSampleModel) wr.getSampleModel();

    final int width = wr.getWidth();

    final int scanStride = csm.getScanlineStride();
    final int pixStride = csm.getPixelStride();
    final int[] bandOff = csm.getBandOffsets();

    DataBufferByte db = (DataBufferByte) wr.getDataBuffer();
    final int base = (db.getOffset() + csm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(),
            wr.getMinY() - wr.getSampleModelTranslateY()));

    int aOff = bandOff[bandOff.length - 1];
    int bands = bandOff.length - 1;

    // Access the pixel data array
    final byte[] pixels = db.getBankData()[0];
    for (int y = 0; y < wr.getHeight(); y++) {
        int sp = base + y * scanStride;
        final int end = sp + width * pixStride;
        while (sp < end) {
            int a = pixels[sp + aOff] & 0xFF;
            if (a != 0xFF)
                for (int b = 0; b < bands; b++) {
                    int i = sp + bandOff[b];
                    pixels[i] = (byte) (((pixels[i] & 0xFF) * a) >> 8);
                }
            sp += pixStride;
        }
    }
}

From source file:GraphicsUtil.java

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

    ComponentSampleModel csm;//from  www . j a va 2 s  .  co m
    csm = (ComponentSampleModel) wr.getSampleModel();

    final int width = wr.getWidth();

    final int scanStride = csm.getScanlineStride();
    final int pixStride = csm.getPixelStride();
    final int[] bandOff = csm.getBandOffsets();

    DataBufferByte db = (DataBufferByte) wr.getDataBuffer();
    final int base = (db.getOffset() + csm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(),
            wr.getMinY() - wr.getSampleModelTranslateY()));

    int aOff = bandOff[bandOff.length - 1];
    int bands = bandOff.length - 1;

    // Access the pixel data array
    final byte[] pixels = db.getBankData()[0];
    for (int y = 0; y < wr.getHeight(); y++) {
        int sp = base + y * scanStride;
        final int end = sp + width * pixStride;
        while (sp < end) {
            int a = pixels[sp + aOff] & 0xFF;
            if (a == 0) {
                for (int b = 0; b < bands; b++)
                    pixels[sp + bandOff[b]] = (byte) 0xFF;
            } else if (a < 255) { // this does NOT include a == 255 (0xff) !
                int aFP = (0x00FF0000 / a);
                for (int b = 0; b < bands; b++) {
                    int i = sp + bandOff[b];
                    pixels[i] = (byte) (((pixels[i] & 0xFF) * aFP) >>> 16);
                }
            }
            sp += pixStride;
        }
    }
}

From source file:com.occamlab.te.parsers.ImageParser.java

private static void processBufferedImage(BufferedImage buffimage, String formatName, NodeList nodes)
        throws Exception {
    HashMap<Object, Object> bandMap = new HashMap<Object, Object>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("subimage")) {
                Element e = (Element) node;
                int x = Integer.parseInt(e.getAttribute("x"));
                int y = Integer.parseInt(e.getAttribute("y"));
                int w = Integer.parseInt(e.getAttribute("width"));
                int h = Integer.parseInt(e.getAttribute("height"));
                processBufferedImage(buffimage.getSubimage(x, y, w, h), formatName, e.getChildNodes());
            } else if (node.getLocalName().equals("checksum")) {
                CRC32 checksum = new CRC32();
                Raster raster = buffimage.getRaster();
                DataBufferByte buffer;
                if (node.getParentNode().getLocalName().equals("subimage")) {
                    WritableRaster outRaster = raster.createCompatibleWritableRaster();
                    buffimage.copyData(outRaster);
                    buffer = (DataBufferByte) outRaster.getDataBuffer();
                } else {
                    buffer = (DataBufferByte) raster.getDataBuffer();
                }//from w w  w . j  a v  a  2s .  c o  m
                int numbanks = buffer.getNumBanks();
                for (int j = 0; j < numbanks; j++) {
                    checksum.update(buffer.getData(j));
                }
                Document doc = node.getOwnerDocument();
                node.appendChild(doc.createTextNode(Long.toString(checksum.getValue())));
            } else if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                if (sample.equals("all")) {
                    bandMap.put(band, null);
                } else {
                    HashMap<Object, Object> sampleMap = (HashMap<Object, Object>) bandMap.get(band);
                    if (sampleMap == null) {
                        if (!bandMap.containsKey(band)) {
                            sampleMap = new HashMap<Object, Object>();
                            bandMap.put(band, sampleMap);
                        }
                    }
                    sampleMap.put(Integer.decode(sample), new Integer(0));
                }
            } else if (node.getLocalName().equals("transparentNodata")) { // 2011-08-24
                                                                          // PwD
                String transparentNodata = checkTransparentNodata(buffimage, node);
                node.setTextContent(transparentNodata);
            }
        }
    }

    Iterator bandIt = bandMap.keySet().iterator();
    while (bandIt.hasNext()) {
        String band_str = (String) bandIt.next();
        int band_indexes[];
        if (buffimage.getType() == BufferedImage.TYPE_BYTE_BINARY
                || buffimage.getType() == BufferedImage.TYPE_BYTE_GRAY) {
            band_indexes = new int[1];
            band_indexes[0] = 0;
        } else {
            band_indexes = new int[band_str.length()];
            for (int i = 0; i < band_str.length(); i++) {
                if (band_str.charAt(i) == 'A')
                    band_indexes[i] = 3;
                if (band_str.charAt(i) == 'B')
                    band_indexes[i] = 2;
                if (band_str.charAt(i) == 'G')
                    band_indexes[i] = 1;
                if (band_str.charAt(i) == 'R')
                    band_indexes[i] = 0;
            }
        }

        Raster raster = buffimage.getRaster();
        java.util.HashMap sampleMap = (java.util.HashMap) bandMap.get(band_str);
        boolean addall = (sampleMap == null);
        if (sampleMap == null) {
            sampleMap = new java.util.HashMap();
            bandMap.put(band_str, sampleMap);
        }

        int minx = raster.getMinX();
        int maxx = minx + raster.getWidth();
        int miny = raster.getMinY();
        int maxy = miny + raster.getHeight();
        int bands[][] = new int[band_indexes.length][raster.getWidth()];

        for (int y = miny; y < maxy; y++) {
            for (int i = 0; i < band_indexes.length; i++) {
                raster.getSamples(minx, y, maxx, 1, band_indexes[i], bands[i]);
            }
            for (int x = minx; x < maxx; x++) {
                int sample = 0;
                for (int i = 0; i < band_indexes.length; i++) {
                    sample |= bands[i][x] << ((band_indexes.length - i - 1) * 8);
                }

                Integer sampleObj = new Integer(sample);

                boolean add = addall;
                if (!addall) {
                    add = sampleMap.containsKey(sampleObj);
                }
                if (add) {
                    Integer count = (Integer) sampleMap.get(sampleObj);
                    if (count == null) {
                        count = new Integer(0);
                    }
                    count = new Integer(count.intValue() + 1);
                    sampleMap.put(sampleObj, count);
                }
            }
        }
    }

    Node node = nodes.item(0);
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                HashMap sampleMap = (HashMap) bandMap.get(band);
                Document doc = node.getOwnerDocument();
                if (sample.equals("all")) {
                    Node parent = node.getParentNode();
                    Node prevSibling = node.getPreviousSibling();
                    Iterator sampleIt = sampleMap.keySet().iterator();
                    Element countnode = null;
                    int digits;
                    String prefix;
                    switch (buffimage.getType()) {
                    case BufferedImage.TYPE_BYTE_BINARY:
                        digits = 1;
                        prefix = "";
                        break;
                    case BufferedImage.TYPE_BYTE_GRAY:
                        digits = 2;
                        prefix = "0x";
                        break;
                    default:
                        prefix = "0x";
                        digits = band.length() * 2;
                    }
                    while (sampleIt.hasNext()) {
                        countnode = doc.createElementNS(node.getNamespaceURI(), "count");
                        Integer sampleInt = (Integer) sampleIt.next();
                        Integer count = (Integer) sampleMap.get(sampleInt);
                        if (band.length() > 0) {
                            countnode.setAttribute("bands", band);
                        }
                        countnode.setAttribute("sample", prefix + HexString(sampleInt.intValue(), digits));
                        Node textnode = doc.createTextNode(count.toString());
                        countnode.appendChild(textnode);
                        parent.insertBefore(countnode, node);
                        if (sampleIt.hasNext()) {
                            if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE) {
                                parent.insertBefore(prevSibling.cloneNode(false), node);
                            }
                        }
                    }
                    parent.removeChild(node);
                    node = countnode;
                } else {
                    Integer count = (Integer) sampleMap.get(Integer.decode(sample));
                    if (count == null)
                        count = new Integer(0);
                    Node textnode = doc.createTextNode(count.toString());
                    node.appendChild(textnode);
                }
            }
        }
        node = node.getNextSibling();
    }
}