#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <stdio.h>


void *AllocMem();
void  FreeMem();

extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase	    *GfxBase;

BOOL GetImage(filename,I)

char   *filename;
struct Image *I;

{ FILE *f;

  int x1,x2,y1,y2;
  
  int  i,x,y,xl;
  
  long sp_size;
  
  UBYTE *Buffer;

  if (!(f = fopen((UBYTE*)filename,"r")))
     return(FALSE);
  
  x1 = getw(f);
  y1 = getw(f), 
  
  x            = x1;
  I->Width     = (SHORT)(x1 * 8);
  I->Height    = (SHORT)y1;
    
  I->LeftEdge  = 0;
  I->TopEdge   = 0;
  
  if ((I->Width > 640) || (I->Height > 256)) 
     {
      I->Width  = 0;
      I->Height = 0;
      I->ImageData = 0L;
      
      return(FALSE);
     }
   
  I->Depth     = getw(f);

  sp_size      = (long)I->Depth * (long)I->Height * (long)(I->Width+8);
  sp_size      = sp_size/8L;
  Buffer       = (UBYTE*)AllocMem((ULONG)sp_size,(ULONG)MEMF_CHIP|MEMF_CLEAR);

#ifdef DEBUG
printf("X : %d, Y : %d, D : %d, Buffer : %ld Bytes.\n",(int)I->Width,
        (int)I->Height,(int)I->Depth,sp_size);
#endif  

  I->ImageData = (USHORT*)Buffer;
 
  x2 = 0;
  for (i = 0; i < I->Depth; i++)
      {
       for (y=0; y < I->Height; y++)
           {
             xl = fread(Buffer,x,1,f); 
	    Buffer += (LONG)x;
            if ((x % 2) != 0) 
               {
                x2++;
                *Buffer = (UBYTE)0;
                Buffer ++; /* ImageDaten : 16 Bit */ 
               }
            x2 += x;
           }
     }

 I->PlanePick   = (UBYTE)0x07;
 I->PlaneOnOff  = (UBYTE)0x00;
 
 fclose(f);

#ifdef DEBUG
printf("X : %d, Y : %d, D : %d, Buffer : %ld Bytes.\n",(int)I->Width,
        (int)I->Height,(int)I->Depth,sp_size);
#endif  
 return(TRUE);
}
  
DisposeImage(I)

struct Image *I;

{
 long sp_size;
 
 if (I->ImageData == 0L)
    return();

 sp_size      = (long)I->Depth * (long)I->Height * (long)(I->Width+8);
 sp_size      = sp_size / 8L;
 FreeMem((UBYTE*)I->ImageData,(ULONG)sp_size);
} 

