From 06fafcb6d698f5b891514924292589ddcfdaa796 Mon Sep 17 00:00:00 2001 From: Bill Kendrick Date: Sun, 3 Jul 2022 17:18:58 -0700 Subject: [PATCH] "test-png" tool to find PNGs that cause warnings New tool, "test-png", which exercises libpng library on a set of PNG image files, allowing us to find those which cause Tux Paint (via SDL_image, in turn via libpng) to report back warnings to STDERR. h/t Tim Dickson, per https://sourceforge.net/p/tuxpaint/bugs/252/ --- .gitignore | 1 + Makefile | 10 ++++- docs/CHANGES.txt | 6 +++ src/test-png.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/test-png.c diff --git a/.gitignore b/.gitignore index 4126c2677..dfd2f9673 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ TuxPaint.dmg src/po/POTFILES.in src/tuxpaint.desktop src/org.tuxpaint.Tuxpaint.appdata.xml +test-png diff --git a/Makefile b/Makefile index cd60a21d7..565befccc 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ # Various contributors (see AUTHORS.txt) # http://www.tuxpaint.org/ -# June 14, 2002 - June 29, 2022 +# June 14, 2002 - July 3, 2022 # The version number, for release: @@ -315,6 +315,9 @@ NOSVGFLAG:=$(if $(SVG_LIB),,-DNOSVG$(warning No SVG for you!)) OLDSVGFLAG:=$(if $(filter -lsvg-cairo,$(SVG_LIB)),-DOLD_SVG,) +PNG_CFLAGS:=$(shell $(PKG_CONFIG) libpng --cflags) + + ifeq ($(hack),1) hack: @echo 'SDL_PANGO_LIB is' $(SDL_PANGO_LIB) @@ -715,6 +718,7 @@ clean: @-rm -f TuxPaint.dmg temp.dmg; rm -rf magic/*.dSYM Resources @-rm -f dlllist a.exe @-rm -f win32/Preprocessed.iss win32/tuxpaint-*.zip win32/tuxpaint-*.exe + @-rm -f test-png @echo # "make uninstall" should remove the various parts from their @@ -1400,3 +1404,7 @@ $(MAGIC_SO): magic/%.$(SO_TYPE): magic/src/%.c .PHONY: magic-plugins magic-plugins: src/tp_magic_api.h $(MAGIC_SO) + +test-png: src/test-png.c + $(CC) $(PNG_CFLAGS) src/test-png.c -o test-png $(PNG) + diff --git a/docs/CHANGES.txt b/docs/CHANGES.txt index e44cf7667..c7ea34b2f 100644 --- a/docs/CHANGES.txt +++ b/docs/CHANGES.txt @@ -91,6 +91,12 @@ http://www.tuxpaint.org/ Terrence Sheflin Pere Pujal i Carabantes + * New tool, "test-png", which exercises libpng library on + a set of PNG image files, allowing us to find those which + cause Tux Paint (via SDL_image, in turn via libpng) to + report back warnings to STDERR. + h/t Tim Dickson, per https://sourceforge.net/p/tuxpaint/bugs/252/ + * Documentation updates: --------------------- * Update macOS build instructions for SDL2.0. diff --git a/src/test-png.c b/src/test-png.c new file mode 100644 index 000000000..8cc3c2bb3 --- /dev/null +++ b/src/test-png.c @@ -0,0 +1,96 @@ +/* + test-png.c + + A tiny test program that opens a PNG file using libpng + library. If any warnings come back, this can help us tell + when any images include oddities that would cause Tux Paint + to echo warnings (via SDL_image->libpng) to stderr when it + goes to load them. + + See https://sourceforge.net/p/tuxpaint/bugs/252/ + + Bill Kendrick + + (Based loosely on example code by Yoshimasa Niwa, https://gist.github.com/niw, + located at https://gist.github.com/niw/5963798, which itself was based on + code by Guillaume Cottenceau found at http://zarb.org/~gc/html/libpng.html) + + 2022-07-03 - 2022-07-03 +*/ + + +#include +#include +#include +#include +#include + +int main(int argc, char * argv[]) { + int i; + FILE * fi; + png_structp png; + png_infop info; + + /* Usage output */ + if (argc == 1 || strcmp(argv[1], "--help") == 0) { + fprintf(stderr, "Usage: %s file.png [file.png ...]\n", argv[0]); + exit(1); + } + + + /* Redirect stderr to stdout, so libpng's warnings can + be in the same stream as our's (where we report each + file's filename); allows more easily dumping to a file, + piping through `| less`, etc. */ + dup2(STDOUT_FILENO, STDERR_FILENO); + + + /* Open each PNG image!... */ + for (i = 1; i < argc; i++) { + printf("%5d ------------------------------------------------------------------\n", i); + printf("%s\n", argv[i]); + + /* Open the file */ + fi = fopen(argv[i], "rb"); + if (fi == NULL) { + printf("Cannot open\n"); + } else { + /* Prepare PNG library stuff... */ + png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!png) { + fprintf(stderr, "Cannot png_create_read_struct()!\n"); + exit(1); + } + + info = png_create_info_struct(png); + if (!info) { + fprintf(stderr, "Cannot png_create_info_struct()!\n"); + exit(1); + } + + if (setjmp(png_jmpbuf(png))) { + fprintf(stderr, "Cannot setjmp(png_jmpbuf(png)))!\n"); + exit(1); + } + + /* Read the PNG's info */ + png_init_io(png, fi); + png_read_info(png, info); + + /* *** At this point, libpng would have reported issues *** */ + + /* (We could do additional things, like reporting image + dimensions, color depth, etc., but I don't see any reason to + care right now. -bjk 2022.07.03) */ + + /* Close and move on to the next file */ + png_destroy_read_struct(&png, &info, NULL); + fclose(fi); + } + + printf("\n"); + fflush(stdout); + } + + return(0); +}