added 'Substitute all FNAME occurrences' option
[gpivtools.git] / src / misc / img2gpiv.pl
blobcd45e975ddffbb9a8964416cdd9ccb3e22eb64d3
1 #!/usr/bin/perl -w
3 # img2gpiv - Converts an image frame "image_name", containing a PIV image
4 # pair, into a raw data frame "image_name.r" in order to
5 # analyse with gpiv's 'rr' program. Additional header info is
6 # saved in "image_name.h".
8 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
9 # Gerber van der Graaf <gerber_graaf@users.sourceforge.net
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2, or (at your option)
14 # any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software Foundation,
23 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #------------------------------------------------------------------
27 $VERSION = q$Id: img2gpiv.pl,v 1.12 2008-04-28 12:09:30 gerber Exp $;
28 $HELP = "Converts images into raw data (extension .r) format with belonging
29 ASCII header or hdf (extension .hdf) format. \n";
30 $USAGE = "gpiv_img2gpiv [-a|--auto] [-c|--cols ncolumns] [--combine] [-d|--depth]
31 [-h|--help] [-hdf] [-i|--inverse] [--suf_a]
32 [--suf_b] [--suf_num N] [-n] [--noclean] [-p] [-r|--rows nrows] [-t] [-v|--version]
33 file_base_name
35 keys:
36 -x|cross: double image for cross-correlation (default: single image
37 for auto-correlation)
38 -c|cols ncolumns: the number of columns in the frame (default 1280)
39 --combine combinates two separate image frames to a single one for
40 cross-correlation. Also look at --suf_a/b and --suf_num
41 -d|depth depth: color depth (default 8)
42 -h|help: on-line help
43 -hdf: converts to (or from) hdf format (extension .hdf)
44 -i: inverse conversion; from raw 8-bit data to image format
45 --suf_a with --combine: suffix of first image (default: _a)
46 --suf_b with --combine: suffix of second image (default: _b)
47 --suf_num N with --combine: subsequent numbered images of which the
48 first one with number N and the second N+1. The converted
49 image will be named to file_base_name + N
50 -n: no execute, only print processes
51 -noclean: do not remove existing image
52 -r|rows rows: the number of rows in the frame of a SINGLE image
53 (default 1024)
54 -p: prints process to stdout
55 -t: image type or format (default bmp)
56 -v|version: version";
59 #----------------- Default parameters ----------
60 $opt_x = 0;
61 $ncols = 1280;
62 $opt_combine = 0;
63 ##$opt_d = 0;
64 ##$opt_f = 0;
65 $opt_h = 0;
66 $opt_hdf = 0;
67 $opt_i = 0;
68 $opt_n = 0;
69 $opt_noclean = 0;
70 $nrows = 1024;
71 $opt_p = 0;
72 ##$opt_t = ".bmp";
73 $opt_v = 0;
74 $suf_set = 0;
75 $suf_num = -9999;
76 $suf_num_next = -9999;
78 #------------------ Command line arguments handling ----------
79 use Getopt::Long;
80 ##$result =
81 GetOptions("h|help", "i|inverse", "n|none", "p|print",
82 'hdf',
83 "noclean",
84 "t|type=s" => \$format,
85 "v|version",
86 'c|cols=i' => \$ncols,
87 "combine",
88 'suf_a=s' => \$suf_a,
89 'suf_b=s' => \$suf_b,
90 'suf_num=i' => \$suf_num,
91 'd|depth=i' => \$depth,
92 'r|rows=i' => \$nrows,
93 "x|cross"
97 if ($opt_h) {
98 print ("$HELP\n");
99 print ("$USAGE\n");
100 exit;
103 if ($opt_v) {
104 print ("$VERSION\n");
105 exit;
108 $file_base_name = shift (@ARGV);
111 if ($#ARGV != -1) {
112 printf ("\nUsage: $USAGE\n");
113 exit;
117 #------------------ Default variables ----------
118 if (!$nrows) {
119 $nrows = 1024;
122 if (!$ncols) {
123 $ncols = 1280;
126 if (!$depth) {
127 $depth = 8;
130 if (!$format) {
131 $format = "bmp";
134 if (!$suf_a) {
135 $suf_a = "_a";
136 } else {
137 $suf_set = 1;
140 if (!$suf_b) {
141 $suf_b = "_b";
142 } else {
143 $suf_set = 1;
146 if ($suf_num != -9999) {
147 $suf_num_next = $suf_num + 1;
148 if ($opt_p || $opt_n) {
149 print ("suf_num=$suf_num suf_num_next=$suf_num_next\n");
153 if ($suf_set == 1 && $suf_num != -9999) {
154 print ("error: a suffix is defined as well as a numbered filenames is used.");
155 exit;
158 if ($opt_p || $opt_n) {
159 print ("c=$ncols r=$nrows i=$opt_i t=$format f=$file_base_name\n");
162 if (!$opt_i) {
163 if ($suf_num == -9999) {
164 $file_name_header = $file_base_name.".h";
165 } else {
166 $file_name_header = $file_base_name.$suf_num.".h";
169 if ($opt_combine) {$opt_x = 1;}
170 #------------------ Convert the entire frame to raw data
171 if ($opt_combine) {
172 if ($opt_noclean) {
173 if ($suf_num == -9999) {
174 @args=("convert $file_base_name$suf_a.$format -depth $depth gray:$file_base_name$suf_a.r && \
175 convert $file_base_name$suf_b.$format -depth $depth gray:$file_base_name$suf_b.r");
176 } else {
177 @args=("convert $file_base_name$suf_num.$format -depth $depth gray:$file_base_name$suf_num.r && \
178 convert $file_base_name$suf_num_next.$format -depth $depth gray:$file_base_name$suf_num_next.r");
180 } else {
181 if ($suf_num == -9999) {
182 @args=("convert $file_base_name$suf_a.$format -depth $depth gray:$file_base_name$suf_a.r && rm $file_base_name$suf_a.$format && \
183 convert $file_base_name$suf_b.$format -depth $depth gray:$file_base_name$suf_b.r && rm $file_base_name$suf_b.$format");
184 } else {
185 @args=("convert $file_base_name$suf_num.$format -depth $depth gray:$file_base_name$suf_num.r && rm $file_base_name$suf_num.$format && \
186 convert $file_base_name$suf_num_next.$format -depth $depth gray:$file_base_name$suf_num_next.r && rm $file_base_name$suf_num_next.$format");
189 } else {
190 if ($opt_noclean) {
191 @args=("convert $file_base_name.$format -depth $depth gray:$file_base_name.r");
192 } else {
193 @args=("convert $file_base_name.$format -depth $depth gray:$file_base_name.r && rm $file_base_name.$format");
196 if ($opt_p || $opt_n) {printf ("@args \n");}
197 if (!$opt_n) {system (@args) == 0 || die "system @args failed: $?";}
199 if ($opt_combine) {
200 if ($suf_num == -9999) {
201 @args=("cat $file_base_name$suf_b.r >> $file_base_name$suf_a.r && mv $file_base_name$suf_a.r $file_base_name.r && rm $file_base_name$suf_b.r");
202 if ($opt_p || $opt_n) {printf ("@args \n");}
203 if (!$opt_n) {system (@args) == 0 || die "system @args failed: $?";}
204 } else {
205 @args=("cat $file_base_name$suf_num_next.r >> $file_base_name$suf_num.r && rm $file_base_name$suf_num_next.r");
206 if ($opt_p || $opt_n) {printf ("@args \n");}
207 if (!$opt_n) {system (@args) == 0 || die "system @args failed: $?";}
211 #------------------ Add header info
212 if (!$opt_n) {
213 open (OUTH,">$file_name_header") || die 'can\'t open $file_name_header';
214 printf(OUTH "Ncolumns: $ncols\n");
215 printf(OUTH "Nrows: %d\n", $nrows);
216 printf(OUTH "X_corr: %d\n", $opt_x);
217 printf(OUTH "Nbits: %d\n", $depth);
218 close (OUTH) || die 'can\'t close $file_name_header';
219 } else {
220 printf("Ncolumns: $ncols\n");
221 printf("Nrows: %d\n", $nrows);
222 printf("X_corr: %d\n", $opt_x);
223 printf("Nbits: %d\n", $depth);
226 if ($opt_hdf) {
227 if ($opt_p || $opt_n) {
228 @args=("gpiv_piv2hdf -i -p -e $file_base_name");
229 } else {
230 @args=("gpiv_piv2hdf -i -e $file_base_name");
232 if ($opt_p || $opt_n) {printf ("@args \n");}
233 if (!$opt_n) {system (@args) == 0 || die "system @args failed: $?";}
235 @args=("rm $file_base_name.r $file_base_name.h");
236 if ($opt_p || $opt_n) {printf ("@args \n");}
237 if (!$opt_n) {system (@args) == 0 || die "system @args failed: $?";}
241 } else { # $opt_i; inverse conversion
242 if ($opt_hdf) {
243 if ($opt_p || $opt_n) {
244 @args=("hdf2piv -p -e $file_base_name");
245 } else {
246 @args=("hdf2piv -e $file_base_name");
248 if ($opt_p || $opt_n) {printf ("@args \n");}
249 if (!$opt_n) {system (@args) == 0 || die "system @args failed: $?";}
250 if ($opt_x) {
251 if ($opt_noclean) {
252 $CMD = sprintf("convert -size %dx%d -depth $depth gray:$file_base_name.r $file_base_name.$format",
253 $ncols, $nrows * 2);
254 } else {
255 $CMD = sprintf("convert -size %dx%d -depth $depth gray:$file_base_name.r $file_base_name.$format && rm $file_base_name.r $file_base_name.hdf",
256 $ncols, $nrows * 2);
258 } else {
259 if ($opt_noclean) {
260 $CMD = sprintf("convert -size %dx%d -depth $depth gray:$file_base_name.r $file_base_name.$format",
261 $ncols, $nrows);
262 } else {
263 $CMD = sprintf("convert -size %dx%d -depth $depth gray:$file_base_name.r $file_base_name.$format && rm $file_base_name.r $file_base_name.hdf",
264 $ncols, $nrows);
267 } else {
268 if ($opt_x) {
269 if ($opt_noclean) {
270 $CMD = sprintf("convert -size %dx%d -depth $depth gray:$file_base_name.r $file_base_name.$format",
271 $ncols, $nrows * 2);
272 } else {
273 $CMD = sprintf("convert -size %dx%d -depth $depth gray:$file_base_name.r $file_base_name.$format && rm $file_base_name.r",
274 $ncols, $nrows * 2);
276 } else {
277 if ($opt_noclean) {
278 $CMD = sprintf("convert -size %dx%d -depth $depth gray:$file_base_name.r $file_base_name.$format",
279 $ncols, $nrows);
280 } else {
281 $CMD = sprintf("convert -size %dx%d -depth $depth gray:$file_base_name.r $file_base_name.$format && rm $file_base_name.r",
282 $ncols, $nrows);
286 @args=($CMD);
287 if ($opt_p || $opt_n) {printf ("@args \n");}
288 if (!$opt_n) {system (@args) == 0 || die "system @args failed: $?";}
293 # ----------------- That's all folks.