/* $Revision Header *** Header built automatically - do not edit! ***********
 *
 *	(C) Copyright 1991 by Metalworx
 *
 *	Name .....: saveiff.c
 *	Created ..: Mon 07-Oct-91 20:50
 *	Revision .: 0
 *
 *	Date		Author		Comment
 *	=========	========	====================
 *	07-Oct-91	Mtwx		Created this file!
 *
 * $Revision Header ********************************************************/
 #define REVISION 0

#include <exec/libraries.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/icon.h>

#include "saveiff.h"
#include <iff/ilbm.h>

#include "chem_defs.h"
#include "messages.h"

/* ------------------------------- external references ------------------ */

extern struct ChemPrefs ChemPrefs;

extern void SimpleRequest();

/* ------------------------------- routines ----------------------------- */

LONG	  save(filename, screen, wwidth, sheight)       /* saves displayed map as IFF FORM */
UBYTE	 *filename;
struct Screen *screen;
int	  wwidth, sheight;
{
  IFFP	    PutAnILBM();
  struct Library *IconBase;
  LONG	    file;
  struct BitMap *picBitMap;
  struct ViewPort *vp;
  ULONG     picViewModes;
  WORD	   *picColorTable;
  extern struct Screen *s;
  IFFP	    iffp = NO_FILE;
  LONG	    returncode = NOT_OK;

  vp = &(screen->ViewPort);
  if (!(file = Open(filename, MODE_NEWFILE)))
    return (returncode);

  Write(file, "x", 1);                 /* 1.1 so Seek to beginning works ? (?!?!???)*/

  picBitMap = (struct BitMap *) vp->RasInfo->BitMap;
  picColorTable = (WORD *) vp->ColorMap->ColorTable;
  picViewModes = (ULONG) vp->Modes;

  iffp = PutAnILBM(file, picBitMap, picColorTable, picBitMap->Depth,
		   picViewModes, wwidth, sheight);

  Close(file);

  if (iffp == IFF_OKAY)
  {
    if(ChemPrefs.IconCreaIFF==TRUE)
    {
      IconBase = OpenLibrary("icon.library", 0);
      if (IconBase != NULL)
      {
	if (PutDiskObject(filename, &ILBMobject))
	  returncode = OK;
	else
	{
	  DisplayBeep(screen);
	  SimpleRequest(ERROR_PUT_ICON);
	}
	CloseLibrary(IconBase);
      }
      else
	SimpleRequest(NO_ICON_LIB);
    }
    else
      returncode=OK;
  }
  return (returncode);
}


#define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}

IFFP	  PutAnILBM(file, bitmap, colorMap, depth,
			      viewmodes, wwidth, sheight)
LONG	  file;
struct BitMap *bitmap;
WORD	 *colorMap;
UBYTE	  depth;
ULONG	  viewmodes;
int	  wwidth, sheight;
{
  BYTE	    buffer[bufsize];
  BitMapHeader bmHdr;
  CamgChunk camgChunk;
  GroupContext fileContext, formContext;
  IFFP	    ifferr;

  ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
		     cmpByteRun1, 0, wwidth, sheight);
  bmHdr.y = 0;
  bmHdr.h = sheight;

  camgChunk.ViewModes = viewmodes & CAMGMASK;

  CkErr(OpenWIFF(file, &fileContext, szNotYetKnown));
  CkErr(StartWGroup(&fileContext, FORM, szNotYetKnown, ID_ILBM, &formContext));

  CkErr(PutBMHD(&formContext, &bmHdr));
  CkErr(PutCAMG(&formContext, &camgChunk));
  CkErr(PutCMAP(&formContext, colorMap, depth));
  CkErr(PutBODY(&formContext, bitmap, mskNone, &bmHdr, buffer, bufsize));

  CkErr(EndWGroup(&formContext));
  CkErr(CloseWGroup(&fileContext));
  return (ifferr);
}
