-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshuffle.html
218 lines (192 loc) · 8.46 KB
/
shuffle.html
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
<!DOCTYPE html>
<head>
<h1>Shuffle images</h1>
</head>
<body>
<input type="file" id="upload" accept="image/*">
<input type="password" id="password" placeholder="Enter Password">
<button id="scramble">Scramble Image</button>
<button id="recover">Recover Image</button>
<h1>Canvas:</h1>
<canvas id="canvas"></canvas>
<h1>Canvas 2:</h1>
<canvas id="canvas2"></canvas>
<h1>Canvas 3:</h1>
<canvas id="canvas3"></canvas>
<h1>Canvas 4:</h1>
<canvas id="canvas4"></canvas>
<h1>Upload image to be unscrambled:</h1>
<input type="file" id="scrambledUpload" accept="image/*">
<input type="password" id="scramblePassword" placeholder="Enter Descramble Password">
<canvas id="canvas5"></canvas>
<!-- <button id="unscramble">Unscramble Image</button> -->
</body>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const canvas2 = document.getElementById('canvas2');
const ctx2 = canvas2.getContext('2d');
const canvas3 = document.getElementById('canvas3');
const ctx3 = canvas3.getContext('2d');
const canvas4 = document.getElementById('canvas4');
const ctx4 = canvas4.getContext('2d');
const canvas5 = document.getElementById('canvas5');
const ctx5 = canvas5.getContext('2d');
let data, src, img;
let indexMap = [];
let originalImage = null;
document.getElementById('upload').addEventListener('change', handleFileSelect);
document.getElementById('scramble').addEventListener('click', scrambleImage);
document.getElementById('scrambledUpload').addEventListener('change', unscrambleImg);
function unscrambleImg(event) {
// read uploaded file
const file = event.target.files[0];
const reader = new FileReader();
console.log("Uploading scrambled image")
reader.onload = function(e) { // once loaded, execute function
console.log('Scrambled Image loaded')
img = new Image();
img.onload = function() {
// get password, find indicies, and reconstruct the image from the indicie and scrambled image
canvas5.width = img.width;
canvas5.height = img.height;
ctx5.drawImage(img, 0, 0);
scrambledImage = ctx5.getImageData(0, 0, img.width, img.height);
scrambledData = scrambledImage.data;
const scramblePassword = document.getElementById('scramblePassword').value;
console.log("scramblePassword: ",scramblePassword);
indicies = pass2idxs(scramblePassword, img.height);
reconstructImg(indicies, scrambledData, img.width, img.height);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
};
function scrambleImage(event) {
const password = document.getElementById('password').value;
console.log("Password: ",password);
indicies = pass2idxs(password, img.height);
// indicies = getRandomRowIdx(img.height);
scrambledData = displayRandomizedRows(indicies, data, img.width, img.height);
reconstructImg(indicies, scrambledData, img.width, img.height);
};
function handleFileSelect(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
img = new Image();
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
originalImage = ctx.getImageData(0, 0, canvas.width, canvas.height);
data = originalImage.data;
// pull and display row from image
const rowData = getImgRow(data, canvas.width, 1);
console.log(rowData);
displayRow(rowData);
const password = document.getElementById('password').value;
console.log("Password: ",password);
indicies = getRandomRowIdx(img.height);
scrambledData = displayRandomizedRows(indicies, data, img.width, img.height);
reconstructImg(indicies, scrambledData, img.width, img.height);
};
img.src = e.target.result;
// read the canvas into an array using openCV
// let src = cv.imread(canvas)
};
reader.readAsDataURL(file);
}
function getImgRow(data, width, rowIndex) {
const rowData = [];
const start = rowIndex * width * 4; // 4 for RGBA
for (let i = 0; i < width; i++) {
const pixelIndex = start + i * 4;
rowData.push({
r: data[pixelIndex], // Red channel
g: data[pixelIndex + 1], // Green channel
b: data[pixelIndex + 2], // Blue channel
a: data[pixelIndex + 3] // Alpha channel
});
}
return rowData;
}
function displayRow(rowData) {
canvas2.width = rowData.length; // Width is the number of pixels in the row
canvas2.height = 1; // Height is 1 since we only want to display one row
const imageData = ctx2.createImageData(canvas2.width, 1);
// Fill the ImageData with the row's pixel data
for (let i = 0; i < rowData.length; i++) {
const pixelIndex = i * 4;
imageData.data[pixelIndex] = rowData[i].r; // Red
imageData.data[pixelIndex + 1] = rowData[i].g; // Green
imageData.data[pixelIndex + 2] = rowData[i].b; // Blue
imageData.data[pixelIndex + 3] = rowData[i].a; // Alpha
}
ctx2.putImageData(imageData, 0, 0);
}
function getRandomRowIdx(height){
const indicies = Array.from({ length: height}, (_,i) => i);
for (let i = indicies.length - 1; i>0; i--){
const j = Math.floor(Math.random() * (i+1));
[indicies[i], indicies[j]] = [indicies[j], indicies[i]];
}
console.log("Found indicies: ", indicies);
return indicies;
}
function displayRandomizedRows(indicies, data, width, height) {
canvas3.height = height;
canvas3.width = width
indicies.forEach((rowIndex,idx) => {
const rowData = getImgRow(data, width, rowIndex);
const rowHeight = 1;
const imageData = ctx3.createImageData(width, rowHeight);
for (let i=0; i < rowData.length; i++) {
const pixelIndex = i*4;
imageData.data[pixelIndex]=rowData[i].r;
imageData.data[pixelIndex+1]=rowData[i].g;
imageData.data[pixelIndex+2]=rowData[i].b;
imageData.data[pixelIndex+3]=rowData[i].a;
}
ctx3.putImageData(imageData,0,idx);
});
scrambledImg = ctx3.getImageData(0, 0, canvas.width, canvas.height);
scrambledData = scrambledImg.data;
console.log('Displayed Randomized Rows')
return scrambledData
}
function reconstructImg(indicies, scrambledData, width, height) {
canvas4.height = height;
canvas4.width = width
indicies.forEach((rowIndex,idx) =>{
const rowData = getImgRow(scrambledData, width, idx);
const rowHeight = 1;
const imageData = ctx4.createImageData(width, rowHeight);
for (let i=0; i < rowData.length; i++) {
const pixelIndex = i*4;
imageData.data[pixelIndex]=rowData[i].r;
imageData.data[pixelIndex+1]=rowData[i].g;
imageData.data[pixelIndex+2]=rowData[i].b;
imageData.data[pixelIndex+3]=rowData[i].a;
}
ctx4.putImageData(imageData,0,rowIndex);
// console.log('Drew image on context 4')
});
console.log('Reconstructed Image')
}
function pass2idxs(password, length) {
let asciiPswd = Array.from(password).map(char => char.charCodeAt(0));
seed = asciiPswd.join('');
const indicies = Array.from({ length: length}, (_,i) => i);
for (let i=0; i<length; i++) {
rand = Math.abs(((2.892 * seed * i ** 1.3)* Math.sin(i) +2)) % 13;
// console.log("Rand: ", rand);
rand_scaled = rand / 13;
// console.log(rand_scaled);
const j = Math.floor(rand_scaled * (i+1));
[indicies[i],indicies[j]] = [indicies[j],indicies[i]];
}
console.log("Indicies: ", indicies);
return indicies
}
</script>