I have this png image:
and a string, say "Hello World". In order to map texture coords for LWJGL, I need to know the X and Y position of each 16x16 character in the PNG. I am completely lost on how one would do that.. Anyone?
Start with something like this:
final int spriteWidth = 16;
final int spriteHeight = 16;
...
int rows = sheet.getWidth()/spriteWidth;
int cols = sheet.getHeight()/spriteHeight;
BufferedImage sheet = ImageIO.read(new File("\\a\b\\c\\sprite_file.png"));
BufferedImage[] images = new BufferedImage[rows * cols];
for(int y = 0; y < cols; y++) {
for(int x = 0; x < rows; x++) {
images[y * x] = sheet.getSubImage(x * rows, y * cols, spriteWidth, spriteHeight);
}
}
Then make final int variables like so:
public static final int SPRITE_0 = 0; public static final int SPRITE_1 = 1;...
and access like so:
images[SPRITE_0]
Edit:
taking into consideration what #MadProgrammer has stated, I would recommend that you split the image into two parts, like so:
(split at the red line)
and then simply altering the code to handle the two different parts. The code will remain the same except for the variables final int spriteWidth and final int spriteHeight. I'm sure you can handle this yourself.
Edit 2:
if you just want the x and y co-ords of the top left corner of each sprite, do the following:
final int spriteWidth = 16;
final int spriteHeight = 16;
...
int rows = sheet.getWidth()/spriteWidth;
int cols = sheet.getHeight()/spriteHeight;
Point[] spriteTopLeftCorner = new Point[rows * cols];
for(int y = 0; y < sheet.getHeight(); y += spriteHeight) {
for(int x = 0; x < sheet.getWidth(); x += spriteWidth) {
spriteTopLeftCorner[y/spriteHeight * x/spriteWidth] = new Point(y, x);
}
}
you would ofcourse still need to make variables representing each sprite's index in this Array, otherwise you wouldn't know what sprite you are taking out.
Do this like so:
public static final int SPRITE_0 = 0; public static final int SPRITE_1 = 1;...
and access like so:
spriteTopLeftCorner[SPRITE_0];
Related
as the title says, i'm trying to get the screen coordinates for each cell in my map. is it even possible?
I'm really frustraited and can't figure it out!
I appreciate your help!
note that my map has a static position on the screen.
Another note: i have a custom class Cell that extends Actor. I made it to make my cells clickable and it looks like this:
public class Cell extends Actor {
private TiledMapTileLayer.Cell tiledMapCell;
private Texture cellTexture;
public Cell(TiledMapTileLayer.Cell tiledMapCell, Field field){
this.tiledMapCell = tiledMapCell;
this.field = field;
this.cellTexture = tiledMapCell.getTile().getTextureRegion().getTexture();
}
public TiledMapTileLayer.Cell getTiledMapCell(){
return this.tiledMapCell;
}
public Texture getCellTexture(){
return this.cellTexture;
}
public void setCellTexture(Texture texture){
this.cellTexture = texture;
this.tiledMapCell.setTile(new StaticTiledMapTile(new TextureRegion(cellTexture)));
Thanks!!
You can process like this :
In your Cell class add a new Variable to get the position of the tile.
private Vector2 pos;
Change your constructor.
public Cell(TiledMapTileLayer.Cell tiledMapCell, Field field, Vector2 pos) {
this.tiledMapCell = tiledMapCell;
this.field = field;
this.cellTexture = tiledMapCell.getTile().getTextureRegion().getTexture();
this.pos = pos;
}
In your main class get the number of tiles in the width of your map, do the same for the height and the size of a tile.
int mapWidth = tiledMapTileLayer.getWidth();
int mapHeight = tiledMapTileLayer.getHeight();
int tileSize = tiledMapTileLayer.getTileWidth();
now use a loop
// These variables will store the coordinates of each new tile. and will be changed at each loop turn.
int tempX = 0;
int tempY = 0;
for (int i = 0; i < mapWidth * mapHeight; i++) {
pos = new Vector2 (tempX + tileSize / 2, tempY + tileSize / 2);
// Create a new cell with your Cell class, pass it the position.
new Cell(TiledMapTileLayer.Cell tiledMapCell, Field field, pos)
// Increase the tempX
tempX += tileSize;
// If the first line is finish we go to the next one.
if (tempX > mapWidth * tileSize) {
tempY += tileSize;
tempX = 0;
}
}
This is the method i use cause i did not find any other way.
Hi I am in need of some help. I need to write a convolution method from scratch that takes in the following inputs: int[][] and BufferedImage inputImage. I can assume that the kernel has size 3x3.
My approach is to do the follow:
convolve inner pixels
convolve corner pixels
convolve outer pixels
In the program that I will post below I believe I convolve the inner pixels but I am a bit lost at how to convolve the corner and outer pixels. I am aware that corner pixels are at (0,0), (width-1,0), (0, height-1) and (width-1,height-1). I think I know to how approach the problem but not sure how to execute that in writing though. Please to aware that I am very new to programming :/ Any assistance will be very helpful to me.
import java.awt.*;
import java.awt.image.BufferedImage;
import com.programwithjava.basic.DrawingKit;
import java.util.Scanner;
public class Problem28 {
// maximum value of a sample
private static final int MAX_VALUE = 255;
//minimum value of a sample
private static final int MIN_VALUE = 0;
public BufferedImage convolve(int[][] kernel, BufferedImage inputImage) {
}
public BufferedImage convolveInner(double center, BufferedImage inputImage) {
int width = inputImage.getWidth();
int height = inputImage.getHeight();
BufferedImage inputImage1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//inner pixels
for (int x = 1; x < width - 1; x++) {
for (int y = 1; y < height - 1; y ++) {
//get pixels at x, y
int colorValue = inputImage.getRGB(x, y);
Color pixelColor = new Color(colorValue);
int red = pixelColor.getRed() ;
int green = pixelColor.getGreen() ;
int blue = pixelColor.getBlue();
int innerred = (int) center*red;
int innergreen = (int) center*green;
int innerblue = (int) center*blue;
Color newPixelColor = new Color(innerred, innergreen, innerblue);
int newRgbvalue = newPixelColor.getRGB();
inputImage1.setRGB(x, y, newRgbvalue);
}
}
return inputImage1;
}
public BufferedImage convolveEdge(double edge, BufferedImage inputImage) {
int width = inputImage.getWidth();
int height = inputImage.getHeight();
BufferedImage inputImage2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//inner pixels
for (int x = 0; x < width - 1; x++) {
for (int y = 0; y < height - 1; y ++) {
//get pixels at x, y
int colorValue = inputImage.getRGB(x, y);
Color pixelColor = new Color(colorValue);
int red = pixelColor.getRed() ;
int green = pixelColor.getGreen() ;
int blue = pixelColor.getBlue();
int innerred = (int) edge*red;
int innergreen = (int) edge*green;
int innerblue = (int) edge*blue;
Color newPixelColor = new Color(innerred, innergreen, innerblue);
int newRgbvalue = newPixelColor.getRGB();
inputImage2.setRGB(x, y, newRgbvalue);
}
}
return inputImage2;
}
public BufferedImage convolveCorner(double corner, BufferedImage inputImage) {
int width = inputImage.getWidth();
int height = inputImage.getHeight();
BufferedImage inputImage3 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//inner pixels
for (int x = 0; x < width - 1; x++) {
for (int y = 0; y < height - 1; y ++) {
//get pixels at x, y
int colorValue = inputImage.getRGB(x, y);
Color pixelColor = new Color(colorValue);
int red = pixelColor.getRed() ;
int green = pixelColor.getGreen() ;
int blue = pixelColor.getBlue();
int innerred = (int) corner*red;
int innergreen = (int) corner*green;
int innerblue = (int) corner*blue;
Color newPixelColor = new Color(innerred, innergreen, innerblue);
int newRgbvalue = newPixelColor.getRGB();
inputImage3.setRGB(x, y, newRgbvalue);
}
}
return inputImage3;
}
public static void main(String[] args) {
DrawingKit dk = new DrawingKit("Compositor", 1000, 1000);
BufferedImage p1 = dk.loadPicture("image/pattern1.jpg");
Problem28 c = new Problem28();
BufferedImage p5 = c.convolve();
dk.drawPicture(p5, 0, 100);
}
}
I changed the code a bit but the output comes out as black. What did I do wrong:
import java.awt.*;
import java.awt.image.BufferedImage;
import com.programwithjava.basic.DrawingKit;
import java.util.Scanner;
public class Problem28 {
// maximum value of a sample
private static final int MAX_VALUE = 255;
//minimum value of a sample
private static final int MIN_VALUE = 0;
public BufferedImage convolve(int[][] kernel, BufferedImage inputImage) {
int width = inputImage.getWidth();
int height = inputImage.getHeight();
BufferedImage inputImage1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//for every pixel
for (int x = 0; x < width; x ++) {
for (int y = 0; y < height; y ++) {
int colorValue = inputImage.getRGB(x,y);
Color pixelColor = new Color(colorValue);
int red = pixelColor.getRed();
int green = pixelColor.getGreen();
int blue = pixelColor.getBlue();
double gray = 0;
//multiply every value of kernel with corresponding image pixel
for (int i = 0; i < 3; i ++) {
for (int j = 0; j < 3; j ++) {
int imageX = (x - 3/2 + i + width) % width;
int imageY = (x -3/2 + j + height) % height;
int RGB = inputImage.getRGB(imageX, imageY);
int GRAY = (RGB) & 0xff;
gray += (GRAY*kernel[i][j]);
}
}
int out;
out = (int) Math.min(Math.max(gray * 1, 0), 255);
inputImage1.setRGB(x, y, new Color(out,out,out).getRGB());
}
}
return inputImage1;
}
public static void main(String[] args) {
int[][] newArray = {{1/9, 1/9, 1/9}, {1/9, 1/9, 1/9}, {1/9, 1/9, 1/9}};
DrawingKit dk = new DrawingKit("Problem28", 1000, 1000);
BufferedImage p1 = dk.loadPicture("image/pattern1.jpg");
Problem28 c = new Problem28();
BufferedImage p2 = c.convolve(newArray, p1);
dk.drawPicture(p2, 0, 100);
}
}
Welcome ewuzz! I wrote a convolution using CUDA about a week ago, and the majority of my experience is with Java, so I feel qualified to provide advice for this problem.
Rather than writing all of the code for you, the best way to solve this large program is to discuss individual elements. You mentioned you are very new to programming. As the programs you write become more complex, it's essential to write small working snippets before combining them into a large successful program (or iteratively add snippets). With this being said, it's already apparent you're trying to debug a ~100 line program, and this approach will cost you time in most cases.
The first point to discuss is the general approach you mentioned. If you think about the program, what is the simplest and most repeated step? Obviously this is the kernel/mask step, so we can start from here. When you convolute each pixel, you are performing a similar option, regardless of the position (corner, edge, inside). While there are special steps necessary for these edge cases, they share similar underlying steps. If you try to write code for each of these cases separately, you will have to update the code in multiple (three) places with each adjustment and it will make the whole program more difficult to grasp.
To support my point above, here's what happened when I pasted your code into IntelliJ. This illustrates the (yellow) red flag of using the same code in multiple places:
The concrete way to fix this problem is to combine the three convolve methods into a single one and use if statements for edge-cases as necessary.
Our pseudocode with this change:
convolve(kernel, inputImage)
for each pixel in the image
convolve the single pixel and check edge cases
endfor
end
That seems pretty basic right? If we are able to successfully check edge cases, then this extremely simple logic will work. The reason I left it so general above to show how convolve the single pixel and check edge cases is logically grouped. This means it's a good candidate for extracting a method, which could look like:
private void convolvePixel(int x, int y, int[][] kernel, BufferedImage input, BufferedImage output)
Now to implement our method above, we will need to break it into a few steps, which we may then break into more steps if necessary. We'll need to look at the input image, if possible for each pixel accumulate the values using the kernel, and then set this in the output image. For brevity I will only write pseudocode from here.
convolvePixel(x, y, kernel, input, output)
accumulation = 0
for each row of kernel applicable pixels
for each column of kernel applicable pixels
if this neighboring pixel location is within the image boundaries then
input color = get the color at this neighboring pixel
adjusted value = input color * relative kernel mask value
accumulation += adjusted value
else
//handle this somehow, mentioned below
endif
endfor
endfor
set output pixel as accumulation, assuming this convolution method does not require normalization
end
The pseudocode above is already relatively long. When implementing you could write methods for the if and the else cases, but it you should be fine with this structure.
There are a few ways to handle the edge case of the else above. Your assignment probably specifies a requirement, but the fancy way is to tile around, and pretend like there's another instance of the same image next to this input image. Wikipedia explains three possibilities:
Extend - The nearest border pixels are conceptually extended as far as necessary to provide values for the convolution. Corner pixels are extended in 90° wedges. Other edge pixels are extended in lines.
Wrap - (The method I mentioned) The image is conceptually wrapped (or tiled) and values are taken from the opposite edge or corner.
Crop - Any pixel in the output image which would require values from beyond the edge is skipped. This method can result in the output image being slightly smaller, with the edges having been cropped.
A huge part of becoming a successful programmer is researching on your own. If you read about these methods, work through them on paper, run your convolvePixel method on single pixels, and compare the output to your results by hand, you will find success.
Summary:
Start by cleaning-up your code before anything.
Group the same code into one place.
Hammer out a small chunk (convolving a single pixel). Print out the result and the input values and verify they are correct.
Draw out edge/corner cases.
Read about ways to solve edge cases and decide what fits your needs.
Try implementing the else case through the same form of testing.
Call your convolveImage method with the loop, using the convolvePixel method you know works. Done!
You can look up pseudocode and even specific code to solve the exact problem, so I focused on providing general insight and strategies I have developed through my degree and personal experience. Good luck and please let me know if you want to discuss anything else in the comments below.
Java code for multiple blurs via convolution.
Background
I am doing a game project for my java class and we are using a grid system for the playing area. 50 wide by 25 tall. The squares are dynamically sized based on your monitor size and it involves a bit of math. I removed that from my example below so it is easier to look at.
What I currently decided to do is store the grid squares in a nice and simple 2D array so we can access and update the playing area as needed. Here is that code:
// Updates when told to, saves new values
public static void GridList() {
// Record which grid squares are open (0) or blocked (1)
// X = Row and Y = Column
for (int x = 0; x < 50; x++ ) {
for ( int y = 0; y < 25; y++ ) {
gridList[x][y] = 0;
}
}
}
Our Problem
Now the trouble starts when I move on to saving order pairs (x,y) that represent the middle of these grid squares. For example, based on all the math we did to figure out your monitor size, we made a grid 50 wide by 25 tall and now need to save the (x,y) coordinates of the middle of those squares. This is so our AI knows where to move enemies; point by point as long as it is open.
This is what I have so far that saves X coordinates and Y coordinates in their own arrays:
public static void NodeList() {
for (int x = 0; x < 50; x++ ) {
for ( int y = 0; y < 25; y++ ) {
nodeListX[x][y] = *Removed all the math.*;
nodeListY[x][y] = *Removed all the math.*;
}
}
}
What We Are Aiming To Have
What I would really like to do is save an array for every grid square like this:
public static void NodeList() {
for (int x = 0; x < 50; x++ ) {
for ( int y = 0; y < 25; y++ ) {
nodeList[x][y] = *array{x,y}*;
}
}
}
Is this possible in Java? I can't figure this out. I saw things mentioned about lists but we have not covered that yet so I am at a loss.
Java doesn't really have a way to store a pair of numbers. But you could make a coordinate class like this:
`public class Coordinate
int x;
int y;
public Coordinate(x,y)
{
this.x=x;
this.y=y;
}
public int getX()
{
return x;
}
public int gety()
{
return y;
}
`
Then you can just create an array of coordinates.
Honestly, I don't see the problem with your or fdsa's solution. But if you're looking for another approach, you can always use a 3-dimensional array, with the third dimension containing 2 elements for X and Y:
public static void NodeList() {
for (int x = 0; x < 50; x++ ) {
for ( int y = 0; y < 25; y++ ) {
int xPos = *Removed all the math.*;
int yPos = *Removed all the math.*;
nodeList[x][y] = new int[] {xPos, yPos};
}
}
}
Just make sure to declare nodeList as int[][][].
I spent the whole day trying to implement the "convolution algorithm" in Java, but this last does not seem to work properly with all kernels, it works great with the blur kernel with a factor of 1/9, but not with the other ones.
For example, if I use the {{0.1.0},{0,0,0},{0,0,0}} matrix which is supposed to shift the image up by 1 pixel, surprisingly, it stretches the image all the way down.
Example of what I get:
And here is the code:
public class ConvolutionTest {
static BufferedImage bfimg;
static BufferedImage outputimg;
static File output = new File("/home/bz/Pictures/Selection_003_mod");
static File input = new File("/home/bz/Pictures/Selection_003.png");
static WritableRaster wr;
static int tempColor;
static double [] newColor = {0,0,0};
static double red=0, green=0, blue=0;
static double sumR=0, sumG=0, sumB=0;
public static void main(String[] args) throws IOException {
int tempIcoor;
int tempJcoor;
double[][] matConv = {
{0d, 1d, 0d},
{0d, 0d, 0d},
{0d, 0d, 0d}
};
bfimg = ImageIO.read(input);
outputimg = bfimg;
wr = outputimg.getRaster();
for (int i = 1; i < bfimg.getHeight()-1; i++) {
for (int j = 1; j < bfimg.getWidth()-1; j++) {
tempIcoor = i - 1;
tempJcoor = j - 1;
for (int tempI = 0; tempI < 3; tempI++) {
for (int tempJ = 0; tempJ < 3; tempJ++) {
tempColor = bfimg.getRGB(tempJcoor, tempIcoor);
red = tempColor >> 16 & 0xff;
red = red * matConv[tempI][tempJ];
green = tempColor >> 8 & 0xff;
green = green * matConv[tempI][tempJ];
blue = tempColor & 0xff;
blue = blue * matConv[tempI][tempJ];;
sumR = red + sumR;
sumG = green + sumG;
sumB = blue + sumB;
tempJcoor++;
}
newColor[0] = sumR;
newColor[1] = sumG;
newColor[2] = sumB;
tempIcoor++;
tempJcoor=j-1;
}
wr.setPixel(j, i, newColor);
sumR=0;
sumG=0;
sumB=0;
}
}
ImageIO.write(sortie, "png", output);
}
}
With
outputimg = bfimg;
you are setting the output image to be the same as the input image. When you perform the convolution of the first row, then (as you said) the first row of pixels from the input image will be written into the the second row of the output image. But they are identical - so you end up with all rows of the output image being copies of the first row of the input image.
Just replace this line with
outputimg = new BufferedImage(
bfimg.getWidth(), bfimg.getHeight(),
BufferedImage.TYPE_INT_ARGB);
to create a new output image to write to.
By the way: All this is already available in the standard API. You might want to have a look at the classes related to http://docs.oracle.com/javase/7/docs/api/java/awt/image/ConvolveOp.html
I want to do a copy (of a rectangle area) of the ARGB values from a source BufferedImage into a destination BufferedImage. No compositing should be done: if I copy a pixel with an ARGB value of 0x8000BE50 (alpha value at 128), then the destination pixel must be exactly 0x8000BE50, totally overriding the destination pixel.
I've got a very precise question and I made a unit test to show what I need. The unit test is fully functional and self-contained and is passing fine and is doing precisely what I want.
However, I want a faster and more memory efficient method to replace copySrcIntoDstAt(...).
That's the whole point of my question: I'm not after how to "fill" the image in a faster way (what I did is just an example to have a unit test). All I want is to know what would be a fast and memory efficient way to do it (ie fast and not creating needless objects).
The proof-of-concept implementation I've made is obviously very memory efficient, but it is slow (doing one getRGB and one setRGB for every pixel).
Schematically, I've got this: (where A indicates corresponding pixels from the destination image before the copy)
AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA
And I want to have this:
AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAABBBBAAA
AAAAAAAAAAAAABBBBAAA
AAAAAAAAAAAAAAAAAAAA
where 'B' represents the pixels from the src image.
Note that I'm looking for an exact replacement of the copySrcIntoDstAt(...) method, not for an API link/quote.
import org.junit.Test;
import java.awt.image.BufferedImage;
import static org.junit.Assert.*;
public class TestCopy {
private static final int COL1 = 0x8000BE50; // alpha at 128
private static final int COL2 = 0x1732FE87; // alpha at 23
#Test
public void testPixelsCopy() {
final BufferedImage src = new BufferedImage( 5, 5, BufferedImage.TYPE_INT_ARGB );
final BufferedImage dst = new BufferedImage( 20, 20, BufferedImage.TYPE_INT_ARGB );
convenienceFill( src, COL1 );
convenienceFill( dst, COL2 );
copySrcIntoDstAt( src, dst, 3, 4 );
for (int x = 0; x < dst.getWidth(); x++) {
for (int y = 0; y < dst.getHeight(); y++) {
if ( x >= 3 && x <= 7 && y >= 4 && y <= 8 ) {
assertEquals( COL1, dst.getRGB(x,y) );
} else {
assertEquals( COL2, dst.getRGB(x,y) );
}
}
}
}
// clipping is unnecessary
private static void copySrcIntoDstAt(
final BufferedImage src,
final BufferedImage dst,
final int dx,
final int dy
) {
// TODO: replace this by a much more efficient method
for (int x = 0; x < src.getWidth(); x++) {
for (int y = 0; y < src.getHeight(); y++) {
dst.setRGB( dx + x, dy + y, src.getRGB(x,y) );
}
}
}
// This method is just a convenience method, there's
// no point in optimizing this method, this is not what
// this question is about
private static void convenienceFill(
final BufferedImage bi,
final int color
) {
for (int x = 0; x < bi.getWidth(); x++) {
for (int y = 0; y < bi.getHeight(); y++) {
bi.setRGB( x, y, color );
}
}
}
}
private static void copySrcIntoDstAt(final BufferedImage src,
final BufferedImage dst, final int dx, final int dy) {
int[] srcbuf = ((DataBufferInt) src.getRaster().getDataBuffer()).getData();
int[] dstbuf = ((DataBufferInt) dst.getRaster().getDataBuffer()).getData();
int width = src.getWidth();
int height = src.getHeight();
int dstoffs = dx + dy * dst.getWidth();
int srcoffs = 0;
for (int y = 0 ; y < height ; y++ , dstoffs+= dst.getWidth(), srcoffs += width ) {
System.arraycopy(srcbuf, srcoffs , dstbuf, dstoffs, width);
}
}