FloatImg4PythonBinding/tools/fimg2text.c

164 lines
3.6 KiB
C
Raw Normal View History

2021-02-27 08:32:42 +11:00
/*
* converting a floatimg to a machinable text file
2021-02-27 22:52:09 +11:00
* an ugly software from tTh - february 2021
2021-02-27 08:32:42 +11:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "../floatimg.h"
int verbosity;
/* ------------------------------------------------------------------------- */
2021-02-27 20:56:42 +11:00
int export_as_machinable(FloatImg *src, char *fname, int steps, int flags)
2021-02-27 08:32:42 +11:00
{
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 */
2021-02-27 20:56:42 +11:00
for (y=0; y<src->height; y+=steps) {
for (x=0; x<src->width; x+=steps) {
2021-02-27 08:32:42 +11:00
2021-02-27 20:56:42 +11:00
fprintf(fp, "%d %d ", x, y);
fprintf(fp, " %f %f %f\n",
src->R[idx], src->G[idx], src->B[idx]);
2021-02-27 08:32:42 +11:00
idx++;
}
}
return 0;
}
/* ------------------------------------------------------------------------- */
2021-02-27 22:52:09 +11:00
static int normalize(FloatImg *pimg, float vmax)
{
float mmv[6], maxi, coef;
int foo, sz, idx;
2021-02-28 09:35:27 +11:00
#if DEBUG_LEVEL
2021-02-27 22:52:09 +11:00
fprintf(stderr, ">>> %s ( %p %g )\n", __func__, pimg, vmax);
2021-02-28 09:35:27 +11:00
#endif
2021-02-27 22:52:09 +11:00
foo = fimg_get_minmax_rgb(pimg, mmv);
2021-02-28 09:35:27 +11:00
// fimg_print_minmax(mmv, "norm it ?");
2021-02-27 22:52:09 +11:00
maxi = mmv[1];
if (mmv[3] > maxi) maxi = mmv[3];
if (mmv[5] > maxi) maxi = mmv[5];
2021-02-28 09:35:27 +11:00
// fprintf(stderr, "maximum = %f\n", maxi);
2021-02-27 22:52:09 +11:00
coef = vmax / maxi;
2021-02-28 09:35:27 +11:00
// fprintf(stderr, "coef = %f\n", coef);
2021-02-27 22:52:09 +11:00
sz = pimg->width * pimg->height;
for (idx=0; idx<sz; idx++) {
pimg->R[idx] *= coef;
pimg->G[idx] *= coef;
pimg->B[idx] *= coef;
}
return 0;
}
/* ------------------------------------------------------------------------- */
int convertir_fimg_en_machinable(char *srcname, char *dstname,
int steps, float norm)
2021-02-27 08:32:42 +11:00
{
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);
}
2021-02-27 22:52:09 +11:00
if (norm > 0.0) {
fprintf(stderr, "normalize %p\n", &fimg);
foo = normalize(&fimg, norm);
}
2021-02-27 08:32:42 +11:00
2021-02-27 20:56:42 +11:00
foo = export_as_machinable(&fimg, dstname, steps, 0);
2021-02-27 08:32:42 +11:00
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");
2021-02-27 22:52:09 +11:00
puts("\t-n 3.14\tnormalize picture");
puts("\t-s N\nsteps on x & y");
2021-02-27 08:32:42 +11:00
if (verbosity) fimg_print_version(1);
exit(0);
}
/* ------------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int foo, opt;
2021-02-27 20:56:42 +11:00
int steps = 16;
2021-02-27 22:52:09 +11:00
float norm_val = 222.0; /* < 0 : don't normalize */
2021-02-27 08:32:42 +11:00
2021-02-27 20:56:42 +11:00
while ((opt = getopt(argc, argv, "hs:v")) != -1) {
2021-02-27 08:32:42 +11:00
switch(opt) {
2021-02-27 22:52:09 +11:00
case 'v': verbosity++; break;
case 'h': help(1); exit(1);
case 's': steps = atoi(optarg); break;
case 'n': norm_val = atof(optarg); break;
2021-02-27 08:32:42 +11:00
}
}
if (1 != argc-optind) {
fprintf(stderr, "error: %s need two filenames\n", argv[0]);
exit(1);
}
2021-02-27 22:52:09 +11:00
foo = convertir_fimg_en_machinable(argv[optind], "-", steps, norm_val);
2021-02-27 08:32:42 +11:00
if (foo)
fprintf(stderr, "%s : got a %d from convertor\n", argv[0], foo);
return 0;
}
/* ------------------------------------------------------------------------- */