/*
 =======================================================================================
  Name        : TexConvert.c
  Author      : GLIGLOU corporation
  Description : function to convert bitmap
 =======================================================================================
 */
 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "textu_st.h"
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
 
 
/*
 ----------------------------------------------------------------------------------------
  Description   : invert alpha fiels
 ----------------------------------------------------------------------------------------
 */
void GLI_vInvertAlphaField(long *pDest, long lSize)
{
    long *pLast = pDest + lSize;
 
    for (;pDest<pLast;pDest++)
        *pDest ^= 0xff000000;
}
 
/*
 ----------------------------------------------------------------------------------------
  Description   : verify alpha field
 ----------------------------------------------------------------------------------------
 */
unsigned  long GLI_vVerifyTextureIsRealyAlpha ( GLI_tdstTexture *p_stTexture )
{
    unsigned long ulBitmapCounter,ulOriginValue;
    
    ulOriginValue = 0xFF000000;
    for (ulBitmapCounter = 0; ulBitmapCounter < (unsigned long) ((p_stTexture -> lWidth * p_stTexture -> lHeight)); ulBitmapCounter ++)
        ulOriginValue &= *(((long *)p_stTexture -> p_vBitMap) + ulBitmapCounter ) ;
 
   return ulOriginValue;
}
 
 
 
/*
 ----------------------------------------------------------------------------------------
  Description   : convert 8888 bitmap to 565 bitmap
 ----------------------------------------------------------------------------------------
 */
#include "texture.h"
void GLI_vCompressNZTex565 ( GLI_tdstTexture *p_stTexture , void *GLI_gs_p_ConvertBuffer);

void GLI_vCompressTex565 ( GLI_tdstTexture *p_stTexture , void *GLI_gs_p_ConvertBuffer)
{
    unsigned long lBitmapCounter,lCompressValue,lOriginValue,*p_vConvertBufferCount;
    p_vConvertBufferCount = (long *)GLI_gs_p_ConvertBuffer ;
    
	/* Begin G.CLEMENT 28/07/1999 */
	if(p_stTexture->lTextureCaps & GLI_C_lNZTexture) {
		GLI_vCompressNZTex565(p_stTexture, GLI_gs_p_ConvertBuffer);
		return;
	}
	/* End G.CLEMENT */
    if ((p_stTexture -> lWidth == 1) && (p_stTexture -> lHeight == 1))
    {
        lOriginValue = *((long *)p_stTexture -> p_vBitMap)  ;
        lCompressValue = ((lOriginValue & 0x00f80000)>>3) + ((lOriginValue & 0x0000fC00)<<11) + ((lOriginValue & 0x000000f8)<<24);
        lCompressValue >>= 16;
        *((long *)p_vConvertBufferCount)  = lCompressValue;
        return;
    }
    
    for (lBitmapCounter = 0; lBitmapCounter < (unsigned long) ((p_stTexture -> lHeight * p_stTexture -> lWidth)>>1); lBitmapCounter ++)
    {
        lOriginValue = *(((long *)p_stTexture -> p_vBitMap) + lBitmapCounter*2 ) ;
        lCompressValue = ((lOriginValue & 0x00f80000)>>3) + ((lOriginValue & 0x0000fC00)<<11) + ((lOriginValue & 0x000000f8)<<24);
        lCompressValue >>= 16;
        lOriginValue = *(((long *)p_stTexture -> p_vBitMap) + lBitmapCounter*2 + 1 ) ;
        lCompressValue += ((lOriginValue & 0x00f80000)>>3) + ((lOriginValue & 0x0000fC00)<<11) + ((lOriginValue & 0x000000f8)<<24);
        *(p_vConvertBufferCount) = lCompressValue;
        p_vConvertBufferCount ++;
    }
}

/* Begin G.CLEMENT 27/07/1999 : OK now the same converter for NZ textures, so as not to mess the chromakeyed pixels */
/* Besides I shall add some comments. Nice idea, huh ? */
void GLI_vCompressNZTex565 ( GLI_tdstTexture *p_stTexture , void *GLI_gs_p_ConvertBuffer)
{
	/* Basic idea : everything works quite like the original version except that when a non-chromakey pixel
	                is converted to a chromakey pixel, we slightly change its color to get its 'non-chromakey-ness' back. */

    unsigned long lBitmapCounter,lCompressValue,lOriginValue,*p_vConvertBufferCount;
    unsigned long lOriginalKey ; /* The original Chroma Key color, needed for comparison */
	unsigned long lConvertedKey; /* The corresponding 16-bit value, so as not to recalculate it endlessly */
	unsigned long lCompressValueTemp;

    p_vConvertBufferCount = (long *)GLI_gs_p_ConvertBuffer ;

	/* I guess this part is rather useless */
    if ((p_stTexture -> lWidth == 1) && (p_stTexture -> lHeight == 1))
    {
        lOriginValue = *((long *)p_stTexture -> p_vBitMap)  ;
        lCompressValue = ((lOriginValue & 0x00f80000)>>3) + ((lOriginValue & 0x0000fC00)<<11) + ((lOriginValue & 0x000000f8)<<24);
        lCompressValue >>= 16;
        *((long *)p_vConvertBufferCount)  = lCompressValue;
        return;
    }

    /* First of all let's convert the chromakey */
	lOriginalKey = p_stTexture->lChromakeyColorRGBA;
    lConvertedKey = ((lOriginalKey & 0x00f80000)>>3) + ((lOriginalKey & 0x0000fC00)<<11) + ((lOriginalKey & 0x000000f8)<<24);

	/* Now we do as before BUT we compare the converted value to the chromakey */
    for (lBitmapCounter = 0; lBitmapCounter < (unsigned long) ((p_stTexture -> lHeight * p_stTexture -> lWidth)>>1); lBitmapCounter ++)
    {
        lOriginValue = *(((long *)p_stTexture -> p_vBitMap) + lBitmapCounter*2 ) ;
        lCompressValue = ((lOriginValue & 0x00f80000)>>3) + ((lOriginValue & 0x0000fC00)<<11) + ((lOriginValue & 0x000000f8)<<24);
		if ((lCompressValue == lConvertedKey) && (lOriginValue != lOriginalKey)) {
			/* Color was converted to chromakey but should not ! */
			lCompressValue ^= 0x00200000; /* We revert the least significant Green bit */
		}
        lCompressValue >>= 16;

        lOriginValue = *(((long *)p_stTexture -> p_vBitMap) + lBitmapCounter*2 + 1 ) ;
        lCompressValueTemp = ((lOriginValue & 0x00f80000)>>3) + ((lOriginValue & 0x0000fC00)<<11) + ((lOriginValue & 0x000000f8)<<24);
		if ((lCompressValueTemp == lConvertedKey) && (lOriginValue != lOriginalKey)) {
			/* Color was converted to chromakey but should not ! */
			lCompressValueTemp ^= 0x00200000; /* We revert the least significant Green bit */
		}
		lCompressValue += lCompressValueTemp;
        *(p_vConvertBufferCount) = lCompressValue;
        p_vConvertBufferCount ++;
    }
}
/* End G.CLEMENT 27/07/1999 */
 
/*
 ----------------------------------------------------------------------------------------
  Description   : convert 8888 bitmap to 4444 bitmap
 ----------------------------------------------------------------------------------------
 */
void GLI_vCompressTex4444( GLI_tdstTexture *p_stTexture , void *GLI_gs_p_ConvertBuffer)
{
    unsigned long lBitmapCounter,lCompressValue,lOriginValue,p_vConvertBufferCount;
 
    p_vConvertBufferCount = (long)GLI_gs_p_ConvertBuffer ;
    for (lBitmapCounter = 0; lBitmapCounter < (unsigned long) ((p_stTexture -> lWidth* p_stTexture -> lHeight)>>1); lBitmapCounter ++)
    {
        lOriginValue = *(((long *)p_stTexture -> p_vBitMap) + lBitmapCounter*2 ) ;
        lCompressValue = ((lOriginValue & 0xf0000000)>>0) + ((lOriginValue & 0x00f00000)>>4) + ((lOriginValue & 0x0000f000)<<8) + ((lOriginValue & 0x000000f0)<<20);
        lCompressValue >>= 16;
        lOriginValue = *(((long *)p_stTexture -> p_vBitMap) + lBitmapCounter*2 + 1 ) ;
        lCompressValue +=((lOriginValue & 0xf0000000)>>0) + ((lOriginValue & 0x00f00000)>>4) +((lOriginValue & 0x0000f000)<<8) + ((lOriginValue & 0x000000f0)<<20);
        *((long *)p_vConvertBufferCount)  = lCompressValue;
        p_vConvertBufferCount += 4;
    }
}
 
/*
 ----------------------------------------------------------------------------------------
  Description   : convert 8888 bitmap to 1555 bitmap
 ----------------------------------------------------------------------------------------
 */
void GLI_vCompressTex1555 ( GLI_tdstTexture *p_stTexture , void *GLI_gs_p_ConvertBuffer)
{
    unsigned long lBitmapCounter,lCompressValue,lOriginValue,p_vConvertBufferCount;
    p_vConvertBufferCount = (long)GLI_gs_p_ConvertBuffer ;
    
    if ((p_stTexture -> lWidth == 1) && (p_stTexture -> lHeight == 1))
    {
        lOriginValue = *((long *)p_stTexture -> p_vBitMap)  ;
        lCompressValue = ((lOriginValue & 0x80000000)>>0) + ((lOriginValue & 0x00f80000)>>3) + ((lOriginValue & 0x0000f800)<<10) + ((lOriginValue & 0x000000f8)<<23);
        lCompressValue >>= 16;
        *((long *)p_vConvertBufferCount) = lCompressValue;
        return;
    }
    
    for (lBitmapCounter = 0;lBitmapCounter < (unsigned long) ((p_stTexture -> lHeight * p_stTexture -> lWidth)>>1);lBitmapCounter ++)
    {
        lOriginValue = *(((long *)p_stTexture -> p_vBitMap) + lBitmapCounter*2 ) ;
        lCompressValue = ((lOriginValue & 0x80000000)>>0) + ((lOriginValue & 0x00f80000)>>3) + ((lOriginValue & 0x0000f800)<<10) + ((lOriginValue & 0x000000f8)<<23);
        lCompressValue >>= 16;
        lOriginValue = *(((long *)p_stTexture -> p_vBitMap) + lBitmapCounter*2 + 1 ) ;
        lCompressValue += ((lOriginValue & 0x80000000)>>0) + ((lOriginValue & 0x00f80000)>>3) + ((lOriginValue & 0x0000f800)<<10) + ((lOriginValue & 0x000000f8)<<23);
        *((long *)p_vConvertBufferCount)  = lCompressValue;
        p_vConvertBufferCount += 4;
    }
}
