This site is 100% GIF free! Powered by libmng!
libmng - The MNG reference library & related info


Example 3 - Create & write a MNG

Below you'll find a rather bogus example on how to create a MNG.

Most of the callbacks are the same as in the previous example. The only difference is that the readdata callback has been replaced with writedata and there's no callback for iterating chunks! Here's the definition of the very complex write-logic.

mng_bool mywritedata (mng_handle hMNG,
                      mng_ptr    pBuf,
                      mng_uint32 iSize,
                      mng_uint32 *iWritten)
{                                      /* get to my file handle */
  userdatap pMydata = (userdatap)mng_get_userdata (hMNG);
                                       /* write it */
  *iWritten = fwrite (pBuf, 1, iSize, pMydata->hFile);
                                       /* iWritten will indicate errors */
  return MNG_TRUE;
}

Again we use the chunk access functions to build the new MNG in memory. The code below speaks pretty much for itself. To figure out what should go into a MNG, I strongly urge you to read the MNG specification. This sample is in no way a substitute.

int makeimage (char * zFilename)
{
  userdatap pMydata;
  mng_handle hMNG;
  mng_retcode iRC;
                                       /* get a data buffer */
  pMydata = (userdatap)calloc (1, sizeof (userdata));

  if (pMydata == NULL)                 /* oke ? */
  {
    fprintf (stderr, "Cannot allocate a data buffer.\n");
    return 1;
  }
                                       /* can we open the file ? */
  if ((pMydata->hFile = fopen (zFilename, "wb")) == NULL)
  {                                    /* error out if we can't */
    fprintf (stderr, "Cannot open output file %s.\n", zFilename);
    return 1;
  }
                                       /* let's initialize the library */
  hMNG = mng_initialize ((mng_ptr)pMydata, myalloc, myfree, MNG_NULL);

  if (!hMNG)                           /* did that work out ? */
  {
    fprintf (stderr, "Cannot initialize libmng.\n");
    iRC = 1;
  }
  else
  {                                    /* setup callbacks */
    if ( ((iRC = mng_setcb_openstream  (hMNG, myopenstream )) != 0) ||
         ((iRC = mng_setcb_closestream (hMNG, myclosestream)) != 0) ||
         ((iRC = mng_setcb_writedata   (hMNG, mywritedata  )) != 0)    )
      fprintf (stderr, "Cannot set callbacks for libmng.\n");
    else
    {                                  /* create the file in memory */
      if ( ((iRC = mng_create        (hMNG)                                                    ) != 0) ||
           ((iRC = mng_putchunk_mhdr (hMNG, 640, 480, 1000, 3, 1, 3, 0x0007)                   ) != 0) ||
           ((iRC = mng_putchunk_basi (hMNG, 640, 160, 8, 2, 0, 0, 0, 0xFF, 0x00, 0x00, 0xFF, 1)) != 0) ||
           ((iRC = mng_putchunk_iend (hMNG)                                                    ) != 0) ||
           ((iRC = mng_putchunk_defi (hMNG, 0, 0, 0, MNG_TRUE, 0, 160, MNG_FALSE, 0, 0, 0, 0  )) != 0) ||
           ((iRC = mng_putchunk_basi (hMNG, 640, 160, 8, 2, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 1)) != 0) ||
           ((iRC = mng_putchunk_iend (hMNG)                                                    ) != 0) ||
           ((iRC = mng_putchunk_defi (hMNG, 0, 0, 0, MNG_TRUE, 0, 320, MNG_FALSE, 0, 0, 0, 0  )) != 0) ||
           ((iRC = mng_putchunk_basi (hMNG, 640, 160, 8, 2, 0, 0, 0, 0x00, 0x00, 0xFF, 0xFF, 1)) != 0) ||
           ((iRC = mng_putchunk_iend (hMNG)                                                    ) != 0) ||
           ((iRC = mng_putchunk_mend (hMNG)                                                    ) != 0)    )
        fprintf (stderr, "Cannot create the chunks for the image.\n");
      else
      {
        if ((iRC = mng_write (hMNG)) != 0)
          fprintf (stderr, "Cannot write the image.\n");

      }
    }

    mng_cleanup (&hMNG);               /* cleanup the library */
  }

  fclose (pMydata->hFile);             /* cleanup */
  free (pMydata);

  return iRC;
}

The main routine of this program is nothing short of a joke.

int main (int argc, char *argv[])
{
  return makeimage ("dutch.mng");
}