From fe0f90af64fedbc82c1bf51afd29f30f65661790 Mon Sep 17 00:00:00 2001 From: tth Date: Mon, 26 Aug 2019 02:35:17 +0200 Subject: [PATCH] better support of gray level --- lib/fimg-2gray.c | 5 ++- lib/fimg-math.c | 17 ++++++--- lib/fimg-pnm.c | 90 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 82 insertions(+), 30 deletions(-) diff --git a/lib/fimg-2gray.c b/lib/fimg-2gray.c index 49414cf5..7e21ca5f 100644 --- a/lib/fimg-2gray.c +++ b/lib/fimg-2gray.c @@ -13,7 +13,7 @@ extern int verbosity; /* must be declared around main() */ /* --------------------------------------------------------------------- */ /* - * floating img MUST be allocated. + * floating imgs MUST be allocated before calling this func. */ int fimg_mk_gray_from(FloatImg *src, FloatImg*dst, int k) { @@ -40,14 +40,13 @@ if (FIMG_TYPE_GRAY != dst->type) { nbb = src->width * src->height; for (foo=0; fooR[foo] = ( (src->R[foo] * kr) + (src->G[foo] * kg) + (src->B[foo] * kb) ) / kdiv; } -return -1; +return 0; } /* --------------------------------------------------------------------- */ diff --git a/lib/fimg-math.c b/lib/fimg-math.c index 2f4be78e..4647a25b 100644 --- a/lib/fimg-math.c +++ b/lib/fimg-math.c @@ -20,7 +20,7 @@ float fimg_get_maxvalue(FloatImg *head) float maxval; int foo; -if (head->type != FIMG_TYPE_RGB) { +if (head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) { fprintf(stderr, "%s : type %d invalide\n", __func__, head->type); return nanf("wtf ?"); @@ -28,10 +28,17 @@ if (head->type != FIMG_TYPE_RGB) { maxval = 0.0; /* no negative values allowed */ -for (foo=0; foo<(head->width*head->height); foo++) { - if (head->R[foo] > maxval) maxval = head->R[foo]; - if (head->G[foo] > maxval) maxval = head->G[foo]; - if (head->B[foo] > maxval) maxval = head->B[foo]; +switch (head->type) { + case FIMG_TYPE_RGB: + for (foo=0; foo<(head->width*head->height); foo++) { + if (head->R[foo] > maxval) maxval = head->R[foo]; + if (head->G[foo] > maxval) maxval = head->G[foo]; + if (head->B[foo] > maxval) maxval = head->B[foo]; + } + case FIMG_TYPE_GRAY: + for (foo=0; foo<(head->width*head->height); foo++) { + if (head->R[foo] > maxval) maxval = head->R[foo]; + } } return maxval; diff --git a/lib/fimg-pnm.c b/lib/fimg-pnm.c index c36ce699..b642ef87 100644 --- a/lib/fimg-pnm.c +++ b/lib/fimg-pnm.c @@ -82,19 +82,69 @@ return 0; } /* ---------------------------------------------------------------- */ +static void dump_gray_values(FILE *fp, FloatImg *picz, float fk) +{ +int cnt, sz, value; +int idx; +cnt = 0; +sz = picz->width * picz->height; +for (idx=0; idx 0) value = (int)(picz->R[idx] / fk); + else value = 0; + cnt += fprintf(fp, "%d", value); + if (cnt > 70) { + fputs("\n", fp); cnt = 0; + } + else { + fputs(" ", fp); cnt++; + } + } +fputs("\n", fp); +} +/* ---------------------------------------------------------------- */ +static void dump_rgb_values(FILE *fp, FloatImg *picz, float fk) +{ +int cnt, sz, idx; +int Rv, Gv, Bv; + +cnt = 0; +sz = picz->width * picz->height; +for (idx=0; idx 0) { + Rv = (int)(picz->R[idx] / fk); + Gv = (int)(picz->G[idx] / fk); + Bv = (int)(picz->B[idx] / fk); + } + else { + Rv = Gv = Bv = 0; + } + cnt += fprintf(fp, "%d %d %d", Rv, Gv, Bv); + if (cnt > 60) { + fputs("\n", fp); cnt = 0; + } + else { + fputs(" ", fp); cnt++; + } + } +fputs("\n", fp); +} +/* ---------------------------------------------------------------- */ +/* + * + */ int fimg_save_as_pnm(FloatImg *head, char *fname, int notused) { FILE *fp; float maximum, fk; -int idx, sz, Rv, Gv, Bv, foo; +char *code; #if DEBUG_LEVEL fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head, fname, notused); #endif -if (head->type != FIMG_TYPE_RGB) { +if ( head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) { #if DEBUG_LEVEL fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type); #endif @@ -103,36 +153,32 @@ if (head->type != FIMG_TYPE_RGB) { if (NULL==(fp=fopen(fname, "w"))) { perror(fname); - return -1; + return -2; } -fprintf(fp, "P3\n%d %d\n", head->width, head->height); +switch(head->type) { + case FIMG_TYPE_GRAY: code = "P2"; break; + case FIMG_TYPE_RGB: code = "P3"; break; + } + +fprintf(fp, "%s\n%d %d\n", code, head->width, head->height); maximum = fimg_get_maxvalue(head); fprintf(fp, "# maxval %15f\n", maximum); fk = maximum / 65536.0; fprintf(fp, "# divisor %15f\n", fk); fprintf(fp, "65535\n"); -sz = head->width*head->height; +fflush(fp); -foo = 0; -for (idx=0; idx 0) { - Rv = (int)(head->R[idx] / fk); - Gv = (int)(head->G[idx] / fk); - Bv = (int)(head->B[idx] / fk); - } - else { - Rv = Gv = Bv = 0; - } - foo += fprintf(fp, "%d %d %d", Rv, Gv, Bv); - if (foo > 60) { - fputs("\n", fp); foo = 0; - } - else { - fputs(" ", fp); foo++; - } +switch(head->type) { + case FIMG_TYPE_GRAY: + dump_gray_values(fp, head, fk); + break; + case FIMG_TYPE_RGB: + dump_rgb_values(fp, head, fk); + break; } + fputs("\n", fp); fclose(fp);