Use Visual C ++ to achieve image fade-out and fade-out

Source: Internet
Author: User

Use Visual C ++ to achieve image fade-out and fade-out

Long weekly hair


Abstract

It is widely used in image processing and multi-media entertainment. Based on the color palette animation and time code technology of windows, this paper designs a general algorithm for image fade-in and fade-out, and implements its Visual C ++ program coding.

Keywords

Fade-in, fade-in, palette, palette animation, Time Code

Gradual display of images

/Gradient is a very important image effect and is widely used in image processing and multi-media entertainment software. The biggest difficulty in the design of the gradient/gradient algorithm is speed control, including timing and fast changing the color of each pixel in the image. If you use a common full Graph Scanning algorithm, the speed is slow, and it is difficult to reflect the effect of gradual display/fading.

Exploitation

The special color palette management and Time Code timing mechanisms of Windows (3. x.95/98/NT) operating systems can design effective image gradient/gradient algorithms. Windows provides a color processing technique called the palette animation, which simulates color changes by rapidly changing the colors in the selected table items in the color palette. Set the time code and call this technique regularly to enable gradient of the image to achieve gradient and gradient of the image.

I. palette Animation

In

The implementation of palette animation in Visual C ++ relies on several member functions in the cpalette class and CDC class provided by the MFC class library. The basic steps are as follows:

  1. Call

Cpalette: createpalette (lplogpalette) function to create a logical palette. Note that the peflags field of the item structure pointed to by the lplogpalette parameter is set to pc_reserved, to prevent other windows from matching the color of the color palette .;

  • Call
  • The CDC: selectpalette and CDC: realizepalette functions select and implement the created logical palette;

  • Call
  • Cpalette: The animation function of the animation palette to change the color;

  • After the animation is completed, the system palette should be restored.
  • Cpalette: animatepalette

    Is the most critical function. Its prototype is as follows:

    Void animatepalette (

    Uint nstartindex ,//

    Start table item number

    Uint nnumentries ,//

    Number of changed table items

    Lppaletteentry lppalettecolors );//

    Logical palette table item pointer

    Lppalettecolors

    A pointer to the paletteentry structure, which stores the color information to be updated in the logical palette. The structure of the paletteentry is defined as follows:

    Typedef struct tagpaletteentry {// PE

    Byte pered;

    Byte pegreen;

    Byte peblue;

    Byte peflags;

    } Paletteentry;

    Pered

    , Pegreen, and peblue respectively represent the R, G, and B color component values of the logical palette items. Peflags should be set to pc_reserved.

    Nstartindex

    It is the starting table item number in lppalettecolors, and nnumentries is the number of table items in lppalettecolors.

    Ii. Timing of time codes

    Cwnd: settimer

    The function can set a system time code and specify that a wm_timer message is sent to the window message queue every time interval. Each time a window receives a corresponding wm_timer message, it performs scheduled processing.

    Generally, it should be accepted and processed in the message loop of the window.

    Wm_timer message, which makes it difficult to compile general scheduled operations. Common scheduled operations should encapsulate scheduled processing in a function, without being entangled with other code. The technique implemented by the author is to intercept window messages in cyclic operations. If a message is a specified time code message, it will be periodically processed; otherwise, messages will be sent to the window message processing mechanism. If the scheduled operation has ended, modify the cycle flag and exit the loop. The specific code is as follows:

    ....................................

    // Set the time code. pwnd is the window object pointer for processing scheduled operations.

    Pwnd-> settimer (0x100, utimeout, null );

    //

    Shields mouse operations, so that scheduled operations are not affected

    Pwnd-> setcapture ();

    //

    Start scheduled operation

    Bool bdone = false;

    MSG;

    While (! Bdone)

    {

    If (: peekmessage (& MSG, null, 0, 0, pm_remove ))

    {

    If (msg. Message = wm_timer & msg. wparam = 0 x100)

    {

    .......................

    Scheduled operation code

    .......................

    // If the scheduled operation is completed, set the cycle flag to end the operation.

    If (scheduled operation completed)

    Bdone = true;

    }

    : Translatemessage (& MSG );

    : Dispatchmessage (& MSG );

    }

    }

    // Release the mouse

    : Releasecapture ();

    //

    Delete Time Code

    Pwnd-> killtimer (0x100 );

    ................................

    Function

    Peekmessage intercepts window messages. The translatemessage and dispatchmessage functions explain and distribute all messages except the specified time code to avoid message loss.

    Iii. incremental display

    Fade is to show the color from Black (

    RGB (0, 0, 0) Gradually changes to the color of each pixel of the image. At the beginning, call the cpalette: getpaletteentries function to save the information of each logical table in the image palette, and then call cpalette :: the setpaletteentries function sets pered, pegreen, and peblue of each logical table item in the logical palette to 0, and regularly calls cpalette: animatepalette, add the pered, pegreen, and peblue values of each logical table item each time until they are equal to the pered, pegreen, and peblue values of each logical table item in the image logical palette.

    The following functions

    Fadein sets each color component value in the color palette item to 0 first, and then increments until all the color values are restored to the color value in the original color palette.

    //

    Image gradient effect

    //

    Parameters:

    // Pwnd

    -Display the image window

    // Ppal

    -Palette pointer

    // Ndeta

    -Decrease of each color component

    // Utimeout

    -Time Variation

    Void fadein (cwnd * pwnd, cpalette * ppal, int ndeta, uint utimeout)

    {

    //

    Retain the color table items of the original palette

    Int ntotalcolors = ppal-> getentrycount ();

    Paletteentry palettecolors0 [2, 256];

    Ppal-> getpaletteentries (0, ntotalcolors, palettecolors0 );

    // First, set the color components in the palette table to 0.

    Paletteentry palettecolors1 [256];

    For (INT I = 0; I <ntotalcolors; ++ I)

    {

    Palettecolors1 [I]. pered = 0;

    Palettecolors1 [I]. pegreen = 0;

    Palettecolors1 [I]. peblue = 0;

    Palettecolors1 [I]. peflags = pc_reserved;

    }

    Ppal-> setpaletteentries (0, ntotalcolors, palettecolors1 );

    Ppal-> animatepalette (0, ntotalcolors, palettecolors1 );

    // Set the time code

    Pwnd-> settimer (0x100, utimeout, null );

    // Start to fade

    Pwnd-> setcapture ();

    Bool bdone = false;

    MSG;

    While (! Bdone)

    {

    If (: peekmessage (& MSG, null, 0, 0, pm_remove ))

    {

    If (msg. Message = wm_timer & msg. wparam = 0 x100)

    {

    Cclientdc DC (pwnd );

    Cpalette * poldpal = Dc. selectpalette (ppal, false );

    DC. realizepalette ();

    // Increment each color component

    Paletteentry palettecolors [2, 256];

    Ppal-> getpaletteentries (0, ntotalcolors, palettecolors );

    Bool bredzero = false

    ;

    Bool bgreenzero = false;

    Bool bbluezero = false;

    For (INT I = 0; I <ntotalcolors; ++ I)

    {

    If (palettecolors [I]. pered + ndeta <

    Palettecolors0 [I]. pered)

    {

    Palettecolors [I]. pered + = ndeta;

    Bredzero = false;

    }

    Else if (palettecolors [I]. pered + 1 <

    Palettecolors0 [I]. pered)

    {

    Palettecolors [I]. pered ++;

    Bredzero = false;

    }

    Else

    Bredzero = true;

    If (palettecolors [I]. pegreen + ndeta <

    Palettecolors0 [I]. pegreen)

    {

    Palettecolors [I]. pegreen + = ndeta;

    Bgreenzero = false;

    }

    Else if (palettecolors [I]. pegreen + 1 <

    Palettecolors0 [I]. pegreen)

    {

    Palettecolors [I]. pegreen ++;

    Bgreenzero = false;

    }

    Else

    Bgreenzero = true;

    If (palettecolors [I]. peblue + ndeta <

    Palettecolors0 [I]. peblue)

    {

    Palettecolors [I]. peblue + = ndeta;

    Bbluezero = false;

    }

    Else if (palettecolors [I]. peblue + 1 <

    Palettecolors0 [I]. peblue)

    {

    Palettecolors [I]. peblue ++;

    Bbluezero = false;

    }

    Else

    Bbluezero = true;

    }

    //

    Until the original recovery value ends

    Bdone = bredzero & bgreenzero & bbluezero;

    //

    Change the system palette

    Ppal-> animatepalette (0, ntotalcolors, palettecolors );

    }

    : Translatemessage (& MSG );

    : Dispatchmessage (& MSG );

    }

    }

    : Releasecapture ();

    Pwnd-> killtimer (0x100 );

    // Restore the original palette

    Ppal-> setpaletteentries (0, ntotalcolors, palettecolors0 );

    Ppal-> animatepalette (0, ntotalcolors, palettecolors0 );

    }

    Iv. Gradient

    The gradient is to gradually change the display color from the color of each pixel of the image to Black (

    RGB (0, 0, 0), that is, the cpalette: animatepalette is called regularly. Each time, the pered, pegreen, and peblue values of each logical table item are reduced by one change, until they are all 0.

    The following functions

    Fadeout fades in by decreasing the values of each color component in the color palette item until all the color values change to 0 (that is, black.

    //

    Image fading effect

    //

    Parameters:

    // Pwnd

    -Display the image window

    // Ppal

    -Palette pointer

    // Ndeta

    -Decrease of each color component

    // Utimeout

    -Time Variation

    Void fadeout (cwnd * pwnd, cpalette * ppal, int ndeta, uint utimeout)

    {

    //

    Retain the color table items of the original palette

    Int ntotalcolors = ppal-> getentrycount ();

    Paletteentry palettecolors0 [2, 256];

    Ppal-> getpaletteentries (0, ntotalcolors, palettecolors0 );

    // Set the time code

    Pwnd-> settimer (0x100, utimeout, null );

    //

    Start to fade

    Pwnd-> setcapture ();

    Bool bdone = false;

    MSG;

    While (! Bdone)

    {

    If (: peekmessage (& MSG, null, 0, 0, pm_remove ))

    {

    If (msg. Message = wm_timer & msg. wparam = 0 x100)

    {

    Cclientdc DC (pwnd );

    Cpalette * poldpal = Dc. selectpalette (ppal, false );

    DC. realizepalette ();

    Paletteentry palettecolors [2, 256];

    Ppal-> getpaletteentries (0, ntotalcolors, palettecolors );

    Bool bredzero = false

    ;

    Bool bgreenzero = false;

    Bool bbluezero = false;

    // Decrease the color weight

    For (INT I = 0; I <ntotalcolors; ++ I)

    {

    If (palettecolors [I]. pered> ndeta)

    {

    Palettecolors [I]. pered-= ndeta;

    Bredzero = false;

    }

    Else if (palettecolors [I]. pered> 1)

    {

    Palettecolors [I]. pered --;

    Bredzero = false;

    }

    Else

    Bredzero = true;

    If (palettecolors [I]. pegreen> ndeta)

    {

    Palettecolors [I]. pegreen-= ndeta;

    Bgreenzero = false;

    }

    Else if (palettecolors [I]. pegreen> 1)

    {

    Palettecolors [I]. pegreen --;

    Bgreenzero = false;

    }

    Else

    Bgreenzero = true;

    If (palettecolors [I]. peblue> ndeta)

    {

    Palettecolors [I]. peblue-= ndeta;

    Bbluezero = false;

    }

    Else if (palettecolors [I]. peblue> 1)

    {

    Palettecolors [I]. peblue --;

    Bbluezero = false;

    }

    Else

    Bbluezero = true;

    }

    //

    If all the color components are 0, the end fades

    Bdone = bredzero & bgreenzero & bbluezero;

    //

    Change the system palette

    Ppal-> animatepalette (0, ntotalcolors, palettecolors );

    }

    : Translatemessage (& MSG );

    : Dispatchmessage (& MSG );

    }

    }

    : Releasecapture ();

    Pwnd-> killtimer (0x100 );

    // Restore the original palette

    Ppal-> setpaletteentries (0, ntotalcolors, palettecolors0 );

    Ppal-> animatepalette (0, ntotalcolors, palettecolors0 );

    }

    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.