PaletteOptimiser

Changeset

85:ab5c1ce7de8f
2015-10-10 Paul Boddie raw files shortlog changelog graph Moved the SimpleImage class into the extension module. (This seems to slow the program down somewhat.) simpleimage-shedskin
optimiser.py (file) optimiserlib.py (file)
     1.1 --- a/optimiser.py	Sat Oct 10 15:59:38 2015 +0200
     1.2 +++ b/optimiser.py	Sat Oct 10 16:04:09 2015 +0200
     1.3 @@ -210,28 +210,6 @@
     1.4      except ValueError:
     1.5          return 0.0
     1.6  
     1.7 -class SimpleImage:
     1.8 -
     1.9 -    "An image behaving like PIL.Image."
    1.10 -
    1.11 -    def __init__(self, data, size):
    1.12 -        self.data = data
    1.13 -        self.width, self.height = self.size = size
    1.14 -
    1.15 -    def copy(self):
    1.16 -        return SimpleImage(self.data[:], self.size)
    1.17 -
    1.18 -    def getpixel(self, xy):
    1.19 -        x, y = xy
    1.20 -        return self.data[y * self.width + x]
    1.21 -
    1.22 -    def putpixel(self, xy, value):
    1.23 -        x, y = xy
    1.24 -        self.data[y * self.width + x] = value
    1.25 -
    1.26 -    def getdata(self):
    1.27 -        return self.data
    1.28 -
    1.29  # Main program.
    1.30  
    1.31  if __name__ == "__main__":
     2.1 --- a/optimiserlib.py	Sat Oct 10 15:59:38 2015 +0200
     2.2 +++ b/optimiserlib.py	Sat Oct 10 16:04:09 2015 +0200
     2.3 @@ -191,6 +191,28 @@
     2.4      all.sort(reverse=True)
     2.5      return [l for total, l in all]
     2.6  
     2.7 +class SimpleImage:
     2.8 +
     2.9 +    "An image behaving like PIL.Image."
    2.10 +
    2.11 +    def __init__(self, data, size):
    2.12 +        self.data = data
    2.13 +        self.width, self.height = self.size = size
    2.14 +
    2.15 +    def copy(self):
    2.16 +        return SimpleImage(self.data[:], self.size)
    2.17 +
    2.18 +    def getpixel(self, xy):
    2.19 +        x, y = xy
    2.20 +        return self.data[y * self.width + x]
    2.21 +
    2.22 +    def putpixel(self, xy, value):
    2.23 +        x, y = xy
    2.24 +        self.data[y * self.width + x] = value
    2.25 +
    2.26 +    def getdata(self):
    2.27 +        return self.data
    2.28 +
    2.29  # Exercise functions for Shedskin.
    2.30  
    2.31  if __name__ == "__main__":
    2.32 @@ -203,4 +225,11 @@
    2.33      clip(200.0)
    2.34      get_combinations([(0.5, (255, 0, 0)), (0.25, (255, 255, 0)), (0.25, (0, 0, 0))], 2)
    2.35  
    2.36 +    data = [(0, 0, 0), (0, 0, 0)]
    2.37 +    im = SimpleImage(data, (2, 1))
    2.38 +    im2 = im.copy()
    2.39 +    im2.getpixel((0, 0)) == (0, 0, 0)
    2.40 +    im2.putpixel((0, 0), (255, 255, 255))
    2.41 +    im2.getdata() == [(255, 255, 255), (0, 0, 0)]
    2.42 +
    2.43  # vim: tabstop=4 expandtab shiftwidth=4