1 #ifdef  _DEBUG
2 #define IL_DEBUG
3 #endif//_DEBUG
4 
5 #include <IL/il.h>
6 #include <windows.h>
7 #include <direct.h>
8 #include <string>
9 using namespace std;
10 
11 TCHAR *ImageExtArray[] =
12 {
13 	L"jpe", L"jpg", L"jpeg",
14 	L"bmp",
15 	L"ico",
16 	L"pbm", L"pgm", L"pnm", L"ppm",
17 	L"png",
18 	L"bw", L"rgb", L"rgba", L"sgi",
19 	L"tga", L"tif", L"tiff",
20 	L"pcx",
21 	NULL
22 };
23 
24 
25 void	ParseDirs(const string &_Dir, char **ExtList, char *ConvExt, bool Recurse);
26 bool	IsDir(WIN32_FIND_DATA *_Data);
27 char	*GetExtension(const char *FileName);
28 bool	CheckExtension(char *Arg, char *Ext);
29 
30 
31 TCHAR	*Ext;
32 string	NewExt;
33 int		i, j;
34 
35 
36 //void BatchConv(TCHAR *Directory, TCHAR *ExtList, TCHAR *ConvExt, bool Recurse)
37 //{
38 //	ILuint Id, OrigId;
39 //	ilGenImages(1, &Id);
40 //	OrigId = ilGetInteger(IL_CUR_IMAGE);
41 //	ilBindImage(Id);
42 //	if (ExtList == NULL)
43 //		ParseDirs(string(Directory), ImageExtArray, ConvExt, Recurse);
44 //	else {
45 //		/*char **List = ConvertExtList(ExtList);
46 //		ParseDirs(string(Directory), ConvertExtList(ExtList), ConvExt, Recurse);
47 //		DestroyExtList(List);*/
48 //	}
49 //	ilDeleteImages(1, &Id);
50 //	ilBindImage(OrigId);
51 //	return;
52 //}
53 //
54 //
55 //void ParseDirs(const string &_Dir, TCHAR **ExtList, TCHAR *ConvExt, bool Recurse)
56 //{
57 //	HANDLE			Search;
58 //	WIN32_FIND_DATA	FindData;
59 //
60 //	_chdir(_Dir.c_str());
61 //	Search = FindFirstFile("*.*", &FindData);
62 //
63 //	do {
64 //		if (!strcmp(FindData.cFileName, ".") || !strcmp(FindData.cFileName, ".."))
65 //			continue;
66 //		if (IsDir(&FindData) && Recurse) {
67 //			_chdir(FindData.cFileName);
68 //			string NewDir = _Dir + string("\\");
69 //			NewDir += FindData.cFileName;
70 //			ParseDirs(NewDir, ExtList, ConvExt, Recurse);
71 //			_chdir("..");
72 //		}
73 //		Ext = GetExtension(FindData.cFileName);
74 //		if (Ext == NULL)
75 //			continue;
76 //		if (!_stricmp(Ext, ConvExt))  // Already has that extension.
77 //			continue;
78 //		for (j = 0; ExtList[j] != NULL; j++) {
79 //			if (CheckExtension(FindData.cFileName, ExtList[j])) {
80 //				string NewName;
81 //				for (i = 0; i < Ext - FindData.cFileName; i++) {
82 //					NewName += FindData.cFileName[i];
83 //				}
84 //				NewName += ConvExt;
85 //				if (!ilLoadImage(FindData.cFileName))
86 //					break;
87 //				ilSaveImage((TCHAR*)NewName.c_str());
88 //				break;
89 //			}
90 //		}
91 //	} while (FindNextFile(Search, &FindData));
92 //
93 //	FindClose(Search);
94 //	return;
95 //}
96 //
97 //
98 //// Is the file actually a directory?
99 //bool IsDir(WIN32_FIND_DATA *_Data)
100 //{
101 //	if (_Data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
102 //		return true;
103 //	return false;
104 //}
105 
106 
GetExtension(const TCHAR * FileName)107 TCHAR *GetExtension(const TCHAR *FileName)
108 {
109 	bool PeriodFound = false;
110 	TCHAR *Ext = (TCHAR*)FileName;
111 	long i, Len = (long)wcslen(FileName);
112 
113 	if (FileName == NULL || !Len)  // if not a good filename/extension, exit early
114 		return NULL;
115 
116 	Ext += Len;  // start at the end
117 
118 	for (i = Len; i >= 0; i--) {
119 		if (*Ext == '.') {  // try to find a period
120 			PeriodFound = true;
121 			break;
122 		}
123 		Ext--;
124 	}
125 
126 	if (!PeriodFound)  // if no period, no extension
127 		return NULL;
128 
129 	return Ext+1;
130 }
131 
132 
133 // Simple function to test if a filename has a given extension, disregarding case
CheckExtension(TCHAR * Arg,TCHAR * Ext)134 bool CheckExtension(TCHAR *Arg, TCHAR *Ext)
135 {
136 	bool	PeriodFound = false;
137 	TCHAR	*Argu = Arg;  // pointer to arg so we don't destroy arg
138 	unsigned int i;
139 
140 	if (Arg == NULL || Ext == NULL || !wcslen(Arg) || !wcslen(Ext))  // if not a good filename/extension, exit early
141 		return false;
142 
143 	Argu += wcslen(Arg);  // start at the end
144 
145 
146 	for (i = (int)wcslen(Arg); i >= 0; i--) {
147 		if (*Argu == '.') {  // try to find a period
148 			PeriodFound = true;
149 			break;
150 		}
151 		Argu--;
152 	}
153 
154 	if (!PeriodFound)  // if no period, no extension
155 		return false;
156 
157 	if (!_wcsicmp(Argu+1, Ext))  // extension and ext match?
158 		return true;
159 
160 	return false;  // if all else fails, return IL_FALSE
161 }
162 
163