RampGenerator.cpp 14 KB

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