aboutsummaryrefslogtreecommitdiff
path: root/src/js/image-generator.js
blob: 99f6ed9ffebe6e68fe97105d671f6f0f4047c054 (plain)
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
// Image generator from SHA512SUM to 256px*256px canvas
//
// Kaotisk Hund - 2024
//
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0
//

export function generateImage(hash, what)
{
    var pixels = [];

    var yi = 0;
    var xi = 0;
    for ( var y = 0; y < 256; ++y ){
        pixels[y] = [];
        for ( var x = 0; x < 256; ++x )
        {
            pixels[y][x] = '#000';
        }
    }
    var z = 1;
    for ( var y = 0; y < 256; ++y )
    {
        yi = y - 128;
        for ( var x = 0; x < 256; ++x )
        {
            var pixel;
            xi = x - 128;
            z=Math.abs(Math.floor((yi*yi+xi*xi)/3));
            //z=Math.abs(Math.floor((xi*yi)/1));
            var r1 = hash[z] === undefined ?'0':hash[z];
            var r2 = hash[z+1] === undefined ?'0':hash[z+1];
            var g1 = hash[z+2] === undefined ?'0':hash[z+2];
            var g2 = hash[z+3] === undefined ?'0':hash[z+3];
            var b1 = hash[z+4] === undefined ?'0':hash[z+4];
            var b2 = hash[z+5] === undefined ?'0':hash[z+5];
            var t1 = hash[z+6] === undefined ?'0':hash[z+6];
            var t2 = hash[z+7] === undefined ?'0':hash[z+7];
            if (t2 === undefined)
            {
                pixel = "#000";
            }
            else
            {
                pixel = `#${r1}${r2}${g1}${g2}${b1}${b2}`;
                //pixel = `#${r1}${r2}${g1}${g2}${b1}${b2}${t1}${t2}`;
                //pixel = `#${r1}${r1}${r1}${r1}${r1}${r1}${r1}${r1}`;
                //pixel = `#${r1}${r1}${r1}${r1}${r1}${r1}`;
                //pixel = `#${r1}${g1}${b1}`;
            }
            if ( 0 >= xi && 0 >= yi )
            {
            //    //z=Math.abs(Math.floor((yi*xi)/4));
            //    //if ( z > 120 ) z = z-120;
            //    //pixel = `#${hash[z-113]}${hash[z-114]}${hash[z-115]}${hash[z-116]}${hash[z-117]}${hash[z-118]}${hash[z-119]}${hash[z-120]}`;
            //    pixel = `#${hash[z]}${hash[z+1]}${hash[z+2]}${hash[z+3]}${hash[z+4]}${hash[z+5]}${hash[z+6]}${hash[z+7]}`;
            //    //pixel = '#2a2'
            }
            else if ( 0 <= xi && 0 >= yi )
            {
            //pixel = `#${hash[z]}${hash[z+1]}${hash[z+2]}${hash[z+3]}${hash[z+4]}${hash[z+5]}`; //${hash[z+6]}${hash[z+7]}`;
            //    //z=Math.abs(Math.floor((yi*xi)/128));
            //    pixel = `#${hash[z]}${hash[z+1]}${hash[z+2]}${hash[z+3]}${hash[z+4]}${hash[z+5]}${hash[z+6]}${hash[z+7]}`;
            //    // pixel = '#000';
            //    //pixel = '#22a';
            }
            else if ( 0 >= xi && 0 <= yi )
            {
            //pixel = `#${hash[z]}${hash[z+1]}${hash[z+2]}${hash[z+3]}${hash[z+4]}${hash[z+5]}`; //${hash[z+6]}${hash[z+7]}`;
            //    z=Math.abs(Math.floor((yi*xi)/128));
            //    // pixel = '#000';
            //    pixel = '#a22';
            }
            else if ( 0 <= xi && 0 <= yi )
            {
            //pixel = `#${hash[z]}${hash[z+1]}${hash[z+2]}${hash[z+3]}${hash[z+4]}${hash[z+5]}`; //${hash[z+6]}${hash[z+7]}`;
            //    z=Math.abs(Math.floor((yi*xi)/128));
            //    pixel = '#000';
            }
            pixels[y][x] = pixel;
        }
    }
    createImage( pixels, hash, what );
}

function createImage(pixels, hash, what)
{
    //debugLog(pixels);
    var canvas = document.createElement('canvas');
    //canvas.width = pixels[0].length;
    //canvas.height = pixels.length;
    var output_x_y = 256;

    canvas.width = output_x_y;
    canvas.height = output_x_y;
    var context = canvas.getContext('2d');
    var zoom_factor = 6;
    var zoom_size = Math.floor(output_x_y/zoom_factor);
    var radius = Math.floor(output_x_y/zoom_size);
    // debugLog(`Radius: ${radius}`);
    for ( var y = 0; y < zoom_size; ++y )
    {
        for ( var x = 0; x < zoom_size; ++x )
        {
            var fy = (canvas.height-zoom_size)/2;
            var fx = (canvas.width-zoom_size)/2;
            var ax = Math.floor(x+fx+1);
            var ay = Math.floor(y+fy+1);
            //debugLog(`${ax} + ${ay}`);
            var pixel_to_be_enhanced = pixels[ay][ax];
            var c = 0;
            var r = 0;
            for (var i = 0; i < radius; ++i)
            {
                for (var k = 0; k < radius; ++k)
                {
                    var subpixel = pixel_to_be_enhanced;
                    if ( i >= radius - Math.floor(radius/3) )
                    {
                        if ( k >= radius - Math.floor(radius/3) )
                        {
                            //     debugLog(i);
                            c = (x*radius)+k;
                            r = (y*radius)+i;
                            context.fillStyle = subpixel;
                            context.fillRect(c, r, radius/3, radius/3);
                            context.stroke();
                        }
                    }
                    //debugLog(`${x} -> ${c}, ${y} -> ${r}`);
                }
            }
        }
    }
    if ( what !== 'new' )
    {
        document.querySelector('.generated').src = canvas.toDataURL('image/png');
    }
    else
    {
        document.querySelector(`#i-${hash}`).src = canvas.toDataURL('image/png');
        document.querySelector(`#i-${hash}`).width = 128;
    }
}

// @license-end