Automatically convert texture size to nearest-power-of-two
After a week's stressed test on the 3Ds Max 9 exporter for the new engine, my code is pretty much done.
However, I really want to test my exporter with as many models as possible. After I down the models online or asking from some of my friends,
I found out that there is a serious problem about the format and size of texture. For example, the texture size is not power of two, the tga file is not RLE compressed and etc.
All these convertion done manully just tears me out. Then, I came up with a thought to programm an automatic tool in order to do these routine job for me.
The new engine only supports jpeg and RLE compressed TGA. I searched online about some code snippets and it came down that no perfect lib is smart enough or small enough to do the requirement job.
Okay, I knew that I needed to do it myself due to the specific function of the program
.
Let's nail it down! There are severial things we need to handel:
1, read/write jpeg (use jpeglib, easy!)
2, read/write RLE tga ( takes a long time to search for a good lib to handel this)
3,calculate the nearest power-of-2 (well, I got a cool function to deal with it, u really need to check)
4,Resize the image ( I wrote my own resize algorithm, which is quite stupid and fast)
5,User Interface. (U should check the interface screenshot, which looks great, I have to say... )
Considering the convertion speed, I decide to use C++ to read and write the image and use C# to write a nice interface.
Code snippet to calculate the nearest power of two:
--length; 2
length |= length >> 1;3
length |= length >> 2; 4
length |= length >> 4; 5
length |= length >> 8; 6
length |= length >> 16;7
++length;8
length = (length > 2048) ? 2048 : length; //no more than 20489
length = (length < 32) ? 32 : length; //no less than 3210
return length;It is 10 times faster than using pow(2, ceil ( log (length, 2))).
Interface Screenshot:
If you want to check about the tool, you can download it from here
Nice weekend~~~~.
浙公网安备 33010602011771号