diff --git a/Makefile b/Makefile index 904720c..766ecf2 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,8 @@ all: bin2png png2bin clean: @rm -rf *.o bin2png png2bin -CFLAGS = -D_XOPEN_SOURCE=600 -std=c99 -Wall -Wextra -LDFLAGS = -lpng +CFLAGS += -D_XOPEN_SOURCE=600 -std=c99 -Wall -Wextra +LDFLAGS += -lpng ifeq ($(PLATFORM_OS), Linux) LDFLAGS += -lm # required for sqrt() diff --git a/README.md b/README.md index 6e2f34c..2b23ac5 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,10 @@ Install `libpng`: cd imgify make +If you're on macOS, you need to install libpng using [Homebrew](https://brew.sh/) and run `make` using some extra flags. Example: + + CFLAGS="-I$(libpng-config --prefix)/include" LDFLAGS="-L$(libpng-config --prefix)/lib" make + ## Usage ### bin2png diff --git a/bin2png.c b/bin2png.c index 1cf51b1..7bd8451 100644 --- a/bin2png.c +++ b/bin2png.c @@ -27,11 +27,10 @@ #include "imgify.h" #include "common.h" +#include "common_options.h" +#include "version.h" #define PROGRAM "bin2png" -#define VERSION "1.0" - -#include "common_options.h" static void do_work(const options_t *options, void *data, size_t filesize) { const uint8_t channels = 4; diff --git a/imgify.c b/imgify.c index 0875c69..9202197 100644 --- a/imgify.c +++ b/imgify.c @@ -228,6 +228,14 @@ bool png_save(const char *filename, uint8_t *data, uint32_t width, uint32_t heig // The length of one row in bytes - the seme as `width * bit_depth * channels / 8`. const uint32_t rowbytes = png_get_rowbytes(png_ptr, info_ptr); + uint8_t *padded_row_ptr = malloc(rowbytes); + if (padded_row_ptr == NULL) { + png_destroy_write_struct(&png_ptr, &info_ptr); + fclose(fp); + perror("malloc"); + return false; + } + const bool needs_padding = padding > 0; for (uint32_t i=0; i < height; i++) { const bool is_lastrow = i == height - 1; @@ -236,19 +244,9 @@ bool png_save(const char *filename, uint8_t *data, uint32_t width, uint32_t heig //printf("DEBUG: [png_save] row=%u width=%u rowbytes=%u\n", i+1, width, rowbytes); - if (!is_padding_now) - { + if (!is_padding_now) { png_write_row(png_ptr, row_ptr); - } - else - { - uint8_t *padded_row_ptr = malloc(rowbytes); - if (padded_row_ptr == NULL) { - png_destroy_write_struct(&png_ptr, &info_ptr); - fclose(fp); - perror("malloc"); - return false; - } + } else { memset(padded_row_ptr, pad_byte, rowbytes); memcpy(padded_row_ptr, row_ptr, rowbytes - padding); png_write_row(png_ptr, padded_row_ptr); @@ -256,6 +254,8 @@ bool png_save(const char *filename, uint8_t *data, uint32_t width, uint32_t heig } } + free(padded_row_ptr); + // Signal we have finishing writing. png_write_end(png_ptr, NULL); diff --git a/png2bin.c b/png2bin.c index 5647283..33c76b1 100644 --- a/png2bin.c +++ b/png2bin.c @@ -27,11 +27,10 @@ #include "imgify.h" #include "common.h" +#include "common_options.h" +#include "version.h" #define PROGRAM "png2bin" -#define VERSION "1.0" - -#include "common_options.h" static void do_work(const options_t *options, void *data, size_t filesize) { uint8_t *buffer; @@ -49,6 +48,7 @@ static void do_work(const options_t *options, void *data, size_t filesize) { FILE *outf = fopen(options->output, "wb"); if (outf == NULL) { + free(buffer); perror("fopen"); exit(EXIT_FAILURE); } @@ -68,6 +68,7 @@ static void do_work(const options_t *options, void *data, size_t filesize) { } fclose(outf); + free(buffer); printf("Output file => %s\n size => %zu bytes\n", options->output, fsize(options->output)); } diff --git a/version.h b/version.h new file mode 100644 index 0000000..7b98d73 --- /dev/null +++ b/version.h @@ -0,0 +1 @@ +#define VERSION "1.0.1" \ No newline at end of file