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