Click here to Skip to main content
13,295,243 members (54,447 online)
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 1 Oct 2001

Simple decoder/encoder for MNG animations

, 1 Oct 2001
Rate this:
Please Sign up or sign in to vote.
CxImageMNG: the module of CxImage used to play MNG animations.


CxImageMNG is the module of CxImage used to play MNG animations. It uses the CxImage members, but to play the animation, additional variables and functions are required. I decided to keep the MNG stuff separated from the "static" formats, to limit the growth of the base class due to the new features.

As with the other modules, CxImageMNG is mainly an interface for the LibMNG functions, it takes care of the I/O operations and of the specific requirements of the library. CxImageMNG can also read PNG and JNG images.

What do I need?

  1. LibMNG. You can find the latest version here.
  2. JPEG and ZLIB libraries. Used by LibMNG to endode/decode files.

All these libraries are included in the source code.

Structures and callbacks

"Libmng makes extensive use of callback functions. This is meant to keep the library as platform-independent and flexible as possible." In CxImageMNG, these are declared as:

mng_setcb_errorproc    (mng_handle, mymngerror);
mng_setcb_openstream   (mng_handle, mymngopenstream);
mng_setcb_closestream  (mng_handle, mymngclosestream);
mng_setcb_readdata     (mng_handle, mymngreadstream);
mng_setcb_processheader(mng_handle, mymngprocessheader);
mng_setcb_getcanvasline(mng_handle, mymnggetcanvasline);
mng_setcb_refresh      (mng_handle, mymngrefresh);
mng_setcb_gettickcount (mng_handle, mymnggetticks);
mng_setcb_settimer     (mng_handle, mymngsettimer);
mng_setcb_refresh      (mng_handle, mymngrefresh);

Some of these are empty (mymngopenstream), or obvious (mymngreadstream), the most useful for the final applications are mymngprocessheader, where we get the initial information about the image, and mymngsettimer, where we get the essential value for the timer.

Another important parameter is pUserdata, where the application can store its parameters; in CxImageMNG, this is a pointer to the mnginfo variable.

typedef struct
  FILE *file;        // file handle
  BYTE *image;       // image pixels
  HANDLE thread;     // timer
  mng_uint32 delay;  // timer duration
  mng_uint32 width;  // image attributes
  mng_uint32 height;
  mng_uint32 effwdt;
  mng_int16 bpp;
  mng_bool animation; // true if it's an animation
  mng_bool animation_enabled; // enable the timer
  float speed;       // speed factor
} mngstuff;

In the callbacks, this information is available at all times through the mng_get_userdata function.

How to play animations

First, we must load the file:

image = new CxImageMNG();

Then, if it's an animation, we must create a thread to play it:

if (image->mnginfo.animation) {
    DWORD dwID;
    image->mnginfo.thread = CreateThread(NULL,0,RunMNGThread,this,0,&dwID);

In the end, we must handle the execution of the thread:

unsigned long _stdcall RunMNGThread(void *lpParam){
    Sleep((DWORD)(image->mnginfo.delay / image->mnginfo.speed));
    while (image->mnginfo.animation_enabled && image->Resume()){
        SendMessage(hWnd, WM_USER_NEWFRAME,0,0);
        Sleep((DWORD)(image->mnginfo.delay / image->mnginfo.speed));
    return 0;

The Resume() function is used to obtain the next frame. To stop the animation, simply set image->mnginfo.animation_enabled = 0. The thread is automatically closed in the CxImageMNG destructor.


  • LIBMNG Copyright (c) 2000,2001 Gerard Juyn.
  • Original mng.cpp code created by Nikolaus Brennig, November 14th, 2000.
  • Special thanks to Frank Haug for suggestions and code.

More specific credits and disclaimers are in every header file of each library.


Win95, WinNT, Win98, WinME, W2K, WinXP = Yes.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

GeneralPNG animation Pin
serup28-Feb-09 9:10
memberserup28-Feb-09 9:10 
GeneralRe: PNG animation Pin
Davide Pizzolato1-Mar-09 7:51
memberDavide Pizzolato1-Mar-09 7:51 
QuestionRichiesta Pin
Nikitto7-Jan-08 4:43
memberNikitto7-Jan-08 4:43 
GeneralMPEG-2 parser Pin
hiravi19-Oct-04 12:29
memberhiravi19-Oct-04 12:29 
I would like to develop a program to parse the mpeg-2 (.mv2) file and integrate it with RTP protocal for to provide real time environment. I need a help like I would like to see the sample mpeg-2 file structure before develop a script. I don't know the mpeg-2 file look like. Please provide me the basic layout like how to extract the mpeg-2 file elementary stream and put into buffer and then parse to intergate with RFC 2250. Please ASAP response is more Appreciated or logic to extract the mpeg-2 Smile | :)

Thanks in advance

GeneralMNG transparency Pin
galleus27-Dec-03 1:22
membergalleus27-Dec-03 1:22 
GeneralRe: MNG transparency Pin
galleus27-Dec-03 9:35
membergalleus27-Dec-03 9:35 
GeneralUsing it for COM Pin
JDMoore5-Feb-02 0:14
memberJDMoore5-Feb-02 0:14 
GeneralRe: Using it for COM Pin
Davide Pizzolato5-Feb-02 0:42
memberDavide Pizzolato5-Feb-02 0:42 
GeneralRe: Using it for COM Pin
Davide Pizzolato29-May-02 2:20
memberDavide Pizzolato29-May-02 2:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 2 Oct 2001
Article Copyright 2001 by Davide Pizzolato
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid