/* ChunkyToHAM.c © Paweî Marciniak <pmarciniak@lodz.home.pl>*/
#include <exec/execbase.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/rastport.h>
#include <graphics/gfxbase.h>
#include <graphics/view.h>
#include <intuition/screens.h>
#include <utility/tagitem.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/utility.h>

#include "/include/libraries/image.h"
#include "chunkytobm.h"

void RGBtoHAM( UBYTE *, UBYTE *, ULONG, ULONG, UBYTE );

extern struct NewBitMap *MyAllocBitMap( UWORD, UWORD, BYTE );

/* ChunkyToHAMA() (c) 1997 Paweî Marciniak
  Modify date  |  Version  |  Comment
---------------+-----------+------------------------------
11-05-99 18:10 |  0.1      | Not working yet
23-05-99 18:52 |  1.0      | NOW! working
27-05-99 18:37 |  1.01     | BUG in Alloc rgbbuf2
*/
struct BitMap * __saveds __asm ChunkyToHAMA( register __a0 struct ChunkyImg *CI_reg, register __a1 struct TagItem *Tags  )
{
  struct NewBitMap *NBM;
  struct ChunkyImg *CI = CI_reg;
  ULONG Error, n, i, csize=CI->ci_Width*CI->ci_Height;
  UBYTE *rgbbuf, *rgbbuf2, *ptr;
  UBYTE *chunkyPtr, *PalettePtr;
  UBYTE mode=MODE_HAM6;
  UBYTE paltab[3][256];

  struct RastPort temprp, *ChunkyRPort;
  struct TagItem *ti, *TagsTmp = Tags;

/* Tagi */
  while ( ti = NextTagItem( &TagsTmp ) )
  {
    switch ( ti->ti_Tag )
    {
      case CTBH_HamMode:
        mode = ti->ti_Data;
      break;

      default:
      break;
    }
  }

/* sprawdzam iloôê kolorów jeôli > 256 wyjôcie */
  if(CI->ci_NumColors > 256)
  {
    Error=1000;
    return( 0 );
  }



  if(!(ChunkyRPort=AllocVec( sizeof(struct RastPort),MEMF_ANY|MEMF_CLEAR )))
  {
    Error=1002;
    return( 0 );
  }

  InitRastPort( ChunkyRPort );
  
  PalettePtr = CI->ci_Palette;
  chunkyPtr = CI->ci_ChunkyData;

  if(!(rgbbuf=AllocVec( csize*3, MEMF_ANY|MEMF_CLEAR )))
  {
    FreeVec( ChunkyRPort );
    Error=1002;
    return( 0 );
  }

  if(!(rgbbuf2=AllocVec( csize, MEMF_ANY|MEMF_CLEAR )))
  {
    FreeVec( ChunkyRPort );
    FreeVec( rgbbuf );
    Error=1002;
    return( 0 );
  }

  for( i=0; i<CI->ci_NumColors; i++ )
  {
    paltab[0][i]=*PalettePtr;
    PalettePtr++;
    paltab[1][i]=*PalettePtr;
    PalettePtr++;
    paltab[2][i]=*PalettePtr;
    PalettePtr++;
  }

  ptr=rgbbuf;
/* Konwersja chunky z paletâ na RGB */
  for( i=0; i<csize; i++ )
  {
    *ptr=paltab[0][*chunkyPtr];
    ptr++;
    *ptr=paltab[1][*chunkyPtr];
    ptr++;
    *ptr=paltab[2][*chunkyPtr];
    ptr++;
    chunkyPtr++;
  }

  RGBtoHAM( rgbbuf, rgbbuf2, CI->ci_Width, CI->ci_Height, mode );

  FreeVec( rgbbuf );

/* Iniciacja temprp */
  InitRastPort( &temprp );
  if(!(temprp.BitMap = AllocBitMap( CI->ci_Width, 1,
                                    8, BMF_CLEAR, 0 )))
  {
    FreeVec( ChunkyRPort );
    FreeVec( rgbbuf2 );
    Error=1003;
    return( 0 );
  }

  if(!( NBM = (struct NewBitMap*)MyAllocBitMap( CI->ci_Width,
                                                CI->ci_Height,
                                                8)))
  {
    FreeVec( ChunkyRPort );
    FreeBitMap( temprp.BitMap );
    FreeVec( rgbbuf2 );
    Error=1002;
    return( 0 );
  }
  NBM->Width = CI->ci_Width;
  NBM->Height = CI->ci_Height;
  NBM->NumColors = 0;
  ChunkyRPort->BitMap = (struct BitMap *)NBM;

  chunkyPtr = rgbbuf2;

/* konwersja chunky na bitmapë */
  for( n = 0; n < CI->ci_Height; n++ )
  {
    chunkyPtr+=CI->ci_Width;
    WritePixelLine8( ChunkyRPort, 0, n, CI->ci_Width, chunkyPtr, &temprp );
  }

    FreeVec( ChunkyRPort );
    FreeBitMap( temprp.BitMap );
    FreeVec( rgbbuf2 );
  return( (struct BitMap *)NBM );
}
/***********************************************************************/
