3 # Copyright (c) International Business Machines Corp., 2002
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or (at
8 # your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # This script creates an overview PNG image of a source code file by
23 # representing each source code character by a single pixel.
25 # Note that the PERL module GD.pm is required for this script to work.
26 # It may be obtained from http://www.cpan.org
29 # 2002-08-26: created by Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
38 our $lcov_version = "LCOV version 1.7";
39 our $lcov_url = "http://ltp.sourceforge.net/coverage/lcov.php";
40 our $tool_name = basename
($0);
45 sub check_and_load_module
($);
46 sub genpng_print_usage
(*);
47 sub genpng_process_file
($$$$);
56 # Check whether required module GD.pm is installed
57 if (check_and_load_module
("GD"))
59 # Note: cannot use die() to print this message because inserting this
60 # code into another script via do() would not fail as required!
61 print(STDERR
<<END_OF_TEXT)
62 ERROR: required module GD.pm not found on this system (see www.cpan.org).
68 # Check whether we're called from the command line or from another script
78 $SIG{__WARN__} = \&warn_handler;
79 $SIG{__DIE__} = \&die_handler;
81 # Parse command line options
82 if (!GetOptions("tab-size=i" => \$tab_size,
84 "output-filename=s" => \$out_filename,
86 "version" => \$version))
88 print(STDERR "Use $tool_name --help to get usage ".
98 genpng_print_usage(*STDOUT);
102 # Check for version flag
105 print("$tool_name: $lcov_version\n");
112 die("No filename specified\n");
115 # Check for output filename
118 $out_filename = "$filename.png";
121 genpng_process_file($filename, $out_filename, $width, $tab_size);
127 # genpng_print_usage(handle)
129 # Write out command line usage information to given filehandle.
132 sub genpng_print_usage(*)
134 local *HANDLE = $_[0];
136 print(HANDLE <<END_OF_USAGE
)
137 Usage
: $tool_name [OPTIONS
] SOURCEFILE
139 Create an overview image
for a
given source code file of either plain text
140 or .gcov file format
.
142 -h
, --help Print this help
, then
exit
143 -v
, --version Print version number
, then
exit
144 -t
, --tab
-size TABSIZE Use TABSIZE spaces
in place of tab
145 -w
, --width WIDTH Set width of output image to WIDTH pixel
146 -o
, --output
-filename FILENAME Write image to FILENAME
148 For more information see
: $lcov_url
155 # check_and_load_module(module_name)
157 # Check whether a module by the given name is installed on this system
158 # and make it known to the interpreter if available. Return undefined if it
159 # is installed, an error message otherwise.
162 sub check_and_load_module
($)
170 # genpng_process_file(filename, out_filename, width, tab_size)
173 sub genpng_process_file
($$$$)
175 my $filename = $_[0];
176 my $out_filename = $_[1];
178 my $tab_size = $_[3];
182 open(HANDLE
, "<$filename")
183 or die("ERROR: cannot open $filename!\n");
185 # Check for .gcov filename extension
186 if ($filename =~ /^(.*).gcov$/)
188 # Assume gcov text format
193 # Uninstrumented line
194 push(@source, ":$1");
196 elsif (/^ ###### (.*)$/)
198 # Line with zero execution count
199 push(@source, "0:$1");
201 elsif (/^( *)(\d*) (.*)$/)
203 # Line with positive execution count
204 push(@source, "$2:$3");
211 while (<HANDLE
>) { push(@source, ":$_"); }
215 gen_png
($out_filename, $width, $tab_size, @source);
220 # gen_png(filename, width, tab_size, source)
222 # Write an overview PNG file to FILENAME. Source code is defined by SOURCE
223 # which is a list of lines <count>:<source code> per source code line.
224 # The output image will be made up of one pixel per character of source,
225 # coloring will be done according to execution counts. WIDTH defines the
226 # image width. TAB_SIZE specifies the number of spaces to use as replacement
227 # string for tabulator signs in source code text.
234 my $filename = shift(@_); # Filename for PNG file
235 my $overview_width = shift(@_); # Imagewidth for image
236 my $tab_size = shift(@_); # Replacement string for tab signs
237 my @source = @_; # Source code as passed via argument 2
238 my $height = scalar(@source); # Height as define by source size
239 my $overview; # Source code overview image data
240 my $col_plain_back; # Color for overview background
241 my $col_plain_text; # Color for uninstrumented text
242 my $col_cov_back; # Color for background of covered lines
243 my $col_cov_text; # Color for text of covered lines
244 my $col_nocov_back; # Color for background of lines which
245 # were not covered (count == 0)
246 my $col_nocov_text; # Color for test of lines which were not
247 # covered (count == 0)
248 my $col_hi_back; # Color for background of highlighted lines
249 my $col_hi_text; # Color for text of highlighted lines
250 my $line; # Current line during iteration
251 my $row = 0; # Current row number during iteration
252 my $column; # Current column number during iteration
253 my $color_text; # Current text color during iteration
254 my $color_back; # Current background color during iteration
255 my $last_count; # Count of last processed line
256 my $count; # Count of current line
257 my $source; # Source code of current line
258 my $replacement; # Replacement string for tabulator chars
259 local *PNG_HANDLE
; # Handle for output PNG file
262 $overview = new GD
::Image
($overview_width, $height)
263 or die("ERROR: cannot allocate overview image!\n");
266 $col_plain_back = $overview->colorAllocate(0xff, 0xff, 0xff);
267 $col_plain_text = $overview->colorAllocate(0xaa, 0xaa, 0xaa);
268 $col_cov_back = $overview->colorAllocate(0xaa, 0xa7, 0xef);
269 $col_cov_text = $overview->colorAllocate(0x5d, 0x5d, 0xea);
270 $col_nocov_back = $overview->colorAllocate(0xff, 0x00, 0x00);
271 $col_nocov_text = $overview->colorAllocate(0xaa, 0x00, 0x00);
272 $col_hi_back = $overview->colorAllocate(0x00, 0xff, 0x00);
273 $col_hi_text = $overview->colorAllocate(0x00, 0xaa, 0x00);
275 # Visualize each line
276 foreach $line (@source)
278 # Replace tabs with spaces to keep consistent with source
280 while ($line =~ /^([^\t]*)(\t)/)
282 $replacement = " "x
($tab_size - ((length($1) - 1) %
284 $line =~ s/^([^\t]*)(\t)/$1$replacement/;
287 # Skip lines which do not follow the <count>:<line>
288 # specification, otherwise $1 = count, $2 = source code
289 if (!($line =~ /(\*?)(\d*):(.*)$/)) { next; }
293 # Decide which color pair to use
295 # If this line was not instrumented but the one before was,
296 # take the color of that line to widen color areas in
298 if (($count eq "") && defined($last_count) &&
301 $count = $last_count;
306 # Line was not instrumented
307 $color_text = $col_plain_text;
308 $color_back = $col_plain_back;
312 # Line was instrumented but not executed
313 $color_text = $col_nocov_text;
314 $color_back = $col_nocov_back;
318 # Line was highlighted
319 $color_text = $col_hi_text;
320 $color_back = $col_hi_back;
324 # Line was instrumented and executed
325 $color_text = $col_cov_text;
326 $color_back = $col_cov_back;
329 # Write one pixel for each source character
331 foreach (split("", $source))
334 if ($column >= $overview_width) { last; }
339 $overview->setPixel($column++, $row,
345 $overview->setPixel($column++, $row,
351 while ($column < $overview_width)
353 $overview->setPixel($column++, $row, $color_back);
362 open (PNG_HANDLE
, ">$filename")
363 or die("ERROR: cannot write png file $filename!\n");
364 binmode(*PNG_HANDLE
);
365 print(PNG_HANDLE
$overview->png());
373 warn("$tool_name: $msg");
380 die("$tool_name: $msg");