]> git.imager.perl.org - imager.git/blob - lib/Imager/Preprocess.pm
0e03eaf17f80a8981f188517f086363cdd3e7119
[imager.git] / lib / Imager / Preprocess.pm
1 package Imager::Preprocess;
2 use strict;
3 require Exporter;
4 use vars qw(@ISA @EXPORT);
5
6 @EXPORT = qw(preprocess);
7 @ISA = qw(Exporter);
8
9
10 sub preprocess {
11   my $src = shift @ARGV;
12   my $dest = shift @ARGV
13     or usage();
14
15   open SRC, "< $src"
16   or die "Cannot open $src: $!\n";
17
18   my $cond;
19   my $cond_line;
20   my $save_code;
21   my @code;
22   my $code_line;
23   my @out;
24   my $failed;
25
26   push @out, 
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",
31     "#line 1 \"$src\"\n";
32   while (defined(my $line = <SRC>)) {
33     if ($line =~ /^\#code\s+(\S.+)$/) {
34       $save_code
35         and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
36       
37       $cond = $1;
38       $cond_line = $.;
39       $code_line = $. + 1;
40       $save_code = 1;
41     }
42     elsif ($line =~ /^\#code\s*$/) {
43       $save_code
44         and do { warn "$src:$code_line:Unclosed #code block\n"; ++$failed; };
45       
46       $cond = '';
47       $cond_line = 0;
48       $code_line = $. + 1;
49       $save_code = 1;
50     }
51     elsif ($line =~ /^\#\/code$/) {
52       $save_code
53         or do { warn "$src:$.:#/code without #code\n"; ++$failed; next; };
54       
55       if ($cond) {
56         push @out, "#line $cond_line \"$src\"\n";
57         push @out, "  if ($cond) {\n";
58       }
59       push @out,
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"
69         if $cond;
70       push @out, 
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);
78       push @out, "  }\n"
79         if $cond;
80       push @out, "#line ",$.+1," \"$src\"\n";
81       @code = ();
82       $save_code = 0;
83     }
84     elsif ($save_code) {
85       push @code, $line;
86     }
87     else {
88       push @out, $line;
89     }
90   }
91   
92   if ($save_code) {
93     warn "$src:$code_line:#code block not closed by EOF\n";
94     ++$failed;
95   }
96
97   close SRC;
98   
99   $failed 
100     and die "Errors during parsing, aborting\n";
101   
102   open DEST, "> $dest"
103     or die "Cannot open $dest: $!\n";
104   print DEST @out;
105   close DEST;
106 }
107   
108 sub byte_samples {
109   # important we make a copy
110   my @lines = @_;
111   
112   for (@lines) {
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;
123     s/\bIM_Sf\b/"%d"/g;
124     s/\bIM_Wf\b/"%d"/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;
131   }
132   
133   @lines;
134 }
135
136 sub double_samples {
137   # important we make a copy
138   my @lines = @_;
139   
140   for (@lines) {
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;
151     s/\bIM_Sf\b/"%f"/g;
152     s/\bIM_Wf\b/"%f"/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;
159   }
160
161   @lines;
162 }
163
164 1;
165
166 __END__
167
168 =head1 NAME
169
170 Imager::Preprocess - simple preprocessor for handling multiple sample sizes
171
172 =head1 SYNOPSIS
173
174   /* in the source: */
175   #code condition true to work with 8-bit samples
176   ... code using preprocessor types/values ...
177   #/code
178
179   perl -MImager -epreprocess foo.im foo.c
180
181 =head1 DESCRIPTION
182
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.
186
187 Imager's Makefile.PL currently scans the MANIFEST for .im files and
188 adds Makefile files to convert these to .c files.
189
190 The beginning of a sample-independent section of code is preceded by:
191
192   #code expression
193
194 where I<expression> should return true if processing should be done at
195 8-bits/sample.
196
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.
201
202 The end of a sample-independent section of code is terminated by:
203
204   #/code
205
206 #code sections cannot be nested.
207
208 #/code without a starting #code is an error.
209
210 The following types and values are defined in a #code section:
211
212 =over
213
214 =item *
215
216 IM_GPIX(im, x, y, &col)
217
218 =item *
219
220 IM_GLIN(im, l, r, y, colors)
221
222 =item *
223
224 IM_PPIX(im, x, y, &col)
225
226 =item *
227
228 IM_PLIN(im, x, y, colors)
229
230 =item *
231
232 IM_GSAMP(im, l, r, y, samples, chans, chan_count)
233
234 These correspond to the appropriate image function, eg. IM_GPIX()
235 becomes i_gpix() or i_gpixf() as appropriate.
236
237 =item *
238
239 IM_ADAPT_COLORS(dest_channes, src_channels, colors, count)
240
241 Call i_adapt_colors() or i_adapt_fcolors().
242
243 =item *
244
245 IM_FILL_COMBINE(fill) - retrieve the combine function from a fill
246 object.
247
248 =item *
249
250 IM_FILL_FILLER(fill) - retrieve the fill_with_* function from a fill
251 object.
252
253 =item *
254
255 IM_SAMPLE_MAX - maximum value for a sample
256
257 =item *
258
259 IM_SAMPLE_MAX2 - maximum value for a sample, squared
260
261 =item *
262
263 IM_SAMPLE_T - type of a sample (i_sample_t or i_fsample_t)
264
265 =item *
266
267 IM_COLOR - color type, either i_color or i_fcolor.
268
269 =item *
270
271 IM_WORK_T - working sample type, either int or double.
272
273 =item *
274
275 IM_Sf - format string for the sample type, C<"%d"> or C<"%f">.
276
277 =item *
278
279 IM_Wf - format string for the work type, C<"%d"> or C<"%f">.
280
281 =item *
282
283 IM_SUFFIX(identifier) - adds _8 or _double onto the end of identifier.
284
285 =item *
286
287 IM_EIGHT_BIT - this is a macro defined only in 8-bit/sample code.
288
289 =back
290
291 Other types, functions and values may be added in the future.
292
293 =head1 AUTHOR
294
295 Tony Cook <tony@imager.perl.org>
296
297 =cut