-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e81fd1
commit 7cd65f5
Showing
5 changed files
with
239 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#ifndef IMG_TO_TXT_ARG_H | ||
#define IMG_TO_TXT_ARG_H | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
|
||
#include "options.h" | ||
|
||
#define STR_EQUAL 0 | ||
|
||
#define VERSION "v0.1.1" | ||
|
||
#define HELP "Usage: img_to_txt [OPTION]... [FILE]... \n\ | ||
Convert images to text. Either Ascii, Ansi (true colour), \n\ | ||
or solid background (Ansi true color). \n\ | ||
\n\ | ||
Note: Long arg (--example) not yet supported. \n\ | ||
\n\ | ||
Available input image types: \n\ | ||
- jpg/jpeg \n\ | ||
- png \n\ | ||
- bmp \n\ | ||
- tga \n\ | ||
- hdr \n\ | ||
- gif (static) \n\ | ||
- pic (not tested yet) \n\ | ||
- ppm/pnm/pgm \n\ | ||
\n\ | ||
Options: \n\ | ||
-w <width> Set width and height (rows and cols) of output. \n\ | ||
-h <height> \n\ | ||
\n\ | ||
-o Output original size. (Size of image in text) \n\ | ||
\n\ | ||
-a Ascii mode. (no ansi colors) \n\ | ||
-s Solid mode. (Background colors with no ascii chars) \n\ | ||
\n\ | ||
-t Use true color for ANSI output. \n\ | ||
(Need true-color supported terminal) \n\ | ||
\n\ | ||
-n Disable standard image squashing that tries to make \n\ | ||
image look less elongated by chars being taller than\n\ | ||
they are wide. \n\ | ||
\n\ | ||
-q Quiet mode (suppress header) \n\ | ||
\n\ | ||
-? Print help message and exit. \n\ | ||
\n\ | ||
-v Print version and exit. \n\ | ||
\n\ | ||
Examples: \n\ | ||
\n\ | ||
img_to_txt -h 100 -w 200 -s some_img.png \n\ | ||
\n\ | ||
img_to_txt -whqt 30 some_img.png \n\ | ||
\n\ | ||
img_to_txt some_img.jpg -a \n\ | ||
\n\ | ||
img_to_txt -q -t -o some_img.bmp \n\ | ||
" | ||
|
||
|
||
typedef struct _FileJob { | ||
char * path; | ||
struct _FileJob * nextJob; | ||
} FileJob; | ||
|
||
FileJob * arg_parse(int argc, char ** argv, ImageOptions * opts); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef IMG_TO_TXT_OPTIONS_H | ||
#define IMG_TO_TXT_OPTIONS_H | ||
|
||
// Some preprocessor magic to generate an enum and string array with the same items. | ||
#define OUTPUT_MODES(MODE) \ | ||
MODE(ANSI) \ | ||
MODE(SOLID_ANSI) \ | ||
MODE(ASCII) \ | ||
|
||
#define GENERATE_ENUM(ENUM) ENUM, | ||
#define GENERATE_STRING(STRING) #STRING, | ||
|
||
enum OutputModes {OUTPUT_MODES(GENERATE_ENUM)}; | ||
static const char * OutputModeStr[] = {OUTPUT_MODES(GENERATE_STRING)}; // Strings used for output. | ||
|
||
typedef struct { | ||
unsigned int width; | ||
unsigned int height; | ||
|
||
enum OutputModes output_mode; | ||
|
||
bool original_size; | ||
bool true_color; | ||
bool squashing_enabled; | ||
bool suppress_header; | ||
} ImageOptions; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include "arg_parse.h" | ||
|
||
static void print_version(void) { | ||
printf("img_to_text %s\n", VERSION); | ||
} | ||
|
||
static void print_help(void) { | ||
printf("%s\n", HELP); | ||
} | ||
|
||
static int get_opt_int_param(int argc, char ** argv, int opt) { | ||
int scan_result; | ||
int param; | ||
|
||
if (opt == argc - 1) { | ||
fprintf(stderr, "Not enough arguments to read option parameter!\n"); | ||
return -1; | ||
} | ||
|
||
scan_result = sscanf( argv[opt + 1], "%d", ¶m ); | ||
if (!scan_result) { | ||
fprintf(stderr, "Couldn't read option parameter!\n"); | ||
return -1; | ||
} | ||
|
||
#ifdef DEBUG_CONFIG_SET | ||
printf("[DEBUG] Arg: %d\n", param); | ||
#endif | ||
|
||
return param; | ||
} | ||
|
||
FileJob * arg_parse(int argc, char ** argv, ImageOptions * opts) { | ||
// Returns a pointer to FileJobs array or NULL. | ||
|
||
FileJob * jobs = NULL; | ||
|
||
// Parse arguments | ||
bool stop_opt = false; | ||
for (int opt = 1; opt < argc; opt++) { | ||
|
||
// If not an option then add to job linked list. | ||
if (argv[opt][0] != '-' || stop_opt == true) { | ||
FileJob * job = (FileJob *) malloc(sizeof(FileJob)); | ||
|
||
if (job == NULL) { | ||
perror("Couldn't allocate memory!"); | ||
return NULL; | ||
} | ||
|
||
job->path = argv[opt]; | ||
job->nextJob = jobs; | ||
|
||
jobs = job; | ||
continue; | ||
} | ||
|
||
if ( strcmp(argv[opt], "--") == STR_EQUAL ) { | ||
stop_opt = true; | ||
continue; | ||
} | ||
|
||
#ifdef DEBUG_CONFIG_SET | ||
printf("[DEBUG] Opt[%d]: %s\n", opt, argv[opt]); | ||
#endif | ||
|
||
int width; | ||
int height; | ||
bool read_param = false; | ||
for (size_t c = 1; c < strlen(argv[opt]); c++) { | ||
switch (argv[opt][c]) { | ||
|
||
case '?': | ||
print_help(); | ||
return NULL; | ||
case 'v': | ||
print_version(); | ||
return NULL; | ||
|
||
case '-': | ||
//parse_long_arg(argv[opt]); | ||
break; | ||
|
||
case 'w': | ||
width = get_opt_int_param(argc, argv, opt); | ||
if (width < 0) { | ||
fprintf(stderr, "[ERR] Width is non-zero!\n"); | ||
return NULL; | ||
} | ||
opts->width = (unsigned int)width; | ||
read_param = true; | ||
break; | ||
case 'h': | ||
height = get_opt_int_param(argc, argv, opt); | ||
if (height < 0) { | ||
fprintf(stderr, "[ERR] Height is non-zero!\n"); | ||
return NULL; | ||
} | ||
opts->height = (unsigned int)height; | ||
read_param = true; | ||
break; | ||
|
||
case 'a': | ||
opts->output_mode = ASCII; | ||
opts->true_color = false; | ||
break; | ||
case 's': | ||
opts->output_mode = SOLID_ANSI; | ||
opts->true_color = true; | ||
break; | ||
case 'o': | ||
opts->original_size = true; | ||
break; | ||
case 't': | ||
opts->true_color = true; | ||
break; | ||
case 'n': | ||
opts->squashing_enabled = false; | ||
break; | ||
case 'q': | ||
opts->suppress_header = true; | ||
break; | ||
default: | ||
printf("Unknown option: %s\n", argv[opt]); | ||
return NULL; | ||
} | ||
} | ||
if (read_param) opt++; | ||
} | ||
return jobs; | ||
} |
Oops, something went wrong.