File: decompress.cpp

package info (click to toggle)
nvidia-texture-tools 2.1.2~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,676 kB
  • sloc: cpp: 50,344; ansic: 10,439; sh: 62; makefile: 15
file content (247 lines) | stat: -rw-r--r-- 6,492 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// 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 <nvtt/nvtt.h>

#include "cmdline.h"

#include <time.h> // clock

int main(int argc, char *argv[])
{
	MyAssertHandler assertHandler;
	MyMessageHandler messageHandler;

	bool forcenormal = false;
	bool mipmaps = false;
	bool faces = false;
	bool savePNG = false;
    bool rgbm = false;
    bool histogram = false;

	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("-rgbm", argv[i]) == 0)
		{
			rgbm = true;
		}
		else if (strcmp("-faces", argv[i]) == 0)
		{
			faces = true;
		}
		else if (strcmp("-histogram", argv[i]) == 0)
		{
            histogram = true;
        }
		else if (strcmp("-format", argv[i]) == 0)
		{
			if (i+1 == argc) break;
			i++;

			// !!!UNDONE: Support at least one HDR output format

#ifdef HAVE_PNG
			if (strcmp("png", argv[i]) == 0) savePNG = true;
			else 
#endif
			if (strcmp("tga", argv[i]) == 0) savePNG = false;
			else
			{
				fprintf(stderr, "Unsupported output format '%s', defaulting to 'tga'.\n", argv[i]);
				savePNG = false;
			}
		}
		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;
		}
		else
		{
			printf("Warning: unrecognized option \"%s\"\n", argv[i]);
		}
	}
	
	printf("NVIDIA Texture Tools - Copyright NVIDIA Corporation 2007\n\n");

	if (input.isNull())
	{
		printf("usage: nvdecompress [options] infile.dds [outfile]\n\n");

		printf("Note: the .tga or .png extension is forced on outfile\n\n");

		printf("Input options:\n");
		printf("  -forcenormal      The input image is a normal map.\n");
		printf("  -mipmaps          Decompress all mipmaps.\n");
		printf("  -faces            Decompress all faces.\n");
        printf("  -histogram        Output histogram.\n");
		printf("  -format <format>  Output format ('tga' or 'png').\n");

 		return 1;
 	}


    if (histogram) {
        nvtt::Surface img;
        if (!img.load(input.str())) {
		    fprintf(stderr, "The file '%s' is not a valid DDS file.\n", input.str());
		    return 1;
        }

        float exposure = 2.2f;
        float scale = 1.0f / exposure;
        img.scaleBias(0, scale, 0);
        img.scaleBias(1, scale, 0);
        img.scaleBias(2, scale, 0);

        //img.toneMap(nvtt::ToneMapper_Reindhart, NULL);
        //img.toSrgb();
        img.toGamma(2.2f);

        nvtt::Surface hist = nvtt::histogram(img, 3*512, 128);

        // Resize for pretier histograms.
        hist.resize(512, 128, 1, nvtt::ResizeFilter_Box);

        nv::Path name;
        name.copy(output);
        name.stripExtension();
        name.append(".histogram");
        name.append(savePNG ? ".png" : ".tga");

        hist.save(name.str());
    }
    else {

	    // Load surface.
	    // !!! DirectDrawSurface API doesn't support float images, so BC6 will be converted to 8-bit on load.
	    // Should use nvtt::Surface instead.
        nv::DirectDrawSurface dds;
        if (!dds.load(input.str()) || !dds.isValid())
	    {
		    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());
		    return 1;
	    }
    	
	    uint faceCount;
	    if (dds.isTexture2D())
	    {
		    faceCount = 1;
	    }
	    else
	    {
		    nvCheck(dds.isTextureCube());
		    faceCount = 6;
	    }
    	
	    uint mipmapCount = dds.mipmapCount();
    	
	    clock_t start = clock();
     
	    // apply arguments
	    if (forcenormal)
	    {
		    dds.setNormalFlag(true);
	    }
	    if (!faces)
	    {
		    faceCount = 1;
	    }
	    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++)
		    {
                if (!imageFromDDS(&mipmap, dds, f, m))
                    continue;
    	
			    // 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);
			    name.append(savePNG ? ".png" : ".tga");
    			
			    nv::StdOutputStream stream(name.str());
			    if (stream.isError()) {
				    fprintf(stderr, "Error opening '%s' for writing\n", name.str());
				    return 1;
			    }
    			
			    nv::ImageIO::save(name.str(), stream, &mipmap);
		    }
	    }

	    clock_t end = clock();
	    printf("\rtime taken: %.3f seconds\n", float(end-start) / CLOCKS_PER_SEC);
    }
	
	return 0;
}