1 package Imager::Preprocess;
4 use vars qw(@ISA @EXPORT $VERSION);
8 @EXPORT = qw(preprocess);
14 unshift @ARGV, grep /^-/, shellwords($ENV{IMAGER_PREPROCESS_OPTS})
15 if $ENV{IMAGER_PREPROCESS_OPTS};
17 GetOptions("l" => \$skip_lines)
19 my $keep_lines = !$skip_lines;
21 my $src = shift @ARGV;
22 my $dest = shift @ARGV
26 or die "Cannot open $src: $!\n";
37 "#define IM_ROUND_8(x) ((int)((x)+0.5))\n",
38 "#define IM_ROUND_double(x) (x)\n",
39 "#define IM_LIMIT_8(x) ((x) < 0 ? 0 : (x) > 255 ? 255 : (x))\n",
40 "#define IM_LIMIT_double(x) ((x) < 0.0 ? 0.0 : (x) > 1.0 ? 1.0 : (x))\n";
41 push @out, "#line 1 \"$src\"\n" if $keep_lines;
42 while (defined(my $line = <SRC>)) {
43 if ($line =~ /^\#code\s+(\S.+)$/) {
45 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
52 elsif ($line =~ /^\#code\s*$/) {
54 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
61 elsif ($line =~ /^\#\/code\s*$/) {
63 or do { warn "$src:$.:#/code without #code\n"; ++$failed; next; };
66 push @out, "#line $cond_line \"$src\"\n" if $keep_lines;
67 push @out, " if ($cond) {\n";
70 "#undef IM_EIGHT_BIT\n",
71 "#define IM_EIGHT_BIT 1\n",
72 "#undef IM_FILL_COMBINE\n",
73 "#define IM_FILL_COMBINE(fill) ((fill)->combine)\n",
74 "#undef IM_FILL_FILLER\n",
75 "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_color)\n";
76 push @out, "#line $code_line \"$src\"\n" if $keep_lines;
77 push @out, byte_samples(@code);
78 push @out, " }\n", " else {\n"
81 "#undef IM_EIGHT_BIT\n",
82 "#undef IM_FILL_COMBINE\n",
83 "#define IM_FILL_COMBINE(fill) ((fill)->combinef)\n",
84 "#undef IM_FILL_FILLER\n",
85 "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_fcolor)\n";
86 push @out, "#line $code_line \"$src\"\n" if $keep_lines;
87 push @out, double_samples(@code);
90 push @out, "#line ",$.+1," \"$src\"\n" if $keep_lines;
103 warn "$src:$code_line:#code block not closed by EOF\n";
110 and die "Errors during parsing, aborting\n";
113 or die "Cannot open $dest: $!\n";
119 # important we make a copy
123 s/\bIM_GPIX\b/i_gpix/g;
124 s/\bIM_GLIN\b/i_glin/g;
125 s/\bIM_PPIX\b/i_ppix/g;
126 s/\bIM_PLIN\b/i_plin/g;
127 s/\bIM_GSAMP\b/i_gsamp/g;
128 s/\bIM_PSAMP\b/i_psamp/g;
129 s/\bIM_SAMPLE_MAX\b/255/g;
130 s/\bIM_SAMPLE_MAX2\b/65025/g;
131 s/\bIM_SAMPLE_T/i_sample_t/g;
132 s/\bIM_COLOR\b/i_color/g;
133 s/\bIM_WORK_T\b/int/g;
136 s/\bIM_SUFFIX\((\w+)\)/$1_8/g;
137 s/\bIM_ROUND\(/IM_ROUND_8(/g;
138 s/\bIM_ADAPT_COLORS\(/i_adapt_colors(/g;
139 s/\bIM_LIMIT\(/IM_LIMIT_8(/g;
140 s/\bIM_RENDER_LINE\(/i_render_line(/g;
141 s/\bIM_FILL_COMBINE_F\b/i_fill_combine_f/g;
148 # important we make a copy
152 s/\bIM_GPIX\b/i_gpixf/g;
153 s/\bIM_GLIN\b/i_glinf/g;
154 s/\bIM_PPIX\b/i_ppixf/g;
155 s/\bIM_PLIN\b/i_plinf/g;
156 s/\bIM_GSAMP\b/i_gsampf/g;
157 s/\bIM_PSAMP\b/i_psampf/g;
158 s/\bIM_SAMPLE_MAX\b/1.0/g;
159 s/\bIM_SAMPLE_MAX2\b/1.0/g;
160 s/\bIM_SAMPLE_T/i_fsample_t/g;
161 s/\bIM_COLOR\b/i_fcolor/g;
162 s/\bIM_WORK_T\b/double/g;
165 s/\bIM_SUFFIX\((\w+)\)/$1_double/g;
166 s/\bIM_ROUND\(/IM_ROUND_double(/g;
167 s/\bIM_ADAPT_COLORS\(/i_adapt_fcolors(/g;
168 s/\bIM_LIMIT\(/IM_LIMIT_double(/g;
169 s/\bIM_RENDER_LINE\(/i_render_linef(/g;
170 s/\bIM_FILL_COMBINE_F\b/i_fill_combinef_f/g;
178 Usage: perl -MImager::Preprocess -epreprocess [-l] infile outfile
179 -l don't produce #line directives
183 See perldoc Imager::Preprocess for details.
193 =for stopwords preprocessor
195 Imager::Preprocess - simple preprocessor for handling multiple sample sizes
200 #code condition true to work with 8-bit samples
201 ... code using preprocessor types/values ...
204 # process and make #line directives
205 perl -MImager::Preprocess -epreprocess foo.im foo.c
207 # process and no #line directives
208 perl -MImager::Preprocess -epreprocess -l foo.im foo.c
212 This is a simple preprocessor that aims to reduce duplication of
213 source code when implementing an algorithm both for 8-bit samples and
214 double samples in Imager.
216 Imager's C<Makefile.PL> currently scans the F<MANIFEST> for F<.im>
217 files and adds Makefile files to convert these to F<.c> files.
219 The beginning of a sample-independent section of code is preceded by:
223 where I<expression> should return true if processing should be done at
226 You can also use a #code block around a function definition to produce
227 8-bit and double sample versions of a function. In this case #code
228 has no expression and you will need to use IM_SUFFIX() to produce
229 different function names.
231 The end of a sample-independent section of code is terminated by:
235 #code sections cannot be nested.
237 #/code without a starting #code is an error.
239 The following types and values are defined in a #code section:
245 IM_GPIX(C<im>, C<x>, C<y>, C<&col>)
249 IM_GLIN(C<im>, C<l>, C<r>, C<y>, C<colors>)
253 IM_PPIX(C<im>, C<x>, C<y>, C<&col>)
257 IM_PLIN(C<im>, C<x>, C<y>, C<colors>)
261 IM_GSAMP(C<im>, C<l>, C<r>, C<y>, C<samples>, C<chans>, C<chan_count>)
263 These correspond to the appropriate image function, eg. IM_GPIX()
264 becomes i_gpix() or i_gpixf() as appropriate.
268 IM_ADAPT_COLORS(C<dest_channels>, C<src_channels>, C<colors>, C<count>)
270 Call i_adapt_colors() or i_adapt_fcolors().
274 IM_FILL_COMBINE(C<fill>) - retrieve the combine function from a fill
279 IM_FILL_FILLER(C<fill>) - retrieve the fill_with_* function from a fill
284 IM_SAMPLE_MAX - maximum value for a sample
288 IM_SAMPLE_MAX2 - maximum value for a sample, squared
292 IM_SAMPLE_T - type of a sample (i_sample_t or i_fsample_t)
296 IM_COLOR - color type, either i_color or i_fcolor.
300 IM_WORK_T - working sample type, either int or double.
304 IM_Sf - format string for the sample type, C<"%d"> or C<"%f">.
308 IM_Wf - format string for the work type, C<"%d"> or C<"%f">.
312 IM_SUFFIX(identifier) - adds _8 or _double onto the end of identifier.
316 IM_EIGHT_BIT - this is a macro defined only in 8-bit/sample code.
320 Other types, functions and values may be added in the future.
324 Tony Cook <tonyc@cpan.org>