/*
 *  FILE: bitmap.c
 *  Support routines for dynamic (de)allocation of bitmaps.
 *
 *  Public Domain, but keep my name in it as the original author.
 *  31-Aug-88	Jan Sven Trabandt   first release version
 */


#include "gimmelib/gimmefuncs.h"


struct BitMap *gimmeBitMap( depth, width, height )
    SHORT   depth, width, height;
{
    register struct BitMap  *bm;
    SHORT		    i;

    bm = (struct BitMap *) AllocMem( (ULONG)sizeof(struct BitMap),
					MEMF_PUBLIC | MEMF_CLEAR );
    if( !bm ) {
	return( NULL );
    }
    if( depth > 0 && width > 0 && height > 0 ) {
	InitBitMap( bm, (ULONG) depth, (ULONG) width, (ULONG) height );
	for( i = 0; i < depth; ++i ) {
	    if( !(bm->Planes[i] = (PLANEPTR)
				    AllocRaster((ULONG)width,(ULONG)height)) ) {
		bm->Depth = i;
		getRidOfBitMap( bm );
		return( NULL );
	    }
	    BltClear( bm->Planes[i],
			(ULONG)RASSIZE((ULONG)width,(ULONG)height), 0L );
	} /* for */
    }
    return( bm );
} /* gimmeBitMap */


short getRidOfBitMap( bitmap )
    register struct BitMap  *bitmap;
{
    SHORT   i;
    SHORT   width;

#ifdef GIMME_WIMPY
    if( !bitmap ) {
	return( -1 );
    }
#endif
    width = bitmap->BytesPerRow << 3;
    for( i = 0; i < bitmap->Depth && bitmap->Planes[i]; ++i ) {
	FreeRaster( bitmap->Planes[i], (ULONG) width, (ULONG) bitmap->Rows );
    } /* for */
    FreeMem( bitmap, (ULONG)sizeof(struct BitMap) );
    return( 0 );
} /* getRidOfBitMap */
