Solved

Combining bmp's on the fly?

Posted on 2000-05-11
10
220 Views
Last Modified: 2010-04-01
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
Comment
Question by:setaglibb
  • 5
  • 3
  • 2
10 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2802095
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
 

Author Comment

by:setaglibb
ID: 2802152
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2804083
>> 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
 

Author Comment

by:setaglibb
ID: 2804834
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2805171
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2810087
Umm, ok, so did my suggestion help you?
0
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 100 total points
ID: 2853387
Posting an answer based on the fact that I answered the question.
0
 
LVL 3

Expert Comment

by:darinw
ID: 2945771
Reviewing question.

darinw
Customer Service
0
 
LVL 3

Expert Comment

by:darinw
ID: 3090801
Answer accepted
0
 
LVL 3

Expert Comment

by:darinw
ID: 3090802
Moving question to the PAQ.

darinw
Customer Service
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now