9 or die "Cannot open $src: $!\n";
20 "#define IM_ROUND_8(x) ((int)((x)+0.5))\n",
21 "#define IM_ROUND_double(x) (x)\n",
22 "#define IM_LIMIT_8(x) ((x) < 0 ? 0 : (x) > 255 ? 255 : (x))\n",
23 "#define IM_LIMIT_double(x) ((x) < 0.0 ? 0.0 : (x) > 1.0 ? 1.0 : (x))\n",
25 while (defined(my $line = <SRC>)) {
26 if ($line =~ /^\#code\s+(\S.+)$/) {
28 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
35 elsif ($line =~ /^\#code\s*$/) {
37 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
44 elsif ($line =~ /^\#\/code$/) {
46 or do { warn "$src:$.:#/code without #code\n"; ++$failed; next; };
49 push @out, "#line $cond_line \"$src\"\n";
50 push @out, " if ($cond) {\n";
52 push @out, "#undef IM_EIGHT_BIT\n",
53 "#define IM_EIGHT_BIT 1\n";
54 push @out, "#line $code_line \"$src\"\n";
55 push @out, byte_samples(@code);
56 push @out, " }\n", " else {\n"
58 push @out, "#undef IM_EIGHT_BIT\n";
59 push @out, "#line $code_line \"$src\"\n";
60 push @out, double_samples(@code);
63 push @out, "#line $. \"$src\"\n";
76 warn "$src:$code_line:#code block not closed by EOF\n";
83 and die "Errors during parsing, aborting\n";
86 or die "Cannot open $dest: $!\n";
91 # important we make a copy
95 s/\bIM_GPIX\b/i_gpix/g;
96 s/\bIM_GLIN\b/i_glin/g;
97 s/\bIM_PPIX\b/i_ppix/g;
98 s/\bIM_PLIN\b/i_plin/g;
99 s/\bIM_GSAMP\b/i_gsamp/g;
100 s/\bIM_SAMPLE_MAX\b/255/g;
101 s/\bIM_SAMPLE_T/i_sample_t/g;
102 s/\bIM_COLOR\b/i_color/g;
103 s/\bIM_WORK_T\b/int/g;
106 s/\bIM_SUFFIX\((\w+)\)/$1_8/g;
107 s/\bIM_ROUND\(/IM_ROUND_8(/g;
108 s/\bIM_LIMIT\(/IM_LIMIT_8(/g;
115 # important we make a copy
119 s/\bIM_GPIX\b/i_gpixf/g;
120 s/\bIM_GLIN\b/i_glinf/g;
121 s/\bIM_PPIX\b/i_ppixf/g;
122 s/\bIM_PLIN\b/i_plinf/g;
123 s/\bIM_GSAMP\b/i_gsampf/g;
124 s/\bIM_SAMPLE_MAX\b/1.0/g;
125 s/\bIM_SAMPLE_T/i_fsample_t/g;
126 s/\bIM_COLOR\b/i_fcolor/g;
127 s/\bIM_WORK_T\b/double/g;
130 s/\bIM_SUFFIX\((\w+)\)/$1_double/g;
131 s/\bIM_ROUND\(/IM_ROUND_double(/g;
132 s/\bIM_LIMIT\(/IM_LIMIT_double(/g;
140 imtoc.perl - simple preprocessor for handling multiple sample sizes
145 #code condition true to work with 8-bit samples
146 ... code using preprocessor types/values ...
149 perl imtoc.perl foo.im foo.c
153 This is a simple preprocessor that aims to reduce duplication of
154 source code when implementing an algorithm both for 8-bit samples and
155 double samples in Imager.
157 Imager's Makefile.PL currently scans the MANIFEST for .im files and
158 adds Makefile files to convert these to .c files.
160 The beginning of a sample-independent section of code is preceded by:
164 where I<expression> should return true if processing should be done at
167 You can also use a #code block around a function definition to produce
168 8-bit and double sample versions of a function. In this case #code
169 has no expression and you will need to use IM_SUFFIX() to produce
170 different function names.
172 The end of a sample-independent section of code is terminated by:
176 #code sections cannot be nested.
178 #/code without a starting #code is an error.
180 The following types and values are defined in a #code section:
186 IM_GPIX(im, x, y, &col)
190 IM_GLIN(im, l, r, y, colors)
194 IM_PPIX(im, x, y, &col)
198 IM_PLIN(im, x, y, colors)
202 IM_GSAMP(im, l, r, y, samples, chans, chan_count)
204 These correspond to the appropriate image function, eg. IM_GPIX()
205 becomes i_gpix() or i_gpixf() as appropriate.
209 IM_SAMPLE_MAX - maximum value for a sample
213 IM_SAMPLE_T - type of a sample (i_sample_t or i_fsample_t)
217 IM_COLOR - color type, either i_color or i_fcolor.
221 IM_WORK_T - working sample type, either int or double.
225 IM_Sf - format string for the sample type, C<"%d"> or C<"%f">.
229 IM_Wf - format string for the work type, C<"%d"> or C<"%f">.
233 IM_SUFFIX(identifier) - adds _8 or _double onto the end of identifier.
237 IM_EIGHT_BIT - this is a macro defined only in 8-bit/sample code.
241 Other types, functions and values may be added in the future.
245 Tony Cook <tony@imager.perl.org>