#include <intuition/intuition.h>
#include <proto/intuition.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <proto/graphics.h>
#include <stdio.h>

#include "iffpack.h"

#define BADFLAGS (SPRITES|VP_HIDE|GENLOCK_AUDIO|GENLOCK_VIDEO)
#define FLAGMASK (~BADFLAGS)
#define CAMGMASK (FLAGMASK & 0xffffL)

#define ID(a,b,c,d) ((((a)<<8|(b))<<8|(c))<<8|(d))

void SetRGB32( struct ViewPort *, ULONG, ULONG, ULONG, ULONG );
void GetRGB32( struct ColorMap *, ULONG, ULONG, ULONG *);

#pragma libcall GfxBase SetRGB32 354 3210805
#pragma libcall GfxBase GetRGB32 384 910804

extern struct GfxBase *GfxBase;

struct BMHD {
   ULONG  Size;
   USHORT Width,Height;
   SHORT  LeftEdge,TopEdge;
   UBYTE  Depth;
   UBYTE  Mask;
   UBYTE  Compression;
   UBYTE  Pad;
   USHORT Transparent;
   UBYTE  XAspect,YAspect;
   SHORT  ScrWidth,ScrHeight;
};

static int buf[256];
static short count,runlen,ptr;

static short rows,bytes,depth;
static cmode;

static short TempY=0,TempX=0;
static struct BitMap MyBm;
static struct RastPort MyRast;
static short anzcolors;

/* static ULONG colors[256]; */

static UBYTE red[256],green[256],blue[256];

/* Prototypes */
static ReadHeader(FILE *fp,struct BMHD *bmhd,USHORT *viewmode);
static ReadBMHD(FILE *fp,struct BMHD *bmhd,USHORT *vmode);
static ReadCMAP(FILE *fp);
static ReadCAMG(FILE *fp,USHORT *vmode);
static WriteBMHD(struct Window *w,FILE *fp,short mode);
static WriteCMAP(struct Screen *s,FILE *fp,int);
static WriteCAMG(struct Screen *s,FILE *fp);
static WriteBODY(struct Window *w,FILE *fp,short mode);
static compline(FILE *fp,short bytes,short y,short p);
static comprow(FILE *fp,short rows,short x,short p);
static initcomp(short width,short height,short depth,short mode);
static compget(FILE *fp);


#define RED(c) (((c)>>16)&0xff)
#define BLUE(c) (((c)>>8)&0xff)
#define GREEN(c) ((c)&0xff)


#define COLOR32(c) ((c)|((c)<<8)|((c)<<16)|((c)<<24))


void SetColors(struct ViewPort *vp)
{
   short a;
   if(GfxBase->LibNode.lib_Version<39) {
      for(a=0;a<anzcolors;a++) {
         SetRGB4(vp,a,red[a]>>4,green[a]>>4,blue[a]>>4);
      }
   } else {
      for(a=0;a<anzcolors;a++) {
         SetRGB32(vp,a,COLOR32(red[a]),COLOR32(green[a]),COLOR32(blue[a]));
      }
   }
}
ReadPicSize(FILE *fp,SHORT *winx,SHORT *winy,SHORT *scrx,SHORT *scry,SHORT *depth,USHORT *vmode)
{
   struct BMHD bmhd;
   int err;
   if(err=ReadHeader(fp,&bmhd,vmode)) return(err);
   *winx=bmhd.Width;
   *winy=bmhd.Height;
   *scrx=bmhd.ScrWidth;
   *scry=bmhd.ScrHeight;
   *depth=bmhd.Depth;
   return(0);
}
ReadBody(struct RastPort *rp,FILE *fp)
{
   long size;
   short x,y,p;
   int c;

   if(!fread(&size,4,1,fp)) return(BAD_IFF);

   if(cmode==0) {
      for(y=0;y<rows;y++) {
         for(p=0;p<depth;p++) {
            for(x=0;x<bytes;x++) {
               if((c=getc(fp))==EOF) return(BAD_IFF);
               MyBm.Planes[p][x]=c;
            }
         }
         ClipBlit(&MyRast,0L,0L,rp,0L,(long)y,(long)TempX,1L,192L);
         WaitBlit();
      }
   } else if(cmode==1) {
      for(y=0;y<rows;y++) {
         for(p=0;p<depth;p++) {
            for(x=0;x<bytes;x++) {
               if((c=compget(fp))==EOF) return(BAD_IFF);
               if(c<256) {
                  MyBm.Planes[p][x]=c;
               }
            }
         }
         ClipBlit(&MyRast,0L,0L,rp,0L,(long)y,(long)TempX,1L,192L);
         WaitBlit();
      }
   } else if(cmode==2) {
      for(x=0;x<bytes;x++) {
         for(p=0;p<depth;p++) {
            for(y=0;y<rows;y++) {
               if((c=compget(fp))==EOF) return(BAD_IFF);
               if(c<256) {
                  MyBm.Planes[p][y<<1]=c;
               }
            }
         }
         ClipBlit(&MyRast,0L,0L,rp,(long)(x<<3),0L,8L,(long)TempY,192L);
         WaitBlit();
      }
   }
   IFFCleanup();
   return(0);
}
WriteWindow(FILE *fp,struct Window *w,int mode,int LastColors)
{
   if(mode<0 || mode>2) return(UNKNOWN_COMPRESSION);
   if(WriteBMHD(w,fp,mode)) return(WRITE_ERROR);
   if(WriteCMAP(w->WScreen,fp,LastColors)) return(WRITE_ERROR);
   if(WriteCAMG(w->WScreen,fp)) return(WRITE_ERROR);
   if(initcomp((short)w->Width,(short)w->Height,(short)w->WScreen->BitMap.Depth,
          mode)) return(NO_MEMORY);
   if(WriteBODY(w,fp,mode)) return(WRITE_ERROR);
   IFFCleanup();
   return(0);
}
void IFFCleanup(void)
{
   short a;
   if(TempX) {
      for(a=0;a<8;a++) {
         if(MyBm.Planes[a])
                 FreeRaster(MyBm.Planes[a],(long)TempX,(long)TempY);
      }
   }
   TempX=TempY=0;
}

static ReadHeader(FILE *fp,struct BMHD *bmhd,USHORT *viewmode)
{
   short status=0;
   long id[3],sid,size;
   if(!fread(id,12,1,fp)) return(BAD_IFF);
   if(id[0]!=ID('F','O','R','M')) return(BAD_IFF);
   if(id[2]!=ID('I','L','B','M')) return(BAD_IFF);
   *viewmode=0;
   do {
      if(!fread(&sid,4,1,fp)) return(BAD_IFF);
      if(sid==ID('C','M','A','P')) {
         if(status&1) return(BAD_IFF); /* doppelt */
         if(ReadCMAP(fp)) return(BAD_IFF);
         status|=1;
      } else if(sid==ID('C','A','M','G')) {
         if(status&2) return(BAD_IFF); /* doppelt */
         if(ReadCAMG(fp,viewmode)) return(BAD_IFF);
         status|=2;
      } else if(sid==ID('B','M','H','D')) {
         if(status&4) return(BAD_IFF); /* doppelt */
         if(ReadBMHD(fp,bmhd,viewmode)) return(BAD_IFF);
         status|=4;
      } else if(sid!=ID('B','O','D','Y')) {
         if(!fread(&size,4,1,fp)) return(BAD_IFF);
         size+=size&1;
         if(fseek(fp,size,1)) return(BAD_IFF);
      }
   } while(sid!=ID('B','O','D','Y'));
   if(!(status&1) || !(status&4)) return(BAD_IFF); /* Daten fehlen */
   if(initcomp((short)bmhd->Width,(short)bmhd->Height,(short)bmhd->Depth,
            (short)bmhd->Compression)) return(NO_MEMORY);
   rows=bmhd->Height;
   bytes=((bmhd->Width+15)>>3)&0xfffe;
   depth=bmhd->Depth;
   cmode=bmhd->Compression;
   if(cmode<0 || cmode>2) return(UNKNOWN_COMPRESSION);
   return(0);
}
static ReadBMHD(FILE *fp,struct BMHD *bmhd,USHORT *vmode)
{
   if(!fread(bmhd,sizeof(struct BMHD),1,fp)) return(BAD_IFF);
   if(bmhd->Size!=20) return(BAD_IFF);
   *vmode=0;
   if(bmhd->Width>400 && bmhd->Depth<=4) *vmode|=HIRES;
   if(bmhd->Height>300) *vmode|=LACE;
   if(bmhd->Depth>6) *vmode|=HAM;
   return(0);
}
static ReadCMAP(FILE *fp)
{
   long size;
   short a;
   int r,g,b;
   if(!fread(&size,4,1,fp)) return(1);
   if(size % 3) return(1);
   size/=3;
   if(size>256) return(1);
   anzcolors=size;
   for(a=0;a<size;a++) {
      if((r=getc(fp))==EOF) return(1);
      if((g=getc(fp))==EOF) return(1);
      if((b=getc(fp))==EOF) return(1);

      red[a]=r;
      green[a]=g;
      blue[a]=b;
   }
   return(0);
}
static ReadCAMG(FILE *fp,USHORT *vmode)
{
   long a;
   long size;
   if(!fread(&size,4,1,fp)) return(1);
   if(size!=4) return(1);
   if(!fread(&a,4,1,fp)) return(1);
   *vmode=a&CAMGMASK;
   return(0);
}

static WriteBMHD(struct Window *w,FILE *fp,short mode)
{
   struct BMHD bm;
   if(!fwrite("FORM    ILBMBMHD",16,1,fp)) return(1);
   bm.Size=20;
   bm.Width=w->Width;
   bm.Height=w->Height;
   bm.ScrWidth=w->WScreen->Width;
   bm.ScrHeight=w->WScreen->Height;
   bm.LeftEdge=bm.TopEdge=0;
   bm.Depth=w->WScreen->BitMap.Depth;
   bm.Mask=bm.Pad=0;
   bm.Compression=mode;
   bm.Transparent=0;
   bm.YAspect=11;
   bm.XAspect=10;
   if((w->WScreen->ViewPort.Modes&LACE) && !(w->WScreen->ViewPort.Modes&HIRES)) bm.XAspect=20;
   if(!(w->WScreen->ViewPort.Modes&LACE) && (w->WScreen->ViewPort.Modes&HIRES)) bm.XAspect=5;
   if(!fwrite(&bm,sizeof(struct BMHD),1,fp)) return(1);
   return(0);
}
static WriteCMAP(struct Screen *s,FILE *fp,int LastColors)
{
   long size;
   short anz,col;
   short i;
   struct ViewPort *vp;
   ULONG ColorSet[3];

   vp=&s->ViewPort;

   if(!fwrite("CMAP",4,1,fp)) return(1);
   anz=s->BitMap.Depth;
   anz=1<<anz;

   if(vp->Modes&HAM) { /* HAM-Screen */
      if(anz<256) anz=16; else anz=64;
   }

   if(vp->Modes&EXTRA_HALFBRITE) anz=32;

   size=3*anz;
   if(!fwrite(&size,4,1,fp)) return(1);

   if(!LastColors || anz>anzcolors) {
      if(GfxBase->LibNode.lib_Version<39) {

         for(i=0;i<anz;i++) {
            col=GetRGB4(vp->ColorMap,(long)i);
            red[i]=((col>>8)&0xf)<<4;
            green[i]=((col>>4)&0xf)<<4;
            blue[i]=(col&0xf)<<4;
         }
      } else {
         for(i=0;i<anz;i++) {
            GetRGB32(vp->ColorMap,i,1,ColorSet);
            red[i]=ColorSet[0]>>24;
            green[i]=ColorSet[1]>>24;
            blue[i]=ColorSet[2]>>24;
         }
      }
   }

   for(i=0;i<anz;i++) {
      if(putc(red[i],fp)==EOF) return(1);
      if(putc(green[i],fp)==EOF) return(1);
      if(putc(blue[i],fp)==EOF) return(1);
   }

   return(0);
}
static WriteCAMG(struct Screen *s,FILE *fp)
{
   long a;
   if(!fwrite("CAMG",4,1,fp)) return(1);
   a=4;
   if(!fwrite(&a,4,1,fp)) return(1);
   a=s->ViewPort.Modes;
   a&=CAMGMASK;
   if(!fwrite(&a,4,1,fp)) return(1);
   return(0);
}
static WriteBODY(struct Window *w,FILE *fp,short mode)
{
   struct RastPort *rp;
   struct BitMap *bm;
   short bytes,rows;
   long pos;
   long size,bodysize,depth;
   short x,y,p;

   UBYTE *plane;

   rp=w->RPort;

   if(!fwrite("BODY    ",8,1,fp)) return(1);
   pos=ftell(fp);

   bm=MyRast.BitMap;

   bytes=((w->Width+15)>>3) & 0xfffe;
   rows=w->Height;
   depth=bm->Depth;

   if(mode==2) {
      for(x=0;x<bytes;x++) {
         if(x) {
            ClipBlit(rp,(x-1L)<<3L,0L,&MyRast,0L,0L,16L,(long)TempY,192L);
            WaitBlit();
         } else {
            ClipBlit(rp,0L,0L,&MyRast,8L,0L,8L,(long)TempY,192L);
            WaitBlit();
         }
         for(p=0;p<depth;p++) {
            if(comprow(fp,rows,x,p)) return(1);
         }
      }
   } else if(mode==1) {
      for(y=0;y<rows;y++) {
         if(y) {
            ClipBlit(rp,0L,(long)y-1L,&MyRast,0L,0L,(long)TempX,2L,192L);
            WaitBlit();
         } else {
            ClipBlit(rp,0L,0L,&MyRast,0L,1L,(long)TempX,1L,192L);
            WaitBlit();
         }
         for(p=0;p<depth;p++) {
            if(compline(fp,bytes,y,p)) return(1);
         }
      }
   } else {
      for(y=0;y<rows;y++) {
         ClipBlit(rp,0L,(long)y,&MyRast,0L,0L,(long)TempX,1L,192L);
         WaitBlit();
         for(p=0;p<depth;p++) {
            plane=MyBm.Planes[p];
            for(x=0;x<bytes;x++) {
               if(putc((int)(plane[x]),fp)==EOF) return(1);
            }
         }
      }
   }

   size=ftell(fp);
   if(size&1) if(putc(0,fp)==EOF) return(1); /* auf Wortgrenze auffuellen */
   bodysize=size-pos;
   if(fseek(fp,pos-4L,0)) return(1);
   if(!fwrite(&bodysize,4,1,fp)) return(1);
   if(fseek(fp,4L,0)) return(1);
   size=(size-8+1)&0xfffffffe;
   if(!fwrite(&size,4,1,fp)) return(1);
   return(0);
}

static compline(FILE *fp,short bytes,short y,short p)
{
   short x,x2,lx;
   short found;
   short a;
   UBYTE * ptr;

   ptr=&MyBm.Planes[p][bytes];
   lx=0;
   for(x=0;x<bytes;x++) {
      x2=x;
      found=0;
      while(ptr[x2]==ptr[x2+1] && x2<bytes-1 && x2-x<127) x2++;
      x2++;
      if((x-lx)==0 && x2-x>=2 || x2-x>=3) {
         if(x-lx) {
            if(putc((int)(x-lx-1),fp)==EOF) return(1);
            for(a=lx;a<x;a++) if(putc((int)ptr[a],fp)==EOF) return(1);
         }
         if(putc(x-x2+1,fp)==EOF) return(1);
         if(putc((int)ptr[x],fp)==EOF) return(1);
         x=x2-1;
         found=1;
      }
      if(found) {
         lx=x+1;
      } else {
         if(x-lx==127) {
            if(fputc(127,fp)==EOF) return(1);
            for(a=lx;a<=x;a++) if(putc(ptr[a],fp)==EOF) return(1);
            lx=x+1;
         }
      }
   }
   if(lx<bytes) {
      if(putc(bytes-lx-1,fp)==EOF) return(1);
      for(a=lx;a<bytes;a++) if(putc((int)ptr[a],fp)==EOF) return(1);
   }
   return(0);
}
static comprow(FILE *fp,short rows,short x,short p)
{
   short y,y2,y3,ly;
   short found;
   short a;
   UBYTE * ptr;

   ptr=MyBm.Planes[p];
   ly=0;
   for(y=0;y<rows;y++) {
      y3=y2=y;
      found=0;
      while(ptr[(y2<<1)+1]==ptr[(y2<<1)+3] && y2<rows-1 &&
                                                    y2-y<127) y2++;
      y2++;
      if(x) {
         while(ptr[y3<<1]==ptr[(y3<<1)+1] && y3<rows &&
                                                    y3-y<256) y3++;
      }
      if(y2>=y3) {
         if(y-ly==0 && y2-y>=2 || y2-y>=3) {
            if(y-ly) {
               if(putc(y-ly-1,fp)==EOF) return(1);
               for(a=ly;a<y;a++)
                  if(putc((int)ptr[(a<<1)+1],fp)==EOF) return(1);
            }
            if(putc(y-y2+1,fp)==EOF) return(1);
            if(putc((int)ptr[(y<<1)+1],fp)==EOF) return(1);
            y=y2-1;
            found=1;
         }
      } else {
         if(!y-ly && y3-y>=2 || y3-y>=3) {
            if(y-ly) {
               if(putc(y-ly-1,fp)==EOF) return(1);
               for(a=ly;a<y;a++)
                  if(putc((int)ptr[(a<<1)+1],fp)==EOF) return(1);
            }
            if(putc(128,fp)==EOF) return(1);
            if(putc(y3-y-1,fp)==EOF) return(1);
            y=y3-1;
            found=1;
         }
      }
      if(found) {
         ly=y+1;
      } else {
         if(y-ly==127) {
            if(fputc(127,fp)==EOF) return(1);
            for(a=ly;a<=y;a++)
                  if(putc(ptr[(a<<1)+1],fp)==EOF) return(1);
            ly=y+1;
         }
      }
   }
   if(ly<rows) {
      if(putc(rows-ly-1,fp)==EOF) return(1);
      for(a=ly;a<rows;a++)
         if(putc((int)ptr[(a<<1)+1],fp)==EOF) return(1);
   }
   return(0);
}

static initcomp(short width,short height,short depth,short mode)
{
   short a;
   if(TempX) IFFCleanup();
   if(mode!=2) {
      TempX=width;
      TempY=2;
      InitRastPort(&MyRast);
      InitBitMap(&MyBm,(long)depth,(long)width,2L);
      for(a=0;a<8;a++) MyBm.Planes[a]=0;
      for(a=0;a<depth;a++) {
         if(!(MyBm.Planes[a]=AllocRaster((long)width,2L))) return(NO_MEMORY);
      }
   } else {
      TempX=16;
      TempY=height;
      InitRastPort(&MyRast);
      InitBitMap(&MyBm,(long)depth,16L,(long)height);
      for(a=0;a<8;a++) MyBm.Planes[a]=0;
      for(a=0;a<depth;a++) {
         if(!(MyBm.Planes[a]=AllocRaster(16L,(long)height))) return(NO_MEMORY);
      }
   }
   MyRast.BitMap=&MyBm;
   count=ptr=0;
   runlen=1;
   return(0);
}

static compget(FILE *fp)
{
   int c;
   char cc;
   UBYTE val;
   if(ptr>=count) {
      if((c=getc(fp))==EOF) return(EOF);
      cc=c;
      ptr=0;
      if(c==128) {
         if((c=getc(fp))==EOF) return(EOF);
         count=c+1;
         for(c=0;c<count;c++) buf[c]=256;
      } else if(cc>=0) {
         count=cc+1;
         for(c=0;c<count;c++) if((buf[c]=getc(fp))==EOF) return(EOF);
      } else {
         count=-cc+1;
         if((c=getc(fp))==EOF) return(EOF);
         val=c;
         for(c=0;c<count;c++) buf[c]=val;
      }
   }
   if(ptr>=count) return(EOF);
   return(buf[ptr++]);
}


