1 package Imager::Preprocess;
4 use vars qw(@ISA @EXPORT $VERSION);
6 @EXPORT = qw(preprocess);
13 my $src = shift @ARGV;
14 my $dest = shift @ARGV
18 or die "Cannot open $src: $!\n";
29 "#define IM_ROUND_8(x) ((int)((x)+0.5))\n",
30 "#define IM_ROUND_double(x) (x)\n",
31 "#define IM_LIMIT_8(x) ((x) < 0 ? 0 : (x) > 255 ? 255 : (x))\n",
32 "#define IM_LIMIT_double(x) ((x) < 0.0 ? 0.0 : (x) > 1.0 ? 1.0 : (x))\n",
34 while (defined(my $line = <SRC>)) {
35 if ($line =~ /^\#code\s+(\S.+)$/) {
37 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
44 elsif ($line =~ /^\#code\s*$/) {
46 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
53 elsif ($line =~ /^\#\/code\s*$/) {
55 or do { warn "$src:$.:#/code without #code\n"; ++$failed; next; };
58 push @out, "#line $cond_line \"$src\"\n";
59 push @out, " if ($cond) {\n";
62 "#undef IM_EIGHT_BIT\n",
63 "#define IM_EIGHT_BIT 1\n",
64 "#undef IM_FILL_COMBINE\n",
65 "#define IM_FILL_COMBINE(fill) ((fill)->combine)\n",
66 "#undef IM_FILL_FILLER\n",
67 "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_color)\n";
68 push @out, "#line $code_line \"$src\"\n";
69 push @out, byte_samples(@code);
70 push @out, " }\n", " else {\n"
73 "#undef IM_EIGHT_BIT\n",
74 "#undef IM_FILL_COMBINE\n",
75 "#define IM_FILL_COMBINE(fill) ((fill)->combinef)\n",
76 "#undef IM_FILL_FILLER\n",
77 "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_fcolor)\n";
78 push @out, "#line $code_line \"$src\"\n";
79 push @out, double_samples(@code);
82 push @out, "#line ",$.+1," \"$src\"\n";
95 warn "$src:$code_line:#code block not closed by EOF\n";
102 and die "Errors during parsing, aborting\n";
105 or die "Cannot open $dest: $!\n";
111 # important we make a copy
115 s/\bIM_GPIX\b/i_gpix/g;
116 s/\bIM_GLIN\b/i_glin/g;
117 s/\bIM_PPIX\b/i_ppix/g;
118 s/\bIM_PLIN\b/i_plin/g;
119 s/\bIM_GSAMP\b/i_gsamp/g;
120 s/\bIM_SAMPLE_MAX\b/255/g;
121 s/\bIM_SAMPLE_MAX2\b/65025/g;
122 s/\bIM_SAMPLE_T/i_sample_t/g;
123 s/\bIM_COLOR\b/i_color/g;
124 s/\bIM_WORK_T\b/int/g;
127 s/\bIM_SUFFIX\((\w+)\)/$1_8/g;
128 s/\bIM_ROUND\(/IM_ROUND_8(/g;
129 s/\bIM_ADAPT_COLORS\(/i_adapt_colors(/g;
130 s/\bIM_LIMIT\(/IM_LIMIT_8(/g;
131 s/\bIM_RENDER_LINE\(/i_render_line(/g;
132 s/\bIM_FILL_COMBINE_F\b/i_fill_combine_f/g;
139 # important we make a copy
143 s/\bIM_GPIX\b/i_gpixf/g;
144 s/\bIM_GLIN\b/i_glinf/g;
145 s/\bIM_PPIX\b/i_ppixf/g;
146 s/\bIM_PLIN\b/i_plinf/g;
147 s/\bIM_GSAMP\b/i_gsampf/g;
148 s/\bIM_SAMPLE_MAX\b/1.0/g;
149 s/\bIM_SAMPLE_MAX2\b/1.0/g;
150 s/\bIM_SAMPLE_T/i_fsample_t/g;
151 s/\bIM_COLOR\b/i_fcolor/g;
152 s/\bIM_WORK_T\b/double/g;
155 s/\bIM_SUFFIX\((\w+)\)/$1_double/g;
156 s/\bIM_ROUND\(/IM_ROUND_double(/g;
157 s/\bIM_ADAPT_COLORS\(/i_adapt_fcolors(/g;
158 s/\bIM_LIMIT\(/IM_LIMIT_double(/g;
159 s/\bIM_RENDER_LINE\(/i_render_linef(/g;
160 s/\bIM_FILL_COMBINE_F\b/i_fill_combinef_f/g;
172 =for stopwords preprocessor
174 Imager::Preprocess - simple preprocessor for handling multiple sample sizes
179 #code condition true to work with 8-bit samples
180 ... code using preprocessor types/values ...
183 perl -MImager -epreprocess foo.im foo.c
187 This is a simple preprocessor that aims to reduce duplication of
188 source code when implementing an algorithm both for 8-bit samples and
189 double samples in Imager.
191 Imager's C<Makefile.PL> currently scans the F<MANIFEST> for F<.im>
192 files and adds Makefile files to convert these to F<.c> files.
194 The beginning of a sample-independent section of code is preceded by:
198 where I<expression> should return true if processing should be done at
201 You can also use a #code block around a function definition to produce
202 8-bit and double sample versions of a function. In this case #code
203 has no expression and you will need to use IM_SUFFIX() to produce
204 different function names.
206 The end of a sample-independent section of code is terminated by:
210 #code sections cannot be nested.
212 #/code without a starting #code is an error.
214 The following types and values are defined in a #code section:
220 IM_GPIX(C<im>, C<x>, C<y>, C<&col>)
224 IM_GLIN(C<im>, C<l>, C<r>, C<y>, C<colors>)
228 IM_PPIX(C<im>, C<x>, C<y>, C<&col>)
232 IM_PLIN(C<im>, C<x>, C<y>, C<colors>)
236 IM_GSAMP(C<im>, C<l>, C<r>, C<y>, C<samples>, C<chans>, C<chan_count>)
238 These correspond to the appropriate image function, eg. IM_GPIX()
239 becomes i_gpix() or i_gpixf() as appropriate.
243 IM_ADAPT_COLORS(C<dest_channels>, C<src_channels>, C<colors>, C<count>)
245 Call i_adapt_colors() or i_adapt_fcolors().
249 IM_FILL_COMBINE(C<fill>) - retrieve the combine function from a fill
254 IM_FILL_FILLER(C<fill>) - retrieve the fill_with_* function from a fill
259 IM_SAMPLE_MAX - maximum value for a sample
263 IM_SAMPLE_MAX2 - maximum value for a sample, squared
267 IM_SAMPLE_T - type of a sample (i_sample_t or i_fsample_t)
271 IM_COLOR - color type, either i_color or i_fcolor.
275 IM_WORK_T - working sample type, either int or double.
279 IM_Sf - format string for the sample type, C<"%d"> or C<"%f">.
283 IM_Wf - format string for the work type, C<"%d"> or C<"%f">.
287 IM_SUFFIX(identifier) - adds _8 or _double onto the end of identifier.
291 IM_EIGHT_BIT - this is a macro defined only in 8-bit/sample code.
295 Other types, functions and values may be added in the future.
299 Tony Cook <tony@imager.perl.org>