Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Combining bmp's on the fly?

Greetings,

I have the following problem:

I have two bmp's of equal dimensions. I would like to combine part of one (depending on an equation involving time) with the opposite part of the second bitmap to produce a third.

I would like to create a single bitmap for this process so I could proceed to use it as a texture for a 3d object.

This process would need to be repeated roughly every ten minutes.

Any ideas/code snippets would be greatly appreciated.

Thanks in advance.
0
setaglibb
Asked:
setaglibb
  • 5
  • 3
  • 2
1 Solution
 
Sasha_MapaCommented:
What do you mean by bitmap? Do you have 2 Image objects and you want to combine them to create another one? If so, here's how you do it:


public static Image combineImages(Image img1, Image img2){
  Canvas c = new Canvas();
  Image newImage = c.createImage(img1.getWidth(null)*2,img1.getHeight(null));
  Graphics g = newImage.getGraphics();
  g.drawImage(img1,0,0,null);
  g.drawImage(img2,newImage.getWidth(null)/2,0,null);
  return newImage;
}

This will make the new Image the combination of the 2 given images, one next to the other.
If you want to draw only a part of an image use the Graphics.clipRect(x,y,width,height) to set the clipping rectangle before you draw...

If you have any questions or wish to clarify what you want, please post a comment.

Hope this helps, Sasha.
0
 
setaglibbAuthor Commented:
To clarify:

I have two bitmaps (of equal dimensions) stored on my hard drive that I want to access during runtime and combine parts of them to produce a single image.

I would also like to save the combined image in the bmp format (if possible) on the fly.

The area to be replaced will be bounded by a sin wave so I don't think cliprect will help.

I am new to this image processing stuff so I'm not even sure how to access/store them in my program.

By what you have told me so far I assume they are stored internally as image objects?

Or do I have to use more advanced stuff like bufferedimage, bitmapinfoheaders, and copyoffscreenregion?

Hope this makes more sense now.
0
 
Sasha_MapaCommented:
>> Hope this makes more sense now.
Yeah, it does :-)

Ok, since the file is in bmp format, the easiest thing to do will be to open the file as regular binary file, read the contents into a matrix that will represent the image and perform all operations on that matrix. It would be very easy to copy portions of the matirx into another matrix, even if it involves complex functions... you just copy parts of an array by some rule. After you are done, again, if this was another format, it would have been more difficult, but since it's bmp, you can just write the file as binary on the hard disk...

Can you do that yourself or do you need examples in file I/O and copying matrices?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
setaglibbAuthor Commented:
That's a big help but I would appreciate some file I/O and copying matrices examples if you don't mind.

Thanks for your time.
0
 
Sasha_MapaCommented:
Umm, ok, well, here's a method that reads an array of bytes of given length from a given file:

public static byte[] readArrayFromFile(String fileName, int length) throws IOException, FileNotFoundException{
  InputStream input = new FileInputStream(fileName);
  byte [] byteArray = new byte[length];
  input.read(byteArray);
  input.close();
  return byteArray;
}

Here's a method that reads one byte from a given file:

public static byte readByteFromFile(String fileName) throws IOException, FileNotFoundException{
  InputStream input = new FileInputStream(fileName);
  byte readByte = (byte)input.read();
  input.close();
  return readByte;
}

I am not sure what exactly is the bmp format, but I suppose its 2 numbers specifying width and height of the image and then the all the pixel colors, row by row. You should find the exact specification somewhere on the web.

Well, copying matrices is pretty straightforward, this method will take 2 matrices of same dimensions and return a new one which will be the combination of the given ones in such a way that the upper left triangle half of the 1st matrix will be combined with the lower right triangle half of the 2nd matrix:


public static int[][] combineMatrices(int [][] matrix1, int [][] matrix2){
  if ((matrix1.length!=matrix2.length)||(matrix1[0].length!=matrix2[0].length))
    throw IllegalArgumentException;
  int width = matrix1.length;
  int height = matrix1[0].length;
  int [][] newMatrix = new int[width][height];
  for (int i=0;i<width;i++)
    for (int j=0;j<height;j++){
      if (j*width+i*height<=width*height)
        newMatrix[i][j] = matrix1[i][j];
      else
        newMatrix[i][j] = matrix2[i][j];
    }
  return newMatrix;
}


You may also use the System.arraycopy method to imporove efficiency when copying arrays.

I didn't compile or test this code, so there may be some small bugs, but the idea is correct.


Good luck, Sasha :-)
0
 
Sasha_MapaCommented:
Umm, ok, so did my suggestion help you?
0
 
Sasha_MapaCommented:
Posting an answer based on the fact that I answered the question.
0
 
darinwCommented:
Reviewing question.

darinw
Customer Service
0
 
darinwCommented:
Answer accepted
0
 
darinwCommented:
Moving question to the PAQ.

darinw
Customer Service
 
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now