5. Image Processing for videos using VC + DirectShow

Source: Internet
Author: User
Apply to d3d

We may wish to go to the 3D environment to see more images and videos.

I won't introduce d3d here. If you want to learn about it, you have to find your own materials. Here we only talk about the key to playing in a 3D environment-picture to texture.

3D textures have one feature: the width and height must be multiples of 2. As you know, the image size is usually 320*240, and the width and height are passed in to create a texture of 512*256. Therefore, you have to stretch the image to the texture. Otherwise, the texture you get will be black, not beautiful. Although my aesthetic ability makes me not think that the Golden Section is very beautiful, but I'm sure the texture is ugly. In my program, I used the closest point method to stretch the image to the texture size. However, this also results in image aspect ratio changes, resulting in a certain degree of distortion. You can write a program to keep the image Aspect Ratio stretched. See how to dynamically change the texture.

Hresult d3d: settex (byte * pb)
{
If (! Ptexture) return e_fail; // texture creation failed
If (! PB) return e_fail; // Pointer Error
// Lock the texture
D3dlocked_rect d3dlr;
If (failed (ptexture-> lockrect (0, & d3dlr, 0, 0 )))
Return e_fail;
Byte * ptexbits = (byte *) d3dlr. pbits; // get the texture data area pointer
Uint texpitch = d3dlr. Pitch; // specifies the texture pitch.
Uint BMP pitch = bmp wid * 4; // the pitch of the image
Float xstep = float (BMP wid-1)/float (texwid-1 );//
Float ystep = float (bmphei-1)/float (texhei-1 );//
Byte * pnewbits = ptexbits;
Byte * poldbits = Pb;
Byte * pnewpixel;
Byte * poldpixel;
// Zoom in at the nearest point
For (INT y = 0; y <texhei; y ++ ){
Poldbits = Pb + int (ystep * y) * BMP pitch; // locate y
Pnewbits = ptexbits + y * texpitch;
For (INT x = 0; x <texwid; X ++ ){
Ppixel = poldbits + 4 * int (xstep * X); // locate x
Pnewpixel = pnewbits + 4 * X;
Pnewpixel [0] = poldpixel [0];
Pnewpixel [1] = poldpixel [1];
Pnewpixel [2] = poldpixel [2];
Pnewpixel [3] = 255; // The Alpha value of the texture. If transparency is enabled, you can change the transparency.
}
}
// Unlock texture
If (failed (ptexture-> unlockrect (0 )))
Return e_fail;
Return s_ OK;
}

Among them, ptexture is the texture set for the image, and other unknown variables are also members of the d3d class, which are assigned values in the following function.

Hresult d3d: createtex (int wid, int HEI)
{
// Create a texture based on the input width and height
If (failed (d3dxcreatetexture (this-> m_pd3ddevice, WID, Hei, 1, 0, d3dfmt_a8r8g8b8, d3dpool_managed, & ptexture ))){
Return e_fail;
}
// Texture description
D3dsurface_desc ddsd;
If (failed (ptexture-> getleveldesc (0, & ddsd ))){
Return e_fail;
}
// Check the texture format, in the 32bit argb format of a8r8g8b8
If (ddsd. Format! = D3dfmt_a8r8g8b8 ){
Ptexture-> release ();
Ptexture = NULL;
Return e_fail;
}
Texwid = ddsd. width; // texture width
Texhei = ddsd. height; // texture height
Bmp wid = WID; // Image Width
Bmphei = Hei; // picture height
Return s_ OK;
}

I don't want to create a new texture every time I use it. It will be released after each rendering, which affects performance. So I should first create a texture after I press the play button, then, the texture is updated when a new image arrives. I use direct3d8 instead of Direct3D9, because I use gf4 (I really want it to disappear, but it is empty in the box) to run d3d9 very slowly. In simple scenarios, it is enough to use d3d8. Additionally, a wonderful scenario can increase the video playback color.

In addition, I have tried to use the film texture in OpenGL, but my method makes the CPU usage rate reach 100%, but one thing is certain, this can also be applied to OpenGL.

Warning: if you are interested in reading my program, please do not try to learn direct3d through my d3d class, it is just the framework class I set up to learn d3d some time ago, there are a lot of irregularities, and 3D objects are even more messy. The code written is temporary and readability is not considered at all. Now I have a headache and don't want to fix it. So in order not to give you a bad impression on d3d, but also to keep you enthusiastic about exploring the 3D world, please refer to the data system to learn about d3d. For my programs, you only need to know where I used d3d functions to achieve the effect. Remember!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.