How to multiply an image by another image?

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! :slight_smile:

r2 = r0 * r1;
g2 = g0 * g1;
b2 = b0 * b1;
a2 = a0 * a1;

Red book
Chapter 8 : Drawing pixels , Bitmaps and fonts and images.

Refer to the topic “Convolutions”.

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:)

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 :slight_smile:

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!

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

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 :slight_smile:

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!

Anyone else can help me?
Thanks a lot.

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!

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.

Thank you. I’m new to OpenGL, but I’ll try.
Thanks again:)

don’t you have the redbook?

http://www.parallab.uib.no/SGI_bookshelv…html#id5503024

got to have one!

:slight_smile:

heres some tutorials on texture mapping:

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

all kinds of tasty bits out there!

:slight_smile: