PDA

View Full Version : How to multiply an image by another image?



ChinaShrimp
09-01-2004, 06:09 PM
Hi,
now I want to multiply an image by another image using opengl? Anyone can tell me how to implement it?
Thanks for any suggestions! :)

ioquan
09-01-2004, 07:15 PM
r2 = r0 * r1;
g2 = g0 * g1;
b2 = b0 * b1;
a2 = a0 * a1;

Only Sophoo...
09-01-2004, 08:33 PM
Red book
Chapter 8 : Drawing pixels , Bitmaps and fonts and images.

Refer to the topic "Convolutions".

ChinaShrimp
09-01-2004, 11:15 PM
Sorry, I don't think so.
For example, I have two images: Img1[256][256] and Img2[256][256]. Now I allocate a block of memory result[256][256] to save the result just like the following C code:


for(int i = 0; i < 255; i++)
{
for(int j = 0; j < 255; j++)
{
result[i][j] = Img1[i][j]*Img[i][j];
}
}Now I want to use Opengl to realize the multiply of two images:)

Mazy
09-01-2004, 11:35 PM
Simplest way to use opengl for this is to

1. draw the first image as a texture on a quad.
2. enable blend with the func (GL_DST_COLOR,GL_ZERO)
3. draw the next image
4. turn off blend again( if you want to make the operation more than once)
5. fetch the result with glReadPixels

This only works if everything is viewed on the screen. for invisible useage you need to use p-buffers which is a rather unpleasant trip :)

09-02-2004, 05:02 AM
What I do now is read the values from the framebuffer using glReadPixels and implement the multiply of two images by CPU. I'll do the steps mentioned above several times, so the frequent callings of glReadPixels becomes the time bottleneck. So I want to do it using Opengl. Just now I did just like what Mazy said and found it becomes slower :confused: Are there anyother better methods?
Thank you!

-NiCo-
09-02-2004, 05:39 AM
This is very easy to implement in a fragment program.

If you want to use the fixed function pipeline, take a look at
http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_env_combine.txt

Use GL_MODULATE with the two textures as input to multiply the two images.

Greetz,

Nico

Aeluned
09-02-2004, 05:53 AM
I'm not sure I understand the problem but can't you do this?

Texture map the first image onto a quad that's the size of the image using GL_DECAL. At this point you would have the original image.
Use multi texturing to map the other image onto the same quad using GL_MODULATE. what you end up with is the two images multiplied together.

if you need to store it somewhere after that you can do a read pixels cropped using the quad size.

I might be grossly misunderstanding the problem or what you want to achieve but this seems pretty straight forward to me.

EDIT: looks like Nico and I posted simultaneously.
we're both saying the same thing :)

09-02-2004, 06:40 PM
Hi, NiCo
I really don't know how to use the ARB_texture_env_combine, can you give me an URL where I can find how to use it. Thank you in advance!

ChinaShrimp
09-03-2004, 11:15 PM
Anyone else can help me?
Thanks a lot.

09-04-2004, 02:16 AM
have you read the spec that Nico posted? it descibes the workings in great detail. you can learn about any extension in the same way! its beautiful!

09-04-2004, 02:23 AM
and Aeluned's advice on modulate is good, thats the default tex env mode. just load your textures into units 0 and 1, set the tex env mode to GL_MODULATE (the default), and draw. thats how light-mapping is done, for example, just a modulation of the base diffuse texture with a lightmap.

read the replies carefully, else youll miss the gems.

ChinaShrimp
09-04-2004, 11:40 PM
Thank you. I'm new to OpenGL, but I'll try.
Thanks again:)

09-04-2004, 11:56 PM
don't you have the redbook?

http://www.parallab.uib.no/SGI_bookshelv....html#id5503024 (http://www.parallab.uib.no/SGI_bookshelves/SGI_Developer/books/OpenGL_PG/sgi_html/ch10.html#id5503024)

got to have one!

:)

09-05-2004, 12:32 AM
heres some tutorials on texture mapping:

http://www.gametutorials.com/Tutorials/opengl/OpenGL_Pg2.htm#TextureMapping

all kinds of tasty bits out there!

:)