From 6ffc08188d1eaf081b7898e6b85efb099311ba41 Mon Sep 17 00:00:00 2001 From: tth Date: Wed, 9 Feb 2022 23:21:58 +0100 Subject: [PATCH] work on the real PGM export --- .gitignore | 1 + floatimg.h | 4 +++- funcs/exporter.c | 4 ++++ funcs/utils.c | 1 + lib/fimg-pnm.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ lib/t.c | 18 ++++++++++++----- tools/cumulfimgs.c | 16 +++++++++++++--- 7 files changed, 83 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index b8315fe9..deb40398 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ gmon.out *.swp *.pnm +*.pgm *.fimg essai MANIFEST diff --git a/floatimg.h b/floatimg.h index 7e7ea51c..b54ea05d 100644 --- a/floatimg.h +++ b/floatimg.h @@ -4,7 +4,7 @@ * http://la.buvette.org/photos/cumul */ -#define FIMG_VERSION 167 +#define FIMG_VERSION 169 /* * in memory descriptor @@ -54,6 +54,7 @@ typedef struct { #define FILE_TYPE_BMP 7 #define FILE_TYPE_EXR 8 #define FILE_TYPE_DICOM 9 +#define FILE_TYPE_PGM 10 /* lib/contrast.c */ #define CONTRAST_NONE 0 @@ -151,6 +152,7 @@ int fimg_export_picture(FloatImg *pic, char *fname, int flags); /* PNM files module */ int fimg_save_as_pnm(FloatImg *head, char *fname, int flags); +int fimg_save_as_pgm(FloatImg *head, char *fname, int flags); int fimg_load_from_pnm(char *fname, FloatImg *head, int notused); double fimg_timer_set(int whot); diff --git a/funcs/exporter.c b/funcs/exporter.c index 8d732fcb..30d233e3 100644 --- a/funcs/exporter.c +++ b/funcs/exporter.c @@ -59,6 +59,10 @@ switch(filetype) { fprintf(stderr, "%s: file type EXR experimental\n", __func__); foo = fimg_save_as_exr(pic, fname, 0); break; + case FILE_TYPE_PGM: + fprintf(stderr, "XXX %s EXPERIMENT!\n", __func__); + foo = fimg_save_as_pgm(pic, fname, 0); + break; default: foo = -1789; break; diff --git a/funcs/utils.c b/funcs/utils.c index 62d639ab..6ba0e81e 100644 --- a/funcs/utils.c +++ b/funcs/utils.c @@ -100,6 +100,7 @@ if (!strcasecmp(name, "tif" )) return FILE_TYPE_TIFF; if (!strcasecmp(name, "fits")) return FILE_TYPE_FITS; if (!strcasecmp(name, "exr")) return FILE_TYPE_EXR; if (!strcasecmp(name, "dicom")) return FILE_TYPE_EXR; +if (!strcasecmp(name, "pgm" )) return FILE_TYPE_PGM; return -1; } diff --git a/lib/fimg-pnm.c b/lib/fimg-pnm.c index 88e0e707..02dba782 100644 --- a/lib/fimg-pnm.c +++ b/lib/fimg-pnm.c @@ -196,3 +196,51 @@ fputs("\n", fp); fclose(fp); return 0; } /* ---------------------------------------------------------------- */ + +/* nouveau 10 fevrier 2022 */ + + +int fimg_save_as_pgm(FloatImg *src, char *fname, int flags) +{ +FILE *fp; +float maximum, fk; +int area, idx, printed; +float accu; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %s %d )\n", __func__, src, fname, flags); +#endif + +if ( src->type != FIMG_TYPE_RGB ) { +#if DEBUG_LEVEL + fprintf(stderr, "%s : type %d is bad.\n", __func__, src->type); +#endif + return -1; + } + +if (NULL==(fp=fopen(fname, "w"))) { + perror(fname); + return -2; + } + +fprintf(fp, "P2\n%d\n%d\n65532\n\n", src->width, src->height); + +area = src->width * src->height; +maximum = fimg_get_maxvalue(src); +fk = maximum / 65535.0; + +printed = 0; +for (idx=0; idxR[idx] + src->G[idx] + src->B[idx]) / 3.0; + printed += fprintf(fp, "%d ", (int)(accu/fk)); + if (printed > 72) { + fputs("\n", fp); + printed = 0; + } + } + +fclose(fp); + +return 0; +} + diff --git a/lib/t.c b/lib/t.c index b55c50fa..562c8537 100644 --- a/lib/t.c +++ b/lib/t.c @@ -83,12 +83,13 @@ return 0; } /* ---------------------------------------------------------------- */ -int essai_2gray(FloatImg *picz, char *outname) +int essai_2gray(FloatImg *picz, char *basename) { int foo; FloatImg gray; +char outname[200]; -fprintf(stderr, ">>> %s ( %p '%s' )\n", __func__, picz, outname); +fprintf(stderr, ">>> %s ( %p '%s' )\n", __func__, picz, basename); foo = fimg_create(&gray, picz->width, picz->height, FIMG_TYPE_GRAY); if (foo) { @@ -101,9 +102,16 @@ if (foo) { exit(1); } +strcpy(outname,basename); strcat(outname, ".pnm"); foo = fimg_save_as_pnm(&gray, outname, 0); if (foo) { - fprintf(stderr, "%s : err %d on save_as_pnm\n", __func__, foo); + fprintf(stderr, "%s : err %d on save_as_PNM\n", __func__, foo); + exit(1); + } +strcpy(outname,basename); strcat(outname, ".pgm"); +foo = fimg_save_as_pgm(&gray, outname, 0); +if (foo) { + fprintf(stderr, "%s : err %d on save_as_PGM\n", __func__, foo); exit(1); } @@ -261,8 +269,8 @@ if (verbosity) { fimg_print_sizeof(); } -foo = essai_contraste("quux.fimg"); -fprintf(stderr, "retour essai contraste -> %d\n", foo); +foo = essai_2gray(NULL, "quux"); +fprintf(stderr, "retour essai -> %d\n", foo); // foo = essai_clone_et_copy(0); // fprintf(stderr, "retour essai clone'n'copy -> %d\n", foo); diff --git a/tools/cumulfimgs.c b/tools/cumulfimgs.c index a1b7a756..8304cfa1 100644 --- a/tools/cumulfimgs.c +++ b/tools/cumulfimgs.c @@ -43,7 +43,7 @@ puts("\t-v\tincrease verbosity"); puts("\t-o\tname of output file"); puts("\t-g\tconvert to gray level"); puts(""); -if (verbosity) { puts(""); fimg_print_version(1); } +if (verbosity) { puts("Xperiment"); fimg_print_version(1); } exit(0); } /* --------------------------------------------------------------------- */ @@ -54,6 +54,7 @@ int opt; int compte = 0; int to_gray = 0; +int experiment = 0; char *output_file = "out.fimg"; FloatImg accu, temp; int src_loaded = 0; @@ -61,12 +62,13 @@ float vals[6]; g_width = g_height = 0; -while ((opt = getopt(argc, argv, "gho:v")) != -1) { +while ((opt = getopt(argc, argv, "gho:vx")) != -1) { switch(opt) { case 'g': to_gray = 1; break; case 'h': help(0); break; case 'o': output_file = optarg; break; case 'v': verbosity++; break; + case 'x': experiment++; break; } } @@ -101,6 +103,13 @@ for (idx=optind; idx