fimg2pnm can now do on-the-fly to-gray conversion

This commit is contained in:
tth 2019-09-10 15:38:24 +02:00
parent ffc99e6114
commit 3a141c5a17
1 changed files with 33 additions and 7 deletions

View File

@ -11,13 +11,14 @@
int verbosity; int verbosity;
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
int convertir_fimg_en_pnm(char *srcname, char *dstname, int notused) int convertir_fimg_en_pnm(char *srcname, char *dstname, int to_gray)
{ {
int foo, infos[3]; int foo, infos[3];
FloatImg fimg; FloatImg fimg, gris, *outptr;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, ">>> %25s ( '%s' '%s' )\n", __func__, srcname, dstname); fprintf(stderr, ">>> %25s ( '%s' '%s' %d )\n", __func__,
srcname, dstname, to_gray);
#endif #endif
foo = fimg_fileinfos(srcname, infos); foo = fimg_fileinfos(srcname, infos);
@ -35,13 +36,36 @@ if (foo) {
return -1; return -1;
} }
outptr = &fimg; /* safe default value */
if (to_gray) {
puts("converting to gray...");
foo = fimg_create(&gris, fimg.width, fimg.height, FIMG_TYPE_GRAY);
if (foo) {
fprintf(stderr, "err create gray %d\n", foo);
return -2;
}
foo = fimg_mk_gray_from(&fimg, &gris, 0);
if (foo) {
fprintf(stderr, "err mk gray %d\n", foo);
return -4;
}
outptr = &gris;
}
#if DEBUG_LEVEL > 1 #if DEBUG_LEVEL > 1
print_floatimg(&fimg, "created fimg"); print_floatimg(outptr, "created fimg");
#endif #endif
foo = fimg_save_as_pnm(&fimg, dstname, 0); foo = fimg_save_as_pnm(outptr, dstname, 0);
if(foo) { fprintf(stderr, "%p to '%s' -> %d\n", &fimg, dstname, foo); } if(foo) { fprintf(stderr, "%p to '%s' -> %d\n", &fimg, dstname, foo); }
if (to_gray) {
fimg_destroy(&gris);
outptr = NULL;
/* please run valgrind every hour */
}
return 0; return 0;
} }
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
@ -58,14 +82,16 @@ puts("\tfimg2pnm [flags] infile.fimg outfile.pnm");
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int foo, opt; int foo, opt;
int to_gray = 0;
if (argc == 1) { if (argc == 1) {
help(0); help(0);
exit(0); exit(0);
} }
while ((opt = getopt(argc, argv, "v")) != -1) { while ((opt = getopt(argc, argv, "ghv")) != -1) {
switch(opt) { switch(opt) {
case 'g': to_gray = 1;
case 'v': verbosity++; break; case 'v': verbosity++; break;
case 'h': help(1); exit(1); case 'h': help(1); exit(1);
} }
@ -89,7 +115,7 @@ if ( 0 != access(argv[optind], R_OK) ) { /* fimg is NOT readable */
exit(2); exit(2);
} }
foo = convertir_fimg_en_pnm(argv[optind], argv[optind+1], 0); foo = convertir_fimg_en_pnm(argv[optind], argv[optind+1], to_gray);
if (foo) fprintf(stderr, "conversion -> %d\n", foo); if (foo) fprintf(stderr, "conversion -> %d\n", foo);
return 0; return 0;