Skip to content

Commit

Permalink
Merge pull request #2 from jweyrich/v1.0.0-fixes
Browse files Browse the repository at this point in the history
v1.0.0 fixes
  • Loading branch information
jweyrich committed Sep 3, 2023
2 parents 9511c15 + b63a56a commit 66e54e0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions bin2png.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 12 additions & 12 deletions imgify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -236,26 +244,18 @@ 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);
free(padded_row_ptr);
}
}

free(padded_row_ptr);

// Signal we have finishing writing.
png_write_end(png_ptr, NULL);

Expand Down
7 changes: 4 additions & 3 deletions png2bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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));
}
Expand Down
1 change: 1 addition & 0 deletions version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define VERSION "1.0.1"

0 comments on commit 66e54e0

Please sign in to comment.