Reputation: 149
So basically I have a image and I need to split it into 4 smaller images.
All I got right now is that the images print on the top- left corner but I need to print on in the top right, bottom left and bottom right.
I have two for loops that go through each pixel of the images.
This is the code that prints out just the top-left.
setRGB(x/2,y/2,everypixel)
the "everypixel" is just the pixels in the image.
That will print out just the top-left but how do I print it out for every other corner.
Thanks.
EDIT:
This is basically what I want. The original to the n = 4 (Ignore the n=2)
Thats why I basically want to do with my picture ^^
Upvotes: 1
Views: 1225
Reputation: 1143
You should start by scaling down your original image to 1/4 size (see this StackOverflow answer for details - use at.scale(0.5, 0.5)
instead of 2.0,2.0
).
Then you loop over the x and y dimensions of the scaled-down image and in your "corner" images and 4 times you set the pixel with x and y offsets:
for (int x = 0; x < scaledImageWidth; x++) {
for (int y = 0; y < scaledImageHeight; y++) {
imageTL.setRGB (x, y, scaledImagePixels[x][y]);
imageTR.setRGB (x + scaledImageWidth, y, scaledImagePixels[x][y]);
imageBL.setRGB (x, y + scaledImageHeight, scaledImagePixels[x][y]);
imageBR.setRGB (x + scaledImageWidth, y + scaledImageHeight, scaledImagePixels[x][y]);
}
}
Note that scaledImageWidth should be your original image width divided by 2, and scaledImageHeight should be your original image height divided by 2.
Upvotes: 2
Reputation: 1264
Assuming you have a 2-dimentional array everypixel having pixel color value of every pixel in original image, and maxX and maxY as the dimentions or original image, following skeleton code should ideally do the job:
// Top-Left sub-image (imageTL)
for (int x = 0; x < maxX/2; x++) {
for (int y = 0; y < maxY/2; y++) {
imageTL.setRGB (x, y, everypixel[x][y]);
}
}
// Top-Right sub-image (imageTR)
for (int x = maxX/2; x < maxX; x++) {
for (int y = 0; y < maxY/2; y++) {
imageTR.setRGB (x - maxX/2, y, everypixel[x][y]);
}
}
// Bottom-Left sub-image (imageBL)
for (int x = 0; x < maxX/2; x++) {
for (int y = maxY/2; y < maxY; y++) {
imageBL.setRGB (x, y - maxY/2, everypixel[x][y]);
}
}
// Botton-Right sub-image (image BR)
for (int x = maxX/2; x < maxX; x++) {
for (int y = maxY/2; y < maxY; y++) {
imageBR.setRGB (x - maxX/2, y - maxY/2, everypixel[x][y]);
}
}
Upvotes: 1