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)
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.
29 ------------------------------------------------------------------------*/
38 #define PARFILE "t-avg-img.par" /* Parameter file name */
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\
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\
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
;
71 command_args (int argc
, char *argv
[],
72 char fname
[GPIV_MAX_CHARS
],
74 GpivImageProcPar
*image_proc_par
76 /* ----------------------------------------------------------------------------
77 * Command line argument handling
82 while (--argc
> 0 && (*++argv
)[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
90 while (argc_next
== 0 && (c
= *++argv
[0])) {
94 * Use Git revision control system
97 printf ("git hash: %s\n", GIT_REV
);
99 printf ("version: %s\n", GPIVTOOLS_VERSION
);
104 printf("\n%s", argv
[0]);
105 printf("\n%s", HELP
);
106 printf("\n%s", USAGE
);
114 * file name and numbers
117 strcpy(fname
, *++argv
);
124 gen_par
->first_file
= atoi(*++argv
);
125 gen_par
->first_file__set
= TRUE
;
131 gen_par
->last_file
= atoi(*++argv
);
132 gen_par
->last_file__set
= TRUE
;
138 gen_par
->file_prefix
= TRUE
;
141 * subtract mean from input data
144 image_proc_par
->smooth_operator
= GPIV_IMGOP_SUBTRACT
;
150 if (strcmp("-help", *argv
) == 0) {
151 printf("\n%s", argv
[0]);
152 printf("\n%s", HELP
);
153 printf("\n%s", USAGE
);
155 } else if (strcmp("-print", *argv
) == 0) {
157 } else if (strcmp("-basename", *argv
) == 0) {
158 strcpy(fname
, *++argv
);
162 } else if (strcmp("-first", *argv
) == 0) {
163 gen_par
->first_file
= atoi(*++argv
);
164 gen_par
->first_file__set
= TRUE
;
167 } else if (strcmp("-last", *argv
) == 0) {
168 gen_par
->last_file
= atoi(*++argv
);
169 gen_par
->last_file__set
= TRUE
;
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
;
179 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
186 gpiv_error("%s: unknown argument: %s", argv
[0], *argv
);
195 make_fname_out (char *argv
[],
198 gchar
*fname_parameter
,
201 /*-----------------------------------------------------------------------------
202 * generates filenames for output
206 char f_dum
[GPIV_MAX_CHARS
];
208 if (fname__set
== FALSE
)
209 gpiv_error("Software: %s git hash: %s: File basename has to be set",
212 if (fname__set
== FALSE
)
213 gpiv_error("Software: %s version: %s: File basename has to be set",
214 argv
[0], GPIVTOOLS_VERSION
);
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
);
232 make_fname_in (char *argv
[],
238 /*-----------------------------------------------------------------------------
239 * generates filenames for input
242 char f_dum
[GPIV_MAX_CHARS
];
244 if (fname__set
== FALSE
)
245 gpiv_error("Software: %s git hash: %s: File basename has to be set",
248 if (fname__set
== FALSE
)
249 gpiv_error("Software: %s version: %s: File basename has to be set",
250 argv
[0], GPIVTOOLS_VERSION
);
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
);
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");
269 imgproc_mean (char *argv
[],
271 GpivImageProcPar
*image_proc_par
,
274 /*-----------------------------------------------------------------------------
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",
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");
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
++) {
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
++) {
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
);
363 imgproc_subtract (char *argv
[],
366 GpivImage
*image_subtr
368 /*-----------------------------------------------------------------------------
371 gchar
*err_msg
= NULL
;
373 gchar f_dum1
[GPIV_MAX_CHARS
] = "";
374 gchar f_dum2
[GPIV_MAX_CHARS
] = "";
375 gchar fname_out
[GPIV_MAX_CHARS
] = "";
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";
400 if ((image_in
= gpiv_read_png_image (stdin
)) == NULL
) {
401 err_msg
= "imgproc_subtract: failing gpiv_read_png_image";
407 if ((err_msg
= gpiv_imgproc_subtractimg (image_subtr
, image_in
)) != NULL
) {
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";
420 gpiv_write_png_image (fp
, image_in
, TRUE
);
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
, " ");;
429 gpiv_write_png_image (stdout
, image_in
, TRUE
);
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
);
465 printf ("# Software: %s\n# git hash: %s\n# Command line options:\n",
468 printf ("# Software: %s\n# version: %s\n# Command line options:\n",
469 argv
[0], GPIVTOOLS_VERSION
);
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
);
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
,
505 gpiv_scan_resourcefiles (GPIV_IMGPROCPAR_KEY
, image_proc_par
, verbose
);
506 gpiv_imgproc_print_parameters (fp_par_dat
, image_proc_par
);
512 gpiv_error("Software: %s git hash: %s: File basename has to be set",
515 gpiv_error("Software: %s version: %s: File basename has to be set",
516 argv
[0], GPIVTOOLS_VERSION
);
522 * Check parameters on correct values and adjust belonging variables
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
))
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
) {
546 gpiv_write_png_image (fp
, image_mean
, TRUE
);
550 gpiv_error("Software: %s git hash: %s: File basename has to be set",
553 gpiv_error("Software: %s version: %s: File basename has to be set",
554 argv
[0], GPIVTOOLS_VERSION
);