Skip to content

Commit 03b234b

Browse files
committed
Init
0 parents  commit 03b234b

22 files changed

+2166
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
OLD/node_modules

OLD/bintofile.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const prompt = require('prompt-sync')();
4+
5+
function binaryToFile(binaryData, outputPath) {
6+
const byteArray = binaryData.split(' ')
7+
.map(binaryString => parseInt(binaryString, 2));
8+
9+
const fileBuffer = Buffer.from(byteArray);
10+
11+
fs.writeFileSync(outputPath, fileBuffer);
12+
console.log(`Soubor byl úspěšně vytvořen na ${outputPath}`);
13+
}
14+
15+
var inputFile = prompt("Definuj input soubor: ");
16+
const iFilePath = path.join(__dirname, inputFile);
17+
18+
const data = fs.readFileSync(iFilePath, 'utf8');
19+
20+
var outputFile = prompt("Definuj output soubor: ");
21+
const oFilePath = path.join(__dirname, outputFile);
22+
23+
try {
24+
binaryToFile(data, oFilePath);
25+
} catch (error) {
26+
console.error('Chyba:', error.message);
27+
}

OLD/bintofilewext.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const prompt = require('prompt-sync')();
4+
5+
async function binaryToFile(binaryData, outputPath) {
6+
const byteArray = binaryData.split(' ')
7+
.map(binaryString => parseInt(binaryString, 2));
8+
9+
const fileBuffer = Buffer.from(byteArray);
10+
console.log(fileBuffer, "\n")
11+
12+
const { fileTypeFromBuffer } = await import('file-type');
13+
const type = await fileTypeFromBuffer(fileBuffer);
14+
console.log(type);
15+
let extension = 'bin';
16+
if (type) {
17+
extension = type.ext;
18+
}
19+
20+
const outputFilePath = `${outputPath}.${extension}`;
21+
fs.writeFileSync(outputFilePath, fileBuffer);
22+
console.log(`Soubor byl úspěšně vytvořen na ${outputFilePath} (přípona .${extension})`);
23+
}
24+
25+
const inputFile = prompt("Definuj input soubor: ");
26+
const iFilePath = path.join(__dirname, inputFile);
27+
28+
const data = fs.readFileSync(iFilePath, 'utf8');
29+
30+
const outputFile = prompt("Definuj output soubor bez přípony: ");
31+
const oFilePath = path.join(__dirname, outputFile);
32+
33+
binaryToFile(data, oFilePath).catch(error => {
34+
console.error('Chyba:', error.message);
35+
});

OLD/bintoimg.js

Whitespace-only changes.

OLD/btf.js

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
const sharp = require("sharp");
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
const colorMap = {
6+
'0000': { r: 255, g: 255, b: 255 },
7+
'0001': { r: 0, g: 0, b: 0 },
8+
'0010': { r: 255, g: 0, b: 0 },
9+
'0011': { r: 0, g: 255, b: 0 },
10+
'0100': { r: 0, g: 0, b: 255 },
11+
'0101': { r: 255, g: 255, b: 0 },
12+
'0110': { r: 0, g: 255, b: 255 },
13+
'0111': { r: 255, g: 0, b: 255 },
14+
'1000': { r: 128, g: 0, b: 0 },
15+
'1001': { r: 0, g: 128, b: 0 },
16+
'1010': { r: 0, g: 0, b: 128 },
17+
'1011': { r: 255, g: 165, b: 0 },
18+
'1100': { r: 75, g: 0, b: 130 },
19+
'1101': { r: 173, g: 255, b: 47 },
20+
'1110': { r: 255, g: 20, b: 147 },
21+
'1111': { r: 192, g: 192, b: 192 },
22+
'A': { r: 128, g: 128, b: 128 },
23+
'B': { r: 128, g: 128, b: 0 },
24+
'C': { r: 0, g: 128, b: 128 },
25+
'D': { r: 128, g: 0, b: 128 }
26+
};
27+
28+
function get4BitIndex(r, g, b) {
29+
for (let [bitValue, color] of Object.entries(colorMap)) {
30+
if (
31+
Math.abs(color.r - r) <= 1 &&
32+
Math.abs(color.g - g) <= 1 &&
33+
Math.abs(color.b - b) <= 1
34+
) {
35+
return bitValue;
36+
}
37+
}
38+
return null; // Return null for values not found.
39+
}
40+
41+
42+
async function pixelDecode(imagePath) {
43+
try {
44+
const image = sharp(imagePath);
45+
const { width, height } = await image.metadata();
46+
const buffer = await image.raw().toBuffer();
47+
const output = [];
48+
49+
for (let y = 0; y < height; y++) {
50+
for (let x = 0; x < width; x++) {
51+
const pixelIndex = (y * width + x) * 3;
52+
const r = buffer[pixelIndex];
53+
const g = buffer[pixelIndex + 1];
54+
const b = buffer[pixelIndex + 2];
55+
56+
const bitValue = get4BitIndex(r, g, b);
57+
58+
output.push({ x, y, bitValue });
59+
}
60+
}
61+
62+
return { output, width, height };
63+
} catch (error) {
64+
console.error("Cant read", error.message);
65+
}
66+
}
67+
68+
// ----------------------------------------------------------------
69+
70+
const folderPath = "./out";
71+
let outA = [];
72+
73+
fs.readdir(folderPath, (err, files) => {
74+
if (err) {
75+
console.error("What is da folder my nigga", err);
76+
return;
77+
}
78+
79+
files.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
80+
81+
const promises = files.map((file) => {
82+
const filePath = path.join(folderPath, file);
83+
84+
return new Promise((resolve, reject) => {
85+
fs.stat(filePath, (err, stats) => {
86+
if (err) {
87+
console.error("Err for file", file, err);
88+
reject(err);
89+
return;
90+
}
91+
92+
if (stats.isFile()) {
93+
pixelDecode(filePath)
94+
.then(({ output }) => {
95+
resolve(output || []);
96+
})
97+
.catch((error) => {
98+
console.error("Err:", error.message);
99+
reject(error);
100+
});
101+
} else {
102+
resolve([]);
103+
}
104+
});
105+
});
106+
});
107+
108+
function processNibbles(data) {
109+
const nibbleArray = [];
110+
111+
data.forEach((item) => {
112+
const paddedValue = item.bitValue.padStart(
113+
Math.ceil(item.bitValue.length / 4) * 4,
114+
"0"
115+
);
116+
const nibbles = paddedValue.match(/.{1,4}/g);
117+
nibbleArray.push(...nibbles);
118+
});
119+
120+
return nibbleArray;
121+
}
122+
123+
function nibblesToBytes(nibbles) {
124+
const bytes = [];
125+
for (let i = 0; i < nibbles.length; i += 2) {
126+
const high = parseInt(nibbles[i], 2) << 4;
127+
const low = parseInt(nibbles[i + 1], 2);
128+
bytes.push(high | low);
129+
}
130+
return bytes;
131+
}
132+
133+
function convertNibblesToBytes(nibbles) {
134+
const bytes = [];
135+
for (let i = 0; i < nibbles.length; i += 2) {
136+
const highNibble = parseInt(nibbles[i], 2);
137+
const lowNibble = parseInt(nibbles[i + 1], 2);
138+
const byte = (highNibble << 4) | lowNibble;
139+
bytes.push(byte.toString(2).padStart(8, "0"));
140+
}
141+
return bytes;
142+
}
143+
144+
function decodeBinaryArray(binaryArray) {
145+
return binaryArray
146+
.map((binary) => String.fromCharCode(parseInt(binary, 2)))
147+
.join("");
148+
}
149+
150+
function createFileFromNibbles(inputData) {
151+
const data = [];
152+
const name = [];
153+
let readingExt = false;
154+
155+
function isValidNibble(nibble) {
156+
return /^[01]{4}$/.test(nibble);
157+
}
158+
159+
for (let index = 0; index < inputData.length; index++) {
160+
const nibble = inputData[index];
161+
162+
switch (nibble) {
163+
case "000A":
164+
readingExt = !readingExt;
165+
continue;
166+
167+
case "000D":
168+
return {
169+
data: nibblesToBytes(data),
170+
name: convertNibblesToBytes(name),
171+
};
172+
173+
default:
174+
if (isValidNibble(nibble)) {
175+
if (readingExt) {
176+
name.push(nibble);
177+
} else {
178+
data.push(nibble);
179+
}
180+
}
181+
break;
182+
}
183+
}
184+
185+
return {
186+
data: nibblesToBytes(data),
187+
name: convertNibblesToBytes(name),
188+
};
189+
}
190+
191+
Promise.all(promises)
192+
.then((results) => {
193+
outA = results.flat();
194+
195+
const result = processNibbles(outA);
196+
const res = createFileFromNibbles(result);
197+
198+
const dataBytes = (res.data);
199+
const decodedData = Buffer.from(dataBytes);
200+
const decodedName = decodeBinaryArray(res.name);
201+
202+
const folderPath = path.join(__dirname, 'dat_out');
203+
204+
const fileName = decodedName;
205+
206+
const filePath = path.join(folderPath, fileName);
207+
208+
fs.writeFileSync(filePath, decodedData);
209+
210+
})
211+
.catch((error) => {
212+
console.error("Err", error);
213+
});
214+
});

0 commit comments

Comments
 (0)