Skip to content

Commit

Permalink
cleanup code; added bash version
Browse files Browse the repository at this point in the history
  • Loading branch information
8bitmcu committed Jan 8, 2024
1 parent 3583c6a commit ee073a8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
gcode-preview
=============

Extract images as png/jpg from .gcode files with C or Bash


Building and installing the C version
-------------------------------------

1. clone this repository locally on your machine
2. run `make clean && sudo make install` from within the repository folder


Which is faster?
----------------



7 changes: 7 additions & 0 deletions gcode-preview-bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

if [ "$#" -ne 2 ]; then
echo "illegal number of parameters. You should specify an input .gcode file and an output png/jpg file"
fi

sed -n '/thumbnail begin/,/thumbnail end/p;' "$1" | sed '$d;1d; s/^; //;' | sed -z "s/\n//g" | base64 -d - > "$2"
52 changes: 22 additions & 30 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include "Turbo-Base64/turbob64.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Turbo-Base64/turbob64.h"

static const char thumb_begin[] = "; thumbnail begin";
static const char thumb_end[] = "; thumbnail end\n";

void
gcodeimg_to_file(char* input, char* output)
{
void gcodeimg_to_file(char *input, char *output) {
// open input file for reading
FILE *fp = fopen(input, "r");
if (fp == NULL) {
Expand All @@ -29,7 +27,8 @@ gcodeimg_to_file(char* input, char* output)

// skip everything until we find the magic string
if (thumbnail_begin == 0) {
thumbnail_begin = strncmp(line, thumb_begin, sizeof(thumb_begin)-1) == 0;
thumbnail_begin =
strncmp(line, thumb_begin, sizeof(thumb_begin) - 1) == 0;
continue;
}

Expand All @@ -38,13 +37,13 @@ gcodeimg_to_file(char* input, char* output)
break;

// copy base64 string into b64
b64 = realloc(b64, (total_len+line_len) * sizeof(unsigned char));
b64 = realloc(b64, (total_len + line_len) * sizeof(unsigned char));
memcpy(b64 + total_len, &line[2], line_len);
total_len += line_len;
}

// allocate 3/4th the memory for the output
unsigned char out[(total_len*3/4)+8];
unsigned char out[(total_len * 3 / 4) + 8];

// decode b64 to out
tb64xdec(b64, total_len, out);
Expand All @@ -61,35 +60,28 @@ gcodeimg_to_file(char* input, char* output)
free(b64);
}

static void
usage(void)
{
puts("usage: gcode-preview [-i inputfile] [-o outputfile]");
static void usage(void) {
puts("usage: gcode-preview [-i inputfile] [-o outputfile]");
}


int
main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
int i;
char* input;
char* output;
char *input;
char *output;
for (i = 1; i < argc; i++)
/* these options take no arguments */
if (!strcmp(argv[i], "-v")) { /* prints version information */
puts("gcode-preview-0.1");
exit(EXIT_SUCCESS);
}
else if (!strcmp(argv[i], "-h")) { /* prints version information */
usage();
exit(EXIT_SUCCESS);
/* these options take no arguments */
if (!strcmp(argv[i], "-v")) { /* prints version information */
puts("gcode-preview-0.1");
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[i], "-h")) { /* prints version information */
usage();
exit(EXIT_SUCCESS);
}
/* these options take one argument */
else if (!strcmp(argv[i], "-i")) /* input filename */
input = argv[++i];
else if (!strcmp(argv[i], "-o")) /* input filename */
output = argv[++i];

else if (!strcmp(argv[i], "-i")) /* input filename */
input = argv[++i];
else if (!strcmp(argv[i], "-o")) /* input filename */
output = argv[++i];

if (!input) {
puts("input file not specified");
Expand Down

0 comments on commit ee073a8

Please sign in to comment.