Java Code Examples for java.awt.image.BufferedImage#setData()

The following examples show how to use java.awt.image.BufferedImage#setData() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: gcs   File: JPEGFactory.java    License: Mozilla Public License 2.0 6 votes vote down vote up
private static BufferedImage getAlphaImage(BufferedImage image) throws IOException
{
    if (!image.getColorModel().hasAlpha())
    {
        return null;
    }
    if (image.getTransparency() == Transparency.BITMASK)
    {
        throw new UnsupportedOperationException("BITMASK Transparency JPEG compression is not" +
                " useful, use LosslessImageFactory instead");
    }
    WritableRaster alphaRaster = image.getAlphaRaster();
    if (alphaRaster == null)
    {
        // happens sometimes (PDFBOX-2654) despite colormodel claiming to have alpha
        return null;
    }
    BufferedImage alphaImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_BYTE_GRAY);
    alphaImage.setData(alphaRaster);
    return alphaImage;
}
 
Example 2
/**
 * Decompresses jpeg data as a BufferedImage using turbojpeg with imageIO as fallback.
 * BufferedImage must be initialized with correct width and height.
 */
public void createImage(byte[] data, BufferedImage bi) throws Exception {
    if (useTJ.get()) {
        try {
            int pixelType = TJ.PF_RGB;
            //int pixelSize = TJ.getPixelSize(pixelType);
            TJDecompressor decoder = new TJDecompressor(data);
            decoder.decompress(bi, pixelType);
            decoder.close();
        } catch (UnsatisfiedLinkError e) {
            useTJ.set(false);
            logger.info("TurboJPEG not working, using ImageIO as fallback");
        }
    }

    if (!useTJ.get()) {
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        BufferedImage temp = ImageIO.read(in);
        in.close();
        bi.setData(temp.getData());
    }

}
 
Example 3
@NotNull
@Override
public BufferedImage applyFilter(@NotNull BufferedImage bufferedImage) {
    Raster src = bufferedImage.getData();
    WritableRaster dest = src.createCompatibleWritableRaster();
    bufferedImage.copyData(dest);
    for (int y = pixelateSection.y; y < pixelateSection.y + pixelateSection.getHeight(); y += PIX_SIZE) {
        for (int x = pixelateSection.x; x < pixelateSection.x + pixelateSection.getWidth(); x += PIX_SIZE) {
            double[] pixel = new double[3];
            pixel = src.getPixel(x, y, pixel);
            for (int yd = y; (yd < y + PIX_SIZE) && (yd < dest.getHeight()); yd++) {
                for (int xd = x; (xd < x + PIX_SIZE) && (xd < dest.getWidth()); xd++) {
                    dest.setPixel(xd, yd, pixel);
                }
            }
        }
    }
    bufferedImage.setData(dest);
    return bufferedImage;
}
 
Example 4
public static RenderedImage toRenderedImage (String[] rows)
{
    // Create the DataBuffer to hold the pixel samples
    final int width = rows[0].length();
    final int height = rows.length;
    final int size = width * height;
    byte[] pixels = new byte[size];
    int index = 0;
    for (String row : rows) {
        for (int x = 0; x < width; x++) {
            pixels[index++] = (byte) decodeGray(row.charAt(x));
        }
    }
    DataBuffer dataBuffer = new DataBufferByte(pixels, size);

    // Create Raster
    WritableRaster writableRaster = Raster.createBandedRaster
        (dataBuffer, width, height,
         width,                      // scanlineStride
         new int[] {0},             // bankIndices,
         new int[] {0},             // bandOffsets,
         null);                     // location

    // Create the image
    BufferedImage bufferedImage = new BufferedImage
            (width, height, BufferedImage.TYPE_BYTE_GRAY);
    bufferedImage.setData(writableRaster);

    return bufferedImage;
}
 
Example 5
public static ImagePlus toImagePlus(PlanarImage source, Rectangle roi, BorderExtender border) {
    if (roi == null) roi = source.getBounds();
    Raster r = source.getExtendedData(roi, border);
    int numBands = source.getNumBands();
    BufferedImage bi = new BufferedImage(roi.width, roi.height, numBands == 1 ? BufferedImage.TYPE_BYTE_GRAY : BufferedImage.TYPE_INT_RGB);
    bi.setData(r);
    ImagePlus ip = new ImagePlus("IJManipulation", bi);
    return ip;
}
 
Example 6
Source Project: sambox   File: PDDeviceRGB.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public BufferedImage toRGBImage(WritableRaster raster) throws IOException
{
    init();
    //
    // WARNING: this method is performance sensitive, modify with care!
    //
    // Please read PDFBOX-3854 and PDFBOX-2092 and look at the related commits first.
    // The current code returns TYPE_INT_RGB images which prevents slowness due to threads
    // blocking each other when TYPE_CUSTOM images are used.
    BufferedImage image = new BufferedImage(raster.getWidth(), raster.getHeight(),
            BufferedImage.TYPE_INT_RGB);
    image.setData(raster);
    return image;
}
 
Example 7
Source Project: MyBox   File: ImageConvert.java    License: Apache License 2.0 4 votes vote down vote up
public static BufferedImage cmyk2rgb(final byte[] buffer, final int w,
        final int h) throws IOException {

    final ColorSpace CMYK = ImageValue.adobeCmykColorSpace();

    final int pixelCount = w * h * 4;
    int C, M, Y, K, lastC = -1, lastM = -1, lastY = -1, lastK = -1;

    int j = 0;
    float[] RGB = new float[]{0f, 0f, 0f};
    //turn YCC in Buffer to CYM using profile
    for (int i = 0; i < pixelCount; i = i + 4) {

        C = (buffer[i] & 255);
        M = (buffer[i + 1] & 255);
        Y = (buffer[i + 2] & 255);
        K = (buffer[i + 3] & 255);

        // System.out.println(C+" "+M+" "+Y+" "+K);
        if (C == lastC && M == lastM && Y == lastY && K == lastK) {
            //no change so use last value
        } else { //new value

            RGB = CMYK.toRGB(new float[]{C / 255f, M / 255f, Y / 255f, K / 255f});

            //flag so we can just reuse if next value the same
            lastC = C;
            lastM = M;
            lastY = Y;
            lastK = K;
        }

        //put back as CMY
        buffer[j] = (byte) (RGB[0] * 255f);
        buffer[j + 1] = (byte) (RGB[1] * 255f);
        buffer[j + 2] = (byte) (RGB[2] * 255f);

        j = j + 3;

    }

    /**
     * create CMYK raster from buffer
     */
    final Raster raster = Raster.createInterleavedRaster(new DataBufferByte(buffer, j), w, h, w * 3, 3, new int[]{0, 1, 2}, null);

    //data now sRGB so create image
    final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    image.setData(raster);

    return image;
}
 
Example 8
Source Project: jump-jump-game   File: GameUIAnalyzer.java    License: Apache License 2.0 4 votes vote down vote up
public static BufferedImage markeRedPoint(BufferedImage bi, int x, int y){
    BufferedImage newBi = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    newBi.setData(bi.getData());
    newBi.setRGB(x,y,getRBInt(255, 0, 0));
    return newBi;
}
 
Example 9
@Override
public BufferedImage getImage() {
    int sizeUncompressed = 0;
    int size8888 = 0;
    int maxWriteBuf = 2;
    int maxHeight = 3;
    byte[] writeBuf = new byte[maxWriteBuf];
    @SuppressWarnings ("unused")
    byte[] rowPointers = new byte[maxHeight];
    switch (getFormat()) {
        case 1:
        case 513:
            sizeUncompressed = getHeight() * getWidth() * 4;
            break;
        case 2:
            sizeUncompressed = getHeight() * getWidth() * 8;
            break;
        case 517:
            sizeUncompressed = getHeight() * getWidth() / 128;
            break;
    }
    size8888 = getHeight() * getWidth() * 8;
    if (size8888 > maxWriteBuf) {
        maxWriteBuf = size8888;
        writeBuf = new byte[maxWriteBuf];
    }
    if (getHeight() > maxHeight) {
        maxHeight = getHeight();
        rowPointers = new byte[maxHeight];
    }
    Inflater dec = new Inflater();
    dec.setInput(getData(), 0, dataLength);
    int declen = 0;
    byte[] uc = new byte[sizeUncompressed];
    try {
        declen = dec.inflate(uc);
    } catch (DataFormatException ex) {
        throw new RuntimeException("zlib fucks", ex);
    }
    dec.end();
    if (getFormat() == 1) {
        for (int i = 0; i < sizeUncompressed; i++) {
            byte low = (byte) (uc[i] & 0x0F);
            byte high = (byte) (uc[i] & 0xF0);
            writeBuf[(i << 1)] = (byte) (((low << 4) | low) & 0xFF);
            writeBuf[(i << 1) + 1] = (byte) (high | ((high >>> 4) & 0xF));
        }
    } else if (getFormat() == 2) {
        writeBuf = uc;
    } else if (getFormat() == 513) {
        for (int i = 0; i < declen; i += 2) {
            byte bBits = (byte) ((uc[i] & 0x1F) << 3);
            byte gBits = (byte) (((uc[i + 1] & 0x07) << 5) | ((uc[i] & 0xE0) >> 3));
            byte rBits = (byte) (uc[i + 1] & 0xF8);
            writeBuf[(i << 1)] = (byte) (bBits | (bBits >> 5));
            writeBuf[(i << 1) + 1] = (byte) (gBits | (gBits >> 6));
            writeBuf[(i << 1) + 2] = (byte) (rBits | (rBits >> 5));
            writeBuf[(i << 1) + 3] = (byte) 0xFF;
        }
    } else if (getFormat() == 517) {
        byte b = 0x00;
        int pixelIndex = 0;
        for (int i = 0; i < declen; i++) {
            for (int j = 0; j < 8; j++) {
                b = (byte) (((uc[i] & (0x01 << (7 - j))) >> (7 - j)) * 255);
                for (int k = 0; k < 16; k++) {
                    pixelIndex = (i << 9) + (j << 6) + k * 2;
                    writeBuf[pixelIndex] = b;
                    writeBuf[pixelIndex + 1] = b;
                    writeBuf[pixelIndex + 2] = b;
                    writeBuf[pixelIndex + 3] = (byte) 0xFF;
                }
            }
        }
    }
    DataBufferByte imgData = new DataBufferByte(writeBuf, sizeUncompressed);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, getWidth(), getHeight(), 4, getWidth() * 4, ZAHLEN);
    WritableRaster imgRaster = Raster.createWritableRaster(sm, imgData, new Point(0, 0));
    BufferedImage aa = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    aa.setData(imgRaster);
    return aa;
}
 
Example 10
Source Project: scrimage   File: MarvinImage.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Return a new instance of the BufferedImage
 *
 * @return BufferedImage
 */
public BufferedImage getNewImageInstance() {
   BufferedImage buf = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
   buf.setData(image.getData());
   return buf;
}
 
Example 11
@Override
public BufferedImage getImage() {
    int sizeUncompressed = 0;
    int size8888 = 0;
    int maxWriteBuf = 2;
    int maxHeight = 3;
    byte[] writeBuf = new byte[maxWriteBuf];
    @SuppressWarnings ("unused")
    byte[] rowPointers = new byte[maxHeight];
    switch (getFormat()) {
        case 1:
        case 513:
            sizeUncompressed = getHeight() * getWidth() * 4;
            break;
        case 2:
            sizeUncompressed = getHeight() * getWidth() * 8;
            break;
        case 517:
            sizeUncompressed = getHeight() * getWidth() / 128;
            break;
    }
    size8888 = getHeight() * getWidth() * 8;
    if (size8888 > maxWriteBuf) {
        maxWriteBuf = size8888;
        writeBuf = new byte[maxWriteBuf];
    }
    if (getHeight() > maxHeight) {
        maxHeight = getHeight();
        rowPointers = new byte[maxHeight];
    }
    Inflater dec = new Inflater();
    dec.setInput(getData(), 0, dataLength);
    int declen = 0;
    byte[] uc = new byte[sizeUncompressed];
    try {
        declen = dec.inflate(uc);
    } catch (DataFormatException ex) {
        throw new RuntimeException("zlib fucks", ex);
    }
    dec.end();
    if (getFormat() == 1) {
        for (int i = 0; i < sizeUncompressed; i++) {
            byte low = (byte) (uc[i] & 0x0F);
            byte high = (byte) (uc[i] & 0xF0);
            writeBuf[(i << 1)] = (byte) (((low << 4) | low) & 0xFF);
            writeBuf[(i << 1) + 1] = (byte) (high | ((high >>> 4) & 0xF));
        }
    } else if (getFormat() == 2) {
        writeBuf = uc;
    } else if (getFormat() == 513) {
        for (int i = 0; i < declen; i += 2) {
            byte bBits = (byte) ((uc[i] & 0x1F) << 3);
            byte gBits = (byte) (((uc[i + 1] & 0x07) << 5) | ((uc[i] & 0xE0) >> 3));
            byte rBits = (byte) (uc[i + 1] & 0xF8);
            writeBuf[(i << 1)] = (byte) (bBits | (bBits >> 5));
            writeBuf[(i << 1) + 1] = (byte) (gBits | (gBits >> 6));
            writeBuf[(i << 1) + 2] = (byte) (rBits | (rBits >> 5));
            writeBuf[(i << 1) + 3] = (byte) 0xFF;
        }
    } else if (getFormat() == 517) {
        byte b = 0x00;
        int pixelIndex = 0;
        for (int i = 0; i < declen; i++) {
            for (int j = 0; j < 8; j++) {
                b = (byte) (((uc[i] & (0x01 << (7 - j))) >> (7 - j)) * 255);
                for (int k = 0; k < 16; k++) {
                    pixelIndex = (i << 9) + (j << 6) + k * 2;
                    writeBuf[pixelIndex] = b;
                    writeBuf[pixelIndex + 1] = b;
                    writeBuf[pixelIndex + 2] = b;
                    writeBuf[pixelIndex + 3] = (byte) 0xFF;
                }
            }
        }
    }
    DataBufferByte imgData = new DataBufferByte(writeBuf, sizeUncompressed);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, getWidth(), getHeight(), 4, getWidth() * 4, ZAHLEN);
    WritableRaster imgRaster = Raster.createWritableRaster(sm, imgData, new Point(0, 0));
    BufferedImage aa = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    aa.setData(imgRaster);
    return aa;
}
 
Example 12
Source Project: BLELocalization   File: ImageHolder.java    License: MIT License 4 votes vote down vote up
static BufferedImage copyImage(BufferedImage src){
	BufferedImage dist=new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
	dist.setData(src.getData());
	return dist;
}
 
Example 13
@Override
public BufferedImage getImage() {
    int sizeUncompressed = 0;
    int size8888 = 0;
    int maxWriteBuf = 2;
    int maxHeight = 3;
    byte[] writeBuf = new byte[maxWriteBuf];
    @SuppressWarnings ("unused")
    byte[] rowPointers = new byte[maxHeight];
    switch (getFormat()) {
        case 1:
        case 513:
            sizeUncompressed = getHeight() * getWidth() * 4;
            break;
        case 2:
            sizeUncompressed = getHeight() * getWidth() * 8;
            break;
        case 517:
            sizeUncompressed = getHeight() * getWidth() / 128;
            break;
    }
    size8888 = getHeight() * getWidth() * 8;
    if (size8888 > maxWriteBuf) {
        maxWriteBuf = size8888;
        writeBuf = new byte[maxWriteBuf];
    }
    if (getHeight() > maxHeight) {
        maxHeight = getHeight();
        rowPointers = new byte[maxHeight];
    }
    Inflater dec = new Inflater();
    dec.setInput(getData(), 0, dataLength);
    int declen = 0;
    byte[] uc = new byte[sizeUncompressed];
    try {
        declen = dec.inflate(uc);
    } catch (DataFormatException ex) {
        throw new RuntimeException("zlib fucks", ex);
    }
    dec.end();
    if (getFormat() == 1) {
        for (int i = 0; i < sizeUncompressed; i++) {
            byte low = (byte) (uc[i] & 0x0F);
            byte high = (byte) (uc[i] & 0xF0);
            writeBuf[(i << 1)] = (byte) (((low << 4) | low) & 0xFF);
            writeBuf[(i << 1) + 1] = (byte) (high | ((high >>> 4) & 0xF));
        }
    } else if (getFormat() == 2) {
        writeBuf = uc;
    } else if (getFormat() == 513) {
        for (int i = 0; i < declen; i += 2) {
            byte bBits = (byte) ((uc[i] & 0x1F) << 3);
            byte gBits = (byte) (((uc[i + 1] & 0x07) << 5) | ((uc[i] & 0xE0) >> 3));
            byte rBits = (byte) (uc[i + 1] & 0xF8);
            writeBuf[(i << 1)] = (byte) (bBits | (bBits >> 5));
            writeBuf[(i << 1) + 1] = (byte) (gBits | (gBits >> 6));
            writeBuf[(i << 1) + 2] = (byte) (rBits | (rBits >> 5));
            writeBuf[(i << 1) + 3] = (byte) 0xFF;
        }
    } else if (getFormat() == 517) {
        byte b = 0x00;
        int pixelIndex = 0;
        for (int i = 0; i < declen; i++) {
            for (int j = 0; j < 8; j++) {
                b = (byte) (((uc[i] & (0x01 << (7 - j))) >> (7 - j)) * 255);
                for (int k = 0; k < 16; k++) {
                    pixelIndex = (i << 9) + (j << 6) + k * 2;
                    writeBuf[pixelIndex] = b;
                    writeBuf[pixelIndex + 1] = b;
                    writeBuf[pixelIndex + 2] = b;
                    writeBuf[pixelIndex + 3] = (byte) 0xFF;
                }
            }
        }
    }
    DataBufferByte imgData = new DataBufferByte(writeBuf, sizeUncompressed);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, getWidth(), getHeight(), 4, getWidth() * 4, ZAHLEN);
    WritableRaster imgRaster = Raster.createWritableRaster(sm, imgData, new Point(0, 0));
    BufferedImage aa = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    aa.setData(imgRaster);
    return aa;
}
 
Example 14
@Override
public BufferedImage getImage() {
    int sizeUncompressed = 0;
    int size8888 = 0;
    int maxWriteBuf = 2;
    int maxHeight = 3;
    byte[] writeBuf = new byte[maxWriteBuf];
    @SuppressWarnings ("unused")
    byte[] rowPointers = new byte[maxHeight];
    switch (getFormat()) {
        case 1:
        case 513:
            sizeUncompressed = getHeight() * getWidth() * 4;
            break;
        case 2:
            sizeUncompressed = getHeight() * getWidth() * 8;
            break;
        case 517:
            sizeUncompressed = getHeight() * getWidth() / 128;
            break;
    }
    size8888 = getHeight() * getWidth() * 8;
    if (size8888 > maxWriteBuf) {
        maxWriteBuf = size8888;
        writeBuf = new byte[maxWriteBuf];
    }
    if (getHeight() > maxHeight) {
        maxHeight = getHeight();
        rowPointers = new byte[maxHeight];
    }
    Inflater dec = new Inflater();
    dec.setInput(getData(), 0, dataLength);
    int declen = 0;
    byte[] uc = new byte[sizeUncompressed];
    try {
        declen = dec.inflate(uc);
    } catch (DataFormatException ex) {
        throw new RuntimeException("zlib fucks", ex);
    }
    dec.end();
    if (getFormat() == 1) {
        for (int i = 0; i < sizeUncompressed; i++) {
            byte low = (byte) (uc[i] & 0x0F);
            byte high = (byte) (uc[i] & 0xF0);
            writeBuf[(i << 1)] = (byte) (((low << 4) | low) & 0xFF);
            writeBuf[(i << 1) + 1] = (byte) (high | ((high >>> 4) & 0xF));
        }
    } else if (getFormat() == 2) {
        writeBuf = uc;
    } else if (getFormat() == 513) {
        for (int i = 0; i < declen; i += 2) {
            byte bBits = (byte) ((uc[i] & 0x1F) << 3);
            byte gBits = (byte) (((uc[i + 1] & 0x07) << 5) | ((uc[i] & 0xE0) >> 3));
            byte rBits = (byte) (uc[i + 1] & 0xF8);
            writeBuf[(i << 1)] = (byte) (bBits | (bBits >> 5));
            writeBuf[(i << 1) + 1] = (byte) (gBits | (gBits >> 6));
            writeBuf[(i << 1) + 2] = (byte) (rBits | (rBits >> 5));
            writeBuf[(i << 1) + 3] = (byte) 0xFF;
        }
    } else if (getFormat() == 517) {
        byte b = 0x00;
        int pixelIndex = 0;
        for (int i = 0; i < declen; i++) {
            for (int j = 0; j < 8; j++) {
                b = (byte) (((uc[i] & (0x01 << (7 - j))) >> (7 - j)) * 255);
                for (int k = 0; k < 16; k++) {
                    pixelIndex = (i << 9) + (j << 6) + k * 2;
                    writeBuf[pixelIndex] = b;
                    writeBuf[pixelIndex + 1] = b;
                    writeBuf[pixelIndex + 2] = b;
                    writeBuf[pixelIndex + 3] = (byte) 0xFF;
                }
            }
        }
    }
    DataBufferByte imgData = new DataBufferByte(writeBuf, sizeUncompressed);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, getWidth(), getHeight(), 4, getWidth() * 4, ZAHLEN);
    WritableRaster imgRaster = Raster.createWritableRaster(sm, imgData, new Point(0, 0));
    BufferedImage aa = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    aa.setData(imgRaster);
    return aa;
}
 
Example 15
Source Project: sambox   File: PageDrawer.java    License: Apache License 2.0 4 votes vote down vote up
private Paint applySoftMaskToPaint(Paint parentPaint, PDSoftMask softMask) throws IOException
{
    if (softMask == null || softMask.getGroup() == null)
    {
        return parentPaint;
    }
    PDColor backdropColor = null;
    if (COSName.LUMINOSITY.equals(softMask.getSubType()))
    {
        COSArray backdropColorArray = softMask.getBackdropColor();
        PDTransparencyGroup form = softMask.getGroup();
        PDColorSpace colorSpace = form.getGroup().getColorSpace(form.getResources());
        if (colorSpace != null && backdropColorArray != null)
        {
            backdropColor = new PDColor(backdropColorArray, colorSpace);
        }
    }
    TransparencyGroup transparencyGroup = new TransparencyGroup(softMask.getGroup(), true,
            softMask.getInitialTransformationMatrix(), backdropColor);
    BufferedImage image = transparencyGroup.getImage();
    if (image == null)
    {
        // Adobe Reader ignores empty softmasks instead of using bc color
        // sample file: PDFJS-6967_reduced_outside_softmask.pdf
        return parentPaint;
    }
    BufferedImage gray = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_BYTE_GRAY);
    if (COSName.ALPHA.equals(softMask.getSubType()))
    {
        gray.setData(image.getAlphaRaster());
    }
    else if (COSName.LUMINOSITY.equals(softMask.getSubType()))
    {
        Graphics g = gray.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
    }
    else
    {
        throw new IOException("Invalid soft mask subtype.");
    }
    gray = adjustImage(gray);
    Rectangle2D tpgBounds = transparencyGroup.getBounds();
    adjustRectangle(tpgBounds);
    return new SoftMask(parentPaint, gray, tpgBounds, backdropColor,
            softMask.getTransferFunction());
}
 
Example 16
@Override
public BufferedImage getImage() {
    int sizeUncompressed = 0;
    int size8888 = 0;
    int maxWriteBuf = 2;
    int maxHeight = 3;
    byte[] writeBuf = new byte[maxWriteBuf];
    @SuppressWarnings ("unused")
    byte[] rowPointers = new byte[maxHeight];
    switch (getFormat()) {
        case 1:
        case 513:
            sizeUncompressed = getHeight() * getWidth() * 4;
            break;
        case 2:
            sizeUncompressed = getHeight() * getWidth() * 8;
            break;
        case 517:
            sizeUncompressed = getHeight() * getWidth() / 128;
            break;
    }
    size8888 = getHeight() * getWidth() * 8;
    if (size8888 > maxWriteBuf) {
        maxWriteBuf = size8888;
        writeBuf = new byte[maxWriteBuf];
    }
    if (getHeight() > maxHeight) {
        maxHeight = getHeight();
        rowPointers = new byte[maxHeight];
    }
    Inflater dec = new Inflater();
    dec.setInput(getData(), 0, dataLength);
    int declen = 0;
    byte[] uc = new byte[sizeUncompressed];
    try {
        declen = dec.inflate(uc);
    } catch (DataFormatException ex) {
        throw new RuntimeException("zlib fucks", ex);
    }
    dec.end();
    if (getFormat() == 1) {
        for (int i = 0; i < sizeUncompressed; i++) {
            byte low = (byte) (uc[i] & 0x0F);
            byte high = (byte) (uc[i] & 0xF0);
            writeBuf[(i << 1)] = (byte) (((low << 4) | low) & 0xFF);
            writeBuf[(i << 1) + 1] = (byte) (high | ((high >>> 4) & 0xF));
        }
    } else if (getFormat() == 2) {
        writeBuf = uc;
    } else if (getFormat() == 513) {
        for (int i = 0; i < declen; i += 2) {
            byte bBits = (byte) ((uc[i] & 0x1F) << 3);
            byte gBits = (byte) (((uc[i + 1] & 0x07) << 5) | ((uc[i] & 0xE0) >> 3));
            byte rBits = (byte) (uc[i + 1] & 0xF8);
            writeBuf[(i << 1)] = (byte) (bBits | (bBits >> 5));
            writeBuf[(i << 1) + 1] = (byte) (gBits | (gBits >> 6));
            writeBuf[(i << 1) + 2] = (byte) (rBits | (rBits >> 5));
            writeBuf[(i << 1) + 3] = (byte) 0xFF;
        }
    } else if (getFormat() == 517) {
        byte b = 0x00;
        int pixelIndex = 0;
        for (int i = 0; i < declen; i++) {
            for (int j = 0; j < 8; j++) {
                b = (byte) (((uc[i] & (0x01 << (7 - j))) >> (7 - j)) * 255);
                for (int k = 0; k < 16; k++) {
                    pixelIndex = (i << 9) + (j << 6) + k * 2;
                    writeBuf[pixelIndex] = b;
                    writeBuf[pixelIndex + 1] = b;
                    writeBuf[pixelIndex + 2] = b;
                    writeBuf[pixelIndex + 3] = (byte) 0xFF;
                }
            }
        }
    }
    DataBufferByte imgData = new DataBufferByte(writeBuf, sizeUncompressed);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, getWidth(), getHeight(), 4, getWidth() * 4, ZAHLEN);
    WritableRaster imgRaster = Raster.createWritableRaster(sm, imgData, new Point(0, 0));
    BufferedImage aa = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    aa.setData(imgRaster);
    return aa;
}
 
Example 17
@Override
public BufferedImage getImage() {
    int sizeUncompressed = 0;
    int size8888 = 0;
    int maxWriteBuf = 2;
    int maxHeight = 3;
    byte[] writeBuf = new byte[maxWriteBuf];
    @SuppressWarnings ("unused")
    byte[] rowPointers = new byte[maxHeight];
    switch (getFormat()) {
        case 1:
        case 513:
            sizeUncompressed = getHeight() * getWidth() * 4;
            break;
        case 2:
            sizeUncompressed = getHeight() * getWidth() * 8;
            break;
        case 517:
            sizeUncompressed = getHeight() * getWidth() / 128;
            break;
    }
    size8888 = getHeight() * getWidth() * 8;
    if (size8888 > maxWriteBuf) {
        maxWriteBuf = size8888;
        writeBuf = new byte[maxWriteBuf];
    }
    if (getHeight() > maxHeight) {
        maxHeight = getHeight();
        rowPointers = new byte[maxHeight];
    }
    Inflater dec = new Inflater();
    dec.setInput(getData(), 0, dataLength);
    int declen = 0;
    byte[] uc = new byte[sizeUncompressed];
    try {
        declen = dec.inflate(uc);
    } catch (DataFormatException ex) {
        throw new RuntimeException("zlib fucks", ex);
    }
    dec.end();
    if (getFormat() == 1) {
        for (int i = 0; i < sizeUncompressed; i++) {
            byte low = (byte) (uc[i] & 0x0F);
            byte high = (byte) (uc[i] & 0xF0);
            writeBuf[(i << 1)] = (byte) (((low << 4) | low) & 0xFF);
            writeBuf[(i << 1) + 1] = (byte) (high | ((high >>> 4) & 0xF));
        }
    } else if (getFormat() == 2) {
        writeBuf = uc;
    } else if (getFormat() == 513) {
        for (int i = 0; i < declen; i += 2) {
            byte bBits = (byte) ((uc[i] & 0x1F) << 3);
            byte gBits = (byte) (((uc[i + 1] & 0x07) << 5) | ((uc[i] & 0xE0) >> 3));
            byte rBits = (byte) (uc[i + 1] & 0xF8);
            writeBuf[(i << 1)] = (byte) (bBits | (bBits >> 5));
            writeBuf[(i << 1) + 1] = (byte) (gBits | (gBits >> 6));
            writeBuf[(i << 1) + 2] = (byte) (rBits | (rBits >> 5));
            writeBuf[(i << 1) + 3] = (byte) 0xFF;
        }
    } else if (getFormat() == 517) {
        byte b = 0x00;
        int pixelIndex = 0;
        for (int i = 0; i < declen; i++) {
            for (int j = 0; j < 8; j++) {
                b = (byte) (((uc[i] & (0x01 << (7 - j))) >> (7 - j)) * 255);
                for (int k = 0; k < 16; k++) {
                    pixelIndex = (i << 9) + (j << 6) + k * 2;
                    writeBuf[pixelIndex] = b;
                    writeBuf[pixelIndex + 1] = b;
                    writeBuf[pixelIndex + 2] = b;
                    writeBuf[pixelIndex + 3] = (byte) 0xFF;
                }
            }
        }
    }
    DataBufferByte imgData = new DataBufferByte(writeBuf, sizeUncompressed);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, getWidth(), getHeight(), 4, getWidth() * 4, ZAHLEN);
    WritableRaster imgRaster = Raster.createWritableRaster(sm, imgData, new Point(0, 0));
    BufferedImage aa = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    aa.setData(imgRaster);
    return aa;
}
 
Example 18
@Override
public BufferedImage getImage() {
    int sizeUncompressed = 0;
    int size8888 = 0;
    int maxWriteBuf = 2;
    int maxHeight = 3;
    byte[] writeBuf = new byte[maxWriteBuf];
    @SuppressWarnings ("unused")
    byte[] rowPointers = new byte[maxHeight];
    switch (getFormat()) {
        case 1:
        case 513:
            sizeUncompressed = getHeight() * getWidth() * 4;
            break;
        case 2:
            sizeUncompressed = getHeight() * getWidth() * 8;
            break;
        case 517:
            sizeUncompressed = getHeight() * getWidth() / 128;
            break;
    }
    size8888 = getHeight() * getWidth() * 8;
    if (size8888 > maxWriteBuf) {
        maxWriteBuf = size8888;
        writeBuf = new byte[maxWriteBuf];
    }
    if (getHeight() > maxHeight) {
        maxHeight = getHeight();
        rowPointers = new byte[maxHeight];
    }
    Inflater dec = new Inflater();
    dec.setInput(getData(), 0, dataLength);
    int declen = 0;
    byte[] uc = new byte[sizeUncompressed];
    try {
        declen = dec.inflate(uc);
    } catch (DataFormatException ex) {
        throw new RuntimeException("zlib fucks", ex);
    }
    dec.end();
    if (getFormat() == 1) {
        for (int i = 0; i < sizeUncompressed; i++) {
            byte low = (byte) (uc[i] & 0x0F);
            byte high = (byte) (uc[i] & 0xF0);
            writeBuf[(i << 1)] = (byte) (((low << 4) | low) & 0xFF);
            writeBuf[(i << 1) + 1] = (byte) (high | ((high >>> 4) & 0xF));
        }
    } else if (getFormat() == 2) {
        writeBuf = uc;
    } else if (getFormat() == 513) {
        for (int i = 0; i < declen; i += 2) {
            byte bBits = (byte) ((uc[i] & 0x1F) << 3);
            byte gBits = (byte) (((uc[i + 1] & 0x07) << 5) | ((uc[i] & 0xE0) >> 3));
            byte rBits = (byte) (uc[i + 1] & 0xF8);
            writeBuf[(i << 1)] = (byte) (bBits | (bBits >> 5));
            writeBuf[(i << 1) + 1] = (byte) (gBits | (gBits >> 6));
            writeBuf[(i << 1) + 2] = (byte) (rBits | (rBits >> 5));
            writeBuf[(i << 1) + 3] = (byte) 0xFF;
        }
    } else if (getFormat() == 517) {
        byte b = 0x00;
        int pixelIndex = 0;
        for (int i = 0; i < declen; i++) {
            for (int j = 0; j < 8; j++) {
                b = (byte) (((uc[i] & (0x01 << (7 - j))) >> (7 - j)) * 255);
                for (int k = 0; k < 16; k++) {
                    pixelIndex = (i << 9) + (j << 6) + k * 2;
                    writeBuf[pixelIndex] = b;
                    writeBuf[pixelIndex + 1] = b;
                    writeBuf[pixelIndex + 2] = b;
                    writeBuf[pixelIndex + 3] = (byte) 0xFF;
                }
            }
        }
    }
    DataBufferByte imgData = new DataBufferByte(writeBuf, sizeUncompressed);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, getWidth(), getHeight(), 4, getWidth() * 4, ZAHLEN);
    WritableRaster imgRaster = Raster.createWritableRaster(sm, imgData, new Point(0, 0));
    BufferedImage aa = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    aa.setData(imgRaster);
    return aa;
}
 
Example 19
@Override
public BufferedImage getImage() {
    int sizeUncompressed = 0;
    int size8888 = 0;
    int maxWriteBuf = 2;
    int maxHeight = 3;
    byte[] writeBuf = new byte[maxWriteBuf];
    @SuppressWarnings ("unused")
    byte[] rowPointers = new byte[maxHeight];
    switch (getFormat()) {
        case 1:
        case 513:
            sizeUncompressed = getHeight() * getWidth() * 4;
            break;
        case 2:
            sizeUncompressed = getHeight() * getWidth() * 8;
            break;
        case 517:
            sizeUncompressed = getHeight() * getWidth() / 128;
            break;
    }
    size8888 = getHeight() * getWidth() * 8;
    if (size8888 > maxWriteBuf) {
        maxWriteBuf = size8888;
        writeBuf = new byte[maxWriteBuf];
    }
    if (getHeight() > maxHeight) {
        maxHeight = getHeight();
        rowPointers = new byte[maxHeight];
    }
    Inflater dec = new Inflater();
    dec.setInput(getData(), 0, dataLength);
    int declen = 0;
    byte[] uc = new byte[sizeUncompressed];
    try {
        declen = dec.inflate(uc);
    } catch (DataFormatException ex) {
        throw new RuntimeException("zlib fucks", ex);
    }
    dec.end();
    if (getFormat() == 1) {
        for (int i = 0; i < sizeUncompressed; i++) {
            byte low = (byte) (uc[i] & 0x0F);
            byte high = (byte) (uc[i] & 0xF0);
            writeBuf[(i << 1)] = (byte) (((low << 4) | low) & 0xFF);
            writeBuf[(i << 1) + 1] = (byte) (high | ((high >>> 4) & 0xF));
        }
    } else if (getFormat() == 2) {
        writeBuf = uc;
    } else if (getFormat() == 513) {
        for (int i = 0; i < declen; i += 2) {
            byte bBits = (byte) ((uc[i] & 0x1F) << 3);
            byte gBits = (byte) (((uc[i + 1] & 0x07) << 5) | ((uc[i] & 0xE0) >> 3));
            byte rBits = (byte) (uc[i + 1] & 0xF8);
            writeBuf[(i << 1)] = (byte) (bBits | (bBits >> 5));
            writeBuf[(i << 1) + 1] = (byte) (gBits | (gBits >> 6));
            writeBuf[(i << 1) + 2] = (byte) (rBits | (rBits >> 5));
            writeBuf[(i << 1) + 3] = (byte) 0xFF;
        }
    } else if (getFormat() == 517) {
        byte b = 0x00;
        int pixelIndex = 0;
        for (int i = 0; i < declen; i++) {
            for (int j = 0; j < 8; j++) {
                b = (byte) (((uc[i] & (0x01 << (7 - j))) >> (7 - j)) * 255);
                for (int k = 0; k < 16; k++) {
                    pixelIndex = (i << 9) + (j << 6) + k * 2;
                    writeBuf[pixelIndex] = b;
                    writeBuf[pixelIndex + 1] = b;
                    writeBuf[pixelIndex + 2] = b;
                    writeBuf[pixelIndex + 3] = (byte) 0xFF;
                }
            }
        }
    }
    DataBufferByte imgData = new DataBufferByte(writeBuf, sizeUncompressed);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, getWidth(), getHeight(), 4, getWidth() * 4, ZAHLEN);
    WritableRaster imgRaster = Raster.createWritableRaster(sm, imgData, new Point(0, 0));
    BufferedImage aa = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    aa.setData(imgRaster);
    return aa;
}
 
Example 20
public static PlanarImage decodeImage (String[] rows)
    {
        // Create the DataBuffer to hold the pixel samples
        final int width = rows[0].length();
        final int height = rows.length;

        // Create Raster
        Raster raster;
        if (true) {
            raster = Raster.createPackedRaster
            (DataBuffer.TYPE_INT, width, height,
             new int[] {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000},// bandMasks RGBA
             null);
        } else {
            raster = Raster.createInterleavedRaster
                (DataBuffer.TYPE_BYTE, width, height,
                 4,// num of bands
                 null);
        }

        // Populate the data buffer
        DataBuffer dataBuffer = raster.getDataBuffer();
        int index = 0;
        for (String row : rows) {
            for (int x = 0; x < width; x++) {
                int argb = toARGB(row.charAt(x));
                dataBuffer.setElem(index, argb);
                index++;
            }
        }

        // Dump
//         final int size = width * height;
//         System.out.println("DataBuffer :");
//         for (int i = 0; i < size; i++) {
//             if (i % width == 0) {
//                 System.out.println();
//             }
//             System.out.print(String.format("%8x ", dataBuffer.getElem(i)));
//         }
//         System.out.println();

        // Create the image
        BufferedImage bufferedImage = new BufferedImage
                (width, height, BufferedImage.TYPE_INT_ARGB);
        bufferedImage.setData(raster);

        // Dump
//         System.out.println("BufferedImage :");
//         for (int y = 0; y < height; y++) {
//             System.out.println();
//             for (int x = 0; x < width; x++) {
//                 System.out.print(String.format("%8x ", bufferedImage.getRGB(x, y)));
//             }
//         }
//         System.out.println();

        return PlanarImage.wrapRenderedImage(bufferedImage);
    }