forked from aosabook/500lines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.py
More file actions
24 lines (23 loc) · 825 Bytes
/
Copy pathimage.py
File metadata and controls
24 lines (23 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from color import Color
from geometry import AABox, Vector
class PPMImage:
def __init__(self, resolution, bg=Color()):
self.resolution = resolution
self.pixels = []
for i in xrange(self.resolution):
lst = []
for j in xrange(self.resolution):
lst.append(Color(rgb=bg.rgb, a=bg.a))
self.pixels.append(lst)
def bounds(self):
return AABox(Vector(0,0), Vector(1,1))
def __getitem__(self, a):
return self.pixels[a.y][a.x]
def __setitem__(self, a, color):
self.pixels[a.y][a.x] = color
def write_ppm(self, out):
n = self.resolution
out.write("P6\n%s\n%s\n255\n" % (n,n))
for y in xrange(n-1, -1, -1):
for x in xrange(n):
out.write(self.pixels[y][x].as_ppm())