Solved

Combining bmp's on the fly?

Posted on 2000-05-11
10
219 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 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

12 Experts available now in Live!

Get 1:1 Help Now