/**
* cli.c: An exemplar, but fully functional and highly configurable
* command line interface (CLI) to the paper tape low level drawing
* routines (lochstreifen.c), which uses the famous cairo graphics
* library for drawing.
*
* See ./program --help for an overview about the self-explanatory
* arguments. By default, the program will read in any files in
* stdin and print the genereated PNG file on stdout.
*
* This program is written in english only (but the sourcecode
* contains some german comments). See an exemplar usage of this
* program in a PHP web program in the web-frontend subproject.
*
* This program uses the argp.h argument parser from the glibc.
* Thus it unfortunately won't compile with any other libc.
*
* Copyright (C) 2008 Sven Köppel
*
* This program 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.
*
* This program 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 this program; if not, see
* .
*
**/
#include
#include
#include
#include
#include "lochstreifen.h"
LOCHSTREIFEN *l;
int surface_type = 0; // 0 = PNG, 1 = SVG
char *output_file;
int verbosity = 0;
#define DPRINTF(msg...) if(verbosity!=0) fprintf(stderr,msg);
const char *argp_program_version = "Punch card visualisator - CLI frontend";
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Produce verbose output on stderr" },
//{"quiet", 'q', 0, 0, "Don't produce any output" },
//{"silent", 's', 0, OPTION_ALIAS },
{"output", 'o', "FILE", 0, "Output to FILE (instead of standard output)" },
{"image", 'i', 0, OPTION_ALIAS },
{"format", 'f', "SVG|PNG",0, "Set desired output image format (PNG or SVG)" },
{"", 0, 0, OPTION_DOC, ""},
{"Dimensions", 0, 0, OPTION_DOC, "Dimensions are integers without units"},
{"width", 'w', "NUM", 0, "Set desired width for output image (in px or points, according to output format)", 1 },
{"height", 'h', "NUM", 0, "Set desired height for output image (unit like width argument)", 1 },
{"diameter", 'd', "NUM", 0, "Set dimensions for output image by punched hole diameter (pixel)", 1 },
{"", 0, 0, OPTION_DOC, "",1},
{"Empty bytes", 1, 0, OPTION_DOC, "Bytes with value=0 which are not content of files", 1},
{"empty-start", -1, "NUM", 0, "Set number of empty bytes at beginning (default=0)", 2 },
{"empty-end", -2, "NUM", 0, "Set number of empty bytes at end (default=0)", 2 },
{"", 0, 0, OPTION_DOC, "",2},
{"Colors", 0, 0, OPTION_DOC, "Color format: #RGB[A], #RRGGBB[AA]", 2 },
{"hide-imagebg", -3, 0, 0, "Make the image background (space around paper tape) transparent", 3 },
{"color-imagebg", -4, "#RGBA", 0, "Set image background color", 3 },
{"hide-tapebg", -5, 0, 0, "Hide the paper tape background", 3 },
{"color-tapebg", -6, "#RGBA", 0, "Set tape background color", 3 },
{"hide-punched", -7, 0, 0, "Hide the holes (only the punched ones)", 3 },
{"color-punched", -8, "#RGBA", 0, "Set color of holes (punched bits)", 3 },
{"hide-notpunched", -9, 0, 0, "Hide the holes which aren't punched on real tapes", 3 },
{"color-notpunched",-10, "#RGBA", 0, "Set color of bits with boolean value \"false\"", 3 },
{"hide-feedholes", -11, 0, 0, "Hide the feed holes (which means they wouldn't be punched)", 3 },
{"color-feedholes", -12, "#RGBA", 0, "Set color of feed holes (the small ones)", 3 },
{"", 0, 0, OPTION_DOC, "",3},
{"Transformations and Rotations", 0, 0, OPTION_DOC, "", 3},
{"rotation", -13, "right|bottom|left|top", 0, "Rotation of punched tape (alternative short notation: 0=right, 1=bottom, 2=left, 3=top)", 4 },
{"reflection-byte-direction", -14, 0, 0, "Enables horizontal reflecion on the data axis (inverts data direction)", 4 },
{"reflection-bit-direction", -15, 0, 0, "Enables vertical reflection on the other axis (inverts bit direction)", 4 },
{"", 0, 0, OPTION_DOC, "",-2},
{ 0 }
};
char *strtolower(char *string) {
// komisch, ich dachte echt, es gaebe dafuer eine Funktion in C...
int x=0;
for(;string[x];x++) string[x] = tolower(string[x]);
return string; // ja, ich veraenderte den Ursprungsstring. Egal ;-)
}
cairo_pattern_t *hex2cairo_pattern(const char *string) {
/** Kriegt aus #RRGGBBAA oder #RGBA eine Cairo-Surface */
char buf[2]; // zum temporaeren Speichern der einzelnen umzuwandelnden Zeichen
buf[1] = '\0'; // nur ein Ein-Zeichen-String also
int len = strlen(string);
unsigned char numbers[8]; // rausgefundende Ziffern
int numbers_count; // wie viele Ziffern rausgefunden
cairo_pattern_t *pattern;
// erst mal schauen, ob ne schoene Raute da ist und auf Laenge (entsprechendes Format)
if(string[0] != '#' || (len != 4 && len != 5 && len != 7 && len != 9)) {
fprintf(stderr, "Bad color: %s -- colors shall have format #RGB or #RRGGBB or #RGBA or #RRGGBBAA\n", string, len);
exit(1);
}
string++; // das "#"-Zeichen braucht keiner mehr
for(numbers_count=0; numbers_count < len-1; numbers_count++) {
buf[0] = string[numbers_count];
numbers[numbers_count] = strtol(buf, NULL, 16);
//printf("Read digit %s = 0x%x\n", buf, numbers[numbers_count]);
}
//numbers_count++; // wir zaehlen ab 1 -- komisch, tud er sowieso schon.
if(numbers_count == 3 || numbers_count == 4) {
// #RGB oder #RGBA
pattern = cairo_pattern_create_rgba(
(double)numbers[0] / (double)0xF,
(double)numbers[1] / (double)0xF,
(double)numbers[2] / (double)0xF,
(numbers_count == 4) ? (double)numbers[3] / (double)0xF : 1
);
} else {
// #RRGGBB oder #RRGGBBAA
pattern = cairo_pattern_create_rgba(
(double)(numbers[1] + numbers[0]*0x10) / (double)0xFF,
(double)(numbers[3] + numbers[2]*0x10) / (double)0xFF,
(double)(numbers[5] + numbers[4]*0x10) / (double)0xFF,
(numbers_count == 8) ? (double)(numbers[7] + numbers[6]*0x10) / (double)0xFF : 1
);
}
// Fuer Debug-Ausgaben:
{ double r,g,b,a; cairo_pattern_get_rgba(pattern, &r, &g, &b, &a);
printf("Allocated color #%s as #%x%x%x, opacity %x\n", string, (unsigned int)(r*256), (unsigned int)(g*256), (unsigned int)(b*256), (unsigned int)(a*256)); }
printf("Farbe fertig.\n");
return pattern;
}
error_t parse_option (int key, char *arg, struct argp_state *state) {
//printf("OPTION %x (%c = %i)...\n", key, key, key);
switch(key) {
case 'v':
verbosity = 1;
break;
case 'o': case 'i':
// Ausgabedatei setzen.
output_file = arg;
break;
case 'w':
// Breite setzen.
lochstreifen_set_d_by_width(l, atoi(arg));
break;
case 'h':
// Hoehe setzen.
lochstreifen_set_d_by_height(l, atoi(arg));
break;
case 'd':
// Durchmesser setzen
lochstreifen_set_d(l, atoi(arg));
break;
case 'f':
// Dateiformat setzen
// keine Lust auf enums
if(strcmp(strtolower(arg), "png") == 0) surface_type = 0;
else if(strcmp(strtolower(arg), "svg") == 0) surface_type = 1;
else argp_error(state, "Only PNG and SVG are supported as file formats.\n");
break;
case -1:
// Emptystart-Bytes
l->empty_start = atoi(arg);
break;
case -2:
// Emptyend-Bytes
l->empty_end = atoi(arg);
break;
case -3:
// hide imagebg
l->hintergrund = NULL;
break;
case -4:
// color imagebg
l->hintergrund = hex2cairo_pattern(arg);
break;
case -5:
// hide tape
l->streifenbg = NULL;
break;
case -6:
// color tapebg
l->streifenbg = hex2cairo_pattern(arg);
break;
case -7:
// hide punched
l->punched = NULL;
break;
case -8:
// color punched
l->punched = hex2cairo_pattern(arg);
break;
case -9:
// hide notpunched
l->notpunched = NULL;
break;
case -10:
// color notpunched
l->notpunched = hex2cairo_pattern(arg);
break;
case -11:
// hide feedholes
l->fuehrung = NULL;
break;
case -12:
// color fuerhung
l->fuehrung = hex2cairo_pattern(arg);
break;
case -13:
// rotation
if(strcmp(arg, "right") == 0) arg = "0";
else if(strcmp(arg, "bottom") == 0) arg = "1";
else if(strcmp(arg, "left") == 0) arg = "2";
else if(strcmp(arg, "top") == 0) arg = "3";
arg[1] = '\0'; // String ist jetzt auf jeden Fall nur ein zeichen lang
lochstreifen_set_direction(l, atoi(arg), -1, -1);
break;
case -14:
// horizontal spiegeln
lochstreifen_set_direction(l, -1, 1, -1);
break;
case -15:
// vertikal spiegeln
lochstreifen_set_direction(l, -1, -1, 1);
break;
//case ARGP_KEY_END:
//printf("bla...");
default:
return ARGP_ERR_UNKNOWN;
} // switch
return 0; // success
}
static struct argp argp = { options, parse_option, "[FILE TO READ FROM]", // Als Argument in der ersten Zeile
"This program uses cairo to draw a punched tape as a PNG or SVG file. Any binary " // Hilfe oben
"data are accepted via stdin or read in from the file given as argument. "
"The produced image is written into stdout or in the filename given by -o."
// mit \v danach koennte man ausfuehrliche Hilfe unten anzeigen.
}; // static struct argp
cairo_status_t* lochstreifen_out_closure(void *closure, unsigned char *data, unsigned int length) {
// einfach nur in das uebergebene Dateihandle schreiben
fwrite(data, length, 1, (FILE *)closure);
return CAIRO_STATUS_SUCCESS;
}
int main(int argc, char *argv[]) {
//unsigned char data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
/*int x;
unsigned char data[256];
for(x=0;x<256;x++) {
data[x] = (unsigned char) x;
}
draw_lochstreifen(256, data);
return 0;*/
LOCHSTREIFEN *lochstreifen;
cairo_t *cr;
cairo_surface_t *surface;
byte_t *data;
int length; // laenge des *data-Arrays
FILE *out;
int input_argc; // Index der argv-Option, wo die zu handelnden Dateien stehen.
l = lochstreifen_new();
lochstreifen = l;
// Argumente parsen
argp_parse(&argp, argc, argv, 0, &input_argc, NULL);
//DPRINTF("Stop.\n");
//return 0;
// Daten einlesen
if(input_argc < argc) {
FILE *fh;
DPRINTF("Reading from file %s\n", argv[input_argc]);
fh = fopen(argv[input_argc], "r");
if(fh == NULL) {
perror("opening input file");
exit(1);
}
length = file_get_contents(fh, &data);
fclose(fh);
} else {
DPRINTF("Reading from stdin, type [STRG]+[D] to generate paper tape from data\n");
length = file_get_contents(stdin, &data);
}
DPRINTF("Read in %d bytes\n", length);
// Ausgabestream oeffnen
if(output_file != NULL) {
out = fopen(output_file, "w");
if(out == NULL) {
perror("opening output file");
exit(1);
}
DPRINTF("File %s successfully opened for writing\n", output_file);
} else {
DPRINTF("Writing image data to stdout\n");
out = stdout;
}
/*int x;
for(x=0; x