9 or die "Cannot open $src: $!\n";
19 push @out, "#line 1 \"$src\"\n";
20 while (defined(my $line = <SRC>)) {
21 if ($line =~ /^\#code\s+(\S.+)$/) {
23 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
30 elsif ($line =~ /^\#code\s*$/) {
32 and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
39 elsif ($line =~ /^\#\/code$/) {
41 or do { warn "$src:$.:#/code without #code\n"; ++$failed; next; };
44 push @out, "#line $cond_line \"$src\"\n";
45 push @out, " if ($cond) {\n";
47 push @out, "#undef IM_EIGHT_BIT\n",
48 "#define IM_EIGHT_BIT 1\n";
49 push @out, "#line $code_line \"$src\"\n";
50 push @out, byte_samples(@code);
51 push @out, " }\n", " else {\n"
53 push @out, "#undef IM_EIGHT_BIT\n";
54 push @out, "#line $code_line \"$src\"\n";
55 push @out, double_samples(@code);
58 push @out, "#line $. \"$src\"\n";
71 warn "$src:$code_line:#code block not closed by EOF\n";
78 and die "Errors during parsing, aborting\n";
81 or die "Cannot open $dest: $!\n";
86 # important we make a copy
90 s/\bIM_GPIX\b/i_gpix/g;
91 s/\bIM_GLIN\b/i_glin/g;
92 s/\bIM_PPIX\b/i_ppix/g;
93 s/\bIM_PLIN\b/i_plin/g;
94 s/\bIM_GSAMP\b/i_gsamp/g;
95 s/\bIM_SAMPLE_MAX\b/255/g;
96 s/\bIM_SAMPLE_T/i_sample_t/g;
97 s/\bIM_COLOR\b/i_color/g;
98 s/\bIM_WORK_T\b/int/g;
101 s/\bIM_SUFFIX\((\w+)\)/$1_8/g;
108 # important we make a copy
112 s/\bIM_GPIX\b/i_gpixf/g;
113 s/\bIM_GLIN\b/i_glinf/g;
114 s/\bIM_PPIX\b/i_ppixf/g;
115 s/\bIM_PLIN\b/i_plinf/g;
116 s/\bIM_GSAMP\b/i_gsampf/g;
117 s/\bIM_SAMPLE_MAX\b/1.0/g;
118 s/\bIM_SAMPLE_T/i_fsample_t/g;
119 s/\bIM_COLOR\b/i_fcolor/g;
120 s/\bIM_WORK_T\b/double/g;
123 s/\bIM_SUFFIX\((\w+)\)/$1_double/g;
131 imtoc.perl - simple preprocessor for handling multiple sample sizes
136 #code condition true to work with 8-bit samples
137 ... code using preprocessor types/values ...
140 perl imtoc.perl foo.im foo.c
144 This is a simple preprocessor that aims to reduce duplication of
145 source code when implementing an algorithm both for 8-bit samples and
146 double samples in Imager.
148 Imager's Makefile.PL currently scans the MANIFEST for .im files and
149 adds Makefile files to convert these to .c files.
151 The beginning of a sample-independent section of code is preceded by:
155 where I<expression> should return true if processing should be done at
158 You can also use a #code block around a function definition to produce
159 8-bit and double sample versions of a function. In this case #code
160 has no expression and you will need to use IM_SUFFIX() to produce
161 different function names.
163 The end of a sample-independent section of code is terminated by:
167 #code sections cannot be nested.
169 #/code without a starting #code is an error.
171 The following types and values are defined in a #code section:
177 IM_GPIX(im, x, y, &col)
181 IM_GLIN(im, l, r, y, colors)
185 IM_PPIX(im, x, y, &col)
189 IM_PLIN(im, x, y, colors)
193 IM_GSAMP(im, l, r, y, samples, chans, chan_count)
195 These correspond to the appropriate image function, eg. IM_GPIX()
196 becomes i_gpix() or i_gpixf() as appropriate.
200 IM_SAMPLE_MAX - maximum value for a sample
204 IM_SAMPLE_T - type of a sample (i_sample_t or i_fsample_t)
208 IM_COLOR - color type, either i_color or i_fcolor.
212 IM_WORK_T - working sample type, either int or double.
216 IM_Sf - format string for the sample type, C<"%d"> or C<"%f">.
220 IM_Wf - format string for the work type, C<"%d"> or C<"%f">.
224 IM_SUFFIX(identifier) - adds _8 or _double onto the end of identifier.
228 IM_EIGHT_BIT - this is a macro defined only in 8-bit/sample code.
232 Other types, functions and values may be added in the future.
236 Tony Cook <tony@imager.perl.org>