java.awt.image.DataBufferByte Java Examples

The following examples show how to use java.awt.image.DataBufferByte. 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: ImageTests.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public Image makeImage(TestEnvironment env, int w, int h) {
    BufferedImage img = new BufferedImage(w, h, type);
    if (unmanaged) {
        DataBuffer db = img.getRaster().getDataBuffer();
        if (db instanceof DataBufferInt) {
            ((DataBufferInt)db).getData();
        } else if (db instanceof DataBufferShort) {
            ((DataBufferShort)db).getData();
        } else if (db instanceof DataBufferByte) {
            ((DataBufferByte)db).getData();
        } else {
            try {
                img.setAccelerationPriority(0.0f);
            } catch (Throwable e) {}
        }
    }
    return img;
}
 
Example #2
Source File: ImageIOHelper.java    From tess4j with Apache License 2.0 6 votes vote down vote up
/**
 * Converts <code>BufferedImage</code> to <code>ByteBuffer</code>.
 *
 * @param bi Input image
 * @return pixel data
 */
public static ByteBuffer convertImageData(BufferedImage bi) {
    DataBuffer buff = bi.getRaster().getDataBuffer();
    // ClassCastException thrown if buff not instanceof DataBufferByte because raster data is not necessarily bytes.
    // Convert the original buffered image to grayscale.
    if (!(buff instanceof DataBufferByte)) {
        BufferedImage grayscaleImage = ImageHelper.convertImageToGrayscale(bi);
        buff = grayscaleImage.getRaster().getDataBuffer();
    }
    byte[] pixelData = ((DataBufferByte) buff).getData();
    //        return ByteBuffer.wrap(pixelData);
    ByteBuffer buf = ByteBuffer.allocateDirect(pixelData.length);
    buf.order(ByteOrder.nativeOrder());
    buf.put(pixelData);
    ((Buffer) buf).flip();
    return buf;
}
 
Example #3
Source File: IncorrectUnmanagedImageSourceOffset.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI(final int type) {
    final BufferedImage bi = new BufferedImage(511, 255, type);
    final DataBuffer db = bi.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            bi.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return bi;
}
 
Example #4
Source File: IncorrectAlphaConversionBicubic.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
                                             int type) {
    BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(RGB);
    g2d.fillRect(0, 0, SIZE, SIZE);
    g2d.dispose();
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
 
Example #5
Source File: IncorrectClipXorModeSW2Surface.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage getBufferedImage(int sw) {
    final BufferedImage bi = new BufferedImage(sw, sw, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.setColor(Color.RED);
    g2d.fillRect(0, 0, sw, sw);
    g2d.dispose();

    final DataBuffer db = bi.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            bi.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return bi;
}
 
Example #6
Source File: IncorrectAlphaConversionBicubic.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
                                             int type) {
    BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(RGB);
    g2d.fillRect(0, 0, SIZE, SIZE);
    g2d.dispose();
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
 
Example #7
Source File: IncorrectAlphaConversionBicubic.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
                                             int type) {
    BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(RGB);
    g2d.fillRect(0, 0, SIZE, SIZE);
    g2d.dispose();
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
 
Example #8
Source File: ImageTests.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public Image makeImage(TestEnvironment env, int w, int h) {
    BufferedImage img = new BufferedImage(w, h, type);
    if (unmanaged) {
        DataBuffer db = img.getRaster().getDataBuffer();
        if (db instanceof DataBufferInt) {
            ((DataBufferInt)db).getData();
        } else if (db instanceof DataBufferShort) {
            ((DataBufferShort)db).getData();
        } else if (db instanceof DataBufferByte) {
            ((DataBufferByte)db).getData();
        } else {
            try {
                img.setAccelerationPriority(0.0f);
            } catch (Throwable e) {}
        }
    }
    return img;
}
 
Example #9
Source File: IncorrectAlphaConversionBicubic.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
                                             int type) {
    BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(RGB);
    g2d.fillRect(0, 0, SIZE, SIZE);
    g2d.dispose();
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
 
Example #10
Source File: ByteRaster.java    From osp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Constructs a byte raster with the given size.
 *
 * Unsigned cell values are 0 to 255. Signed cell values are -128 to 127.
 *
 * @param _nx the number of values in x direction
 * @param _ny the number of values in y direction
 */
public ByteRaster(int _nx, int _ny) {
  ny = _ny;
  nx = _nx;
  dimension = new Dimension(nx-1, ny-1); // decrease by one to fit inside axes
  int len = nx*ny;
  packedData = new byte[len];
  DataBuffer databuffer = new DataBufferByte(packedData, len);
  raster = Raster.createPackedRaster(databuffer, nx, ny, 8, null);
  colorModel = createColorModel();
  image = new BufferedImage(colorModel, raster, false, null);
  xmin = 0;
  xmax = nx;
  ymin = 0;
  ymax = ny;
}
 
Example #11
Source File: GifEncoder.java    From kk-anti-reptile with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts image pixels into byte array "pixels"
 */
protected void getImagePixels() {
    int w = image.getWidth();
    int h = image.getHeight();
    int type = image.getType();
    if ((w != width)
            || (h != height)
            || (type != BufferedImage.TYPE_3BYTE_BGR)) {
        // create new image with right size/format
        BufferedImage temp =
                new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = temp.createGraphics();
        g.drawImage(image, 0, 0, null);
        image = temp;
    }
    pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
}
 
Example #12
Source File: UnmanagedDrawImagePerformance.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI(final int type) {
    final BufferedImage img = new BufferedImage(SIZE, SIZE, type);
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
 
Example #13
Source File: ImageTests.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public Image makeImage(TestEnvironment env, int w, int h) {
    BufferedImage img = new BufferedImage(w, h, type);
    if (unmanaged) {
        DataBuffer db = img.getRaster().getDataBuffer();
        if (db instanceof DataBufferInt) {
            ((DataBufferInt)db).getData();
        } else if (db instanceof DataBufferShort) {
            ((DataBufferShort)db).getData();
        } else if (db instanceof DataBufferByte) {
            ((DataBufferByte)db).getData();
        } else {
            try {
                img.setAccelerationPriority(0.0f);
            } catch (Throwable e) {}
        }
    }
    return img;
}
 
Example #14
Source File: IncorrectAlphaConversionBicubic.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
                                             int type) {
    BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(RGB);
    g2d.fillRect(0, 0, SIZE, SIZE);
    g2d.dispose();
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
 
Example #15
Source File: UnmanagedDrawImagePerformance.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI(final int type) {
    final BufferedImage img = new BufferedImage(SIZE, SIZE, type);
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
 
Example #16
Source File: IncorrectUnmanagedImageRotatedClip.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI() {
    final BufferedImage bi = new BufferedImage(500, 200, TYPE_INT_ARGB);
    final DataBuffer db = bi.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            bi.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return bi;
}
 
Example #17
Source File: ImageLoader.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Load a rastered image from file
 *
 * @param file the file to load
 * @return the rastered image
 * @throws IOException
 */
public int[][][] fromFileMultipleChannels(File file) throws IOException {
    BufferedImage image = ImageIO.read(file);
    image = scalingIfNeed(image, channels > 3);

    int w = image.getWidth(), h = image.getHeight();
    int bands = image.getSampleModel().getNumBands();
    int[][][] ret = new int[(int) Math.min(channels, Integer.MAX_VALUE)]
            [(int) Math.min(h, Integer.MAX_VALUE)]
            [(int) Math.min(w, Integer.MAX_VALUE)];
    byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            for (int k = 0; k < channels; k++) {
                if (k >= bands)
                    break;
                ret[k][i][j] = pixels[(int) Math.min(channels * w * i + channels * j + k, Integer.MAX_VALUE)];
            }
        }
    }
    return ret;
}
 
Example #18
Source File: ImageTests.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public Image makeImage(TestEnvironment env, int w, int h) {
    BufferedImage img = new BufferedImage(w, h, type);
    if (unmanaged) {
        DataBuffer db = img.getRaster().getDataBuffer();
        if (db instanceof DataBufferInt) {
            ((DataBufferInt)db).getData();
        } else if (db instanceof DataBufferShort) {
            ((DataBufferShort)db).getData();
        } else if (db instanceof DataBufferByte) {
            ((DataBufferByte)db).getData();
        } else {
            try {
                img.setAccelerationPriority(0.0f);
            } catch (Throwable e) {}
        }
    }
    return img;
}
 
Example #19
Source File: ImageTests.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public Image makeImage(TestEnvironment env, int w, int h) {
    BufferedImage img = new BufferedImage(w, h, type);
    if (unmanaged) {
        DataBuffer db = img.getRaster().getDataBuffer();
        if (db instanceof DataBufferInt) {
            ((DataBufferInt)db).getData();
        } else if (db instanceof DataBufferShort) {
            ((DataBufferShort)db).getData();
        } else if (db instanceof DataBufferByte) {
            ((DataBufferByte)db).getData();
        } else {
            try {
                img.setAccelerationPriority(0.0f);
            } catch (Throwable e) {}
        }
    }
    return img;
}
 
Example #20
Source File: Utils.java    From Pixie with MIT License 6 votes vote down vote up
/**
 * Flip vertically the image
 *
 * @param image the image containing the pixel data which has to be flipped
 * vertically. This function will alter the bytes of input image, so that
 * after this function the input image will contain the flipped information
 */
public static void flipVerticallyImage(BufferedImage image) {
    byte[] bgr = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    byte[] flipped = new byte[bgr.length];
    final int multiplier = 3; // BGR

    for (int j = 0; j < image.getHeight(); j++) {
        for (int i = 0; i < image.getWidth(); i++) {
            flipped[((image.getHeight() - 1) - j) * (image.getWidth() * multiplier) + (i * multiplier + 0)] = bgr[(j * (image.getWidth() * multiplier)) + (i * multiplier + 0)]; // R
            flipped[((image.getHeight() - 1) - j) * (image.getWidth() * multiplier) + (i * multiplier + 1)] = bgr[(j * (image.getWidth() * multiplier)) + (i * multiplier + 1)]; // G
            flipped[((image.getHeight() - 1) - j) * (image.getWidth() * multiplier) + (i * multiplier + 2)] = bgr[(j * (image.getWidth() * multiplier)) + (i * multiplier + 2)]; // B
        }
    }

    System.arraycopy(flipped, 0, bgr, 0, image.getWidth() * image.getHeight() * multiplier);
}
 
Example #21
Source File: IncorrectUnmanagedImageRotatedClip.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI() {
    final BufferedImage bi = new BufferedImage(500, 200, TYPE_INT_ARGB);
    final DataBuffer db = bi.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            bi.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return bi;
}
 
Example #22
Source File: Utils.java    From BlindWatermark with Apache License 2.0 6 votes vote down vote up
public static Mat drawNonAscii(String watermark) {
    Font font = new Font("Default", Font.PLAIN, 64);
    FontMetrics metrics = new Canvas().getFontMetrics(font);
    int width = metrics.stringWidth(watermark);
    int height = metrics.getHeight();
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    Graphics2D graphics = bufferedImage.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
    graphics.setFont(font);
    graphics.setColor(Color.WHITE);
    graphics.drawString(watermark, 0, metrics.getAscent());
    graphics.dispose();
    byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
    Mat res = new Mat(bufferedImage.getHeight(), bufferedImage.getWidth(), CV_8U);
    res.put(0, 0, pixels);
    return res;
}
 
Example #23
Source File: ImageTests.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public Image makeImage(TestEnvironment env, int w, int h) {
    BufferedImage img = new BufferedImage(w, h, type);
    if (unmanaged) {
        DataBuffer db = img.getRaster().getDataBuffer();
        if (db instanceof DataBufferInt) {
            ((DataBufferInt)db).getData();
        } else if (db instanceof DataBufferShort) {
            ((DataBufferShort)db).getData();
        } else if (db instanceof DataBufferByte) {
            ((DataBufferByte)db).getData();
        } else {
            try {
                img.setAccelerationPriority(0.0f);
            } catch (Throwable e) {}
        }
    }
    return img;
}
 
Example #24
Source File: IncorrectAlphaConversionBicubic.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
                                             int type) {
    BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(RGB);
    g2d.fillRect(0, 0, SIZE, SIZE);
    g2d.dispose();
    final DataBuffer db = img.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            img.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return img;
}
 
Example #25
Source File: SciView.java    From sciview with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Take a screenshot and return it as an Img
 * @return an Img of type UnsignedByteType
 */
public Img<UnsignedByteType> getScreenshot() {
    RenderedImage screenshot = getSceneryRenderer().requestScreenshot();

    BufferedImage image = new BufferedImage(screenshot.getWidth(), screenshot.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    byte[] imgData = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    System.arraycopy(screenshot.getData(), 0, imgData, 0, screenshot.getData().length);

    Img<UnsignedByteType> img = null;
    File tmpFile = null;
    try {
        tmpFile = File.createTempFile("sciview-", "-tmp.png");
        ImageIO.write(image, "png", tmpFile);
        img = (Img<UnsignedByteType>)io.open(tmpFile.getAbsolutePath());
        tmpFile.delete();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return img;
}
 
Example #26
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Extracts pixel data as arrays of unsigned bytes, one per channel. */
public static byte[][] getBytes(final WritableRaster r, final int x,
	final int y, final int w, final int h)
{
	if (canUseBankDataDirectly(r, DataBuffer.TYPE_BYTE, DataBufferByte.class) &&
		x == 0 && y == 0 && w == r.getWidth() && h == r.getHeight())
	{
		return ((DataBufferByte) r.getDataBuffer()).getBankData();
	}
	final int c = r.getNumBands();
	final byte[][] samples = new byte[c][w * h];
	final int[] buf = new int[w * h];
	for (int i = 0; i < c; i++) {
		r.getSamples(x, y, w, h, i, buf);
		for (int j = 0; j < buf.length; j++)
			samples[i][j] = (byte) buf[j];
	}
	return samples;
}
 
Example #27
Source File: AnimatedGifEncoder.java    From util.commons with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Extracts image pixels into byte array "pixels"
 */
protected void getImagePixels() {
    int w = image.getWidth();
    int h = image.getHeight();
    int type = image.getType();
    if ((w != width) || (h != height)
            || (type != BufferedImage.TYPE_3BYTE_BGR)) {
        // create new image with right size/format
        BufferedImage temp = new BufferedImage(width, height,
                BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = temp.createGraphics();
        g.drawImage(image, 0, 0, null);
        image = temp;
    }
    pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
}
 
Example #28
Source File: GifEncoder.java    From OneBlog with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Extracts image pixels into byte array "pixels"
 */
protected void getImagePixels() {
    int w = image.getWidth();
    int h = image.getHeight();
    int type = image.getType();
    if ((w != width)
            || (h != height)
            || (type != BufferedImage.TYPE_3BYTE_BGR)) {
        // create new image with right size/format
        BufferedImage temp =
                new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = temp.createGraphics();
        g.drawImage(image, 0, 0, null);
        image = temp;
    }
    pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
}
 
Example #29
Source File: LCDJessie.java    From ev3dev-lang-java with MIT License 5 votes vote down vote up
private void init(
    final int width,
    final int height,
    final int lineLength,
    final int bufferSize) {

    this.SCREEN_WIDTH = width;
    this.SCREEN_HEIGHT = height;
    this.LINE_LEN = lineLength;
    this.BUFFER_SIZE = bufferSize;

    if (Files.notExists(Paths.get(FB_PATH))) {
        throw new RuntimeException("Device path not found: " + FB_PATH);
    }

    byte[] data = new byte[bufferSize];
    byte[] bwarr = {(byte) 0xff, (byte) 0x00};
    IndexColorModel bwcm = new IndexColorModel(1, bwarr.length, bwarr, bwarr, bwarr);

    DataBuffer db = new DataBufferByte(data, data.length);
    WritableRaster wr = Raster.createPackedRaster(db, SCREEN_WIDTH, SCREEN_HEIGHT, 1, null);

    this.image = new BufferedImage(bwcm, wr, false, null);
    this.g2d = (Graphics2D) image.getGraphics();

    g2d.setColor(Color.WHITE);
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    this.refresh();
}
 
Example #30
Source File: JFIFMarkerSegment.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
BufferedImage getThumbnail(ImageInputStream iis,
                           JPEGImageReader reader)
    throws IOException {
    iis.mark();
    iis.seek(streamPos);
    DataBufferByte buffer = new DataBufferByte(getLength());
    readByteBuffer(iis,
                   buffer.getData(),
                   reader,
                   1.0F,
                   0.0F);
    iis.reset();

    WritableRaster raster =
        Raster.createInterleavedRaster(buffer,
                                       thumbWidth,
                                       thumbHeight,
                                       thumbWidth*3,
                                       3,
                                       new int [] {0, 1, 2},
                                       null);
    ColorModel cm = new ComponentColorModel(JPEG.JCS.sRGB,
                                            false,
                                            false,
                                            ColorModel.OPAQUE,
                                            DataBuffer.TYPE_BYTE);
    return new BufferedImage(cm,
                             raster,
                             false,
                             null);
}