FloatImg/tools/fimg2text.c

196 lines
4.4 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
2023-10-09 10:48:39 +02:00
fprintf(stderr, ">>> %s ( %p '%s' 0x%x )\n", __func__,
2021-02-26 22:32:42 +01:00
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 */
2023-10-09 10:48:39 +02:00
2023-10-18 16:08:51 +02:00
/*
* put a first line, so we can load those data with R
*/
2023-10-09 10:48:39 +02:00
if (flags & 1) { fputs("X Y R G B\n", fp); }
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;
}
/* --------------------------------------------------------------------- */
2023-10-09 10:48:39 +02:00
/*
* This function must be in the libfloatimg !
*/
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,
2023-10-09 10:48:39 +02:00
int steps, float norm, int header)
2021-02-26 22:32:42 +01:00
{
int foo, infos[3];
2023-10-09 10:48:39 +02:00
int flg;
2021-02-26 22:32:42 +01:00
FloatImg fimg;
#if DEBUG_LEVEL
2023-10-09 10:48:39 +02:00
fprintf(stderr, ">>> %s ( '%s' '%s' %d %f %d )\n", __func__,
srcname, dstname, steps, norm, header);
2021-02-26 22:32:42 +01:00
#endif
2021-04-24 15:18:06 +02:00
if (steps < 1) {
fprintf(stderr, "%s: steps MUST be > 0\n", __func__);
exit(1);
}
2023-10-09 10:48:39 +02:00
flg = 0;
if (header) flg |= 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;
}
2021-02-27 12:52:09 +01:00
if (norm > 0.0) {
2023-10-18 16:08:51 +02:00
if (verbosity) fprintf(stderr, "normalize to %f\n", norm);
2021-02-27 12:52:09 +01:00
foo = normalize(&fimg, norm);
}
2021-02-26 22:32:42 +01:00
2023-10-09 10:48:39 +02:00
foo = export_as_machinable(&fimg, dstname, steps, flg);
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:");
2023-10-09 10:48:39 +02:00
puts("\t-n Value\tnormalize picture if Value > 0.0");
puts("\t-s N\t\tsteps on x & y");
2023-10-09 10:48:39 +02:00
puts("\t-T\t\tadd header with colon name");
puts("\t-v\t\tincrease verbosity");
2021-06-12 13:54:35 +02:00
if (verbosity) {
printf("*** compiled: %s at %s\n", __DATE__, __TIME__);
2022-07-06 10:27:55 +02:00
fimg_print_version(k);
2021-06-12 13:54:35 +02:00
}
2021-02-26 22:32:42 +01:00
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;
2023-10-09 10:48:39 +02:00
int header = 0;
2023-10-18 16:08:51 +02:00
float norm_val = -1; /* < 0 : don't normalize */
2022-07-06 10:27:55 +02:00
// char separator = ' ';
2021-02-26 22:32:42 +01:00
2023-10-09 10:48:39 +02:00
while ((opt = getopt(argc, argv, "f:hn:s:Tv")) != -1) {
2021-02-26 22:32:42 +01:00
switch(opt) {
2022-07-06 10:27:55 +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;
2023-10-09 10:48:39 +02:00
case 'T': header = 1; break;
2021-02-27 12:52:09 +01:00
case 'n': norm_val = atof(optarg); break;
2023-05-29 09:06:57 +02:00
default: exit(1);
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);
}
2023-10-09 10:48:39 +02:00
foo = convertir_fimg_en_machinable(argv[optind], "-",
steps, norm_val, header);
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