RampGenerator.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Container/ArrayPtr.h>
  23. #include <Urho3D/Core/Context.h>
  24. #include <Urho3D/IO/File.h>
  25. #include <Urho3D/Core/ProcessUtils.h>
  26. #include <Urho3D/Core/StringUtils.h>
  27. #ifdef WIN32
  28. #include <windows.h>
  29. #endif
  30. #include <STB/stb_image_write.h>
  31. #include <Urho3D/DebugNew.h>
  32. using namespace Urho3D;
  33. // Kernel used for blurring IES lights
  34. static const float sigma3Kernel9x9[9 * 9] = {
  35. 0.00401f, 0.005895f, 0.007763f, 0.009157f, 0.009675f, 0.009157f, 0.007763f, 0.005895f, 0.00401f,
  36. 0.005895f, 0.008667f, 0.011412f, 0.013461f, 0.014223f, 0.013461f, 0.011412f, 0.008667f, 0.005895f,
  37. 0.007763f, 0.011412f, 0.015028f, 0.017726f, 0.018729f, 0.017726f, 0.015028f, 0.011412f, 0.007763f,
  38. 0.009157f, 0.013461f, 0.017726f, 0.020909f, 0.022092f, 0.020909f, 0.017726f, 0.013461f, 0.009157f,
  39. 0.009675f, 0.014223f, 0.018729f, 0.022092f, 0.023342f, 0.022092f, 0.018729f, 0.014223f, 0.009675f,
  40. 0.009157f, 0.013461f, 0.017726f, 0.020909f, 0.022092f, 0.020909f, 0.017726f, 0.013461f, 0.009157f,
  41. 0.007763f, 0.011412f, 0.015028f, 0.017726f, 0.018729f, 0.017726f, 0.015028f, 0.011412f, 0.007763f,
  42. 0.005895f, 0.008667f, 0.011412f, 0.013461f, 0.014223f, 0.013461f, 0.011412f, 0.008667f, 0.005895f,
  43. 0.00401f, 0.005895f, 0.007763f, 0.009157f, 0.009675f, 0.009157f, 0.007763f, 0.005895f, 0.00401f
  44. };
  45. int main(int argc, char** argv);
  46. void Run(const Vector<String>& arguments);
  47. bool ReadIES(File* data, PODVector<float>& vertical, PODVector<float>& horizontal, PODVector<float>& luminance);
  48. void WriteIES(unsigned char* data, unsigned width, unsigned height, PODVector<float>& horizontal, PODVector<float>& vertical, PODVector<float>& luminance);
  49. void Blur(unsigned char* data, unsigned width, unsigned height, const float* kernel, unsigned kernelWidth);
  50. int main(int argc, char** argv)
  51. {
  52. Vector<String> arguments;
  53. #ifdef WIN32
  54. arguments = ParseArguments(GetCommandLineW());
  55. #else
  56. arguments = ParseArguments(argc, argv);
  57. #endif
  58. Run(arguments);
  59. return 0;
  60. }
  61. void Run(const Vector<String>& arguments)
  62. {
  63. if (arguments.Size() < 3)
  64. ErrorExit("Usage: RampGenerator <output png file> <width> <power> [dimensions]\n"
  65. "IES Usage: RampGenerator <input file> <output png file> <width> [dimensions]");
  66. if (arguments[0].EndsWith(".ies")) // Generate an IES light derived ramp
  67. {
  68. String inputFile = arguments[0];
  69. String ouputFile = arguments[1];
  70. int width = ToInt(arguments[2]);
  71. int dim = 1;
  72. if (arguments.Size() > 3)
  73. dim = ToInt(arguments[3]);
  74. int blurLevel = 0;
  75. if (arguments.Size() > 4)
  76. blurLevel = ToInt(arguments[4]);
  77. const int height = dim == 2 ? width : 1;
  78. Context context;
  79. File file(&context);
  80. file.Open(inputFile);
  81. PODVector<float> horizontal;
  82. PODVector<float> vertical;
  83. PODVector<float> luminance;
  84. ReadIES(&file, vertical, horizontal, luminance);
  85. SharedArrayPtr<unsigned char> data(new unsigned char[width * height]);
  86. WriteIES(data, width, height, vertical, horizontal, luminance);
  87. // Apply a blur, simpler than interpolating through the 2 dimensions of coarse samples
  88. Blur(data, width, height, sigma3Kernel9x9, 9);
  89. stbi_write_png(arguments[1].CString(), width, height, 1, data.Get(), 0);
  90. }
  91. else // Generate a regular power based ramp
  92. {
  93. int width = ToInt(arguments[1]);
  94. float power = ToFloat(arguments[2]);
  95. int dimensions = 1;
  96. if (arguments.Size() > 3)
  97. dimensions = ToInt(arguments[3]);
  98. if (width < 2)
  99. ErrorExit("Width must be at least 2");
  100. if (dimensions < 1 || dimensions > 2)
  101. ErrorExit("Dimensions must be 1 or 2");
  102. if (dimensions == 1)
  103. {
  104. SharedArrayPtr<unsigned char> data(new unsigned char[width]);
  105. for (int i = 0; i < width; ++i)
  106. {
  107. float x = ((float)i) / ((float)(width - 1));
  108. data[i] = (unsigned char)((1.0f - pow(x, power)) * 255.0f);
  109. }
  110. // Ensure start is full bright & end is completely black
  111. data[0] = 255;
  112. data[width - 1] = 0;
  113. stbi_write_png(arguments[0].CString(), width, 1, 1, data.Get(), 0);
  114. }
  115. if (dimensions == 2)
  116. {
  117. SharedArrayPtr<unsigned char> data(new unsigned char[width * width]);
  118. for (int y = 0; y < width; ++y)
  119. {
  120. for (int x = 0; x < width; ++x)
  121. {
  122. unsigned i = y * width + x;
  123. float halfWidth = width * 0.5f;
  124. float xf = (x - halfWidth + 0.5f) / (halfWidth - 0.5f);
  125. float yf = (y - halfWidth + 0.5f) / (halfWidth - 0.5f);
  126. float dist = sqrtf(xf * xf + yf * yf);
  127. if (dist > 1.0f)
  128. dist = 1.0f;
  129. data[i] = (unsigned char)((1.0f - pow(dist, power)) * 255.0f);
  130. }
  131. }
  132. // Ensure the border is completely black
  133. for (int x = 0; x < width; ++x)
  134. {
  135. data[x] = 0;
  136. data[(width - 1) * width + x] = 0;
  137. data[x * width] = 0;
  138. data[x * width + (width - 1)] = 0;
  139. }
  140. stbi_write_png(arguments[0].CString(), width, width, 1, data.Get(), 0);
  141. }
  142. }
  143. }
  144. unsigned GetSample(float position, PODVector<float>& inputs)
  145. {
  146. unsigned pos = 0;
  147. // Early outs
  148. if (position < inputs[0])
  149. return 0;
  150. else if (position > inputs.Back())
  151. return inputs.Size() - 1;
  152. // Find best candidate
  153. float closestVal = M_INFINITY;
  154. unsigned samplePos = -1;
  155. for (unsigned i = 0; i < inputs.Size(); ++i)
  156. {
  157. float val = inputs[i];
  158. float diff = Abs(val - position);
  159. if (diff < closestVal)
  160. {
  161. closestVal = diff;
  162. samplePos = i;
  163. }
  164. }
  165. return samplePos;
  166. }
  167. bool IsWhitespace(const String& string)
  168. {
  169. bool anyNot = false;
  170. for (unsigned i = 0; i < string.Length(); ++i)
  171. {
  172. if (!::isspace(string[i]))
  173. anyNot = true;
  174. }
  175. return !anyNot;
  176. }
  177. float PopFirstFloat(Vector<String>& words)
  178. {
  179. if (words.Size() > 0)
  180. {
  181. float ret = ToFloat(words[0]);
  182. words.Erase(0);
  183. return ret;
  184. }
  185. return -1.0f; // is < 0 ever valid?
  186. }
  187. int PopFirstInt(Vector<String>& words)
  188. {
  189. if (words.Size() > 0)
  190. {
  191. int ret = ToInt(words[0]);
  192. words.Erase(0);
  193. return ret;
  194. }
  195. return -1; // < 0 ever valid?
  196. }
  197. bool ReadIES(File* data, PODVector<float>& vertical, PODVector<float>& horizontal, PODVector<float>& luminance)
  198. {
  199. String line = data->ReadLine();
  200. if (!line.Contains("IESNA:LM-63-1995") && !line.Contains("IESNA:LM-63-2002"))
  201. ErrorExit("Unsupported format: " + line);
  202. // Skip over the misc data
  203. while (!data->IsEof())
  204. {
  205. line = data->ReadLine();
  206. if (line.Contains("TILT=NONE"))
  207. break;
  208. else if (line.Contains("TILT=")) // tilt is a whole different ballgame
  209. ErrorExit("Unsupported tilt: " + line);
  210. else if (line.Contains("[")) // eat this line, it's metadata
  211. continue;
  212. }
  213. // Collect everything into a a list to process, we're now reading actual values
  214. Vector<String> lines;
  215. while (!data->IsEof())
  216. lines.Push(data->ReadLine());
  217. Vector<String> words;
  218. for (unsigned i = 0; i < lines.Size(); ++i)
  219. words.Push(lines[i].Split(' '));
  220. // Prune any 'junk' collected
  221. for (unsigned i = 0; i < words.Size(); ++i)
  222. {
  223. if (words[i].Empty() || IsWhitespace(words[i]))
  224. {
  225. words.Erase(i);
  226. --i;
  227. }
  228. }
  229. const int sampleCount = PopFirstInt(words);
  230. const float lumens = PopFirstFloat(words);
  231. const float multiplier = PopFirstFloat(words); // Scales the candelas, used below
  232. const int verticalCount = PopFirstInt(words); //longitude
  233. const int horizontalCount = PopFirstInt(words); //latitude
  234. const int photometricType = PopFirstInt(words);
  235. const int measureType = PopFirstInt(words); // feet or meters
  236. const float width = PopFirstFloat(words);
  237. const float length = PopFirstFloat(words);
  238. const float height = PopFirstFloat(words);
  239. const float ballast = PopFirstFloat(words);
  240. PopFirstFloat(words); // Junk, called 'reserved' spot in spec
  241. PopFirstFloat(words); // Watts, unused
  242. for (int i = 0; i < verticalCount; ++i)
  243. {
  244. float value = PopFirstFloat(words);
  245. vertical.Push(value);
  246. }
  247. for (int i = 0; i < horizontalCount; ++i)
  248. {
  249. float value = PopFirstFloat(words);
  250. horizontal.Push(value);
  251. }
  252. for (int x = 0; x < horizontalCount; ++x)
  253. {
  254. for (int y = 0; y < verticalCount; ++y)
  255. luminance.Push(PopFirstFloat(words) * multiplier);
  256. }
  257. return true;
  258. }
  259. void WriteIES(unsigned char* data, unsigned width, unsigned height, PODVector<float>& horizontal, PODVector<float>& vertical, PODVector<float>& luminance)
  260. {
  261. // Find maximum luminance value
  262. float maximum = -1;
  263. for (unsigned i = 0; i < luminance.Size(); ++i)
  264. maximum = Max(maximum, luminance[i]);
  265. // Find maximum radial slice
  266. float maxVert = 0;
  267. for (unsigned i = 0; i < vertical.Size(); ++i)
  268. maxVert = Max(maxVert, vertical[i]);
  269. // Find maximum altitude value
  270. float maxHoriz = 0;
  271. for (unsigned i = 0; i < horizontal.Size(); ++i)
  272. maxHoriz = Max(maxHoriz, horizontal[i]);
  273. const float inverseLightValue = 1.0f / maximum;
  274. const float inverseWidth = 1.0f / width;
  275. const float inverseHeight = 1.0f / height;
  276. float dirY = -1.0f;
  277. const float stepX = 2.0f / width;
  278. const float stepY = 2.0f / height;
  279. Vector3 centerVec(0, 0, 0);
  280. centerVec.Normalize();
  281. // Fitting to 90 degrees for better image usage
  282. // otherwise the space used would fit the light's traits and potentially incude a lot of wasted black space
  283. const float angularFactor = 90.0f;
  284. const float fraction = angularFactor / ((float)width);
  285. ::memset(data, 0, width * height);
  286. for (unsigned y = 0; y < height; ++y, dirY += stepY)
  287. {
  288. float dirX = -1.0f;
  289. for (unsigned x = 0; x < width; ++x, dirX += stepX)
  290. {
  291. Vector3 dirVec(dirX * width, dirY * height, 0);
  292. const float len = dirVec.Length();
  293. const float weight = height > 1 ? Abs(1.0f - Cos(dirVec.Length() * fraction)) : x * inverseWidth;
  294. unsigned vert = GetSample(weight * angularFactor, horizontal);
  295. if (vert == -1)
  296. continue;
  297. float value = 0.0f;
  298. if (weight > 0.0f)
  299. {
  300. if (vertical.Size() == 1) // easy case
  301. value = luminance[vert];
  302. else
  303. {
  304. if (height > 1)
  305. {
  306. Vector3 normalized = dirVec.Normalized();
  307. float angle = Atan2(normalized.x_, normalized.y_) - maxHoriz;
  308. while (angle < 0)
  309. angle += 360.0f;
  310. const float moddedAngle = fmodf(angle, maxVert);
  311. unsigned horiz = GetSample(moddedAngle, vertical);
  312. value = luminance[vert + horizontal.Size() * horiz];
  313. }
  314. else
  315. {
  316. // Accumulate for an average across the radial slices
  317. for (unsigned i = 0; i < vertical.Size(); ++i)
  318. value += luminance[vert + i * vertical.Size()];
  319. value /= vertical.Size();
  320. }
  321. }
  322. }
  323. *data = (unsigned char)(inverseLightValue * value * 255.0f);
  324. ++data;
  325. }
  326. }
  327. }
  328. void Blur(unsigned char* data, unsigned width, unsigned height, const float* kernel, unsigned kernelWidth)
  329. {
  330. const int kernelDim = (kernelWidth / 2);
  331. for (int x = 0; x < width; ++x)
  332. {
  333. for (int y = 0; y < height; ++y)
  334. {
  335. float average = 0.0f;
  336. for (int filterX = 0; filterX < kernelWidth; ++filterX)
  337. {
  338. for (int filterY = 0; filterY < kernelWidth; ++filterY)
  339. {
  340. const int xSample = (x - kernelWidth / 2 + filterX + width) % width;
  341. const int ySample = (y - kernelWidth / 2 + filterY + height) % height;
  342. const float value = data[ySample + xSample * height] / 255.0f;
  343. average += value * kernel[filterY + filterX * kernelWidth];
  344. }
  345. }
  346. data[y + x * height] = average * 255.0f;
  347. }
  348. }
  349. }