FloatImg/tools/fimg2text.c

180 lines
4.0 KiB
C
Raw Normal View History

2021-02-26 22:32:42 +01:00
/*
* converting a floatimg to a machinable text file
2021-02-27 12:52:09 +01:00
* an ugly software from tTh - february 2021
2021-02-26 22:32:42 +01:00
*/
#include <stdio.h>
#include <stdlib.h>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2021-02-26 22:32:42 +01:00
#include <unistd.h>
#include <string.h>
#include "../floatimg.h"
int verbosity;
/* --------------------------------------------------------------------- */
2021-02-27 10:56:42 +01:00
int export_as_machinable(FloatImg *src, char *fname, int steps, int flags)
2021-02-26 22:32:42 +01:00
{
FILE *fp;
2021-02-28 00:45:09 +01:00
int x, y;
float rgb[3];
2021-02-26 22:32:42 +01:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %25s ( %p '%s' %d )\n", __func__,
src, fname, flags);
#endif
fp = NULL; /* molly guard */
if (strcmp("-", fname)) { /* real file */
2021-02-28 00:45:09 +01:00
fprintf(stderr, "real file '%s'\n", fname);
2021-02-26 22:32:42 +01:00
}
2021-02-28 00:45:09 +01:00
else {
2021-02-28 13:50:19 +01:00
// fprintf(stderr, "kitchen sink\n");
2021-02-26 22:32:42 +01:00
}
fp = stdout; /* XXX */
2021-02-27 10:56:42 +01:00
for (y=0; y<src->height; y+=steps) {
for (x=0; x<src->width; x+=steps) {
2021-02-28 00:45:09 +01:00
fimg_get_rgb(src, x, y, rgb);
2021-02-27 10:56:42 +01:00
fprintf(fp, "%d %d ", x, y);
2021-02-28 00:45:09 +01:00
fprintf(fp, "%f %f %f\n", rgb[0], rgb[1], rgb[2]);
2021-02-26 22:32:42 +01:00
}
}
return 0;
}
/* --------------------------------------------------------------------- */
2021-02-27 12:52:09 +01:00
static int normalize(FloatImg *pimg, float vmax)
{
float mmv[6], maxi, coef;
int foo, sz, idx;
2021-02-27 23:35:27 +01:00
#if DEBUG_LEVEL
2021-02-27 12:52:09 +01:00
fprintf(stderr, ">>> %s ( %p %g )\n", __func__, pimg, vmax);
2021-02-27 23:35:27 +01:00
#endif
2021-02-27 12:52:09 +01:00
foo = fimg_get_minmax_rgb(pimg, mmv);
2021-02-28 00:45:09 +01:00
if (foo) {
fprintf(stderr, "%s: ABEND\n", __func__);
abort();
}
2021-02-27 12:52:09 +01:00
maxi = mmv[1];
if (mmv[3] > maxi) maxi = mmv[3];
if (mmv[5] > maxi) maxi = mmv[5];
coef = vmax / maxi;
2021-02-28 00:45:09 +01:00
if (verbosity) {
fprintf(stderr, "mins %f %f %f\n", mmv[0], mmv[2], mmv[4]);
fprintf(stderr, "maxs %f %f %f\n", mmv[1], mmv[3], mmv[5]);
fprintf(stderr, "coef = %f\n", coef);
}
2021-02-27 12:52:09 +01: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;
}
/* --------------------------------------------------------------------- */
2021-02-27 12:52:09 +01:00
int convertir_fimg_en_machinable(char *srcname, char *dstname,
int steps, float norm)
2021-02-26 22:32:42 +01:00
{
int foo, infos[3];
FloatImg fimg;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %25s ( '%s' '%s' %d )\n", __func__,
srcname, dstname, notused);
#endif
2021-04-24 15:18:06 +02:00
if (steps < 1) {
fprintf(stderr, "%s: steps MUST be > 0\n", __func__);
exit(1);
}
2021-02-26 22:32:42 +01:00
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-28 00:45:09 +01:00
fprintf(stderr, "normalize to %f\n", norm);
2021-02-26 22:32:42 +01:00
}
2021-02-27 12:52:09 +01:00
if (norm > 0.0) {
2021-02-28 00:45:09 +01:00
// fprintf(stderr, "normalize %p\n", &fimg);
2021-02-27 12:52:09 +01:00
foo = normalize(&fimg, norm);
}
2021-02-26 22:32:42 +01:00
2021-02-27 10:56:42 +01:00
foo = export_as_machinable(&fimg, dstname, steps, 0);
2021-03-11 18:32:42 +01:00
if (foo) {
fprintf(stderr,"%s: err %d on export\n", __func__, foo);
}
2021-02-26 22:32:42 +01:00
fimg_destroy(&fimg);
return 0;
}
/* --------------------------------------------------------------------- */
2021-02-26 22:32:42 +01:00
void help(int k)
{
puts("usage:\n\tfimg2text [options] foo.fimg > bar.csv");
puts("options:");
puts("\t-v\t\tincrease verbosity");
puts("\t-n 3.14\t\tnormalize picture");
puts("\t-s N\t\tsteps on x & y");
2021-02-26 22:32:42 +01:00
if (verbosity) fimg_print_version(1);
exit(0);
}
/* --------------------------------------------------------------------- */
2021-02-26 22:32:42 +01:00
int main(int argc, char *argv[])
{
int foo, opt;
2021-04-24 15:18:06 +02:00
int steps = 1;
2021-02-27 12:52:09 +01:00
float norm_val = 222.0; /* < 0 : don't normalize */
2021-04-24 15:18:06 +02:00
char separator = ' ';
2021-02-26 22:32:42 +01:00
2021-04-24 15:18:06 +02:00
while ((opt = getopt(argc, argv, "f:hn:s:v")) != -1) {
2021-02-26 22:32:42 +01:00
switch(opt) {
2021-04-24 15:18:06 +02:00
case 'f': separator = optarg[0]; break;
2021-02-27 12:52:09 +01: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-26 22:32:42 +01:00
}
}
if (1 != argc-optind) {
fprintf(stderr, "error: %s need one intput filename\n", argv[0]);
2021-02-26 22:32:42 +01:00
exit(1);
}
2021-02-27 12:52:09 +01:00
foo = convertir_fimg_en_machinable(argv[optind], "-", steps, norm_val);
2021-02-28 00:45:09 +01:00
if (foo) {
2021-02-26 22:32:42 +01:00
fprintf(stderr, "%s : got a %d from convertor\n", argv[0], foo);
2021-04-30 15:32:14 +02:00
return 1;
2021-02-28 00:45:09 +01:00
}
2021-02-26 22:32:42 +01:00
return 0;
}
/* --------------------------------------------------------------------- */
2021-02-26 22:32:42 +01:00