/***************************************************************************
*  SaveILBM.c --  Save raw raster image as ILBM file
*                 by Carolyn Scheppner  CBM  01/86
*     Using IFF rtns by J.Morrison and S.Shaw of Electronic Arts
*
***************************************************************************/

/*
#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <graphics/gfxbase.h>
#include <graphics/rastport.h>
#include <graphics/gfx.h>
#include <graphics/view.h>
*/

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

#include <libraries/dos.h>

#include "ilbm.h"

#define	bufSize	512


/*
struct	Window	*activeWindow;
struct	Screen	*activeScreen;
struct	Screen	*firstScreen;

struct	Screen		*nextScreen, *highScreen;
struct	View		*thisView;
struct	View		*picView = 0;
*/
struct	ViewPort	*picViewPort;
struct	BitMap		*picBitMap;
WORD			*picColorTable;


/* main() */
void	save_screen( s, name )
struct	Screen	*s;
char	name[];
{
	LONG	file;

	if( file = Open(name, MODE_NEWFILE) ) {

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

		picViewPort = &( s -> ViewPort );

		picBitMap = (struct BitMap *)
				picViewPort -> RasInfo -> BitMap;

		picColorTable = (WORD *)
				picViewPort -> ColorMap -> ColorTable;

		/* iffp = */
		PutPicture(file, picBitMap, picColorTable);

		Close(file);
	} else
		printf("Error opening file [%s].\n", name );
}

/** PutPicture() ***********************************************************
 *
 * Put a picture into an IFF file.
 * This procedure calls PutAnILBM, passing in an <x, y> location of <0, 0>,
 * a NULL mask, and a locally-allocated buffer. It also assumes you want to
 * write out all the bitplanes in the BitMap.
 *
 ***************************************************************************/
Point2D nullPoint = {0, 0};

IFFP	PutPicture(file, bitmap, colorMap)
LONG	file;
struct	BitMap	*bitmap;
WORD	*colorMap;
{
	BYTE	buffer[bufSize];

	return(
		PutAnILBM(file, bitmap, NULL,
			colorMap, bitmap->Depth, &nullPoint,
			buffer, bufSize
		)
	);
}

   
/** PutAnILBM() ************************************************************
 *
 * Write an entire BitMap as a FORM ILBM in an IFF file.
 * This version works for any display mode (C. Scheppner).
 *
 * Normal return result is IFF_OKAY.
 *
 * The utility program IFFCheck would print the following outline of the
 * resulting file:
 *
 *   FORM ILBM
 *     BMHD
 *     CMAP
 *     BODY       (compressed)
 *
 ***************************************************************************/
#define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}

IFFP PutAnILBM(file, bitmap, mask, colorMap, depth, xy, buffer, bufsize)
      LONG file;  struct BitMap *bitmap;  BYTE *mask;  WORD *colorMap;
      UBYTE depth;  Point2D *xy;
      BYTE *buffer;  LONG bufsize;
{
   BitMapHeader bmHdr;
   GroupContext fileContext, formContext;
   IFFP ifferr;
   WORD pageWidth, pageHeight;

   pageWidth  = (bitmap->BytesPerRow) << 3;
   pageHeight = bitmap->Rows;

   ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
                      cmpByteRun1, 0, pageWidth, pageHeight);
   /* You could write an uncompressed image by passing cmpNone instead
    * of cmpByteRun1 to InitBMHdr. */
   bmHdr.nPlanes = depth;   /* This must be  <= bitmap->Depth */
   if (mask != NULL) bmHdr.masking = mskHasMask;
   bmHdr.x = xy->x;   bmHdr.y = xy->y;

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

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

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