better support of gray level

This commit is contained in:
Tonton Th 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)
{
@ -40,14 +40,13 @@ if (FIMG_TYPE_GRAY != dst->type) {
nbb = src->width * src->height;
for (foo=0; foo<nbb; foo++) {
dst->R[foo] = ( (src->R[foo] * kr) +
(src->G[foo] * kg) +
(src->B[foo] * kb) ) /
kdiv;
}
return -1;
return 0;
}
/* --------------------------------------------------------------------- */

View File

@ -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;

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)
{
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<sz; idx++) {
if (fk > 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);