-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
147 lines (118 loc) · 4.6 KB
/
main.js
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
const { app, ipcMain, BrowserWindow, Menu } = require('electron');
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const Jimp = require('jimp');
let mainWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 650,
height: 500,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
Menu.setApplicationMenu(null);
mainWindow.loadFile(path.join(__dirname, 'index.html'));
ipcMain.on('process-image', async (event, filePath) => {
try {
const image = await Jimp.read(filePath);
image.resize(500, 500);
const width = image.bitmap.width;
const height = image.bitmap.height;
const ymlObject = {};
const colorSet = new Set();
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const color = image.getPixelColor(x, y);
const hexColor = Jimp.intToRGBA(color);
const hex = `#${hexColor.r.toString(16).padStart(2, '0')}${hexColor.g.toString(16).padStart(2, '0')}${hexColor.b.toString(16).padStart(2, '0')}`;
ymlObject[`${x}x${y}`] = hex;
colorSet.add(hex);
}
}
const ymlData = yaml.dump(ymlObject);
const ymlObjectPath = path.join(__dirname, 'YML_object.yml');
fs.writeFileSync(ymlObjectPath, ymlData);
const uniqueColors = Array.from(colorSet);
if (uniqueColors.length < 26) {
event.sender.send('alert', 'Image not applicable, try a new one.');
} else {
const colorMapping = uniqueColors.slice(0, 26).reduce((acc, color, index) => {
acc[`${color}`] = String.fromCharCode(97 + index);
return acc;
}, {});
const colorYmlData = yaml.dump(colorMapping);
const ymlAlphaPath = path.join(__dirname, 'YML_alpha.yml');
fs.writeFileSync(ymlAlphaPath, colorYmlData);
}
event.sender.send('process-image-reply', 'Image processed and YAML files saved.');
} catch (error) {
console.error('Error processing image:', error);
event.sender.send('alert', 'Failed to process image.');
}
});
ipcMain.on('encode-message', (event, message) => {
try {
const ymlAlphaPath = path.join(__dirname, 'YML_alpha.yml');
const ymlObjectPath = path.join(__dirname, 'YML_object.yml');
const ymlAlphaContent = fs.readFileSync(ymlAlphaPath, 'utf8');
const ymlObjectContent = fs.readFileSync(ymlObjectPath, 'utf8');
const ymlAlpha = yaml.load(ymlAlphaContent);
const ymlObject = yaml.load(ymlObjectContent);
const messageArray = message.toLowerCase().split('');
const encodedMessage = messageArray.map(char => {
if (char === ' ') {
return ':';
} else {
const hex = Object.keys(ymlAlpha).find(key => ymlAlpha[key] === char);
if (hex) {
const coordinates = Object.keys(ymlObject).find(key => ymlObject[key] === hex);
if (coordinates) {
return coordinates;
}
}
return '';
}
});
const encodedCoordinates = encodedMessage.join(':');
event.sender.send('encoded-message', encodedCoordinates);
} catch (error) {
console.error('Error encoding message:', error);
event.sender.send('alert', 'Failed to encode message.');
}
});
ipcMain.on('decode-message', (event, coordinates) => {
try {
const ymlAlphaPath = path.join(__dirname, 'YML_alpha.yml');
const ymlObjectPath = path.join(__dirname, 'YML_object.yml');
const ymlAlphaContent = fs.readFileSync(ymlAlphaPath, 'utf8');
const ymlObjectContent = fs.readFileSync(ymlObjectPath, 'utf8');
const ymlAlpha = yaml.load(ymlAlphaContent);
const ymlObject = yaml.load(ymlObjectContent);
const coordinatesArray = coordinates.split(':');
const decodedMessage = [];
coordinatesArray.forEach(coord => {
if (coord === '') {
decodedMessage.push(' ');
} else {
const hex = ymlObject[coord];
if (hex) {
const letter = ymlAlpha[hex];
if (letter) {
decodedMessage.push(letter);
}
}
}
});
event.sender.send('decoded-message', decodedMessage.join(''));
} catch (error) {
console.error('Error decoding coordinates:', error);
event.sender.send('alert', 'Failed to decode coordinates.');
}
});
});
app.on('window-all-closed', () => {
app.quit();
});