format binary as "tga"

; DON'T _RUN_ this file, just COMPILE it and view it with any
; image program that supports 8-bit paletted TGA-images (many does).

; usage: "FASM MANDELDB.ASM MANDELDB.TGA"
; better keep the ".TGA"-extension, else this might confuse some lamish
; image programs.

; Some basic image properties. Feel free to modify
Width equ 80
Height equ 80
IterLim equ 256
Prec equ 29; don't set above 29 nor to low, else image may be scrambled

; TGA-header: 8-bit paletted image
dw 256,1
db 0,0,1,24
dw 0,0,Width,Height
db 8,8

; palette: 256*3bytes (RGB)
repeat 256
db (%+54h) and 0FFh
db (%+0A9h) and 0FFh
db % - 1
end repeat

; actual image (Mandelbrot fractal)
PrecScale = 1 shl (Prec-1)

StepR = (5 shl (Prec-1) +Width shr 1) / Width; = 2.5/Width
StepI = (5 shl (Prec-1) +Height shr 1) / Height; = 3/Height

PosI = -5 shl (Prec-2); = -1.25
repeat Height
PosR = -2 shl Prec; = -2
repeat Width
R = PosR
I = PosI
RR = (PosR*PosR) shr Prec
II = (PosI*PosI) shr Prec

Color = IterLim - 1; Color is also the current iteration number
repeat IterLim
if (RR + II) < 4 shl Prec
Tmp = (RR - II) + PosR
I = (R*I) / PrecScale + PosI
R = Tmp
RR = (Tmp*Tmp) shr Prec
II = (I*I) shr Prec
Color = Color - 1; discounting Color
end if
end repeat
if Color < 0
Color = 0
end if
db Color

PosR = PosR + StepR
end repeat
PosI = PosI + StepI
end repeat