Skip to content

Commit

Permalink
New Identicons
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 22, 2024
1 parent 552b708 commit 1ff2d03
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package convex.gui.components;


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import convex.gui.MainGUI;
import convex.gui.utils.Toolkit;
import net.miginfocom.swing.MigLayout;

/**
* Base class for Convex GUI apps
Expand All @@ -35,8 +35,8 @@ public void run() {
Toolkit.closeIfFirstFrame(frame);


frame.getContentPane().setLayout(new MigLayout());
frame.getContentPane().add(AbstractGUI.this, "dock center");
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(AbstractGUI.this, BorderLayout.CENTER);
frame.setVisible(true);


Expand Down
75 changes: 62 additions & 13 deletions convex-gui/src/main/java/convex/gui/components/Identicon.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package convex.gui.components;

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.util.WeakHashMap;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;

import convex.core.data.ABlob;
import convex.core.data.AArrayBlob;
import convex.core.data.Blobs;
import convex.core.data.Hash;
import convex.core.data.prim.CVMLong;
import convex.core.util.Utils;
import convex.gui.utils.Toolkit;

Expand All @@ -16,27 +21,71 @@
@SuppressWarnings("serial")
public class Identicon extends JLabel {

public static BufferedImage createImage(ABlob data, int renderSize) {
int SIZE = 3;
byte[] bs = data.getBytes();

BufferedImage bi = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
protected final int size;

public BufferedImage createImage(AArrayBlob data, int renderSize) {
int SIZE = size;
byte[] bs = Blobs.ensureZeroBasedArray(data);

BufferedImage bi = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
int[] cols=new int[4];

// last 12 bytes define colours, 3 bytes per colour, we mask the first byte for ARGB
for (int i=0; i<4; i++) {
int col=0xFF000000|(Utils.readInt(bs, 20+i*3-1));
cols[i]=col;
}

// First 20 bytes define bitmap
for (int y = 0; y < SIZE; y++) {
for (int x = 0; x < SIZE; x++) {
int i = x + y * SIZE;
int bits = Utils.extractBits(bs, 9, 9 * i); // take 3 bits per channel
int rgb = ((bits & 0b111000000) << 15) + ((bits & 0b111000) << 10) + ((bits & 0b111) << 5);
for (int x = 0; x <= SIZE/2; x++) {
int i = (x + y * SIZE)%(20*4); // 4 2-byte segments per byte
int bits = Utils.extractBits(bs, 2, 2 * i); // take 2 bits for colour index
int rgb = cols[bits];
bi.setRGB(x, y, rgb);
bi.setRGB(SIZE-x-1, y, rgb);
}
}

return Toolkit.smoothResize(bi, renderSize, renderSize);
return Toolkit.pixelResize(bi, renderSize, renderSize);
}

private WeakHashMap<AArrayBlob,ImageIcon> wm=new WeakHashMap<>();

private ImageIcon createIcon(AArrayBlob hash, int size) {
ImageIcon cached=wm.get(hash);
if ((cached!=null)&&(cached.getImage().getWidth(null)==size)) return cached;

ImageIcon result=new ImageIcon(createImage(hash,size));
wm.put(hash, result);
return result;
}

public Identicon(ABlob a) {
super(new ImageIcon(Identicon.createImage(a, 36)));

public Identicon(AArrayBlob a) {
super();
if (a.count()<32) throw new IllegalArgumentException("Blob too short for Identicon, must be 32+ bytes. Maybe use a Hash?");
size=7;
ImageIcon icon=createIcon(a, 36);
setIcon(icon);
this.setToolTipText(a.toString());
setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
}

public static class IdenticApp extends AbstractGUI{
public IdenticApp() {
setLayout(new FlowLayout());
for (int i=0; i<256; i++) {
add(new Identicon(Hash.compute(CVMLong.create(i).getHash())));
}
}
}

public static void main(String... args) {
Toolkit.init();
IdenticApp app=new IdenticApp() {

};
app.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public WalletComponent(Convex convex,BasicWalletEntry initialWalletEntry) {
setLayout(new MigLayout("fillx"));

// identicon
JLabel identicon = new Identicon(walletEntry.getIdenticonHash());
JLabel identicon = new Identicon(walletEntry.getPublicKey());
JPanel idPanel=new JPanel();
idPanel.add(identicon);
add(idPanel,"west"); // add to MigLayout
Expand Down
10 changes: 10 additions & 0 deletions convex-gui/src/main/java/convex/gui/utils/Toolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ public static BufferedImage smoothResize(BufferedImage src, int w, int h) {
graphics.drawImage(src, 0, 0, w, h, null);
return newImage;
}

public static BufferedImage pixelResize(BufferedImage src, int w, int h) {
BufferedImage newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D) newImage.getGraphics();

graphics.drawImage(src, 0, 0, w, h, null);
return newImage;
}

public static DocumentListener createDocumentListener(final Runnable a) {
return new DocumentListener() {
Expand Down Expand Up @@ -233,4 +241,6 @@ public void actionPerformed(ActionEvent e) {
}
};
}


}

0 comments on commit 1ff2d03

Please sign in to comment.