| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright 2015 Google Inc. All rights reserved. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
|
| 18 |
"""Serve fractal images over HTTP by calling out to mandelbrot. |
| 19 |
""" |
| 20 |
|
| 21 |
import json |
| 22 |
import math |
| 23 |
import os |
| 24 |
import subprocess |
| 25 |
import sys |
| 26 |
import time |
| 27 |
|
| 28 |
import flask |
| 29 |
|
| 30 |
app = flask.Flask(__name__) |
| 31 |
|
| 32 |
CONF = None |
| 33 |
for i in xrange(1, 60): |
| 34 |
try: |
| 35 |
with open('conf.json') as conf_file: |
| 36 |
CONF = json.load(conf_file) |
| 37 |
except IOError: |
| 38 |
time.sleep(1) |
| 39 |
|
| 40 |
if CONF == None: |
| 41 |
sys.stderr.write('ERROR: Could not open conf.json.') |
| 42 |
sys.exit(1) |
| 43 |
|
| 44 |
|
| 45 |
@app.route("/") |
| 46 |
def handle_fractal(): |
| 47 |
"""Get fractal coordinates from query string, call mandelbrot to generate image. |
| 48 |
|
| 49 |
Returns: |
| 50 |
The image, wrapped in an HTML response. |
| 51 |
""" |
| 52 |
|
| 53 |
level = int(flask.request.args.get("l", "0")) |
| 54 |
x = float(int(flask.request.args.get("x", "0"))) |
| 55 |
y = float(int(flask.request.args.get("y", "0"))) |
| 56 |
|
| 57 |
if level < 0: |
| 58 |
level = 0 |
| 59 |
|
| 60 |
grid_size = math.pow(2, level) |
| 61 |
|
| 62 |
x0 = "%.30g" % ((x - 0) / grid_size) |
| 63 |
y0 = "%.30g" % ((y - 0) / grid_size) |
| 64 |
x1 = "%.30g" % ((x + 1) / grid_size) |
| 65 |
y1 = "%.30g" % ((y + 1) / grid_size) |
| 66 |
|
| 67 |
print "Tile: %s %s %s %s" % (x0, y0, x1, y1) |
| 68 |
|
| 69 |
width = str(CONF['width']) |
| 70 |
height = str(CONF['height']) |
| 71 |
iters = str(CONF['iters']) |
| 72 |
|
| 73 |
cmd = ['./mandelbrot', width, height, iters, x0, y0, x1, y1] |
| 74 |
image_data = subprocess.check_output(cmd) |
| 75 |
|
| 76 |
response = flask.make_response(image_data) |
| 77 |
response.headers["Content-Type"] = "image/png" |
| 78 |
response.headers["cache-control"] = "public, max-age=600" |
| 79 |
|
| 80 |
return response |
| 81 |
|
| 82 |
@app.route("/thumb") |
| 83 |
def handle_thumb(): |
| 84 |
"""Get fractal coordinates from query string, call mandelbrot to generate image. |
| 85 |
|
| 86 |
Returns: |
| 87 |
The image, wrapped in an HTML response. |
| 88 |
""" |
| 89 |
|
| 90 |
level = int(flask.request.args.get("l", "0")) |
| 91 |
x = float(flask.request.args.get("x", "0")) |
| 92 |
y = float(flask.request.args.get("y", "0")) |
| 93 |
|
| 94 |
if level < 0: |
| 95 |
level = 0 |
| 96 |
|
| 97 |
grid_size = math.pow(2, -level) |
| 98 |
|
| 99 |
x0 = "%.30g" % (x - 1.5*grid_size) |
| 100 |
y0 = "%.30g" % (y - 1.5*grid_size) |
| 101 |
x1 = "%.30g" % (x + 1.5*grid_size) |
| 102 |
y1 = "%.30g" % (y + 1.5*grid_size) |
| 103 |
|
| 104 |
print "Thumbnail: %s %s %s %s" % (x0, y0, x1, y1) |
| 105 |
|
| 106 |
width = str(CONF['thumb_width']) |
| 107 |
height = str(CONF['thumb_height']) |
| 108 |
iters = str(CONF['iters']) |
| 109 |
|
| 110 |
cmd = ['./mandelbrot', width, height, iters, x0, y0, x1, y1] |
| 111 |
image_data = subprocess.check_output(cmd) |
| 112 |
|
| 113 |
response = flask.make_response(image_data) |
| 114 |
response.headers["Content-Type"] = "image/png" |
| 115 |
response.headers["cache-control"] = "public, max-age=600" |
| 116 |
|
| 117 |
return response |
| 118 |
|
| 119 |
if __name__ == "__main__": |
| 120 |
app.run(host="0.0.0.0", port=8000, debug=True) |