summaryrefslogtreecommitdiffstats
path: root/identicons-cli.go
blob: 05a76677e34e9d919d653acc4fe08bb82b29c53d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package main

import (
	"bytes"
	"crypto/sha256"
	"encoding/base64"
	"flag"
	"fmt"
	"image"
	"image/color"
	"image/png"
	"os"
)

// OptimizedIdenticon with indexed colors for smaller file sizes
type OptimizedIdenticon struct {
	source []byte
	size   int
}

// NewOptimizedIdenticon creates a generator with indexed colors
func NewOptimizedIdenticon(source []byte) *OptimizedIdenticon {
	return &OptimizedIdenticon{
		source: source,
		size:   256,
	}
}

// NewOptimizedIdenticonWithSize creates a generator with custom size
func NewOptimizedIdenticonWithSize(source []byte, size int) *OptimizedIdenticon {
	return &OptimizedIdenticon{
		source: source,
		size:   size,
	}
}

// getBit returns the n-th bit (0-indexed) from source
func (identicon *OptimizedIdenticon) getBit(n int) bool {
	if len(identicon.source) == 0 || n < 0 {
		return false
	}
	byteIndex := n / 8
	bitIndex := n % 8
	if byteIndex >= len(identicon.source) {
		return false
	}
	return (identicon.source[byteIndex]>>bitIndex)&1 == 1
}

// getByte returns the n-th byte, wraps around if needed
func (identicon *OptimizedIdenticon) getByte(n int) byte {
	if len(identicon.source) == 0 {
		return 0
	}
	return identicon.source[n%len(identicon.source)]
}

// getColorIndices returns color indices for indexed version
func (identicon *OptimizedIdenticon) getColorIndices() (primaryIndex, secondaryIndex, bgIndex uint8) {
	if len(identicon.source) < 32 {
		return 0, 1, 2
	}

	// Primary color index (4 bits → 16 colors)
	primaryIndex = 0
	for i := 0; i < 4; i++ {
		if identicon.getBit(248 + i) {
			primaryIndex |= 1 << i
		}
	}
	primaryIndex %= 16

	// Secondary color index (4 bits → 16 colors)
	secondaryIndex = 0
	for i := 0; i < 4; i++ {
		if identicon.getBit(244 + i) {
			secondaryIndex |= 1 << i
		}
	}
	secondaryIndex %= 16

	// Background choice (2 bits → 4 options)
	bgChoice := 0
	for i := 0; i < 2; i++ {
		if identicon.getBit(252 + i) {
			bgChoice |= 1 << i
		}
	}
	bgIndex = uint8(bgChoice % 3) // 0, 1, or 2

	return primaryIndex, secondaryIndex, bgIndex
}

// generatePixelPattern generates 5x5 symmetric pixel grid
func (identicon *OptimizedIdenticon) generatePixelPattern() ([]bool, []bool) {
	primary := make([]bool, 25)
	secondary := make([]bool, 25)

	// Use bits 0-14 for primary pattern
	bitIndex := 0
	for row := 0; row < 5; row++ {
		for col := 0; col < 3; col++ {
			paint := identicon.getBit(bitIndex)
			bitIndex++

			ix := row*5 + col
			mirrorIx := row*5 + (4 - col)
			primary[ix] = paint
			primary[mirrorIx] = paint
		}
	}

	// Use bits 15-29 for secondary pattern
	for row := 0; row < 5; row++ {
		for col := 0; col < 3; col++ {
			paint := identicon.getBit(bitIndex)
			bitIndex++

			ix := row*5 + col
			mirrorIx := row*5 + (4 - col)
			secondary[ix] = paint
			secondary[mirrorIx] = paint
		}
	}

	return primary, secondary
}

// createPalette creates an optimized palette with only necessary colors
func createPalette(primaryIdx, secondaryIdx, bgIdx uint8, darkMode bool) color.Palette {
	// Optimized color palettes - only 16 colors per palette
	primaryPalette := []color.Color{
		color.RGBA{0x00, 0xbf, 0x93, 0xff}, // turquoise
		color.RGBA{0x2d, 0xcc, 0x70, 0xff}, // mint
		color.RGBA{0x42, 0xe4, 0x53, 0xff}, // green
		color.RGBA{0xf1, 0xc4, 0x0f, 0xff}, // yellowOrange
		color.RGBA{0xe6, 0x7f, 0x22, 0xff}, // brown
		color.RGBA{0xff, 0x94, 0x4e, 0xff}, // orange
		color.RGBA{0xe8, 0x4c, 0x3d, 0xff}, // red
		color.RGBA{0x35, 0x98, 0xdb, 0xff}, // blue
		color.RGBA{0x9a, 0x59, 0xb5, 0xff}, // purple
		color.RGBA{0xef, 0x3e, 0x96, 0xff}, // magenta
		color.RGBA{0xdf, 0x21, 0xb9, 0xff}, // violet
		color.RGBA{0x7d, 0xc2, 0xd2, 0xff}, // lightBlue
		color.RGBA{0x16, 0xa0, 0x86, 0xff}, // turquoiseIntense
		color.RGBA{0x27, 0xae, 0x61, 0xff}, // mintIntense
		color.RGBA{0x24, 0xc3, 0x33, 0xff}, // greenIntense
		color.RGBA{0x1c, 0xab, 0xbb, 0xff}, // lightBlueIntense
	}

	secondaryPalette := []color.Color{
		color.RGBA{0x34, 0x49, 0x5e, 0xff}, // darkBlue
		color.RGBA{0x95, 0xa5, 0xa5, 0xff}, // grey
		color.RGBA{0xd2, 0x54, 0x00, 0xff}, // brownIntense
		color.RGBA{0xc1, 0x39, 0x2b, 0xff}, // redIntense
		color.RGBA{0x29, 0x7f, 0xb8, 0xff}, // blueIntense
		color.RGBA{0x8d, 0x44, 0xad, 0xff}, // purpleIntense
		color.RGBA{0xbe, 0x12, 0x7e, 0xff}, // violetIntense
		color.RGBA{0xe5, 0x23, 0x83, 0xff}, // magentaIntense
		color.RGBA{0x27, 0xae, 0x61, 0xff}, // mintIntense
		color.RGBA{0x24, 0xc3, 0x33, 0xff}, // greenIntense
		color.RGBA{0xd9, 0xd9, 0x21, 0xff}, // yellowIntense
		color.RGBA{0xf3, 0x9c, 0x11, 0xff}, // yellowOrangeIntense
		color.RGBA{0xff, 0x55, 0x00, 0xff}, // orangeIntense
		color.RGBA{0x1c, 0xab, 0xbb, 0xff}, // lightBlueIntense
		color.RGBA{0x23, 0x23, 0x23, 0xff}, // lightBlackIntense
		color.RGBA{0x7e, 0x8c, 0x8d, 0xff}, // greyIntense
	}

	// Background colors based on mode
	lightBackgrounds := []color.Color{
		color.RGBA{255, 255, 255, 255}, // white
		color.RGBA{243, 245, 247, 255}, // light gray 1
		color.RGBA{236, 240, 241, 255}, // light gray 2
		color.RGBA{0, 0, 0, 0},         // transparent (position 3)
	}

	darkBackgrounds := []color.Color{
		color.RGBA{30, 30, 30, 255},  // dark gray
		color.RGBA{45, 62, 80, 255},  // dark blue
		color.RGBA{57, 57, 57, 255},  // dark gray 2
		color.RGBA{0, 0, 0, 0},       // transparent (position 3)
	}

	// Palette in exact order:
	// 0: Background
	// 1: Primary color
	// 2: Secondary color
	// 3: Transparent (optional)
	palette := make(color.Palette, 0, 4)

	// Background first
	if darkMode {
		palette = append(palette, darkBackgrounds[bgIdx])
	} else {
		palette = append(palette, lightBackgrounds[bgIdx])
	}

	// Then primary and secondary colors
	palette = append(palette,
		primaryPalette[primaryIdx],
		secondaryPalette[secondaryIdx],
		color.RGBA{0, 0, 0, 0}, // transparent as last option
	)

	return palette
}

// Generate48x48ForFace creates a 48x48 pixel image specifically for Face headers
func (identicon *OptimizedIdenticon) Generate48x48ForFace(transparent bool) *image.Paletted {
	const (
		size       = 48
		spriteSize = 5
		pixelSize  = 6                             // 48/8 = 6
		margin     = (size - pixelSize*spriteSize) / 2 // = 9
	)

	// Determine color indices
	primaryIdx, secondaryIdx, bgIdx := identicon.getColorIndices()

	// For transparent background, set bgIdx to 3 (transparent)
	if transparent {
		bgIdx = 3
	}

	// Palette for export (always light mode for better compatibility)
	palette := createPalette(primaryIdx, secondaryIdx, bgIdx, false)

	// Create image
	img := image.NewPaletted(image.Rect(0, 0, size, size), palette)

	// Fill background
	bgIndex := uint8(0)
	if transparent {
		bgIndex = 3 // transparent
	}

	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			img.SetColorIndex(j, i, bgIndex)
		}
	}

	primaryPixels, secondaryPixels := identicon.generatePixelPattern()

	// Secondary pixels (index 2)
	for row := 0; row < spriteSize; row++ {
		for col := 0; col < spriteSize; col++ {
			if secondaryPixels[row*spriteSize+col] {
				x := col*pixelSize + margin
				y := row*pixelSize + margin
				for py := y; py < y+pixelSize; py++ {
					for px := x; px < x+pixelSize; px++ {
						if px < size && py < size {
							img.SetColorIndex(px, py, 2)
						}
					}
				}
			}
		}
	}

	// Primary pixels (index 1)
	for row := 0; row < spriteSize; row++ {
		for col := 0; col < spriteSize; col++ {
			if primaryPixels[row*spriteSize+col] {
				x := col*pixelSize + margin
				y := row*pixelSize + margin
				for py := y; py < y+pixelSize; py++ {
					for px := x; px < x+pixelSize; px++ {
						if px < size && py < size {
							img.SetColorIndex(px, py, 1)
						}
					}
				}
			}
		}
	}

	return img
}

// GenerateForExportOptimized for indexed export (256x256)
func (identicon *OptimizedIdenticon) GenerateForExportOptimized(transparent bool) *image.Paletted {
	const (
		spriteSize = 5
	)

	pixelSize := identicon.size / 8
	margin := (identicon.size - pixelSize*spriteSize) / 2

	// Determine color indices
	primaryIdx, secondaryIdx, bgIdx := identicon.getColorIndices()

	// For transparent background, set bgIdx to 3 (transparent)
	if transparent {
		bgIdx = 3
	}

	// Palette for export (always light mode for better compatibility)
	palette := createPalette(primaryIdx, secondaryIdx, bgIdx, false)

	// Create image
	img := image.NewPaletted(image.Rect(0, 0, identicon.size, identicon.size), palette)

	// Fill background
	bgIndex := uint8(0)
	if transparent {
		bgIndex = 3 // transparent
	}

	for i := 0; i < identicon.size; i++ {
		for j := 0; j < identicon.size; j++ {
			img.SetColorIndex(j, i, bgIndex)
		}
	}

	primaryPixels, secondaryPixels := identicon.generatePixelPattern()

	// Secondary pixels (index 2)
	for row := 0; row < spriteSize; row++ {
		for col := 0; col < spriteSize; col++ {
			if secondaryPixels[row*spriteSize+col] {
				x := col*pixelSize + margin
				y := row*pixelSize + margin
				for py := y; py < y+pixelSize; py++ {
					for px := x; px < x+pixelSize; px++ {
						if px < identicon.size && py < identicon.size {
							img.SetColorIndex(px, py, 2)
						}
					}
				}
			}
		}
	}

	// Primary pixels (index 1)
	for row := 0; row < spriteSize; row++ {
		for col := 0; col < spriteSize; col++ {
			if primaryPixels[row*spriteSize+col] {
				x := col*pixelSize + margin
				y := row*pixelSize + margin
				for py := y; py < y+pixelSize; py++ {
					for px := x; px < x+pixelSize; px++ {
						if px < identicon.size && py < identicon.size {
							img.SetColorIndex(px, py, 1)
						}
					}
				}
			}
		}
	}

	return img
}

func main() {
	// CLI flags
	input := flag.String("input", "", "Input text (username|email|pubkey)")
	size := flag.Int("size", 48, "Image size (48 or 256)")
	transparent := flag.Bool("transparent", true, "Transparent background")
	outputFormat := flag.String("format", "base64", "Output format: base64, dataurl, or png")
	outputFile := flag.String("output", "", "Output file (for png format)")
	hash := flag.String("hash", "", "Direct SHA256 hash (hex) instead of input")

	flag.Parse()

	// Validate input
	if *input == "" && *hash == "" {
		fmt.Fprintln(os.Stderr, "Error: -input or -hash required")
		fmt.Fprintln(os.Stderr, "Usage: identicons-cli -input 'username|email|pubkey' [-size 48|256] [-transparent] [-format base64|dataurl|png] [-output file.png]")
		os.Exit(1)
	}

	// Generate hash
	var hashBytes []byte
	if *hash != "" {
		// Use provided hash (hex string)
		fmt.Sscanf(*hash, "%x", &hashBytes)
		if len(hashBytes) != 32 {
			fmt.Fprintln(os.Stderr, "Error: hash must be 32 bytes (64 hex chars)")
			os.Exit(1)
		}
	} else {
		// Hash the input
		h := sha256.Sum256([]byte(*input))
		hashBytes = h[:]
	}

	// Generate identicon
	var img *image.Paletted
	if *size == 48 {
		identicon := NewOptimizedIdenticonWithSize(hashBytes, 48)
		img = identicon.Generate48x48ForFace(*transparent)
	} else if *size == 256 {
		identicon := NewOptimizedIdenticonWithSize(hashBytes, 256)
		img = identicon.GenerateForExportOptimized(*transparent)
	} else {
		fmt.Fprintln(os.Stderr, "Error: size must be 48 or 256")
		os.Exit(1)
	}

	// Encode to PNG
	var buf bytes.Buffer
	encoder := png.Encoder{
		CompressionLevel: png.BestCompression,
	}

	if err := encoder.Encode(&buf, img); err != nil {
		fmt.Fprintln(os.Stderr, "Error encoding PNG:", err)
		os.Exit(1)
	}

	// Output based on format
	switch *outputFormat {
	case "base64":
		// Output base64 only (for Face header)
		b64 := base64.StdEncoding.EncodeToString(buf.Bytes())
		fmt.Print(b64)

	case "dataurl":
		// Output data URL (for web)
		b64 := base64.StdEncoding.EncodeToString(buf.Bytes())
		fmt.Printf("data:image/png;base64,%s", b64)

	case "png":
		// Output PNG file
		if *outputFile == "" {
			fmt.Fprintln(os.Stderr, "Error: -output required for png format")
			os.Exit(1)
		}
		if err := os.WriteFile(*outputFile, buf.Bytes(), 0644); err != nil {
			fmt.Fprintln(os.Stderr, "Error writing PNG:", err)
			os.Exit(1)
		}
		fmt.Fprintf(os.Stderr, "PNG saved to %s (%d bytes)\n", *outputFile, buf.Len())

	default:
		fmt.Fprintln(os.Stderr, "Error: format must be base64, dataurl, or png")
		os.Exit(1)
	}
}