Convert short[] into a grayscale image - java

I am writing a Buddhabrot fractal generator using aparapi. I got the OpenCL part of it to work, resulting in a single-dimension array that represents each pixel. I have the dimensions of the final image as final ints, and have written code to get the index of arbitrary points in that array. I want to save this as an image and I'm trying to use BufferedImage with TYPE_USHORT_GRAY. Here's what I have so far:
BufferedImage image=new BufferedImage(VERTICAL_PIXELS, HORIZONTAL_PIXELS, BufferedImage.TYPE_USHORT_GRAY);
for(int i=0; i<VERTICAL_PIXELS; i++)
for(int k=0; k<HORIZONTAL_PIXELS; k++)
image.setRGB(k, i, normalized[getArrayIndex(k,i,HORIZONTAL_PIXELS)]);
The problem is, I don't know what to set the RGB as. What do I need to do?

The problem here is that setRGB() wants an 0xRRGGBB color value. BufferedImage likes to pretend that the image is RGB, no matter what the data is stored as. You can actually get at the internal DataBufferShort (with getTile(0, 0).getDataBuffer()), but it can be tricky to figure out how it is laid out.
If you already have your pixels in a short[], a simpler solution might be to copy them into an int[] instead an jam it into a MemoryImageSource:
int[] buffer = /* pixels */;
ColorModel model = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] { 16 },
false, true, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
Image image = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(VERTICAL_PIXELS, HORIZONTAL_PIXELS,
model, buffer, 0, VERTICAL_PIXELS));
The advantage of this approach is that you control the underlying pixel array. You could make changes to that array and call newPixels() on your MemoryImageSource, and it would update live. It also gives you complete power to define your own palette other than grayscale:
int[] cmap = new int[65536];
for(int i = 0; i < 65536; ++i) {
cmap[i] = (((i % 10000) * 256 / 10000) << 16)
| (((i % 20000) * 256 / 20000) << 8)
| (((i % 40000) * 256 / 40000) << 0);
}
ColorModel model = new IndexColorModel(16, 65536, cmap, 0, false, -1, DataBuffer.TYPE_USHORT);
This approach works fine if you just want to display the image on the screen:
JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
However, if you wanted to write it out to a file and preserve the one-short-per-pixel format (say, to load into Matlab) then you're out of luck. The best you can do is to paint it into a BufferedImage and save that with ImageIO, which will save as RGB.
If you definitely need a BufferedImage at the end, another approach is to apply the color palette yourself, calculate the RGB values, and then copy them into the image:
short[] data = /* your data */;
int[] cmap = /* as above */;
int[] rgb = new int[data.length];
for(int i = i; i < rgb.length; ++i) {
rgb[i] = cmap[data[i]];
}
BufferedImage image = new BufferedImage(
VERTICAL_PIXELS, HORIZONTAL_PIXELS,
BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, VERTICAL_PIXELS, HORIZONTAL_PIXELS,
pixels, 0, VERTICAL_PIXELS);

For reference, this example shows how two different BufferedImage types interpret the same 16 bit data. You can mouse over the images to see the pixel values.
Addendum: To elaborate on the word interpret, note that setRGB() tries to find the closest match to the specified value in the given ColorModel.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** #see http://stackoverflow.com/questions/8765004 */
public class BufferedImageTest extends JPanel {
private static final int SIZE = 256;
private static final Random r = new Random();
private final BufferedImage image;
public BufferedImageTest(int type) {
image = new BufferedImage(SIZE, SIZE, type);
this.setPreferredSize(new Dimension(SIZE, SIZE));
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
image.setRGB(col, row, 0xff00 << 16 | row << 8 | col);
}
}
this.addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
int x = p.x * SIZE / getWidth();
int y = p.y * SIZE / getHeight();
int c = image.getRGB(x, y);
setToolTipText(x + "," + y + ": "
+ String.format("%08X", c));
}
});
}
#Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
static private void display() {
JFrame f = new JFrame("BufferedImageTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(1, 0));
f.add(new BufferedImageTest(BufferedImage.TYPE_INT_ARGB));
f.add(new BufferedImageTest(BufferedImage.TYPE_USHORT_GRAY));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
display();
}
});
}
}

Related

is there a faster Way to display massive amounts of ascii (with font size and font type) than JTextPane

I have a video to ASCII animation converter and the pre-processing and compiling I do outside is fast but when I start displaying text it lags a lot in the beginning I thought it was that bad to calculate text size but using system.out.prinln was superfast and even if the carriage return works (which it doesn't) I'd like to have a sperate GUI/CLI panel for it
here is sample code that turns an image into ASCII
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class Main {
static short width;
static short height;
public static BufferedImage mytoBufferedImage(Image img){
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
BufferedImage buffImage = new BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D bGr = buffImage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return buffImage;
}
public static BufferedImage scaleApple(BufferedImage image){
return mytoBufferedImage(
image.getScaledInstance(width, height, BufferedImage.SCALE_FAST));
}
public static void main(String[] args) throws IOException, FontFormatException, InterruptedException {
width = 256;
height = 192;
BufferedImage Frame;
ArrayList<String> FrameBuffer = new ArrayList<>();
StringBuilder ASCIIFrame;
ArrayList<Byte> Bright;
final String[] Rendered;
byte R;
byte G;
byte B;
Frame = mytoBufferedImage(new ImageIcon("example.jpg").getImage());
Bright = new ArrayList<>();
int[] data =((DataBufferInt)
scaleApple(Frame)
.getRaster()
.getDataBuffer())
.getData();
for (int datum : data) {
Color c = new Color(datum);
R = (byte) c.getRed();
G = (byte) c.getGreen();
B = (byte) c.getBlue();
Bright.add((byte) Math.round(Math.sqrt( 0.299*((R&0xFF)^2) + 0.587*((G&0xFF)^2) + 0.114*((B&0xFF)^2))));
}
ASCIIFrame = new StringBuilder();
// j+(width*i) is cords
for (int i = 0; i < Main.height; i++) {
for (int j = 0; j < Main.width; j++) {
byte bright = Bright.get((j + (width * i)));
if ((bright <= 16) && (bright >= 12)) {
ASCIIFrame.append('█');
ASCIIFrame.append('█');
}else if ((bright <= 12) && (bright >= 8)) {
ASCIIFrame.append('▓');
ASCIIFrame.append('▓');
}else if ((bright <= 8) && (bright >= 4)) {
ASCIIFrame.append('▒');
ASCIIFrame.append('▒');
}else {
ASCIIFrame.append(' ');
ASCIIFrame.append(' ');
}
}
ASCIIFrame.append('\n');
}
FrameBuffer.add(ASCIIFrame.toString());
Rendered = FrameBuffer.toArray(String[]::new);
JTextPane render = new JTextPane();
render.setDoubleBuffered(false);
InputStream fontIS = new FileInputStream("JetBrainsMonoNL-Regular.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontIS).deriveFont(3f);
render.setFont(font);
StyledDocument doc = render.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
JFrame Jframe = new JFrame();
JPanel MainPanel = new JPanel();
MainPanel.add(render);
render.setBackground(new Color(255,255,255));
Jframe.setTitle("Ascii");
Jframe.setSize(730, 645);
Jframe.setLocationRelativeTo(null);
Jframe.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
Jframe.setContentPane(MainPanel);
Jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Jframe.setVisible(true);
for (String Rendered_frame: Rendered){
System.out.println(Rendered_frame); // FAST
render.setText(Rendered_frame); // 99%GPU usage on rtx 3060
Thread.sleep(300);// timer methode in actual code
}
}
}
I tried carriage return, to make it easy to see System.out.println('\r'+Rendered_frame); and it didn't work
and Making A buffered Image and drawing on it with Graphics2D then displaying it on a Jlabel, is inaccurate, the display flickers and chops even with double Buffered set to true

How to recognize an image in another image?

So I'm fairly new to Java, so I'm not sure that this my method of doing this is actually a good idea, but basically I am trying to check for an instance of an image inside another image. So for my testing to see if this would work, I had a 200x148 jpg, got the bytes from that, then got the bytes from a screenshot of the window, and got the bytes from that, and then compared them.
Now, since normally the first image wouldn't be in that screenshot, I opened it in my photos app and put it in while the program was sleeping (before it took the screenshot). And yes, I can confirm that the first image was in the screenshot by looking at the screenshot. However, when I compare the strings (to check if a String with all of the byte data of image one is in a String with all of the byte data of image two), it turns out negative.
Here's the code that I'm trying to use so far:
public static void main(String[] args) throws IOException, HeadlessException, AWTException, InterruptedException {
// Get the first image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(ImageIO.read(new File("profile.jpg")), "jpg", baos);
byte[] bytes = baos.toByteArray();
String bytes1S = "";
for (int i = 0; i < bytes.length; i++) {
bytes1S += bytes[i];
}
// Give yourself enough time to open the other image
TimeUnit.SECONDS.sleep(6);
// Take the screenshot
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "jpg", new File("screenshot.jpg"));
baos = new ByteArrayOutputStream();
ImageIO.write(ImageIO.read(new File("screenshot.jpg")), "jpg", baos);
byte[] bytes2 = baos.toByteArray();
String bytes2S = "";
for (int i = 0; i < bytes2.length; i++) {
bytes2S += bytes2[i];
}
// Check if the second String of bytes contains the first String of bytes.
if (bytes2S.contains(bytes1S))
System.out.println("Yes");
else
System.out.println("No");
}
And for reference, here's the first image, and the screenshot that it took:
What's the reason behind it not detecting the first image in the screenshot, and is there a better way to do this (preferably without another library)?
A brute-force approach is to simply load both images as BufferedImage objects, and then walk through the "main" image, pixel by pixel, and see if the "sub image" can be found there.
I have implemented this a while ago, and will post the code below as a MCVE.
But note: When you save images as JPG files, then they are compressed, and this compression is lossy. This means that the pixels will not have the perfectly same colors, even if they have been equal on the screen. In the example below, this is solved pragmatically, with a "threshold" that defines how different the pixels may be. But this is a bit arbitrary and not so reliable. (A more robust solution would require more effort).
I'd strongly recommend to save the images as PNG files. They use a lossless compression. So for PNG files, you can set threshold=0 in the code below.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.function.IntBinaryOperator;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class FindImageInImage
{
public static void main(String[] args) throws Exception
{
BufferedImage mainImage =
ImageIO.read(new URL("https://i.stack.imgur.com/rEouF.jpg"));
BufferedImage subImage =
ImageIO.read(new URL("https://i.stack.imgur.com/wISyn.jpg"));
int threshold = 100;
Point location = findImageLocation(
mainImage, subImage, threshold);
System.out.println("At " + location);
SwingUtilities.invokeLater(() -> showIt(mainImage, subImage, location));
}
private static void showIt(
BufferedImage mainImage, BufferedImage subImage, Point location)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JPanel()
{
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(mainImage, 0, 0, null);
if (location != null)
{
g.setColor(Color.RED);
g.drawRect(location.x, location.y,
subImage.getWidth(), subImage.getHeight());
}
}
});
f.setSize(1500, 800);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
static Point findImageLocation(
BufferedImage mainImage,
BufferedImage subImage,
int threshold)
{
return findImageLocation(mainImage, subImage, (rgb0, rgb1) ->
{
int difference = computeDifference(rgb0, rgb1);
if (difference > threshold)
{
return 1;
}
return 0;
});
}
private static int computeDifference(int rgb0, int rgb1)
{
int r0 = (rgb0 & 0x00FF0000) >> 16;
int g0 = (rgb0 & 0x0000FF00) >> 8;
int b0 = (rgb0 & 0x000000FF);
int r1 = (rgb1 & 0x00FF0000) >> 16;
int g1 = (rgb1 & 0x0000FF00) >> 8;
int b1 = (rgb1 & 0x000000FF);
int dr = Math.abs(r0 - r1);
int dg = Math.abs(g0 - g1);
int db = Math.abs(b0 - b1);
return dr + dg + db;
}
static Point findImageLocation(
BufferedImage mainImage,
BufferedImage subImage,
IntBinaryOperator rgbComparator)
{
int w = mainImage.getWidth();
int h = mainImage.getHeight();
for (int x=0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
if (isSubImageAt(mainImage, x, y, subImage, rgbComparator))
{
return new Point(x, y);
}
}
}
return null;
}
static boolean isSubImageAt(
BufferedImage mainImage, int x, int y,
BufferedImage subImage,
IntBinaryOperator rgbComparator)
{
int w = subImage.getWidth();
int h = subImage.getHeight();
if (x + w > mainImage.getWidth())
{
return false;
}
if (y + h > mainImage.getHeight())
{
return false;
}
for (int ix=0; ix < w; ix++)
{
for (int iy = 0; iy < h; iy++)
{
int mainRgb = mainImage.getRGB(x + ix, y + iy);
int subRgb = subImage.getRGB(ix, iy);
if (rgbComparator.applyAsInt(mainRgb, subRgb) != 0)
{
return false;
}
}
}
return true;
}
}

How do I fade the edges of an image in Java? (example given)

I am currently in the middle of a project in which I have to grayscale, blue filter, red filter, green filter, and fade the edges of a picture of a cat at the press of a JButton. grayscale and color filtering the picture was easy, but I can't seem to figure out how to fade the image's edges to black.
What I mean by this, is I have this picture of the cat I need to change:
and I need to change it into something like this one:
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class GUIKittenPicJC {
public static class KittenChanger extends JPanel {
BufferedImage img = null;
File Kitten = null;
ImageIcon imagetransformed = null;
JButton grayscale = new JButton("Grayscale Image");
JButton rgbB = new JButton("Blue filter this image");
JButton rgbR = new JButton("Red filter this image");
JButton rgbG = new JButton("Green filter this image");
JButton fader = new JButton("Fade this image");
{
try
{
Kitten = new File("C:\\Users\\Jarrod\\Desktop\\Lab 3\\Lab 3\\kitten.bmp");
img = ImageIO.read(Kitten);
}
catch(IOException e)
{
System.out.println(e);
}
ImageIcon image = new ImageIcon(img);
JLabel imageLabel = new JLabel(image);
add(imageLabel);
grayscale.addActionListener(e->{
imagetransformed = new ImageIcon(Grayscale(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
rgbB.addActionListener(e->{
imagetransformed = new ImageIcon(Bluify(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
rgbG.addActionListener(e->{
imagetransformed = new ImageIcon(Greenify(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
rgbR.addActionListener(e->{
imagetransformed = new ImageIcon(Redify(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
add(grayscale);
add(rgbB);
add(rgbG);
add(rgbR);
}
private void imgReset() {
try {
img = ImageIO.read(Kitten);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
public static void main(String[] args)
{
createGUI();
}
private static void createGUI() {
JFrame frame = new JFrame("Kitten Changer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
KittenChanger newContentPane = new KittenChanger();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setSize(400, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//Grayscale and rgb filter methods follow, not important to question
}
How would I create a method for this sort of filter and properly apply it to a button like the others?
You would have to edit each of the pixels individually. The main idea of this solution is that you have the center of an image, and each pixel is changed as a function of the distance to the center.
More simply put, pixels farther from the center will be made darker.
This is what your code would look like:
BufferedImage cat; //assuming it is assigned
for(int i = 0; i < cat.getWidth(); i++) { // i is the x coord
for(int j = 0; j < cat.getHeight(); j++) { // j is the y coord
int color = cat.getRGB(i, j);
int r = (color >> 16) & 0xff; //extract red value
int g = (color >> 8) & 0xff;
int b = color & 0xff;
double scale = 0.75; /**** Change this to change the resulting effect ****/
//pixel's distance from center
double dist = Math.sqrt( Math.pow(i - cat.getWidth()/2, 2) + Math.pow(j - cat.getHeight()/2, 2) );
r = (int) Math.max(0, r - dist*scale); //r - dist*scale makes px darker
g = (int) Math.max(0, g - dist*scale); //Math.max makes sure r is always >= 0
b = (int) Math.max(0, b - dist*scale);
int newRGB = (r << 16) + (g << 8) + b; //convert r,g,b to single int
cat.setRGB(i, j, newRGB); //finally, update rgb value
}
}
And, when I ran this code:
Remember, you can always change the effect by changing the scale variable in the code above.

Get color of each pixel of an image using BufferedImages

I am trying to get every single color of every single pixel of an image.
My idea was following:
int[] pixels;
BufferedImage image;
image = ImageIO.read(this.getClass.getResources("image.png");
int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
Is that right? I can't even check what the "pixels" array contains, because i get following error:
java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
I just would like to receive the color of every pixel in an array, how do i achieve that?
import java.io.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class GetPixelColor {
public static void main(String args[]) throws IOException {
File file = new File("your_file.jpg");
BufferedImage image = ImageIO.read(file);
// Getting pixel color by position x and y
int clr = image.getRGB(x, y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
System.out.println("Red Color value = " + red);
System.out.println("Green Color value = " + green);
System.out.println("Blue Color value = " + blue);
}
}
of course you have to add a for loop for all pixels
The problem (also with the answer that was linked from the first answer) is that you hardly ever know what exact type your buffered image will be after reading it with ImageIO. It could contain a DataBufferByte or a DataBufferInt. You may deduce it in some cases via BufferedImage#getType(), but in the worst case, it has type TYPE_CUSTOM, and then you can only fall back to some instanceof tests.
However, you can convert your image into a BufferedImage that is guaranteed to have a DataBufferInt with ARGB values - namely with something like
public static BufferedImage convertToARGB(BufferedImage image)
{
BufferedImage newImage = new BufferedImage(
image.getWidth(), image.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}
Otherwise, you can call image.getRGB(x,y), which may perform the required conversions on the fly.
BTW: Note that obtaining the data buffer of a BufferedImage may degrade painting performance, because the image can no longer be "managed" and kept in VRAM internally.
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedImage bufferedImage = ImageIO.read(new File("norris.jpg"));
int height = bufferedImage.getHeight(), width = bufferedImage.getWidth();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int RGBA = bufferedImage.getRGB(x, y);
int alpha = (RGBA >> 24) & 255;
int red = (RGBA >> 16) & 255;
int green = (RGBA >> 8) & 255;
int blue = RGBA & 255;
}
}
}
}
Assume the buffered image represents an image with 8-bit RGBA color components packed into integer pixels, I search for "RGBA color space" on wikipedia and found following:
In the byte-order scheme, "RGBA" is understood to mean a byte R,
followed by a byte G, followed by a byte B, and followed by a byte A.
This scheme is commonly used for describing file formats or network
protocols, which are both byte-oriented.
With simple Bitwise and Bitshift you can get the value of each color and the alpha value of the pixel.
Very interesting is also the other order scheme of RGBA:
In the word-order scheme, "RGBA" is understood to represent a complete
32-bit word, where R is more significant than G, which is more
significant than B, which is more significant than A. This scheme can
be used to describe the memory layout on a particular system. Its
meaning varies depending on the endianness of the system.
byte[] pixels
not
int[] pixels
try this : Java - get pixel array from image
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageUtil {
public static Color[][] loadPixelsFromImage(File file) throws IOException {
BufferedImage image = ImageIO.read(file);
Color[][] colors = new Color[image.getWidth()][image.getHeight()];
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
colors[x][y] = new Color(image.getRGB(x, y));
}
}
return colors;
}
public static void main(String[] args) throws IOException {
Color[][] colors = loadPixelsFromImage(new File("image.png"));
System.out.println("Color[0][0] = " + colors[0][0]);
}
}
I know this has already been answered, but the answers given are a bit convoluted and could use improvement.
The simple idea is to just loop through every (x,y) pixel in the image, and get the color of that pixel.
BufferedImage image = MyImageLoader.getSomeImage();
for ( int x = 0; x < image.getWidth(); x++ ) {
for( int y = 0; y < image.getHeight(); y++ ) {
Color pixel = new Color( image.getRGB( x, y ) );
// Do something with pixel color here :)
}
}
You could then perhaps wrap this method in a class, and implement Java's Iterable API.
class IterableImage implements Iterable<Color> {
private BufferedImage image;
public IterableImage( BufferedImage image ) {
this.image = image;
}
#Override
public Iterator<Color> iterator() {
return new Itr();
}
private final class Itr implements Iterator<Color> {
private int x = 0, y = 0;
#Override
public boolean hasNext() {
return x < image.getWidth && y < image.getHeight();
}
#Override
public Color next() {
x += 1;
if ( x >= image.getWidth() ) {
x = 0;
y += 1;
}
return new Color( image.getRGB( x, y ) );
}
}
}
The usage of which might look something like the following
BufferedImage image = MyImageLoader.getSomeImage();
for ( Color color : new IterableImage( image ) ) {
// Do something with color here :)
}

How to get sub image from a sprite sheet into an array

I have a sprite sheet that will be 5x5 and each sprite is 20x20 pixels. I need to get each item of that sprite sheet into an array. I made a for loop but got an error. So I tried defining one of the sub images in the array myself but still get an error. I do not get an error when I don't print an item of that array. I now can not figure out the for loop problem.
Here's my code so far
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class LoopSub extends Component {
BufferedImage img;
BufferedImage img2;
BufferedImage bigImg;
BufferedImage[] sprites;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
g.drawImage(img2, 18, 0, null); //letter size 20x20 ; set lower to overlap
g.drawImage(sprites[0], 36, 0, null); //this is whats causing the error
}
public LoopSub() {
try {
img = ImageIO.read(new File("res/a.png"));
img2 = ImageIO.read(new File("res/b.png"));
/////////////////////////////
bigImg = ImageIO.read(new File("sheet.png"));
final int width = 20;
final int height = 20;
final int rows = 5;
final int cols = 5;
sprites = new BufferedImage[rows * cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sprites[(i * cols) + j] = bigImg.getSubimage(
j * width,
i * height,
width,
height
);
}
}
sprites[0] = bigImg.getSubimage(1,1,1,1); //where I tried to define the array myself
/////////////////////////////////////////////
} catch (IOException e) {
}
}
public Dimension getPreferredSize() { //sets size of screen
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null)); //sets size to one image //// change to all images
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new LoopSub());
f.pack();
f.setVisible(true);
}
}
New error message
Exception in thread "main" java.awt.image.RasterFormatException: (x + width) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at LoopSub.<init>(LoopSub.java:48)
at LoopSub.main(LoopSub.java:85)
When changing j*width to 1 it works fine. So that's the problem. Just don't know why.
You have a NullPointerException, which means that either sprites or sprites[0] is null.
Given that you initialize them in the constructor, it can happens only if there is an IOException occuring in the constructor, because you are catching it, so it doesn't terminate the program.
You should:
Never silently ignore an exception, like you are doing here. if you don't do anything useful, at least put a message to print the error like this:
} catch (IOException e) {
e.printStackTrace();
}
Look what is the content of the exception, which contains the root of the problem. Probably the program can not find one of your files for some reason.
Edit: for the RasterFormatException, it means that you are getting out of the limits of the image. More specifically, j * width + width is apparently larger than the width of the image.
Since j is at most 4 and width is 20, then the maximum value that j * width + width can have is 100. Your original image should then be at least 100px wide (and also 100px tall). Check that it is indeed the case.
You can for example print the width this way before the loop:
System.out.println("bigImg width: " + bigImg.getWidth());

Categories

Resources