﻿#! /usr/bin/python
#  -*- coding: utf-8 -*-
#
#  Vladimír Slávik 2008-2011
#  Python 2.5-3.1
#
#  for Simutrans
#  http://www.simutrans.com
#
#  code is public domain
#

# read all dat files in all subfolders and produce list of these that have
# wrong color type (other than 24bit)

from __future__ import print_function, division

import os, glob
from socket import ntohl
from struct import unpack

badcounter = 0
allcounter = 0

#-----

def procFile(png) :
	global badcounter, allcounter
	f = open(png, 'rb')
	f.seek(8 + (4 + 4) + (4 + 4 + 1))
	# 8 bytes header, 4 length, 4 IHDR, 4+4 width+height, 1 palette
	c = unpack("B", f.read(1))[0] # read "color" entry
	
	if c != 2 :
		print(png)
		print("color type: %d" % c)
		badcounter = badcounter + 1
	f.close()
	allcounter = allcounter + 1

#-----

def walkFiles(topdir) :
	for png in glob.glob(os.path.join(topdir, "*.png")) :
		procFile(png)
	for fsitem in glob.glob(os.path.join(topdir, "*")) :
		if os.path.isdir(fsitem) :
			if not os.path.exists(os.path.join(topdir, fsitem, "statsignore.conf")) :
				walkFiles(os.path.join(topdir, fsitem))


#-----

badcounter = 0
allcounter = 0

walkFiles(os.getcwd())
print("%d of %d files bad, or %.2f%%" % (badcounter, allcounter, 100*badcounter/allcounter))

# EOF

@@ -426,16 +426,260 @@
 er = 0%0A%0A
+png_types = %7B%0A%090: %22grayscale%22,%0A%092: %22RGB%22,%0A%093: %22indexed%22,%0A%094: %22grey+alpha%22,%0A%096: %22RGBA%22,%0A%7D%0A%0A#-----%0A%0Adef pngType(color, palette) :%0A%09try :%0A%09%09kind = png_types%5Bcolor%5D%0A%09except :%0A%09%09kind = str(color)%0A%09return %22 color type: %25s, %25d bpp%22 %25 (kind, palette)%0A%0A
 #-----%0A%0A
@@ -778,20 +778,16 @@
 + (4 + 4
- + 1
 ))%0A%09# 8 
@@ -838,19 +838,72 @@
 ight
-, 1 palette
+%0A%09d = unpack(%22B%22, f.read(1))%5B0%5D # read %22bit depth/palette%22 entry
 %0A%09c 
@@ -961,14 +961,28 @@
 %09if 
+(
 c != 2
+) or (d != 8)
  :%0A%09
@@ -1005,28 +1005,21 @@
 int(
-%22color type: %25d%22 %25 c
+pngType(c, d)
 )%0A%09%09

make PNG lister recognize wrong bit depth, add more "human" reporting