#if !defined(PerceptualROI_H)
#define PerceptualROI_H

// The file is written by Zhongkang.

#include <stdio.h>
#include <stdlib.h>
#include "H264AVCCommonLib/Cfmo.h"

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define	MAX_PROI_NUMBER			8
#define	MAX_PROI_SLICE_NUMBER	63

struct ROIDescriptionUnit
{ 
  int	m_iLeftTopX, m_iLeftTopY, m_iWidth, m_iHeight;
};

class PerceptualROI
{
public:
  PerceptualROI():
  m_uiPerceptualROI(NULL),
  m_fImportanceValue(NULL),
  m_uiGradientWidth(NULL),
  m_ucSliceFlag(NULL),
  m_ucSliceFlagIndex(NULL),
  m_uiIndexSize(NULL),
  m_uiMBList(NULL),
  m_fImportance(NULL) {};

  virtual ~PerceptualROI();

  static ErrVal create( PerceptualROI*& rpcPerceptualROI );
  ErrVal init();
  ErrVal uninit();
  ErrVal destroy();

  UInt					IsROIChanged( UInt RefFrameIndex, UInt CurFrameIndex );

  ErrVal				xReadPROIFromFile( std::string ROIFileName, UInt uiFrameNumber, UInt uiFrameWidth, UInt uiFrameHeight );
  ErrVal				PROI_to_MBSliceIndex( UInt uiFrameIndex );

  int					getFrameWidthinMB() { return m_uiFrameWidthinMB; }
  int					getFrameHeightinMB() { return m_uiFrameHeightinMB; }

  ErrVal				InitializeFMO( h264::FMO *pcFMO );
  ErrVal				PROI_to_FMO( h264::FMO *pcFMO, UInt uiFrameIndex );

  unsigned char*		getSliceFlag( UInt uiFrameIndex );
  UInt*					getMBList( UInt uiFrameIndex );
  float*				getImportance( UInt uiFrameIndex );

  UInt					getNumberOfSlices( UInt uiFrameIndex );
  UInt					getNumberOfPROISliceGroups( UInt uiFrameIndex );

private:

  ErrVal				xReadLine( FILE* hFile, Char* pcFormat, Void* pvP0 );
  ErrVal				xReadLine( FILE* hFile, Char* pcFormat, Void* pvP0, Void* pvP1 );

  ErrVal				IsInteger( char *inString );
  Int					FindSymbol( char *inString, char *targetString, int beginningPosition );
  ErrVal				SliceFlag2SliceIndex( unsigned char *t_ucSliceFlag, UInt uiMBlistSize, UInt *uiMBList, unsigned char *t_ucSliceFlagIndex, UInt &t_IndexSize );

  ErrVal				SortROIbyPosition( UInt uiFrameIndex, UInt *uiTopLeft, UInt *uiBottomRight );

  unsigned char			*m_ucSliceFlag;
  unsigned char			*m_ucSliceFlagIndex;
  UInt					*m_uiMBList;
  float					*m_fImportance;
  UInt					*m_uiIndexSize;
  UInt					m_uiFrameWidthinMB, m_uiFrameHeightinMB;

  UInt					m_uiPROINumber;
  UInt					m_uiFrameNumber;
  ROIDescriptionUnit	**m_uiPerceptualROI;
  float					*m_fImportanceValue;
  UInt					*m_uiGradientWidth;
};

#endif

