better support of gray level

This commit is contained in:
tth 2019-08-26 02:35:17 +02:00
parent 20d122890b
commit fe0f90af64
3 changed files with 82 additions and 30 deletions

View File

@ -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) 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; nbb = src->width * src->height;
for (foo=0; foo<nbb; foo++) { for (foo=0; foo<nbb; foo++) {
dst->R[foo] = ( (src->R[foo] * kr) + dst->R[foo] = ( (src->R[foo] * kr) +
(src->G[foo] * kg) + (src->G[foo] * kg) +
(src->B[foo] * kb) ) / (src->B[foo] * kb) ) /
kdiv; kdiv;
} }
return -1; return 0;
} }
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */

View File

@ -20,7 +20,7 @@ float fimg_get_maxvalue(FloatImg *head)
float maxval; float maxval;
int foo; 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", fprintf(stderr, "%s : type %d invalide\n",
__func__, head->type); __func__, head->type);
return nanf("wtf ?"); return nanf("wtf ?");
@ -28,10 +28,17 @@ if (head->type != FIMG_TYPE_RGB) {
maxval = 0.0; /* no negative values allowed */ maxval = 0.0; /* no negative values allowed */
for (foo=0; foo<(head->width*head->height); foo++) { switch (head->type) {
if (head->R[foo] > maxval) maxval = head->R[foo]; case FIMG_TYPE_RGB:
if (head->G[foo] > maxval) maxval = head->G[foo]; for (foo=0; foo<(head->width*head->height); foo++) {
if (head->B[foo] > maxval) maxval = head->B[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; return maxval;

View File

@ -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<sz; idx++) {
if (fk > 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<sz; idx++) {
if (fk > 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) int fimg_save_as_pnm(FloatImg *head, char *fname, int notused)
{ {
FILE *fp; FILE *fp;
float maximum, fk; float maximum, fk;
int idx, sz, Rv, Gv, Bv, foo; char *code;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head, fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head,
fname, notused); fname, notused);
#endif #endif
if (head->type != FIMG_TYPE_RGB) { if ( head->type != FIMG_TYPE_RGB && head->type != FIMG_TYPE_GRAY) {
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type); fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type);
#endif #endif
@ -103,36 +153,32 @@ if (head->type != FIMG_TYPE_RGB) {
if (NULL==(fp=fopen(fname, "w"))) { if (NULL==(fp=fopen(fname, "w"))) {
perror(fname); 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); maximum = fimg_get_maxvalue(head);
fprintf(fp, "# maxval %15f\n", maximum); fprintf(fp, "# maxval %15f\n", maximum);
fk = maximum / 65536.0; fk = maximum / 65536.0;
fprintf(fp, "# divisor %15f\n", fk); fprintf(fp, "# divisor %15f\n", fk);
fprintf(fp, "65535\n"); fprintf(fp, "65535\n");
sz = head->width*head->height; fflush(fp);
foo = 0; switch(head->type) {
for (idx=0; idx<sz; idx++) { case FIMG_TYPE_GRAY:
if (fk > 0) { dump_gray_values(fp, head, fk);
Rv = (int)(head->R[idx] / fk); break;
Gv = (int)(head->G[idx] / fk); case FIMG_TYPE_RGB:
Bv = (int)(head->B[idx] / fk); dump_rgb_values(fp, head, fk);
} break;
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++;
}
} }
fputs("\n", fp); fputs("\n", fp);
fclose(fp); fclose(fp);