// KJL - 21/03/2001 14:53:25
// 
// Asura_TIM2.h

#ifdef ASURA_USE_PRAGMA_ONCE
    #pragma once
#endif

#ifndef __ASURA_TIM2_H__
#define __ASURA_TIM2_H__

#include <libgraph.h>                       // For sceGsDispEnv struct.

#define ASURA_PS2_TIM2_CLUT_FORMAT_MASK (0x3f)

// Type definition for size unification

typedef u_char  TIM2_UCHAR8;
typedef u_int   TIM2_UINT32;
typedef u_short TIM2_UINT16;
typedef u_long  TIM2_UINT64;

// Definition of constants that are used in ClutType and ImageType of the picture header

enum TIM2_gattr_type {
	TIM2_NONE = 0,			// Used by ClutType when there is no CLUT data

	TIM2_RGB16,				// 16-bit color (used with both ClutType and ImageType)

	TIM2_RGB24,				// 24-bit color (used only with ImageType)

	TIM2_RGB32,				// 32-bit color (used with both ClutType and ImageType)

	TIM2_IDTEX4,			// 16-color texture (used only with ImageType)

	TIM2_IDTEX8				// 16-color texture (used only with ImageType)

};

// TIM2 file header

typedef struct {
	TIM2_UCHAR8 FileId[4];				// File ID ("T", "I", "M", "2"  or "C", "L", "T", "2")

	TIM2_UCHAR8 FormatVersion;			// File format version

	TIM2_UCHAR8 FormatId;				// Format ID

	TIM2_UINT16 Pictures;				// Number of picture data parts

	TIM2_UCHAR8 pad[8];					// 16-byte alignment

} TIM2_FILEHEADER;


// TIM2 picture header

typedef struct {
	TIM2_UINT32 TotalSize;				// Total size in bytes of all picture data

	TIM2_UINT32 ClutSize;				// Size in bytes of CLUT data part

	TIM2_UINT32 ImageSize;				// Size in bytes of image data part

	TIM2_UINT16 HeaderSize;				// Image header size

	TIM2_UINT16 ClutColors;				// CLUT size (number of colors in CLUT part)

	TIM2_UCHAR8 PictFormat;				// Image ID

	TIM2_UCHAR8 MipMapTextures;			// Number of MIPMAPs

	TIM2_UCHAR8 ClutType;				// CLUT part type

	TIM2_UCHAR8 ImageType;				// Image part type

	TIM2_UINT16 ImageWidth;				// Image width (not bit count)

	TIM2_UINT16 ImageHeight;			// Image height (not bit count)

    union
    {
        sceGsTex0 xTex0;
        u_long    uTex0;
    };

    union
    {
        sceGsTex1 xTex1;
        u_long    uTex1;
    };
    
	TIM2_UINT32 GsTexaFbaPabe;			// Combination of TEXA, FBA, and PABE bits

	TIM2_UINT32 GsTexClut;				// TEXCLUT (low-order 32 bits directly)

} TIM2_PICTUREHEADER;


// TIM2 MIPMAP header

typedef struct {
	TIM2_UINT64 GsMiptbp1;				// MIPTBP1 (64 bits directly)

	TIM2_UINT64 GsMiptbp2;				// MIPTBP2  (64 bits directly)

	TIM2_UINT32 MMImageSize[0];			// Size in bytes of image for MIPMAP number [?]

} TIM2_MIPMAPHEADER;


// TIM2 extended header

typedef struct {
	TIM2_UCHAR8 ExHeaderId[4];			// Extended comment header  ID("e", "X", "t", "\x00")

	TIM2_UINT32 UserSpaceSize;			// User space size

	TIM2_UINT32 UserDataSize;			// User data size

	TIM2_UINT32 Reserved;				// Reserved

} TIM2_EXHEADER;


// TIM2 file loader prototype declaration

bool Tim2CheckFileHeader( const void* pvFileHdr );  // Check to determine whether or not file is TIM2 file

TIM2_PICTUREHEADER *Tim2GetPictureHeader(void *p, int imgno);	// Obtain specified number picture header 

int   Tim2GetMipMapPictureSize(TIM2_PICTUREHEADER *ph,
					int mipmap, int *pWidth, int *pHeight);		// Obtain picture size for each MIPMAP level

TIM2_MIPMAPHEADER *Tim2GetMipMapHeader(TIM2_PICTUREHEADER *ph, int *pSize);
																// Obtain starting address and size of MIPMAP header

const void *Tim2GetUserSpace( const TIM2_PICTUREHEADER *ph, u_int* puSize);		// Obtain starting address and size of user space

const void *Tim2GetUserData( const TIM2_PICTUREHEADER *ph, u_int* puSize);		// Obtain starting address and size of user data

unsigned char *Tim2GetImage(TIM2_PICTUREHEADER *ph, int mipmap);			// Obtain starting address of image data of specified level

unsigned char *Tim2GetClut(TIM2_PICTUREHEADER *ph);						// Obtain starting address of CLUT data

void Tim2SetupTim2Header( TIM2_PICTUREHEADER* ph, bool bIsSwizzled );

int Tim2CalcBufWidth(int psm, int w);
int Tim2CalcBufSize(int psm, int w, int h);

int Tim2TakeSnapshot( const sceGsDispEnv* d0, const char* pszFname );	// Write snapshot image in TIM2 file on hard disk drive of host 
int Tim2TakeSnapshot( const char* pszFname, u_int tbp, u_int tbw, u_int psm, short x, short y, short w, short h );

#endif//__ASURA_TIM2_H__
