From ffb95d9a143ad17268f7d6d75f56ec2907205c86 Mon Sep 17 00:00:00 2001 From: Joe DeCapo Date: Sun, 7 May 2023 15:23:02 -0500 Subject: [PATCH] +libspng --- projects/libspng.org/example.c | 124 +++++++++++++++++++++++ projects/libspng.org/expected_output.txt | 7 ++ projects/libspng.org/package.yml | 31 ++++++ projects/libspng.org/test.png | Bin 0 -> 82 bytes 4 files changed, 162 insertions(+) create mode 100644 projects/libspng.org/example.c create mode 100644 projects/libspng.org/expected_output.txt create mode 100644 projects/libspng.org/package.yml create mode 100644 projects/libspng.org/test.png diff --git a/projects/libspng.org/example.c b/projects/libspng.org/example.c new file mode 100644 index 00000000..32a4c453 --- /dev/null +++ b/projects/libspng.org/example.c @@ -0,0 +1,124 @@ +#include + +#include +#include + +int main(int argc, char **argv) +{ + int r = 0; + FILE *png; + char *pngbuf = NULL; + spng_ctx *ctx = NULL; + unsigned char *out = NULL; + + if(argc < 2) + { + printf("no input file\n"); + goto err; + } + + png = fopen(argv[1], "rb"); + if(png == NULL) + { + printf("error opening input file %s\n", argv[1]); + goto err; + } + + fseek(png, 0, SEEK_END); + + long siz_pngbuf = ftell(png); + rewind(png); + + if(siz_pngbuf < 1) goto err; + + pngbuf = malloc(siz_pngbuf); + if(pngbuf == NULL) + { + printf("malloc() failed\n"); + goto err; + } + + if(fread(pngbuf, siz_pngbuf, 1, png) != 1) + { + printf("fread() failed\n"); + goto err; + } + + ctx = spng_ctx_new(0); + if(ctx == NULL) + { + printf("spng_ctx_new() failed\n"); + goto err; + } + + r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE); + + if(r) + { + printf("spng_set_crc_action() error: %s\n", spng_strerror(r)); + goto err; + } + + r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf); + + if(r) + { + printf("spng_set_png_buffer() error: %s\n", spng_strerror(r)); + goto err; + } + + struct spng_ihdr ihdr; + r = spng_get_ihdr(ctx, &ihdr); + + if(r) + { + printf("spng_get_ihdr() error: %s\n", spng_strerror(r)); + goto err; + } + + char *clr_type_str; + if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE) + clr_type_str = "grayscale"; + else if(ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR) + clr_type_str = "truecolor"; + else if(ihdr.color_type == SPNG_COLOR_TYPE_INDEXED) + clr_type_str = "indexed color"; + else if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA) + clr_type_str = "grayscale with alpha"; + else + clr_type_str = "truecolor with alpha"; + + printf("width: %" PRIu32 "\nheight: %" PRIu32 "\n" + "bit depth: %" PRIu8 "\ncolor type: %" PRIu8 " - %s\n", + ihdr.width, ihdr.height, + ihdr.bit_depth, ihdr.color_type, clr_type_str); + printf("compression method: %" PRIu8 "\nfilter method: %" PRIu8 "\n" + "interlace method: %" PRIu8 "\n", + ihdr.compression_method, ihdr.filter_method, + ihdr.interlace_method); + + size_t out_size; + + r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size); + + if(r) goto err; + + out = malloc(out_size); + if(out == NULL) goto err; + + r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0); + + if(r) + { + printf("spng_decode_image() error: %s\n", spng_strerror(r)); + goto err; + } + +err: + spng_ctx_free(ctx); + + free(out); + free(pngbuf); + + return r; +} diff --git a/projects/libspng.org/expected_output.txt b/projects/libspng.org/expected_output.txt new file mode 100644 index 00000000..ea04929d --- /dev/null +++ b/projects/libspng.org/expected_output.txt @@ -0,0 +1,7 @@ +width: 8 +height: 8 +bit depth: 1 +color type: 3 - indexed color +compression method: 0 +filter method: 0 +interlace method: 0 diff --git a/projects/libspng.org/package.yml b/projects/libspng.org/package.yml new file mode 100644 index 00000000..327358ba --- /dev/null +++ b/projects/libspng.org/package.yml @@ -0,0 +1,31 @@ +distributable: + url: https://github.com/randy408/libspng/archive/refs/tags/v{{version}}.tar.gz + strip-components: 1 + +versions: + gitlab: randy408/libspng + +dependencies: + darwin: + zlib.net: '*' + +build: + dependencies: + tea.xyz/gx/cc: c99 + ninja-build.org: 1 + freedesktop.org/pkg-config: '*' + mesonbuild.com: '*' + working-directory: build + script: | + meson .. --prefix={{prefix}} --libdir={{prefix}}/lib --buildtype=release + ninja -v + ninja install -v + +test: + dependencies: + tea.xyz/gx/cc: c99 + script: | + cc example.c -lspng -o example + actual_output="$(./example 'test.png')" + echo "$actual_output" > actual_output.txt + test "$(cat actual_output.txt)" = "$(cat expected_output.txt)" diff --git a/projects/libspng.org/test.png b/projects/libspng.org/test.png new file mode 100644 index 0000000000000000000000000000000000000000..18d7a15a9b6204ab5ba01cc1855ae91624fcb1a4 GIT binary patch literal 82 zcmeAS@N?(olHy`uVBq!ia0vp^93afd3?%;@)Hwm9m;-!5Tp1YtcU4>p2XeVQT^vI= cWRo3$N(2}fXF7A91d1|vy85}Sb4q9e0Bvv(uK)l5 literal 0 HcmV?d00001