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

The following examples show how to use java.awt.image.BufferedImage#setData() . 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 check out the related API usage on the sidebar.
Example 1
Source File: JPEGFactory.java    From gcs with 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
Source File: ImageUtils.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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
Source File: PixelatedImageSectionFilter.java    From cognitivej with Apache License 2.0 6 votes vote down vote up
@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
Source File: TestImage2.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
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
Source File: ManipulationUtils.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
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 File: PDDeviceRGB.java    From sambox with 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 File: ImageConvert.java    From MyBox with 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 File: GameUIAnalyzer.java    From jump-jump-game with 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
Source File: MarvinImage.java    From scrimage with 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 10
Source File: ImageHolder.java    From BLELocalization with 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 11
Source File: PageDrawer.java    From sambox with 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 12
Source File: TestImage3.java    From libreveris with GNU Lesser General Public License v3.0 4 votes vote down vote up
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);
    }