From ef80d11beb370a18d8cd01f881fc3c2bd43d7ceb Mon Sep 17 00:00:00 2001 From: hooper114 Date: Sun, 21 Dec 2008 12:17:14 +0000 Subject: [PATCH] Allow output to stdout --- backend/png.c | 18 ++++++++++-------- backend/ps.c | 10 +++++++--- backend/zint.h | 1 + frontend/main.c | 17 +++++++++++++++-- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/backend/png.c b/backend/png.c index d35d25fb..b24624f9 100644 --- a/backend/png.c +++ b/backend/png.c @@ -110,15 +110,17 @@ int png_to_file(struct zint_symbol *symbol, int image_height, int image_width, c bgblu = (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]); /* Open output file in binary mode */ - if (!(graphic->outfile = fopen(symbol->outfile, "wb"))) { - strcpy(symbol->errtxt, "Can't open output file [B5]"); - return ERROR_FILE_ACCESS; + if((symbol->output_options & BARCODE_STDOUT) != 0) { + graphic->outfile = stdout; + } else { + if (!(graphic->outfile = fopen(symbol->outfile, "wb"))) { + strcpy(symbol->errtxt, "Can't open output file [B5]"); + return ERROR_FILE_ACCESS; + } } - /* graphic->outfile = stdout; */ /* Set up error handling routine as proc() above */ - png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, graphic, - writepng_error_handler, NULL); + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, graphic, writepng_error_handler, NULL); if (!png_ptr) { strcpy(symbol->errtxt, "Out of memory [B6]"); return ERROR_MEMORY; @@ -758,7 +760,7 @@ int png_plot(struct zint_symbol *symbol, int rotate_angle) xoffset -= comp_offset; /* Put boundary bars or box around symbol */ - if ((symbol->output_options == BARCODE_BOX) || (symbol->output_options == BARCODE_BIND)) { + if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { if(symbol->symbology != BARCODE_CODABLOCKF) { /* boundary bars */ draw_bar(pixelbuf, 0, (symbol->width + xoffset + xoffset) * scaler, textoffset * scaler, symbol->border_width * scaler, image_width, image_height); @@ -782,7 +784,7 @@ int png_plot(struct zint_symbol *symbol, int rotate_angle) } } - if (symbol->output_options == BARCODE_BOX) { + if((symbol->output_options & BARCODE_BOX) != 0) { /* side bars */ draw_bar(pixelbuf, 0, symbol->border_width * scaler, textoffset * scaler, (symbol->height + (2 * symbol->border_width)) * scaler, image_width, image_height); draw_bar(pixelbuf, (symbol->width + xoffset + xoffset - symbol->border_width) * scaler, symbol->border_width * scaler, textoffset * scaler, (symbol->height + (2 * symbol->border_width)) * scaler, image_width, image_height); diff --git a/backend/ps.c b/backend/ps.c index ad9666b2..9f5a5011 100644 --- a/backend/ps.c +++ b/backend/ps.c @@ -66,7 +66,11 @@ int ps_plot(struct zint_symbol *symbol) comp_offset = 0; addon_text_posn = 0.0; - feps = fopen(symbol->outfile, "w"); + if((symbol->output_options & BARCODE_STDOUT) != 0) { + feps = stdout; + } else { + feps = fopen(symbol->outfile, "w"); + } if(feps == NULL) { strcpy(symbol->errtxt, "Could not open output file [C1]"); return ERROR_FILE_ACCESS; @@ -683,7 +687,7 @@ int ps_plot(struct zint_symbol *symbol) /* Put boundary bars or box around symbol */ - if ((symbol->output_options == BARCODE_BOX) || (symbol->output_options == BARCODE_BIND)) { + if (((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { if(symbol->symbology != BARCODE_CODABLOCKF) { /* boundary bars */ fprintf(feps, "TE\n"); @@ -719,7 +723,7 @@ int ps_plot(struct zint_symbol *symbol) } } - if (symbol->output_options == BARCODE_BOX) { + if((symbol->output_options & BARCODE_BOX) != 0) { /* side bars */ fprintf(feps, "TE\n"); fprintf(feps, "%.2f %.2f %.2f setrgbcolor\n", red_ink, green_ink, blue_ink); diff --git a/backend/zint.h b/backend/zint.h index bd332bb3..3597e07c 100644 --- a/backend/zint.h +++ b/backend/zint.h @@ -129,6 +129,7 @@ struct zint_symbol { #define BARCODE_NO_ASCII 1 #define BARCODE_BIND 2 #define BARCODE_BOX 4 +#define BARCODE_STDOUT 8 #define WARN_INVALID_OPTION 2 #define ERROR_TOO_LONG 5 diff --git a/frontend/main.c b/frontend/main.c index 9b21ea55..36eb7b02 100644 --- a/frontend/main.c +++ b/frontend/main.c @@ -73,6 +73,9 @@ void usage(void) " -r, --reverse Reverse colours (white on black).\n" " --fg=COLOUR Specify a foreground colour.\n" " --bg=COLOUR Specify a background colour.\n" + " --scale=NUMBER Adjust size of output image.\n" + " --directpng Send PNG output to stdout\n" + " --directeps Send EPS output to stdout\n" " --rotate=NUMBER Rotate symbol (PNG output only).\n" " --cols=NUMBER (PDF417) Number of columns.\n" " --vers=NUMBER (QR Code) Version\n" @@ -180,6 +183,8 @@ int main(int argc, char **argv) {"types", 0, 0, 't'}, {"bind", 0, 0, 0}, {"box", 0, 0, 0}, + {"directeps", 0, 0, 0}, + {"directpng", 0, 0, 0}, {"barcode=", 1, 0, 'b'}, {"height=", 1, 0, 0}, {"whitesp=", 1, 0, 'w'}, @@ -205,10 +210,18 @@ int main(int argc, char **argv) switch(c) { case 0: if(!strcmp(long_options[option_index].name, "bind")) { - my_symbol->output_options = BARCODE_BIND; + my_symbol->output_options += BARCODE_BIND; } if(!strcmp(long_options[option_index].name, "box")) { - my_symbol->output_options = BARCODE_BOX; + my_symbol->output_options += BARCODE_BOX; + } + if(!strcmp(long_options[option_index].name, "directeps")) { + my_symbol->output_options += BARCODE_STDOUT; + strncpy(my_symbol->outfile, "dummy.eps", 10); + } + if(!strcmp(long_options[option_index].name, "directpng")) { + my_symbol->output_options += BARCODE_STDOUT; + strncpy(my_symbol->outfile, "dummy.png", 10); } if(!strcmp(long_options[option_index].name, "fg=")) { strncpy(my_symbol->fgcolour, optarg, 7);