2021-07-27 15:09:09 +02:00
|
|
|
###
|
|
|
|
# qidenticon.py is Licesensed under FreeBSD License.
|
|
|
|
# (http://www.freebsd.org/copyright/freebsd-license.html)
|
|
|
|
#
|
|
|
|
# Copyright 1994-2009 Shin Adachi. All rights reserved.
|
|
|
|
# Copyright 2013 "Sendiulo". All rights reserved.
|
|
|
|
# Copyright 2018-2021 The Bitmessage Developers. All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms,
|
|
|
|
# with or without modification, are permitted provided that the following
|
|
|
|
# conditions are met:
|
|
|
|
#
|
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
|
|
|
|
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
2018-05-16 13:31:06 +02:00
|
|
|
# pylint: disable=too-many-locals,too-many-arguments,too-many-function-args
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2019-10-11 13:14:08 +02:00
|
|
|
Usage
|
|
|
|
-----
|
2013-08-27 11:47:14 +02:00
|
|
|
|
2021-07-27 15:09:09 +02:00
|
|
|
>>> import qidenticon
|
|
|
|
>>> qidenticon.render_identicon(code, size)
|
2013-08-27 11:47:14 +02:00
|
|
|
|
2021-07-27 15:09:09 +02:00
|
|
|
Returns an instance of :class:`QPixmap` which have generated identicon image.
|
2019-10-11 13:14:08 +02:00
|
|
|
``size`` specifies `patch size`. Generated image size is 3 * ``size``.
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
|
|
|
|
2021-08-25 17:12:36 +02:00
|
|
|
import six.moves
|
2021-03-03 17:15:00 +01:00
|
|
|
try:
|
|
|
|
from PyQt5 import QtCore, QtGui
|
|
|
|
except ImportError:
|
|
|
|
from PyQt4 import QtCore, QtGui
|
2013-08-27 11:47:14 +02:00
|
|
|
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
class IdenticonRendererBase(object):
|
2018-05-16 13:31:06 +02:00
|
|
|
"""Encapsulate methods around rendering identicons"""
|
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
PATH_SET = []
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
def __init__(self, code):
|
|
|
|
"""
|
2019-10-11 13:14:08 +02:00
|
|
|
:param code: code for icon
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
|
|
|
if not isinstance(code, int):
|
|
|
|
code = int(code)
|
|
|
|
self.code = code
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-18 17:39:45 +02:00
|
|
|
def render(self, size, twoColor, opacity, penwidth):
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2021-07-27 15:09:09 +02:00
|
|
|
render identicon to QPixmap
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2019-10-11 13:14:08 +02:00
|
|
|
:param size: identicon patchsize. (image size is 3 * [size])
|
2021-07-27 15:09:09 +02:00
|
|
|
:returns: :class:`QPixmap`
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# decode the code
|
2021-03-03 17:15:00 +01:00
|
|
|
middle, corner, side, foreColor, secondColor, swap_cross = \
|
|
|
|
self.decode(self.code, twoColor)
|
2013-08-27 11:47:14 +02:00
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
# make image
|
2021-03-03 17:15:00 +01:00
|
|
|
image = QtGui.QPixmap(
|
|
|
|
QtCore.QSize(size * 3 + penwidth, size * 3 + penwidth))
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# fill background
|
2018-05-16 13:31:06 +02:00
|
|
|
backColor = QtGui.QColor(255, 255, 255, opacity)
|
2013-09-17 10:55:26 +02:00
|
|
|
image.fill(backColor)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
kwds = {
|
2013-09-16 21:08:55 +02:00
|
|
|
'image': image,
|
2013-08-27 11:47:14 +02:00
|
|
|
'size': size,
|
2013-09-17 10:55:26 +02:00
|
|
|
'foreColor': foreColor if swap_cross else secondColor,
|
|
|
|
'penwidth': penwidth,
|
2013-08-27 11:47:14 +02:00
|
|
|
'backColor': backColor}
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# middle patch
|
2021-03-03 17:15:00 +01:00
|
|
|
image = self.drawPatchQt(
|
|
|
|
(1, 1), middle[2], middle[1], middle[0], **kwds)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# side patch
|
2013-09-17 10:55:26 +02:00
|
|
|
kwds['foreColor'] = foreColor
|
2018-05-16 13:31:06 +02:00
|
|
|
kwds['patch_type'] = side[0]
|
2021-08-25 17:12:36 +02:00
|
|
|
for i in six.moves.range(4):
|
2013-08-27 11:47:14 +02:00
|
|
|
pos = [(1, 0), (2, 1), (1, 2), (0, 1)][i]
|
2013-09-16 21:08:55 +02:00
|
|
|
image = self.drawPatchQt(pos, side[2] + 1 + i, side[1], **kwds)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# corner patch
|
2013-09-17 10:55:26 +02:00
|
|
|
kwds['foreColor'] = secondColor
|
2018-05-16 13:31:06 +02:00
|
|
|
kwds['patch_type'] = corner[0]
|
2021-08-25 17:12:36 +02:00
|
|
|
for i in six.moves.range(4):
|
2013-08-27 11:47:14 +02:00
|
|
|
pos = [(0, 0), (2, 0), (2, 2), (0, 2)][i]
|
2013-09-16 21:08:55 +02:00
|
|
|
image = self.drawPatchQt(pos, corner[2] + 1 + i, corner[1], **kwds)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
return image
|
2013-09-16 21:08:55 +02:00
|
|
|
|
2021-03-03 17:15:00 +01:00
|
|
|
def drawPatchQt(
|
|
|
|
self, pos, turn, invert, patch_type, image, size, foreColor,
|
|
|
|
backColor, penwidth): # pylint: disable=unused-argument
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2019-10-11 13:14:08 +02:00
|
|
|
:param size: patch size
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2018-05-16 13:31:06 +02:00
|
|
|
path = self.PATH_SET[patch_type]
|
2013-08-27 11:47:14 +02:00
|
|
|
if not path:
|
|
|
|
# blank patch
|
|
|
|
invert = not invert
|
|
|
|
path = [(0., 0.), (1., 0.), (1., 1.), (0., 1.), (0., 0.)]
|
2013-09-16 21:08:55 +02:00
|
|
|
|
2021-03-03 17:15:00 +01:00
|
|
|
polygon = QtGui.QPolygonF([
|
|
|
|
QtCore.QPointF(x * size, y * size) for x, y in path])
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
rot = turn % 4
|
2021-03-03 17:15:00 +01:00
|
|
|
rect = [
|
|
|
|
QtCore.QPointF(0., 0.), QtCore.QPointF(size, 0.),
|
|
|
|
QtCore.QPointF(size, size), QtCore.QPointF(0., size)]
|
2018-05-16 13:31:06 +02:00
|
|
|
rotation = [0, 90, 180, 270]
|
|
|
|
|
2021-03-03 17:15:00 +01:00
|
|
|
nopen = QtGui.QPen(foreColor, QtCore.Qt.NoPen)
|
|
|
|
foreBrush = QtGui.QBrush(foreColor, QtCore.Qt.SolidPattern)
|
2013-09-17 10:55:26 +02:00
|
|
|
if penwidth > 0:
|
2013-09-18 17:39:45 +02:00
|
|
|
pen_color = QtGui.QColor(255, 255, 255)
|
2021-03-03 17:15:00 +01:00
|
|
|
pen = QtGui.QPen(pen_color, QtCore.Qt.SolidPattern)
|
2013-09-17 10:55:26 +02:00
|
|
|
pen.setWidth(penwidth)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2021-03-03 17:15:00 +01:00
|
|
|
painter = QtGui.QPainter()
|
2013-09-16 21:08:55 +02:00
|
|
|
painter.begin(image)
|
2013-09-17 10:55:26 +02:00
|
|
|
painter.setPen(nopen)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2021-03-03 17:15:00 +01:00
|
|
|
painter.translate(
|
|
|
|
pos[0] * size + penwidth / 2, pos[1] * size + penwidth / 2)
|
2013-09-17 10:55:26 +02:00
|
|
|
painter.translate(rect[rot])
|
2013-09-16 21:08:55 +02:00
|
|
|
painter.rotate(rotation[rot])
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-17 10:55:26 +02:00
|
|
|
if invert:
|
|
|
|
# subtract the actual polygon from a rectangle to invert it
|
2021-03-03 17:15:00 +01:00
|
|
|
poly_rect = QtGui.QPolygonF(rect)
|
2013-09-18 17:39:45 +02:00
|
|
|
polygon = poly_rect.subtracted(polygon)
|
2013-09-16 21:08:55 +02:00
|
|
|
painter.setBrush(foreBrush)
|
2013-09-17 10:55:26 +02:00
|
|
|
if penwidth > 0:
|
|
|
|
# draw the borders
|
|
|
|
painter.setPen(pen)
|
2021-03-03 17:15:00 +01:00
|
|
|
painter.drawPolygon(polygon, QtCore.Qt.WindingFill)
|
2013-09-17 10:55:26 +02:00
|
|
|
# draw the fill
|
|
|
|
painter.setPen(nopen)
|
2021-03-03 17:15:00 +01:00
|
|
|
painter.drawPolygon(polygon, QtCore.Qt.WindingFill)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
painter.end()
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-16 21:08:55 +02:00
|
|
|
return image
|
2013-08-27 11:47:14 +02:00
|
|
|
|
2018-05-16 13:31:06 +02:00
|
|
|
def decode(self, code, twoColor):
|
|
|
|
"""virtual functions"""
|
2013-08-27 11:47:14 +02:00
|
|
|
raise NotImplementedError
|
2018-05-16 13:31:06 +02:00
|
|
|
|
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
class DonRenderer(IdenticonRendererBase):
|
|
|
|
"""
|
2021-07-27 15:09:09 +02:00
|
|
|
Don Park's implementation of identicon, see:
|
|
|
|
https://blog.docuverse.com/2007/01/18/identicon-updated-and-source-released
|
2013-08-27 11:47:14 +02:00
|
|
|
"""
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
PATH_SET = [
|
2018-05-16 13:31:06 +02:00
|
|
|
# [0] full square:
|
2013-09-18 17:39:45 +02:00
|
|
|
[(0, 0), (4, 0), (4, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [1] right-angled triangle pointing top-left:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (4, 0), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [2] upwardy triangle:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 0), (4, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [3] left half of square, standing rectangle:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (2, 0), (2, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [4] square standing on diagonale:
|
2013-09-18 17:39:45 +02:00
|
|
|
[(2, 0), (4, 2), (2, 4), (0, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [5] kite pointing topleft:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (4, 2), (4, 4), (2, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [6] Sierpinski triangle, fractal triangles:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 0), (4, 4), (2, 4), (3, 2), (1, 2), (2, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [7] sharp angled lefttop pointing triangle:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (4, 2), (2, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [8] small centered square:
|
2013-09-18 17:39:45 +02:00
|
|
|
[(1, 1), (3, 1), (3, 3), (1, 3)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [9] two small triangles:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 0), (4, 0), (0, 4), (0, 2), (2, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [10] small topleft square:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (2, 0), (2, 2), (0, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [11] downpointing right-angled triangle on bottom:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 2), (4, 2), (2, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [12] uppointing right-angled triangle on bottom:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 2), (4, 4), (0, 4)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [13] small rightbottom pointing right-angled triangle on topleft:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(2, 0), (2, 2), (0, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [14] small lefttop pointing right-angled triangle on topleft:
|
2013-08-27 11:47:14 +02:00
|
|
|
[(0, 0), (2, 0), (0, 2)],
|
2018-05-16 13:31:06 +02:00
|
|
|
# [15] empty:
|
2013-09-18 17:39:45 +02:00
|
|
|
[]]
|
2021-03-03 17:15:00 +01:00
|
|
|
# get the [0] full square, [4] square standing on diagonale,
|
|
|
|
# [8] small centered square, or [15] empty tile:
|
2013-08-27 11:47:14 +02:00
|
|
|
MIDDLE_PATCH_SET = [0, 4, 8, 15]
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
# modify path set
|
2021-03-03 17:15:00 +01:00
|
|
|
for idx, path in enumerate(PATH_SET):
|
|
|
|
if path:
|
|
|
|
p = [(vec[0] / 4.0, vec[1] / 4.0) for vec in path]
|
2013-08-27 11:47:14 +02:00
|
|
|
PATH_SET[idx] = p + p[:1]
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-17 10:55:26 +02:00
|
|
|
def decode(self, code, twoColor):
|
2018-05-16 13:31:06 +02:00
|
|
|
"""decode the code"""
|
|
|
|
|
|
|
|
shift = 0
|
|
|
|
middleType = (code >> shift) & 0x03
|
|
|
|
shift += 2
|
|
|
|
middleInvert = (code >> shift) & 0x01
|
|
|
|
shift += 1
|
|
|
|
cornerType = (code >> shift) & 0x0F
|
|
|
|
shift += 4
|
|
|
|
cornerInvert = (code >> shift) & 0x01
|
|
|
|
shift += 1
|
|
|
|
cornerTurn = (code >> shift) & 0x03
|
|
|
|
shift += 2
|
|
|
|
sideType = (code >> shift) & 0x0F
|
|
|
|
shift += 4
|
|
|
|
sideInvert = (code >> shift) & 0x01
|
|
|
|
shift += 1
|
|
|
|
sideTurn = (code >> shift) & 0x03
|
|
|
|
shift += 2
|
|
|
|
blue = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
green = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
red = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
second_blue = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
second_green = (code >> shift) & 0x1F
|
|
|
|
shift += 5
|
|
|
|
second_red = (code >> shift) & 0x1F
|
|
|
|
shift += 1
|
|
|
|
swap_cross = (code >> shift) & 0x01
|
|
|
|
|
2013-09-17 10:55:26 +02:00
|
|
|
middleType = self.MIDDLE_PATCH_SET[middleType]
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
foreColor = (red << 3, green << 3, blue << 3)
|
2013-09-16 21:08:55 +02:00
|
|
|
foreColor = QtGui.QColor(*foreColor)
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-09-17 10:55:26 +02:00
|
|
|
if twoColor:
|
2021-03-03 17:15:00 +01:00
|
|
|
secondColor = (
|
|
|
|
second_blue << 3, second_green << 3, second_red << 3)
|
2013-09-17 10:55:26 +02:00
|
|
|
secondColor = QtGui.QColor(*secondColor)
|
|
|
|
else:
|
|
|
|
secondColor = foreColor
|
2018-05-16 13:31:06 +02:00
|
|
|
|
2013-08-27 11:47:14 +02:00
|
|
|
return (middleType, middleInvert, 0),\
|
|
|
|
(cornerType, cornerInvert, cornerTurn),\
|
|
|
|
(sideType, sideInvert, sideTurn),\
|
2018-05-16 13:31:06 +02:00
|
|
|
foreColor, secondColor, swap_cross
|
2013-08-27 11:47:14 +02:00
|
|
|
|
|
|
|
|
2021-03-03 17:15:00 +01:00
|
|
|
def render_identicon(
|
|
|
|
code, size, twoColor=False, opacity=255, penwidth=0, renderer=None):
|
2018-05-16 13:31:06 +02:00
|
|
|
"""Render an image"""
|
2013-08-27 11:47:14 +02:00
|
|
|
if not renderer:
|
|
|
|
renderer = DonRenderer
|
2018-05-16 13:31:06 +02:00
|
|
|
return renderer(code).render(size, twoColor, opacity, penwidth)
|