#ifndef __IMG_COMMON_20151214__
#define __IMG_COMMON_20151214__

#pragma once

//#include "gdi.h"
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")

// WNetAddConnection的使用;
#include <Winnetwk.h>
#pragma comment(lib,"Mpr.lib")

#include <string>
using namespace std;

#ifndef _UNICODE
typedef string TString;
#else
typedef wstring TString;
#endif

enum	// 缩放结果;
{
	ZoomNull = 0,		// 无缩放;
	ZoomIn = 1,			// 缩小;
	ZoomOut = 2			// 放大;
};

#define GET_XPIX(x)			( x >> 16)				// 高16位;
#define GET_YPIX(y)			(y & 0x0000ffff)		// 低16位;
#define SET_PIX(x,y)		(( x << 16) | y)		// 高低合并;
#define INVALID_COPY_PIX	((DWORD)-1)				// 传递此值,表示没有复制压缩;

#if 0
#define MAKEWORD(a, b)      ((WORD)(((BYTE)(((DWORD_PTR)(a)) & 0xff)) | ((WORD)((BYTE)(((DWORD_PTR)(b)) & 0xff))) << 8))
#define MAKELONG(a, b)      ((LONG)(((WORD)(((DWORD_PTR)(a)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(b)) & 0xffff))) << 16))
#define LOWORD(l)           ((WORD)(((DWORD_PTR)(l)) & 0xffff))
#define HIWORD(l)           ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
#define LOBYTE(w)           ((BYTE)(((DWORD_PTR)(w)) & 0xff))
#define HIBYTE(w)           ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff))
#endif

class IMGCommon
{
public:
	IMGCommon(void);
	~IMGCommon(void);

	static ULONG_PTR m_gdiplusToken;

public:
	static BOOL IsValidpath(IN CONST TString &strPath);
	static void Splitpath(IN CONST TCHAR *path, IN TCHAR * drive, IN size_t driveNumberOfElements, IN TCHAR * dir, IN size_t dirNumberOfElements, IN CONST int &nTimes);
	static BOOL CreateDirectoryEx(IN LPCTSTR lpNewDirectory);
	//////////////////////////////////////////////////////////////////////////
	static BOOL LoadImgFromFile(IN Image** pImg, LPCTSTR lpPath);
	static BOOL LoadImgFromBuffer(IN Image** pImg, IN BYTE* pBuffer, IN CONST INT& nBufLen);
	static BOOL LoadImgFromBuffer(IN Image** pImg, LPCTSTR lpPath);
	static Image* LoadImgFromResource( IN HMODULE hModule, IN LPCTSTR lpName, IN LPCTSTR lpType);
	//////////////////////////////////////////////////////////////////////////
	static BOOL GetOrientation(IN Image *pImg);
	static int GetEncoderClsid(IN CONST WCHAR* format, OUT CLSID* pClsid);
	static BOOL SaveImg2newfile(IN Image* pImg, IN CString strNewfile, IN ULONG uQuality);
	static BOOL SaveImg2newfile(IN Image* pImg, IN TString strNewfile, IN ULONG uQuality);
	static int ZoomImg(IN CRect &Imgrc, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix);
	static int ZoomImg(IN CONST DWORD &dwImgPix, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix);
	static BOOL ImgThumbnail(IN Image* pImg, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality);
	static BOOL ImgThumbnail(IN LPCTSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality);
	static BOOL ImgThumbnail(IN CONST TString &strImgpath, IN CONST DWORD &dwDisplayPix, IN TString strNewfile, IN ULONG uQuality);
	static BOOL SimpleImgThumbnail(IN LPCTSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality);
	// 给指定的图片加图片水印(位置、大小暂不支持);
	static Image* AppendImgWatermark(IN Image* pImg, IN Image* pWatemarkImg);
	static Image* AppendImgWatermark(IN Image* pImg, IN LPCTSTR lpWatemarkImgpath);
	static Image* AppendImgWatermark(IN LPCTSTR lpImgpath, IN Image* pWatemarkImg);
	static Image* AppendImgWatermark(IN LPCTSTR lpImgpath, IN LPCTSTR lpWatemarkImgpath);
	static BOOL AppendImgWatermark(IN Image* pImg, IN Image* pWatemarkImg, IN LPCTSTR lpSaveImgpath);
	static BOOL AppendImgWatermark(IN Image* pImg, IN LPCTSTR lpWatemarkImgpath, IN LPCTSTR lpSaveImgpath);
	static BOOL AppendImgWatermark(IN LPCTSTR lpImgpath, IN LPCTSTR lpWatemarkImgpath, IN LPCTSTR lpSaveImgpath);
	// 给指定图片加文字水印(位置、字体、颜色暂不支持);
	static Image* AppendWordWatermark(IN Image* pImg, IN LPCTSTR lpWatemarkString);
	static Image* AppendWordWatermark(IN LPCTSTR lpImgpath, IN LPCTSTR lpWatemarkString);
	static BOOL SaveBitmap(IN CDC *pDC, IN CBitmap* pBitmap, IN CONST INT& nBitdepth, IN const INT& nWidth, IN const INT& nHeight, IN LPCTSTR lpSavepath);
	static BOOL SaveBitmap2(IN CDC *pDC, IN CBitmap* pBitmap, IN CONST INT& nBitdepth, IN const INT& nWidth, IN const INT& nHeight, IN LPCTSTR lpSavepath);
	static void DrawString(IN CDC* pdc, IN CONST INT& nFontSize, IN LPCTSTR lpFontName, IN LPCTSTR lpString, IN OUT CRect &rcString);
};

#endif