/* This function writes the chunky pixels to the screen.
   In Amiga mode it calls WritePixelArray8() or WriteChunkyPixels()
   when 3.1 is detected. A special fast c2p version will be included
   later.
   In Merlin mode it will write the chunkypixels directly to the screen,
   same for Picasso, EGS and Retina, if I get the source for it.
   Please if you know how to program your gfxcard, try to replace the Merlin
   functions the card specific ones.
   I tried this pure from the autodocs I have and had little possibilities
   to really test it so if it breaks and you know why, please let me know!
*/

/***********************************************************/
extern LONG gfxcard;
/***********************************************************/


XPutImage(
    Display*		display,
    Drawable		d,
    GC				gc,
    XImage*			image,
    int				src_x,
    int				src_y,
    int				dest_x,
    int				dest_y,
    unsigned int	width,
    unsigned int	height	   )
{
extern struct HRHandle *hrh;
extern UBYTE  *basemem,*memstart;
extern char *im_buff0,*im_buff1;

/* oli start */
  extern struct E_EScreen  *pEgsScreen;
  struct E_EBitMap         *pTempBitmap;
/* oli end */

/* screen is opened in the other module */
extern Screen *picasso_scr;
extern UBYTE *picasso_mem;

	struct IntuiMessage	*imsg;
	struct BitMap tempbm;
	struct VilCopyRecord blitrec;

	ULONG				class;
	static long frame = 0;
	static char framename[ 256 ];

	if( image->depth == 1 )
	{
		/* B&W Bitmap (1 Bit = 1 Pixel) */
		
		/* erst ins ChipMem kopieren */
		CopyMem( image->data, bwbm->Planes[0], bwbm->BytesPerRow * bwbm->Rows );
		
		/* und dann anzeigen */
		BltBitMapRastPort( bwbm, 0, 0, rp, offx, offy, width, height, 0x030 );
	}
	else if( image->depth == 8 )
	{
		/* Standard 8 Bit Bitmap (chunky pixels) */
		switch (gfxcard)
		{
			case AMIGA:
	 			if(GfxBase->LibNode.lib_Version>39)
					WriteChunkyPixels( rp, offx, offy, offx+width-1, offy+height-1, image->data, width );
				else
				{
					CopyMemQuick(image->data,im_buff1,width*height);
    	    		WritePixelArray8(rp, offx, offy, offx+width-1, offy+height-1, im_buff1, temprp);
				}
				break;
			case MERLIN:
				hrWriteRectPK8(&hrh->BitMap,image->data,0,0,width,height,1,width); /*MERLIN*/
				break;
			case EGS:
fprintf(stderr,"Going to blit imagedata to EGS\n");
/* oli start */
				pTempBitmap = E_AllocBitMap(width, height, 8, E_PIXELMAP,pEgsScreen->Map->Flags, NULL);
				if (pTempBitmap)
				{
					CopyMemQuick(image->data, pTempBitmap->Typekey.PixelMap.Planes.Dest,width*height);
					EB_CopyBitMap(pTempBitmap, pEgsScreen->Map, src_x, src_y, width, height,dest_x, dest_y, 0L);
					E_DisposeBitMap(pTempBitmap);
				}
/* oli end */
				break;
			case PICASSO:
fprintf(stderr,"Going to blit imagedata to Picasso\n");
				Forbid();
				ScreenToFront(picasso_scr);
				picasso_mem=LockVillageScreen(picasso_scr);
				blitrec.SrcPitch=picasso_scr->Width;	/* Width of source display */
				blitrec.DestPitch=picasso_scr->Width;	/* Width of destination display */
				blitrec.Width=width;				/* Width of rectangle box */
				blitrec.Height=height;				/* Height of rectangle box */
				blitrec.ROP=VIL_SRCCOPY;

				blitrec.SrcAdr=(UBYTE *)image->data;
				blitrec.DestAdr=(UBYTE *)picasso_mem;
				VillageBlitCopy(picasso_scr,&blitrec);
				WaitVillageBlit();
				UnLockVillageScreen(picasso_scr);
				Permit();
				break;
			default:
				break;
		}
	}
	else if( image->depth == 16 )
	{
		/* Standard 16 Bit Bitmap (chunky pixels) */
		switch (gfxcard)
		{
			case MERLIN:
/*				hrWriteRectRGB(&hrh->BitMap,image->data,image->data,image->data,0,0,width,height,2,2*width);*/
				break;
			case EGS:
			case PICASSO:
			default:
				break;
		}
	}
	else if( image->depth >= 24 )
	{
		switch (gfxcard)
		{
			case MERLIN:
/*				hrWriteRectRGB(&hrh->BitMap,image->data,image->data,image->data,0,0,width,height,2,2*width);*/
				break;
			case EGS:
			case PICASSO:
			default:
				break;
		}
	}
}


XStoreColors(Display *theDisp,Colormap theCmap,XColor defs[], int xa_cmap_size)
{
	ULONG i;

	switch (gfxcard)
	{
		case AMIGA:
			for(i=0;i<xa_cmap_size;i++)
			{
				SetRGB32(vp,i,(ULONG )defs[i].red<<24,(ULONG )defs[i].green<<24,(ULONG )defs[i].blue<<24);
			}
			break;
		case MERLIN:
			for(i=0;i<xa_cmap_size;i++)
			{
				hrSetRGB32(hrh,i,(ULONG )defs[i].red<<24,(ULONG )defs[i].green<<24,(ULONG )defs[i].blue<<24);/*MERLIN*/
			}
			break;
		case EGS:
/* Oli start */
			for (i=0; i < xa_cmap_size; i++)
			{
				E_SetRGB8(pEgsScreen, i, (UWORD)defs[i].red << 24,(UWORD)defs[i].green << 24, (UWORD)defs[i].blue << 24);
			}
/* Oli end */
			break;
		case PICASSO:
			for (i=0; i < xa_cmap_size; i++)
			{
				SETRGB(picasso_scr,i , (UWORD)defs[i].red << 24,(UWORD)defs[i].green << 24, (UWORD)defs[i].blue << 24);
			}
		default:
			break;
	}
}
/* array defs contains colors scaled to 16 bits. */
