]> git.cworth.org Git - apitrace/commitdiff
Support grayscale PNM in retracediff.py
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Tue, 30 Oct 2012 15:54:04 +0000 (15:54 +0000)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Tue, 30 Oct 2012 15:54:35 +0000 (15:54 +0000)
scripts/retracediff.py

index 8a0837a82bf6a17d9e973c62db70cdbaf6b9860c..d665c7601b7acf7a91ef3708fd521c4165e0891f 100755 (executable)
@@ -98,7 +98,15 @@ def read_pnm(stream):
     magic = stream.readline()
     if not magic:
         return None, None
-    assert magic.rstrip() == 'P6'
+    magic = magic.rstrip()
+    if magic == 'P5':
+        channels = 1
+        mode = 'L'
+    elif magic == 'P6':
+        channels = 3
+        mode = 'RGB'
+    else:
+        raise Exception('Unsupported magic `%s`' % magic)
     comment = ''
     line = stream.readline()
     while line.startswith('#'):
@@ -107,8 +115,8 @@ def read_pnm(stream):
     width, height = map(int, line.strip().split())
     maximum = int(stream.readline().strip())
     assert maximum == 255
-    data = stream.read(height * width * 3)
-    image = Image.frombuffer('RGB', (width, height), data, 'raw', 'RGB', 0, 1)
+    data = stream.read(height * width * channels)
+    image = Image.frombuffer(mode, (width, height), data, 'raw', mode, 0, 1)
     return image, comment