1
0
forked from 0ad/0ad

Texture converter source, plus modified DevIL.

This was SVN commit r1851.
This commit is contained in:
Ykkrosh 2005-01-28 19:05:33 +00:00
parent f25f6a2d70
commit 60dbb779eb
3 changed files with 515 additions and 0 deletions

View File

@ -0,0 +1,334 @@
#include "IL/il.h"
#include "IL/ilu.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <assert.h>
#include <string>
#include <vector>
#include <stack>
#include <tchar.h>
#include <time.h>
//#include <crtdbg.h>
#ifdef UNICODE
#define tstring wstring
#define tstrcmp wcscmp
#define tstrrchr wcsrchr
#define tsprintf wsprintf
#define tmain wmain
#else
#define tstring string
#define tstrcmp strcmp
#define tstrrchr strrchr
#define tsprintf sprintf
#define tmain main
#endif
const TCHAR* msgbox_title = _T("Wildfire Games - Texture Converter");
enum OutputFormat { DXTn, DXT1, DXT3, DXT5, BMP, TGA, BEST };
enum trool { tr_false, tr_true, tr_maybe };
void process_args(int argc, TCHAR** argv);
void convert(std::tstring filename, OutputFormat fmt, trool alphablock);
void msg(const TCHAR* message, int icon)
{
MessageBox(NULL, message, msgbox_title, MB_OK | icon);
}
void die(const TCHAR* message)
{
msg(message, MB_ICONERROR);
exit(1);
}
int tmain(int argc, TCHAR** argv)
{
//_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(108);
if (argc <= 1)
{
msg(_T("To run the texture converter, drag-and-drop BMP files onto the Texture Converter's icon."), MB_ICONINFORMATION);
exit(0);
}
ilInit();
clock_t t=clock();
process_args(argc-1, argv+1);
t=clock()-t;
TCHAR buf[256];
tsprintf(buf, _T("Conversion complete (%.2f seconds)"), (double)t/CLOCKS_PER_SEC);
msg(buf, MB_ICONINFORMATION);
return 0;
}
void check()
{
ILenum err = ilGetError();
if (err != IL_NO_ERROR)
die(_T("DevIL error encountered"));
}
struct outputdata
{
OutputFormat fmt;
bool mipmaps;
trool alphablock;
};
void process_args(int argc, TCHAR** argv)
{
// Process arguments: Things like "-dxt5" alter the current output format
// and settings. Brackets allow scoped changes. Anything else is a filename
// to be converted.
//
// Example: "textureconv.exe a.bmp ( -dxt1 b.bmp -dxt3 c.bmp ) d.bmp"
// will use default settings for a.bmp and d.bmp, DXT1 for b.bmp, and
// DXT3 for c.bmp.
std::stack<outputdata> formats;
outputdata def = { BEST, false, tr_maybe };
formats.push(def);
for (int i = 0; i < argc; ++i)
{
#define CASE(s) else if (tstrcmp(argv[i], _T(s)) == 0)
if(0);
CASE("(")
formats.push(formats.top());
CASE(")")
{
if (formats.size() <= 1)
die(_T("Incorrect command-line parenthesis nesting"));
formats.pop();
}
CASE("-dxt1")
formats.top().fmt = DXT1;
CASE("-dxt3")
formats.top().fmt = DXT3;
CASE("-dxt5")
formats.top().fmt = DXT5;
CASE("-bmp")
formats.top().fmt = BMP;
CASE("-tga")
formats.top().fmt = TGA;
CASE("-mipmaps")
formats.top().mipmaps = true;
CASE("-nomipmaps")
formats.top().mipmaps = false;
CASE("-alphablock")
formats.top().alphablock = tr_true;
CASE("-noalphablock")
formats.top().alphablock = tr_false;
else
{
OutputFormat fmt = formats.top().fmt;
if (fmt == BEST)
{
// Convert .dds->BMP, and anything else to DDS
const TCHAR* dot = tstrrchr(argv[i], _T('.'));
if (dot && tstrcmp(dot, _T(".dds"))==0)
fmt = BMP;
else
fmt = DXTn;
}
convert(argv[i], fmt, formats.top().alphablock);
}
#undef CASE
}
}
void convert(std::tstring filename, OutputFormat fmt, trool alphablock)
{
// Generate the output .dds/etc filename:
size_t dot = filename.rfind(_T("."));
if (dot == filename.npos)
{
std::tstring msg = _T("Attempted conversion of invalid filename '") + filename + _T("' - aborting.");
die(msg.c_str());
}
const TCHAR* extn;
switch (fmt)
{
case DXTn:
case DXT1:
case DXT3:
case DXT5:
extn = _T(".dds");
break;
case BMP:
extn = _T(".bmp");
break;
case TGA:
extn = _T(".tga");
break;
default:
die(_T("Internal error - invalid output format"));
}
std::tstring filename_out = filename.substr(0, dot) + extn;
// Load the original image:
ILuint img;
ilGenImages(1, &img);
ilBindImage(img);
check();
ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
ilEnable(IL_ORIGIN_SET);
check();
if (! ilLoadImage((TCHAR*) filename.c_str()))
{
std::tstring msg = _T("Error loading file '") + filename + _T("' - aborting.");
die(msg.c_str());
}
// Make sure it's in a nice format for future processing
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
check();
ILinfo info;
iluGetImageInfo(&info);
check();
// Check for the existence of a non-solid alpha channel (so that it won't
// be stored when it has no useful data)
bool has_alpha = false;
{
if (info.Type != IL_UNSIGNED_BYTE || info.Format != IL_RGBA)
die(_T("Internal error - invalid data format"));
ILubyte* pos = ilGetData();
ILubyte* end = pos + info.Height*info.Width*info.Bpp;
for (pos += 3; pos < end; pos += info.Bpp)
{
if (*pos != 0xff)
{
has_alpha = true;
break;
}
}
}
if (fmt == DXTn || fmt == DXT1 || fmt == DXT3 || fmt == DXT5)
{
if (alphablock == tr_true || (alphablock == tr_maybe && info.Height == info.Width*2))
{
// Reading from file with special alpha mode: colour stored in top
// half, alpha in bottom half (as greyscale)
// Start/end of top half
ILubyte* start = ilGetData();
ILubyte* end = start + ((info.Width*info.Height)/2) * info.Bpp;
// Get the start/end of the colour and alpha (as greyscale) sections
ILubyte* buf_c = start + 3; // 4th byte of RGBA
ILubyte* buf_a = end;
ILubyte* end_a = end + (end-start);
// Copy greyscale's R component into colour's A
for (; buf_a < end_a; buf_c+=4, buf_a+=4)
*buf_c = *buf_a;
// Chop off the bottom of the image
iluCrop(0, 0, 0, info.Width, info.Height/2, 1);
has_alpha = true;
}
}
else if (fmt == BMP)
{
if (has_alpha)
{
// Writing to file with special alpha mode: colour stored in top
// half, alpha in bottom half (as greyscale)
// Add some space for the new image
iluImageParameter(ILU_PLACEMENT, ILU_UPPER_LEFT);
iluEnlargeCanvas(info.Width, info.Height*2, 1);
// Start/end of top half
ILubyte* start = ilGetData();
ILubyte* end = start + (info.Width*info.Height) * info.Bpp;
// Get the start/end of the colour and alpha (as greyscale) sections
ILubyte* buf_c = start + 3;
ILubyte* buf_a = end;
ILubyte* end_a = end + (end-start);
// Copy colour's A component into greyscale's RGB
for (; buf_a < end_a; buf_c+=4, buf_a+=4)
{
*buf_a = *(buf_a+1) = *(buf_a+2) = *buf_c;
*buf_c = *(buf_a+3) = 0xff; // clear the alpha channel
}
}
}
switch (fmt)
{
case DXTn:
if (has_alpha)
ilSetInteger(IL_DXTC_FORMAT, IL_DXT3);
else
ilSetInteger(IL_DXTC_FORMAT, IL_DXT1);
break;
case DXT1:
ilSetInteger(IL_DXTC_FORMAT, IL_DXT1);
break;
case DXT3:
ilSetInteger(IL_DXTC_FORMAT, IL_DXT3);
break;
case DXT5:
ilSetInteger(IL_DXTC_FORMAT, IL_DXT5);
break;
}
if (fmt == DXTn || fmt == DXT1 || fmt == DXT3 || fmt == DXT5)
{
// TODO: Do the mipmaps work better with a different scale filter?
iluBuildMipmaps();
}
ilEnable(IL_FILE_OVERWRITE);
check();
if (! ilSaveImage((TCHAR*) filename_out.c_str()))
{
std::tstring msg = _T("Error saving file '") + filename_out + _T("' - aborting.");
die(msg.c_str());
}
check();
}

View File

@ -0,0 +1,41 @@
Microsoft Visual Studio Solution File, Format Version 8.00
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "textureconv", "textureconv.vcproj", "{FFB317CC-4B72-4CA5-9CB8-86A8A0E8382E}"
ProjectSection(ProjectDependencies) = postProject
{CF979F49-FDC6-4A13-A6E7-C481DB98D856} = {CF979F49-FDC6-4A13-A6E7-C481DB98D856}
{984C31F1-11EB-4D44-ACF4-B3F573E1F08E} = {984C31F1-11EB-4D44-ACF4-B3F573E1F08E}
EndProjectSection
EndProject
Project("{EAF909A5-FA59-4C3D-9431-0FCC20D5BCF9}") = "IL", "..\..\..\libraries\devil\src\src-IL\msvc\il.icproj", "{CF979F49-FDC6-4A13-A6E7-C481DB98D856}"
ProjectSection(ProjectDependencies) = postProject
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ILU", "..\..\..\libraries\devil\src\src-ILU\msvc\ILU.vcproj", "{984C31F1-11EB-4D44-ACF4-B3F573E1F08E}"
ProjectSection(ProjectDependencies) = postProject
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfiguration) = preSolution
Debug = Debug
Release = Release
EndGlobalSection
GlobalSection(ProjectConfiguration) = postSolution
{FFB317CC-4B72-4CA5-9CB8-86A8A0E8382E}.Debug.ActiveCfg = Debug|Win32
{FFB317CC-4B72-4CA5-9CB8-86A8A0E8382E}.Debug.Build.0 = Debug|Win32
{FFB317CC-4B72-4CA5-9CB8-86A8A0E8382E}.Release.ActiveCfg = Release|Win32
{FFB317CC-4B72-4CA5-9CB8-86A8A0E8382E}.Release.Build.0 = Release|Win32
{CF979F49-FDC6-4A13-A6E7-C481DB98D856}.Debug.ActiveCfg = Debug|Win32
{CF979F49-FDC6-4A13-A6E7-C481DB98D856}.Debug.Build.0 = Debug|Win32
{CF979F49-FDC6-4A13-A6E7-C481DB98D856}.Release.ActiveCfg = Release|Win32
{CF979F49-FDC6-4A13-A6E7-C481DB98D856}.Release.Build.0 = Release|Win32
{BAC3571A-7AD2-40BD-B920-88443D73F84F}.Debug.ActiveCfg = Debug|Win32
{BAC3571A-7AD2-40BD-B920-88443D73F84F}.Release.ActiveCfg = Release|Win32
{984C31F1-11EB-4D44-ACF4-B3F573E1F08E}.Debug.ActiveCfg = Debug|Win32
{984C31F1-11EB-4D44-ACF4-B3F573E1F08E}.Debug.Build.0 = Debug|Win32
{984C31F1-11EB-4D44-ACF4-B3F573E1F08E}.Release.ActiveCfg = Release|Win32
{984C31F1-11EB-4D44-ACF4-B3F573E1F08E}.Release.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EndGlobalSection
GlobalSection(ExtensibilityAddIns) = postSolution
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,140 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="7.10"
Name="textureconv"
ProjectGUID="{FFB317CC-4B72-4CA5-9CB8-86A8A0E8382E}"
Keyword="Win32Proj">
<Platforms>
<Platform
Name="Win32"/>
</Platforms>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
IntermediateDirectory="Debug"
ConfigurationType="1"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="E:\devil\DevIL-1.6.7\include"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="TRUE"
BasicRuntimeChecks="0"
RuntimeLibrary="5"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="3"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="libcd.lib DevIL_DBG.lib DevILU_DBG.lib"
OutputFile="$(OutDir)/textureconv.exe"
LinkIncremental="2"
AdditionalLibraryDirectories="&quot;E:\devil\DevIL-1.6.7\lib&quot;;&quot;e:\icc81\compiler80\ia32\lib&quot;"
GenerateDebugInformation="TRUE"
ProgramDatabaseFile="$(OutDir)/textureconv.pdb"
SubSystem="0"
EntryPointSymbol=""
TargetMachine="1"/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
IntermediateDirectory="Release"
ConfigurationType="1"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="..\..\..\libraries\devil\src\include"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="4"
EnableFunctionLevelLinking="TRUE"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="3"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="libc.lib DevIL.lib DevILU.lib"
OutputFile="$(OutDir)/textureconv.exe"
LinkIncremental="1"
AdditionalLibraryDirectories="&quot;..\..\..\libraries\devil\src\lib&quot;;&quot;e:\icc81\compiler80\ia32\lib&quot;"
GenerateDebugInformation="FALSE"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}">
<File
RelativePath=".\main.cpp">
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}">
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}">
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>