BatchConv.cpp
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 4k
Development Platform:

Visual C++

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