Debian Codegraph @

bin/genpng

raw
1 #!/usr/bin/env perl
2 #
3 # Copyright (c) International Business Machines Corp., 2002
4 #
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.
9 #
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.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, see
17 # <http://www.gnu.org/licenses/>.
18 #
19 #
20 # genpng
21 #
22 # This script creates an overview PNG image of a source code file by
23 # representing each source code character by a single pixel.
24 #
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
27 #
28 # History:
29 # 2002-08-26: created by Peter Oberparleiter <[email protected]>
30 #
31
32 use strict;
33 use warnings;
34 use File::Basename;
35 use Getopt::Long;
36 use Cwd qw/abs_path/;
37 use FindBin;
38
39 use lib "$FindBin::RealBin/../lib";
40 use lcovutil qw (%tlaColor %tlaTextColor
41 $tool_name $tool_dir $lcov_version $lcov_url
42 die_handler warn_handler);
43
44 # Constants
45 # (now imported from lcovutil.pm)
46
47 # Prototypes
48 sub gen_png($$$$$@);
49 sub check_and_load_module($);
50 sub genpng_print_usage(*);
51 sub genpng_process_file($$$$$);
52
53 #
54 # Code entry point
55 #
56
57 # Check whether required module GD.pm is installed
58 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).
63 END_OF_TEXT
64 ;
65 exit(2);
66 }
67
68 # Check whether we're called from the command line or from another script
69 if (!caller) {
70 my $filename;
71 my $tab_size = 4;
72 my $width = 80;
73 my $dark = 0;
74 my $out_filename;
75 my $help;
76 my $version;
77
78 $SIG{__WARN__} = \&warn_handler;
79 $SIG{__DIE__} = \&die_handler;
80
81 # Parse command line options
82 if (!GetOptions("tab-size=i" => \$tab_size,
83 "width=i" => \$width,
84 "output-filename=s" => \$out_filename,
85 "dark-mode" => \$dark,
86 "help" => \$help,
87 "version" => \$version
88 )) {
89 print(STDERR "Use $tool_name --help to get usage information\n");
90 exit(1);
91 }
92
93 $filename = $ARGV[0];
94
95 # Check for help flag
96 if ($help) {
97 genpng_print_usage(*STDOUT);
98 exit(0);
99 }
100
101 # Check for version flag
102 if ($version) {
103 print("$tool_name: $lcov_version\n");
104 exit(0);
105 }
106
107 # Check options
108 if (!$filename) {
109 die("No filename specified\n");
110 }
111
112 # Check for output filename
113 if (!$out_filename) {
114 $out_filename = "$filename.png";
115 }
116
117 genpng_process_file($filename, $out_filename, $width, $tab_size, $dark);
118 exit(0);
119 }
120
121 #
122 # genpng_print_usage(handle)
123 #
124 # Write out command line usage information to given filehandle.
125 #
126
127 sub genpng_print_usage(*)
128 {
129 local *HANDLE = $_[0];
130
131 print(HANDLE <<END_OF_USAGE)
132 Usage: $tool_name [OPTIONS] SOURCEFILE
133
134 Create an overview image for a given source code file of either plain text
135 or .gcov file format.
136
137 OPTIONS
138 -h, --help Print this help, then exit
139 --version Print version number, then exit
140 -t, --tab-size TABSIZE Use TABSIZE spaces in place of tab
141 -w, --width WIDTH Set width of output image to WIDTH pixel
142 -d, --dark-mode Use a light-on-dark color scheme
143 -o, --output-filename FILENAME Write image to FILENAME
144
145 For more information see the genpng man page.
146 END_OF_USAGE
147 ;
148 }
149
150 #
151 # check_and_load_module(module_name)
152 #
153 # Check whether a module by the given name is installed on this system
154 # and make it known to the interpreter if available. Return undefined if it
155 # is installed, an error message otherwise.
156 #
157
158 sub check_and_load_module($)
159 {
160 eval("use $_[0];");
161 return $@;
162 }
163
164 #
165 # genpng_process_file(filename, out_filename, width, tab_size, dark)
166 #
167
168 sub genpng_process_file($$$$$)
169 {
170 my $filename = $_[0];
171 my $out_filename = $_[1];
172 my $width = $_[2];
173 my $tab_size = $_[3];
174 my $dark = $_[4];
175 local *HANDLE;
176 my @source;
177
178 open(HANDLE, "<", $filename) or
179 die("cannot open $filename!\n");
180
181 # Check for .gcov filename extension
182 if ($filename =~ /^(.*).gcov$/) {
183 # Assume gcov text format
184 while (<HANDLE>) {
185 if (/^\t\t(.*)$/) {
186 # Uninstrumented line
187 push(@source, ":$1");
188 } elsif (/^ ###### (.*)$/) {
189 # Line with zero execution count
190 push(@source, "0:$1");
191 } elsif (/^( *)(\d*) (.*)$/) {
192 # Line with positive execution count
193 push(@source, "$2:$3");
194 }
195 }
196 } else {
197 # Plain text file
198 while (<HANDLE>) { push(@source, ":$_"); }
199 }
200 close(HANDLE) or die("unable to close $filename: $!\n");
201
202 my $show_tla = 1;
203 gen_png($out_filename, $show_tla, $dark, $width, $tab_size, @source);
204 }
205
206 #
207 # gen_png(filename, show_tla, dark, width, tab_size, source)
208 #
209 # Write an overview PNG file to FILENAME. Source code is defined by SOURCE
210 # which is a list of lines <count>:<source code> per source code line.
211 # The output image will be made up of one pixel per character of source,
212 # coloring will be done according to execution counts. WIDTH defines the
213 # image width. TAB_SIZE specifies the number of spaces to use as replacement
214 # string for tabulator signs in source code text.
215 #
216 # Die on error.
217 #
218
219 sub gen_png($$$$$@)
220 {
221 my $filename = shift(@_); # Filename for PNG file
222 my $show_tla = shift(@_); # differential categories
223 my $dark_mode = shift(@_); # dark-on-light, if set
224 my $overview_width = shift(@_); # Imagewidth for image
225 my $tab_size = shift(@_); # Replacement string for tab signs
226 my @source = @_; # Source code as passed via argument 2
227 my $height; # Height as define by source size
228 my $overview; # Source code overview image data
229 my $col_plain_back; # Color for overview background
230 my $col_plain_text; # Color for uninstrumented text
231 my $col_cov_back; # Color for background of covered lines
232 my $col_cov_text; # Color for text of covered lines
233 my $col_nocov_back; # Color for background of lines which
234 # were not covered (count == 0)
235 my $col_nocov_text; # Color for test of lines which were not
236 # covered (count == 0)
237 my $col_hi_back; # Color for background of highlighted lines
238 my $col_hi_text; # Color for text of highlighted lines
239
240 my %col_tla_back; # Color for background of TLA lines
241 my %col_tla_text; # Color for text of TLA lines
242
243 my $line; # Current line during iteration
244 my $row = 0; # Current row number during iteration
245 my $column; # Current column number during iteration
246 my $color_text; # Current text color during iteration
247 my $color_back; # Current background color during iteration
248 my $last_tag; # Tag of last processed line
249 my $tag; # Tag of current line
250 my $last_count; # Count of last processed line
251 my $count; # Count of current line
252 my $source; # Source code of current line
253 my $replacement; # Replacement string for tabulator chars
254 local *PNG_HANDLE; # Handle for output PNG file
255
256 # Handle empty source files
257 if (!@source) {
258 @source = ("");
259 }
260 $height = scalar(@source);
261 # Create image
262 $overview = new GD::Image($overview_width, $height) or
263 die("cannot allocate overview image!\n");
264
265 # Define colors
266 # overview->colorAllocate(red, green, blue)
267 if ($dark_mode) {
268 # just reverse foreground and background
269 # there is probably a better color scheme than this.
270 $col_plain_text =
271 $overview->colorAllocate(0xaa, 0xaa, 0xaa); # light grey
272 $col_plain_back = $overview->colorAllocate(0x00, 0x00, 0x00);
273 $col_cov_text = $overview->colorAllocate(0xaa, 0xa7, 0xef);
274 $col_cov_back = $overview->colorAllocate(0x5d, 0x5d, 0xea);
275 $col_nocov_text = $overview->colorAllocate(0xff, 0x00, 0x00);
276 $col_nocov_back = $overview->colorAllocate(0xaa, 0x00, 0x00);
277 $col_hi_text = $overview->colorAllocate(0x00, 0xff, 0x00);
278 $col_hi_back = $overview->colorAllocate(0x00, 0xaa, 0x00);
279 } else {
280 $col_plain_back = $overview->colorAllocate(0xff, 0xff, 0xff);
281 $col_plain_text = $overview->colorAllocate(0xaa, 0xaa, 0xaa);
282 $col_cov_back = $overview->colorAllocate(0xaa, 0xa7, 0xef);
283 $col_cov_text = $overview->colorAllocate(0x5d, 0x5d, 0xea);
284 $col_nocov_back = $overview->colorAllocate(0xff, 0x00, 0x00);
285 $col_nocov_text = $overview->colorAllocate(0xaa, 0x00, 0x00);
286 $col_hi_back = $overview->colorAllocate(0x00, 0xff, 0x00);
287 $col_hi_text = $overview->colorAllocate(0x00, 0xaa, 0x00);
288 }
289
290 foreach my $tla (keys(%lcovutil::tlaColor)) {
291 next if 'D' eq substr($tla, 0, 1); # skip deleted TLAs..they don't apper
292
293 if ($show_tla) {
294 my $text = $tlaTextColor{$tla};
295 my $back = $tlaColor{$tla};
296 $text =~ s/^#/0x/;
297 $back =~ s/^#/0x/;
298 $text = hex($text);
299 $back = hex($back);
300
301 $col_tla_back{$tla} =
302 $overview->colorAllocate($back >> 16,
303 ($back >> 8) & 0xFF,
304 $back & 0xFF);
305 $col_tla_text{$tla} =
306 $overview->colorAllocate($text >> 16,
307 ($text >> 8) & 0xFF,
308 $text & 0xFF);
309 } else {
310 # no differential categories...use vanilla colors
311 if (grep(/^$tla$/, ("GBC", "CBC", "GIC", "GNC"))) {
312 $col_tla_back{$tla} = $col_cov_back;
313 $col_tla_text{$tla} = $col_cov_text;
314 } elsif (grep(/^$tla$/, ("ECB", "EUB"))) {
315 $col_tla_back{$tla} = $col_plain_back;
316 $col_tla_text{$tla} = $col_plain_text;
317 } else {
318 $col_tla_back{$tla} = $col_nocov_back;
319 $col_tla_text{$tla} = $col_nocov_text;
320 }
321 }
322 }
323 # Visualize each line
324 foreach $line (@source) {
325 # Replace tabs with spaces to keep consistent with source
326 # code view
327 while ($line =~ /^([^\t]*)(\t)/) {
328 $replacement = " " x ($tab_size - ((length($1) - 1) % $tab_size));
329 $line =~ s/^([^\t]*)(\t)/$1$replacement/;
330 }
331 # Skip lines which do not follow the <count>:<line>
332 # specification, otherwise $1 = count, $2 = source code
333 if (!($line =~ /([-+<>=]?)(\d*):(.*)$/)) { next; }
334 $tag = $1;
335 $count = $2;
336 $source = $3;
337
338 # Decide which color pair to use
339 my $tla = undef;
340
341 # If this line was not instrumented but the one before was,
342 # take the color of that line to widen color areas in
343 # resulting image
344 if (($count eq "") &&
345 defined($last_count) &&
346 ($last_count ne "")) {
347 $tag = $last_tag;
348 $count = $last_count;
349 }
350
351 if ($tag eq "" && $count eq "") {
352 # Line was not instrumented
353 $color_text = $col_plain_text;
354 $color_back = $col_plain_back;
355 } else {
356 die("unexpected PNG tag '$tag'")
357 unless exists($lcovutil::pngMap{$tag});
358 # index '1' if not covered (count is zero)
359 $tla = $lcovutil::pngMap{$tag}[$count == 0];
360 }
361
362 if (defined($tla)) {
363 $color_text = $col_tla_text{$tla};
364 $color_back = $col_tla_back{$tla};
365 }
366 # Write one pixel for each source character
367 $column = 0;
368 foreach (split("", $source)) {
369 # Check for width
370 if ($column >= $overview_width) { last; }
371
372 if ($_ eq " ") {
373 # Space
374 $overview->setPixel($column++, $row, $color_back);
375 } else {
376 # Text
377 $overview->setPixel($column++, $row, $color_text);
378 }
379 }
380
381 # Fill rest of line
382 while ($column < $overview_width) {
383 $overview->setPixel($column++, $row, $color_back);
384 }
385
386 $last_tag = $1;
387 $last_count = $2;
388
389 $row++;
390 }
391
392 # Write PNG file
393 open(PNG_HANDLE, ">", $filename) or
394 die("cannot write png file $filename!\n");
395 binmode(*PNG_HANDLE);
396 print(PNG_HANDLE $overview->png());
397 close(PNG_HANDLE) or die("unable to close $filename: $!\n");
398 }