diff --git a/tools/colorchecker.py b/tools/colorchecker.py --- a/tools/colorchecker.py +++ b/tools/colorchecker.py @@ -31,67 +31,84 @@ path = sys.argv[fileIndex] print("Processing " + path) xmlFile = QtCore.QFile(path) - - if xmlFile.exists(): - if (xmlFile.open(QtCore.QIODevice.ReadOnly)): + + if not xmlFile.exists(): + print("Error: File {} does not exist".format(path)) + else: + if not xmlFile.open(QtCore.QIODevice.ReadOnly): + print("Error: Could not open {} for reading".format(path)) + else: doc = QtXml.QDomDocument() doc.setContent(xmlFile.readAll()) root = doc.documentElement() - if (root.tagName() == "map"): + if root.tagName() != "map": + print("Error: The map description file should begin with the map tag") + else: imagePath = QtCore.QFileInfo(path).absolutePath() + "/" + root.firstChildElement("mapFile").text() - + if not QtCore.QFile.exists(imagePath): print ("Error: Map file {} does not exist".format(imagePath)) sys.exit(2) - + colorList = []; divisionTag = root.firstChildElement("division"); - while (not divisionTag.isNull()): + while not divisionTag.isNull(): colorTag = divisionTag.firstChildElement("color"); red = int(colorTag.firstChildElement("red").text()) green = int(colorTag.firstChildElement("green").text()) blue = int(colorTag.firstChildElement("blue").text()) - contains = colorList.count(QtGui.qRgb(red, green, blue)) - if (contains == 0): - colorList.append(QtGui.qRgb(red, green, blue)) + rgba = QtGui.qRgba(red, green, blue, 255) + if rgba not in colorList: + colorList.append(rgba) else: print("Error: The color {},{},{} is used more than once in the kgm file".format(red, green, blue)) - + divisionTag = divisionTag.nextSiblingElement("division"); - + image = QtGui.QImage(imagePath) - error = False + ct = image.colorTable() + error = len(image.colorTable()) == 0 + if error: + print("Error: The PNG file should be in indexed color mode") + ac = image.convertToFormat(QtGui.QImage.Format_Alpha8) + act = ac.colorTable() usedColors = set() + notfoundcolors = {} for x in range(0, image.width()): for y in range(0, image.height()): - rgbcolor = image.pixel(x,y) - qcolor = QtGui.qRgb(QtGui.qRed(rgbcolor), QtGui.qGreen(rgbcolor), QtGui.qBlue(rgbcolor)) - contains = colorList.count(qcolor) - usedColors.add(qcolor) - if contains == 0: - qcolor = QtGui.QColor(rgbcolor) - print("Error: The pixel ({},{}) has color {},{},{} that is not defined in the kgm file".format(x, y, qcolor.red(), qcolor.green(), qcolor.blue())) - error = True - + rgbcolor = image.pixel(x, y) + ipix = image.pixelIndex(x, y) + c = ct[ipix] + al0 = ac.pixel(x, y) + al = QtGui.qAlpha(al0) + rgba = QtGui.qRgba(QtGui.qRed(rgbcolor), QtGui.qGreen(rgbcolor), QtGui.qBlue(rgbcolor), al) + usedColors.add(rgba) + if rgbcolor not in colorList: + tri = notfoundcolors.get(rgba) + if tri is None: + notfoundcolors[rgba] = [(x, y), 1, (x, y)] + else: + tri[1] += 1 + tri[2] = (x, y) + + error |= len(notfoundcolors) > 0 + for rgba, tri in notfoundcolors.items(): + qcolor = QtGui.QColor.fromRgba(rgba) + first, c, last = tri + x, y = first + print ("Error: The pixel (%d ,%d) has color rgba %d,%d,%d,%d that is not defined in the kgm file" % ( + x, y, qcolor.red(), qcolor.green(), qcolor.blue(), qcolor.alpha())) + + nonUsedColors = set(colorList) + nonUsedColors.difference_update(usedColors) + error |= len(nonUsedColors) > 0 + for rgba in nonUsedColors: + qcolor = QtGui.QColor.fromRgba(rgba) + print("Error: the rgb(a) color {},{},{},({}) is absent from the png pixels".format( + qcolor.red(), qcolor.green(), qcolor.blue(), qcolor.alpha())) + if not error: - if len(colorList) == len(usedColors): - if (len(image.colorTable()) > 0): - print("The map is correctly formed") - else: - print("Error: The PNG file should be in indexed color mode") - else: - nonUsedColors = set(colorList) - nonUsedColors.difference_update(usedColors) - print("Warning: There are colors defined that are not used in the map file") - for color in nonUsedColors: - qcolor = QtGui.QColor(color) - print("{},{},{}".format(qcolor.red(), qcolor.green(), qcolor.blue())) - else: - print("Error: The map description file should begin with the map tag") + print("The map is correctly formed") xmlFile.close(); - else: - print("Error: Could not open {} for reading".format(path)) - else: - print("Error: File {} does not exist".format(path)) sys.exit(0)