// ImageSpliterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ImageSpliter.h"
#include "ImageSpliterDlg.h"
#include "string.h"
#include "..\..\..\Libraries\ImageLib\Image.h"
#include "ProgDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif




/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageSpliterDlg dialog

CImageSpliterDlg::CImageSpliterDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CImageSpliterDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CImageSpliterDlg)
	m_ImgName = _T("");
	m_OBase = _T("");
	m_tardir = _T("");
	m_fimgx = 0;
	m_fimgy = 0;
	m_OutFormat = -1;
	m_RevOrder = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CImageSpliterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CImageSpliterDlg)
	DDX_Text(pDX, IDC_IMGNAME, m_ImgName);
	DDX_Text(pDX, IDC_OBASE, m_OBase);
	DDX_Text(pDX, IDC_TARDIR, m_tardir);
	DDX_Text(pDX, IDC_FIMAGEX, m_fimgx);
	DDV_MinMaxInt(pDX, m_fimgx, 0, 999999999);
	DDX_Text(pDX, IDC_FIMAGEY, m_fimgy);
	DDV_MinMaxInt(pDX, m_fimgy, 0, 999999999);
	DDX_CBIndex(pDX, IDC_OUTFORMAT, m_OutFormat);
	DDX_Check(pDX, IDC_RORDER, m_RevOrder);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CImageSpliterDlg, CDialog)
	//{{AFX_MSG_MAP(CImageSpliterDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_BROWSEDIR, OnBrowsedir)
	ON_WM_CLOSE()
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_GO, OnGo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageSpliterDlg message handlers

BOOL CImageSpliterDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	char dname[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,dname);
	m_tardir=dname;
	m_fimgx=m_fimgy=256;
	m_OutFormat=1;
	UpdateData(FALSE);
CheckDlgButton(IDC_NUMBERS,1);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CImageSpliterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CImageSpliterDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CImageSpliterDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CImageSpliterDlg::OnBrowse() 
{
UpdateData(TRUE);

CFileDialog fdlg(TRUE,NULL,NULL,NULL,"Bitmap Images (*.tif,*.gif,*.tga,*.png)|*.tif;*.gif;*.tga;*.png||",NULL);	
if(fdlg.DoModal()==IDOK) 
	{
	m_ImgName=fdlg.GetPathName();	
	m_OBase=fdlg.GetFileName();
	m_OBase=m_OBase.Left(m_OBase.GetLength()-4);
	UpdateData(FALSE);

	}


}




//
// Setup the GameOS structure
//
void __stdcall GetGameOSEnvironment( char* CommandLine )
{
}

void __stdcall ILFree(LPITEMIDLIST pidl); 

void CImageSpliterDlg::OnBrowsedir() 
{

	char pname[MAX_PATH];
	BROWSEINFO bi; 
		bi.hwndOwner =m_hWnd; 
	
	bi.pidlRoot = NULL; 
	bi.pszDisplayName = NULL; 
	bi.lpszTitle = "Choose Target Path..."; 
	bi.ulFlags = BIF_RETURNONLYFSDIRS; 
	bi.lpfn = NULL; 
	bi.lParam = (LPARAM)this; 
	
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi); 
	
	if (SHGetPathFromIDList(pidl,pname))  
	{
		m_tardir=pname;
		UpdateData(FALSE);
	}
	
	if (bi.pidlRoot) 
	{ 
		ILFree( const_cast< ITEMIDLIST* >( bi.pidlRoot ) );
	} 
	
}

void __stdcall ILFree(LPITEMIDLIST pidl) 
{ 
	LPMALLOC pMalloc; 
	
    if (pidl) 
    { 
#ifdef DEBUG 
		UINT cbSize = ILGetSize(pidl); 
		VALIDATE_PIDL(pidl); 
		
		// Fill the memory with some bad value... 
		_fmemset(pidl, 0xE5, cbSize); 
		
		// If large enough try to save the call return address of who 
		// freed us in the 3-6 byte of the structure. 
		if (cbSize >= 6) 
			*((UINT*)((LPSTR)pidl + 2)) =  *(((UINT*)&pidl) - 1); 
		
#endif 
		SHGetMalloc(&pMalloc); 
		pMalloc->Free( pidl); 
		pMalloc->Release(); 
		
    } 
} 

void CImageSpliterDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnClose();
}

void CImageSpliterDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	UpdateData(TRUE);
	
//	m_eper.Format("%i%%",m_edist);

	UpdateData(FALSE);
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}


void CImageSpliterDlg::OnGo() 
{
Image SourceImg,DestImg;
CString str;
CProgressDlg pdlg;
UpdateData(TRUE);

pdlg.Create();

SourceImg.Load((char *)(LPCSTR)m_ImgName);
DestImg.CreateBlank(m_fimgx,m_fimgy,(ITYPE)SourceImg.GetIType(),SourceImg.GetBpp());

CRect dst,src;

dst.top=dst.left=0;
dst.bottom=m_fimgy;
dst.right=m_fimgx;

int img,imgtot,x,y,nx,ny;
img=0;
imgtot=(SourceImg.GetHeight()/m_fimgy)*(SourceImg.GetWidth()/m_fimgx);
for(y=0;y<SourceImg.GetHeight()/m_fimgy;y++)
	for(x=0;x<SourceImg.GetWidth()/m_fimgx;x++)
	{
	src.top=y*m_fimgy;
	src.left=x*m_fimgx;
	src.bottom=src.top+m_fimgy;
	src.right=src.left+m_fimgx;
	DestImg.Blt(SourceImg,dst,src);

	if(m_RevOrder) 
	{
		ny=((SourceImg.GetHeight()/m_fimgy)-y)-1;
		nx=((SourceImg.GetWidth()/m_fimgx)-x)-1;
	}
	else
	{
	nx=x;
	ny=y;
	}

	CString str;
	if(IsDlgButtonChecked(IDC_NUMBERS))
	{
	switch(m_OutFormat)
	{
	case 0:
		str.Format("%s\\%s(%03i,%03i).Tga",m_tardir,m_OBase,ny,nx);
		DestImg.SaveTga((char *)(LPCSTR)str);
		break;
	case 1:
		str.Format("%s\\%s(%03i,%03i).Tif",m_tardir,m_OBase,ny,nx);
		DestImg.SaveTiff((char *)(LPCSTR)str);
		break;
	case 2:
		str.Format("%s\\%s(%03i,%03i).Raw",m_tardir,m_OBase,ny,nx);
		DestImg.SaveRaw((char *)(LPCSTR)str);
		break;
	case 3:
		str.Format("%s\\%s(%03i,%03i).Png",m_tardir,m_OBase,ny,nx);
		DestImg.SavePng((char *)(LPCSTR)str);
		break;
	}
	}
else
	{
	switch(m_OutFormat)
	{
	case 0:
		str.Format("%s\\%s%c%c.Tga",m_tardir,m_OBase,'A'+ny,'A'+nx);
		DestImg.SaveTga((char *)(LPCSTR)str);
		break;
	case 1:
		str.Format("%s\\%s%c%c.Tif",m_tardir,m_OBase,'A'+ny,'A'+nx);
		DestImg.SaveTiff((char *)(LPCSTR)str);
		break;
	case 2:
		str.Format("%s\\%s%c%c.Raw",m_tardir,m_OBase,'A'+ny,'A'+nx);
		DestImg.SaveRaw((char *)(LPCSTR)str);
		break;
	case 3:
		str.Format("%s\\%s%c%c.Png",m_tardir,m_OBase,'A'+ny,'A'+nx);
		DestImg.SavePng((char *)(LPCSTR)str);
		break;
	}
	}
	
	img++;
	pdlg.SetPos((img*100)/imgtot);
	}
	
}
