2015-11-09 04:51:46 +01:00
|
|
|
/* qr_obj_code.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-06-16 05:07:16 +02:00
|
|
|
#ifdef QRCODE
|
|
|
|
|
2015-11-09 04:51:46 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <qrencode.h>
|
|
|
|
#include <stdbool.h>
|
2016-10-18 09:38:28 +02:00
|
|
|
#include <string.h>
|
2015-11-09 04:51:46 +01:00
|
|
|
|
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.h"
|
|
|
|
#include "qr_code.h"
|
|
|
|
|
2016-10-05 11:55:45 +02:00
|
|
|
#ifdef QRPNG
|
|
|
|
#include <png.h>
|
|
|
|
#define INCHES_PER_METER (100.0/2.54)
|
2016-10-06 11:50:21 +02:00
|
|
|
#define DPI 72
|
|
|
|
#define SQUARE_SIZE 5
|
2016-10-05 11:55:45 +02:00
|
|
|
#endif /* QRPNG */
|
|
|
|
|
2015-11-09 04:51:46 +01:00
|
|
|
#define BORDER_LEN 1
|
|
|
|
#define CHAR_1 "\342\226\210"
|
|
|
|
#define CHAR_2 "\342\226\204"
|
|
|
|
#define CHAR_3 "\342\226\200"
|
|
|
|
|
2016-10-05 11:55:45 +02:00
|
|
|
/* Converts a tox ID string into a QRcode and prints it into the given filename.
|
2015-11-09 04:51:46 +01:00
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
* Returns -1 on failure.
|
|
|
|
*/
|
2016-10-05 11:55:45 +02:00
|
|
|
int ID_to_QRcode_txt(const char *tox_id, const char *outfile)
|
2015-11-09 04:51:46 +01:00
|
|
|
{
|
2016-10-05 11:55:45 +02:00
|
|
|
FILE *fp = fopen(outfile, "wb");
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (fp == NULL) {
|
2015-11-09 04:51:46 +01:00
|
|
|
return -1;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-11-09 04:51:46 +01:00
|
|
|
|
|
|
|
QRcode *qr_obj = QRcode_encodeString(tox_id, 0, QR_ECLEVEL_L, QR_MODE_8, 0);
|
|
|
|
|
2016-10-06 11:54:27 +02:00
|
|
|
if (qr_obj == NULL) {
|
|
|
|
fclose(fp);
|
2015-11-09 04:51:46 +01:00
|
|
|
return -1;
|
2016-10-06 11:54:27 +02:00
|
|
|
}
|
2015-11-09 04:51:46 +01:00
|
|
|
|
|
|
|
size_t width = qr_obj->width;
|
|
|
|
size_t i, j;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
for (i = 0; i < width + BORDER_LEN * 2; ++i) {
|
2015-11-09 08:28:41 +01:00
|
|
|
fprintf(fp, "%s", CHAR_1);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-11-09 04:51:46 +01:00
|
|
|
|
2015-11-09 08:28:41 +01:00
|
|
|
fprintf(fp, "\n");
|
2015-11-09 04:51:46 +01:00
|
|
|
|
2015-11-09 08:28:41 +01:00
|
|
|
for (i = 0; i < width; i += 2) {
|
2018-07-18 17:33:16 +02:00
|
|
|
for (j = 0; j < BORDER_LEN; ++j) {
|
2015-11-09 04:51:46 +01:00
|
|
|
fprintf(fp, "%s", CHAR_1);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-11-09 04:51:46 +01:00
|
|
|
|
2015-11-09 08:28:41 +01:00
|
|
|
const unsigned char *row_1 = qr_obj->data + width * i;
|
|
|
|
const unsigned char *row_2 = row_1 + width;
|
|
|
|
|
2015-11-09 04:51:46 +01:00
|
|
|
for (j = 0; j < width; ++j) {
|
|
|
|
bool x = row_1[j] & 1;
|
|
|
|
bool y = (i + 1) < width ? (row_2[j] & 1) : false;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (x && y) {
|
2015-11-09 04:51:46 +01:00
|
|
|
fprintf(fp, " ");
|
2018-07-18 17:33:16 +02:00
|
|
|
} else if (x) {
|
2015-11-09 04:51:46 +01:00
|
|
|
fprintf(fp, "%s", CHAR_2);
|
2018-07-18 17:33:16 +02:00
|
|
|
} else if (y) {
|
2015-11-09 04:51:46 +01:00
|
|
|
fprintf(fp, "%s", CHAR_3);
|
2018-07-18 17:33:16 +02:00
|
|
|
} else {
|
2015-11-09 04:51:46 +01:00
|
|
|
fprintf(fp, "%s", CHAR_1);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-11-09 04:51:46 +01:00
|
|
|
}
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
for (j = 0; j < BORDER_LEN; ++j) {
|
2015-11-09 04:51:46 +01:00
|
|
|
fprintf(fp, "%s", CHAR_1);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-11-09 04:51:46 +01:00
|
|
|
|
2015-11-09 08:28:41 +01:00
|
|
|
fprintf(fp, "\n");
|
2015-11-09 04:51:46 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 11:55:45 +02:00
|
|
|
fclose(fp);
|
|
|
|
QRcode_free(qr_obj);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef QRPNG
|
|
|
|
/* Converts a tox ID string into a QRcode and prints it into the given filename as png.
|
|
|
|
*
|
|
|
|
* Returns 0 on success.
|
|
|
|
* Returns -1 on failure.
|
|
|
|
*/
|
|
|
|
int ID_to_QRcode_png(const char *tox_id, const char *outfile)
|
|
|
|
{
|
|
|
|
static FILE *fp;
|
2016-10-05 21:17:56 +02:00
|
|
|
unsigned char *p;
|
2016-10-05 11:55:45 +02:00
|
|
|
unsigned char black[4] = {0, 0, 0, 255};
|
|
|
|
size_t x, y, xx, yy, real_width;
|
|
|
|
png_structp png_ptr;
|
|
|
|
png_infop info_ptr;
|
|
|
|
|
|
|
|
fp = fopen(outfile, "wb");
|
|
|
|
|
2016-10-05 21:17:56 +02:00
|
|
|
if (fp == NULL) {
|
2016-10-05 11:55:45 +02:00
|
|
|
return -1;
|
2016-10-05 21:17:56 +02:00
|
|
|
}
|
2016-10-05 11:55:45 +02:00
|
|
|
|
|
|
|
QRcode *qr_obj = QRcode_encodeString(tox_id, 0, QR_ECLEVEL_L, QR_MODE_8, 0);
|
|
|
|
|
2016-10-05 21:17:56 +02:00
|
|
|
if (qr_obj == NULL) {
|
|
|
|
fclose(fp);
|
2016-10-05 11:55:45 +02:00
|
|
|
return -1;
|
2016-10-05 21:17:56 +02:00
|
|
|
}
|
2016-10-05 11:55:45 +02:00
|
|
|
|
2016-10-06 11:50:21 +02:00
|
|
|
real_width = (qr_obj->width + BORDER_LEN * 2) * SQUARE_SIZE;
|
2016-10-05 21:17:56 +02:00
|
|
|
size_t row_size = real_width * 4;
|
|
|
|
unsigned char row[row_size];
|
2016-10-05 11:55:45 +02:00
|
|
|
|
|
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
|
|
2016-10-05 21:17:56 +02:00
|
|
|
if (png_ptr == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
QRcode_free(qr_obj);
|
2016-10-05 11:55:45 +02:00
|
|
|
return -1;
|
2016-10-05 21:17:56 +02:00
|
|
|
}
|
2016-10-05 11:55:45 +02:00
|
|
|
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
2016-10-05 21:17:56 +02:00
|
|
|
if (info_ptr == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
QRcode_free(qr_obj);
|
2016-10-05 11:55:45 +02:00
|
|
|
return -1;
|
2016-10-05 21:17:56 +02:00
|
|
|
}
|
2016-10-05 11:55:45 +02:00
|
|
|
|
|
|
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
2016-10-05 21:17:56 +02:00
|
|
|
fclose(fp);
|
|
|
|
QRcode_free(qr_obj);
|
2016-10-05 11:55:45 +02:00
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
png_init_io(png_ptr, fp);
|
|
|
|
png_set_IHDR(png_ptr, info_ptr, real_width, real_width, 8,
|
|
|
|
PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
2016-10-06 11:50:21 +02:00
|
|
|
png_set_pHYs(png_ptr, info_ptr, DPI * INCHES_PER_METER,
|
|
|
|
DPI * INCHES_PER_METER, PNG_RESOLUTION_METER);
|
2016-10-05 11:55:45 +02:00
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
/* top margin */
|
2016-10-05 21:17:56 +02:00
|
|
|
memset(row, 0xff, row_size);
|
2016-10-05 11:55:45 +02:00
|
|
|
|
2016-10-06 11:50:21 +02:00
|
|
|
for (y = 0; y < BORDER_LEN * SQUARE_SIZE; y++) {
|
2016-10-05 11:55:45 +02:00
|
|
|
png_write_row(png_ptr, row);
|
2016-10-05 21:17:56 +02:00
|
|
|
}
|
2016-10-05 11:55:45 +02:00
|
|
|
|
|
|
|
/* data */
|
|
|
|
p = qr_obj->data;
|
|
|
|
|
|
|
|
for (y = 0; y < qr_obj->width; y++) {
|
2016-10-05 21:17:56 +02:00
|
|
|
memset(row, 0xff, row_size);
|
2016-10-05 11:55:45 +02:00
|
|
|
|
|
|
|
for (x = 0; x < qr_obj->width; x++) {
|
2016-10-06 11:50:21 +02:00
|
|
|
for (xx = 0; xx < SQUARE_SIZE; xx++) {
|
2016-10-05 21:17:56 +02:00
|
|
|
if (*p & 1) {
|
2016-10-06 11:50:21 +02:00
|
|
|
memcpy(&row[((BORDER_LEN + x) * SQUARE_SIZE + xx) * 4], black, 4);
|
2016-10-05 21:17:56 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-05 11:55:45 +02:00
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2016-10-06 11:50:21 +02:00
|
|
|
for (yy = 0; yy < SQUARE_SIZE; yy++) {
|
2016-10-05 11:55:45 +02:00
|
|
|
png_write_row(png_ptr, row);
|
2016-10-05 21:17:56 +02:00
|
|
|
}
|
2016-10-05 11:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* bottom margin */
|
2016-10-05 21:17:56 +02:00
|
|
|
memset(row, 0xff, row_size);
|
2016-10-05 11:55:45 +02:00
|
|
|
|
2016-10-06 11:50:21 +02:00
|
|
|
for (y = 0; y < BORDER_LEN * SQUARE_SIZE; y++) {
|
2016-10-05 11:55:45 +02:00
|
|
|
png_write_row(png_ptr, row);
|
2016-10-05 21:17:56 +02:00
|
|
|
}
|
2016-10-05 11:55:45 +02:00
|
|
|
|
|
|
|
png_write_end(png_ptr, info_ptr);
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
|
|
|
|
fclose(fp);
|
2015-11-09 04:51:46 +01:00
|
|
|
QRcode_free(qr_obj);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-05 11:55:45 +02:00
|
|
|
#endif /* QRPNG */
|
2018-06-16 05:07:16 +02:00
|
|
|
|
|
|
|
#endif /* QRCODE */
|