FloatImg4PythonBinding/lib/fimg-pnm.c

77 lines
1.4 KiB
C
Raw Normal View History

2019-03-03 16:22:55 +01:00
/*
* fimg-pnm.c
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "string.h"
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */
int fimg_save_as_pnm(FloatImg *head, char *fname, int notused)
{
FILE *fp;
float maximum, fk;
int idx, sz, Rv, Gv, Bv, foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head,
fname, notused);
#endif
if (head->type != 3) {
#if DEBUG_LEVEL
fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type);
#endif
return -1;
}
if (NULL==(fp=fopen(fname, "w"))) {
perror(fname);
return -1;
}
fprintf(fp, "P3\n%d %d\n", 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;
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++;
}
}
fputs("\n", fp);
fclose(fp);
return 0;
}
/* ---------------------------------------------------------------- */