Saturday, June 21, 2008

Python: Isotropic fractal surface generator

#! /usr/bin/env python
from __future__ import division
import Image
from scipy import *

class FractalSurface(object):
'''Generate isotropic fractal surface image using
spectral synthesis method [1, p.]
References:
1. Yuval Fisher, Michael McGuire,
The Science of Fractal Images, 1988
'''

def __init__(self,fd=2.5, N=256):
self.N=N
self.H=1-(fd-2);
self.X=zeros((self.N,self.N),complex)
self.A=zeros((self.N,self.N),complex)
self.img=Image.Image()

def genSurface(self):
'''Spectral synthesis method
'''
N=self.N; A=self.A
powerr=-(self.H+1.0)/2.0

for i in range(int(N/2)+1):
for j in range(int(N/2)+1):

phase=2*pi*rand()

if i is not 0 or j is not 0:
rad=(i*i+j*j)**powerr*random.normal()
else:
rad=0.0

self.A[i,j]=complex(rad*cos(phase),rad*sin(phase))

if i is 0:
i0=0.0
else:
i0=N-i
if j is 0:
j0=0.0
else:
j0=N-j

self.A[i0,j0]=complex(rad*cos(phase),-rad*sin(phase))


self.A.imag[N/2][0]=0.0
self.A.imag[0,N/2]=0.0
self.A.imag[N/2][N/2]=0.0

for i in range(1,int(N/2)):
for j in range(1,int(N/2)):
phase=2*pi*rand()
rad=(i*i+j*j)**powerr*random.normal()
self.A[i,N-j]=complex(rad*cos(phase),rad*sin(phase))
self.A[N-i,j]=complex(rad*cos(phase),-rad*sin(phase))

itemp=fftpack.ifft2(self.A)
itemp=itemp-itemp.min()
self.X=itemp


def genImage(self):
#Aa=abs(Aa)
Aa=self.X
im=Aa.real/Aa.real.max()*255.0
self.img=Image.fromarray(uint8(im))
#img2=Image.fromstring("L",(N,N),uint8(im).tostring())

def showImg(self):
self.img.show()

def saveImg(self,fname="fs.tiff"):
self.img.save(fname)

def getFSimg(self):
return self.img

def main():
fs=FractalSurface()
fs.genSurface()
fs.genImage()
fs.saveImg()
fs.showImg()

if __name__ == '__main__':
main()

Example


FD=2.1




FD=2.5




FD=2.9

Tuesday, June 03, 2008

ubuntu: iptables port redirect

I want to redirect all incoming requests on port 80 to 8080. I did it using the following command:sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
Following this operation my iptables -L wasChain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

The iptables -t nat -L was:Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- anywhere anywhere tcp dpt:www redir ports 8080

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

I saved these setings using sudo iptables-save.

Note:
This works only when some other computer tries to connect to port 80. If I tried to connect from the same server (i.e. localhost) it did not work. The reason for now is unknown, but it works, an this is good.