mng_retcode iRetcode;
mng_retcode restore_state (mng_datap pData){  mng_savedatap pSave;  mng_imagep    pImage;#ifdef MNG_SUPPORT_TRACE  MNG_TRACE (pData, MNG_FN_RESTORE_STATE, MNG_LC_START)#endif  if (pData->pSavedata)                /* do we have a saved state ? */  {    pSave = pData->pSavedata;          /* address it more directly */                                       /* and copy it back to the main struct */#if defined(MNG_SUPPORT_READ) || defined(MNG_SUPPORT_WRITE)    pData->bHasglobalPLTE       = pSave->bHasglobalPLTE;    pData->bHasglobalTRNS       = pSave->bHasglobalTRNS;    pData->bHasglobalGAMA       = pSave->bHasglobalGAMA;    pData->bHasglobalCHRM       = pSave->bHasglobalCHRM;    pData->bHasglobalSRGB       = pSave->bHasglobalSRGB;    pData->bHasglobalICCP       = pSave->bHasglobalICCP;    pData->bHasglobalBKGD       = pSave->bHasglobalBKGD;#endif /* MNG_SUPPORT_READ || MNG_SUPPORT_WRITE */    pData->iDEFIobjectid        = pSave->iDEFIobjectid;    pData->iDEFIdonotshow       = pSave->iDEFIdonotshow;    pData->iDEFIconcrete        = pSave->iDEFIconcrete;    pData->bDEFIhasloca         = pSave->bDEFIhasloca;    pData->iDEFIlocax           = pSave->iDEFIlocax;    pData->iDEFIlocay           = pSave->iDEFIlocay;    pData->bDEFIhasclip         = pSave->bDEFIhasclip;    pData->iDEFIclipl           = pSave->iDEFIclipl;    pData->iDEFIclipr           = pSave->iDEFIclipr;    pData->iDEFIclipt           = pSave->iDEFIclipt;    pData->iDEFIclipb           = pSave->iDEFIclipb;    pData->iBACKred             = pSave->iBACKred;    pData->iBACKgreen           = pSave->iBACKgreen;    pData->iBACKblue            = pSave->iBACKblue;    pData->iBACKmandatory       = pSave->iBACKmandatory;    pData->iBACKimageid         = pSave->iBACKimageid;    pData->iBACKtile            = pSave->iBACKtile;    pData->iFRAMmode            = pSave->iFRAMmode;    pData->iFRAMdelay           = pSave->iFRAMdelay;    pData->iFRAMtimeout         = pSave->iFRAMtimeout;    pData->bFRAMclipping        = pSave->bFRAMclipping;    pData->iFRAMclipl           = pSave->iFRAMclipl;    pData->iFRAMclipr           = pSave->iFRAMclipr;    pData->iFRAMclipt           = pSave->iFRAMclipt;    pData->iFRAMclipb           = pSave->iFRAMclipb;                                       /* also the next subframe parms */    pData->iFramemode           = pSave->iFRAMmode;    pData->iFramedelay          = pSave->iFRAMdelay;    pData->iFrametimeout        = pSave->iFRAMtimeout;    pData->bFrameclipping       = pSave->bFRAMclipping;    pData->iFrameclipl          = pSave->iFRAMclipl;    pData->iFrameclipr          = pSave->iFRAMclipr;    pData->iFrameclipt          = pSave->iFRAMclipt;    pData->iFrameclipb          = pSave->iFRAMclipb;    pData->iNextdelay           = pSave->iFRAMdelay;    pData->iGlobalPLTEcount     = pSave->iGlobalPLTEcount;    MNG_COPY (pData->aGlobalPLTEentries, pSave->aGlobalPLTEentries, sizeof (mng_rgbpaltab))    pData->iGlobalTRNSrawlen    = pSave->iGlobalTRNSrawlen;    MNG_COPY (pData->aGlobalTRNSrawdata, pSave->aGlobalTRNSrawdata, 256)    pData->iGlobalGamma         = pSave->iGlobalGamma;    pData->iGlobalWhitepointx   = pSave->iGlobalWhitepointx;    pData->iGlobalWhitepointy   = pSave->iGlobalWhitepointy;    pData->iGlobalPrimaryredx   = pSave->iGlobalPrimaryredx;    pData->iGlobalPrimaryredy   = pSave->iGlobalPrimaryredy;    pData->iGlobalPrimarygreenx = pSave->iGlobalPrimarygreenx;    pData->iGlobalPrimarygreeny = pSave->iGlobalPrimarygreeny;    pData->iGlobalPrimarybluex  = pSave->iGlobalPrimarybluex;    pData->iGlobalPrimarybluey  = pSave->iGlobalPrimarybluey;    pData->iGlobalRendintent    = pSave->iGlobalRendintent;    pData->iGlobalProfilesize   = pSave->iGlobalProfilesize;    if (pData->iGlobalProfilesize)     /* has a profile ? */    {                                  /* then copy that ! */      MNG_ALLOC (pData, pData->pGlobalProfile, pData->iGlobalProfilesize)      MNG_COPY (pData->pGlobalProfile, pSave->pGlobalProfile, pData->iGlobalProfilesize)    }    pData->iGlobalBKGDred       = pSave->iGlobalBKGDred;    pData->iGlobalBKGDgreen     = pSave->iGlobalBKGDgreen;    pData->iGlobalBKGDblue      = pSave->iGlobalBKGDblue;  }  else                                 /* no saved-data; so reset the lot */  {#if defined(MNG_SUPPORT_READ) || defined(MNG_SUPPORT_WRITE)    pData->bHasglobalPLTE       = MNG_FALSE;    pData->bHasglobalTRNS       = MNG_FALSE;    pData->bHasglobalGAMA       = MNG_FALSE;    pData->bHasglobalCHRM       = MNG_FALSE;    pData->bHasglobalSRGB       = MNG_FALSE;    pData->bHasglobalICCP       = MNG_FALSE;    pData->bHasglobalBKGD       = MNG_FALSE;#endif /* MNG_SUPPORT_READ || MNG_SUPPORT_WRITE */    pData->iDEFIobjectid        = 0;    pData->iDEFIdonotshow       = 0;    pData->iDEFIconcrete        = 0;    pData->bDEFIhasloca         = MNG_FALSE;    pData->iDEFIlocax           = 0;    pData->iDEFIlocay           = 0;    pData->bDEFIhasclip         = MNG_FALSE;    pData->iDEFIclipl           = 0;    pData->iDEFIclipr           = 0;    pData->iDEFIclipt           = 0;    pData->iDEFIclipb           = 0;    if (!pData->bEMNGMAhack)             /* TODO: remove line in 1.0.0 !!! */    {                                    /* TODO: remove line in 1.0.0 !!! */    pData->iBACKred             = 0;    pData->iBACKgreen           = 0;    pData->iBACKblue            = 0;    pData->iBACKmandatory       = 0;    pData->iBACKimageid         = 0;    pData->iBACKtile            = 0;    }                                    /* TODO: remove line in 1.0.0 !!! */    pData->iFRAMmode            = 1;    pData->iFRAMdelay           = 1;    pData->iFRAMtimeout         = 0x7fffffffl;    pData->bFRAMclipping        = MNG_FALSE;    pData->iFRAMclipl           = 0;    pData->iFRAMclipr           = 0;    pData->iFRAMclipt           = 0;    pData->iFRAMclipb           = 0;                                         /* also the next subframe parms */    pData->iFramemode           = 1;    pData->iFramedelay          = 1;    pData->iFrametimeout        = 0x7fffffffl;    pData->bFrameclipping       = MNG_FALSE;    pData->iFrameclipl          = 0;    pData->iFrameclipr          = 0;    pData->iFrameclipt          = 0;    pData->iFrameclipb          = 0;    pData->iNextdelay           = 1;    pData->iGlobalPLTEcount     = 0;    pData->iGlobalTRNSrawlen    = 0;    pData->iGlobalGamma         = 0;    pData->iGlobalWhitepointx   = 0;    pData->iGlobalWhitepointy   = 0;    pData->iGlobalPrimaryredx   = 0;    pData->iGlobalPrimaryredy   = 0;    pData->iGlobalPrimarygreenx = 0;    pData->iGlobalPrimarygreeny = 0;    pData->iGlobalPrimarybluex  = 0;    pData->iGlobalPrimarybluey  = 0;    pData->iGlobalRendintent    = 0;    if (pData->iGlobalProfilesize)       /* free a previous profile ? */      MNG_FREE (pData, pData->pGlobalProfile, pData->iGlobalProfilesize)    pData->iGlobalProfilesize   = 0;    pData->iGlobalBKGDred       = 0;    pData->iGlobalBKGDgreen     = 0;    pData->iGlobalBKGDblue      = 0;  }  if (!pData->bEMNGMAhack)             /* TODO: remove line in 1.0.0 !!! */  {                                    /* TODO: remove line in 1.0.0 !!! */                                       /* drop un-frozen image objects */  pImage = (mng_imagep)pData->pFirstimgobj;  while (pImage)  {                                    /* is it un-frozen ? */    if (!pImage->bFrozen)    {      mng_imagep  pPrev = (mng_imagep)pImage->sHeader.pPrev;      mng_imagep  pNext = (mng_imagep)pImage->sHeader.pNext;      mng_retcode iRetcode;      if (pPrev)                       /* unlink it */        pPrev->sHeader.pNext = pNext;      else        pData->pFirstimgobj  = pNext;      if (pNext)        pNext->sHeader.pPrev = pPrev;      else        pData->pLastimgobj   = pPrev;      if (pImage->pImgbuf->bFrozen)    /* buffer frozen ? */      {        if (pImage->pImgbuf->iRefcount <= 2)          MNG_ERROR (pData, MNG_INTERNALERROR)                                       /* decrease ref counter */        pImage->pImgbuf->iRefcount--;                                       /* just cleanup the object then */        MNG_FREEX (pData, pImage, sizeof (mng_image))      }      else      {                                /* free the image buffer */        iRetcode = free_imagedataobject (pData, pImage->pImgbuf);                                       /* and cleanup the object */        MNG_FREEX (pData, pImage, sizeof (mng_image))        if (iRetcode)                  /* on error bail out */          return iRetcode;      }      pImage = pNext;                  /* this is the next */    }    else                               /* neeeext */      pImage = (mng_imagep)pImage->sHeader.pNext;  }  }                                    /* TODO: remove line in 1.0.0 !!! */#ifdef MNG_SUPPORT_TRACE  MNG_TRACE (pData, MNG_FN_RESTORE_STATE, MNG_LC_END)#endif  return MNG_NOERROR;}