blib/lib/Imager/Preprocess.pm untested

filter: Statementstmt 0.0% 0 / 137 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 218 CC 16 SCAR 51.6
CC
16
Cov
0%
preprocess
272.0
byte_samples
2.0
double_samples
2.0
CRAP 174.8
1 package Imager::Preprocess;
2 use 5.006;
3 use strict;
4 use warnings;
5 use Exporter "import";
6 use Getopt::Long;
7 use Text::ParseWords;
8  
9 our @EXPORT = qw(preprocess);
10  
11 our $VERSION = "1.004";
12  
13 sub preprocess {
14 unshift @ARGV, grep /^-/, shellwords($ENV{IMAGER_PREPROCESS_OPTS})
15 if $ENV{IMAGER_PREPROCESS_OPTS};
16 my $skip_lines = 0;
17 GetOptions("l" => \$skip_lines)
18 or usage();
19 my $keep_lines = !$skip_lines;
20  
21 my $src = shift @ARGV;
22 my $dest = shift @ARGV
23 or usage();
24  
25 open SRC, "< $src"
26 or die "Cannot open $src: $!\n";
27  
28 my $cond;
29 my $cond_line;
30 my $save_code;
31 my @code;
32 my $code_line;
33 my @out;
34 my $failed;
35 my %replace;
36  
37 push @out,
38 "#define IM_ROUND_8(x) ((int)((x)+0.5))\n",
39 "#define IM_ROUND_double(x) (x)\n",
40 "#define IM_LIMIT_8(x) ((x) < 0 ? 0 : (x) > 255 ? 255 : (x))\n",
41 "#define IM_LIMIT_double(x) ((x) < 0.0 ? 0.0 : (x) > 1.0 ? 1.0 : (x))\n";
42 push @out, "#line 1 \"$src\"\n" if $keep_lines;
43 while (defined(my $line = <SRC>)) {
44 if ($line =~ /^\#code\s+(\S.+)$/) {
45 $save_code
46 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
47  
48 $cond = $1;
49 $cond_line = $.;
50 $code_line = $. + 1;
51 $save_code = 1;
52 }
53 elsif ($line =~ /^\#code\s*$/) {
54 $save_code
55 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
56  
57 $cond = '';
58 $cond_line = 0;
59 $code_line = $. + 1;
60 $save_code = 1;
61 }
62 elsif ($line =~ /^\#\/code\s*$/) {
63 $save_code
64 or do { warn "$src:$.:#/code without #code\n"; ++$failed; next; };
65  
66 if ($cond) {
67 push @out, "#line $cond_line \"$src\"\n" if $keep_lines;
68 push @out, " if ($cond) {\n";
69 }
70 push @out,
71 "#undef IM_EIGHT_BIT\n",
72 "#define IM_EIGHT_BIT 1\n",
73 "#undef IM_FILL_COMBINE\n",
74 "#define IM_FILL_COMBINE(fill) ((fill)->combine)\n",
75 "#undef IM_FILL_FILLER\n",
76 "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_color)\n";
77 for my $key (sort keys %replace) {
78 push @out,
79 "#undef $key\n",
80 "#define $key $replace{$key}[0]\n";
81 }
82 push @out, "#line $code_line \"$src\"\n" if $keep_lines;
83 push @out, byte_samples(@code);
84 push @out, " }\n", " else {\n"
85 if $cond;
86 push @out,
87 "#undef IM_EIGHT_BIT\n",
88 "#undef IM_FILL_COMBINE\n",
89 "#define IM_FILL_COMBINE(fill) ((fill)->combinef)\n",
90 "#undef IM_FILL_FILLER\n",
91 "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_fcolor)\n";
92 for my $key (sort keys %replace) {
93 push @out,
94 "#undef $key\n",
95 "#define $key $replace{$key}[1]\n";
96 }
97 push @out, "#line $code_line \"$src\"\n" if $keep_lines;
98 push @out, double_samples(@code);
99 push @out, " }\n"
100 if $cond;
101 push @out, "#line ",$.+1," \"$src\"\n" if $keep_lines;
102 @code = ();
103 $save_code = 0;
104 }
105 elsif ($save_code) {
106 push @code, $line;
107 }
108 elsif ($line =~ /^\#!\s*define\s+(\w+)\s+(\w+)\s+(\w+)\s*$/) {
109 exists $replace{$1}
110 and die "Duplicate definition of $1\n";
111 $replace{$1} = [ $2, $3 ];
112 }
113 else {
114 push @out, $line;
115 }
116 }
117  
118 if ($save_code) {
119 warn "$src:$code_line:#code block not closed by EOF\n";
120 ++$failed;
121 }
122  
123 close SRC;
124  
125 $failed
126 and die "Errors during parsing, aborting\n";
127  
128 open DEST, "> $dest"
129 or die "Cannot open $dest: $!\n";
130 print DEST @out;
131 close DEST;
132 }
133  
134 sub byte_samples {
135 # important we make a copy
136 my @lines = @_;
137  
138 for (@lines) {
139 s/\bIM_GPIX\b/i_gpix/g;
140 s/\bIM_GLIN\b/i_glin/g;
141 s/\bIM_PPIX\b/i_ppix/g;
142 s/\bIM_PLIN\b/i_plin/g;
143 s/\bIM_GSAMP\b/i_gsamp/g;
144 s/\bIM_PSAMP\b/i_psamp/g;
145 s/\bIM_SAMPLE_MAX\b/255/g;
146 s/\bIM_SAMPLE_MAX2\b/65025/g;
147 s/\bIM_SAMPLE_T/i_sample_t/g;
148 s/\bIM_COLOR\b/i_color/g;
149 s/\bIM_WORK_T\b/int/g;
150 s/\bIM_Sf\b/"%d"/g;
151 s/\bIM_Wf\b/"%d"/g;
152 s/\bIM_SUFFIX\((\w+)\)/$1_8/g;
153 s/\bIM_ROUND\(/IM_ROUND_8(/g;
154 s/\bIM_ADAPT_COLORS\(/i_adapt_colors(/g;
155 s/\bIM_LIMIT\(/IM_LIMIT_8(/g;
156 s/\bIM_RENDER_LINE\(/i_render_line(/g;
157 s/\bIM_FILL_COMBINE_F\b/i_fill_combine_f/g;
158 s/\bIM_ABS\b/abs/g;
159 }
160  
161 @lines;
162 }
163  
164 sub double_samples {
165 # important we make a copy
166 my @lines = @_;
167  
168 for (@lines) {
169 s/\bIM_GPIX\b/i_gpixf/g;
170 s/\bIM_GLIN\b/i_glinf/g;
171 s/\bIM_PPIX\b/i_ppixf/g;
172 s/\bIM_PLIN\b/i_plinf/g;
173 s/\bIM_GSAMP\b/i_gsampf/g;
174 s/\bIM_PSAMP\b/i_psampf/g;
175 s/\bIM_SAMPLE_MAX\b/1.0/g;
176 s/\bIM_SAMPLE_MAX2\b/1.0/g;
177 s/\bIM_SAMPLE_T/i_fsample_t/g;
178 s/\bIM_COLOR\b/i_fcolor/g;
179 s/\bIM_WORK_T\b/double/g;
180 s/\bIM_Sf\b/"%f"/g;
181 s/\bIM_Wf\b/"%f"/g;
182 s/\bIM_SUFFIX\((\w+)\)/$1_double/g;
183 s/\bIM_ROUND\(/IM_ROUND_double(/g;
184 s/\bIM_ADAPT_COLORS\(/i_adapt_fcolors(/g;
185 s/\bIM_LIMIT\(/IM_LIMIT_double(/g;
186 s/\bIM_RENDER_LINE\(/i_render_linef(/g;
187 s/\bIM_FILL_COMBINE_F\b/i_fill_combinef_f/g;
188 s/\bIM_ABS\b/fabs/g;
189 }
190  
191 @lines;
192 }
193  
194 sub usage {
195 die <<EOS;
196 Usage: perl -MImager::Preprocess -epreprocess [-l] infile outfile
197 -l don't produce #line directives
198 infile - input file
199 outfile output file
200  
201 See perldoc Imager::Preprocess for details.
202 EOS
203 }
204  
205 1;
206  
207 __END__