1 // Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
2 //
3 // Permission is hereby granted, free of charge, to any person
4 // obtaining a copy of this software and associated documentation
5 // files (the "Software"), to deal in the Software without
6 // restriction, including without limitation the rights to use,
7 // copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following
10 // conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 // OTHER DEALINGS IN THE SOFTWARE.
23 
24 #include "nvtt.h"
25 #include "CompressionOptions.h"
26 
27 using namespace nv;
28 using namespace nvtt;
29 
30 
31 /// Constructor. Sets compression options to the default values.
CompressionOptions()32 CompressionOptions::CompressionOptions() : m(*new CompressionOptions::Private())
33 {
34 	reset();
35 }
36 
37 
38 /// Destructor.
~CompressionOptions()39 CompressionOptions::~CompressionOptions()
40 {
41 	delete &m;
42 }
43 
44 
45 /// Set default compression options.
reset()46 void CompressionOptions::reset()
47 {
48 	m.format = Format_DXT1;
49 	m.quality = Quality_Normal;
50 	m.colorWeight.set(1.0f, 1.0f, 1.0f, 1.0f);
51 
52 	m.bitcount = 32;
53 	m.bmask = 0x000000FF;
54 	m.gmask = 0x0000FF00;
55 	m.rmask = 0x00FF0000;
56 	m.amask = 0xFF000000;
57 
58 	m.enableColorDithering = false;
59 	m.enableAlphaDithering = false;
60 	m.binaryAlpha = false;
61 	m.alphaThreshold = 127;
62 }
63 
64 
65 /// Set desired compression format.
setFormat(Format format)66 void CompressionOptions::setFormat(Format format)
67 {
68 	m.format = format;
69 }
70 
71 
72 /// Set compression quality settings.
setQuality(Quality quality)73 void CompressionOptions::setQuality(Quality quality)
74 {
75 	m.quality = quality;
76 }
77 
78 
79 /// Set the weights of each color channel.
80 /// The choice for these values is subjective. In many case uniform color weights
81 /// (1.0, 1.0, 1.0) work very well. A popular choice is to use the NTSC luma encoding
82 /// weights (0.2126, 0.7152, 0.0722), but I think that blue contributes to our
83 /// perception more than a 7%. A better choice in my opinion is (3, 4, 2).
setColorWeights(float red,float green,float blue,float alpha)84 void CompressionOptions::setColorWeights(float red, float green, float blue, float alpha/*=1.0f*/)
85 {
86 //	float total = red + green + blue;
87 //	float x = red / total;
88 //	float y = green / total;
89 //	m.colorWeight.set(x, y, 1.0f - x - y);
90 	m.colorWeight.set(red, green, blue, alpha);
91 }
92 
93 
94 /// Set color mask to describe the RGB/RGBA format.
setPixelFormat(uint bitcount,uint rmask,uint gmask,uint bmask,uint amask)95 void CompressionOptions::setPixelFormat(uint bitcount, uint rmask, uint gmask, uint bmask, uint amask)
96 {
97 	// Validate arguments.
98 	nvCheck(bitcount == 8 || bitcount == 16 || bitcount == 24 || bitcount == 32);
99 	nvCheck((rmask & gmask) == 0);
100 	nvCheck((rmask & bmask) == 0);
101 	nvCheck((rmask & amask) == 0);
102 	nvCheck((gmask & bmask) == 0);
103 	nvCheck((gmask & amask) == 0);
104 	nvCheck((bmask & amask) == 0);
105 
106 	if (bitcount != 32)
107 	{
108 		uint maxMask = (1 << bitcount);
109 		nvCheck(maxMask > rmask);
110 		nvCheck(maxMask > gmask);
111 		nvCheck(maxMask > bmask);
112 		nvCheck(maxMask > amask);
113 	}
114 
115 	m.bitcount = bitcount;
116 	m.rmask = rmask;
117 	m.gmask = gmask;
118 	m.bmask = bmask;
119 	m.amask = amask;
120 }
121 
122 /// Use external compressor.
setExternalCompressor(const char * name)123 void CompressionOptions::setExternalCompressor(const char * name)
124 {
125 	m.externalCompressor = name;
126 }
127 
128 /// Set quantization options.
129 /// @warning Do not enable dithering unless you know what you are doing. Quantization
130 /// introduces errors. It's better to let the compressor quantize the result to
131 /// minimize the error, instead of quantizing the data before handling it to
132 /// the compressor.
setQuantization(bool colorDithering,bool alphaDithering,bool binaryAlpha,int alphaThreshold)133 void CompressionOptions::setQuantization(bool colorDithering, bool alphaDithering, bool binaryAlpha, int alphaThreshold/*= 127*/)
134 {
135 	nvCheck(alphaThreshold >= 0 && alphaThreshold < 256);
136 	m.enableColorDithering = colorDithering;
137 	m.enableAlphaDithering = alphaDithering;
138 	m.binaryAlpha = binaryAlpha;
139 	m.alphaThreshold = alphaThreshold;
140 }
141 
142 
143 
144