FloatImg/tools/fimg2text.c
2021-02-27 10:56:42 +01:00

127 lines
2.6 KiB
C

/*
* converting a floatimg to a machinable text file
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "../floatimg.h"
int verbosity;
/* ------------------------------------------------------------------------- */
int export_as_machinable(FloatImg *src, char *fname, int steps, int flags)
{
FILE *fp;
int x, y, idx;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %25s ( %p '%s' %d )\n", __func__,
src, fname, flags);
#endif
fp = NULL; /* molly guard */
if (strcmp("-", fname)) { /* real file */
fprintf(stderr, "real file\n");
}
else {
fprintf(stderr, "kitchen sink\n");
}
idx = 0;
fp = stdout; /* XXX */
for (y=0; y<src->height; y+=steps) {
for (x=0; x<src->width; x+=steps) {
fprintf(fp, "%d %d ", x, y);
fprintf(fp, " %f %f %f\n",
src->R[idx], src->G[idx], src->B[idx]);
idx++;
}
}
return 0;
}
/* ------------------------------------------------------------------------- */
int convertir_fimg_en_machinable(char *srcname, char *dstname, int steps)
{
int foo, infos[3];
FloatImg fimg;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %25s ( '%s' '%s' %d )\n", __func__,
srcname, dstname, notused);
#endif
foo = fimg_fileinfos(srcname, infos);
if (foo) {
fprintf(stderr, "'%s' get dims -> %d\n", srcname, foo);
return foo;
}
if (verbosity) {
fprintf(stderr, "%s: image '%s' is %d x %d %s\n",
__func__,
srcname, infos[0], infos[1],
fimg_str_type(infos[2]));
}
foo = fimg_create_from_dump(srcname, &fimg);
if (foo) {
fprintf(stderr, "create fimg from '%s' -> %d\n", srcname, foo);
return -1;
}
if (verbosity) {
fimg_describe(&fimg, srcname);
}
foo = export_as_machinable(&fimg, dstname, steps, 0);
fimg_destroy(&fimg);
return 0;
}
/* ------------------------------------------------------------------------- */
void help(int k)
{
puts("usage:\n\tfimg2text [options] foo.fimg > bar.csv");
puts("options:");
puts("\t-v\tincrease verbosity");
if (verbosity) fimg_print_version(1);
exit(0);
}
/* ------------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int foo, opt;
int steps = 16;
while ((opt = getopt(argc, argv, "hs:v")) != -1) {
switch(opt) {
case 'v': verbosity++; break;
case 'h': help(1); exit(1);
case 's': steps = atoi(optarg); break;
}
}
if (1 != argc-optind) {
fprintf(stderr, "error: %s need two filenames\n", argv[0]);
exit(1);
}
foo = convertir_fimg_en_machinable(argv[optind], "-", steps);
if (foo)
fprintf(stderr, "%s : got a %d from convertor\n", argv[0], foo);
return 0;
}
/* ------------------------------------------------------------------------- */