MandelVideoGenerator.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "MandelVideoGenerator.h"
  2. #include "VideoStream.h"
  3. #include <thread>
  4. #include <omp.h>
  5. #include <cmath>
  6. MandelVideoGenerator::MandelVideoGenerator(const ExportVideoInfo& evi) :
  7. evi{ evi }
  8. {
  9. }
  10. void MandelVideoGenerator::generate(void)
  11. {
  12. mnd::MandelContext ctxt = mnd::initializeContext();
  13. mnd::MandelGenerator& gen = ctxt.getDefaultGenerator();
  14. mnd::MandelInfo mi;
  15. mi.bWidth = evi.width * 2;
  16. mi.bHeight = evi.height * 2;
  17. mi.maxIter = evi.maxIterations;
  18. VideoStream vs(evi.width, evi.height, evi.path, evi.bitrate, evi.fps, evi.preset.c_str());
  19. mnd::Real x = evi.end.x + evi.end.width / 2;
  20. mnd::Real y = evi.end.y + evi.end.height / 2;
  21. mnd::Real w = evi.start.width;
  22. mnd::Real h = evi.start.height;
  23. mnd::Real bigW = 10000000000000000.0;
  24. double bigFac = 1.0;
  25. Bitmap<RGBColor> big;
  26. Bitmap<RGBColor> small;
  27. while(w > evi.end.width || h > evi.end.height) {
  28. mi.view = mnd::MandelViewport{ x - w/2, y - h/2, w, h };
  29. if (bigW > 2 * w) {
  30. Bitmap<float> raw{ evi.width * 2, evi.height * 2 };
  31. gen.generate(mi, raw.pixels.get());
  32. //auto before = std::chrono::high_resolution_clock::now();
  33. big = raw.map<RGBColor>([&mi, this] (float i) {
  34. return i >= mi.maxIter ? RGBColor{ 0, 0, 0 } : evi.gradient.get(i);
  35. });
  36. /*mi.view.zoomCenter(0.5);
  37. gen.generate(mi, raw.pixels.get());
  38. small = raw.map<RGBColor>([] (float x) { return
  39. RGBColor{ uint8_t(::sin(x / 100) * 127 + 127), uint8_t(::sin(x / 213) * 127 + 127), uint8_t(::cos(x / 173) * 127 + 127) };
  40. });*/
  41. bigW = w;
  42. bigFac = 1.0;
  43. }
  44. vs.addFrame(overlay(big, small, bigFac));
  45. w *= ::pow(0.99, evi.zoomSpeed);
  46. h *= ::pow(0.99, evi.zoomSpeed);
  47. bigFac *= ::pow(0.99, evi.zoomSpeed);
  48. }
  49. }
  50. inline RGBColor biliniear(const Bitmap<RGBColor>& img, double x, double y)
  51. {
  52. int xfloor = int(::floor(x));
  53. int yfloor = int(::floor(y));
  54. int xceil = int(::ceil(x));
  55. int yceil = int(::ceil(y));
  56. double xLerp = x - xfloor;
  57. double yLerp = y - yfloor;
  58. RGBColor samples[2][2] = {
  59. {
  60. img.get(xfloor, yfloor),
  61. img.get(xfloor, yceil),
  62. },
  63. {
  64. img.get(xceil, yfloor),
  65. img.get(xceil, yceil),
  66. }
  67. };
  68. double r = 0, g = 0, b = 0;
  69. auto mklin = [] (double x) {
  70. return x;
  71. };
  72. auto unlin = [] (double x) {
  73. return x;
  74. };
  75. r += (1 - xLerp) * (1 - yLerp) * mklin(samples[0][0].r);
  76. r += (1 - xLerp) * yLerp * mklin(samples[0][1].r);
  77. r += xLerp * (1 - yLerp) * mklin(samples[1][0].r);
  78. r += xLerp * yLerp * mklin(samples[1][1].r);
  79. g += (1 - xLerp) * (1 - yLerp) * mklin(samples[0][0].g);
  80. g += (1 - xLerp) * yLerp * mklin(samples[0][1].g);
  81. g += xLerp * (1 - yLerp) * mklin(samples[1][0].g);
  82. g += xLerp * yLerp * mklin(samples[1][1].g);
  83. b += (1 - xLerp) * (1 - yLerp) * mklin(samples[0][0].b);
  84. b += (1 - xLerp) * yLerp * mklin(samples[0][1].b);
  85. b += xLerp * (1 - yLerp) * mklin(samples[1][0].b);
  86. b += xLerp * yLerp * mklin(samples[1][1].b);
  87. return RGBColor{ uint8_t(unlin(r)), uint8_t(unlin(g)), uint8_t(unlin(b)) };
  88. }
  89. inline RGBColor nearest(const Bitmap<RGBColor>& img, double x, double y)
  90. {
  91. int xfloor = int(::floor(x));
  92. int yfloor = int(::floor(y));
  93. return img.get(xfloor, yfloor);
  94. }
  95. Bitmap<RGBColor> MandelVideoGenerator::overlay(const Bitmap<RGBColor>& outer,
  96. const Bitmap<RGBColor>& /* inner */, double scale)
  97. {
  98. printf("%lf\n", scale);
  99. Bitmap<RGBColor> ret{ outer.width / 2, outer.height / 2 };
  100. double newW = outer.width * scale * 2;
  101. double newH = outer.height * scale * 2;
  102. double newX = outer.width * (1 - scale) / 2;
  103. double newY = outer.height * (1 - scale) / 2;
  104. //auto before = std::chrono::high_resolution_clock::now();
  105. #pragma omp parallel for
  106. for (int i = 0; i < ret.height; i++) {
  107. for (int j = 0; j < ret.width; j++) {
  108. double newJ = newX + j * newW / outer.width;
  109. double newI = newY + i * newH / outer.height;
  110. RGBColor a = biliniear(outer, newJ, newI);
  111. ret.get(j, i) = a;
  112. }
  113. }
  114. //auto after = std::chrono::high_resolution_clock::now();
  115. //printf("gradient applied in: %lld microseconds\n", std::chrono::duration_cast<std::chrono::microseconds>(after - before).count());
  116. fflush(stdout);
  117. /*for (int i = 0; i < ret.height * ret.width; i++) {
  118. ret.pixels[i] = outer.pixels[i];
  119. }*/
  120. return ret;
  121. }