File indexing completed on 2024-04-14 03:40:32
0001 #!/usr/bin/python 0002 # -*- coding: utf-8 -*- 0003 0004 # Copyright 2008 Albert Astals Cid <aacid@kde.org> 0005 0006 # This program is free software; you can redistribute it and/or 0007 # modify it under the terms of the GNU General Public License as 0008 # published by the Free Software Foundation; either version 2 of 0009 # the License, or (at your option) any later version. 0010 0011 # This program is distributed in the hope that it will be useful, 0012 # but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 # GNU General Public License for more details. 0015 0016 # You should have received a copy of the GNU General Public License 0017 # along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 0019 import sys 0020 from PyQt5 import QtCore 0021 from PyQt5 import QtXml 0022 from PIL import Image 0023 0024 app = QtCore.QCoreApplication(sys.argv) 0025 0026 if len(sys.argv) == 1: 0027 print("Error: You have to specify the file to check") 0028 sys.exit(1) 0029 0030 for path in sys.argv[1:]: 0031 print("Processing " + path) 0032 xmlFile = QtCore.QFile(path) 0033 0034 if not xmlFile.exists(): 0035 print("Error: File {} does not exist".format(path)) 0036 else: 0037 if not xmlFile.open(QtCore.QIODevice.ReadOnly): 0038 print("Error: Could not open {} for reading".format(path)) 0039 else: 0040 doc = QtXml.QDomDocument() 0041 doc.setContent(xmlFile.readAll()) 0042 root = doc.documentElement() 0043 if root.tagName() != "map": 0044 print("Error: The map description file should begin with the map tag") 0045 else: 0046 imagePath = QtCore.QFileInfo(path).absolutePath() + "/" + root.firstChildElement("mapFile").text() 0047 0048 if not QtCore.QFile.exists(imagePath): 0049 print ("Error: Map file {} does not exist".format(imagePath)) 0050 sys.exit(2) 0051 0052 colorList = set() 0053 divisionTag = root.firstChildElement("division"); 0054 while not divisionTag.isNull(): 0055 colorTag = divisionTag.firstChildElement("color"); 0056 red = int(colorTag.firstChildElement("red").text()) 0057 green = int(colorTag.firstChildElement("green").text()) 0058 blue = int(colorTag.firstChildElement("blue").text()) 0059 rgba = (red, green, blue, 255) 0060 if rgba not in colorList: 0061 colorList.add(rgba) 0062 else: 0063 print("Error: The color {},{},{} is used more than once in the kgm file".format(red, green, blue)) 0064 0065 divisionTag = divisionTag.nextSiblingElement("division"); 0066 0067 image = Image.open(imagePath) 0068 error = image.mode != 'P' 0069 if error: 0070 print("Error: The PNG file should be in indexed color mode") 0071 image = image.convert('RGBA') 0072 usedColors = set([rgba for count, rgba in image.getcolors()]) 0073 notFoundColors = usedColors - colorList 0074 0075 error |= len(notFoundColors) > 0 0076 if notFoundColors: 0077 pixels = image.load() 0078 width, height = image.size 0079 notFoundColorsToSearch = notFoundColors 0080 for x in range(width): 0081 for y in range(height): 0082 rgba = pixels[x, y] 0083 if rgba in notFoundColorsToSearch: 0084 print ("Error: The pixel (%d ,%d) has color rgba %d,%d,%d,%d that is not defined in the kgm file" % ( 0085 x, y, rgba[0], rgba[1], rgba[2], rgba[3])) 0086 notFoundColorsToSearch.remove(rgba) 0087 0088 nonUsedColors = colorList 0089 nonUsedColors.difference_update(usedColors) 0090 error |= len(nonUsedColors) > 0 0091 for rgba in nonUsedColors: 0092 print("Error: the rgb(a) color {},{},{},({}) is absent from the png pixels".format( 0093 rgba[0], rgba[1], rgba[2], rgba[3])) 0094 0095 if not error: 0096 print("The map is correctly formed") 0097 xmlFile.close(); 0098 0099 sys.exit(0)