work on the real PGM export

This commit is contained in:
tth
2022-02-09 23:21:58 +01:00
parent 0da81df892
commit 6ffc08188d
7 changed files with 83 additions and 9 deletions

View File

@@ -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; idx<area; idx++) {
accu = (src->R[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;
}

18
lib/t.c
View File

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