Skip to content

Instantly share code, notes, and snippets.

@meyer9
Forked from anonymous/converttoppm.py
Last active August 9, 2017 20:22
Show Gist options
  • Save meyer9/35d746cbfaa662f26ee6327f4d48f24a to your computer and use it in GitHub Desktop.
Save meyer9/35d746cbfaa662f26ee6327f4d48f24a to your computer and use it in GitHub Desktop.
Modifications to Scheme art to run faster
import struct
with open('test.ppm', 'wb') as ppm_file:
ppm_file.write("P6\n".encode('ascii'))
with open('test.txt', 'r') as out:
width = int(out.readline())
height = int(out.readline())
ppm_file.write((str(width) + ' ' + str(height) + '\n').encode('ascii'))
ppm_file.write('255\n'.encode('ascii'))
ppm = []
for i in range(height):
ppm_line = []
for j in range(width):
r = int(float(out.readline()))
g = int(float(out.readline()))
b = int(float(out.readline()))
ppm_line.append(struct.pack('BBB', r, g, b))
ppm.append(b''.join(ppm_line[::-1]))
ppm_file.write(b''.join(ppm[::-1]))
;; ADD THESE FUNCTIONS TO THE TOP OF contest.scm
(define (screen_width)
600
)
(define (screen_height)
600
)
(define (rgb r g b)
(newColor r g b)
)
(define (pixel x y c)
(print (floor (* (color_r c) 255)))
(print (floor (* (color_g c) 255)))
(print (floor (* (color_b c) 255)))
)
;; REPLACE THE DRAW FUNCTION WITH THIS
(define (draw)
(print (screen_width))
(print (screen_height))
(drawfunc raytrace 0 (screen_width) (screen_height))
)
csc contest.scm && ./contest > test.txt && python outtoppm.py && convert test.ppm test.png && rm test.ppm test.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment