From b73c25f470c7059141c2901df9120929d3332d86 Mon Sep 17 00:00:00 2001 From: le vieux Date: Mon, 9 Nov 2020 14:27:28 +0100 Subject: [PATCH] reading PNG with pnglite is wtf --- .gitignore | 1 + funcs/README.md | 6 ++++++ funcs/fimg-png.c | 51 ++++++++++++++++-------------------------------- funcs/t.c | 38 ++++++++++++++++++++++++++++++++---- 4 files changed, 58 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index a95a764e..ac86982e 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ funcs/V/* funcs/*.gif funcs/*.fits funcs/*.tiff +funcs/toto scripts/*.fimg scripts/*.pnm diff --git a/funcs/README.md b/funcs/README.md index 65b2f6eb..530ceccd 100644 --- a/funcs/README.md +++ b/funcs/README.md @@ -2,6 +2,12 @@ Plein de fonctions qu'il serait bon de documenter :) +## PNG + +__Attention__ : la bibliothèque `pnglite`actuellement utiilsée pour lire les +fichiers PNG n'accepte que **certains** types de fichiers. +Et en particulier, elle brotche sur ceux produits par ImageMagick ! + ## Contours Détecter des contours est une activité respectable. diff --git a/funcs/fimg-png.c b/funcs/fimg-png.c index 072a0814..622eb6c1 100644 --- a/funcs/fimg-png.c +++ b/funcs/fimg-png.c @@ -1,6 +1,6 @@ /* * Lecture des images PNG - * L'ecriture est en chantier :} + * ---------------------- */ #include @@ -13,29 +13,14 @@ extern int verbosity; -/* --------------------------------------------------------------------- */ -static char *pngerr2str(int code) -{ -switch (code) { - case 1: return "Done"; - case 0: return "No error"; - case -1: return "File error"; - case -2: return "Header error"; - case -3: return "IO error"; - case -4: return "EOF error"; - case -5: return "CRC error"; - case -6: return "Memory error"; - case -7: return "Zlib error"; - case -8: return "Unknow filter"; - case -9: return "Not supported"; - case -10: return "Wrong arguments"; - } -return "*unknow*"; -} /* --------------------------------------------------------------------- */ /* * warning : this func has been tested only with * RGB (3x8bits) PNG files + * + * Attention : certains fichiers, tels que ceux + * produits par ImageMagick ne sont pas lisibles + * par cette fonction :( */ int fimg_create_from_png(char *filename, FloatImg *fimg) { @@ -57,31 +42,27 @@ png_init(NULL, NULL); /* this is VITAL ! */ foo = png_open_file_read(&png, filename); if (PNG_NO_ERROR != foo) { fprintf(stderr, "%s :\n\topen_file '%s' = %d %s\n", __func__, - filename, foo, pngerr2str(foo)); + filename, foo, png_error_string(foo)); + png_close_file(&png); return foo; } #if DEBUG_LEVEL > 1 fprintf(stderr, "%s opened\n", filename); #endif -datasize = png.width * png.height * png.bpp; +if (verbosity) { + printf("----------- %s ---------\n", filename); + png_print_info(&png); puts(""); + fflush(stdout); + } if ( 3 != png.bpp ) { - /* I don't really understand this part of the code */ fprintf(stderr, "bpp format %d of '%s' not supported\n", png.color_type, filename); return -21; } -#if DEBUG_LEVEL -printf("\t%s is %d x %d\n", filename, png.width, png.height); -printf("\tdatalen %d\n", png.png_datalen); -printf("\tcolor type %d\n", png.color_type); -printf("\tbyte/pixel %d\n", png.bpp); -printf("\tdatasize %d\n", datasize); -puts(""); png_print_info(&png); puts(""); -#endif - +datasize = png.width * png.height * png.bpp; datas = malloc(datasize); if (NULL==datas) { fprintf(stderr, "%s : fatal memory failure\n", __func__); @@ -95,10 +76,12 @@ if (foo) { exit(1); } +/* I GET AN 'Unknown file error' HERE, WHY ???*/ foo = png_get_data(&png, datas); if (PNG_NO_ERROR != foo) { - fprintf(stderr, "error in '%s' :\n\tpng_get_data -> %d = %s\n", - __func__, foo, pngerr2str(foo)); + fprintf(stderr, "err in '%s:%s' :\n\tpng_get_data -> %d = %s\n\n", + __FILE__, __func__, foo, png_error_string(foo)); + png_close_file(&png); return foo; } diff --git a/funcs/t.c b/funcs/t.c index 758ddd7b..d76487e6 100644 --- a/funcs/t.c +++ b/funcs/t.c @@ -1,4 +1,5 @@ /* + * tests des fonctions diverses */ #include @@ -6,7 +7,8 @@ #include #include - +#undef DEBUG_LEVEL +#define DEBUG_LEVEL 1 #include "../floatimg.h" @@ -48,6 +50,8 @@ return 0; /* --------------------------------------------------------------------- */ /* * nouveau 7 octobre 2020 pendant sonoptic + * + * inspiration: Olivier Baudu */ int essai_qsort_rgb(char *infile, char *outfile) @@ -314,10 +318,8 @@ if (foo) { return -44; } - fimg_filter_3x3(&src, &dst, &filter_a); - foo = fimg_clamp_negativ(&dst); if (foo) { @@ -539,6 +541,30 @@ fprintf(stderr, "make h deg -> %d\n", foo); foo = fimg_save_as_pnm(&fimg, "vdeg.pnm", 0); fprintf(stderr, "%s: save as pnm -> %d\n", __func__, foo); +return 0; +} +/* --------------------------------------------------------------------- */ +int essai_lecture_png(char *fname, char *outfile, int notused) +{ +FloatImg fimg; +int foo; + +fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, fname, notused); + +memset(&fimg, 0, sizeof(FloatImg)); +foo = fimg_create_from_png(fname, &fimg); +if (foo) { + fprintf(stderr, "%s: createfrom -> %d\n", __func__, foo); + return foo; + } +fimg_describe(&fimg, "created from png"); + +foo = fimg_export_picture(&fimg, outfile, 0); +if (foo) { + fprintf(stderr, "%s : err %d saving result to %s\n", __func__, + foo, outfile); + return foo; + } return 0; } /* --------------------------------------------------------------------- */ @@ -595,7 +621,7 @@ return 0; /* --------------------------------------------------------------------- */ enum nCmd { Equalize=1, Rotate, Sfx0, F3x3, MIRE, Wfits, Wpng, Wtiff, Histo, Hsv, Classif, Ctr2x2, Qsortrgb, - Displace }; + Displace, ReadPNG }; typedef struct { char *name; int Cmd; @@ -616,6 +642,7 @@ Command commands[] = { { "ctr2x2", Ctr2x2 }, { "qsortrgb", Qsortrgb }, { "displace", Displace }, + { "readpng", ReadPNG }, { NULL, 0 } } ; @@ -733,6 +760,9 @@ switch(opt) { case Displace: foo = essai_displacement(filename, outfile); break; + case ReadPNG: + foo = essai_lecture_png(filename, outfile, 0); + break; default: fprintf(stderr, "%s : bad command\n", command); exit(1);