Skip to content

Commit

Permalink
Merge pull request #5 from plerros/getopt
Browse files Browse the repository at this point in the history
Getopt
  • Loading branch information
plerros authored Oct 20, 2021
2 parents f07bcd0 + ff61911 commit 02efdcc
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 17 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ git clone https://github.com/plerros/pixmap565.git
```
make
```
(to be filled in later)
## Run:
```
./pixmap565 -i infile.bmp -o outfile
./pixmap565 -w width -i infile -o outfile.bmp
```
## Scripts:
```
./mkstft28.sh infile outfile
./mkstft28-icon.sh infile outfile
```
3 changes: 3 additions & 0 deletions mkstft28-icon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# MKS TFT28 icon resolution: 78x104
./pixmap -w 78 -i $1 -o $2
3 changes: 3 additions & 0 deletions mkstft28.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# MKS TFT28 resolution: 320x240
./pixmap -w 320 -i $1 -o $2
201 changes: 186 additions & 15 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,225 @@
*/

#include <assert.h>
#include <ctype.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "picture.h"
#include "pixmap.h"

void help()
{
printf(
"Usage: pixmap565 [options] -i infile%s -o outfile\n"
" or: pixmap565 [options] -w width -i infile -o outfile%s\n"
"Convert between %s image and RGB565 pixmap.\n\n"
" -w [width] set the width (height is derived from filesize/width)\n"
"\nOptions:\n"
" --help display this help and exit\n",
picture_extension(),
picture_extension(),
picture_type()
);
}

static void strnewcpy(char **new, const char *old)
{
assert(old != NULL);
free(*new);
*new = malloc(sizeof(char) * (strlen(old) + 1));
if (new == NULL)
abort();

strcpy(*new, old);
}

static bool willoverflow(unsigned long x, unsigned digit)
{
assert(digit < 10);
if (x > ULONG_MAX / 10)
return true;
if (x == ULONG_MAX / 10 && digit > ULONG_MAX % 10)
return true;
return false;
}

static int strto_ul(const char *str, unsigned long *number) // string to unsigned long
{
assert(str != NULL);
assert(number != NULL);
int err = 0;
unsigned long tmp = 0;
for (size_t i = 0; isgraph(str[i]); i++) {
if (!isdigit(str[i])) {
err = 1;
goto out;
}
unsigned digit = str[i] - '0';
if (willoverflow(tmp, digit)) {
err = 1;
goto out;
}
tmp = 10 * tmp + digit;
}
*number = tmp;
out:
if (err)
fprintf(stderr, "Input out of range: [0, %lu]\n", ULONG_MAX);
return err;
}

int main(int argc, char *argv[])
{
int rc = 0;
// (test code)
char in[] = "about.bmp";
char out[] = "out.bin";

char *inname = NULL;
char *outname = NULL;
unsigned long width = 0;

struct picture *pic = NULL;
struct pixmap *pix = NULL;
FILE *infile = NULL;
FILE *outfile = NULL;

{
static int help_flag = 0;
bool infile_is_set = false;
bool outfile_is_set = false;
bool width_is_set = false;
int c;
while (1) {
static struct option long_options[] = {
{"help", no_argument, &help_flag, true},
{"infile", required_argument, NULL, 'i'},
{"outfile", required_argument, NULL, 'o'},
{"width", required_argument, NULL, 'w'},
{NULL, 0, NULL, 0}
};
int option_index = 0;
c = getopt_long(argc, argv, "i:o:w:", long_options, &option_index);

// Detect end of the options
if (c == -1)
break;

if (help_flag){
help();
goto out;
}

switch (c) {
case 0:
break;

FILE *infile = fopen(in, "r");
assert(infile != NULL);
case 'i':
if (infile_is_set) {
help();
goto out;
}
strnewcpy(&inname, optarg);
infile_is_set = true;
break;

FILE *outfile = fopen(out, "w+");
assert(outfile != NULL);
case 'o':
if (outfile_is_set) {
help();
goto out;
}
strnewcpy(&outname, optarg);
outfile_is_set = true;
break;

if (is_pic(in))
case 'w':
if (width_is_set) {
help();
goto out;
}
rc = strto_ul(optarg, &width);
width_is_set = true;
break;

case '?':
help();
goto out;

default:
abort();
}
}
if (!infile_is_set || !outfile_is_set) {
help();
goto out;
}
if (!(is_pic(inname) || is_pic(outname))) {
help();
goto out;
}
if (!is_pic(inname) && is_pic(outname) && !width_is_set) {
help();
goto out;
}
}

infile = fopen(inname, "r");
if (infile == NULL) {
printf("Cannot open file '%s'\n", inname);
rc = 1;
goto out;
}
if (access(outname, F_OK) == 0) {
printf("File '%s' already exists.\n", outname);
rc = 1;
goto out;
}
outfile = fopen(outname, "w+");
if (outfile == NULL) {
printf("Cannot open file '%s'\n", outname);
rc = 1;
goto out;
}

picture_new(&pic);

if (is_pic(inname))
{
picture_new(&pic);
rc = picture_read(pic, infile);
if (rc)
goto out;
pix = picture_get_pixmap(pic);
rc = pixmap_write(pix, outfile);
if (rc)
goto out;
} else {
pixmap_new(&pix, 78);
pixmap_new(&pix, width);
rc = pixmap_read(pix, infile);
if (rc)
goto out;
}

if (is_pic(outname)) {
picture_set_pixmap(pic, pix);
pix = NULL;
rc = picture_write(pic, outfile);
if (rc)
goto out;
} else {
rc = pixmap_write(pix, outfile);
if (rc)
goto out;
}

out:
picture_free(pic);
pixmap_free(pix);

fclose(infile);
fclose(outfile);
if (infile != NULL)
fclose(infile);
if (outfile != NULL)
fclose(outfile);

free(inname);
free(outname);
return rc;
}
12 changes: 11 additions & 1 deletion src/picture/picture.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void picture_set_pixmap(struct picture *ptr, struct pixmap *matrix)

ptr->image_size = dword_abs(ptr->width) * dword_abs(ptr->height) * BYTES_PER_PIXEL;
ptr->image_size += (ptr->image_size % 4);
ptr->file_bytes += ptr->image_size;
ptr->file_bytes = ptr->image_size;
}

struct pixmap *picture_get_pixmap(struct picture *ptr)
Expand All @@ -144,6 +144,16 @@ struct pixmap *picture_get_pixmap(struct picture *ptr)
return ret;
}

char *picture_type()
{
return ("BMP565");
}

char *picture_extension()
{
return (".bmp\0");
}

bool is_pic(char *filename)
{
bool ret = false;
Expand Down
2 changes: 2 additions & 0 deletions src/picture/picture.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void picture_free(struct picture *ptr);
void picture_set_pixmap(struct picture *ptr, struct pixmap *matrix);
struct pixmap *picture_get_pixmap(struct picture *ptr);

char *picture_type();
char *picture_extension();
bool is_pic(char *filename);

int picture_read(struct picture *ptr, FILE *fp);
Expand Down

0 comments on commit 02efdcc

Please sign in to comment.