libgpiv so version 4: GpivFt. Show git hash (if available) in output and
[gpivtools.git] / src / image / t-avg-img.c
blob834299303a37b4628c84e8ec1c87da9fefd84432
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*---------------------------------------------------------------------------
4 t-avg-img: calculates time-averaged values from a series if images
6 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
7 Gerber van der Graaf <gerber_graaf@users.sourceforge.net
9 T-avg-img is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 TODO:
27 BUGS:
29 ------------------------------------------------------------------------*/
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <glib.h>
34 #include <gpiv.h>
35 #include "config.h"
36 #include "git-rev.h"
38 #define PARFILE "t-avg-img.par" /* Parameter file name */
39 #define USAGE "\
40 Usage: gpiv_t-avg-img [-x | --prefix] [-b | --basename file] [-f | --first int] \
41 [l | --last int] [-h | --help] [-p | --print] \n\
42 [-s | --subtr] [-v | --version] \n\
43 \n\
44 keys: \n\
45 \n\
46 -b | --basename FILE: file base-name (without .number.r extension) \n\
47 instead of stdin and stdout \n\
48 -f | --first N: number of first image file \n\
49 -h | --help: this on-line help \n\
50 -l | --last N: number of last image file \n\
51 -p | --print: print parameters to stdout \n\
52 -s | --subtr: subtract mean from input images \n\
53 -v | --version: version number \n\
54 -x | --prefix: prefix numbering to file base name \n\
57 #define HELP "\
58 Calculates time-averaged intensities from a series of images at each pixel. \n\
59 Images should be numbered at the start or at the end of its name"
61 /*---------- Global variables ---------------------------------------------*/
62 gboolean fname__set = FALSE;
63 gboolean verbose = FALSE;
67 * Function prototypes
70 void
71 command_args (int argc, char *argv[],
72 char fname[GPIV_MAX_CHARS],
73 GpivGenPar *gen_par,
74 GpivImageProcPar *image_proc_par
76 /* ----------------------------------------------------------------------------
77 * Command line argument handling
80 char c;
81 int argc_next;
82 while (--argc > 0 && (*++argv)[0] == '-') {
83 argc_next = 0;
85 * argc_next is set to 1 if the next cmd line argument has to
86 * be searched for; in case that the command line argument
87 * concerns more than one char or cmd line argument needs a
88 * parameter
90 while (argc_next == 0 && (c = *++argv[0])) {
91 switch (c) {
92 case 'v':
94 * Use Git revision control system
96 #ifdef GIT_HASH
97 printf ("git hash: %s\n", GIT_REV);
98 #else
99 printf ("version: %s\n", GPIVTOOLS_VERSION);
100 #endif
101 exit(0);
102 break;
103 case 'h':
104 printf("\n%s", argv[0]);
105 printf("\n%s", HELP);
106 printf("\n%s", USAGE);
107 exit(0);
108 break;
110 case 'p':
111 verbose = TRUE;
112 break;
114 * file name and numbers
116 case 'b':
117 strcpy(fname, *++argv);
118 fname__set = TRUE;
119 argc_next = 1;
120 --argc;
121 break;
123 case 'f':
124 gen_par->first_file = atoi(*++argv);
125 gen_par->first_file__set = TRUE;
126 --argc;
127 argc_next = 1;
128 break;
130 case 'l':
131 gen_par->last_file = atoi(*++argv);
132 gen_par->last_file__set = TRUE;
133 --argc;
134 argc_next = 1;
135 break;
137 case 'x':
138 gen_par->file_prefix = TRUE;
139 break;
141 * subtract mean from input data
143 case 's':
144 image_proc_par->smooth_operator = GPIV_IMGOP_SUBTRACT;
145 break;
147 * long option keys
149 case '-':
150 if (strcmp("-help", *argv) == 0) {
151 printf("\n%s", argv[0]);
152 printf("\n%s", HELP);
153 printf("\n%s", USAGE);
154 exit(0);
155 } else if (strcmp("-print", *argv) == 0) {
156 verbose = TRUE;
157 } else if (strcmp("-basename", *argv) == 0) {
158 strcpy(fname, *++argv);
159 fname__set = TRUE;
160 argc_next = 1;
161 --argc;
162 } else if (strcmp("-first", *argv) == 0) {
163 gen_par->first_file = atoi(*++argv);
164 gen_par->first_file__set = TRUE;
165 --argc;
166 argc_next = 1;
167 } else if (strcmp("-last", *argv) == 0) {
168 gen_par->last_file = atoi(*++argv);
169 gen_par->last_file__set = TRUE;
170 --argc;
171 argc_next = 1;
172 } else if (strcmp("-subtr", *argv) == 0) {
173 image_proc_par->smooth_operator = GPIV_IMGOP_SUBTRACT;
174 } else if (strcmp("-prefix", *argv) == 0) {
175 gen_par->file_prefix = TRUE;
178 default:
179 gpiv_error("%s: unknown option: %s", argv[0], *argv);
180 break;
185 if (argc != 0) {
186 gpiv_error("%s: unknown argument: %s", argv[0], *argv);
194 void
195 make_fname_out (char *argv[],
196 gchar *fname_base,
197 gchar *fname_header,
198 gchar *fname_parameter,
199 gchar *fname_out
201 /*-----------------------------------------------------------------------------
202 * generates filenames for output
206 char f_dum[GPIV_MAX_CHARS];
207 #ifdef GIT_HASH
208 if (fname__set == FALSE)
209 gpiv_error("Software: %s git hash: %s: File basename has to be set",
210 argv[0], GIT_REV);
211 #else
212 if (fname__set == FALSE)
213 gpiv_error("Software: %s version: %s: File basename has to be set",
214 argv[0], GPIVTOOLS_VERSION);
215 #endif
217 gpiv_io_make_fname(fname_base, GPIV_EXT_HEADER, fname_header);
218 if (verbose) printf("# Data parameter file: %s\n", fname_header);
220 gpiv_io_make_fname(fname_base, GPIV_EXT_PAR, fname_parameter);
221 if (verbose) printf("# Data parameter file: %s\n", fname_parameter);
223 gpiv_io_make_fname(fname_base, GPIV_EXT_TA, f_dum);
224 gpiv_io_make_fname(f_dum, GPIV_EXT_PNG_IMAGE, fname_out);
225 if (verbose) printf("# Output file: %s\n", fname_out);
231 void
232 make_fname_in (char *argv[],
233 GpivGenPar *gen_par,
234 gchar *fname_base,
235 int f_number,
236 gchar *fname_in
238 /*-----------------------------------------------------------------------------
239 * generates filenames for input
242 char f_dum[GPIV_MAX_CHARS];
243 #ifdef GIT_HASH
244 if (fname__set == FALSE)
245 gpiv_error("Software: %s git hash: %s: File basename has to be set",
246 argv[0], GIT_REV);
247 #else
248 if (fname__set == FALSE)
249 gpiv_error("Software: %s version: %s: File basename has to be set",
250 argv[0], GPIVTOOLS_VERSION);
251 #endif
253 if (gen_par->file_prefix) {
254 snprintf(f_dum, GPIV_MAX_CHARS, "%d%s", f_number, fname_base);
255 gpiv_io_make_fname(f_dum, GPIV_EXT_RAW_IMAGE, fname_in);
256 } else {
257 snprintf(f_dum, GPIV_MAX_CHARS, "%s%d", fname_base, f_number);
258 gpiv_io_make_fname(f_dum, GPIV_EXT_RAW_IMAGE, fname_in);
261 if (verbose) printf("# Input file: %s\n", fname_in);
262 fprintf(stderr, "MAKE_FNAME_IN:: leaving\n");
268 GpivImage *
269 imgproc_mean (char *argv[],
270 GpivGenPar *gen_par,
271 GpivImageProcPar *image_proc_par,
272 gchar *fname_base
274 /*-----------------------------------------------------------------------------
277 guint j, k, l;
278 guint first_file = gen_par->first_file;
279 guint last_file = gen_par->last_file;
281 gchar fname_in[GPIV_MAX_CHARS];
283 GpivImage *image_sum = NULL, *image_mean = NULL, *image_in = NULL;
286 if (verbose) printf ("# Calculating mean from input data\n");
288 for (j = first_file; j <= last_file; j++) {
289 make_fname_in (argv, gen_par, fname_base, j, fname_in);
290 if ((image_in = gpiv_fread_image (fname_in)) == NULL) {
291 gpiv_error ("local_image_mean: failing gpiv_fread_image %s",
292 fname_in);
295 * Allocate image_sum when image dimensions are known
296 * Check image dimensions for all subsequent images
298 if (j == first_file) {
299 if ((image_sum = gpiv_alloc_img (image_in->header)) == NULL) {
300 gpiv_error ("local_image_mean: failing gpiv_alloc_img for image_sum");
302 } else {
303 if (image_in->header->ncolumns != image_sum->header->ncolumns
304 || image_in->header->nrows != image_sum->header->nrows
305 || image_in->header->depth != image_sum->header->depth
306 || image_in->header->x_corr != image_sum->header->x_corr
308 gpiv_error ("local_image_mean: in-and output images are of different dimensions");
313 * Summing image values at each pixel
315 for (k = 0; k < image_in->header->nrows; k++) {
316 for (l = 0; l < image_in->header->ncolumns; l++) {
317 image_sum->frame1[k][l] += image_in->frame1[k][l];
318 if (image_sum->header->x_corr == TRUE) {
319 image_sum->frame2[k][l] += image_in->frame2[k][l];
327 * Calculate average value at each pixel
329 if ((image_mean = gpiv_alloc_img (image_sum->header)) == NULL) {
330 gpiv_error ("local_image_mean: failing gpiv_alloc_img for image_mean");
333 for (k = 0; k < image_in->header->nrows; k++) {
334 for (l = 0; l < image_in->header->ncolumns; l++) {
335 gfloat mean = 0.0;
336 mean = (gfloat) image_sum->frame1[k][l] /
337 (gfloat) (last_file - first_file + 1);
338 image_mean->frame1[k][l] = (guint16) mean;
342 if (image_sum->header->x_corr == TRUE) {
343 for (k = 0; k < image_in->header->nrows; k++) {
344 for (l = 0; l < image_in->header->ncolumns; l++) {
345 gfloat mean = 0.0;
346 mean = (gfloat) image_sum->frame2[k][l] /
347 (gfloat) (last_file - first_file + 1);
348 image_mean->frame2[k][l] = (guint16) mean;
354 gpiv_free_img (image_in);
355 gpiv_free_img (image_sum);
356 return image_mean;
362 static gchar *
363 imgproc_subtract (char *argv[],
364 GpivGenPar *gen_par,
365 gchar *fname_base,
366 GpivImage *image_subtr
368 /*-----------------------------------------------------------------------------
371 gchar *err_msg = NULL;
372 FILE *fp = NULL;
373 gchar f_dum1[GPIV_MAX_CHARS] = "";
374 gchar f_dum2[GPIV_MAX_CHARS] = "";
375 gchar fname_out[GPIV_MAX_CHARS] = "";
377 guint j, k, l;
378 /* guint16 **img_in1 = NULL, **img_in2 = NULL; */
379 guint first_file = gen_par->first_file;
380 guint last_file = gen_par->last_file;
382 guint nrows = image_subtr->header->nrows;
383 guint ncolumns = image_subtr->header->ncolumns;
384 gchar fname_in[GPIV_MAX_CHARS];
386 GpivImage *image_in = NULL;
389 if (verbose) printf ("# Subtracting mean from input images\n");
391 for (j = first_file; j <= last_file; j++) {
393 if (fname__set == TRUE) {
394 make_fname_in (argv, gen_par, fname_base, j, fname_in);
395 if ((image_in = gpiv_fread_image (fname_in)) == NULL) {
396 err_msg = "imgproc_subtract: failing gpiv_fread_image";
397 return (err_msg);
399 } else {
400 if ((image_in = gpiv_read_png_image (stdin)) == NULL) {
401 err_msg = "imgproc_subtract: failing gpiv_read_png_image";
402 return (err_msg);
407 if ((err_msg = gpiv_imgproc_subtractimg (image_subtr, image_in)) != NULL) {
408 return (err_msg);
412 if (fname__set == TRUE) {
413 snprintf (f_dum1, GPIV_MAX_CHARS, "%s%d", fname_base, j);
414 gpiv_io_make_fname (f_dum1, GPIV_EXT_TA, f_dum2);
415 gpiv_io_make_fname (f_dum2, GPIV_EXT_PNG_IMAGE, fname_out);
416 if ((fp = fopen (fname_out, "wb")) == NULL) {
417 err_msg = "img_subtract: unable to open file";
418 return (err_msg);
420 gpiv_write_png_image (fp, image_in, TRUE);
421 fclose (fp);
423 if (verbose) printf ("# Output file: %s\n", fname_out);
424 snprintf (f_dum1, GPIV_MAX_CHARS, " ");;
425 snprintf (f_dum2, GPIV_MAX_CHARS, " ");;
426 snprintf (fname_out, GPIV_MAX_CHARS, " ");;
428 } else {
429 gpiv_write_png_image (stdout, image_in, TRUE);
434 return (err_msg);
439 int
440 main (int argc,
441 char *argv[]
443 /* ----------------------------------------------------------------------------
444 * main routine to calculates time-averaged image intensity
447 FILE *fp = NULL, *fp_par_dat = NULL;
448 gchar fname_base[GPIV_MAX_CHARS],
449 fname_header[GPIV_MAX_CHARS],
450 fname_out[GPIV_MAX_CHARS],
451 fname_parameter[GPIV_MAX_CHARS];
452 GpivImage *image_mean = NULL;
453 GpivGenPar *gen_par = g_new0 (GpivGenPar, 1);
454 GpivImageProcPar *image_proc_par = g_new0 (GpivImageProcPar, 1);
458 * Image processing parameter initialization
460 gpiv_genpar_parameters_set (gen_par, FALSE);
461 gpiv_imgproc_parameters_set (image_proc_par, FALSE);
462 command_args (argc, argv, fname_base, gen_par, image_proc_par);
463 if (verbose) {
464 #ifdef GIT_HASH
465 printf ("# Software: %s\n# git hash: %s\n# Command line options:\n",
466 argv[0], GIT_REV);
467 #else
468 printf ("# Software: %s\n# version: %s\n# Command line options:\n",
469 argv[0], GPIVTOOLS_VERSION);
470 #endif
471 gpiv_genpar_print_parameters (stdout, gen_par);
472 gpiv_imgproc_print_parameters (stdout, image_proc_par);
476 if (fname__set == TRUE) {
478 * Generating proper filenames
480 make_fname_out (argv, fname_base, fname_header, fname_parameter, fname_out);
481 if (verbose)
482 printf ("\n# Parameters written to: %s", fname_parameter);
486 * Prints command line parameters to par-file
488 if ((fp_par_dat = fopen (fname_parameter, "a")) == NULL) {
489 gpiv_error ("%s: failure opening %s for input",
490 argv[0], fname_parameter);
492 fprintf (fp_par_dat, "\n\n# %s\n# Command line options:\n", argv[0]);
493 gpiv_genpar_print_parameters (fp_par_dat, gen_par);
494 gpiv_imgproc_print_parameters (fp_par_dat, image_proc_par);
497 * Reading parametes from PARFILE (and writing to data par-file)
499 gpiv_scan_parameter (GPIV_GENPAR_KEY, PARFILE, gen_par, verbose);
500 gpiv_scan_resourcefiles (GPIV_GENPAR_KEY, gen_par, verbose);
501 gpiv_genpar_print_parameters (fp_par_dat, gen_par);
503 gpiv_scan_parameter (GPIV_IMGPROCPAR_KEY, PARFILE, image_proc_par,
504 verbose);
505 gpiv_scan_resourcefiles (GPIV_IMGPROCPAR_KEY, image_proc_par, verbose);
506 gpiv_imgproc_print_parameters (fp_par_dat, image_proc_par);
507 fclose (fp_par_dat);
510 } else {
511 #ifdef GIT_HASH
512 gpiv_error("Software: %s git hash: %s: File basename has to be set",
513 argv[0], GIT_REV);
514 #else
515 gpiv_error("Software: %s version: %s: File basename has to be set",
516 argv[0], GPIVTOOLS_VERSION);
517 #endif
522 * Check parameters on correct values and adjust belonging variables
523 * Nothing to do here
528 * Here the function calls of imgproc_mean and imgproc_subtract
530 if ((image_mean = imgproc_mean (argv, gen_par, image_proc_par, fname_base))
531 == NULL) {
532 gpiv_error ("%s: failing img_mean", argv[0]);
535 if (image_proc_par->smooth_operator == GPIV_IMGOP_SUBTRACT)
536 imgproc_subtract (argv, gen_par, fname_base, image_mean);
540 * And writing to output
542 if (fname__set == TRUE) {
543 if ((fp = fopen (fname_out, "wb")) == NULL) {
544 return (1);
546 gpiv_write_png_image (fp, image_mean, TRUE);
547 fclose (fp);
548 } else {
549 #ifdef GIT_HASH
550 gpiv_error("Software: %s git hash: %s: File basename has to be set",
551 argv[0], GIT_REV);
552 #else
553 gpiv_error("Software: %s version: %s: File basename has to be set",
554 argv[0], GPIVTOOLS_VERSION);
555 #endif
558 return 0;