
/** iff.c ******************/
/*                         */
/* by [RK]                 */
/*                         */
/* Copyright © 1988 by DgA */
/*                         */
/***************************/

/*
Firma   : Digital Artists (DgA)
Objekt  : reederei/iff.c
Version : 1.0
Autor   : [RK]
Datum   : 08-15-1988
Status  : geheim
*/

#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/gfx.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include "iff.h"

struct BitMapHeader bmhd;
UBYTE *buf,*buf2,*bp; /* Buffer for useless IFF-variables and the whole file */
ULONG fh;  /* FileHandle */
ULONG cc;  /* ColorCount */
UBYTE r,g,b; /* Color (RGB) */
UBYTE *maps[5];
char id [4];

iff(name,bm,cm)
char *name;
struct BitMap *bm;
USHORT *cm;
 {
  int i,j;
  UWORD x;
  BOOL gotbmhd = FALSE; /* to make sure, that BMHD had loaded first */
  BOOL gotbody = FALSE; /* to make sure, that BODY had loaded */
  LONG s; /* size of the bitmaps */

  fh = Open(name,MODE_OLDFILE);
  if (fh == NULL)
   {
    printf("Error : Can't open File '%s'.\n",name);
    return(FALSE);
   }

  buf = AllocMem(108,MEMF_CHIP|MEMF_CLEAR);

  Read(fh,buf,12); /* Reads in the FORM-ID */

  while(!(gotbody))
   {
    Read(fh,&id[0],4); /* checks the IFF-ID (e.g. BMHD,CMAP or BODY) */

    if ((strncmp(&id[0],&ID_DPPV[0],4)) == 0)
     Read(fh,buf,108);

    if ((strncmp(&id[0],&ID_BMHD[0],4)) == 0)
     {
      Read(fh,buf,4);
      Read(fh,&bmhd,20);
      gotbmhd = TRUE;
     }

    if ((strncmp(&id[0],&ID_CMAP[0],4)) == 0)
     {
      Read(fh,&cc,4);
      for(i=0;i<(cc/3);i++)
       {
        Read(fh,&r,1);
        Read(fh,&g,1);
        Read(fh,&b,1);
        *cm++ =(USHORT)((r*16)+g+(b/16));
       }
     }

    if ((strncmp(&id[0],&ID_CRNG[0],4)) == 0)
     Read(fh,buf,12);

    if (((strncmp(&id[0],&ID_BODY[0],4)) == 0) && (gotbmhd))
     {
      Read(fh,&s,4);
      buf2 = AllocMem(s,MEMF_CHIP|MEMF_CLEAR);
      Read(fh,buf2,s);
      bp = buf2;

      x = (bmhd.w >> 3);

      for (i=0;i<bmhd.nPlanes;i++)
       maps[i]=bm->Planes[i];

      for (i=0;i<bmhd.h;i++)
       for (j=0;j<bmhd.nPlanes;j++)
        maps[j] = uncompress(maps[j],x);
      gotbody = TRUE;
     }

    if ((strncmp(&id[0],&ID_CAMG[0],4)) == 0)
     Read(fh,buf,8);

    if ((strncmp(&id[0],&ID_CCRT[0],4)) == 0)
     Read(fh,buf,18);
   }
  Close(fh);
  FreeMem(buf2,s);
  FreeMem(buf,108);
  return;
 }

uncompress(pp,l)
UBYTE *pp;
UWORD l;
 {
  int i;
  BYTE cb; /* control byte */
  BYTE rb; /* byte to be repeated */

  while (l > 0)
   {
    if (bmhd.compression == cmpByteRun1)
     {
      cb = *bp++;
      if (cb < 0)
       {
        if (cb > -128)
         {
          rb = *bp++;
          for (i=0;i<((-cb)+1);i++)
           {
            *pp++ = rb;
            l--;
           }
         }
       }
      else
       {
        for(i=0;i<(cb+1);i++)
         {
          *pp++ = *bp++;
          l--;
         }
       }
     }
    else
     {
      for (;l>0;l--)
       *pp++ = *bp++;
     }
   }
  return(pp);
 }
