Visual c ++ multimedia design and graphic and image processing

Source: Internet
Author: User
Graphic Processing
A Simple Method for displaying JPEG and GIF images under VC
I. Introduction
The JPEG image compression standard is a Lossy image compression standard. However, the image quality after compression is basically unchanged due to the insensitivity of human eyes, soon, it was widely recognized with a high compression rate. Although the GIF format only supports 256 colors, it has a high compression ratio for images with less colors and even exceeds the JPEG standard, which has been widely recognized. But as many Program Microsoft Visual C ++ 6.0, an important developer's development tool, only provides good support for BMP bitmap files that have not been compressed, you can read, display, store, or even create a memory bitmap in the memory. Because BMP images are not compressed, both as external files of the program and as internal resources of the program occupy a large amount of space, in particular, the latter will greatly increase the length of the executable file. It can be seen that if you can replace BMP files with JPEG or GIF images that have been compressed and have a good compression ratio in VC, it will undoubtedly be very attractive.
Ii. Design Ideas
Although there are some Active X controls for operations, processing JPEG, GIF, and other formats of images, it is not very convenient to use in general. I have explored through experiments, A simple method to implement the above functions by using the OLE method of the COM interface is summarized. The following is an introduction to the readers:
Now we want to use ipicture's COM interface. It is necessary to understand this image interface: this interface mainly manages image objects and their attributes, image objects provide a language-independent abstraction for bitmaps, icons, and elements. Like Standard font objects, the system also provides standard implementations for image objects. The main interfaces are ipicture and ipicturedisp. The latter is derived from the idispatch interface for automatic access to image attributes. Image objects also support the external interface ipropertypolicysink, so that users can make decisions when the image attributes change. The image object also supports the ipersiststream interface, so it can save and load itself from an instance object of an istream interface, and the istream interface also supports data reading and writing of the stream object.
We can use the oleloadpicture function to load images from a stream containing image data. This function simplifies the process of creating a stream-based image object. You can create a new image object and initialize it with the content in the stream. Its function prototype is:
Stdapi oleloadpicture (istream * pstream, // pointer to a stream containing image data long lsize, // number of bytes read from the stream bool frunmode, // The Initial Value refiid riid corresponding to the image attribute, // Interface ID involved, which describes the type of the interface pointer to be returned void ppvobj // the address of the interface pointer variable used in rrid );
Iii. Implementation
Before displaying the image, you must first obtain the storage path of the image file. Here, the standard file opening dialog box is used to select the image file. The file name is stored in the cstring variable m_spath:
Cfiledialog DLG (true, "jpg", "*. jpg ",
Ofn_hidereadonly | ofn_overwriteprompt,
"JPEG file (*. jpg) | *. jpg | GIF file (*. GIF) | *. gif |", null );
If (DLG. domodal () = idok)
{
M_spath = DLG. getpathname ();
Invalidate ();
}
For simple calculation, the graphic displayCodeWrite it directly in ondraw of the video class. First open the file and judge the file availability, and put the file content in the object PSTM of istream:
Istream * PSTM;
Cfilestatus fstatus;
Cfile file;
Long CB;
......
If (file. Open (m_path, cfile: moderead) & file. getstatus (m_path, fstatus) & (cb = fstatus. m_size )! =-1 ))
{
Hglobal = globalalloc (gmem_moveable, CB );
Lpvoid pvdata = NULL;
If (hglobal! = NULL)
{
If (pvdata = globallock (hglobal ))! = NULL)
{
File. readhuge (pvdata, CB );
Globalunlock (hglobal );
Createstreamonhglobal (hglobal, true, & PSTM );
}
}
}

Then, you can directly call the oleloadpicture function to load images from the stream:

Ipicture * PPIC;
......
Oleloadpicture (PSTM, fstatus. m_size, true, iid_ipicture, (lpvoid *) & PPIC ));

Because this function sometimes fails, you should use the succeeded macro to do some appropriate protection work. The following image display work can continue only when the data load is successful:

If (succeeded (oleloadpicture (PSTM, fstatus. m_size, true, iid_ipicture, (lpvoid *) & PPIC )))
{
Ole_xsize_himetric hmwidth;
Ole_ysize_himetric hmheight;
PPIC-> get_width (& hmwidth );
PPIC-> get_height (& hmheight );
Double FX, FY;
......
FX = (double) PDC-> getdevicecaps (horzres) * (double) hmwidth/(double) PDC-> getdevicecaps (horzsize) * 100.0 );
FY = (double) PDC-> getdevicecaps (vertres) * (double) hmheight/(double) PDC-> getdevicecaps (vertsize) * 100.0 );
If (failed (PPIC-> render (* PDC, 0, 0, (DWORD) FX, (DWORD) FY, 0, hmheight, hmwidth,-hmheight, null )))
Afxmessagebox ("rendering image failed! ");
PPIC-> release ();
}
Else
Afxmessagebox ("failed to load image from stream! ");

The display is mainly completed by the render function of the ipicture interface object, which is used to draw the specified part of the image to the specified location of the specified device environment. The prototype is as follows:

Hresult render (HDC, // device environment handle for rendering Images
Long X, // horizontal coordinates on HDC
Long y, // The vertical coordinates on HDC
Long CX, // Image Width
Long cy, // Image Height
Ole_xpos_himetric xsrc, // horizontal offset on the source Image
Ole_ypos_himetric ysrc, // vertical offset on the source Image
Ole_xsize_himetric cxsrc, // number of horizontal copies on the source Image
Ole_ysize_himetric cysrc, // number of vertical copies on the source Image
Lpcrect prcwbounds // pointer to the Environment handle of the target metactor );

Conclusion: by now, the above Code has been able to display JPEG, GIF, and other standard images in the customer area of the program, but for images with multiple frames (I .e., animated) currently, only the first frame can be displayed. To completely display the whole process of GIF animation, you also need the support of the external Active X control.

Drawing anywhere on the screen
You can use your program to draw pictures anywhere on the screen. The effect is as simple as that of an electronic pet. You only need two APIs.

The first one is getdomaintopwindow and the other is getwindowdc

The detail you can see the Win32 help which was plused in delphi4

For example: You paste a bitmap on the screen

VaR WND: longint;

Mydc, bitmapdc: HDC;

Mybitmap: tbitmap;

Mybitmap: = tbitmap. Create ();

Mybitmap. loadfromfile (c: \ windows \ waves.bmp );

Bitmapdc: = getwindowdc (mybitmap. Handle );

WND: = getdomaintopwindow ();

Mydc: = getwindowdc (WND );

Bitblt (my... // I forget the detail if you have any questions can

// Send email to me at BBS

Introducing the concept of turbulence to simulate marble textures
Abstract: In this paper, through the modeling of object surface textureAlgorithmIntroducing the concept of turbulence in fluid mechanics to successfully simulate the marble surface texture.

Preface

Computer simulation technology has been widely used in biology, medicine, national defense, and other scientific research and application fields. The simulation process is to extract the simulation object from an object into a mathematical model, therefore, refining the internal mathematical model and designing the corresponding simulation algorithm from the mathematical model are two key links throughout the simulation process. Taking marble as an example, a texture simulation algorithm with good simulation effect is proposed from the mathematical point of view by introducing the concept of turbulence in fluid mechanics, and the VC ++ coding of key parts is given.

Process texture Modeling Technology

The process texture modeling technology has similar functions as the two-dimensional texture management technology. However, because the two-dimensional texture management technology uses fixed images to describe the surface details, the surface textures of natural objects in the objective world are often irregular and random, therefore, this texture modeling technique, which uses a fixed image to describe the surface details, does not work well and cannot be used to shape and blur natural objects. The process texture modeling technology proposed in the mid-to-late 1980s s uses code segments or algorithms to encode and extract model details, and allows high-level control and standardization to fundamentally overcome the preceding defects of the Two-dimensional texture management technology. In addition, this technology manages the amplification of texture data through control parameters. Therefore, in the simulation process, you only need to set several control parameters to dynamically generate a large number of geometric details.

In 1985, the 3D texture ing method proposed by Peachey and Perlin defined the 3D texture function in the 3D texture space. The mathematical form is as follows:

T = T (x, y, z) (T: RGB value of color; (x, y, z): Three-dimensional Coordinate of spatial points ). Currently, there are two common methods to construct 3D textures:

A digital Texture Based on High-frequency sampling and a texture dynamically calculated using a mathematical model. Because the digital Texture Based on High-Frequency Sampling requires the support of 3D arrays, the space occupied by processing high-resolution textures will increase dramatically, and the space requirements are demanding. Some graphics synthesis systems with strong realism mostly use mathematical models to dynamically calculate texture definitions. After years of research and practice, we have developed and accumulated many process iteration functions to generate various complex textures. These process textures (procedural texture) functions have proved to be very effective, you can simulate the textures of many natural objects, such as wood, marble, clouds, flame, and slate. These functions are essentially an empirical model.

Introduce the concept of turbulence in algorithms

Turbulence is an important research object in fluid mechanics, but it is not prepared to discuss it from the precise physical model of turbulence, instead, an empirical model is proposed to describe the physical phenomenon. This technology was first proposed by Perlin in 1985 and was successfully used to simulate textures such as marble, flame, and clouds. This empirical model is composed of a series of three-dimensional noise functions. According to the concept of turbulence in fluid mechanics, a mathematical expression that can reflect the above empirical model can be obtained:

Turbulence (p) = Σ | noise (POW (2, I) * P)/(POW (2, I) |

P is the point (x, y, z) in the texture space ). The sum range is from 0 to K. The upper limit k is the smallest integer that satisfies the following inequality:
1/(POW (2, K + 1) <pixel length. By selecting the upper limit k of the sum that meets the preceding conditions, this can avoid the pattern when sampling the turbulence function. This function expression is any two adjacent items of the sum type. The variation frequency of the latter noise function is always twice the variation frequency of the former function, and the amplitude is half of the former, that is, the contribution rate to the turbulence function is halved. Therefore, the above constructor ensures that the turbulence function has a certain self-similarity. From the perspective of Signal Analysis, the power spectrum distribution satisfies the 1/f pattern. This function does not directly simulate textures. What we learn from is its randomness and self-correlation. These features can well describe irregular details of various natural textures during texture definition. Therefore, turbulence functions play an important role in texture modeling algorithms. The texture description process is roughly divided into two steps: first, a simple and appropriate function is selected to describe the basic texture structure features of natural objects such as marble, the descriptive function selected should be continuous and its first derivative should have great changes. After the description function is selected, the turbulence function is used to disturb some basic parameters of the description function to produce complex irregular texture details.

For the object of study in this article-Marble, we can note that the beautiful texture of marble is actually a reflection of the color of different internal materials. Therefore, we can define a function to describe this structure. Compared with ordinary stone, marble is characterized by periodic distribution of texture, which can be filtered by a sine wave function in a certain direction:

Marble (p) = marble_color (sin (x ))

P is still the space point (x, y, z), marble_color () is the color filter function, which maps any function value in the [-] range to the color value between RGB colors, it is usually expressed as three independent splines. After the turbulence is introduced in the second step of texture description, the above function becomes the final description function:

Marble (p) = marble_color (sin (x + turbulence (p )))

Algorithm Implementation

Based on the conclusions summarized above, the author finds that if the defined spline has a large first derivative at some points, the marble surface texture simulated by the spline has obvious sharp boundaries. In the following algorithm, the spline curve is a linear function (a spline), and the effect is moderate:

Colorref marble (float PX, float py, float PZ)
{
Float y = py + 3.0 * turbulence (PX, Py, PZ, 0.0125 );
Y = sin (y * M-PI );
Return (marble_color (PX ));
}

......

Colorref marble_color (float px );
{
Colorref Col;
Float x = SQRT (PX + 1.0) * 0.7071;
Int G = (INT) (0.30 + 0.8 * X );
X = SQRT (X );
Int r = (INT) (0.30 + 0.6 * X );
Int B = (INT) (0.60 + 0.4 * X );
Return Col;
}

The above two pieces of code can be used to simulate the marble texture. Like other textures, turbulence functions can also be used to express irregularities in various surface color attributes, such as concave and convex textures and transparency.

Summary

This paper introduces the concept of turbulence in fluid mechanics through the process texture modeling technology, and successfully simulates the marble surface texture. Through the realization of the computer simulation of marble texture, we can have a basic understanding of this simulation program. The turbulence function algorithm is also suitable for other simulation environments that require parameter disturbance processing. In Windows 98 SE, the program algorithm is compiled and debugged by Microsoft Visual C ++ 6.0.

Implement transparent desktop text background with VC to get rid of black and white

Module name: transparent. cpp
*
* Module description:
* To make desktop icon text background transparent.
*
* Project:
*
* Target Platform: Win32
*
* Compiler & Library: Visual C ++ 6.0
*
* Author: Richard Shen
*
* Creation date: 19 June, 1999
*
# Include <windows. h>

Int main (void)
{
Hwnd;

Hwnd = getdomaintopwindow ();
If (hwnd = find1_wex (hwnd, 0, "progman", "program manager") = 0)
Return 1;

If (hwnd = find1_wex (hwnd, 0, "shelldll_defview", null) = 0)
Return 1;

If (hwnd = find1_wex (hwnd, 0, "syslistview32", null) = 0)
Return 1;

// Change icon text attributes
Sendmessage (hwnd, 0x1026, 0, 0 xffffff); // turn background to transparent
Sendmessage (hwnd, 0x1024, 0, 0x00ffffff); // turn foregound to white

Invalidaterect (hwnd, null, true); // repaint

Return 0;
} // Main ()

Fill the background with Gradient
The graphic interface of Windows provides us with infinite convenience and visual pleasure. The light and deep colors give us endless fantasies. There are multiple methods to implement gradient color. Many documents have introduced the implementation of the color palette method. The process and its complexity require us to have a certain foundation for Graphic programming, next I will introduce you to a simple method, even if you do not know the concept of Graphic programming and palette at all.

Step 1: create a single document project. All parameters take the default value.

Step 2: Define the variables in shadowview. h as follows:

PRIVATE:
Int colorr;
Int colorg;

Step 3: Initialize the following variables in the shadowview. cpp constructor:

Cshadowview: cshadowview ()
{
// Todo: Add construction code here
Colorr = 255;
Colorg = 255;
}

Step 4: Add the following implementation code to ondraw:

Void cshadowview: ondraw (CDC * PDC)
{
Cshadowdoc * pdoc = getdocument ();
Assert_valid (pdoc );
// Todo: Add draw code for native data here
Crect m_rcclient;
File: // obtain the filled rectangle of the customer Region
Getclientrect (& m_rcclient );
Int nwidth = m_rcclient.width ();
Int nheight = m_rcclient.height ();
Crect rectangle;
File: // split the customer area into small rectangles and fill them one by one

For (INT I = 0; I <nwidth; I ++)
{
Rectangle. setrect (I, 0, I + 1, nheight );
PDC-> fillsolidrect (& rectangle, RGB (colorr, colorg, 255-muldiv (I, 255, nwidth )));
}
}

Now, compile and run the program. We can see that the background of the single document interface has been filled with yellow gradient. Next, we can click the left mouse button on the interface to change the background color.

Step 5: add the message processing ing function with the left mouse button in classwizard and add the following code:

Void cshadowview: onlbuttondown (uint nflags, cpoint point)
{
File: // generate a random number less than 255 and assign values to colorr and colorg
Int NRand = rand ();
Float fmap = (float) 255/rand_max;
Colorr = (uint) (float) NRand * fmap + 0.5f;
NRand = rand ();
Fmap = (float) 255/rand_max;
Colorg = (uint) (float) NRand * fmap + 0.5f;
File: // update Interface
Invalidate ();
Cview: onlbuttondown (nflags, point );
}

All the functions are implemented. Click the left mouse button on the interface and we can find that the background is filled with different gradient colors.

Related Article

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.