2007-06-21 08:19:30 +00:00
|
|
|
// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person
|
|
|
|
// obtaining a copy of this software and associated documentation
|
|
|
|
// files (the "Software"), to deal in the Software without
|
|
|
|
// restriction, including without limitation the rights to use,
|
|
|
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following
|
|
|
|
// conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#include <nvcore/StrLib.h>
|
|
|
|
#include <nvcore/StdStream.h>
|
|
|
|
|
|
|
|
#include <nvimage/Image.h>
|
|
|
|
#include <nvimage/DirectDrawSurface.h>
|
|
|
|
|
|
|
|
#include <nvimage/ImageIO.h>
|
|
|
|
|
|
|
|
#include "cmdline.h"
|
|
|
|
|
2008-04-11 22:03:42 +00:00
|
|
|
#include <time.h> // clock
|
|
|
|
|
2007-06-21 08:19:30 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
MyAssertHandler assertHandler;
|
|
|
|
MyMessageHandler messageHandler;
|
|
|
|
|
2008-04-11 22:03:42 +00:00
|
|
|
bool forcenormal = false;
|
|
|
|
bool mipmaps = false;
|
|
|
|
bool faces = false;
|
2009-02-03 09:08:39 +00:00
|
|
|
bool savePNG = false;
|
2008-04-11 22:03:42 +00:00
|
|
|
|
|
|
|
nv::Path input;
|
|
|
|
nv::Path output;
|
|
|
|
|
|
|
|
// Parse arguments.
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (strcmp("-forcenormal", argv[i]) == 0)
|
|
|
|
{
|
|
|
|
forcenormal = true;
|
|
|
|
}
|
|
|
|
else if (strcmp("-mipmaps", argv[i]) == 0)
|
|
|
|
{
|
|
|
|
mipmaps = true;
|
|
|
|
}
|
|
|
|
else if (strcmp("-faces", argv[i]) == 0)
|
|
|
|
{
|
|
|
|
faces = true;
|
|
|
|
}
|
2009-02-03 09:08:39 +00:00
|
|
|
else if (strcmp("-format", argv[i]) == 0)
|
|
|
|
{
|
|
|
|
if (i+1 == argc) break;
|
|
|
|
i++;
|
|
|
|
|
2009-03-02 09:21:07 +00:00
|
|
|
#ifdef HAVE_PNG
|
2009-02-03 09:08:39 +00:00
|
|
|
if (strcmp("png", argv[i]) == 0) savePNG = true;
|
2009-03-02 09:21:07 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (strcmp("tga", argv[i]) == 0) savePNG = false;
|
2009-02-03 09:08:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unsupported output format '%s', defaulting to 'tga'.\n", argv[i]);
|
|
|
|
savePNG = false;
|
|
|
|
}
|
|
|
|
}
|
2008-04-11 22:03:42 +00:00
|
|
|
else if (argv[i][0] != '-')
|
|
|
|
{
|
|
|
|
input = argv[i];
|
|
|
|
|
|
|
|
if (i+1 < argc && argv[i+1][0] != '-')
|
|
|
|
{
|
|
|
|
output = argv[i+1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output.copy(input.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2007-06-21 08:19:30 +00:00
|
|
|
}
|
2008-04-11 22:03:42 +00:00
|
|
|
|
|
|
|
printf("NVIDIA Texture Tools - Copyright NVIDIA Corporation 2007\n\n");
|
2007-06-21 08:19:30 +00:00
|
|
|
|
2008-04-11 22:03:42 +00:00
|
|
|
if (input.isNull())
|
|
|
|
{
|
|
|
|
printf("usage: nvdecompress [options] infile [outfile]\n\n");
|
|
|
|
|
2009-02-03 09:08:39 +00:00
|
|
|
printf("Note: the .tga or .png extension is forced on outfile\n\n");
|
2008-04-11 22:03:42 +00:00
|
|
|
|
|
|
|
printf("Input options:\n");
|
2009-02-03 09:08:39 +00:00
|
|
|
printf(" -forcenormal \tThe input image is a normal map.\n");
|
|
|
|
printf(" -mipmaps \tDecompress all mipmaps.\n");
|
|
|
|
printf(" -faces \tDecompress all faces.\n");
|
|
|
|
printf(" -format <format>\tOutput format ('tga' or 'png').\n");
|
2008-04-11 22:03:42 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2009-02-03 09:08:39 +00:00
|
|
|
|
2007-06-21 08:19:30 +00:00
|
|
|
// Load surface.
|
2010-08-31 01:39:08 +00:00
|
|
|
nv::DirectDrawSurface dds(input.str());
|
2007-06-21 08:19:30 +00:00
|
|
|
if (!dds.isValid())
|
|
|
|
{
|
2008-04-11 22:03:42 +00:00
|
|
|
fprintf(stderr, "The file '%s' is not a valid DDS file.\n", input.str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dds.isSupported() || dds.isTexture3D())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "The file '%s' is not a supported DDS file.\n", input.str());
|
2007-06-21 08:19:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-04-11 22:03:42 +00:00
|
|
|
uint faceCount;
|
|
|
|
if (dds.isTexture2D())
|
|
|
|
{
|
|
|
|
faceCount = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nvCheck(dds.isTextureCube());
|
|
|
|
faceCount = 6;
|
|
|
|
}
|
2007-06-21 08:19:30 +00:00
|
|
|
|
2008-04-11 22:03:42 +00:00
|
|
|
uint mipmapCount = dds.mipmapCount();
|
|
|
|
|
|
|
|
clock_t start = clock();
|
|
|
|
|
|
|
|
// apply arguments
|
|
|
|
if (forcenormal)
|
|
|
|
{
|
|
|
|
dds.setNormalFlag(true);
|
|
|
|
}
|
|
|
|
if (!faces)
|
|
|
|
{
|
|
|
|
faceCount = 1;
|
2007-06-21 08:19:30 +00:00
|
|
|
}
|
2008-04-11 22:03:42 +00:00
|
|
|
if (!mipmaps)
|
|
|
|
{
|
|
|
|
mipmapCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nv::Image mipmap;
|
|
|
|
nv::Path name;
|
|
|
|
|
|
|
|
// strip extension, we force the tga extension
|
|
|
|
output.stripExtension();
|
|
|
|
|
|
|
|
// extract faces and mipmaps
|
|
|
|
for (uint f = 0; f < faceCount; f++)
|
|
|
|
{
|
|
|
|
for (uint m = 0; m < mipmapCount; m++)
|
|
|
|
{
|
|
|
|
dds.mipmap(&mipmap, f, m);
|
2007-06-21 08:19:30 +00:00
|
|
|
|
2008-04-11 22:03:42 +00:00
|
|
|
// set output filename, if we are doing faces and/or mipmaps
|
|
|
|
name.copy(output);
|
|
|
|
if (faces) name.appendFormat("_face%d", f);
|
|
|
|
if (mipmaps) name.appendFormat("_mipmap%d", m);
|
2009-02-03 09:08:39 +00:00
|
|
|
name.append(savePNG ? ".png" : ".tga");
|
2008-04-11 22:03:42 +00:00
|
|
|
|
|
|
|
nv::StdOutputStream stream(name.str());
|
|
|
|
if (stream.isError()) {
|
|
|
|
fprintf(stderr, "Error opening '%s' for writting\n", name.str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-31 01:39:08 +00:00
|
|
|
nv::ImageIO::save(name.str(), stream, &mipmap);
|
2008-04-11 22:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-21 08:19:30 +00:00
|
|
|
|
2008-04-11 22:03:42 +00:00
|
|
|
clock_t end = clock();
|
|
|
|
printf("\rtime taken: %.3f seconds\n", float(end-start) / CLOCKS_PER_SEC);
|
|
|
|
|
2007-06-21 08:19:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|