FloatImg/tools/cumulfimgs.c

193 lines
4.2 KiB
C
Raw Normal View History

2020-09-22 09:43:21 +11:00
/*
* This thing is just a mess !
2022-07-11 15:12:25 +11:00
* ***************************
2020-09-22 09:43:21 +11:00
*/
2019-07-04 03:51:42 +11:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2021-05-20 18:31:28 +11:00
#include <stdint.h>
2023-10-08 04:19:49 +11:00
#include <string.h>
2019-07-04 03:51:42 +11:00
#include "../floatimg.h"
int verbosity;
2019-07-16 09:19:59 +11:00
int g_width, g_height;
/* --------------------------------------------------------------------- */
int testfile(char *path)
{
int foo, numbers[3];
foo = fimg_fileinfos(path, numbers);
if (foo) {
2020-09-25 20:43:25 +11:00
fprintf(stderr, "fileinfo of '%s' -> err %d\n", path, foo);
2019-07-16 09:19:59 +11:00
return foo;
}
2020-12-29 10:54:15 +11:00
if (verbosity) {
fprintf(stderr, "%-20s %dx%d\n", path, numbers[0], numbers[1]);
}
2019-07-16 09:19:59 +11:00
2020-09-22 09:43:21 +11:00
if (FIMG_TYPE_RGB != numbers[2]) {
2019-07-16 09:19:59 +11:00
fprintf(stderr, "file %s, %d : bad type\n", path, numbers[2]);
return -7;
}
2020-09-22 09:43:21 +11:00
return 0;
2019-07-16 09:19:59 +11:00
}
2019-07-04 03:51:42 +11:00
/* --------------------------------------------------------------------- */
void help(int v)
{
2020-10-04 00:52:27 +11:00
puts("");
puts("$ cumulfimgs a.fimg b.fimg c-fimg ...");
2020-09-22 09:43:21 +11:00
puts("cumulator options :");
2020-09-25 20:43:25 +11:00
puts("\t-g\tconvert to gray level");
2023-10-08 04:19:49 +11:00
puts("\t-m\tcompute the max of the maxes");
2022-09-18 04:18:45 +11:00
puts("\t-o\tname of output file");
puts("\t-s\trescale end image");
puts("\t-v\tincrease verbosity");
2020-10-04 00:52:27 +11:00
puts("");
2023-10-08 04:19:49 +11:00
fimg_print_version(v);
2020-09-22 09:43:21 +11:00
exit(0);
2019-07-04 03:51:42 +11:00
}
/* --------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
2019-07-16 09:19:59 +11:00
int foo, idx;
2019-07-04 03:51:42 +11:00
int opt;
2020-09-25 20:43:25 +11:00
2022-09-18 04:18:45 +11:00
int compte = 0;
int to_gray = 0;
int experiment = 0;
int rescale = 0;
int src_loaded = 0;
2023-10-08 04:19:49 +11:00
int minmax = 0;
2022-09-18 04:18:45 +11:00
char *output_file = "out.fimg";
2020-09-22 09:43:21 +11:00
FloatImg accu, temp;
2023-10-08 04:19:49 +11:00
float vals[3];
2019-07-04 03:51:42 +11:00
2019-07-16 09:19:59 +11:00
g_width = g_height = 0;
2023-10-08 04:19:49 +11:00
while ((opt = getopt(argc, argv, "ghmo:svx")) != -1) {
2019-07-04 03:51:42 +11:00
switch(opt) {
2020-09-25 20:43:25 +11:00
case 'g': to_gray = 1; break;
2022-07-06 19:27:55 +11:00
case 'h': help(1); break;
2023-10-08 04:19:49 +11:00
case 'm': minmax++; break;
2019-07-04 03:51:42 +11:00
case 'o': output_file = optarg; break;
2022-09-18 04:18:45 +11:00
case 's': rescale = 1; break;
2019-07-04 03:51:42 +11:00
case 'v': verbosity++; break;
2022-02-10 09:21:58 +11:00
case 'x': experiment++; break;
2023-05-29 18:06:57 +11:00
default: exit(1);
2019-07-04 03:51:42 +11:00
}
}
2020-09-22 09:43:21 +11:00
if (verbosity) fprintf(stderr, "------ cumulfimgs ------\n");
2019-07-16 09:19:59 +11:00
#if DEBUG_LEVEL
fprintf(stderr, "argc = %d, optind = %d\n", argc, optind);
#endif
2023-10-08 04:19:49 +11:00
memset(vals, 0, 3*sizeof(float));
2019-07-16 09:19:59 +11:00
for (idx=optind; idx<argc; idx++) {
2023-10-08 04:19:49 +11:00
#if DEBUG_LEVEL
fprintf(stderr, "---------------- %s\n", argv[idx]);
fflush(stderr);
2020-09-25 20:43:25 +11:00
#endif
2019-07-16 09:19:59 +11:00
foo = testfile(argv[idx]);
2020-09-25 20:43:25 +11:00
if (foo) {
2022-09-18 04:18:45 +11:00
fprintf(stderr, "testfile %s -> %d\n", argv[idx], foo);
2020-10-04 00:52:27 +11:00
exit(1);
2020-09-25 20:43:25 +11:00
}
2019-07-16 09:19:59 +11:00
2023-10-08 04:19:49 +11:00
/*
* load the picture, create a clone of the first
*/
2020-09-22 09:43:21 +11:00
if ( ! src_loaded ) {
foo = fimg_create_from_dump(argv[idx], &accu);
2023-10-08 04:19:49 +11:00
if (foo) {
fprintf(stderr, "create from dump -> %d\n", foo);
exit(1);
}
2020-09-22 09:43:21 +11:00
fimg_clone(&accu, &temp, 0);
src_loaded = 1;
}
else {
foo = fimg_load_from_dump(argv[idx], &temp);
2020-09-25 20:43:25 +11:00
if (foo) {
fprintf(stderr, "load from dump -> %d\n", foo);
exit(1);
}
2020-09-22 09:43:21 +11:00
}
2023-10-08 04:19:49 +11:00
if (minmax) {
/*
* print the maximum values and compute the
* maximum of all the maximums
*/
foo = fimg_max_of_max(&temp, vals);
// fprintf(stderr, "%5d max of max %9.3f %9.3f %9.3f\n",
// idx, vals[0], vals[1], vals[2]);
}
fimg_add_2(&temp, &accu);
2020-12-29 10:54:15 +11:00
compte++;
2019-07-16 09:19:59 +11:00
}
2022-09-18 04:18:45 +11:00
if (rescale) {
fprintf(stderr, "cumulfimg: count = %d\n", compte);
fimg_div_cste(&accu, (float)compte);
}
/* XXX */
2022-02-10 09:21:58 +11:00
if (experiment) {
2022-09-18 04:18:45 +11:00
}
/* XXX */
2022-02-10 09:21:58 +11:00
2023-10-08 04:19:49 +11:00
fprintf(stderr, "max of max %9.3f %9.3f %9.3f\n",
vals[0], vals[1], vals[2]);
2022-02-10 09:21:58 +11:00
2020-09-25 20:43:25 +11:00
if (to_gray) {
foo = fimg_desaturate(&accu, &accu, 0);
2022-07-11 15:12:25 +11:00
/* ?
* but here, final picture is in RGB shape, so export give us
* also an image rgb-shaped. this is not optimal, I think.
*/
2020-09-25 20:43:25 +11:00
if (foo) {
fprintf(stderr, "desaturate: error %d\n", foo);
}
}
2022-02-10 09:21:58 +11:00
/* PLEASE CHECK IF EXPORT TO GRAY MAKE A REAL .pgm FILE */
foo = fimg_export_picture(&accu, output_file, 0);
2020-10-04 00:52:27 +11:00
if (foo) {
fprintf(stderr, "error %d while saving '%s'\n", foo, output_file);
exit(1);
}
2020-09-22 09:43:21 +11:00
if (verbosity) {
/* show some numbers about resultant picture */
foo = fimg_get_minmax_rgb(&accu, vals);
if (foo) {
fprintf(stderr, "err %d on fimg_get_minmax_rgb\n", foo);
return foo;
}
2020-12-29 10:54:15 +11:00
printf("Count %d\n", compte);
printf("Rmin %12.4g Rmax %12.4g delta %12g\n",
vals[0], vals[1], vals[1]-vals[0]);
printf("Gmin %12.4g Gmax %12.4g %12g\n",
vals[2], vals[3], vals[3]-vals[2]);
printf("Bmin %12.4g Bmax %12.4g %12g\n",
vals[4], vals[5], vals[5]-vals[4]);
}
2023-10-08 04:19:49 +11:00
fimg_destroy(&accu); fimg_destroy(&temp);
2019-07-04 03:51:42 +11:00
return 0;
}
/* --------------------------------------------------------------------- */