Debian Codegraph @

lib/Imager/Preprocess.pm

raw
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.003";
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__
208
209 =head1 NAME
210
211 =for stopwords preprocessor
212
213 Imager::Preprocess - simple preprocessor for handling multiple sample sizes
214
215 =head1 SYNOPSIS
216
217 /* in the source: */
218 #code condition true to work with 8-bit samples
219 ... code using preprocessor types/values ...
220 #/code
221
222 # process and make #line directives
223 perl -MImager::Preprocess -epreprocess foo.im foo.c
224
225 # process and no #line directives
226 perl -MImager::Preprocess -epreprocess -l foo.im foo.c
227
228 =head1 DESCRIPTION
229
230 This is a simple preprocessor that aims to reduce duplication of
231 source code when implementing an algorithm both for 8-bit samples and
232 double samples in Imager.
233
234 Imager's C<Makefile.PL> currently scans the F<MANIFEST> for F<.im>
235 files and adds Makefile files to convert these to F<.c> files.
236
237 The beginning of a sample-independent section of code is preceded by:
238
239 #code expression
240
241 where I<expression> should return true if processing should be done at
242 8-bits/sample.
243
244 You can also use a #code block around a function definition to produce
245 8-bit and double sample versions of a function. In this case #code
246 has no expression and you will need to use IM_SUFFIX() to produce
247 different function names.
248
249 The end of a sample-independent section of code is terminated by:
250
251 #/code
252
253 #code sections cannot be nested.
254
255 #/code without a starting #code is an error.
256
257 You can also define extra sample-size dependent macros with C<#!define>:
258
259 =over
260
261 C<#define> I<common-name> I<eight-bit-name> I<floating-point-name>
262
263 =back
264
265 The following types and values are defined in a #code section:
266
267 =over
268
269 =item *
270
271 IM_GPIX(C<im>, C<x>, C<y>, C<&col>)
272
273 =item *
274
275 IM_GLIN(C<im>, C<l>, C<r>, C<y>, C<colors>)
276
277 =item *
278
279 IM_PPIX(C<im>, C<x>, C<y>, C<&col>)
280
281 =item *
282
283 IM_PLIN(C<im>, C<x>, C<y>, C<colors>)
284
285 =item *
286
287 IM_GSAMP(C<im>, C<l>, C<r>, C<y>, C<samples>, C<chans>, C<chan_count>)
288
289 These correspond to the appropriate image function, eg. IM_GPIX()
290 becomes i_gpix() or i_gpixf() as appropriate.
291
292 =item *
293
294 IM_ADAPT_COLORS(C<dest_channels>, C<src_channels>, C<colors>, C<count>)
295
296 Call i_adapt_colors() or i_adapt_fcolors().
297
298 =item *
299
300 IM_FILL_COMBINE(C<fill>) - retrieve the combine function from a fill
301 object.
302
303 =item *
304
305 IM_FILL_FILLER(C<fill>) - retrieve the fill_with_* function from a fill
306 object.
307
308 =item *
309
310 IM_ABS(sample) - calculate the absolute value of an IM_WORK_T value.
311
312 =item *
313
314 IM_SAMPLE_MAX - maximum value for a sample
315
316 =item *
317
318 IM_SAMPLE_MAX2 - maximum value for a sample, squared
319
320 =item *
321
322 IM_SAMPLE_T - type of a sample (i_sample_t or i_fsample_t)
323
324 =item *
325
326 IM_COLOR - color type, either i_color or i_fcolor.
327
328 =item *
329
330 IM_WORK_T - working sample type, either int or double.
331
332 =item *
333
334 IM_Sf - format string for the sample type, C<"%d"> or C<"%f">.
335
336 =item *
337
338 IM_Wf - format string for the work type, C<"%d"> or C<"%f">.
339
340 =item *
341
342 IM_SUFFIX(identifier) - adds _8 or _double onto the end of identifier.
343
344 =item *
345
346 IM_EIGHT_BIT - this is a macro defined only in 8-bit/sample code.
347
348 =back
349
350 Other types, functions and values may be added in the future.
351
352 =head1 AUTHOR
353
354 Tony Cook <[email protected]>
355
356 =cut