Example usage for java.awt.image SinglePixelPackedSampleModel getOffset

List of usage examples for java.awt.image SinglePixelPackedSampleModel getOffset

Introduction

In this page you can find the example usage for java.awt.image SinglePixelPackedSampleModel getOffset.

Prototype

public int getOffset(int x, int y) 

Source Link

Document

Returns the offset (in data array elements) of pixel (x,y).

Usage

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  ww  w . j ava  2  s .  c o  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()));
    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];//w  w  w  .  j av a  2 s .  c  o 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)) { // 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));
            }//w ww. j a va  2  s. c om
            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 ww  w . j a  v  a 2  s  .  c  om*/
            sp++;
        }
    }
}

From source file:GraphicsUtil.java

/**
 * An internal optimized version of copyData designed to work on
 * Integer packed data with a SinglePixelPackedSampleModel.  Only
 * the region of overlap between src and dst is copied.
 *
 * Calls to this should be preflighted with is_INT_PACK_Data
 * on both src and dest (requireAlpha can be false).
 *
 * @param src The source of the data//from  w  ww. j  a v a 2s .  c  o  m
 * @param dst The destination for the data.
 */
public static void copyData_INT_PACK(Raster src, WritableRaster dst) {
    // System.out.println("Fast copyData");
    int x0 = dst.getMinX();
    if (x0 < src.getMinX())
        x0 = src.getMinX();

    int y0 = dst.getMinY();
    if (y0 < src.getMinY())
        y0 = src.getMinY();

    int x1 = dst.getMinX() + dst.getWidth() - 1;
    if (x1 > src.getMinX() + src.getWidth() - 1)
        x1 = src.getMinX() + src.getWidth() - 1;

    int y1 = dst.getMinY() + dst.getHeight() - 1;
    if (y1 > src.getMinY() + src.getHeight() - 1)
        y1 = src.getMinY() + src.getHeight() - 1;

    int width = x1 - x0 + 1;
    int height = y1 - y0 + 1;

    SinglePixelPackedSampleModel srcSPPSM;
    srcSPPSM = (SinglePixelPackedSampleModel) src.getSampleModel();

    final int srcScanStride = srcSPPSM.getScanlineStride();
    DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
    final int[] srcPixels = srcDB.getBankData()[0];
    final int srcBase = (srcDB.getOffset()
            + srcSPPSM.getOffset(x0 - src.getSampleModelTranslateX(), y0 - src.getSampleModelTranslateY()));

    SinglePixelPackedSampleModel dstSPPSM;
    dstSPPSM = (SinglePixelPackedSampleModel) dst.getSampleModel();

    final int dstScanStride = dstSPPSM.getScanlineStride();
    DataBufferInt dstDB = (DataBufferInt) dst.getDataBuffer();
    final int[] dstPixels = dstDB.getBankData()[0];
    final int dstBase = (dstDB.getOffset()
            + dstSPPSM.getOffset(x0 - dst.getSampleModelTranslateX(), y0 - dst.getSampleModelTranslateY()));

    if ((srcScanStride == dstScanStride) && (srcScanStride == width)) {
        // System.out.println("VERY Fast copyData");

        System.arraycopy(srcPixels, srcBase, dstPixels, dstBase, width * height);
    } else if (width > 128) {
        int srcSP = srcBase;
        int dstSP = dstBase;
        for (int y = 0; y < height; y++) {
            System.arraycopy(srcPixels, srcSP, dstPixels, dstSP, width);
            srcSP += srcScanStride;
            dstSP += dstScanStride;
        }
    } else {
        for (int y = 0; y < height; y++) {
            int srcSP = srcBase + y * srcScanStride;
            int dstSP = dstBase + y * dstScanStride;
            for (int x = 0; x < width; x++)
                dstPixels[dstSP++] = srcPixels[srcSP++];
        }
    }
}