-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqr.h
276 lines (223 loc) · 7.09 KB
/
qr.h
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
#pragma once
#include <array>
#include <cstring>
#include <gccore.h>
#include <mbedtls/sha512.h>
#include <png.h>
#include <qrencode.h>
#include <string>
#include <vector>
#include "config.h"
// URL for linking.
constexpr char URL[] = "https://accounts.wiilink.ca/link?h=";
std::string HexToString(const std::vector<u8> &hex) {
static constexpr std::array<char, 16> lookup = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f'};
std::string str;
str.reserve(hex.size() * 2);
for (unsigned char i : hex) {
const u8 upper = static_cast<u8>((i >> 4) & 0xf);
const u8 lower = static_cast<u8>(i & 0xf);
str.push_back(lookup[upper]);
str.push_back(lookup[lower]);
}
return str;
}
void encode_qr_code_to_png() {
auto *config = new NWC24Config();
std::string_view str = config->GetPassword();
mbedtls_sha512_context ctx{};
mbedtls_sha512_init(&ctx);
mbedtls_sha512_starts(&ctx, 0);
mbedtls_sha512_update(
&ctx, reinterpret_cast<const unsigned char *>(str.data()), str.length());
std::vector<u8> vec(64);
mbedtls_sha512_finish(&ctx, vec.data());
std::string string(URL);
string.append(HexToString(vec));
QRcode *qr =
QRcode_encodeString(string.data(), 1, QR_ECLEVEL_L, QR_MODE_8, 0);
if (qr == nullptr) {
exit(0);
}
static FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
unsigned char *row, *p, *q;
int x, y, xx, yy, bit;
int realwidth;
const int margin = 4; // Adjust this value to increase/decrease border size
const int size = 1;
realwidth = (qr->width + margin * 2) * size;
row = (unsigned char *)malloc((realwidth + 7) / 8);
if (row == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
fp = fopen("fat:/qr.png", "wb");
if (fp == NULL) {
fprintf(stderr, "Failed to create file: %s\n", "tro");
perror(NULL);
exit(EXIT_FAILURE);
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
fprintf(stderr, "Failed to initialize PNG writer.\n");
exit(EXIT_FAILURE);
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fclose(fp);
fprintf(stderr, "Failed to initialize PNG write.\n");
exit(EXIT_FAILURE);
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
fprintf(stderr, "Failed to write PNG image.\n");
exit(EXIT_FAILURE);
}
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, realwidth, realwidth, 1, PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
/* Top margin (white border) */
memset(row, 0xff, (realwidth + 7) / 8);
for (y = 0; y < margin * size; y++) {
png_write_row(png_ptr, row);
}
/* Data */
p = qr->data;
for (y = 0; y < qr->width; y++) {
bit = 7;
memset(row, 0xff, (realwidth + 7) / 8); // Initialize row to white
q = row;
q += margin * size / 8;
bit = 7 - (margin * size % 8);
for (x = 0; x < qr->width; x++) {
for (xx = 0; xx < size; xx++) {
*q ^= (*p & 1) << bit;
bit--;
if (bit < 0) {
q++;
bit = 7;
}
}
p++;
}
for (yy = 0; yy < size; yy++) {
png_write_row(png_ptr, row);
}
}
/* Bottom margin (white border) */
memset(row, 0xff, (realwidth + 7) / 8);
for (y = 0; y < margin * size; y++) {
png_write_row(png_ptr, row);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
free(row);
}
unsigned char *readPNG(const char *filename, int &width, int &height) {
FILE *fp = fopen(filename, "rb");
if (!fp) {
perror("File opening failed");
return nullptr;
}
png_structp png =
png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png)
return nullptr;
png_infop info = png_create_info_struct(png);
if (!info)
return nullptr;
if (setjmp(png_jmpbuf(png)))
return nullptr;
png_init_io(png, fp);
png_read_info(png, info);
width = png_get_image_width(png, info);
height = png_get_image_height(png, info);
png_byte color_type = png_get_color_type(png, info);
png_byte bit_depth = png_get_bit_depth(png, info);
if (bit_depth == 16)
png_set_strip_16(png);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png);
if (png_get_valid(png, info, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png);
if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_PALETTE)
png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
png_read_update_info(png, info);
unsigned char *image_data =
(unsigned char *)malloc(png_get_rowbytes(png, info) * height);
png_bytep *row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * height);
for (int y = 0; y < height; y++) {
row_pointers[y] = (png_byte *)image_data + y * png_get_rowbytes(png, info);
}
png_read_image(png, row_pointers);
fclose(fp);
if (png && info)
png_destroy_read_struct(&png, &info, nullptr);
if (row_pointers)
free(row_pointers);
return image_data;
}
// Function to write a PNG file
void writePNG(const char *filename, unsigned char *image_data, int width,
int height) {
FILE *fp = fopen(filename, "wb");
if (!fp) {
perror("File opening failed");
return;
}
png_structp png =
png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png)
return;
png_infop info = png_create_info_struct(png);
if (!info)
return;
if (setjmp(png_jmpbuf(png)))
return;
png_init_io(png, fp);
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png, info);
png_bytep row_pointers[height];
for (int y = 0; y < height; y++) {
row_pointers[y] = (png_bytep)(image_data + y * width * 4);
}
png_write_image(png, row_pointers);
png_write_end(png, nullptr);
fclose(fp);
if (png && info)
png_destroy_write_struct(&png, &info);
}
// Function to resize an image using nearest neighbor algorithm
unsigned char *resizeImage(unsigned char *image_data, int width, int height,
int new_width, int new_height) {
unsigned char *resized_data =
(unsigned char *)malloc(new_width * new_height * 4);
for (int y = 0; y < new_height; y++) {
for (int x = 0; x < new_width; x++) {
int src_x = x * width / new_width;
int src_y = y * height / new_height;
for (int c = 0; c < 4; c++) {
resized_data[(y * new_width + x) * 4 + c] =
image_data[(src_y * width + src_x) * 4 + c];
}
}
}
return resized_data;
}