reading PNG with pnglite is wtf

This commit is contained in:
le vieux 2020-11-09 14:27:28 +01:00
parent 0481c39c5e
commit b73c25f470
4 changed files with 58 additions and 38 deletions

1
.gitignore vendored
View File

@ -32,6 +32,7 @@ funcs/V/*
funcs/*.gif funcs/*.gif
funcs/*.fits funcs/*.fits
funcs/*.tiff funcs/*.tiff
funcs/toto
scripts/*.fimg scripts/*.fimg
scripts/*.pnm scripts/*.pnm

View File

@ -2,6 +2,12 @@
Plein de fonctions qu'il serait bon de documenter :) 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 ## Contours
Détecter des contours est une activité respectable. Détecter des contours est une activité respectable.

View File

@ -1,6 +1,6 @@
/* /*
* Lecture des images PNG * Lecture des images PNG
* L'ecriture est en chantier :} * ----------------------
*/ */
#include <stdio.h> #include <stdio.h>
@ -13,29 +13,14 @@
extern int verbosity; 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 * warning : this func has been tested only with
* RGB (3x8bits) PNG files * 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) 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); foo = png_open_file_read(&png, filename);
if (PNG_NO_ERROR != foo) { if (PNG_NO_ERROR != foo) {
fprintf(stderr, "%s :\n\topen_file '%s' = %d %s\n", __func__, 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; return foo;
} }
#if DEBUG_LEVEL > 1 #if DEBUG_LEVEL > 1
fprintf(stderr, "%s opened\n", filename); fprintf(stderr, "%s opened\n", filename);
#endif #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 ) { if ( 3 != png.bpp ) {
/* I don't really understand this part of the code */
fprintf(stderr, "bpp format %d of '%s' not supported\n", fprintf(stderr, "bpp format %d of '%s' not supported\n",
png.color_type, filename); png.color_type, filename);
return -21; return -21;
} }
#if DEBUG_LEVEL datasize = png.width * png.height * png.bpp;
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
datas = malloc(datasize); datas = malloc(datasize);
if (NULL==datas) { if (NULL==datas) {
fprintf(stderr, "%s : fatal memory failure\n", __func__); fprintf(stderr, "%s : fatal memory failure\n", __func__);
@ -95,10 +76,12 @@ if (foo) {
exit(1); exit(1);
} }
/* I GET AN 'Unknown file error' HERE, WHY ???*/
foo = png_get_data(&png, datas); foo = png_get_data(&png, datas);
if (PNG_NO_ERROR != foo) { if (PNG_NO_ERROR != foo) {
fprintf(stderr, "error in '%s' :\n\tpng_get_data -> %d = %s\n", fprintf(stderr, "err in '%s:%s' :\n\tpng_get_data -> %d = %s\n\n",
__func__, foo, pngerr2str(foo)); __FILE__, __func__, foo, png_error_string(foo));
png_close_file(&png);
return foo; return foo;
} }

View File

@ -1,4 +1,5 @@
/* /*
* tests des fonctions diverses
*/ */
#include <stdio.h> #include <stdio.h>
@ -6,7 +7,8 @@
#include <string.h> #include <string.h>
#include <pam.h> #include <pam.h>
#undef DEBUG_LEVEL
#define DEBUG_LEVEL 1
#include "../floatimg.h" #include "../floatimg.h"
@ -48,6 +50,8 @@ return 0;
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
/* /*
* nouveau 7 octobre 2020 pendant sonoptic * nouveau 7 octobre 2020 pendant sonoptic
*
* inspiration: Olivier Baudu
*/ */
int essai_qsort_rgb(char *infile, char *outfile) int essai_qsort_rgb(char *infile, char *outfile)
@ -314,10 +318,8 @@ if (foo) {
return -44; return -44;
} }
fimg_filter_3x3(&src, &dst, &filter_a); fimg_filter_3x3(&src, &dst, &filter_a);
foo = fimg_clamp_negativ(&dst); foo = fimg_clamp_negativ(&dst);
if (foo) { if (foo) {
@ -539,6 +541,30 @@ fprintf(stderr, "make h deg -> %d\n", foo);
foo = fimg_save_as_pnm(&fimg, "vdeg.pnm", 0); foo = fimg_save_as_pnm(&fimg, "vdeg.pnm", 0);
fprintf(stderr, "%s: save as pnm -> %d\n", __func__, foo); 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; return 0;
} }
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
@ -595,7 +621,7 @@ return 0;
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
enum nCmd { Equalize=1, Rotate, Sfx0, F3x3, MIRE, Wfits, Wpng, Wtiff, enum nCmd { Equalize=1, Rotate, Sfx0, F3x3, MIRE, Wfits, Wpng, Wtiff,
Histo, Hsv, Classif, Ctr2x2, Qsortrgb, Histo, Hsv, Classif, Ctr2x2, Qsortrgb,
Displace }; Displace, ReadPNG };
typedef struct { typedef struct {
char *name; char *name;
int Cmd; int Cmd;
@ -616,6 +642,7 @@ Command commands[] = {
{ "ctr2x2", Ctr2x2 }, { "ctr2x2", Ctr2x2 },
{ "qsortrgb", Qsortrgb }, { "qsortrgb", Qsortrgb },
{ "displace", Displace }, { "displace", Displace },
{ "readpng", ReadPNG },
{ NULL, 0 } { NULL, 0 }
} ; } ;
@ -733,6 +760,9 @@ switch(opt) {
case Displace: case Displace:
foo = essai_displacement(filename, outfile); foo = essai_displacement(filename, outfile);
break; break;
case ReadPNG:
foo = essai_lecture_png(filename, outfile, 0);
break;
default: default:
fprintf(stderr, "%s : bad command\n", command); fprintf(stderr, "%s : bad command\n", command);
exit(1); exit(1);