Skip to content

Instantly share code, notes, and snippets.

@poizan42
Created July 21, 2015 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poizan42/3054232ad7624602a7fa to your computer and use it in GitHub Desktop.
Save poizan42/3054232ad7624602a7fa to your computer and use it in GitHub Desktop.
Some experimentation with creating a pdf with a 256x256 Mandelbrot set rendered by the viewer by using a tint transform. Viewers generally seems to reduce the precision to 8bpp before passing the data on to the tint transform, so it doesn't work
#!/usr/bin python
pdf = """%PDF-1.4
1 0 obj
<< /Type /Catalog
/Outlines 2 0 R
/Pages 3 0 R
>>
endobj
2 0 obj
<< /Type Outlines
/Count 0
>>
endobj
3 0 obj
<< /Type /Pages
/Kids [ 4 0 R ]
/Count 1
>>
endobj
4 0 obj
<<
/Type /Page
/Parent 3 0 R
/MediaBox [ 0 0 {0} {0} ]
/Contents 5 0 R
/Resources <<
/ProcSet 6 0 R
/XObject <<
/Im1 7 0 R >>
>>
>>
endobj
7 0 obj
<< /Type /XObject
/Subtype /Image
/Width {0}
/Height {0}
/ColorSpace [/Separation /Mandelbrot /DeviceGray 8 0 R]
/BitsPerComponent 16
/Length null
/Filter /ASCIIHexDecode >>
stream
{1}
endstream
5 0 obj
<< /Length null >> %page content
stream
q % Save graphics state
{0} 0 0 {0} 0 0 cm % Translate to (0,0) and scale by 132
/Im1 Do % Paint image
Q % Restore graphics state
endstream
endobj
8 0 obj
<< /FunctionType 4
/Domain [ 0.0 1.0 ]
/Range [ 0.0 1.0 ]
/Length null
>>
stream
{{
65535 mul % scale input
round
dup % make a copy of the input
% get upper nibble
-8 bitshift % >> 4
7.5 sub % -7.5 .. 7.5
3.75 div % -2 .. 2
exch % original (scaled) input to top of stack
% get lower nibble
256 mod % "and" is broken in sumatra...
7.5 sub
3.75 div % -2 .. 2
% Stack: Re, Im
2 copy % c: (Re, Im), z:(Re, Im)
0 1 eq
% ITERATION START
% stack : stop, Re, Im, Re(c), Im(c)
{2}
{{1}} {{0}} ifelse
}}
endstream
endobj
6 0 obj %procset
[ /PDF ]
endobj
trailer
<< /Size 7
/Root 1 0 R
>>
%%EOF
"""
mandelCode = """
{1 1 eq} {
% Calculate: z^2 + c: (Re(z)^2 - Im(z)^2 + Re(c), 2*Re*Im + Im(c))
dup dup mul % stack : Re^2, Re, Im, ...
2 index % stack: Im, Re^2, Re, Im, ...
dup mul % stack: Im^2, Re^2, Re, Im, ...
sub %stack: Re^2 - Im^2, Re, Im, Re(c), Im(c)
3 index % stack: Re(c), Re^2 - Im^2, Re, Im, Re(c), Im(c)
add % stack: Re^2 - Im^2 + Re(c), Re, Im, Re(c), Im(c)
3 1 roll % stack: Re, Im, Re^2 - Im^2 + Re(c), Re(c), Im(c)
mul % stack: Re*Im, Re^2 - Im^2 + Re(c), Re(c), Im(c)
2 mul % stack: 2*Re*Im, Re^2 - Im^2 + Re(c), Re(c), Im(c)
3 index % stack: Im(c), 2*Re*Im, Re^2 - Im^2 + Re(c), Re(c), Im(c)
add % stack: 2*Re*Im + Im(c), Re^2 - Im^2 + Re(c), Re(c), Im(c)
exch % stack: Re^2 - Im^2 + Re(c), 2*Re*Im + Im(c), Re(c), Im(c)
% stack: Re, Im, Re(c), Im(c)
% Check |z| >= 2: sqrt(Re^2 + Im^2) >= 2
dup dup mul % stack: Re^2, Re, Im, Re(c), Im(c)
2 index % stack: Im, Re^2, Re, Im, Re(c), Im(c)
dup mul % stack: Im^2, Re^2, Re, Im, Re(c), Im(c)
add % stack: Im^2 + Re^2, Re, Im, Re(c), Im(c)
sqrt % stack: sqrt(Im^2 + Re^2), Re, Im, Re(c), Im(c)
2 ge % stack: stop, Re, Im, Re(c), Im(c)
} ifelse
"""
import sys
size = int(sys.argv[1])
iterations = int(sys.argv[2])
image = "\n".join(["".join(["%04X" % i for i in xrange(j*256, 256+j*256)]) for j in xrange(256)])
code = "".join([mandelCode for i in xrange(iterations)])
print pdf.format(size, image, code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment