From 96c1d5c6b957740e006af9f62602ef9dbf830187 Mon Sep 17 00:00:00 2001 From: Dominic Radermacher Date: Mon, 30 Nov 2020 10:13:14 +0100 Subject: [PATCH] images can now also be read from stdin (thanks to Siim Salonen) --- src/ptouch-print.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/ptouch-print.c b/src/ptouch-print.c index 145e647..d45b938 100644 --- a/src/ptouch-print.c +++ b/src/ptouch-print.c @@ -126,15 +126,24 @@ gdImage *image_load(const char *file) FILE *f; gdImage *img=NULL; - if ((f = fopen(file, "rb")) == NULL) { /* error cant open file */ + if (!strcmp(file, "-")) { + f = stdin; + } else { + f = fopen(file, "rb"); + } + if (f == NULL) { /* error could not open file */ return NULL; } - if (fread(d, sizeof(d), 1, f) != 1) { - return NULL; - } - rewind(f); - if (memcmp(d, png, 8) == 0) { + if (fseek(f, 0L, SEEK_SET)) { /* file is not seekable. eg 'stdin' */ img=gdImageCreateFromPng(f); + } else { + if (fread(d, sizeof(d), 1, f) != 1) { + return NULL; + } + rewind(f); + if (memcmp(d, png, 8) == 0) { + img=gdImageCreateFromPng(f); + } } fclose(f); return img;