summaryrefslogtreecommitdiffstats
path: root/build/pypng/pipcolours
diff options
context:
space:
mode:
authorThomas Groman <tgroman@nuegia.net>2020-04-20 20:56:28 -0700
committerThomas Groman <tgroman@nuegia.net>2020-04-20 20:56:28 -0700
commit508d270a4d78d491bbe1c67c309c404f547da58a (patch)
treed16e2a906501dcda7b4d268579896f03e125f553 /build/pypng/pipcolours
parentf9cab004186edb425a9b88ad649726605080a17c (diff)
downloadwebbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.tar
webbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.tar.gz
webbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.tar.lz
webbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.tar.xz
webbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.zip
added Comm Build System
Diffstat (limited to 'build/pypng/pipcolours')
-rw-r--r--build/pypng/pipcolours56
1 files changed, 56 insertions, 0 deletions
diff --git a/build/pypng/pipcolours b/build/pypng/pipcolours
new file mode 100644
index 0000000..7c76df8
--- /dev/null
+++ b/build/pypng/pipcolours
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+# $URL: http://pypng.googlecode.com/svn/trunk/code/pipcolours $
+# $Rev: 96 $
+
+# pipcolours - extract all colours present in source image.
+
+def colours(out, inp):
+ import itertools
+ import png
+
+ r = png.Reader(file=inp)
+ _,_,pixels,info = r.asDirect()
+ planes = info['planes']
+ col = set()
+ for row in pixels:
+ # Ewgh, side effects on col
+ map(col.add, png.group(row, planes))
+ col,planes = channel_reduce(col, planes)
+ col = list(col)
+ col.sort()
+ col = list(itertools.chain(*col))
+ width = len(col)//planes
+ greyscale = planes in (1,2)
+ alpha = planes in (2,4)
+ bitdepth = info['bitdepth']
+ w = png.Writer(width, 1,
+ bitdepth=bitdepth, greyscale=greyscale, alpha=alpha)
+ w.write(out, [col])
+
+def channel_reduce(col, planes):
+ """Attempt to reduce the number of channels in the set of
+ colours."""
+ if planes >= 3:
+ def isgrey(c):
+ return c[0] == c[1] == c[2]
+ if min(map(isgrey, col)) == True:
+ # Every colour is grey.
+ col = set(map(lambda x: x[0::3], col))
+ planes -= 2
+ return col,planes
+
+def main(argv=None):
+ import sys
+
+ if argv is None:
+ argv = sys.argv
+
+ argv = argv[1:]
+ if len(argv) > 0:
+ f = open(argv[0], 'rb')
+ else:
+ f = sys.stdin
+ return colours(sys.stdout, f)
+
+if __name__ == '__main__':
+ main()