Efficient way to pixelate an image by manipulating pixels - java

I made two methods for a class called Picture, the name is self explanatory. The getAverageColor() method gets the average color of all the pixels in a certain area of the image specified by the parameters passed in. In the pixelate() method, it uses getAverageColor() to pixelate the image. The whole thing works, however it takes upwards of 2 minutes to pixelate a single image. It takes even longer if the pixelSize parameter is made smaller and the image is larger. So I was wondering if there is a better algorithm for doing this by manipulating the pixels.
/**
* NOTE: The smaller the pixelSize the longer the pixelation process takes
*/
public void pixelate(int pixelSize)
{
Pixel[][] pixels = this.getPixels2D();
int blockSize = pixelSize;
Color averageColor = null;
for(int row = 0; row < pixels.length; row += blockSize)
{
for (int col = 0; col < pixels[row].length; col += blockSize)
{
if (!((col + blockSize > pixels[0].length) || (row + blockSize > pixels.length)))
{
averageColor = getAverageColor(row, col, row+blockSize, col+blockSize);
}
for (int row_2 = row; (row_2 < row + blockSize) && (row_2 < pixels.length); row_2++)
{
for (int col_2 = col; (col_2 < col + blockSize) && (col_2 < pixels[0].length); col_2++)
{
pixels[row_2][col_2].setColor(averageColor);
}
}
}
}
}
public Color getAverageColor(int startRow, int startCol, int endRow, int endCol)
{
Pixel[][] pixels = this.getPixels2D();
Color averageColor = null;
int totalPixels = (endRow - startRow)*(endCol - startCol);
int totalRed = 0;
int averageRed = 0;
int totalGreen = 0;
int averageGreen = 0;
int totalBlue = 0;
int averageBlue = 0;
for (int row = startRow; row < endRow; row++)
{
for (int col = startCol; col < endCol; col++)
{
totalRed += pixels[row][col].getRed();
totalGreen += pixels[row][col].getGreen();
totalBlue += pixels[row][col].getBlue();
}
}
averageRed = totalRed / totalPixels;
averageGreen = totalGreen / totalPixels;
averageBlue = totalBlue / totalPixels;
averageColor = new Color(averageRed, averageGreen, averageBlue);
return averageColor;
}

Related

Tiles for a BufferedImage in Java, any resolution

I have a class to separate and join image in tiles. It works fine when the sides of the tile correspond to the dimensions of the image, i.e. height 250, tile height 25. But when it's not it doesn't create smaller tiles at the borders as it should.
Where would be the problem to properly create the border tiles smaller than the rest?
Constructor:
public EdgeBufferedImage(BufferedImage image, int w, int h){
this.sourceImg = image;
this.setCol((int)Math.ceil(image.getWidth()/(double)w));
this.setRow((int)Math.ceil(image.getHeight()/(double)h));
this.setWidth(image.getWidth());
this.setHeight(image.getHeight());
this.setTilew(w);
this.setTileh(h);
this.setMatImg(new BufferedImage[row][col]);
}
Methods:
Image tiling
public void createSmallImages() {
int rows = getRow();
int columns = getCol();
int smallWidth = getTilew();
int smallHeight = getTileh();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (j == columns - 1) smallWidth = getWidth() - (getTilew() * j);
if (i == rows - 1) smallHeight = getHeight() - (getTileh() * i);
matImg[i][j] = getSourceImg().getSubimage(j * smallWidth, i
* smallHeight, smallWidth, smallHeight);
}
smallWidth = getTilew();
smallHeight = getTileh();
}
}
Image joining
public void joinTiles(){
int rows = getRow();
int columns = getCol();
int smallWidth = getTilew();
int smallHeight = getTileh();
BufferedImage comb = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) comb.getGraphics();
g.setColor(Color.RED);
for (int row = 0; row < rows; row++){
for (int col = 0; col < columns; col++){
BufferedImage piece = getMatImg()[row][col];
if (col == columns - 1) smallWidth = getWidth() - (getTilew() * col);
if (row == rows - 1) smallHeight = getHeight() - (getTileh() * row);
g.drawImage(piece, col * smallWidth, row * smallHeight, smallWidth, smallHeight, null);
g.drawRect(col * smallWidth, row * smallHeight, smallWidth, smallHeight);
}
smallWidth = getTilew();
smallHeight = getTileh();
}
g.dispose();
setSourceImg(comb);
}
Original image is 512*512

Why does this code only rotate squares?

I will use this algorithm for image rotation, however I realized that it only rotates squares, not rectangles.
Would anyone know why?
Main code-problem:
public static int[] rotate(double angle, int[] pixels, int width, int height) {
final double radians = Math.toRadians(angle);
final double cos = Math.cos(radians);
final double sin = Math.sin(radians);
final int[] pixels2 = new int[pixels.length];
for(int pixel = 0; pixel < pixels2.length; pixel++) {
pixels2[pixel] = 0xFFFFFF;
}
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
final int centerx = width / 2;
final int centery = height / 2;
final int m = x - centerx;
final int n = y - centery;
final int j = ((int) ( m * cos + n * sin ) ) + centerx;
final int k = ((int) ( n * cos - m * sin ) ) + centery;
if( j >= 0 && j < width && k >= 0 && k < height ){
pixels2[ ( y * width + x ) ] = pixels[ ( k * width + j ) ];
}
}
}
return pixels2;
}
Context application:
try {
BufferedImage testrot = ImageIO.read(new File("./32x32.png"));
int[] linearpixels = new int[testrot.getWidth() * testrot.getHeight()];
int c = 0;
for(int i = 0; i < testrot.getWidth(); i++){
for(int j = 0; j < testrot.getHeight(); j++){
linearpixels[c] = testrot.getRGB(i, j);
c++;
}
}
int[] lintestrot = rotate(50, linearpixels, 32, 32);
BufferedImage image = new BufferedImage(70, 70, BufferedImage.TYPE_INT_RGB);
c = 0;
for(int i = 0; i < 32; i++){
for(int j = 0; j < 32; j++){
image.setRGB(i, j, lintestrot[c]);
c++;
}
}
File outputfile = new File("test002.bmp");
ImageIO.write(image, "bmp", outputfile);
} catch (IOException e1) {
e1.printStackTrace();
}
If you alter to 33 width or height the result will be wrong (wrong image).
You algorithm actually does work. The problem is with your loops in your context application. Because the pixels are stored in raster order, the outer loop needs to iterate to the height and the inner loop iterates to the width, e.g:
for(int i = 0; i < testrot.getHeight(); i++){
for(int j = 0; j < testrot.getWidth(); j++){
linearpixels[c] = testrot.getRGB(j, i); //edit here, tested
c++;
}
}
Then if you change height to 40 for example:
int[] lintestrot = rotate(50, linearpixels, 32, 40);
The loops need to change like this:
c = 0;
for(int i = 0; i < 40; i++){
for(int j = 0; j < 32; j++){
image.setRGB(i, j, lintestrot[c]);
c++;
}
}
Note that the order is reversed in the loops (height then width) compared to the function call (width then height).

Java Steganography coding or encoding bug

I'm writing an application to hide animage in another image using LSB. The encoding returns an image that differs from the image that was hidden and after searching the problem for quite some time now, i think i became seriously blind, when it comes to my code.
If somebody could take a look and give me a hint, I would be very thankful. The significant code below and the whole project (if somebody would want to test it) under the link: https://github.com/miassma/Steganography.git
public class SteganographyOperationsUtil {
/*checking if the image to hide can fit the hiding image
it returns the range of shades of gray that can be kept in the hiding image*/
public static int checkImages(ImageModel hiding, ImageModel toHide, ImageModel copyOfToHide){
int hidingSize = hiding.getWidth() * hiding.getHeight();
int toHideSize = toHide.getWidth() * toHide.getHeight();
int header = 40;
int value = 8;
while((toHideSize * value + header) > hidingSize){ //if doesnt fit, reducing one range, checking again
value--;
if(value==0){
break;
}
}
if(value == 0) return -1;
if(value<8) posterize(copyOfToHide, (int)pow(2, value)); //run the posterisation if needed
return (int)pow(2, value);
}
/* preparing the hiding image
we need zero on each LSB
*/
public static void prepareHidingImage(ImageModel imgModel){
for (int x = 0; x < imgModel.getWidth(); ++x) {
for (int y = 0; y < imgModel.getHeight(); ++y) {
int color = imgModel.getImage().getRaster().getPixel(x, y, new int[1])[0];
int temp = 254;
int newColor = color & temp;
int[] newColorPixel = {newColor};
imgModel.getImage().getRaster().setPixel(x, y, newColorPixel);
}
}
imgModel.imageChanged();
}
//fullfill by leading zeros with the lenght of the option
public static String fillString(String toFill, int option){
String zero = "0";
if (toFill.length() < option){
int temp = option - toFill.length();
do{
toFill = zero.concat(toFill);
}while(--temp >0);
}
return toFill;
}
/* fullfill the string to get the color matching the posterisation range
for example 1 will be 11111111 (2 ranges of gray)
101 will become 10110110 (3 ranges of gray)
1011 will become 10111011 (4 ranges of gray)
*/
public static String complete(String toComplete){
while (toComplete.length() < 9){
toComplete = toComplete+toComplete;
}
toComplete = toComplete.substring(0, 8);
return toComplete;
}
//hiding Image
public static void hidingOperation(ImageModel hiding, ImageModel toHide, int value){
prepareHidingImage(hiding);
String posterisation = Integer.toString(value-1, 2);
String hiddenWidth = Integer.toString(toHide.getWidth(), 2);
String hiddenHeight = Integer.toString(toHide.getHeight(), 2);
hiddenWidth = fillString(hiddenWidth, 16);
hiddenHeight = fillString(hiddenHeight, 16);
posterisation = fillString(posterisation, 8);
String header = hiddenWidth;
header = header.concat(hiddenHeight);
header = header.concat(posterisation);
int newColor;
int temp = 0;
int temp2 = 0;
int bitsToCheck = (int)logb(value, 2); //how many bits of each pixel we have to hide for given postarisation
int zero = 0;
int one = 1;
int i = 0;
int j = 0;
String colorOfToHideBinary = "";
outerLoop:
for (int x = 0; x < hiding.getWidth(); ++x) {
for (int y = 0; y < hiding.getHeight(); ++y) {
int color = hiding.getImage().getRaster().getPixel(x, y, new int[1])[0];
//filling header
if(temp < header.length()){
if(header.charAt(temp)== '0'){
newColor = color | zero;
}else{ newColor = color | one;
}temp++;
//hiding image
}else{
/*
getting the value of the next pixel of the image to hide, only if temp ==0,
what means that it is the first pixel or each needed bits by the posterisation range
has been already checked
*/
if(temp2 == 0){
int colorOfToHide = toHide.getImage().getRaster().getPixel(i, j, new int[1]) [0];
colorOfToHideBinary = Integer.toString(colorOfToHide, 2);
colorOfToHideBinary = fillString(colorOfToHideBinary, 8);
}
//i check each value of the color in binary, but only as much as needed by the posterisation range
if (colorOfToHideBinary.charAt(temp2) == '0'){
newColor = color | zero;
}else { newColor = color | one;}
temp2++;
if (temp2 == bitsToCheck){
temp2 = 0;
j++;
if(j == toHide.getHeight()){
j = 0;
i++;
}
}
}
int[] newColorPixel = {newColor};
hiding.getImage().getRaster().setPixel(x, y, newColorPixel);
if(i == toHide.getWidth()){
break outerLoop;
}
}
}
hiding.imageChanged();
}
//decrypting image
public static ImageModel encodingOperation(ImageModel imgModel){
int i = 0;
int j = 0;
String widthB = "";
String heightB = "";
String posterisationB = "";
int temp = 0;
int one = 1;
/* loop for taking values from the header, seems to work pretty fine */
outerLoop:
for (int x = 0; x < imgModel.getWidth(); ++x) {
for (int y = 0; y < imgModel.getHeight(); ++y) {
int color = imgModel.getImage().getRaster().getPixel(x, y, new int[1])[0];
if(temp<16){
if ((color & one) == one) widthB = widthB.concat("1");
else widthB = widthB.concat("0");
}else if(temp < 32){
if ((color & one) == one) heightB = heightB.concat("1");
else heightB = heightB.concat("0");
}else if(temp <40){
if ((color & one) == one) posterisationB = posterisationB.concat("1");
else posterisationB = posterisationB.concat("0");
}else{
break outerLoop;
}temp++; j++;
}i++;
}
int width = Integer.parseInt(widthB, 2);
int height = Integer.parseInt(heightB, 2);
int posterisation = Integer.parseInt(posterisationB, 2);
int bitsToCheck = (int)logb(posterisation+1, 2);
int temp2 = 0;
String colorInBinary = "";
//preparing the canvas for the encoded image, width and height from the header
ImageModel encryptedImage = ImageModel.fromHidden(width, height);
int a = 0;
int b = 0;
/* encoding the image
starting after the point after the header, saved in the variables i,j
*/
outerLoop:
for (int x = i; x < imgModel.getWidth(); ++x) {
for (int y = j; y < imgModel.getHeight(); ++y) {
int color = imgModel.getImage().getRaster().getPixel(x, y, new int[1])[0];
/* pixel by pixel reading color of the hidden image
temp2 checks, where to stop - how many LSB of the hiding image keeps information bout one pixel of the hidden img
*/
if ((color & one) == one) colorInBinary = colorInBinary.concat("1");
else colorInBinary= colorInBinary.concat("0");
temp2++;
if (temp2 == bitsToCheck){
temp2 = 0;
//fullfilling the color to the right by the given posterisation range
colorInBinary = complete(colorInBinary);
int newColor = Integer.parseInt(colorInBinary, 2);
colorInBinary = "";
int[] newColorPixel = {newColor};
encryptedImage.getImage().getRaster().setPixel(a, b, newColorPixel);
b++;
if(b == height){
a++;
b=0;
}if (a == width){
break outerLoop;
}
}
}
}
return encryptedImage;
}
public static double logb( double a, double b ){
return Math.log(a) / Math.log(b);
}
public static void posterize(ImageModel imgModel, int value) {
int[] lut = new int[256];
float param1 = 255.0f / (value - 1);
float param2 = 256.0f / (value);
for (int i = 0; i < 256; ++i) {
lut[i] = (int)((int)(i / param2) * param1);
}
useLUT(imgModel, lut);
}
public static void useLUT(ImageModel imgModel, int[] lut) {
for (int x = 0; x < imgModel.getWidth(); ++x) {
for (int y = 0; y < imgModel.getHeight(); ++y) {
int color = imgModel.getImage().getRaster().getPixel(x, y, new int[1])[0];
int[] newColorPixel = {lut[color]};
imgModel.getImage().getRaster().setPixel(x, y, newColorPixel);
}
}
imgModel.imageChanged();
}

Java mirror image diagonal method not working

I'm having trouble getting my method to work. The method should mirror any image I choose on its diagonal to produce a mirror effect, but at the moment it just produces the same image unedited and I don't what I'm doing wrong. Any help would be greatly appreciated. Thank you.
public Picture mirrorImageDiagonal() {
int size = this.getWidth();
Pixel rightPixel = null;
Pixel leftTargetPixel = null;
Pixel rightTargetPixel = null;
Picture target = new Picture(size, size);
for (double x = 0; x < size; x ++) {
for (double y = 0; y <= x; y ++) {
int yIndex = Math.min((int) y, this.getHeight() - 1);
int xIndex = Math.min((int) x, this.getWidth() - 1);
leftTargetPixel = target.getPixel(yIndex, xIndex);
rightTargetPixel = target.getPixel(xIndex, yIndex);
rightPixel = this.getPixel(xIndex, yIndex);
rightTargetPixel.setColor(rightPixel.getColor());
leftTargetPixel.setColor(rightPixel.getColor());
}
}
return target;
}
I am assuming that you are trying to complete the challenge for A6 in the picture lab packet. I just completed this for school, but if you are not, I hope this still helps you.
public void mirrorDiagonal()
{
Pixel[][] pixels = this.getPixels2D();
Pixel pixel1 = null;
Pixel pixel2 = null;
int width = pixels[0].length;
for (int row = 0; row < pixels.length; row++)
{
for (int col = 0; col < width; col++)
{
if (col < pixels.length)
{
pixel1 = pixels[row][col];
pixel2 = pixels[col][row];
pixel1.setColor(pixel2.getColor());
}
}
}
}

Tile based map scrolling

This script draws the map and other stuff:
public void render(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
Drawable myImage;
int tileWidth = 50;
int tileHeight = 50;
int mapWidth = 3;
int mapHeight = 3;
int rowBaseX = 0;
int rowBaseY = 0;
int[][] board = new int[][] {
{0,0,0},
{0,0,0},
{0,0,2}
};
for (int row = 0; row < mapHeight; row++)
{
for (int col = 0; col < mapWidth; col++)
{
Resources res = this.getContext().getResources();
switch(board[row][col])
{
case 0:
myImage = res.getDrawable(R.drawable.tile1);
break;
case 1:
myImage = res.getDrawable(R.drawable.tile2);
break;
default:
myImage = res.getDrawable(R.drawable.tile3);
break;
}
int curL;
int curU;
int curR;
int curD;
curL = rowBaseX + (col * tileWidth);
curU = rowBaseY + (row * tileHeight);
curR = curL + tileWidth;
curD = curU + tileHeight;
if (droid.x - decentreX < curR & droid.x + decentreX > curL) {
if (droid.y - decentreY < curD & droid.y + decentreY > curU) {
myImage.setBounds(curL,curU,curR,curD);
myImage.draw(canvas);
}
}
}
}
droid.draw(canvas);
butt.draw(canvas);
butt1.draw(canvas);
butt2.draw(canvas);
butt3.draw(canvas);
buttz.draw(canvas);
buttz1.draw(canvas);
buttz2.draw(canvas);
buttz3.draw(canvas);
buttx.draw(canvas);
}
The render(Canvas canvas) methos is called on every frame. How can i scroll the map tiles? I tried this:
public void render(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
Drawable myImage;
int tileWidth = 50;
int tileHeight = 50;
int mapWidth = 3;
int mapHeight = 3;
int rowBaseX = 0;
int rowBaseY = 0;
int[][] board = new int[][] {
{0,0,0},
{0,0,0},
{0,0,2}
};
for (int row = 0; row < mapHeight; row++)
{
for (int col = 0; col < mapWidth; col++)
{
Resources res = this.getContext().getResources();
switch(board[row][col])
{
case 0:
myImage = res.getDrawable(R.drawable.tile1);
break;
case 1:
myImage = res.getDrawable(R.drawable.tile2);
break;
default:
myImage = res.getDrawable(R.drawable.tile3);
break;
}
int curL;
int curU;
int curR;
int curD;
curL = rowBaseX + (col * tileWidth);
curU = rowBaseY + (row * tileHeight);
if (droid.touched & !droid.touched1 & !droid.touched3) {
curL -= 1;
}else if (droid.touched1 & !droid.touched & !droid.touched2){
curU += 1;
}else if (droid.touched2 & !droid.touched1 & !droid.touched3){
curL += 1;
}else if (droid.touched3 & !droid.touched2 & !droid.touched){
curU -= 1;
}else if (droid.touched & droid.touched1){
curL -= 1;
curU += 1;
}else if (droid.touched1 & droid.touched2){
curL += 1;
curU += 1;
}else if (droid.touched2 & droid.touched3){
curL += 1;
curU -= 1;
}else if (droid.touched3 & droid.touched){
curL -= 1;
curU -= 1;
}
curR = curL + tileWidth;
curD = curU + tileHeight;
if (droid.x - decentreX < curR & droid.x + decentreX > curL) {
if (droid.y - decentreY < curD & droid.y + decentreY > curU) {
myImage.setBounds(curL,curU,curR,curD);
myImage.draw(canvas);
}
}
}
}
droid.draw(canvas);
butt.draw(canvas);
butt1.draw(canvas);
butt2.draw(canvas);
butt3.draw(canvas);
buttz.draw(canvas);
buttz1.draw(canvas);
buttz2.draw(canvas);
buttz3.draw(canvas);
buttx.draw(canvas);
}
but it didn't worked. Everything that is not in this method is not important. Help me! :)
Seems that you are changing lcal vars curL and curU. Thore are recalculates from rowBase on each call restricting the movement to the 1-increment you use when checking droid.touched.
You should change the global position of the tiles by changing the rowBase variables.

Categories

Resources