MandelbrotImage.C 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <fstream>
  9. #include <iostream>
  10. #include <Wt/WResource>
  11. #include <Wt/WImage>
  12. #include <Wt/WPainter>
  13. #include <Wt/WPen>
  14. #include <Wt/WRasterImage>
  15. #include <Wt/Http/Response>
  16. #include "MandelbrotImage.h"
  17. namespace {
  18. class MandelbrotResource : public WResource
  19. {
  20. public:
  21. MandelbrotResource(MandelbrotImage *img,
  22. int64_t x, int64_t y, int w, int h)
  23. : img_(img),
  24. x_(x), y_(y), w_(w), h_(h)
  25. { }
  26. virtual ~MandelbrotResource() {
  27. beingDeleted();
  28. }
  29. void handleRequest(const Http::Request& request,
  30. Http::Response& response) {
  31. WRasterImage image("png", w_, h_);
  32. img_->generate(x_, y_, &image);
  33. image.handleRequest(request, response);
  34. }
  35. private:
  36. MandelbrotImage *img_;
  37. int64_t x_, y_;
  38. int w_, h_;
  39. };
  40. }
  41. MandelbrotImage::MandelbrotImage(int width, int height,
  42. int64_t virtualWidth,
  43. int64_t virtualHeight,
  44. double bx1, double by1,
  45. double bx2, double by2,
  46. WContainerWidget *parent)
  47. : WVirtualImage(width, height, virtualWidth, virtualHeight, 256, parent),
  48. bx1_(bx1), by1_(by1),
  49. bwidth_(bx2 - bx1), bheight_(by2 - by1),
  50. maxDepth_(50),
  51. bailOut2_(30*30)
  52. {
  53. enableDragging();
  54. redrawAll();
  55. scroll(width*2, virtualHeight/2 - height);
  56. }
  57. void MandelbrotImage::zoomIn()
  58. {
  59. resizeImage(imageWidth() * 2, imageHeight() * 2);
  60. scrollTo(currentTopLeftX() * 2 + viewPortWidth()/2,
  61. currentTopLeftY() * 2 + viewPortHeight()/2);
  62. }
  63. void MandelbrotImage::zoomOut()
  64. {
  65. scrollTo(currentTopLeftX() / 2 - viewPortWidth()/4,
  66. currentTopLeftY() / 2 - viewPortHeight()/4);
  67. resizeImage(std::max((int64_t)viewPortWidth(), imageWidth() / 2),
  68. std::max((int64_t)viewPortHeight(), imageHeight() / 2));
  69. }
  70. WResource *MandelbrotImage::render(int64_t x, int64_t y, int w, int h)
  71. {
  72. return new MandelbrotResource(this, x, y, w, h);
  73. }
  74. void MandelbrotImage::generate(int64_t x, int64_t y, WRasterImage *img)
  75. {
  76. int w = img->width().toPixels();
  77. int h = img->height().toPixels();
  78. std::cerr << "rendering: (" << x << "," << y << ") ("
  79. << x+w << "," << y+h << ")" << std::endl;
  80. for (int i = 0; i < w; ++i)
  81. for (int j = 0; j < h; ++j) {
  82. double bx = convertPixelX(x + i);
  83. double by = convertPixelY(y + j);
  84. double d = calcPixel(bx, by);
  85. int lowr = 100;
  86. int r, g, b;
  87. if (d == maxDepth_)
  88. r = g = b = 0;
  89. else {
  90. r = lowr + (int)((d * (255-lowr))/maxDepth_);
  91. g = 0 + (int)((d * 255)/maxDepth_);
  92. b = 0;
  93. }
  94. img->setPixel(i, j, WColor(r, g, b));
  95. }
  96. }
  97. double MandelbrotImage::convertPixelX(int64_t x) const
  98. {
  99. return bx1_ + ((double) (x) / imageWidth() * bwidth_);
  100. }
  101. double MandelbrotImage::convertPixelY(int64_t y) const
  102. {
  103. return by1_ + ((double) (y) / imageHeight() * bheight_);
  104. }
  105. double MandelbrotImage::currentX1() const
  106. {
  107. return convertPixelX(currentTopLeftX());
  108. }
  109. double MandelbrotImage::currentY1() const
  110. {
  111. return convertPixelY(currentTopLeftY());
  112. }
  113. double MandelbrotImage::currentX2() const
  114. {
  115. return convertPixelX(currentBottomRightX());
  116. }
  117. double MandelbrotImage::currentY2() const
  118. {
  119. return convertPixelY(currentBottomRightY());
  120. }
  121. double MandelbrotImage::calcPixel(double x, double y)
  122. {
  123. double x1 = x;
  124. double y1 = y;
  125. for (int i = 0; i < maxDepth_; ++i) {
  126. double xs = x1 * x1;
  127. double ys = y1 * y1;
  128. double x2 = xs - ys + x;
  129. double y2 = x1 * y1 * 2 + y;
  130. x1 = x2;
  131. y1 = y2;
  132. double z = xs + ys;
  133. if (xs + ys > bailOut2_)
  134. return (double)i + 1 - log(log(sqrt(z)))/log(2.0);
  135. }
  136. return maxDepth_;
  137. }