mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-25 09:03:02 +01:00
Merge branch 'master' into new_groupchats
This commit is contained in:
commit
9dfa455561
@ -34,6 +34,12 @@ ifneq ($(DESK_NOTIFY), disabled)
|
||||
-include $(CHECKS_DIR)/desktop_notifications.mk
|
||||
endif
|
||||
|
||||
# Check if we want build QR exported as PNG support
|
||||
QR_PNG = $(shell if [ -z "$(DISABLE_QRPNG)" ] || [ "$(DISABLE_QRPNG)" = "0" ] ; then echo enabled ; else echo disabled ; fi)
|
||||
ifneq ($(QR_PNG), disabled)
|
||||
-include $(CHECKS_DIR)/qr_png.mk
|
||||
endif
|
||||
|
||||
# Check if we can build Toxic
|
||||
CHECK_LIBS = $(shell $(PKG_CONFIG) --exists $(LIBS) || echo -n "error")
|
||||
ifneq ($(CHECK_LIBS), error)
|
||||
|
15
cfg/checks/qr_png.mk
Normal file
15
cfg/checks/qr_png.mk
Normal file
@ -0,0 +1,15 @@
|
||||
# Variables for QR exported as PNG support
|
||||
PNG_LIBS = libpng
|
||||
PNG_CFLAGS = -DQRPNG
|
||||
|
||||
# Check if we can build QR exported as PNG support
|
||||
CHECK_PNG_LIBS = $(shell pkg-config --exists $(PNG_LIBS) || echo -n "error")
|
||||
ifneq ($(CHECK_PNG_LIBS), error)
|
||||
LIBS += $(PNG_LIBS)
|
||||
CFLAGS += $(PNG_CFLAGS)
|
||||
else ifneq ($(MAKECMDGOALS), clean)
|
||||
MISSING_PNG_LIBS = $(shell for lib in $(PNG_LIBS) ; do if ! $(PKG_CONFIG) --exists $$lib ; then echo $$lib ; fi ; done)
|
||||
$(warning WARNING -- Toxic will be compiled without QR exported as PNG support)
|
||||
$(warning WARNING -- You need these libraries for QR exported as PNG support)
|
||||
$(warning WARNING -- $(MISSING_PNG_LIBS))
|
||||
endif
|
@ -8,13 +8,13 @@ else
|
||||
endif
|
||||
|
||||
# Check if we can build video support
|
||||
CHECK_VIDEO_LIBS = $(shell pkg-config --exists $(VIDEO_LIBS) || echo -n "error")
|
||||
CHECK_VIDEO_LIBS = $(shell $(PKG_CONFIG) --exists $(VIDEO_LIBS) || echo -n "error")
|
||||
ifneq ($(CHECK_VIDEO_LIBS), error)
|
||||
LIBS += $(VIDEO_LIBS)
|
||||
CFLAGS += $(VIDEO_CFLAGS)
|
||||
OBJ += $(VIDEO_OBJ)
|
||||
else ifneq ($(MAKECMDGOALS), clean)
|
||||
MISSING_VIDEO_LIBS = $(shell for lib in $(VIDEO_LIBS) ; do if ! pkg-config --exists $$lib ; then echo $$lib ; fi ; done)
|
||||
MISSING_VIDEO_LIBS = $(shell for lib in $(VIDEO_LIBS) ; do if ! $(PKG_CONFIG) --exists $$lib ; then echo $$lib ; fi ; done)
|
||||
$(warning WARNING -- Toxic will be compiled without video support)
|
||||
$(warning WARNING -- You will need these libraries for video support)
|
||||
$(warning WARNING -- $(MISSING_VIDEO_LIBS))
|
||||
|
@ -14,6 +14,7 @@ help:
|
||||
@echo " DISABLE_AV: Set to \"1\" to force building without audio call support"
|
||||
@echo " DISABLE_SOUND_NOTIFY: Set to \"1\" to force building without sound notification support"
|
||||
@echo " DISABLE_DESKTOP_NOTIFY: Set to \"1\" to force building without desktop notifications support"
|
||||
@echo " DISABLE_QRPNG: Set to \"1\" to force building without QR exported as PNG support"
|
||||
@echo " USER_CFLAGS: Add custom flags to default CFLAGS"
|
||||
@echo " USER_LDFLAGS: Add custom flags to default LDFLAGS"
|
||||
@echo " PREFIX: Specify a prefix directory for binaries, data files,... (default is \"$(abspath $(PREFIX))\")"
|
||||
|
@ -145,8 +145,8 @@ struct FileTransfer *get_file_transfer_struct_index(uint32_t friendnum, uint32_t
|
||||
|
||||
for (i = 0; i < MAX_FILES; ++i) {
|
||||
struct FileTransfer *ft = direction == FILE_TRANSFER_SEND ?
|
||||
&Friends.list[friendnum].file_sender[i] :
|
||||
&Friends.list[friendnum].file_receiver[i];
|
||||
&Friends.list[friendnum].file_sender[i] :
|
||||
&Friends.list[friendnum].file_receiver[i];
|
||||
|
||||
if (ft->state != FILE_TRANSFER_INACTIVE && ft->index == index)
|
||||
return ft;
|
||||
|
@ -131,7 +131,7 @@ void cmd_add_helper(ToxWindow *self, Tox *m, const char *id_bin, const char *msg
|
||||
|
||||
case TOX_ERR_FRIEND_ADD_NULL:
|
||||
|
||||
/* fallthrough */
|
||||
/* fallthrough */
|
||||
default:
|
||||
errmsg = "Faile to add friend: Unknown error.";
|
||||
break;
|
||||
@ -542,25 +542,42 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
|
||||
char dir[data_file_len + 1];
|
||||
size_t dir_len = get_base_dir(DATA_FILE, data_file_len, dir);
|
||||
|
||||
char qr_path[dir_len + nick_len + strlen(QRCODE_FILENAME_EXT) + 1];
|
||||
snprintf(qr_path, sizeof(qr_path), "%s%s%s", dir, nick, QRCODE_FILENAME_EXT);
|
||||
#ifdef QRPNG
|
||||
|
||||
FILE *output = fopen(qr_path, "wb");
|
||||
if (argc == 0) {
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Required 'txt' or 'png'");
|
||||
return;
|
||||
} else if (!strcmp(argv[1], "txt")) {
|
||||
|
||||
if (output == NULL) {
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code.");
|
||||
#endif /* QRPNG */
|
||||
char qr_path[dir_len + nick_len + strlen(QRCODE_FILENAME_EXT) + 1];
|
||||
snprintf(qr_path, sizeof(qr_path), "%s%s%s", dir, nick, QRCODE_FILENAME_EXT);
|
||||
|
||||
if (ID_to_QRcode_txt(id_string, qr_path) == -1) {
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code.");
|
||||
return;
|
||||
}
|
||||
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "QR code has been printed to the file '%s'", qr_path);
|
||||
|
||||
#ifdef QRPNG
|
||||
} else if (!strcmp(argv[1], "png")) {
|
||||
char qr_path[dir_len + nick_len + strlen(QRCODE_FILENAME_EXT_PNG) + 1];
|
||||
snprintf(qr_path, sizeof(qr_path), "%s%s%s", dir, nick, QRCODE_FILENAME_EXT_PNG);
|
||||
|
||||
if (ID_to_QRcode_png(id_string, qr_path) == -1) {
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code.");
|
||||
return;
|
||||
}
|
||||
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "QR code has been printed to the file '%s'", qr_path);
|
||||
|
||||
} else {
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Unknown option '%s' -- Required 'txt' or 'png'", argv[1]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ID_to_QRcode(id_string, output) == -1) {
|
||||
fclose(output);
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code.");
|
||||
return;
|
||||
}
|
||||
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "QR code has been printed to the file '%s'", qr_path);
|
||||
|
||||
fclose(output);
|
||||
#endif /* QRPNG */
|
||||
}
|
||||
|
||||
void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
||||
|
@ -159,7 +159,11 @@ static void help_draw_global(ToxWindow *self)
|
||||
wprintw(win, " /nospam <value> : Change part of your Tox ID to stop spam\n");
|
||||
wprintw(win, " /log <on> or <off> : Enable/disable logging\n");
|
||||
wprintw(win, " /myid : Print your Tox ID\n");
|
||||
#ifdef QRPNG
|
||||
wprintw(win, " /myqr <txt> or <png> : Print your Tox ID's QR code to a file.\n");
|
||||
#else
|
||||
wprintw(win, " /myqr : Print your Tox ID's QR code to a file.\n");
|
||||
#endif /* QRPNG */
|
||||
wprintw(win, " /clear : Clear window history\n");
|
||||
wprintw(win, " /close : Close the current chat window\n");
|
||||
wprintw(win, " /quit or /exit : Exit Toxic\n");
|
||||
|
@ -159,14 +159,14 @@ void line_info_add(ToxWindow *self, const char *timestr, const char *name1, cons
|
||||
switch (type) {
|
||||
case IN_ACTION:
|
||||
|
||||
/* fallthrough */
|
||||
/* fallthrough */
|
||||
case OUT_ACTION:
|
||||
len += strlen(user_settings->line_normal) + 2;
|
||||
break;
|
||||
|
||||
case IN_MSG:
|
||||
|
||||
/* fallthrough */
|
||||
/* fallthrough */
|
||||
case OUT_MSG:
|
||||
len += strlen(user_settings->line_normal) + 3;
|
||||
break;
|
||||
@ -312,10 +312,10 @@ void line_info_print(ToxWindow *self)
|
||||
switch (type) {
|
||||
case OUT_MSG:
|
||||
|
||||
/* fallthrough */
|
||||
/* fallthrough */
|
||||
case OUT_MSG_READ:
|
||||
|
||||
/* fallthrough */
|
||||
/* fallthrough */
|
||||
case IN_MSG:
|
||||
case IN_PRVT_MSG:
|
||||
case OUT_PRVT_MSG:
|
||||
@ -375,10 +375,10 @@ void line_info_print(ToxWindow *self)
|
||||
|
||||
case OUT_ACTION_READ:
|
||||
|
||||
/* fallthrough */
|
||||
/* fallthrough */
|
||||
case OUT_ACTION:
|
||||
|
||||
/* fallthrough */
|
||||
/* fallthrough */
|
||||
case IN_ACTION:
|
||||
wattron(win, COLOR_PAIR(BLUE));
|
||||
wprintw(win, "%s ", line->timestr);
|
||||
|
@ -26,9 +26,13 @@
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#if defined(__FreeBSD__)
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "toxic.h"
|
||||
#include "windows.h"
|
||||
|
@ -37,10 +37,10 @@ void bgrtoyuv420(uint8_t *plane_y, uint8_t *plane_u, uint8_t *plane_v, uint8_t *
|
||||
|
||||
#ifdef __OBJC__
|
||||
@interface OSXVideo :
|
||||
NSObject <AVCaptureVideoDataOutputSampleBufferDelegate>
|
||||
NSObject <AVCaptureVideoDataOutputSampleBufferDelegate>
|
||||
- (instancetype)initWithDeviceNames:
|
||||
(char **)device_names AmtDevices:
|
||||
(int *)size;
|
||||
(char **)device_names AmtDevices:
|
||||
(int *)size;
|
||||
@end
|
||||
#endif /* __OBJC__ */
|
||||
|
||||
|
@ -127,19 +127,20 @@ void bgrxtoyuv420(uint8_t *plane_y, uint8_t *plane_u, uint8_t *plane_v, uint8_t
|
||||
}
|
||||
|
||||
- (instancetype)initWithDeviceNames:
|
||||
(char **)device_names AmtDevices:
|
||||
(int *)size {
|
||||
(char **)device_names AmtDevices:
|
||||
(int *)size
|
||||
{
|
||||
_session = [[AVCaptureSession alloc] init];
|
||||
|
||||
NSArray *devices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];
|
||||
NSArray *devices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < [devices count]; ++i) {
|
||||
AVCaptureDevice *device = [devices objectAtIndex: i];
|
||||
AVCaptureDevice *device = [devices objectAtIndex: i];
|
||||
char *video_input_name;
|
||||
NSString *localizedName = [device localizedName];
|
||||
video_input_name = (char *)malloc(strlen([localizedName cStringUsingEncoding: NSUTF8StringEncoding]) + 1);
|
||||
strcpy(video_input_name, (char *)[localizedName cStringUsingEncoding: NSUTF8StringEncoding]);
|
||||
video_input_name = (char *)malloc(strlen([localizedName cStringUsingEncoding: NSUTF8StringEncoding]) + 1);
|
||||
strcpy(video_input_name, (char *)[localizedName cStringUsingEncoding: NSUTF8StringEncoding]);
|
||||
device_names[i] = video_input_name;
|
||||
}
|
||||
|
||||
@ -151,7 +152,8 @@ strcpy(video_input_name, (char *)[localizedName cStringUsingEncoding: NSUTF8Stri
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
- (void)dealloc
|
||||
{
|
||||
pthread_mutex_destroy(&_frameLock);
|
||||
[_session release];
|
||||
[_linkerVideo release];
|
||||
@ -160,16 +162,17 @@ strcpy(video_input_name, (char *)[localizedName cStringUsingEncoding: NSUTF8Stri
|
||||
}
|
||||
|
||||
- (int)openVideoDeviceIndex:
|
||||
(uint32_t)device_idx Width:
|
||||
(uint16_t *)width Height:
|
||||
(uint16_t *)height {
|
||||
(uint32_t)device_idx Width:
|
||||
(uint16_t *)width Height:
|
||||
(uint16_t *)height
|
||||
{
|
||||
pthread_mutex_init(&_frameLock, NULL);
|
||||
pthread_mutex_lock(&_frameLock);
|
||||
_processingQueue = dispatch_queue_create("Toxic processing queue", DISPATCH_QUEUE_SERIAL);
|
||||
NSArray *devices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];
|
||||
AVCaptureDevice *device = [devices objectAtIndex: device_idx];
|
||||
NSArray *devices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];
|
||||
AVCaptureDevice *device = [devices objectAtIndex: device_idx];
|
||||
NSError *error = NULL;
|
||||
AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device error: &error];
|
||||
AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device error: &error];
|
||||
|
||||
if ( error != NULL ) {
|
||||
[input release];
|
||||
@ -177,7 +180,7 @@ AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device err
|
||||
}
|
||||
|
||||
[_session beginConfiguration];
|
||||
[_session addInput: input];
|
||||
[_session addInput: input];
|
||||
//_session.sessionPreset = AVCaptureSessionPreset640x480;
|
||||
//*width = 640;
|
||||
//*height = 480;
|
||||
@ -200,19 +203,20 @@ AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device err
|
||||
}
|
||||
|
||||
_linkerVideo = [[AVCaptureVideoDataOutput alloc] init];
|
||||
[_linkerVideo setSampleBufferDelegate: self queue: _processingQueue];
|
||||
[_linkerVideo setSampleBufferDelegate: self queue: _processingQueue];
|
||||
|
||||
// TODO possibly get a better pixel format
|
||||
if (_shouldMangleDimensions) {
|
||||
[_linkerVideo setVideoSettings: @ {
|
||||
[_linkerVideo setVideoSettings: @ {
|
||||
(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA),
|
||||
(id)kCVPixelBufferWidthKey: @640,
|
||||
(id)kCVPixelBufferHeightKey: @480
|
||||
}];
|
||||
} else {
|
||||
[_linkerVideo setVideoSettings: @{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)}];
|
||||
[_linkerVideo setVideoSettings: @ {(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)}];
|
||||
}
|
||||
[_session addOutput: _linkerVideo];
|
||||
|
||||
[_session addOutput: _linkerVideo];
|
||||
[_session startRunning];
|
||||
|
||||
pthread_mutex_unlock(&_frameLock);
|
||||
@ -220,21 +224,23 @@ AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device err
|
||||
}
|
||||
|
||||
- (void)closeVideoDeviceIndex:
|
||||
(uint32_t)device_idx {
|
||||
NSArray *devices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];
|
||||
AVCaptureDevice *device = [devices objectAtIndex: device_idx];
|
||||
(uint32_t)device_idx
|
||||
{
|
||||
NSArray *devices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];
|
||||
AVCaptureDevice *device = [devices objectAtIndex: device_idx];
|
||||
NSError *error = NULL;
|
||||
AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device error: &error];
|
||||
AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device error: &error];
|
||||
[_session stopRunning];
|
||||
[_session removeOutput: _linkerVideo];
|
||||
[_session removeInput: input];
|
||||
[_session removeOutput: _linkerVideo];
|
||||
[_session removeInput: input];
|
||||
[_linkerVideo release];
|
||||
}
|
||||
|
||||
- (void)captureOutput:
|
||||
(AVCaptureOutput *)captureOutput didOutputSampleBuffer:
|
||||
(CMSampleBufferRef)sampleBuffer fromConnection:
|
||||
(AVCaptureConnection *)connection {
|
||||
(AVCaptureOutput *)captureOutput didOutputSampleBuffer:
|
||||
(CMSampleBufferRef)sampleBuffer fromConnection:
|
||||
(AVCaptureConnection *)connection
|
||||
{
|
||||
pthread_mutex_lock(&_frameLock);
|
||||
CVImageBufferRef img = CMSampleBufferGetImageBuffer(sampleBuffer);
|
||||
|
||||
@ -248,15 +254,17 @@ AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device err
|
||||
// we're not going to do anything to it, so it's safe to lock it always
|
||||
CVPixelBufferLockBaseAddress(_currentFrame, kCVPixelBufferLock_ReadOnly);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&_frameLock);
|
||||
}
|
||||
|
||||
- (int)getVideoFrameY:
|
||||
(uint8_t *)y U:
|
||||
(uint8_t *)u V:
|
||||
(uint8_t *)v Width:
|
||||
(uint16_t *)width Height:
|
||||
(uint16_t *)height {
|
||||
(uint8_t *)y U:
|
||||
(uint8_t *)u V:
|
||||
(uint8_t *)v Width:
|
||||
(uint16_t *)width Height:
|
||||
(uint16_t *)height
|
||||
{
|
||||
if (!_currentFrame) {
|
||||
return -1;
|
||||
}
|
||||
@ -293,7 +301,7 @@ static OSXVideo *_OSXVideo = nil;
|
||||
|
||||
int osx_video_init(char **device_names, int *size)
|
||||
{
|
||||
_OSXVideo = [[OSXVideo alloc] initWithDeviceNames: device_names AmtDevices: size];
|
||||
_OSXVideo = [[OSXVideo alloc] initWithDeviceNames: device_names AmtDevices: size];
|
||||
|
||||
if ( _OSXVideo == nil )
|
||||
return -1;
|
||||
@ -312,12 +320,12 @@ int osx_video_open_device(uint32_t selection, uint16_t *width, uint16_t *height)
|
||||
if ( _OSXVideo == nil )
|
||||
return -1;
|
||||
|
||||
return [_OSXVideo openVideoDeviceIndex: selection Width: width Height: height];
|
||||
return [_OSXVideo openVideoDeviceIndex: selection Width: width Height: height];
|
||||
}
|
||||
|
||||
void osx_video_close_device(uint32_t device_idx)
|
||||
{
|
||||
[_OSXVideo closeVideoDeviceIndex: device_idx];
|
||||
[_OSXVideo closeVideoDeviceIndex: device_idx];
|
||||
}
|
||||
|
||||
int osx_video_read_device(uint8_t *y, uint8_t *u, uint8_t *v, uint16_t *width, uint16_t *height)
|
||||
@ -325,7 +333,7 @@ int osx_video_read_device(uint8_t *y, uint8_t *u, uint8_t *v, uint16_t *width, u
|
||||
if ( _OSXVideo == nil )
|
||||
return -1;
|
||||
|
||||
return [_OSXVideo getVideoFrameY: y U: u V: v Width: width Height: height];
|
||||
return [_OSXVideo getVideoFrameY: y U: u V: v Width: width Height: height];
|
||||
}
|
||||
/*
|
||||
* End of C-interface for OSXVideo
|
||||
|
126
src/qr_code.c
126
src/qr_code.c
@ -28,25 +28,36 @@
|
||||
#include "windows.h"
|
||||
#include "qr_code.h"
|
||||
|
||||
#ifdef QRPNG
|
||||
#include <png.h>
|
||||
#define INCHES_PER_METER (100.0/2.54)
|
||||
#define DPI 72
|
||||
#define SQUARE_SIZE 5
|
||||
#endif /* QRPNG */
|
||||
|
||||
#define BORDER_LEN 1
|
||||
#define CHAR_1 "\342\226\210"
|
||||
#define CHAR_2 "\342\226\204"
|
||||
#define CHAR_3 "\342\226\200"
|
||||
|
||||
/* Converts a tox ID string into a QRcode and prints it to the given file stream.
|
||||
/* Converts a tox ID string into a QRcode and prints it into the given filename.
|
||||
*
|
||||
* Returns 0 on success.
|
||||
* Returns -1 on failure.
|
||||
*/
|
||||
int ID_to_QRcode(const char *tox_id, FILE *fp)
|
||||
int ID_to_QRcode_txt(const char *tox_id, const char *outfile)
|
||||
{
|
||||
FILE *fp = fopen(outfile, "wb");
|
||||
|
||||
if (fp == NULL)
|
||||
return -1;
|
||||
|
||||
QRcode *qr_obj = QRcode_encodeString(tox_id, 0, QR_ECLEVEL_L, QR_MODE_8, 0);
|
||||
|
||||
if (qr_obj == NULL)
|
||||
if (qr_obj == NULL) {
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t width = qr_obj->width;
|
||||
size_t i, j;
|
||||
@ -83,7 +94,116 @@ int ID_to_QRcode(const char *tox_id, FILE *fp)
|
||||
fprintf(fp, "\n");
|
||||
}
|
||||
|
||||
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;
|
||||
unsigned char *p;
|
||||
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");
|
||||
|
||||
if (fp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
QRcode *qr_obj = QRcode_encodeString(tox_id, 0, QR_ECLEVEL_L, QR_MODE_8, 0);
|
||||
|
||||
if (qr_obj == NULL) {
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
real_width = (qr_obj->width + BORDER_LEN * 2) * SQUARE_SIZE;
|
||||
size_t row_size = real_width * 4;
|
||||
unsigned char row[row_size];
|
||||
|
||||
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
|
||||
if (png_ptr == NULL) {
|
||||
fclose(fp);
|
||||
QRcode_free(qr_obj);
|
||||
return -1;
|
||||
}
|
||||
|
||||
info_ptr = png_create_info_struct(png_ptr);
|
||||
|
||||
if (info_ptr == NULL) {
|
||||
fclose(fp);
|
||||
QRcode_free(qr_obj);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||
fclose(fp);
|
||||
QRcode_free(qr_obj);
|
||||
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);
|
||||
png_set_pHYs(png_ptr, info_ptr, DPI * INCHES_PER_METER,
|
||||
DPI * INCHES_PER_METER, PNG_RESOLUTION_METER);
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
|
||||
/* top margin */
|
||||
memset(row, 0xff, row_size);
|
||||
|
||||
for (y = 0; y < BORDER_LEN * SQUARE_SIZE; y++) {
|
||||
png_write_row(png_ptr, row);
|
||||
}
|
||||
|
||||
/* data */
|
||||
p = qr_obj->data;
|
||||
|
||||
for (y = 0; y < qr_obj->width; y++) {
|
||||
memset(row, 0xff, row_size);
|
||||
|
||||
for (x = 0; x < qr_obj->width; x++) {
|
||||
for (xx = 0; xx < SQUARE_SIZE; xx++) {
|
||||
if (*p & 1) {
|
||||
memcpy(&row[((BORDER_LEN + x) * SQUARE_SIZE + xx) * 4], black, 4);
|
||||
}
|
||||
}
|
||||
|
||||
p++;
|
||||
}
|
||||
|
||||
for (yy = 0; yy < SQUARE_SIZE; yy++) {
|
||||
png_write_row(png_ptr, row);
|
||||
}
|
||||
}
|
||||
|
||||
/* bottom margin */
|
||||
memset(row, 0xff, row_size);
|
||||
|
||||
for (y = 0; y < BORDER_LEN * SQUARE_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);
|
||||
QRcode_free(qr_obj);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* QRPNG */
|
||||
|
@ -25,11 +25,21 @@
|
||||
|
||||
#define QRCODE_FILENAME_EXT ".QRcode"
|
||||
|
||||
/* Converts a tox ID string into a QRcode and prints it to the given file stream.
|
||||
/* Converts a tox ID string into a QRcode and prints it into the given filename.
|
||||
*
|
||||
* Returns 0 on success.
|
||||
* Returns -1 on failure.
|
||||
*/
|
||||
int ID_to_QRcode(const char *tox_id, FILE *fp);
|
||||
int ID_to_QRcode_txt(const char *tox_id, const char *outfile);
|
||||
|
||||
#ifdef QRPNG
|
||||
#define QRCODE_FILENAME_EXT_PNG ".QRcode.png"
|
||||
/* 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);
|
||||
#endif /* QRPNG */
|
||||
|
||||
#endif /* QR_CODE */
|
||||
|
Loading…
Reference in New Issue
Block a user