//-------------------------------------------------------------------------------------------------
// PSD → TIFF 変換プログラム
//-------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <windows.h>
#include "FreeImage.h"

using namespace std;

//-------------------------------------------------------------------------------------------------
// メイン関数
//-------------------------------------------------------------------------------------------------
int _tmain ( int argc, _TCHAR* argv[] )
{
	//-------------------------------
	// 引数のチェック
	//-------------------------------
	if ( argc < 3 )
	{
		printf ( "usage : Tiff_2T [infile(psd)] [outfile(tif)]\n" );
		return ( 1 );
	}

	//-------------------------------
	// ファイル変換
	//-------------------------------
	// PSDファイルロード
	FIBITMAP *bitmap = FreeImage_Load(FIF_PSD, (const char*)argv[1], PSD_DEFAULT);
	if (bitmap == NULL) {
		return ( 1 );
	}

	// 2値化変換
	FIBITMAP *bitmap_mono =  FreeImage_Threshold(bitmap, 192);
	if ( bitmap_mono == NULL ) {
		return ( 1 );
	}

	// Windows7の場合、この処理は不要(白黒反転してしまうため)
	// 色の反転確認
	FREE_IMAGE_COLOR_TYPE fiColor = FreeImage_GetColorType(bitmap_mono);
	if (fiColor == FIC_MINISBLACK) {
		FreeImage_Invert(bitmap_mono);
	}

	// TIFFファイル保存
	if (!FreeImage_Save(FIF_TIFF, bitmap_mono, (const char*)argv[2], TIFF_CCITTFAX4)) {
		return ( 1 );
	}

	// 解放
	FreeImage_Unload(bitmap);
	FreeImage_Unload(bitmap_mono);
	return ( 0 );
}