1 package Imager::Preprocess;
4 use vars qw(@ISA @EXPORT);
6 @EXPORT = qw(preprocess);
11 my $src = shift @ARGV;
12 my $dest = shift @ARGV
16 or die "Cannot open $src: $!\n";
27 "#define IM_ROUND_8(x) ((int)((x)+0.5))\n",
28 "#define IM_ROUND_double(x) (x)\n",
29 "#define IM_LIMIT_8(x) ((x) < 0 ? 0 : (x) > 255 ? 255 : (x))\n",
30 "#define IM_LIMIT_double(x) ((x) < 0.0 ? 0.0 : (x) > 1.0 ? 1.0 : (x))\n",
32 while (defined(my $line = <SRC>)) {
33 if ($line =~ /^\#code\s+(\S.+)$/) {
35 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
42 elsif ($line =~ /^\#code\s*$/) {
44 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
51 elsif ($line =~ /^\#\/code$/) {
53 or do { warn "$src:$.:#/code without #code\n"; ++$failed; next; };
56 push @out, "#line $cond_line \"$src\"\n";
57 push @out, " if ($cond) {\n";
60 "#undef IM_EIGHT_BIT\n",
61 "#define IM_EIGHT_BIT 1\n",
62 "#undef IM_FILL_COMBINE\n",
63 "#define IM_FILL_COMBINE(fill) ((fill)->combine)\n",
64 "#undef IM_FILL_FILLER\n",
65 "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_color)\n";
66 push @out, "#line $code_line \"$src\"\n";
67 push @out, byte_samples(@code);
68 push @out, " }\n", " else {\n"
71 "#undef IM_EIGHT_BIT\n",
72 "#undef IM_FILL_COMBINE\n",
73 "#define IM_FILL_COMBINE(fill) ((fill)->combinef)\n",
74 "#undef IM_FILL_FILLER\n",
75 "#define IM_FILL_FILLER(fill) ((fill)->f_fill_with_fcolor)\n";
76 push @out, "#line $code_line \"$src\"\n";
77 push @out, double_samples(@code);
80 push @out, "#line ",$.+1," \"$src\"\n";
93 warn "$src:$code_line:#code block not closed by EOF\n";
100 and die "Errors during parsing, aborting\n";
103 or die "Cannot open $dest: $!\n";
109 # important we make a copy
113 s/\bIM_GPIX\b/i_gpix/g;
114 s/\bIM_GLIN\b/i_glin/g;
115 s/\bIM_PPIX\b/i_ppix/g;
116 s/\bIM_PLIN\b/i_plin/g;
117 s/\bIM_GSAMP\b/i_gsamp/g;
118 s/\bIM_SAMPLE_MAX\b/255/g;
119 s/\bIM_SAMPLE_MAX2\b/65025/g;
120 s/\bIM_SAMPLE_T/i_sample_t/g;
121 s/\bIM_COLOR\b/i_color/g;
122 s/\bIM_WORK_T\b/int/g;
125 s/\bIM_SUFFIX\((\w+)\)/$1_8/g;
126 s/\bIM_ROUND\(/IM_ROUND_8(/g;
127 s/\bIM_ADAPT_COLORS\(/i_adapt_colors(/g;
128 s/\bIM_LIMIT\(/IM_LIMIT_8(/g;
129 s/\bIM_RENDER_LINE\(/i_render_line(/g;
130 s/\bIM_FILL_COMBINE_F\b/i_fill_combine_f/g;
137 # important we make a copy
141 s/\bIM_GPIX\b/i_gpixf/g;
142 s/\bIM_GLIN\b/i_glinf/g;
143 s/\bIM_PPIX\b/i_ppixf/g;
144 s/\bIM_PLIN\b/i_plinf/g;
145 s/\bIM_GSAMP\b/i_gsampf/g;
146 s/\bIM_SAMPLE_MAX\b/1.0/g;
147 s/\bIM_SAMPLE_MAX2\b/1.0/g;
148 s/\bIM_SAMPLE_T/i_fsample_t/g;
149 s/\bIM_COLOR\b/i_fcolor/g;
150 s/\bIM_WORK_T\b/double/g;
153 s/\bIM_SUFFIX\((\w+)\)/$1_double/g;
154 s/\bIM_ROUND\(/IM_ROUND_double(/g;
155 s/\bIM_ADAPT_COLORS\(/i_adapt_fcolors(/g;
156 s/\bIM_LIMIT\(/IM_LIMIT_double(/g;
157 s/\bIM_RENDER_LINE\(/i_render_linef(/g;
158 s/\bIM_FILL_COMBINE_F\b/i_fill_combinef_f/g;
170 Imager::Preprocess - simple preprocessor for handling multiple sample sizes
175 #code condition true to work with 8-bit samples
176 ... code using preprocessor types/values ...
179 perl -MImager -epreprocess foo.im foo.c
183 This is a simple preprocessor that aims to reduce duplication of
184 source code when implementing an algorithm both for 8-bit samples and
185 double samples in Imager.
187 Imager's Makefile.PL currently scans the MANIFEST for .im files and
188 adds Makefile files to convert these to .c files.
190 The beginning of a sample-independent section of code is preceded by:
194 where I<expression> should return true if processing should be done at
197 You can also use a #code block around a function definition to produce
198 8-bit and double sample versions of a function. In this case #code
199 has no expression and you will need to use IM_SUFFIX() to produce
200 different function names.
202 The end of a sample-independent section of code is terminated by:
206 #code sections cannot be nested.
208 #/code without a starting #code is an error.
210 The following types and values are defined in a #code section:
216 IM_GPIX(im, x, y, &col)
220 IM_GLIN(im, l, r, y, colors)
224 IM_PPIX(im, x, y, &col)
228 IM_PLIN(im, x, y, colors)
232 IM_GSAMP(im, l, r, y, samples, chans, chan_count)
234 These correspond to the appropriate image function, eg. IM_GPIX()
235 becomes i_gpix() or i_gpixf() as appropriate.
239 IM_ADAPT_COLORS(dest_channes, src_channels, colors, count)
241 Call i_adapt_colors() or i_adapt_fcolors().
245 IM_FILL_COMBINE(fill) - retrieve the combine function from a fill
250 IM_FILL_FILLER(fill) - retrieve the fill_with_* function from a fill
255 IM_SAMPLE_MAX - maximum value for a sample
259 IM_SAMPLE_MAX2 - maximum value for a sample, squared
263 IM_SAMPLE_T - type of a sample (i_sample_t or i_fsample_t)
267 IM_COLOR - color type, either i_color or i_fcolor.
271 IM_WORK_T - working sample type, either int or double.
275 IM_Sf - format string for the sample type, C<"%d"> or C<"%f">.
279 IM_Wf - format string for the work type, C<"%d"> or C<"%f">.
283 IM_SUFFIX(identifier) - adds _8 or _double onto the end of identifier.
287 IM_EIGHT_BIT - this is a macro defined only in 8-bit/sample code.
291 Other types, functions and values may be added in the future.
295 Tony Cook <tony@imager.perl.org>